1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/types.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/ethtool.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/skbuff.h>
14 #include <linux/if_vlan.h>
15 #include <linux/if_bridge.h>
16 #include <linux/workqueue.h>
17 #include <linux/jiffies.h>
18 #include <linux/bitops.h>
19 #include <linux/list.h>
20 #include <linux/notifier.h>
21 #include <linux/dcbnl.h>
22 #include <linux/inetdevice.h>
23 #include <linux/netlink.h>
24 #include <net/switchdev.h>
25 #include <net/pkt_cls.h>
26 #include <net/tc_act/tc_mirred.h>
27 #include <net/netevent.h>
28 #include <net/tc_act/tc_sample.h>
29 #include <net/addrconf.h>
38 #include "spectrum_cnt.h"
39 #include "spectrum_dpipe.h"
40 #include "spectrum_acl_flex_actions.h"
41 #include "spectrum_span.h"
42 #include "../mlxfw/mlxfw.h"
44 #define MLXSW_SP_FWREV_MINOR_TO_BRANCH(minor) ((minor) / 100)
46 #define MLXSW_SP1_FWREV_MAJOR 13
47 #define MLXSW_SP1_FWREV_MINOR 1703
48 #define MLXSW_SP1_FWREV_SUBMINOR 4
49 #define MLXSW_SP1_FWREV_CAN_RESET_MINOR 1702
51 static const struct mlxsw_fw_rev mlxsw_sp1_fw_rev = {
52 .major = MLXSW_SP1_FWREV_MAJOR,
53 .minor = MLXSW_SP1_FWREV_MINOR,
54 .subminor = MLXSW_SP1_FWREV_SUBMINOR,
55 .can_reset_minor = MLXSW_SP1_FWREV_CAN_RESET_MINOR,
58 #define MLXSW_SP1_FW_FILENAME \
61 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
62 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
63 static const char mlxsw_sp_driver_version[] = "1.0";
69 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
72 * Packet control type.
73 * 0 - Ethernet control (e.g. EMADs, LACP)
76 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
79 * Packet protocol type. Must be set to 1 (Ethernet).
81 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
83 /* tx_hdr_rx_is_router
84 * Packet is sent from the router. Valid for data packets only.
86 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
89 * Indicates if the 'fid' field is valid and should be used for
90 * forwarding lookup. Valid for data packets only.
92 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
95 * Switch partition ID. Must be set to 0.
97 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
99 /* tx_hdr_control_tclass
100 * Indicates if the packet should use the control TClass and not one
101 * of the data TClasses.
103 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
106 * Egress TClass to be used on the egress device on the egress port.
108 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
111 * Destination local port for unicast packets.
112 * Destination multicast ID for multicast packets.
114 * Control packets are directed to a specific egress port, while data
115 * packets are transmitted through the CPU port (0) into the switch partition,
116 * where forwarding rules are applied.
118 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
121 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
122 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
123 * Valid for data packets only.
125 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
129 * 6 - Control packets
131 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
133 struct mlxsw_sp_mlxfw_dev {
134 struct mlxfw_dev mlxfw_dev;
135 struct mlxsw_sp *mlxsw_sp;
138 static int mlxsw_sp_component_query(struct mlxfw_dev *mlxfw_dev,
139 u16 component_index, u32 *p_max_size,
140 u8 *p_align_bits, u16 *p_max_write_size)
142 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
143 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
144 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
145 char mcqi_pl[MLXSW_REG_MCQI_LEN];
148 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
149 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcqi), mcqi_pl);
152 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
155 *p_align_bits = max_t(u8, *p_align_bits, 2);
156 *p_max_write_size = min_t(u16, *p_max_write_size,
157 MLXSW_REG_MCDA_MAX_DATA_LEN);
161 static int mlxsw_sp_fsm_lock(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle)
163 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
164 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
165 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
166 char mcc_pl[MLXSW_REG_MCC_LEN];
170 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
171 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
175 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
176 if (control_state != MLXFW_FSM_STATE_IDLE)
179 mlxsw_reg_mcc_pack(mcc_pl,
180 MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
182 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
185 static int mlxsw_sp_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
186 u32 fwhandle, u16 component_index,
189 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
190 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
191 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
192 char mcc_pl[MLXSW_REG_MCC_LEN];
194 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
195 component_index, fwhandle, component_size);
196 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
199 static int mlxsw_sp_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
200 u32 fwhandle, u8 *data, u16 size,
203 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
204 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
205 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
206 char mcda_pl[MLXSW_REG_MCDA_LEN];
208 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
209 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcda), mcda_pl);
212 static int mlxsw_sp_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
213 u32 fwhandle, u16 component_index)
215 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
216 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
217 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
218 char mcc_pl[MLXSW_REG_MCC_LEN];
220 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
221 component_index, fwhandle, 0);
222 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
225 static int mlxsw_sp_fsm_activate(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
227 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
228 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
230 char mcc_pl[MLXSW_REG_MCC_LEN];
232 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE, 0,
234 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
237 static int mlxsw_sp_fsm_query_state(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
238 enum mlxfw_fsm_state *fsm_state,
239 enum mlxfw_fsm_state_err *fsm_state_err)
241 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
242 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
243 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
244 char mcc_pl[MLXSW_REG_MCC_LEN];
249 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
250 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
254 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
255 *fsm_state = control_state;
256 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
257 MLXFW_FSM_STATE_ERR_MAX);
261 static void mlxsw_sp_fsm_cancel(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
263 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
264 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
265 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
266 char mcc_pl[MLXSW_REG_MCC_LEN];
268 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL, 0,
270 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
273 static void mlxsw_sp_fsm_release(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
275 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
276 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
277 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
278 char mcc_pl[MLXSW_REG_MCC_LEN];
280 mlxsw_reg_mcc_pack(mcc_pl,
281 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE, 0,
283 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
286 static const struct mlxfw_dev_ops mlxsw_sp_mlxfw_dev_ops = {
287 .component_query = mlxsw_sp_component_query,
288 .fsm_lock = mlxsw_sp_fsm_lock,
289 .fsm_component_update = mlxsw_sp_fsm_component_update,
290 .fsm_block_download = mlxsw_sp_fsm_block_download,
291 .fsm_component_verify = mlxsw_sp_fsm_component_verify,
292 .fsm_activate = mlxsw_sp_fsm_activate,
293 .fsm_query_state = mlxsw_sp_fsm_query_state,
294 .fsm_cancel = mlxsw_sp_fsm_cancel,
295 .fsm_release = mlxsw_sp_fsm_release
298 static int mlxsw_sp_firmware_flash(struct mlxsw_sp *mlxsw_sp,
299 const struct firmware *firmware)
301 struct mlxsw_sp_mlxfw_dev mlxsw_sp_mlxfw_dev = {
303 .ops = &mlxsw_sp_mlxfw_dev_ops,
304 .psid = mlxsw_sp->bus_info->psid,
305 .psid_size = strlen(mlxsw_sp->bus_info->psid),
311 mlxsw_core_fw_flash_start(mlxsw_sp->core);
312 err = mlxfw_firmware_flash(&mlxsw_sp_mlxfw_dev.mlxfw_dev, firmware);
313 mlxsw_core_fw_flash_end(mlxsw_sp->core);
318 static int mlxsw_sp_fw_rev_validate(struct mlxsw_sp *mlxsw_sp)
320 const struct mlxsw_fw_rev *rev = &mlxsw_sp->bus_info->fw_rev;
321 const struct mlxsw_fw_rev *req_rev = mlxsw_sp->req_rev;
322 const char *fw_filename = mlxsw_sp->fw_filename;
323 const struct firmware *firmware;
326 /* Don't check if driver does not require it */
327 if (!req_rev || !fw_filename)
330 /* Validate driver & FW are compatible */
331 if (rev->major != req_rev->major) {
332 WARN(1, "Mismatch in major FW version [%d:%d] is never expected; Please contact support\n",
333 rev->major, req_rev->major);
336 if (MLXSW_SP_FWREV_MINOR_TO_BRANCH(rev->minor) ==
337 MLXSW_SP_FWREV_MINOR_TO_BRANCH(req_rev->minor) &&
338 (rev->minor > req_rev->minor ||
339 (rev->minor == req_rev->minor &&
340 rev->subminor >= req_rev->subminor)))
343 dev_info(mlxsw_sp->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver\n",
344 rev->major, rev->minor, rev->subminor);
345 dev_info(mlxsw_sp->bus_info->dev, "Flashing firmware using file %s\n",
348 err = reject_firmware_direct(&firmware, fw_filename,
349 mlxsw_sp->bus_info->dev);
351 dev_err(mlxsw_sp->bus_info->dev, "Could not request firmware file %s\n",
356 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware);
357 release_firmware(firmware);
359 dev_err(mlxsw_sp->bus_info->dev, "Could not upgrade firmware\n");
361 /* On FW flash success, tell the caller FW reset is needed
362 * if current FW supports it.
364 if (rev->minor >= req_rev->can_reset_minor)
365 return err ? err : -EAGAIN;
370 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
371 unsigned int counter_index, u64 *packets,
374 char mgpc_pl[MLXSW_REG_MGPC_LEN];
377 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
378 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
379 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
383 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
385 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
389 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
390 unsigned int counter_index)
392 char mgpc_pl[MLXSW_REG_MGPC_LEN];
394 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
395 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
396 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
399 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
400 unsigned int *p_counter_index)
404 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
408 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
410 goto err_counter_clear;
414 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
419 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
420 unsigned int counter_index)
422 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
426 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
427 const struct mlxsw_tx_info *tx_info)
429 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
431 memset(txhdr, 0, MLXSW_TXHDR_LEN);
433 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
434 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
435 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
436 mlxsw_tx_hdr_swid_set(txhdr, 0);
437 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
438 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
439 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
442 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
445 case BR_STATE_FORWARDING:
446 return MLXSW_REG_SPMS_STATE_FORWARDING;
447 case BR_STATE_LEARNING:
448 return MLXSW_REG_SPMS_STATE_LEARNING;
449 case BR_STATE_LISTENING: /* fall-through */
450 case BR_STATE_DISABLED: /* fall-through */
451 case BR_STATE_BLOCKING:
452 return MLXSW_REG_SPMS_STATE_DISCARDING;
458 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
461 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
462 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
466 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
469 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
470 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
472 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
477 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
479 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
482 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
485 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
489 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
490 bool enable, u32 rate)
492 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
493 char mpsc_pl[MLXSW_REG_MPSC_LEN];
495 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
496 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
499 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
502 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
503 char paos_pl[MLXSW_REG_PAOS_LEN];
505 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
506 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
507 MLXSW_PORT_ADMIN_STATUS_DOWN);
508 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
511 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
514 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
515 char ppad_pl[MLXSW_REG_PPAD_LEN];
517 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
518 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
519 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
522 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
524 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
525 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
527 ether_addr_copy(addr, mlxsw_sp->base_mac);
528 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
529 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
532 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
534 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
535 char pmtu_pl[MLXSW_REG_PMTU_LEN];
539 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
540 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
541 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
544 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
549 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
550 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
553 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
555 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
556 char pspa_pl[MLXSW_REG_PSPA_LEN];
558 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
559 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
562 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
564 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
565 char svpe_pl[MLXSW_REG_SVPE_LEN];
567 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
568 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
571 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
574 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
578 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
581 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
583 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
588 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
591 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
592 char spvid_pl[MLXSW_REG_SPVID_LEN];
594 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
595 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
598 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
601 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
602 char spaft_pl[MLXSW_REG_SPAFT_LEN];
604 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
605 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
608 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
613 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
617 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
620 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
622 goto err_port_allow_untagged_set;
625 mlxsw_sp_port->pvid = vid;
628 err_port_allow_untagged_set:
629 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
634 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
636 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
637 char sspr_pl[MLXSW_REG_SSPR_LEN];
639 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
640 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
643 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
644 u8 local_port, u8 *p_module,
645 u8 *p_width, u8 *p_lane)
647 char pmlp_pl[MLXSW_REG_PMLP_LEN];
650 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
651 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
654 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
655 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
656 *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
660 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port,
661 u8 module, u8 width, u8 lane)
663 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
664 char pmlp_pl[MLXSW_REG_PMLP_LEN];
667 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
668 mlxsw_reg_pmlp_width_set(pmlp_pl, width);
669 for (i = 0; i < width; i++) {
670 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
671 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */
674 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
677 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
679 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
680 char pmlp_pl[MLXSW_REG_PMLP_LEN];
682 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
683 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
684 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
687 static int mlxsw_sp_port_open(struct net_device *dev)
689 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
692 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
695 netif_start_queue(dev);
699 static int mlxsw_sp_port_stop(struct net_device *dev)
701 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
703 netif_stop_queue(dev);
704 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
707 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
708 struct net_device *dev)
710 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
711 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
712 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
713 const struct mlxsw_tx_info tx_info = {
714 .local_port = mlxsw_sp_port->local_port,
720 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
721 return NETDEV_TX_BUSY;
723 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
724 struct sk_buff *skb_orig = skb;
726 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
728 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
729 dev_kfree_skb_any(skb_orig);
732 dev_consume_skb_any(skb_orig);
735 if (eth_skb_pad(skb)) {
736 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
740 mlxsw_sp_txhdr_construct(skb, &tx_info);
741 /* TX header is consumed by HW on the way so we shouldn't count its
742 * bytes as being sent.
744 len = skb->len - MLXSW_TXHDR_LEN;
746 /* Due to a race we might fail here because of a full queue. In that
747 * unlikely case we simply drop the packet.
749 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
752 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
753 u64_stats_update_begin(&pcpu_stats->syncp);
754 pcpu_stats->tx_packets++;
755 pcpu_stats->tx_bytes += len;
756 u64_stats_update_end(&pcpu_stats->syncp);
758 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
759 dev_kfree_skb_any(skb);
764 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
768 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
770 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
771 struct sockaddr *addr = p;
774 if (!is_valid_ether_addr(addr->sa_data))
775 return -EADDRNOTAVAIL;
777 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
780 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
784 static u16 mlxsw_sp_pg_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp,
787 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
790 #define MLXSW_SP_CELL_FACTOR 2 /* 2 * cell_size / (IPG + cell_size + 1) */
792 static u16 mlxsw_sp_pfc_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
795 delay = mlxsw_sp_bytes_cells(mlxsw_sp, DIV_ROUND_UP(delay,
797 return MLXSW_SP_CELL_FACTOR * delay + mlxsw_sp_bytes_cells(mlxsw_sp,
801 /* Maximum delay buffer needed in case of PAUSE frames, in bytes.
802 * Assumes 100m cable and maximum MTU.
804 #define MLXSW_SP_PAUSE_DELAY 58752
806 static u16 mlxsw_sp_pg_buf_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
807 u16 delay, bool pfc, bool pause)
810 return mlxsw_sp_pfc_delay_get(mlxsw_sp, mtu, delay);
812 return mlxsw_sp_bytes_cells(mlxsw_sp, MLXSW_SP_PAUSE_DELAY);
817 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres,
821 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
823 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
827 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
828 u8 *prio_tc, bool pause_en,
829 struct ieee_pfc *my_pfc)
831 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
832 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
833 u16 delay = !!my_pfc ? my_pfc->delay : 0;
834 char pbmc_pl[MLXSW_REG_PBMC_LEN];
837 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
838 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
842 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
843 bool configure = false;
849 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
850 if (prio_tc[j] == i) {
851 pfc = pfc_en & BIT(j);
860 lossy = !(pfc || pause_en);
861 thres_cells = mlxsw_sp_pg_buf_threshold_get(mlxsw_sp, mtu);
862 delay_cells = mlxsw_sp_pg_buf_delay_get(mlxsw_sp, mtu, delay,
864 mlxsw_sp_pg_buf_pack(pbmc_pl, i, thres_cells + delay_cells,
868 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
871 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
872 int mtu, bool pause_en)
874 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
875 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
876 struct ieee_pfc *my_pfc;
879 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
880 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
882 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
886 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
888 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
889 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
892 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
895 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
897 goto err_span_port_mtu_update;
898 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
900 goto err_port_mtu_set;
905 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
906 err_span_port_mtu_update:
907 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
912 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
913 struct rtnl_link_stats64 *stats)
915 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
916 struct mlxsw_sp_port_pcpu_stats *p;
917 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
922 for_each_possible_cpu(i) {
923 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
925 start = u64_stats_fetch_begin_irq(&p->syncp);
926 rx_packets = p->rx_packets;
927 rx_bytes = p->rx_bytes;
928 tx_packets = p->tx_packets;
929 tx_bytes = p->tx_bytes;
930 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
932 stats->rx_packets += rx_packets;
933 stats->rx_bytes += rx_bytes;
934 stats->tx_packets += tx_packets;
935 stats->tx_bytes += tx_bytes;
936 /* tx_dropped is u32, updated without syncp protection. */
937 tx_dropped += p->tx_dropped;
939 stats->tx_dropped = tx_dropped;
943 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
946 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
953 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
957 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
958 return mlxsw_sp_port_get_sw_stats64(dev, sp);
964 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
965 int prio, char *ppcnt_pl)
967 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
968 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
970 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
971 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
974 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
975 struct rtnl_link_stats64 *stats)
977 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
980 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
986 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
988 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
990 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
992 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
994 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
996 stats->rx_crc_errors =
997 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
998 stats->rx_frame_errors =
999 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
1001 stats->rx_length_errors = (
1002 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
1003 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
1004 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
1006 stats->rx_errors = (stats->rx_crc_errors +
1007 stats->rx_frame_errors + stats->rx_length_errors);
1014 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
1015 struct mlxsw_sp_port_xstats *xstats)
1017 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1020 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
1023 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
1025 for (i = 0; i < TC_MAX_QUEUE; i++) {
1026 err = mlxsw_sp_port_get_stats_raw(dev,
1027 MLXSW_REG_PPCNT_TC_CONG_TC,
1030 xstats->wred_drop[i] =
1031 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
1033 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
1038 xstats->backlog[i] =
1039 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1040 xstats->tail_drop[i] =
1041 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
1044 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1045 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
1050 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
1051 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
1055 static void update_stats_cache(struct work_struct *work)
1057 struct mlxsw_sp_port *mlxsw_sp_port =
1058 container_of(work, struct mlxsw_sp_port,
1059 periodic_hw_stats.update_dw.work);
1061 if (!netif_carrier_ok(mlxsw_sp_port->dev))
1062 /* Note: mlxsw_sp_port_down_wipe_counters() clears the cache as
1063 * necessary when port goes down.
1067 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
1068 &mlxsw_sp_port->periodic_hw_stats.stats);
1069 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
1070 &mlxsw_sp_port->periodic_hw_stats.xstats);
1073 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1074 MLXSW_HW_STATS_UPDATE_TIME);
1077 /* Return the stats from a cache that is updated periodically,
1078 * as this function might get called in an atomic context.
1081 mlxsw_sp_port_get_stats64(struct net_device *dev,
1082 struct rtnl_link_stats64 *stats)
1084 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1086 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
1089 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1090 u16 vid_begin, u16 vid_end,
1091 bool is_member, bool untagged)
1093 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1097 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1101 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1102 vid_end, is_member, untagged);
1103 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1108 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1109 u16 vid_end, bool is_member, bool untagged)
1114 for (vid = vid_begin; vid <= vid_end;
1115 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1116 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1119 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1120 is_member, untagged);
1128 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port)
1130 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1132 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1133 &mlxsw_sp_port->vlans_list, list)
1134 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1137 static struct mlxsw_sp_port_vlan *
1138 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1140 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1141 bool untagged = vid == 1;
1144 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1146 return ERR_PTR(err);
1148 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1149 if (!mlxsw_sp_port_vlan) {
1151 goto err_port_vlan_alloc;
1154 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1155 mlxsw_sp_port_vlan->ref_count = 1;
1156 mlxsw_sp_port_vlan->vid = vid;
1157 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1159 return mlxsw_sp_port_vlan;
1161 err_port_vlan_alloc:
1162 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1163 return ERR_PTR(err);
1167 mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1169 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1170 u16 vid = mlxsw_sp_port_vlan->vid;
1172 list_del(&mlxsw_sp_port_vlan->list);
1173 kfree(mlxsw_sp_port_vlan);
1174 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1177 struct mlxsw_sp_port_vlan *
1178 mlxsw_sp_port_vlan_get(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1180 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1182 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1183 if (mlxsw_sp_port_vlan) {
1184 mlxsw_sp_port_vlan->ref_count++;
1185 return mlxsw_sp_port_vlan;
1188 return mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid);
1191 void mlxsw_sp_port_vlan_put(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1193 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1195 if (--mlxsw_sp_port_vlan->ref_count != 0)
1198 if (mlxsw_sp_port_vlan->bridge_port)
1199 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1201 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1203 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1206 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1207 __be16 __always_unused proto, u16 vid)
1209 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1211 /* VLAN 0 is added to HW filter when device goes up, but it is
1212 * reserved in our case, so simply return.
1217 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid));
1220 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1221 __be16 __always_unused proto, u16 vid)
1223 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1224 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1226 /* VLAN 0 is removed from HW filter when device goes down, but
1227 * it is reserved in our case, so simply return.
1232 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1233 if (!mlxsw_sp_port_vlan)
1235 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1240 static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name,
1243 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1245 return mlxsw_core_port_get_phys_port_name(mlxsw_sp_port->mlxsw_sp->core,
1246 mlxsw_sp_port->local_port,
1250 static struct mlxsw_sp_port_mall_tc_entry *
1251 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1252 unsigned long cookie) {
1253 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1255 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1256 if (mall_tc_entry->cookie == cookie)
1257 return mall_tc_entry;
1263 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1264 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1265 const struct tc_action *a,
1268 enum mlxsw_sp_span_type span_type;
1269 struct net_device *to_dev;
1271 to_dev = tcf_mirred_dev(a);
1273 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1277 mirror->ingress = ingress;
1278 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1279 return mlxsw_sp_span_mirror_add(mlxsw_sp_port, to_dev, span_type,
1280 true, &mirror->span_id);
1284 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1285 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1287 enum mlxsw_sp_span_type span_type;
1289 span_type = mirror->ingress ?
1290 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1291 mlxsw_sp_span_mirror_del(mlxsw_sp_port, mirror->span_id,
1296 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1297 struct tc_cls_matchall_offload *cls,
1298 const struct tc_action *a,
1303 if (!mlxsw_sp_port->sample)
1305 if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1306 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1309 if (tcf_sample_rate(a) > MLXSW_REG_MPSC_RATE_MAX) {
1310 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1314 rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1315 tcf_sample_psample_group(a));
1316 mlxsw_sp_port->sample->truncate = tcf_sample_truncate(a);
1317 mlxsw_sp_port->sample->trunc_size = tcf_sample_trunc_size(a);
1318 mlxsw_sp_port->sample->rate = tcf_sample_rate(a);
1320 err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, tcf_sample_rate(a));
1322 goto err_port_sample_set;
1325 err_port_sample_set:
1326 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1331 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1333 if (!mlxsw_sp_port->sample)
1336 mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1337 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1340 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1341 struct tc_cls_matchall_offload *f,
1344 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1345 __be16 protocol = f->common.protocol;
1346 const struct tc_action *a;
1350 if (!tcf_exts_has_one_action(f->exts)) {
1351 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1355 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1358 mall_tc_entry->cookie = f->cookie;
1360 a = tcf_exts_first_action(f->exts);
1362 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1363 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1365 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1366 mirror = &mall_tc_entry->mirror;
1367 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1368 mirror, a, ingress);
1369 } else if (is_tcf_sample(a) && protocol == htons(ETH_P_ALL)) {
1370 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1371 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, f,
1378 goto err_add_action;
1380 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1384 kfree(mall_tc_entry);
1388 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1389 struct tc_cls_matchall_offload *f)
1391 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1393 mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1395 if (!mall_tc_entry) {
1396 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1399 list_del(&mall_tc_entry->list);
1401 switch (mall_tc_entry->type) {
1402 case MLXSW_SP_PORT_MALL_MIRROR:
1403 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1404 &mall_tc_entry->mirror);
1406 case MLXSW_SP_PORT_MALL_SAMPLE:
1407 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1413 kfree(mall_tc_entry);
1416 static int mlxsw_sp_setup_tc_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1417 struct tc_cls_matchall_offload *f,
1420 switch (f->command) {
1421 case TC_CLSMATCHALL_REPLACE:
1422 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, f,
1424 case TC_CLSMATCHALL_DESTROY:
1425 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, f);
1433 mlxsw_sp_setup_tc_cls_flower(struct mlxsw_sp_acl_block *acl_block,
1434 struct tc_cls_flower_offload *f)
1436 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_acl_block_mlxsw_sp(acl_block);
1438 switch (f->command) {
1439 case TC_CLSFLOWER_REPLACE:
1440 return mlxsw_sp_flower_replace(mlxsw_sp, acl_block, f);
1441 case TC_CLSFLOWER_DESTROY:
1442 mlxsw_sp_flower_destroy(mlxsw_sp, acl_block, f);
1444 case TC_CLSFLOWER_STATS:
1445 return mlxsw_sp_flower_stats(mlxsw_sp, acl_block, f);
1446 case TC_CLSFLOWER_TMPLT_CREATE:
1447 return mlxsw_sp_flower_tmplt_create(mlxsw_sp, acl_block, f);
1448 case TC_CLSFLOWER_TMPLT_DESTROY:
1449 mlxsw_sp_flower_tmplt_destroy(mlxsw_sp, acl_block, f);
1456 static int mlxsw_sp_setup_tc_block_cb_matchall(enum tc_setup_type type,
1458 void *cb_priv, bool ingress)
1460 struct mlxsw_sp_port *mlxsw_sp_port = cb_priv;
1463 case TC_SETUP_CLSMATCHALL:
1464 if (!tc_cls_can_offload_and_chain0(mlxsw_sp_port->dev,
1468 return mlxsw_sp_setup_tc_cls_matchall(mlxsw_sp_port, type_data,
1470 case TC_SETUP_CLSFLOWER:
1477 static int mlxsw_sp_setup_tc_block_cb_matchall_ig(enum tc_setup_type type,
1481 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1485 static int mlxsw_sp_setup_tc_block_cb_matchall_eg(enum tc_setup_type type,
1489 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1493 static int mlxsw_sp_setup_tc_block_cb_flower(enum tc_setup_type type,
1494 void *type_data, void *cb_priv)
1496 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1499 case TC_SETUP_CLSMATCHALL:
1501 case TC_SETUP_CLSFLOWER:
1502 if (mlxsw_sp_acl_block_disabled(acl_block))
1505 return mlxsw_sp_setup_tc_cls_flower(acl_block, type_data);
1512 mlxsw_sp_setup_tc_block_flower_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1513 struct tcf_block *block, bool ingress,
1514 struct netlink_ext_ack *extack)
1516 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1517 struct mlxsw_sp_acl_block *acl_block;
1518 struct tcf_block_cb *block_cb;
1521 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1524 acl_block = mlxsw_sp_acl_block_create(mlxsw_sp, block->net);
1527 block_cb = __tcf_block_cb_register(block,
1528 mlxsw_sp_setup_tc_block_cb_flower,
1529 mlxsw_sp, acl_block, extack);
1530 if (IS_ERR(block_cb)) {
1531 err = PTR_ERR(block_cb);
1532 goto err_cb_register;
1535 acl_block = tcf_block_cb_priv(block_cb);
1537 tcf_block_cb_incref(block_cb);
1538 err = mlxsw_sp_acl_block_bind(mlxsw_sp, acl_block,
1539 mlxsw_sp_port, ingress);
1541 goto err_block_bind;
1544 mlxsw_sp_port->ing_acl_block = acl_block;
1546 mlxsw_sp_port->eg_acl_block = acl_block;
1551 if (!tcf_block_cb_decref(block_cb)) {
1552 __tcf_block_cb_unregister(block, block_cb);
1554 mlxsw_sp_acl_block_destroy(acl_block);
1560 mlxsw_sp_setup_tc_block_flower_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1561 struct tcf_block *block, bool ingress)
1563 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1564 struct mlxsw_sp_acl_block *acl_block;
1565 struct tcf_block_cb *block_cb;
1568 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1574 mlxsw_sp_port->ing_acl_block = NULL;
1576 mlxsw_sp_port->eg_acl_block = NULL;
1578 acl_block = tcf_block_cb_priv(block_cb);
1579 err = mlxsw_sp_acl_block_unbind(mlxsw_sp, acl_block,
1580 mlxsw_sp_port, ingress);
1581 if (!err && !tcf_block_cb_decref(block_cb)) {
1582 __tcf_block_cb_unregister(block, block_cb);
1583 mlxsw_sp_acl_block_destroy(acl_block);
1587 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1588 struct tc_block_offload *f)
1594 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1595 cb = mlxsw_sp_setup_tc_block_cb_matchall_ig;
1597 } else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1598 cb = mlxsw_sp_setup_tc_block_cb_matchall_eg;
1604 switch (f->command) {
1606 err = tcf_block_cb_register(f->block, cb, mlxsw_sp_port,
1607 mlxsw_sp_port, f->extack);
1610 err = mlxsw_sp_setup_tc_block_flower_bind(mlxsw_sp_port,
1614 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1618 case TC_BLOCK_UNBIND:
1619 mlxsw_sp_setup_tc_block_flower_unbind(mlxsw_sp_port,
1621 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1628 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1631 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1634 case TC_SETUP_BLOCK:
1635 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1636 case TC_SETUP_QDISC_RED:
1637 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1638 case TC_SETUP_QDISC_PRIO:
1639 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1646 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1648 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1651 if (mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->ing_acl_block) ||
1652 mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->eg_acl_block) ||
1653 !list_empty(&mlxsw_sp_port->mall_tc_list)) {
1654 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1657 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->ing_acl_block);
1658 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->eg_acl_block);
1660 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->ing_acl_block);
1661 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->eg_acl_block);
1666 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1668 static int mlxsw_sp_handle_feature(struct net_device *dev,
1669 netdev_features_t wanted_features,
1670 netdev_features_t feature,
1671 mlxsw_sp_feature_handler feature_handler)
1673 netdev_features_t changes = wanted_features ^ dev->features;
1674 bool enable = !!(wanted_features & feature);
1677 if (!(changes & feature))
1680 err = feature_handler(dev, enable);
1682 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1683 enable ? "Enable" : "Disable", &feature, err);
1688 dev->features |= feature;
1690 dev->features &= ~feature;
1694 static int mlxsw_sp_set_features(struct net_device *dev,
1695 netdev_features_t features)
1697 return mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1698 mlxsw_sp_feature_hw_tc);
1701 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1702 .ndo_open = mlxsw_sp_port_open,
1703 .ndo_stop = mlxsw_sp_port_stop,
1704 .ndo_start_xmit = mlxsw_sp_port_xmit,
1705 .ndo_setup_tc = mlxsw_sp_setup_tc,
1706 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1707 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1708 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1709 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1710 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1711 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1712 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1713 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1714 .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name,
1715 .ndo_set_features = mlxsw_sp_set_features,
1718 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1719 struct ethtool_drvinfo *drvinfo)
1721 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1722 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1724 strlcpy(drvinfo->driver, mlxsw_sp->bus_info->device_kind,
1725 sizeof(drvinfo->driver));
1726 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1727 sizeof(drvinfo->version));
1728 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1730 mlxsw_sp->bus_info->fw_rev.major,
1731 mlxsw_sp->bus_info->fw_rev.minor,
1732 mlxsw_sp->bus_info->fw_rev.subminor);
1733 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1734 sizeof(drvinfo->bus_info));
1737 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1738 struct ethtool_pauseparam *pause)
1740 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1742 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1743 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1746 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1747 struct ethtool_pauseparam *pause)
1749 char pfcc_pl[MLXSW_REG_PFCC_LEN];
1751 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1752 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
1753 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
1755 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
1759 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
1760 struct ethtool_pauseparam *pause)
1762 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1763 bool pause_en = pause->tx_pause || pause->rx_pause;
1766 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
1767 netdev_err(dev, "PFC already enabled on port\n");
1771 if (pause->autoneg) {
1772 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
1776 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1778 netdev_err(dev, "Failed to configure port's headroom\n");
1782 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
1784 netdev_err(dev, "Failed to set PAUSE parameters\n");
1785 goto err_port_pause_configure;
1788 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
1789 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
1793 err_port_pause_configure:
1794 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1795 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1799 struct mlxsw_sp_port_hw_stats {
1800 char str[ETH_GSTRING_LEN];
1801 u64 (*getter)(const char *payload);
1805 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
1807 .str = "a_frames_transmitted_ok",
1808 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
1811 .str = "a_frames_received_ok",
1812 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
1815 .str = "a_frame_check_sequence_errors",
1816 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
1819 .str = "a_alignment_errors",
1820 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
1823 .str = "a_octets_transmitted_ok",
1824 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
1827 .str = "a_octets_received_ok",
1828 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
1831 .str = "a_multicast_frames_xmitted_ok",
1832 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
1835 .str = "a_broadcast_frames_xmitted_ok",
1836 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
1839 .str = "a_multicast_frames_received_ok",
1840 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
1843 .str = "a_broadcast_frames_received_ok",
1844 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
1847 .str = "a_in_range_length_errors",
1848 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
1851 .str = "a_out_of_range_length_field",
1852 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
1855 .str = "a_frame_too_long_errors",
1856 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
1859 .str = "a_symbol_error_during_carrier",
1860 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
1863 .str = "a_mac_control_frames_transmitted",
1864 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
1867 .str = "a_mac_control_frames_received",
1868 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
1871 .str = "a_unsupported_opcodes_received",
1872 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
1875 .str = "a_pause_mac_ctrl_frames_received",
1876 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
1879 .str = "a_pause_mac_ctrl_frames_xmitted",
1880 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
1884 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
1886 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2819_stats[] = {
1888 .str = "ether_pkts64octets",
1889 .getter = mlxsw_reg_ppcnt_ether_stats_pkts64octets_get,
1892 .str = "ether_pkts65to127octets",
1893 .getter = mlxsw_reg_ppcnt_ether_stats_pkts65to127octets_get,
1896 .str = "ether_pkts128to255octets",
1897 .getter = mlxsw_reg_ppcnt_ether_stats_pkts128to255octets_get,
1900 .str = "ether_pkts256to511octets",
1901 .getter = mlxsw_reg_ppcnt_ether_stats_pkts256to511octets_get,
1904 .str = "ether_pkts512to1023octets",
1905 .getter = mlxsw_reg_ppcnt_ether_stats_pkts512to1023octets_get,
1908 .str = "ether_pkts1024to1518octets",
1909 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1024to1518octets_get,
1912 .str = "ether_pkts1519to2047octets",
1913 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1519to2047octets_get,
1916 .str = "ether_pkts2048to4095octets",
1917 .getter = mlxsw_reg_ppcnt_ether_stats_pkts2048to4095octets_get,
1920 .str = "ether_pkts4096to8191octets",
1921 .getter = mlxsw_reg_ppcnt_ether_stats_pkts4096to8191octets_get,
1924 .str = "ether_pkts8192to10239octets",
1925 .getter = mlxsw_reg_ppcnt_ether_stats_pkts8192to10239octets_get,
1929 #define MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN \
1930 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2819_stats)
1932 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
1934 .str = "rx_octets_prio",
1935 .getter = mlxsw_reg_ppcnt_rx_octets_get,
1938 .str = "rx_frames_prio",
1939 .getter = mlxsw_reg_ppcnt_rx_frames_get,
1942 .str = "tx_octets_prio",
1943 .getter = mlxsw_reg_ppcnt_tx_octets_get,
1946 .str = "tx_frames_prio",
1947 .getter = mlxsw_reg_ppcnt_tx_frames_get,
1950 .str = "rx_pause_prio",
1951 .getter = mlxsw_reg_ppcnt_rx_pause_get,
1954 .str = "rx_pause_duration_prio",
1955 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
1958 .str = "tx_pause_prio",
1959 .getter = mlxsw_reg_ppcnt_tx_pause_get,
1962 .str = "tx_pause_duration_prio",
1963 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
1967 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
1969 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
1971 .str = "tc_transmit_queue_tc",
1972 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_get,
1973 .cells_bytes = true,
1976 .str = "tc_no_buffer_discard_uc_tc",
1977 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
1981 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
1983 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
1984 MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN + \
1985 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN * \
1986 IEEE_8021QAZ_MAX_TCS) + \
1987 (MLXSW_SP_PORT_HW_TC_STATS_LEN * \
1990 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
1994 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
1995 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
1996 mlxsw_sp_port_hw_prio_stats[i].str, prio);
1997 *p += ETH_GSTRING_LEN;
2001 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
2005 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
2006 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2007 mlxsw_sp_port_hw_tc_stats[i].str, tc);
2008 *p += ETH_GSTRING_LEN;
2012 static void mlxsw_sp_port_get_strings(struct net_device *dev,
2013 u32 stringset, u8 *data)
2018 switch (stringset) {
2020 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
2021 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
2023 p += ETH_GSTRING_LEN;
2025 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN; i++) {
2026 memcpy(p, mlxsw_sp_port_hw_rfc_2819_stats[i].str,
2028 p += ETH_GSTRING_LEN;
2031 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2032 mlxsw_sp_port_get_prio_strings(&p, i);
2034 for (i = 0; i < TC_MAX_QUEUE; i++)
2035 mlxsw_sp_port_get_tc_strings(&p, i);
2041 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
2042 enum ethtool_phys_id_state state)
2044 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2045 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2046 char mlcr_pl[MLXSW_REG_MLCR_LEN];
2050 case ETHTOOL_ID_ACTIVE:
2053 case ETHTOOL_ID_INACTIVE:
2060 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
2061 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
2065 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
2066 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
2069 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
2070 *p_hw_stats = mlxsw_sp_port_hw_stats;
2071 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
2073 case MLXSW_REG_PPCNT_RFC_2819_CNT:
2074 *p_hw_stats = mlxsw_sp_port_hw_rfc_2819_stats;
2075 *p_len = MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2077 case MLXSW_REG_PPCNT_PRIO_CNT:
2078 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
2079 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2081 case MLXSW_REG_PPCNT_TC_CNT:
2082 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
2083 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
2092 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
2093 enum mlxsw_reg_ppcnt_grp grp, int prio,
2094 u64 *data, int data_index)
2096 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2097 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2098 struct mlxsw_sp_port_hw_stats *hw_stats;
2099 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
2103 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
2106 mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
2107 for (i = 0; i < len; i++) {
2108 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
2109 if (!hw_stats[i].cells_bytes)
2111 data[data_index + i] = mlxsw_sp_cells_bytes(mlxsw_sp,
2112 data[data_index + i]);
2116 static void mlxsw_sp_port_get_stats(struct net_device *dev,
2117 struct ethtool_stats *stats, u64 *data)
2119 int i, data_index = 0;
2121 /* IEEE 802.3 Counters */
2122 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
2124 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
2126 /* RFC 2819 Counters */
2127 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2819_CNT, 0,
2129 data_index += MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2131 /* Per-Priority Counters */
2132 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2133 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
2135 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2138 /* Per-TC Counters */
2139 for (i = 0; i < TC_MAX_QUEUE; i++) {
2140 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
2142 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
2146 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
2150 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
2156 struct mlxsw_sp_port_link_mode {
2157 enum ethtool_link_mode_bit_indices mask_ethtool;
2162 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = {
2164 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
2165 .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2169 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
2170 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
2171 .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2172 .speed = SPEED_1000,
2175 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
2176 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2177 .speed = SPEED_10000,
2180 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
2181 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
2182 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
2183 .speed = SPEED_10000,
2186 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2187 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2188 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2189 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
2190 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2191 .speed = SPEED_10000,
2194 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
2195 .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
2196 .speed = SPEED_20000,
2199 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
2200 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2201 .speed = SPEED_40000,
2204 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
2205 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2206 .speed = SPEED_40000,
2209 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
2210 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2211 .speed = SPEED_40000,
2214 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
2215 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2216 .speed = SPEED_40000,
2219 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
2220 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2221 .speed = SPEED_25000,
2224 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
2225 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2226 .speed = SPEED_25000,
2229 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2230 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2231 .speed = SPEED_25000,
2234 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2235 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2236 .speed = SPEED_25000,
2239 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
2240 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2241 .speed = SPEED_50000,
2244 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
2245 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2246 .speed = SPEED_50000,
2249 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
2250 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2251 .speed = SPEED_50000,
2254 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2255 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT,
2256 .speed = SPEED_56000,
2259 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2260 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT,
2261 .speed = SPEED_56000,
2264 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2265 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT,
2266 .speed = SPEED_56000,
2269 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2270 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT,
2271 .speed = SPEED_56000,
2274 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
2275 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2276 .speed = SPEED_100000,
2279 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
2280 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2281 .speed = SPEED_100000,
2284 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
2285 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2286 .speed = SPEED_100000,
2289 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
2290 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2291 .speed = SPEED_100000,
2295 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
2298 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto,
2299 struct ethtool_link_ksettings *cmd)
2301 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2302 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2303 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2304 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2305 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2306 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2307 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2309 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2310 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2311 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2312 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
2313 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
2314 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2317 static void mlxsw_sp_from_ptys_link(u32 ptys_eth_proto, unsigned long *mode)
2321 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2322 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
2323 __set_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
2328 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
2329 struct ethtool_link_ksettings *cmd)
2331 u32 speed = SPEED_UNKNOWN;
2332 u8 duplex = DUPLEX_UNKNOWN;
2338 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2339 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) {
2340 speed = mlxsw_sp_port_link_mode[i].speed;
2341 duplex = DUPLEX_FULL;
2346 cmd->base.speed = speed;
2347 cmd->base.duplex = duplex;
2350 static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto)
2352 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2353 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2354 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2355 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2358 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2359 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2360 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
2363 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2364 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2365 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2366 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
2373 mlxsw_sp_to_ptys_advert_link(const struct ethtool_link_ksettings *cmd)
2378 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2379 if (test_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
2380 cmd->link_modes.advertising))
2381 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2386 static u32 mlxsw_sp_to_ptys_speed(u32 speed)
2391 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2392 if (speed == mlxsw_sp_port_link_mode[i].speed)
2393 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2398 static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed)
2403 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2404 if (mlxsw_sp_port_link_mode[i].speed <= upper_speed)
2405 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2410 static void mlxsw_sp_port_get_link_supported(u32 eth_proto_cap,
2411 struct ethtool_link_ksettings *cmd)
2413 ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
2414 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
2415 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
2417 mlxsw_sp_from_ptys_supported_port(eth_proto_cap, cmd);
2418 mlxsw_sp_from_ptys_link(eth_proto_cap, cmd->link_modes.supported);
2421 static void mlxsw_sp_port_get_link_advertise(u32 eth_proto_admin, bool autoneg,
2422 struct ethtool_link_ksettings *cmd)
2427 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
2428 mlxsw_sp_from_ptys_link(eth_proto_admin, cmd->link_modes.advertising);
2432 mlxsw_sp_port_get_link_lp_advertise(u32 eth_proto_lp, u8 autoneg_status,
2433 struct ethtool_link_ksettings *cmd)
2435 if (autoneg_status != MLXSW_REG_PTYS_AN_STATUS_OK || !eth_proto_lp)
2438 ethtool_link_ksettings_add_link_mode(cmd, lp_advertising, Autoneg);
2439 mlxsw_sp_from_ptys_link(eth_proto_lp, cmd->link_modes.lp_advertising);
2442 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
2443 struct ethtool_link_ksettings *cmd)
2445 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper, eth_proto_lp;
2446 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2447 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2448 char ptys_pl[MLXSW_REG_PTYS_LEN];
2453 autoneg = mlxsw_sp_port->link.autoneg;
2454 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0, false);
2455 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2458 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, ð_proto_admin,
2461 mlxsw_sp_port_get_link_supported(eth_proto_cap, cmd);
2463 mlxsw_sp_port_get_link_advertise(eth_proto_admin, autoneg, cmd);
2465 eth_proto_lp = mlxsw_reg_ptys_eth_proto_lp_advertise_get(ptys_pl);
2466 autoneg_status = mlxsw_reg_ptys_an_status_get(ptys_pl);
2467 mlxsw_sp_port_get_link_lp_advertise(eth_proto_lp, autoneg_status, cmd);
2469 cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2470 cmd->base.port = mlxsw_sp_port_connector_port(eth_proto_oper);
2471 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev), eth_proto_oper,
2478 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
2479 const struct ethtool_link_ksettings *cmd)
2481 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2482 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2483 char ptys_pl[MLXSW_REG_PTYS_LEN];
2484 u32 eth_proto_cap, eth_proto_new;
2488 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0, false);
2489 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2492 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, NULL, NULL);
2494 autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
2495 if (!autoneg && cmd->base.speed == SPEED_56000) {
2496 netdev_err(dev, "56G not supported with autoneg off\n");
2499 eth_proto_new = autoneg ?
2500 mlxsw_sp_to_ptys_advert_link(cmd) :
2501 mlxsw_sp_to_ptys_speed(cmd->base.speed);
2503 eth_proto_new = eth_proto_new & eth_proto_cap;
2504 if (!eth_proto_new) {
2505 netdev_err(dev, "No supported speed requested\n");
2509 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2510 eth_proto_new, autoneg);
2511 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2515 mlxsw_sp_port->link.autoneg = autoneg;
2517 if (!netif_running(dev))
2520 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2521 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
2526 static int mlxsw_sp_flash_device(struct net_device *dev,
2527 struct ethtool_flash *flash)
2529 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2530 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2531 const struct firmware *firmware;
2534 if (flash->region != ETHTOOL_FLASH_ALL_REGIONS)
2540 err = request_firmware_direct(&firmware, flash->data, &dev->dev);
2543 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware);
2544 release_firmware(firmware);
2551 #define MLXSW_SP_I2C_ADDR_LOW 0x50
2552 #define MLXSW_SP_I2C_ADDR_HIGH 0x51
2553 #define MLXSW_SP_EEPROM_PAGE_LENGTH 256
2555 static int mlxsw_sp_query_module_eeprom(struct mlxsw_sp_port *mlxsw_sp_port,
2556 u16 offset, u16 size, void *data,
2557 unsigned int *p_read_size)
2559 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2560 char eeprom_tmp[MLXSW_SP_REG_MCIA_EEPROM_SIZE];
2561 char mcia_pl[MLXSW_REG_MCIA_LEN];
2566 size = min_t(u16, size, MLXSW_SP_REG_MCIA_EEPROM_SIZE);
2568 if (offset < MLXSW_SP_EEPROM_PAGE_LENGTH &&
2569 offset + size > MLXSW_SP_EEPROM_PAGE_LENGTH)
2570 /* Cross pages read, read until offset 256 in low page */
2571 size = MLXSW_SP_EEPROM_PAGE_LENGTH - offset;
2573 i2c_addr = MLXSW_SP_I2C_ADDR_LOW;
2574 if (offset >= MLXSW_SP_EEPROM_PAGE_LENGTH) {
2575 i2c_addr = MLXSW_SP_I2C_ADDR_HIGH;
2576 offset -= MLXSW_SP_EEPROM_PAGE_LENGTH;
2579 mlxsw_reg_mcia_pack(mcia_pl, mlxsw_sp_port->mapping.module,
2580 0, 0, offset, size, i2c_addr);
2582 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcia), mcia_pl);
2586 status = mlxsw_reg_mcia_status_get(mcia_pl);
2590 mlxsw_reg_mcia_eeprom_memcpy_from(mcia_pl, eeprom_tmp);
2591 memcpy(data, eeprom_tmp, size);
2592 *p_read_size = size;
2597 enum mlxsw_sp_eeprom_module_info_rev_id {
2598 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_UNSPC = 0x00,
2599 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8436 = 0x01,
2600 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8636 = 0x03,
2603 enum mlxsw_sp_eeprom_module_info_id {
2604 MLXSW_SP_EEPROM_MODULE_INFO_ID_SFP = 0x03,
2605 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP = 0x0C,
2606 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP_PLUS = 0x0D,
2607 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28 = 0x11,
2610 enum mlxsw_sp_eeprom_module_info {
2611 MLXSW_SP_EEPROM_MODULE_INFO_ID,
2612 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID,
2613 MLXSW_SP_EEPROM_MODULE_INFO_SIZE,
2616 static int mlxsw_sp_get_module_info(struct net_device *netdev,
2617 struct ethtool_modinfo *modinfo)
2619 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
2620 u8 module_info[MLXSW_SP_EEPROM_MODULE_INFO_SIZE];
2621 u8 module_rev_id, module_id;
2622 unsigned int read_size;
2625 err = mlxsw_sp_query_module_eeprom(mlxsw_sp_port, 0,
2626 MLXSW_SP_EEPROM_MODULE_INFO_SIZE,
2627 module_info, &read_size);
2631 if (read_size < MLXSW_SP_EEPROM_MODULE_INFO_SIZE)
2634 module_rev_id = module_info[MLXSW_SP_EEPROM_MODULE_INFO_REV_ID];
2635 module_id = module_info[MLXSW_SP_EEPROM_MODULE_INFO_ID];
2637 switch (module_id) {
2638 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP:
2639 modinfo->type = ETH_MODULE_SFF_8436;
2640 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2642 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP_PLUS:
2643 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28:
2644 if (module_id == MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28 ||
2645 module_rev_id >= MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8636) {
2646 modinfo->type = ETH_MODULE_SFF_8636;
2647 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2649 modinfo->type = ETH_MODULE_SFF_8436;
2650 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2653 case MLXSW_SP_EEPROM_MODULE_INFO_ID_SFP:
2654 modinfo->type = ETH_MODULE_SFF_8472;
2655 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2664 static int mlxsw_sp_get_module_eeprom(struct net_device *netdev,
2665 struct ethtool_eeprom *ee,
2668 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
2669 int offset = ee->offset;
2670 unsigned int read_size;
2677 memset(data, 0, ee->len);
2679 while (i < ee->len) {
2680 err = mlxsw_sp_query_module_eeprom(mlxsw_sp_port, offset,
2681 ee->len - i, data + i,
2684 netdev_err(mlxsw_sp_port->dev, "Eeprom query failed\n");
2689 offset += read_size;
2695 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
2696 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
2697 .get_link = ethtool_op_get_link,
2698 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
2699 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
2700 .get_strings = mlxsw_sp_port_get_strings,
2701 .set_phys_id = mlxsw_sp_port_set_phys_id,
2702 .get_ethtool_stats = mlxsw_sp_port_get_stats,
2703 .get_sset_count = mlxsw_sp_port_get_sset_count,
2704 .get_link_ksettings = mlxsw_sp_port_get_link_ksettings,
2705 .set_link_ksettings = mlxsw_sp_port_set_link_ksettings,
2706 .flash_device = mlxsw_sp_flash_device,
2707 .get_module_info = mlxsw_sp_get_module_info,
2708 .get_module_eeprom = mlxsw_sp_get_module_eeprom,
2712 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
2714 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2715 u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width;
2716 char ptys_pl[MLXSW_REG_PTYS_LEN];
2717 u32 eth_proto_admin;
2719 eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed);
2720 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2721 eth_proto_admin, mlxsw_sp_port->link.autoneg);
2722 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2725 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
2726 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
2727 bool dwrr, u8 dwrr_weight)
2729 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2730 char qeec_pl[MLXSW_REG_QEEC_LEN];
2732 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
2734 mlxsw_reg_qeec_de_set(qeec_pl, true);
2735 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
2736 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
2737 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
2740 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
2741 enum mlxsw_reg_qeec_hr hr, u8 index,
2742 u8 next_index, u32 maxrate)
2744 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2745 char qeec_pl[MLXSW_REG_QEEC_LEN];
2747 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
2749 mlxsw_reg_qeec_mase_set(qeec_pl, true);
2750 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
2751 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
2754 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
2755 enum mlxsw_reg_qeec_hr hr, u8 index,
2756 u8 next_index, u32 minrate)
2758 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2759 char qeec_pl[MLXSW_REG_QEEC_LEN];
2761 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
2763 mlxsw_reg_qeec_mise_set(qeec_pl, true);
2764 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
2766 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
2769 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
2770 u8 switch_prio, u8 tclass)
2772 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2773 char qtct_pl[MLXSW_REG_QTCT_LEN];
2775 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
2777 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
2780 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
2784 /* Setup the elements hierarcy, so that each TC is linked to
2785 * one subgroup, which are all member in the same group.
2787 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2788 MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
2792 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2793 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2794 MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
2799 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2800 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2801 MLXSW_REG_QEEC_HIERARCY_TC, i, i,
2806 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2807 MLXSW_REG_QEEC_HIERARCY_TC,
2814 /* Make sure the max shaper is disabled in all hierarchies that
2817 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2818 MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
2819 MLXSW_REG_QEEC_MAS_DIS);
2822 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2823 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2824 MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
2826 MLXSW_REG_QEEC_MAS_DIS);
2830 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2831 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2832 MLXSW_REG_QEEC_HIERARCY_TC,
2834 MLXSW_REG_QEEC_MAS_DIS);
2838 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2839 MLXSW_REG_QEEC_HIERARCY_TC,
2841 MLXSW_REG_QEEC_MAS_DIS);
2846 /* Configure the min shaper for multicast TCs. */
2847 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2848 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
2849 MLXSW_REG_QEEC_HIERARCY_TC,
2851 MLXSW_REG_QEEC_MIS_MIN);
2856 /* Map all priorities to traffic class 0. */
2857 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2858 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
2866 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
2869 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2870 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
2872 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
2873 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
2876 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2877 bool split, u8 module, u8 width, u8 lane)
2879 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2880 struct mlxsw_sp_port *mlxsw_sp_port;
2881 struct net_device *dev;
2884 err = mlxsw_core_port_init(mlxsw_sp->core, local_port);
2886 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
2891 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
2894 goto err_alloc_etherdev;
2896 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
2897 mlxsw_sp_port = netdev_priv(dev);
2898 mlxsw_sp_port->dev = dev;
2899 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
2900 mlxsw_sp_port->local_port = local_port;
2901 mlxsw_sp_port->pvid = 1;
2902 mlxsw_sp_port->split = split;
2903 mlxsw_sp_port->mapping.module = module;
2904 mlxsw_sp_port->mapping.width = width;
2905 mlxsw_sp_port->mapping.lane = lane;
2906 mlxsw_sp_port->link.autoneg = 1;
2907 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
2908 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
2910 mlxsw_sp_port->pcpu_stats =
2911 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
2912 if (!mlxsw_sp_port->pcpu_stats) {
2914 goto err_alloc_stats;
2917 mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
2919 if (!mlxsw_sp_port->sample) {
2921 goto err_alloc_sample;
2924 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
2925 &update_stats_cache);
2927 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
2928 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
2930 err = mlxsw_sp_port_module_map(mlxsw_sp_port, module, width, lane);
2932 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
2933 mlxsw_sp_port->local_port);
2934 goto err_port_module_map;
2937 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
2939 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
2940 mlxsw_sp_port->local_port);
2941 goto err_port_swid_set;
2944 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
2946 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
2947 mlxsw_sp_port->local_port);
2948 goto err_dev_addr_init;
2951 netif_carrier_off(dev);
2953 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
2954 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
2955 dev->hw_features |= NETIF_F_HW_TC;
2958 dev->max_mtu = ETH_MAX_MTU;
2960 /* Each packet needs to have a Tx header (metadata) on top all other
2963 dev->needed_headroom = MLXSW_TXHDR_LEN;
2965 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
2967 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
2968 mlxsw_sp_port->local_port);
2969 goto err_port_system_port_mapping_set;
2972 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
2974 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
2975 mlxsw_sp_port->local_port);
2976 goto err_port_speed_by_width_set;
2979 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
2981 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
2982 mlxsw_sp_port->local_port);
2983 goto err_port_mtu_set;
2986 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2988 goto err_port_admin_status_set;
2990 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
2992 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
2993 mlxsw_sp_port->local_port);
2994 goto err_port_buffers_init;
2997 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
2999 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
3000 mlxsw_sp_port->local_port);
3001 goto err_port_ets_init;
3004 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
3006 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
3007 mlxsw_sp_port->local_port);
3008 goto err_port_tc_mc_mode;
3011 /* ETS and buffers must be initialized before DCB. */
3012 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
3014 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
3015 mlxsw_sp_port->local_port);
3016 goto err_port_dcb_init;
3019 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
3021 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
3022 mlxsw_sp_port->local_port);
3023 goto err_port_fids_init;
3026 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
3028 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
3029 mlxsw_sp_port->local_port);
3030 goto err_port_qdiscs_init;
3033 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
3034 if (IS_ERR(mlxsw_sp_port_vlan)) {
3035 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
3036 mlxsw_sp_port->local_port);
3037 err = PTR_ERR(mlxsw_sp_port_vlan);
3038 goto err_port_vlan_get;
3041 mlxsw_sp_port_switchdev_init(mlxsw_sp_port);
3042 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
3043 err = register_netdev(dev);
3045 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
3046 mlxsw_sp_port->local_port);
3047 goto err_register_netdev;
3050 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
3051 mlxsw_sp_port, dev, module + 1,
3052 mlxsw_sp_port->split, lane / width);
3053 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
3056 err_register_netdev:
3057 mlxsw_sp->ports[local_port] = NULL;
3058 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
3059 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
3061 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3062 err_port_qdiscs_init:
3063 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3065 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3067 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3068 err_port_tc_mc_mode:
3070 err_port_buffers_init:
3071 err_port_admin_status_set:
3073 err_port_speed_by_width_set:
3074 err_port_system_port_mapping_set:
3076 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3078 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3079 err_port_module_map:
3080 kfree(mlxsw_sp_port->sample);
3082 free_percpu(mlxsw_sp_port->pcpu_stats);
3086 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3090 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3092 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3094 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
3095 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
3096 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
3097 mlxsw_sp->ports[local_port] = NULL;
3098 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
3099 mlxsw_sp_port_vlan_flush(mlxsw_sp_port);
3100 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3101 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3102 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3103 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3104 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3105 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3106 kfree(mlxsw_sp_port->sample);
3107 free_percpu(mlxsw_sp_port->pcpu_stats);
3108 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
3109 free_netdev(mlxsw_sp_port->dev);
3110 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3113 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3115 return mlxsw_sp->ports[local_port] != NULL;
3118 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
3122 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
3123 if (mlxsw_sp_port_created(mlxsw_sp, i))
3124 mlxsw_sp_port_remove(mlxsw_sp, i);
3125 kfree(mlxsw_sp->port_to_module);
3126 kfree(mlxsw_sp->ports);
3127 mlxsw_sp->ports = NULL;
3130 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
3132 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
3133 u8 module, width, lane;
3138 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
3139 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
3140 if (!mlxsw_sp->ports)
3143 mlxsw_sp->port_to_module = kmalloc_array(max_ports, sizeof(int),
3145 if (!mlxsw_sp->port_to_module) {
3147 goto err_port_to_module_alloc;
3150 for (i = 1; i < max_ports; i++) {
3151 /* Mark as invalid */
3152 mlxsw_sp->port_to_module[i] = -1;
3154 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
3157 goto err_port_module_info_get;
3160 mlxsw_sp->port_to_module[i] = module;
3161 err = mlxsw_sp_port_create(mlxsw_sp, i, false,
3162 module, width, lane);
3164 goto err_port_create;
3169 err_port_module_info_get:
3170 for (i--; i >= 1; i--)
3171 if (mlxsw_sp_port_created(mlxsw_sp, i))
3172 mlxsw_sp_port_remove(mlxsw_sp, i);
3173 kfree(mlxsw_sp->port_to_module);
3174 err_port_to_module_alloc:
3175 kfree(mlxsw_sp->ports);
3176 mlxsw_sp->ports = NULL;
3180 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
3182 u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
3184 return local_port - offset;
3187 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
3188 u8 module, unsigned int count)
3190 u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
3193 for (i = 0; i < count; i++) {
3194 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true,
3195 module, width, i * width);
3197 goto err_port_create;
3203 for (i--; i >= 0; i--)
3204 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3205 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3209 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
3210 u8 base_port, unsigned int count)
3212 u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
3215 /* Split by four means we need to re-create two ports, otherwise
3220 for (i = 0; i < count; i++) {
3221 local_port = base_port + i * 2;
3222 if (mlxsw_sp->port_to_module[local_port] < 0)
3224 module = mlxsw_sp->port_to_module[local_port];
3226 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
3231 static struct mlxsw_sp_port *
3232 mlxsw_sp_port_get_by_local_port(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3234 if (mlxsw_sp->ports && mlxsw_sp->ports[local_port])
3235 return mlxsw_sp->ports[local_port];
3239 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
3241 struct netlink_ext_ack *extack)
3243 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3244 struct mlxsw_sp_port *mlxsw_sp_port;
3245 u8 module, cur_width, base_port;
3249 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
3250 if (!mlxsw_sp_port) {
3251 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3253 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
3257 module = mlxsw_sp_port->mapping.module;
3258 cur_width = mlxsw_sp_port->mapping.width;
3260 if (count != 2 && count != 4) {
3261 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
3262 NL_SET_ERR_MSG_MOD(extack, "Port can only be split into 2 or 4 ports");
3266 if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
3267 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
3268 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split further");
3272 /* Make sure we have enough slave (even) ports for the split. */
3274 base_port = local_port;
3275 if (mlxsw_sp->ports[base_port + 1]) {
3276 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3277 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
3281 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3282 if (mlxsw_sp->ports[base_port + 1] ||
3283 mlxsw_sp->ports[base_port + 3]) {
3284 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3285 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
3290 for (i = 0; i < count; i++)
3291 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3292 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3294 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count);
3296 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
3297 goto err_port_split_create;
3302 err_port_split_create:
3303 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
3307 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port,
3308 struct netlink_ext_ack *extack)
3310 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3311 struct mlxsw_sp_port *mlxsw_sp_port;
3312 u8 cur_width, base_port;
3316 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
3317 if (!mlxsw_sp_port) {
3318 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3320 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
3324 if (!mlxsw_sp_port->split) {
3325 netdev_err(mlxsw_sp_port->dev, "Port was not split\n");
3326 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
3330 cur_width = mlxsw_sp_port->mapping.width;
3331 count = cur_width == 1 ? 4 : 2;
3333 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3335 /* Determine which ports to remove. */
3336 if (count == 2 && local_port >= base_port + 2)
3337 base_port = base_port + 2;
3339 for (i = 0; i < count; i++)
3340 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3341 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3343 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
3349 mlxsw_sp_port_down_wipe_counters(struct mlxsw_sp_port *mlxsw_sp_port)
3353 for (i = 0; i < TC_MAX_QUEUE; i++)
3354 mlxsw_sp_port->periodic_hw_stats.xstats.backlog[i] = 0;
3357 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
3358 char *pude_pl, void *priv)
3360 struct mlxsw_sp *mlxsw_sp = priv;
3361 struct mlxsw_sp_port *mlxsw_sp_port;
3362 enum mlxsw_reg_pude_oper_status status;
3365 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
3366 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3370 status = mlxsw_reg_pude_oper_status_get(pude_pl);
3371 if (status == MLXSW_PORT_OPER_STATUS_UP) {
3372 netdev_info(mlxsw_sp_port->dev, "link up\n");
3373 netif_carrier_on(mlxsw_sp_port->dev);
3375 netdev_info(mlxsw_sp_port->dev, "link down\n");
3376 netif_carrier_off(mlxsw_sp_port->dev);
3377 mlxsw_sp_port_down_wipe_counters(mlxsw_sp_port);
3381 static void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
3382 u8 local_port, void *priv)
3384 struct mlxsw_sp *mlxsw_sp = priv;
3385 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3386 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
3388 if (unlikely(!mlxsw_sp_port)) {
3389 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
3394 skb->dev = mlxsw_sp_port->dev;
3396 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
3397 u64_stats_update_begin(&pcpu_stats->syncp);
3398 pcpu_stats->rx_packets++;
3399 pcpu_stats->rx_bytes += skb->len;
3400 u64_stats_update_end(&pcpu_stats->syncp);
3402 skb->protocol = eth_type_trans(skb, skb->dev);
3403 netif_receive_skb(skb);
3406 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
3409 skb->offload_fwd_mark = 1;
3410 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
3413 static void mlxsw_sp_rx_listener_mr_mark_func(struct sk_buff *skb,
3414 u8 local_port, void *priv)
3416 skb->offload_mr_fwd_mark = 1;
3417 skb->offload_fwd_mark = 1;
3418 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
3421 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
3424 struct mlxsw_sp *mlxsw_sp = priv;
3425 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3426 struct psample_group *psample_group;
3429 if (unlikely(!mlxsw_sp_port)) {
3430 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
3434 if (unlikely(!mlxsw_sp_port->sample)) {
3435 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
3440 size = mlxsw_sp_port->sample->truncate ?
3441 mlxsw_sp_port->sample->trunc_size : skb->len;
3444 psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
3447 psample_sample_packet(psample_group, skb, size,
3448 mlxsw_sp_port->dev->ifindex, 0,
3449 mlxsw_sp_port->sample->rate);
3456 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3457 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
3458 _is_ctrl, SP_##_trap_group, DISCARD)
3460 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3461 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
3462 _is_ctrl, SP_##_trap_group, DISCARD)
3464 #define MLXSW_SP_RXL_MR_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3465 MLXSW_RXL(mlxsw_sp_rx_listener_mr_mark_func, _trap_id, _action, \
3466 _is_ctrl, SP_##_trap_group, DISCARD)
3468 #define MLXSW_SP_EVENTL(_func, _trap_id) \
3469 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
3471 static const struct mlxsw_listener mlxsw_sp_listener[] = {
3473 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
3475 MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
3476 MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
3477 MLXSW_SP_RXL_NO_MARK(LLDP, TRAP_TO_CPU, LLDP, true),
3478 MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
3479 MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
3480 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
3481 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
3482 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
3483 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
3484 MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
3485 MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
3486 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, IP2ME, false),
3487 MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY, MIRROR_TO_CPU, IPV6_MLD,
3489 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
3491 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE, TRAP_TO_CPU, IPV6_MLD,
3493 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
3496 MLXSW_SP_RXL_MARK(MTUERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3497 MLXSW_SP_RXL_MARK(TTLERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3498 MLXSW_SP_RXL_MARK(LBERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3499 MLXSW_SP_RXL_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
3500 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
3502 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP, false),
3503 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
3504 MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, TRAP_TO_CPU, ROUTER_EXP, false),
3505 MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, TRAP_TO_CPU, ROUTER_EXP,
3507 MLXSW_SP_RXL_MARK(IPV4_OSPF, TRAP_TO_CPU, OSPF, false),
3508 MLXSW_SP_RXL_MARK(IPV6_OSPF, TRAP_TO_CPU, OSPF, false),
3509 MLXSW_SP_RXL_MARK(IPV6_DHCP, TRAP_TO_CPU, DHCP, false),
3510 MLXSW_SP_RXL_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
3511 MLXSW_SP_RXL_MARK(IPV4_BGP, TRAP_TO_CPU, BGP, false),
3512 MLXSW_SP_RXL_MARK(IPV6_BGP, TRAP_TO_CPU, BGP, false),
3513 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
3515 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
3517 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
3519 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
3521 MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, TRAP_TO_CPU, IPV6_ND, false),
3522 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
3524 MLXSW_SP_RXL_MARK(HOST_MISS_IPV4, TRAP_TO_CPU, HOST_MISS, false),
3525 MLXSW_SP_RXL_MARK(HOST_MISS_IPV6, TRAP_TO_CPU, HOST_MISS, false),
3526 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, TRAP_TO_CPU, ROUTER_EXP, false),
3527 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, TRAP_TO_CPU, ROUTER_EXP, false),
3528 MLXSW_SP_RXL_MARK(IPIP_DECAP_ERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3529 MLXSW_SP_RXL_MARK(IPV4_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
3530 MLXSW_SP_RXL_MARK(IPV6_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
3531 /* PKT Sample trap */
3532 MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
3533 false, SP_IP2ME, DISCARD),
3535 MLXSW_SP_RXL_NO_MARK(ACL0, TRAP_TO_CPU, IP2ME, false),
3536 /* Multicast Router Traps */
3537 MLXSW_SP_RXL_MARK(IPV4_PIM, TRAP_TO_CPU, PIM, false),
3538 MLXSW_SP_RXL_MARK(IPV6_PIM, TRAP_TO_CPU, PIM, false),
3539 MLXSW_SP_RXL_MARK(RPF, TRAP_TO_CPU, RPF, false),
3540 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
3541 MLXSW_SP_RXL_MR_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
3544 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
3546 char qpcr_pl[MLXSW_REG_QPCR_LEN];
3547 enum mlxsw_reg_qpcr_ir_units ir_units;
3548 int max_cpu_policers;
3554 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
3557 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
3559 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
3560 for (i = 0; i < max_cpu_policers; i++) {
3563 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
3564 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
3565 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
3566 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
3567 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
3568 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
3572 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
3573 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
3577 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
3578 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
3579 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
3580 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
3581 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
3582 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
3583 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
3584 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
3588 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
3596 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
3598 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
3606 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
3608 char htgt_pl[MLXSW_REG_HTGT_LEN];
3609 enum mlxsw_reg_htgt_trap_group i;
3610 int max_cpu_policers;
3611 int max_trap_groups;
3616 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
3619 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
3620 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
3622 for (i = 0; i < max_trap_groups; i++) {
3625 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
3626 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
3627 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
3628 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
3629 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
3633 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
3634 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
3638 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
3639 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
3640 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
3644 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
3645 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
3646 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
3650 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
3651 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
3652 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
3653 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
3657 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
3658 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
3659 tc = MLXSW_REG_HTGT_DEFAULT_TC;
3660 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
3666 if (max_cpu_policers <= policer_id &&
3667 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
3670 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
3671 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3679 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
3684 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
3688 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
3692 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3693 err = mlxsw_core_trap_register(mlxsw_sp->core,
3694 &mlxsw_sp_listener[i],
3697 goto err_listener_register;
3702 err_listener_register:
3703 for (i--; i >= 0; i--) {
3704 mlxsw_core_trap_unregister(mlxsw_sp->core,
3705 &mlxsw_sp_listener[i],
3711 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
3715 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3716 mlxsw_core_trap_unregister(mlxsw_sp->core,
3717 &mlxsw_sp_listener[i],
3722 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
3724 char slcr_pl[MLXSW_REG_SLCR_LEN];
3727 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
3728 MLXSW_REG_SLCR_LAG_HASH_DMAC |
3729 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
3730 MLXSW_REG_SLCR_LAG_HASH_VLANID |
3731 MLXSW_REG_SLCR_LAG_HASH_SIP |
3732 MLXSW_REG_SLCR_LAG_HASH_DIP |
3733 MLXSW_REG_SLCR_LAG_HASH_SPORT |
3734 MLXSW_REG_SLCR_LAG_HASH_DPORT |
3735 MLXSW_REG_SLCR_LAG_HASH_IPPROTO);
3736 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
3740 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
3741 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
3744 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
3745 sizeof(struct mlxsw_sp_upper),
3747 if (!mlxsw_sp->lags)
3753 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
3755 kfree(mlxsw_sp->lags);
3758 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
3760 char htgt_pl[MLXSW_REG_HTGT_LEN];
3762 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
3763 MLXSW_REG_HTGT_INVALID_POLICER,
3764 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
3765 MLXSW_REG_HTGT_DEFAULT_TC);
3766 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3769 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
3770 unsigned long event, void *ptr);
3772 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
3773 const struct mlxsw_bus_info *mlxsw_bus_info)
3775 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3778 mlxsw_sp->core = mlxsw_core;
3779 mlxsw_sp->bus_info = mlxsw_bus_info;
3781 err = mlxsw_sp_fw_rev_validate(mlxsw_sp);
3785 err = mlxsw_sp_base_mac_get(mlxsw_sp);
3787 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
3791 err = mlxsw_sp_kvdl_init(mlxsw_sp);
3793 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
3797 err = mlxsw_sp_fids_init(mlxsw_sp);
3799 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
3803 err = mlxsw_sp_traps_init(mlxsw_sp);
3805 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
3806 goto err_traps_init;
3809 err = mlxsw_sp_buffers_init(mlxsw_sp);
3811 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
3812 goto err_buffers_init;
3815 err = mlxsw_sp_lag_init(mlxsw_sp);
3817 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
3821 /* Initialize SPAN before router and switchdev, so that those components
3822 * can call mlxsw_sp_span_respin().
3824 err = mlxsw_sp_span_init(mlxsw_sp);
3826 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
3830 err = mlxsw_sp_switchdev_init(mlxsw_sp);
3832 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
3833 goto err_switchdev_init;
3836 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
3838 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
3839 goto err_counter_pool_init;
3842 err = mlxsw_sp_afa_init(mlxsw_sp);
3844 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
3848 err = mlxsw_sp_router_init(mlxsw_sp);
3850 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
3851 goto err_router_init;
3854 /* Initialize netdevice notifier after router and SPAN is initialized,
3855 * so that the event handler can use router structures and call SPAN
3858 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
3859 err = register_netdevice_notifier(&mlxsw_sp->netdevice_nb);
3861 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
3862 goto err_netdev_notifier;
3865 err = mlxsw_sp_acl_init(mlxsw_sp);
3867 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
3871 err = mlxsw_sp_dpipe_init(mlxsw_sp);
3873 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
3874 goto err_dpipe_init;
3877 err = mlxsw_sp_ports_create(mlxsw_sp);
3879 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
3880 goto err_ports_create;
3886 mlxsw_sp_dpipe_fini(mlxsw_sp);
3888 mlxsw_sp_acl_fini(mlxsw_sp);
3890 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
3891 err_netdev_notifier:
3892 mlxsw_sp_router_fini(mlxsw_sp);
3894 mlxsw_sp_afa_fini(mlxsw_sp);
3896 mlxsw_sp_counter_pool_fini(mlxsw_sp);
3897 err_counter_pool_init:
3898 mlxsw_sp_switchdev_fini(mlxsw_sp);
3900 mlxsw_sp_span_fini(mlxsw_sp);
3902 mlxsw_sp_lag_fini(mlxsw_sp);
3904 mlxsw_sp_buffers_fini(mlxsw_sp);
3906 mlxsw_sp_traps_fini(mlxsw_sp);
3908 mlxsw_sp_fids_fini(mlxsw_sp);
3910 mlxsw_sp_kvdl_fini(mlxsw_sp);
3914 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
3915 const struct mlxsw_bus_info *mlxsw_bus_info)
3917 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3919 mlxsw_sp->req_rev = &mlxsw_sp1_fw_rev;
3920 mlxsw_sp->fw_filename = MLXSW_SP1_FW_FILENAME;
3921 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
3922 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
3923 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
3924 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
3925 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
3927 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
3930 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
3931 const struct mlxsw_bus_info *mlxsw_bus_info)
3933 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3935 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
3936 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
3937 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
3938 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
3939 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
3941 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
3944 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
3946 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3948 mlxsw_sp_ports_remove(mlxsw_sp);
3949 mlxsw_sp_dpipe_fini(mlxsw_sp);
3950 mlxsw_sp_acl_fini(mlxsw_sp);
3951 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
3952 mlxsw_sp_router_fini(mlxsw_sp);
3953 mlxsw_sp_afa_fini(mlxsw_sp);
3954 mlxsw_sp_counter_pool_fini(mlxsw_sp);
3955 mlxsw_sp_switchdev_fini(mlxsw_sp);
3956 mlxsw_sp_span_fini(mlxsw_sp);
3957 mlxsw_sp_lag_fini(mlxsw_sp);
3958 mlxsw_sp_buffers_fini(mlxsw_sp);
3959 mlxsw_sp_traps_fini(mlxsw_sp);
3960 mlxsw_sp_fids_fini(mlxsw_sp);
3961 mlxsw_sp_kvdl_fini(mlxsw_sp);
3964 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
3966 .max_mid = MLXSW_SP_MID_MAX,
3967 .used_flood_tables = 1,
3968 .used_flood_mode = 1,
3970 .max_fid_offset_flood_tables = 3,
3971 .fid_offset_flood_table_size = VLAN_N_VID - 1,
3972 .max_fid_flood_tables = 3,
3973 .fid_flood_table_size = MLXSW_SP_FID_8021D_MAX,
3974 .used_max_ib_mc = 1,
3978 .used_kvd_sizes = 1,
3979 .kvd_hash_single_parts = 59,
3980 .kvd_hash_double_parts = 41,
3981 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
3985 .type = MLXSW_PORT_SWID_TYPE_ETH,
3990 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
3992 .max_mid = MLXSW_SP_MID_MAX,
3993 .used_flood_tables = 1,
3994 .used_flood_mode = 1,
3996 .max_fid_offset_flood_tables = 3,
3997 .fid_offset_flood_table_size = VLAN_N_VID - 1,
3998 .max_fid_flood_tables = 3,
3999 .fid_flood_table_size = MLXSW_SP_FID_8021D_MAX,
4000 .used_max_ib_mc = 1,
4007 .type = MLXSW_PORT_SWID_TYPE_ETH,
4013 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
4014 struct devlink_resource_size_params *kvd_size_params,
4015 struct devlink_resource_size_params *linear_size_params,
4016 struct devlink_resource_size_params *hash_double_size_params,
4017 struct devlink_resource_size_params *hash_single_size_params)
4019 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4020 KVD_SINGLE_MIN_SIZE);
4021 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4022 KVD_DOUBLE_MIN_SIZE);
4023 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4024 u32 linear_size_min = 0;
4026 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
4027 MLXSW_SP_KVD_GRANULARITY,
4028 DEVLINK_RESOURCE_UNIT_ENTRY);
4029 devlink_resource_size_params_init(linear_size_params, linear_size_min,
4030 kvd_size - single_size_min -
4032 MLXSW_SP_KVD_GRANULARITY,
4033 DEVLINK_RESOURCE_UNIT_ENTRY);
4034 devlink_resource_size_params_init(hash_double_size_params,
4036 kvd_size - single_size_min -
4038 MLXSW_SP_KVD_GRANULARITY,
4039 DEVLINK_RESOURCE_UNIT_ENTRY);
4040 devlink_resource_size_params_init(hash_single_size_params,
4042 kvd_size - double_size_min -
4044 MLXSW_SP_KVD_GRANULARITY,
4045 DEVLINK_RESOURCE_UNIT_ENTRY);
4048 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
4050 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4051 struct devlink_resource_size_params hash_single_size_params;
4052 struct devlink_resource_size_params hash_double_size_params;
4053 struct devlink_resource_size_params linear_size_params;
4054 struct devlink_resource_size_params kvd_size_params;
4055 u32 kvd_size, single_size, double_size, linear_size;
4056 const struct mlxsw_config_profile *profile;
4059 profile = &mlxsw_sp1_config_profile;
4060 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
4063 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
4064 &linear_size_params,
4065 &hash_double_size_params,
4066 &hash_single_size_params);
4068 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4069 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
4070 kvd_size, MLXSW_SP_RESOURCE_KVD,
4071 DEVLINK_RESOURCE_ID_PARENT_TOP,
4076 linear_size = profile->kvd_linear_size;
4077 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
4079 MLXSW_SP_RESOURCE_KVD_LINEAR,
4080 MLXSW_SP_RESOURCE_KVD,
4081 &linear_size_params);
4085 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
4089 double_size = kvd_size - linear_size;
4090 double_size *= profile->kvd_hash_double_parts;
4091 double_size /= profile->kvd_hash_double_parts +
4092 profile->kvd_hash_single_parts;
4093 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
4094 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
4096 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
4097 MLXSW_SP_RESOURCE_KVD,
4098 &hash_double_size_params);
4102 single_size = kvd_size - double_size - linear_size;
4103 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
4105 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
4106 MLXSW_SP_RESOURCE_KVD,
4107 &hash_single_size_params);
4114 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
4116 return mlxsw_sp1_resources_kvd_register(mlxsw_core);
4119 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
4124 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
4125 const struct mlxsw_config_profile *profile,
4126 u64 *p_single_size, u64 *p_double_size,
4129 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4133 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
4134 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
4137 /* The hash part is what left of the kvd without the
4138 * linear part. It is split to the single size and
4139 * double size by the parts ratio from the profile.
4140 * Both sizes must be a multiplications of the
4141 * granularity from the profile. In case the user
4142 * provided the sizes they are obtained via devlink.
4144 err = devlink_resource_size_get(devlink,
4145 MLXSW_SP_RESOURCE_KVD_LINEAR,
4148 *p_linear_size = profile->kvd_linear_size;
4150 err = devlink_resource_size_get(devlink,
4151 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
4154 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
4156 double_size *= profile->kvd_hash_double_parts;
4157 double_size /= profile->kvd_hash_double_parts +
4158 profile->kvd_hash_single_parts;
4159 *p_double_size = rounddown(double_size,
4160 MLXSW_SP_KVD_GRANULARITY);
4163 err = devlink_resource_size_get(devlink,
4164 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
4167 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
4168 *p_double_size - *p_linear_size;
4170 /* Check results are legal. */
4171 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
4172 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
4173 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
4179 static struct mlxsw_driver mlxsw_sp1_driver = {
4180 .kind = mlxsw_sp1_driver_name,
4181 .priv_size = sizeof(struct mlxsw_sp),
4182 .init = mlxsw_sp1_init,
4183 .fini = mlxsw_sp_fini,
4184 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
4185 .port_split = mlxsw_sp_port_split,
4186 .port_unsplit = mlxsw_sp_port_unsplit,
4187 .sb_pool_get = mlxsw_sp_sb_pool_get,
4188 .sb_pool_set = mlxsw_sp_sb_pool_set,
4189 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4190 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4191 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4192 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4193 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4194 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4195 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4196 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4197 .txhdr_construct = mlxsw_sp_txhdr_construct,
4198 .resources_register = mlxsw_sp1_resources_register,
4199 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
4200 .txhdr_len = MLXSW_TXHDR_LEN,
4201 .profile = &mlxsw_sp1_config_profile,
4202 .res_query_enabled = true,
4205 static struct mlxsw_driver mlxsw_sp2_driver = {
4206 .kind = mlxsw_sp2_driver_name,
4207 .priv_size = sizeof(struct mlxsw_sp),
4208 .init = mlxsw_sp2_init,
4209 .fini = mlxsw_sp_fini,
4210 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
4211 .port_split = mlxsw_sp_port_split,
4212 .port_unsplit = mlxsw_sp_port_unsplit,
4213 .sb_pool_get = mlxsw_sp_sb_pool_get,
4214 .sb_pool_set = mlxsw_sp_sb_pool_set,
4215 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4216 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4217 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4218 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4219 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4220 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4221 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4222 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4223 .txhdr_construct = mlxsw_sp_txhdr_construct,
4224 .resources_register = mlxsw_sp2_resources_register,
4225 .txhdr_len = MLXSW_TXHDR_LEN,
4226 .profile = &mlxsw_sp2_config_profile,
4227 .res_query_enabled = true,
4230 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
4232 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
4235 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
4237 struct mlxsw_sp_port **p_mlxsw_sp_port = data;
4240 if (mlxsw_sp_port_dev_check(lower_dev)) {
4241 *p_mlxsw_sp_port = netdev_priv(lower_dev);
4248 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
4250 struct mlxsw_sp_port *mlxsw_sp_port;
4252 if (mlxsw_sp_port_dev_check(dev))
4253 return netdev_priv(dev);
4255 mlxsw_sp_port = NULL;
4256 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
4258 return mlxsw_sp_port;
4261 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
4263 struct mlxsw_sp_port *mlxsw_sp_port;
4265 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
4266 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
4269 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
4271 struct mlxsw_sp_port *mlxsw_sp_port;
4273 if (mlxsw_sp_port_dev_check(dev))
4274 return netdev_priv(dev);
4276 mlxsw_sp_port = NULL;
4277 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
4280 return mlxsw_sp_port;
4283 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
4285 struct mlxsw_sp_port *mlxsw_sp_port;
4288 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
4290 dev_hold(mlxsw_sp_port->dev);
4292 return mlxsw_sp_port;
4295 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
4297 dev_put(mlxsw_sp_port->dev);
4301 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
4302 struct net_device *lag_dev)
4304 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
4305 struct net_device *upper_dev;
4306 struct list_head *iter;
4308 if (netif_is_bridge_port(lag_dev))
4309 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
4311 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
4312 if (!netif_is_bridge_port(upper_dev))
4314 br_dev = netdev_master_upper_dev_get(upper_dev);
4315 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
4319 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4321 char sldr_pl[MLXSW_REG_SLDR_LEN];
4323 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
4324 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4327 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4329 char sldr_pl[MLXSW_REG_SLDR_LEN];
4331 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
4332 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4335 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4336 u16 lag_id, u8 port_index)
4338 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4339 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4341 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
4342 lag_id, port_index);
4343 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4346 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4349 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4350 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4352 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
4354 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4357 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
4360 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4361 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4363 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
4365 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4368 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
4371 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4372 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4374 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
4376 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4379 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4380 struct net_device *lag_dev,
4383 struct mlxsw_sp_upper *lag;
4384 int free_lag_id = -1;
4388 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
4389 for (i = 0; i < max_lag; i++) {
4390 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
4391 if (lag->ref_count) {
4392 if (lag->dev == lag_dev) {
4396 } else if (free_lag_id < 0) {
4400 if (free_lag_id < 0)
4402 *p_lag_id = free_lag_id;
4407 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
4408 struct net_device *lag_dev,
4409 struct netdev_lag_upper_info *lag_upper_info,
4410 struct netlink_ext_ack *extack)
4414 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
4415 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported LAG devices");
4418 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
4419 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
4425 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4426 u16 lag_id, u8 *p_port_index)
4428 u64 max_lag_members;
4431 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
4433 for (i = 0; i < max_lag_members; i++) {
4434 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
4442 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
4443 struct net_device *lag_dev)
4445 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4446 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
4447 struct mlxsw_sp_upper *lag;
4452 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
4455 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4456 if (!lag->ref_count) {
4457 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
4463 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
4466 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
4468 goto err_col_port_add;
4470 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
4471 mlxsw_sp_port->local_port);
4472 mlxsw_sp_port->lag_id = lag_id;
4473 mlxsw_sp_port->lagged = 1;
4476 /* Port is no longer usable as a router interface */
4477 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
4478 if (mlxsw_sp_port_vlan->fid)
4479 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
4484 if (!lag->ref_count)
4485 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4489 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
4490 struct net_device *lag_dev)
4492 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4493 u16 lag_id = mlxsw_sp_port->lag_id;
4494 struct mlxsw_sp_upper *lag;
4496 if (!mlxsw_sp_port->lagged)
4498 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4499 WARN_ON(lag->ref_count == 0);
4501 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4503 /* Any VLANs configured on the port are no longer valid */
4504 mlxsw_sp_port_vlan_flush(mlxsw_sp_port);
4505 /* Make the LAG and its directly linked uppers leave bridges they
4508 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
4510 if (lag->ref_count == 1)
4511 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4513 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
4514 mlxsw_sp_port->local_port);
4515 mlxsw_sp_port->lagged = 0;
4518 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
4519 /* Make sure untagged frames are allowed to ingress */
4520 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
4523 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4526 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4527 char sldr_pl[MLXSW_REG_SLDR_LEN];
4529 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
4530 mlxsw_sp_port->local_port);
4531 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4534 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4537 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4538 char sldr_pl[MLXSW_REG_SLDR_LEN];
4540 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
4541 mlxsw_sp_port->local_port);
4542 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4546 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
4550 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
4551 mlxsw_sp_port->lag_id);
4555 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4557 goto err_dist_port_add;
4562 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4567 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
4571 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
4572 mlxsw_sp_port->lag_id);
4576 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
4577 mlxsw_sp_port->lag_id);
4579 goto err_col_port_disable;
4583 err_col_port_disable:
4584 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4588 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
4589 struct netdev_lag_lower_state_info *info)
4591 if (info->tx_enabled)
4592 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
4594 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
4597 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
4600 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4601 enum mlxsw_reg_spms_state spms_state;
4606 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
4607 MLXSW_REG_SPMS_STATE_DISCARDING;
4609 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
4612 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
4614 for (vid = 0; vid < VLAN_N_VID; vid++)
4615 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
4617 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
4622 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
4627 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
4630 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
4632 goto err_port_stp_set;
4633 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 2, VLAN_N_VID - 1,
4636 goto err_port_vlan_set;
4638 for (; vid <= VLAN_N_VID - 1; vid++) {
4639 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4642 goto err_vid_learning_set;
4647 err_vid_learning_set:
4648 for (vid--; vid >= 1; vid--)
4649 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
4651 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4653 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4657 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
4661 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
4662 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4665 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 2, VLAN_N_VID - 1,
4667 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4668 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4671 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
4672 struct net_device *dev,
4673 unsigned long event, void *ptr)
4675 struct netdev_notifier_changeupper_info *info;
4676 struct mlxsw_sp_port *mlxsw_sp_port;
4677 struct netlink_ext_ack *extack;
4678 struct net_device *upper_dev;
4679 struct mlxsw_sp *mlxsw_sp;
4682 mlxsw_sp_port = netdev_priv(dev);
4683 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4685 extack = netdev_notifier_info_to_extack(&info->info);
4688 case NETDEV_PRECHANGEUPPER:
4689 upper_dev = info->upper_dev;
4690 if (!is_vlan_dev(upper_dev) &&
4691 !netif_is_lag_master(upper_dev) &&
4692 !netif_is_bridge_master(upper_dev) &&
4693 !netif_is_ovs_master(upper_dev) &&
4694 !netif_is_macvlan(upper_dev)) {
4695 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4700 if (netdev_has_any_upper_dev(upper_dev) &&
4701 (!netif_is_bridge_master(upper_dev) ||
4702 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4704 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
4707 if (netif_is_lag_master(upper_dev) &&
4708 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
4709 info->upper_info, extack))
4711 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
4712 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
4715 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
4716 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
4717 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
4720 if (netif_is_macvlan(upper_dev) &&
4721 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, lower_dev)) {
4722 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4725 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
4726 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
4729 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
4730 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
4733 if (is_vlan_dev(upper_dev) &&
4734 vlan_dev_vlan_id(upper_dev) == 1) {
4735 NL_SET_ERR_MSG_MOD(extack, "Creating a VLAN device with VID 1 is unsupported: VLAN 1 carries untagged traffic");
4739 case NETDEV_CHANGEUPPER:
4740 upper_dev = info->upper_dev;
4741 if (netif_is_bridge_master(upper_dev)) {
4743 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4748 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
4751 } else if (netif_is_lag_master(upper_dev)) {
4752 if (info->linking) {
4753 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
4756 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
4757 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
4760 } else if (netif_is_ovs_master(upper_dev)) {
4762 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
4764 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
4765 } else if (netif_is_macvlan(upper_dev)) {
4767 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4768 } else if (is_vlan_dev(upper_dev)) {
4769 struct net_device *br_dev;
4771 if (!netif_is_bridge_port(upper_dev))
4775 br_dev = netdev_master_upper_dev_get(upper_dev);
4776 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
4785 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
4786 unsigned long event, void *ptr)
4788 struct netdev_notifier_changelowerstate_info *info;
4789 struct mlxsw_sp_port *mlxsw_sp_port;
4792 mlxsw_sp_port = netdev_priv(dev);
4796 case NETDEV_CHANGELOWERSTATE:
4797 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
4798 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
4799 info->lower_state_info);
4801 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
4809 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
4810 struct net_device *port_dev,
4811 unsigned long event, void *ptr)
4814 case NETDEV_PRECHANGEUPPER:
4815 case NETDEV_CHANGEUPPER:
4816 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
4818 case NETDEV_CHANGELOWERSTATE:
4819 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
4826 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
4827 unsigned long event, void *ptr)
4829 struct net_device *dev;
4830 struct list_head *iter;
4833 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4834 if (mlxsw_sp_port_dev_check(dev)) {
4835 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
4845 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
4846 struct net_device *dev,
4847 unsigned long event, void *ptr,
4850 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
4851 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4852 struct netdev_notifier_changeupper_info *info = ptr;
4853 struct netlink_ext_ack *extack;
4854 struct net_device *upper_dev;
4857 extack = netdev_notifier_info_to_extack(&info->info);
4860 case NETDEV_PRECHANGEUPPER:
4861 upper_dev = info->upper_dev;
4862 if (!netif_is_bridge_master(upper_dev) &&
4863 !netif_is_macvlan(upper_dev)) {
4864 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4869 if (netdev_has_any_upper_dev(upper_dev) &&
4870 (!netif_is_bridge_master(upper_dev) ||
4871 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4873 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
4876 if (netif_is_macvlan(upper_dev) &&
4877 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan_dev)) {
4878 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4882 case NETDEV_CHANGEUPPER:
4883 upper_dev = info->upper_dev;
4884 if (netif_is_bridge_master(upper_dev)) {
4886 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4891 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
4894 } else if (netif_is_macvlan(upper_dev)) {
4896 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4907 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
4908 struct net_device *lag_dev,
4909 unsigned long event,
4912 struct net_device *dev;
4913 struct list_head *iter;
4916 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4917 if (mlxsw_sp_port_dev_check(dev)) {
4918 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
4929 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
4930 unsigned long event, void *ptr)
4932 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
4933 u16 vid = vlan_dev_vlan_id(vlan_dev);
4935 if (mlxsw_sp_port_dev_check(real_dev))
4936 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
4938 else if (netif_is_lag_master(real_dev))
4939 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
4946 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
4947 unsigned long event, void *ptr)
4949 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(br_dev);
4950 struct netdev_notifier_changeupper_info *info = ptr;
4951 struct netlink_ext_ack *extack;
4952 struct net_device *upper_dev;
4957 extack = netdev_notifier_info_to_extack(&info->info);
4960 case NETDEV_PRECHANGEUPPER:
4961 upper_dev = info->upper_dev;
4962 if (!is_vlan_dev(upper_dev) && !netif_is_macvlan(upper_dev)) {
4963 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4968 if (netif_is_macvlan(upper_dev) &&
4969 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev)) {
4970 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4974 case NETDEV_CHANGEUPPER:
4975 upper_dev = info->upper_dev;
4978 if (is_vlan_dev(upper_dev))
4979 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
4980 if (netif_is_macvlan(upper_dev))
4981 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4988 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
4989 unsigned long event, void *ptr)
4991 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
4992 struct netdev_notifier_changeupper_info *info = ptr;
4993 struct netlink_ext_ack *extack;
4995 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
4998 extack = netdev_notifier_info_to_extack(&info->info);
5000 /* VRF enslavement is handled in mlxsw_sp_netdevice_vrf_event() */
5001 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5006 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
5008 struct netdev_notifier_changeupper_info *info = ptr;
5010 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
5012 return netif_is_l3_master(info->upper_dev);
5015 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
5016 unsigned long event, void *ptr)
5018 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5019 struct mlxsw_sp_span_entry *span_entry;
5020 struct mlxsw_sp *mlxsw_sp;
5023 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
5024 if (event == NETDEV_UNREGISTER) {
5025 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
5027 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
5029 mlxsw_sp_span_respin(mlxsw_sp);
5031 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
5032 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
5034 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
5035 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
5037 else if (event == NETDEV_CHANGEADDR || event == NETDEV_CHANGEMTU)
5038 err = mlxsw_sp_netdevice_router_port_event(dev);
5039 else if (mlxsw_sp_is_vrf_event(event, ptr))
5040 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
5041 else if (mlxsw_sp_port_dev_check(dev))
5042 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
5043 else if (netif_is_lag_master(dev))
5044 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
5045 else if (is_vlan_dev(dev))
5046 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
5047 else if (netif_is_bridge_master(dev))
5048 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
5049 else if (netif_is_macvlan(dev))
5050 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
5052 return notifier_from_errno(err);
5055 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
5056 .notifier_call = mlxsw_sp_inetaddr_valid_event,
5059 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly = {
5060 .notifier_call = mlxsw_sp_inetaddr_event,
5063 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
5064 .notifier_call = mlxsw_sp_inet6addr_valid_event,
5067 static struct notifier_block mlxsw_sp_inet6addr_nb __read_mostly = {
5068 .notifier_call = mlxsw_sp_inet6addr_event,
5071 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
5072 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
5076 static struct pci_driver mlxsw_sp1_pci_driver = {
5077 .name = mlxsw_sp1_driver_name,
5078 .id_table = mlxsw_sp1_pci_id_table,
5081 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
5082 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
5086 static struct pci_driver mlxsw_sp2_pci_driver = {
5087 .name = mlxsw_sp2_driver_name,
5088 .id_table = mlxsw_sp2_pci_id_table,
5091 static int __init mlxsw_sp_module_init(void)
5095 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5096 register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5097 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5098 register_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5100 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
5102 goto err_sp1_core_driver_register;
5104 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
5106 goto err_sp2_core_driver_register;
5108 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
5110 goto err_sp1_pci_driver_register;
5112 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
5114 goto err_sp2_pci_driver_register;
5118 err_sp2_pci_driver_register:
5119 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
5120 err_sp1_pci_driver_register:
5121 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
5122 err_sp2_core_driver_register:
5123 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
5124 err_sp1_core_driver_register:
5125 unregister_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5126 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5127 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5128 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5132 static void __exit mlxsw_sp_module_exit(void)
5134 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
5135 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
5136 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
5137 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
5138 unregister_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5139 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5140 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5141 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5144 module_init(mlxsw_sp_module_init);
5145 module_exit(mlxsw_sp_module_exit);
5147 MODULE_LICENSE("Dual BSD/GPL");
5148 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
5149 MODULE_DESCRIPTION("Mellanox Spectrum driver");
5150 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
5151 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);