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 <linux/jhash.h>
25 #include <net/switchdev.h>
26 #include <net/pkt_cls.h>
27 #include <net/tc_act/tc_mirred.h>
28 #include <net/netevent.h>
29 #include <net/tc_act/tc_sample.h>
30 #include <net/addrconf.h>
40 #include "spectrum_cnt.h"
41 #include "spectrum_dpipe.h"
42 #include "spectrum_acl_flex_actions.h"
43 #include "spectrum_span.h"
44 #include "spectrum_ptp.h"
45 #include "../mlxfw/mlxfw.h"
47 #define MLXSW_SP_FWREV_MINOR_TO_BRANCH(minor) ((minor) / 100)
49 #define MLXSW_SP1_FWREV_MAJOR 13
50 #define MLXSW_SP1_FWREV_MINOR 2000
51 #define MLXSW_SP1_FWREV_SUBMINOR 1122
52 #define MLXSW_SP1_FWREV_CAN_RESET_MINOR 1702
54 static const struct mlxsw_fw_rev mlxsw_sp1_fw_rev = {
55 .major = MLXSW_SP1_FWREV_MAJOR,
56 .minor = MLXSW_SP1_FWREV_MINOR,
57 .subminor = MLXSW_SP1_FWREV_SUBMINOR,
58 .can_reset_minor = MLXSW_SP1_FWREV_CAN_RESET_MINOR,
61 #define MLXSW_SP1_FW_FILENAME \
64 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
65 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
66 static const char mlxsw_sp_driver_version[] = "1.0";
68 static const unsigned char mlxsw_sp1_mac_mask[ETH_ALEN] = {
69 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00
71 static const unsigned char mlxsw_sp2_mac_mask[ETH_ALEN] = {
72 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
79 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
82 * Packet control type.
83 * 0 - Ethernet control (e.g. EMADs, LACP)
86 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
89 * Packet protocol type. Must be set to 1 (Ethernet).
91 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
93 /* tx_hdr_rx_is_router
94 * Packet is sent from the router. Valid for data packets only.
96 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
99 * Indicates if the 'fid' field is valid and should be used for
100 * forwarding lookup. Valid for data packets only.
102 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
105 * Switch partition ID. Must be set to 0.
107 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
109 /* tx_hdr_control_tclass
110 * Indicates if the packet should use the control TClass and not one
111 * of the data TClasses.
113 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
116 * Egress TClass to be used on the egress device on the egress port.
118 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
121 * Destination local port for unicast packets.
122 * Destination multicast ID for multicast packets.
124 * Control packets are directed to a specific egress port, while data
125 * packets are transmitted through the CPU port (0) into the switch partition,
126 * where forwarding rules are applied.
128 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
131 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
132 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
133 * Valid for data packets only.
135 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
139 * 6 - Control packets
141 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
143 struct mlxsw_sp_mlxfw_dev {
144 struct mlxfw_dev mlxfw_dev;
145 struct mlxsw_sp *mlxsw_sp;
148 struct mlxsw_sp_ptp_ops {
149 struct mlxsw_sp_ptp_clock *
150 (*clock_init)(struct mlxsw_sp *mlxsw_sp, struct device *dev);
151 void (*clock_fini)(struct mlxsw_sp_ptp_clock *clock);
153 struct mlxsw_sp_ptp_state *(*init)(struct mlxsw_sp *mlxsw_sp);
154 void (*fini)(struct mlxsw_sp_ptp_state *ptp_state);
156 /* Notify a driver that a packet that might be PTP was received. Driver
157 * is responsible for freeing the passed-in SKB.
159 void (*receive)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
162 /* Notify a driver that a timestamped packet was transmitted. Driver
163 * is responsible for freeing the passed-in SKB.
165 void (*transmitted)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
168 int (*hwtstamp_get)(struct mlxsw_sp_port *mlxsw_sp_port,
169 struct hwtstamp_config *config);
170 int (*hwtstamp_set)(struct mlxsw_sp_port *mlxsw_sp_port,
171 struct hwtstamp_config *config);
172 void (*shaper_work)(struct work_struct *work);
173 int (*get_ts_info)(struct mlxsw_sp *mlxsw_sp,
174 struct ethtool_ts_info *info);
177 static int mlxsw_sp_component_query(struct mlxfw_dev *mlxfw_dev,
178 u16 component_index, u32 *p_max_size,
179 u8 *p_align_bits, u16 *p_max_write_size)
181 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
182 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
183 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
184 char mcqi_pl[MLXSW_REG_MCQI_LEN];
187 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
188 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcqi), mcqi_pl);
191 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
194 *p_align_bits = max_t(u8, *p_align_bits, 2);
195 *p_max_write_size = min_t(u16, *p_max_write_size,
196 MLXSW_REG_MCDA_MAX_DATA_LEN);
200 static int mlxsw_sp_fsm_lock(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle)
202 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
203 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
204 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
205 char mcc_pl[MLXSW_REG_MCC_LEN];
209 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
210 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
214 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
215 if (control_state != MLXFW_FSM_STATE_IDLE)
218 mlxsw_reg_mcc_pack(mcc_pl,
219 MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
221 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
224 static int mlxsw_sp_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
225 u32 fwhandle, u16 component_index,
228 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
229 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
230 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
231 char mcc_pl[MLXSW_REG_MCC_LEN];
233 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
234 component_index, fwhandle, component_size);
235 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
238 static int mlxsw_sp_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
239 u32 fwhandle, u8 *data, u16 size,
242 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
243 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
244 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
245 char mcda_pl[MLXSW_REG_MCDA_LEN];
247 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
248 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcda), mcda_pl);
251 static int mlxsw_sp_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
252 u32 fwhandle, u16 component_index)
254 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
255 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
256 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
257 char mcc_pl[MLXSW_REG_MCC_LEN];
259 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
260 component_index, fwhandle, 0);
261 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
264 static int mlxsw_sp_fsm_activate(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
266 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
267 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
268 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
269 char mcc_pl[MLXSW_REG_MCC_LEN];
271 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE, 0,
273 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
276 static int mlxsw_sp_fsm_query_state(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
277 enum mlxfw_fsm_state *fsm_state,
278 enum mlxfw_fsm_state_err *fsm_state_err)
280 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
281 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
282 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
283 char mcc_pl[MLXSW_REG_MCC_LEN];
288 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
289 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
293 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
294 *fsm_state = control_state;
295 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
296 MLXFW_FSM_STATE_ERR_MAX);
300 static void mlxsw_sp_fsm_cancel(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
302 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
303 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
304 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
305 char mcc_pl[MLXSW_REG_MCC_LEN];
307 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL, 0,
309 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
312 static void mlxsw_sp_fsm_release(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
314 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
315 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
316 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
317 char mcc_pl[MLXSW_REG_MCC_LEN];
319 mlxsw_reg_mcc_pack(mcc_pl,
320 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE, 0,
322 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
325 static void mlxsw_sp_status_notify(struct mlxfw_dev *mlxfw_dev,
326 const char *msg, const char *comp_name,
327 u32 done_bytes, u32 total_bytes)
329 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
330 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
331 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
333 devlink_flash_update_status_notify(priv_to_devlink(mlxsw_sp->core),
335 done_bytes, total_bytes);
338 static const struct mlxfw_dev_ops mlxsw_sp_mlxfw_dev_ops = {
339 .component_query = mlxsw_sp_component_query,
340 .fsm_lock = mlxsw_sp_fsm_lock,
341 .fsm_component_update = mlxsw_sp_fsm_component_update,
342 .fsm_block_download = mlxsw_sp_fsm_block_download,
343 .fsm_component_verify = mlxsw_sp_fsm_component_verify,
344 .fsm_activate = mlxsw_sp_fsm_activate,
345 .fsm_query_state = mlxsw_sp_fsm_query_state,
346 .fsm_cancel = mlxsw_sp_fsm_cancel,
347 .fsm_release = mlxsw_sp_fsm_release,
348 .status_notify = mlxsw_sp_status_notify,
351 static int mlxsw_sp_firmware_flash(struct mlxsw_sp *mlxsw_sp,
352 const struct firmware *firmware,
353 struct netlink_ext_ack *extack)
355 struct mlxsw_sp_mlxfw_dev mlxsw_sp_mlxfw_dev = {
357 .ops = &mlxsw_sp_mlxfw_dev_ops,
358 .psid = mlxsw_sp->bus_info->psid,
359 .psid_size = strlen(mlxsw_sp->bus_info->psid),
365 mlxsw_core_fw_flash_start(mlxsw_sp->core);
366 devlink_flash_update_begin_notify(priv_to_devlink(mlxsw_sp->core));
367 err = mlxfw_firmware_flash(&mlxsw_sp_mlxfw_dev.mlxfw_dev,
369 devlink_flash_update_end_notify(priv_to_devlink(mlxsw_sp->core));
370 mlxsw_core_fw_flash_end(mlxsw_sp->core);
375 static int mlxsw_sp_fw_rev_validate(struct mlxsw_sp *mlxsw_sp)
377 const struct mlxsw_fw_rev *rev = &mlxsw_sp->bus_info->fw_rev;
378 const struct mlxsw_fw_rev *req_rev = mlxsw_sp->req_rev;
379 const char *fw_filename = mlxsw_sp->fw_filename;
380 union devlink_param_value value;
381 const struct firmware *firmware;
384 /* Don't check if driver does not require it */
385 if (!req_rev || !fw_filename)
388 /* Don't check if devlink 'fw_load_policy' param is 'flash' */
389 err = devlink_param_driverinit_value_get(priv_to_devlink(mlxsw_sp->core),
390 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
394 if (value.vu8 == DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)
397 /* Validate driver & FW are compatible */
398 if (rev->major != req_rev->major) {
399 WARN(1, "Mismatch in major FW version [%d:%d] is never expected; Please contact support\n",
400 rev->major, req_rev->major);
403 if (MLXSW_SP_FWREV_MINOR_TO_BRANCH(rev->minor) ==
404 MLXSW_SP_FWREV_MINOR_TO_BRANCH(req_rev->minor) &&
405 (rev->minor > req_rev->minor ||
406 (rev->minor == req_rev->minor &&
407 rev->subminor >= req_rev->subminor)))
410 dev_info(mlxsw_sp->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver\n",
411 rev->major, rev->minor, rev->subminor);
412 dev_info(mlxsw_sp->bus_info->dev, "Flashing firmware using file %s\n",
415 err = reject_firmware_direct(&firmware, fw_filename,
416 mlxsw_sp->bus_info->dev);
418 dev_err(mlxsw_sp->bus_info->dev, "Could not request firmware file %s\n",
423 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, NULL);
424 release_firmware(firmware);
426 dev_err(mlxsw_sp->bus_info->dev, "Could not upgrade firmware\n");
428 /* On FW flash success, tell the caller FW reset is needed
429 * if current FW supports it.
431 if (rev->minor >= req_rev->can_reset_minor)
432 return err ? err : -EAGAIN;
437 static int mlxsw_sp_flash_update(struct mlxsw_core *mlxsw_core,
438 const char *file_name, const char *component,
439 struct netlink_ext_ack *extack)
441 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
442 const struct firmware *firmware;
448 err = request_firmware_direct(&firmware, file_name,
449 mlxsw_sp->bus_info->dev);
452 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, extack);
453 release_firmware(firmware);
458 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
459 unsigned int counter_index, u64 *packets,
462 char mgpc_pl[MLXSW_REG_MGPC_LEN];
465 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
466 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
467 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
471 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
473 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
477 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
478 unsigned int counter_index)
480 char mgpc_pl[MLXSW_REG_MGPC_LEN];
482 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
483 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
484 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
487 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
488 unsigned int *p_counter_index)
492 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
496 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
498 goto err_counter_clear;
502 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
507 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
508 unsigned int counter_index)
510 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
514 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
515 const struct mlxsw_tx_info *tx_info)
517 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
519 memset(txhdr, 0, MLXSW_TXHDR_LEN);
521 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
522 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
523 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
524 mlxsw_tx_hdr_swid_set(txhdr, 0);
525 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
526 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
527 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
530 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
533 case BR_STATE_FORWARDING:
534 return MLXSW_REG_SPMS_STATE_FORWARDING;
535 case BR_STATE_LEARNING:
536 return MLXSW_REG_SPMS_STATE_LEARNING;
537 case BR_STATE_LISTENING: /* fall-through */
538 case BR_STATE_DISABLED: /* fall-through */
539 case BR_STATE_BLOCKING:
540 return MLXSW_REG_SPMS_STATE_DISCARDING;
546 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
549 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
550 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
554 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
557 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
558 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
560 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
565 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
567 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
570 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
573 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
577 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
578 bool enable, u32 rate)
580 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
581 char mpsc_pl[MLXSW_REG_MPSC_LEN];
583 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
584 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
587 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
590 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
591 char paos_pl[MLXSW_REG_PAOS_LEN];
593 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
594 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
595 MLXSW_PORT_ADMIN_STATUS_DOWN);
596 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
599 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
602 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
603 char ppad_pl[MLXSW_REG_PPAD_LEN];
605 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
606 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
607 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
610 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
612 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
613 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
615 ether_addr_copy(addr, mlxsw_sp->base_mac);
616 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
617 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
620 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
622 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
623 char pmtu_pl[MLXSW_REG_PMTU_LEN];
627 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
628 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
629 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
632 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
637 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
638 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
641 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
643 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
644 char pspa_pl[MLXSW_REG_PSPA_LEN];
646 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
647 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
650 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
652 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
653 char svpe_pl[MLXSW_REG_SVPE_LEN];
655 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
656 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
659 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
662 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
666 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
669 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
671 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
676 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
679 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
680 char spvid_pl[MLXSW_REG_SPVID_LEN];
682 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
683 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
686 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
689 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
690 char spaft_pl[MLXSW_REG_SPAFT_LEN];
692 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
693 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
696 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
701 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
705 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
708 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
710 goto err_port_allow_untagged_set;
713 mlxsw_sp_port->pvid = vid;
716 err_port_allow_untagged_set:
717 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
722 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
724 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
725 char sspr_pl[MLXSW_REG_SSPR_LEN];
727 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
728 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
731 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
732 u8 local_port, u8 *p_module,
733 u8 *p_width, u8 *p_lane)
735 char pmlp_pl[MLXSW_REG_PMLP_LEN];
738 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
739 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
742 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
743 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
744 *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
748 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port,
749 u8 module, u8 width, u8 lane)
751 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
752 char pmlp_pl[MLXSW_REG_PMLP_LEN];
755 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
756 mlxsw_reg_pmlp_width_set(pmlp_pl, width);
757 for (i = 0; i < width; i++) {
758 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
759 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */
762 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
765 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
767 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
768 char pmlp_pl[MLXSW_REG_PMLP_LEN];
770 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
771 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
772 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
775 static int mlxsw_sp_port_open(struct net_device *dev)
777 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
780 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
783 netif_start_queue(dev);
787 static int mlxsw_sp_port_stop(struct net_device *dev)
789 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
791 netif_stop_queue(dev);
792 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
795 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
796 struct net_device *dev)
798 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
799 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
800 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
801 const struct mlxsw_tx_info tx_info = {
802 .local_port = mlxsw_sp_port->local_port,
808 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
810 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
811 return NETDEV_TX_BUSY;
813 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
814 struct sk_buff *skb_orig = skb;
816 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
818 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
819 dev_kfree_skb_any(skb_orig);
822 dev_consume_skb_any(skb_orig);
825 if (eth_skb_pad(skb)) {
826 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
830 mlxsw_sp_txhdr_construct(skb, &tx_info);
831 /* TX header is consumed by HW on the way so we shouldn't count its
832 * bytes as being sent.
834 len = skb->len - MLXSW_TXHDR_LEN;
836 /* Due to a race we might fail here because of a full queue. In that
837 * unlikely case we simply drop the packet.
839 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
842 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
843 u64_stats_update_begin(&pcpu_stats->syncp);
844 pcpu_stats->tx_packets++;
845 pcpu_stats->tx_bytes += len;
846 u64_stats_update_end(&pcpu_stats->syncp);
848 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
849 dev_kfree_skb_any(skb);
854 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
858 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
860 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
861 struct sockaddr *addr = p;
864 if (!is_valid_ether_addr(addr->sa_data))
865 return -EADDRNOTAVAIL;
867 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
870 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
874 static u16 mlxsw_sp_pg_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp,
877 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
880 #define MLXSW_SP_CELL_FACTOR 2 /* 2 * cell_size / (IPG + cell_size + 1) */
882 static u16 mlxsw_sp_pfc_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
885 delay = mlxsw_sp_bytes_cells(mlxsw_sp, DIV_ROUND_UP(delay,
887 return MLXSW_SP_CELL_FACTOR * delay + mlxsw_sp_bytes_cells(mlxsw_sp,
891 /* Maximum delay buffer needed in case of PAUSE frames, in bytes.
892 * Assumes 100m cable and maximum MTU.
894 #define MLXSW_SP_PAUSE_DELAY 58752
896 static u16 mlxsw_sp_pg_buf_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
897 u16 delay, bool pfc, bool pause)
900 return mlxsw_sp_pfc_delay_get(mlxsw_sp, mtu, delay);
902 return mlxsw_sp_bytes_cells(mlxsw_sp, MLXSW_SP_PAUSE_DELAY);
907 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres,
911 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
913 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
917 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
918 u8 *prio_tc, bool pause_en,
919 struct ieee_pfc *my_pfc)
921 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
922 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
923 u16 delay = !!my_pfc ? my_pfc->delay : 0;
924 char pbmc_pl[MLXSW_REG_PBMC_LEN];
925 u32 taken_headroom_cells = 0;
926 u32 max_headroom_cells;
929 max_headroom_cells = mlxsw_sp_sb_max_headroom_cells(mlxsw_sp);
931 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
932 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
936 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
937 bool configure = false;
944 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
945 if (prio_tc[j] == i) {
946 pfc = pfc_en & BIT(j);
955 lossy = !(pfc || pause_en);
956 thres_cells = mlxsw_sp_pg_buf_threshold_get(mlxsw_sp, mtu);
957 delay_cells = mlxsw_sp_pg_buf_delay_get(mlxsw_sp, mtu, delay,
959 total_cells = thres_cells + delay_cells;
961 taken_headroom_cells += total_cells;
962 if (taken_headroom_cells > max_headroom_cells)
965 mlxsw_sp_pg_buf_pack(pbmc_pl, i, total_cells,
969 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
972 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
973 int mtu, bool pause_en)
975 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
976 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
977 struct ieee_pfc *my_pfc;
980 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
981 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
983 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
987 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
989 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
990 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
993 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
996 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
998 goto err_span_port_mtu_update;
999 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
1001 goto err_port_mtu_set;
1006 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
1007 err_span_port_mtu_update:
1008 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1013 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
1014 struct rtnl_link_stats64 *stats)
1016 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1017 struct mlxsw_sp_port_pcpu_stats *p;
1018 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1023 for_each_possible_cpu(i) {
1024 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
1026 start = u64_stats_fetch_begin_irq(&p->syncp);
1027 rx_packets = p->rx_packets;
1028 rx_bytes = p->rx_bytes;
1029 tx_packets = p->tx_packets;
1030 tx_bytes = p->tx_bytes;
1031 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1033 stats->rx_packets += rx_packets;
1034 stats->rx_bytes += rx_bytes;
1035 stats->tx_packets += tx_packets;
1036 stats->tx_bytes += tx_bytes;
1037 /* tx_dropped is u32, updated without syncp protection. */
1038 tx_dropped += p->tx_dropped;
1040 stats->tx_dropped = tx_dropped;
1044 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
1047 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1054 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
1058 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1059 return mlxsw_sp_port_get_sw_stats64(dev, sp);
1065 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
1066 int prio, char *ppcnt_pl)
1068 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1069 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1071 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1072 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1075 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
1076 struct rtnl_link_stats64 *stats)
1078 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1081 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
1087 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
1089 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
1091 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
1093 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
1095 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
1097 stats->rx_crc_errors =
1098 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
1099 stats->rx_frame_errors =
1100 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
1102 stats->rx_length_errors = (
1103 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
1104 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
1105 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
1107 stats->rx_errors = (stats->rx_crc_errors +
1108 stats->rx_frame_errors + stats->rx_length_errors);
1115 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
1116 struct mlxsw_sp_port_xstats *xstats)
1118 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1121 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
1124 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
1126 for (i = 0; i < TC_MAX_QUEUE; i++) {
1127 err = mlxsw_sp_port_get_stats_raw(dev,
1128 MLXSW_REG_PPCNT_TC_CONG_TC,
1131 xstats->wred_drop[i] =
1132 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
1134 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
1139 xstats->backlog[i] =
1140 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1141 xstats->tail_drop[i] =
1142 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
1145 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1146 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
1151 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
1152 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
1156 static void update_stats_cache(struct work_struct *work)
1158 struct mlxsw_sp_port *mlxsw_sp_port =
1159 container_of(work, struct mlxsw_sp_port,
1160 periodic_hw_stats.update_dw.work);
1162 if (!netif_carrier_ok(mlxsw_sp_port->dev))
1165 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
1166 &mlxsw_sp_port->periodic_hw_stats.stats);
1167 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
1168 &mlxsw_sp_port->periodic_hw_stats.xstats);
1171 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1172 MLXSW_HW_STATS_UPDATE_TIME);
1175 /* Return the stats from a cache that is updated periodically,
1176 * as this function might get called in an atomic context.
1179 mlxsw_sp_port_get_stats64(struct net_device *dev,
1180 struct rtnl_link_stats64 *stats)
1182 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1184 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
1187 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1188 u16 vid_begin, u16 vid_end,
1189 bool is_member, bool untagged)
1191 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1195 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1199 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1200 vid_end, is_member, untagged);
1201 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1206 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1207 u16 vid_end, bool is_member, bool untagged)
1212 for (vid = vid_begin; vid <= vid_end;
1213 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1214 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1217 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1218 is_member, untagged);
1226 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1229 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1231 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1232 &mlxsw_sp_port->vlans_list, list) {
1233 if (!flush_default &&
1234 mlxsw_sp_port_vlan->vid == MLXSW_SP_DEFAULT_VID)
1236 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1241 mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1243 if (mlxsw_sp_port_vlan->bridge_port)
1244 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1245 else if (mlxsw_sp_port_vlan->fid)
1246 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1249 struct mlxsw_sp_port_vlan *
1250 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1252 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1253 bool untagged = vid == MLXSW_SP_DEFAULT_VID;
1256 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1257 if (mlxsw_sp_port_vlan)
1258 return ERR_PTR(-EEXIST);
1260 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1262 return ERR_PTR(err);
1264 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1265 if (!mlxsw_sp_port_vlan) {
1267 goto err_port_vlan_alloc;
1270 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1271 mlxsw_sp_port_vlan->vid = vid;
1272 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1274 return mlxsw_sp_port_vlan;
1276 err_port_vlan_alloc:
1277 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1278 return ERR_PTR(err);
1281 void mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1283 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1284 u16 vid = mlxsw_sp_port_vlan->vid;
1286 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port_vlan);
1287 list_del(&mlxsw_sp_port_vlan->list);
1288 kfree(mlxsw_sp_port_vlan);
1289 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1292 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1293 __be16 __always_unused proto, u16 vid)
1295 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1297 /* VLAN 0 is added to HW filter when device goes up, but it is
1298 * reserved in our case, so simply return.
1303 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid));
1306 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1307 __be16 __always_unused proto, u16 vid)
1309 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1310 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1312 /* VLAN 0 is removed from HW filter when device goes down, but
1313 * it is reserved in our case, so simply return.
1318 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1319 if (!mlxsw_sp_port_vlan)
1321 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1326 static struct mlxsw_sp_port_mall_tc_entry *
1327 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1328 unsigned long cookie) {
1329 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1331 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1332 if (mall_tc_entry->cookie == cookie)
1333 return mall_tc_entry;
1339 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1340 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1341 const struct flow_action_entry *act,
1344 enum mlxsw_sp_span_type span_type;
1347 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1351 mirror->ingress = ingress;
1352 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1353 return mlxsw_sp_span_mirror_add(mlxsw_sp_port, act->dev, span_type,
1354 true, &mirror->span_id);
1358 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1359 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1361 enum mlxsw_sp_span_type span_type;
1363 span_type = mirror->ingress ?
1364 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1365 mlxsw_sp_span_mirror_del(mlxsw_sp_port, mirror->span_id,
1370 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1371 struct tc_cls_matchall_offload *cls,
1372 const struct flow_action_entry *act,
1377 if (!mlxsw_sp_port->sample)
1379 if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1380 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1383 if (act->sample.rate > MLXSW_REG_MPSC_RATE_MAX) {
1384 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1388 rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1389 act->sample.psample_group);
1390 mlxsw_sp_port->sample->truncate = act->sample.truncate;
1391 mlxsw_sp_port->sample->trunc_size = act->sample.trunc_size;
1392 mlxsw_sp_port->sample->rate = act->sample.rate;
1394 err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, act->sample.rate);
1396 goto err_port_sample_set;
1399 err_port_sample_set:
1400 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1405 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1407 if (!mlxsw_sp_port->sample)
1410 mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1411 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1414 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1415 struct tc_cls_matchall_offload *f,
1418 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1419 __be16 protocol = f->common.protocol;
1420 struct flow_action_entry *act;
1423 if (!flow_offload_has_one_action(&f->rule->action)) {
1424 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1428 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1431 mall_tc_entry->cookie = f->cookie;
1433 act = &f->rule->action.entries[0];
1435 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
1436 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1438 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1439 mirror = &mall_tc_entry->mirror;
1440 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1443 } else if (act->id == FLOW_ACTION_SAMPLE &&
1444 protocol == htons(ETH_P_ALL)) {
1445 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1446 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, f,
1453 goto err_add_action;
1455 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1459 kfree(mall_tc_entry);
1463 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1464 struct tc_cls_matchall_offload *f)
1466 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1468 mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1470 if (!mall_tc_entry) {
1471 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1474 list_del(&mall_tc_entry->list);
1476 switch (mall_tc_entry->type) {
1477 case MLXSW_SP_PORT_MALL_MIRROR:
1478 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1479 &mall_tc_entry->mirror);
1481 case MLXSW_SP_PORT_MALL_SAMPLE:
1482 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1488 kfree(mall_tc_entry);
1491 static int mlxsw_sp_setup_tc_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1492 struct tc_cls_matchall_offload *f,
1495 switch (f->command) {
1496 case TC_CLSMATCHALL_REPLACE:
1497 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, f,
1499 case TC_CLSMATCHALL_DESTROY:
1500 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, f);
1508 mlxsw_sp_setup_tc_cls_flower(struct mlxsw_sp_acl_block *acl_block,
1509 struct flow_cls_offload *f)
1511 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_acl_block_mlxsw_sp(acl_block);
1513 switch (f->command) {
1514 case FLOW_CLS_REPLACE:
1515 return mlxsw_sp_flower_replace(mlxsw_sp, acl_block, f);
1516 case FLOW_CLS_DESTROY:
1517 mlxsw_sp_flower_destroy(mlxsw_sp, acl_block, f);
1519 case FLOW_CLS_STATS:
1520 return mlxsw_sp_flower_stats(mlxsw_sp, acl_block, f);
1521 case FLOW_CLS_TMPLT_CREATE:
1522 return mlxsw_sp_flower_tmplt_create(mlxsw_sp, acl_block, f);
1523 case FLOW_CLS_TMPLT_DESTROY:
1524 mlxsw_sp_flower_tmplt_destroy(mlxsw_sp, acl_block, f);
1531 static int mlxsw_sp_setup_tc_block_cb_matchall(enum tc_setup_type type,
1533 void *cb_priv, bool ingress)
1535 struct mlxsw_sp_port *mlxsw_sp_port = cb_priv;
1538 case TC_SETUP_CLSMATCHALL:
1539 if (!tc_cls_can_offload_and_chain0(mlxsw_sp_port->dev,
1543 return mlxsw_sp_setup_tc_cls_matchall(mlxsw_sp_port, type_data,
1545 case TC_SETUP_CLSFLOWER:
1552 static int mlxsw_sp_setup_tc_block_cb_matchall_ig(enum tc_setup_type type,
1556 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1560 static int mlxsw_sp_setup_tc_block_cb_matchall_eg(enum tc_setup_type type,
1564 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1568 static int mlxsw_sp_setup_tc_block_cb_flower(enum tc_setup_type type,
1569 void *type_data, void *cb_priv)
1571 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1574 case TC_SETUP_CLSMATCHALL:
1576 case TC_SETUP_CLSFLOWER:
1577 if (mlxsw_sp_acl_block_disabled(acl_block))
1580 return mlxsw_sp_setup_tc_cls_flower(acl_block, type_data);
1586 static void mlxsw_sp_tc_block_flower_release(void *cb_priv)
1588 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1590 mlxsw_sp_acl_block_destroy(acl_block);
1593 static LIST_HEAD(mlxsw_sp_block_cb_list);
1596 mlxsw_sp_setup_tc_block_flower_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1597 struct flow_block_offload *f, bool ingress)
1599 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1600 struct mlxsw_sp_acl_block *acl_block;
1601 struct flow_block_cb *block_cb;
1602 bool register_block = false;
1605 block_cb = flow_block_cb_lookup(f->block,
1606 mlxsw_sp_setup_tc_block_cb_flower,
1609 acl_block = mlxsw_sp_acl_block_create(mlxsw_sp, f->net);
1612 block_cb = flow_block_cb_alloc(mlxsw_sp_setup_tc_block_cb_flower,
1613 mlxsw_sp, acl_block,
1614 mlxsw_sp_tc_block_flower_release);
1615 if (IS_ERR(block_cb)) {
1616 mlxsw_sp_acl_block_destroy(acl_block);
1617 err = PTR_ERR(block_cb);
1618 goto err_cb_register;
1620 register_block = true;
1622 acl_block = flow_block_cb_priv(block_cb);
1624 flow_block_cb_incref(block_cb);
1625 err = mlxsw_sp_acl_block_bind(mlxsw_sp, acl_block,
1626 mlxsw_sp_port, ingress);
1628 goto err_block_bind;
1631 mlxsw_sp_port->ing_acl_block = acl_block;
1633 mlxsw_sp_port->eg_acl_block = acl_block;
1635 if (register_block) {
1636 flow_block_cb_add(block_cb, f);
1637 list_add_tail(&block_cb->driver_list, &mlxsw_sp_block_cb_list);
1643 if (!flow_block_cb_decref(block_cb))
1644 flow_block_cb_free(block_cb);
1650 mlxsw_sp_setup_tc_block_flower_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1651 struct flow_block_offload *f, bool ingress)
1653 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1654 struct mlxsw_sp_acl_block *acl_block;
1655 struct flow_block_cb *block_cb;
1658 block_cb = flow_block_cb_lookup(f->block,
1659 mlxsw_sp_setup_tc_block_cb_flower,
1665 mlxsw_sp_port->ing_acl_block = NULL;
1667 mlxsw_sp_port->eg_acl_block = NULL;
1669 acl_block = flow_block_cb_priv(block_cb);
1670 err = mlxsw_sp_acl_block_unbind(mlxsw_sp, acl_block,
1671 mlxsw_sp_port, ingress);
1672 if (!err && !flow_block_cb_decref(block_cb)) {
1673 flow_block_cb_remove(block_cb, f);
1674 list_del(&block_cb->driver_list);
1678 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1679 struct flow_block_offload *f)
1681 struct flow_block_cb *block_cb;
1682 flow_setup_cb_t *cb;
1686 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1687 cb = mlxsw_sp_setup_tc_block_cb_matchall_ig;
1689 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1690 cb = mlxsw_sp_setup_tc_block_cb_matchall_eg;
1696 f->driver_block_list = &mlxsw_sp_block_cb_list;
1698 switch (f->command) {
1699 case FLOW_BLOCK_BIND:
1700 if (flow_block_cb_is_busy(cb, mlxsw_sp_port,
1701 &mlxsw_sp_block_cb_list))
1704 block_cb = flow_block_cb_alloc(cb, mlxsw_sp_port,
1705 mlxsw_sp_port, NULL);
1706 if (IS_ERR(block_cb))
1707 return PTR_ERR(block_cb);
1708 err = mlxsw_sp_setup_tc_block_flower_bind(mlxsw_sp_port, f,
1711 flow_block_cb_free(block_cb);
1714 flow_block_cb_add(block_cb, f);
1715 list_add_tail(&block_cb->driver_list, &mlxsw_sp_block_cb_list);
1717 case FLOW_BLOCK_UNBIND:
1718 mlxsw_sp_setup_tc_block_flower_unbind(mlxsw_sp_port,
1720 block_cb = flow_block_cb_lookup(f->block, cb, mlxsw_sp_port);
1724 flow_block_cb_remove(block_cb, f);
1725 list_del(&block_cb->driver_list);
1732 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1735 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1738 case TC_SETUP_BLOCK:
1739 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1740 case TC_SETUP_QDISC_RED:
1741 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1742 case TC_SETUP_QDISC_PRIO:
1743 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1750 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1752 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1755 if (mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->ing_acl_block) ||
1756 mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->eg_acl_block) ||
1757 !list_empty(&mlxsw_sp_port->mall_tc_list)) {
1758 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1761 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->ing_acl_block);
1762 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->eg_acl_block);
1764 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->ing_acl_block);
1765 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->eg_acl_block);
1770 static int mlxsw_sp_feature_loopback(struct net_device *dev, bool enable)
1772 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1773 char pplr_pl[MLXSW_REG_PPLR_LEN];
1776 if (netif_running(dev))
1777 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1779 mlxsw_reg_pplr_pack(pplr_pl, mlxsw_sp_port->local_port, enable);
1780 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pplr),
1783 if (netif_running(dev))
1784 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1789 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1791 static int mlxsw_sp_handle_feature(struct net_device *dev,
1792 netdev_features_t wanted_features,
1793 netdev_features_t feature,
1794 mlxsw_sp_feature_handler feature_handler)
1796 netdev_features_t changes = wanted_features ^ dev->features;
1797 bool enable = !!(wanted_features & feature);
1800 if (!(changes & feature))
1803 err = feature_handler(dev, enable);
1805 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1806 enable ? "Enable" : "Disable", &feature, err);
1811 dev->features |= feature;
1813 dev->features &= ~feature;
1817 static int mlxsw_sp_set_features(struct net_device *dev,
1818 netdev_features_t features)
1820 netdev_features_t oper_features = dev->features;
1823 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1824 mlxsw_sp_feature_hw_tc);
1825 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_LOOPBACK,
1826 mlxsw_sp_feature_loopback);
1829 dev->features = oper_features;
1836 static struct devlink_port *
1837 mlxsw_sp_port_get_devlink_port(struct net_device *dev)
1839 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1840 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1842 return mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
1843 mlxsw_sp_port->local_port);
1846 static int mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1849 struct hwtstamp_config config;
1852 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1855 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port,
1860 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1866 static int mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1869 struct hwtstamp_config config;
1872 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_get(mlxsw_sp_port,
1877 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1883 static inline void mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port *mlxsw_sp_port)
1885 struct hwtstamp_config config = {0};
1887 mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port, &config);
1891 mlxsw_sp_port_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1893 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1897 return mlxsw_sp_port_hwtstamp_set(mlxsw_sp_port, ifr);
1899 return mlxsw_sp_port_hwtstamp_get(mlxsw_sp_port, ifr);
1905 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1906 .ndo_open = mlxsw_sp_port_open,
1907 .ndo_stop = mlxsw_sp_port_stop,
1908 .ndo_start_xmit = mlxsw_sp_port_xmit,
1909 .ndo_setup_tc = mlxsw_sp_setup_tc,
1910 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1911 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1912 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1913 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1914 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1915 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1916 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1917 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1918 .ndo_set_features = mlxsw_sp_set_features,
1919 .ndo_get_devlink_port = mlxsw_sp_port_get_devlink_port,
1920 .ndo_do_ioctl = mlxsw_sp_port_ioctl,
1923 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1924 struct ethtool_drvinfo *drvinfo)
1926 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1927 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1929 strlcpy(drvinfo->driver, mlxsw_sp->bus_info->device_kind,
1930 sizeof(drvinfo->driver));
1931 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1932 sizeof(drvinfo->version));
1933 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1935 mlxsw_sp->bus_info->fw_rev.major,
1936 mlxsw_sp->bus_info->fw_rev.minor,
1937 mlxsw_sp->bus_info->fw_rev.subminor);
1938 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1939 sizeof(drvinfo->bus_info));
1942 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1943 struct ethtool_pauseparam *pause)
1945 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1947 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1948 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1951 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1952 struct ethtool_pauseparam *pause)
1954 char pfcc_pl[MLXSW_REG_PFCC_LEN];
1956 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1957 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
1958 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
1960 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
1964 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
1965 struct ethtool_pauseparam *pause)
1967 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1968 bool pause_en = pause->tx_pause || pause->rx_pause;
1971 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
1972 netdev_err(dev, "PFC already enabled on port\n");
1976 if (pause->autoneg) {
1977 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
1981 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1983 netdev_err(dev, "Failed to configure port's headroom\n");
1987 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
1989 netdev_err(dev, "Failed to set PAUSE parameters\n");
1990 goto err_port_pause_configure;
1993 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
1994 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
1998 err_port_pause_configure:
1999 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
2000 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
2004 struct mlxsw_sp_port_hw_stats {
2005 char str[ETH_GSTRING_LEN];
2006 u64 (*getter)(const char *payload);
2010 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
2012 .str = "a_frames_transmitted_ok",
2013 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
2016 .str = "a_frames_received_ok",
2017 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
2020 .str = "a_frame_check_sequence_errors",
2021 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
2024 .str = "a_alignment_errors",
2025 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
2028 .str = "a_octets_transmitted_ok",
2029 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
2032 .str = "a_octets_received_ok",
2033 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
2036 .str = "a_multicast_frames_xmitted_ok",
2037 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
2040 .str = "a_broadcast_frames_xmitted_ok",
2041 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
2044 .str = "a_multicast_frames_received_ok",
2045 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
2048 .str = "a_broadcast_frames_received_ok",
2049 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
2052 .str = "a_in_range_length_errors",
2053 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
2056 .str = "a_out_of_range_length_field",
2057 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
2060 .str = "a_frame_too_long_errors",
2061 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
2064 .str = "a_symbol_error_during_carrier",
2065 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
2068 .str = "a_mac_control_frames_transmitted",
2069 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
2072 .str = "a_mac_control_frames_received",
2073 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
2076 .str = "a_unsupported_opcodes_received",
2077 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
2080 .str = "a_pause_mac_ctrl_frames_received",
2081 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
2084 .str = "a_pause_mac_ctrl_frames_xmitted",
2085 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
2089 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
2091 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2863_stats[] = {
2093 .str = "if_in_discards",
2094 .getter = mlxsw_reg_ppcnt_if_in_discards_get,
2097 .str = "if_out_discards",
2098 .getter = mlxsw_reg_ppcnt_if_out_discards_get,
2101 .str = "if_out_errors",
2102 .getter = mlxsw_reg_ppcnt_if_out_errors_get,
2106 #define MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN \
2107 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2863_stats)
2109 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2819_stats[] = {
2111 .str = "ether_stats_undersize_pkts",
2112 .getter = mlxsw_reg_ppcnt_ether_stats_undersize_pkts_get,
2115 .str = "ether_stats_oversize_pkts",
2116 .getter = mlxsw_reg_ppcnt_ether_stats_oversize_pkts_get,
2119 .str = "ether_stats_fragments",
2120 .getter = mlxsw_reg_ppcnt_ether_stats_fragments_get,
2123 .str = "ether_pkts64octets",
2124 .getter = mlxsw_reg_ppcnt_ether_stats_pkts64octets_get,
2127 .str = "ether_pkts65to127octets",
2128 .getter = mlxsw_reg_ppcnt_ether_stats_pkts65to127octets_get,
2131 .str = "ether_pkts128to255octets",
2132 .getter = mlxsw_reg_ppcnt_ether_stats_pkts128to255octets_get,
2135 .str = "ether_pkts256to511octets",
2136 .getter = mlxsw_reg_ppcnt_ether_stats_pkts256to511octets_get,
2139 .str = "ether_pkts512to1023octets",
2140 .getter = mlxsw_reg_ppcnt_ether_stats_pkts512to1023octets_get,
2143 .str = "ether_pkts1024to1518octets",
2144 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1024to1518octets_get,
2147 .str = "ether_pkts1519to2047octets",
2148 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1519to2047octets_get,
2151 .str = "ether_pkts2048to4095octets",
2152 .getter = mlxsw_reg_ppcnt_ether_stats_pkts2048to4095octets_get,
2155 .str = "ether_pkts4096to8191octets",
2156 .getter = mlxsw_reg_ppcnt_ether_stats_pkts4096to8191octets_get,
2159 .str = "ether_pkts8192to10239octets",
2160 .getter = mlxsw_reg_ppcnt_ether_stats_pkts8192to10239octets_get,
2164 #define MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN \
2165 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2819_stats)
2167 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_3635_stats[] = {
2169 .str = "dot3stats_fcs_errors",
2170 .getter = mlxsw_reg_ppcnt_dot3stats_fcs_errors_get,
2173 .str = "dot3stats_symbol_errors",
2174 .getter = mlxsw_reg_ppcnt_dot3stats_symbol_errors_get,
2177 .str = "dot3control_in_unknown_opcodes",
2178 .getter = mlxsw_reg_ppcnt_dot3control_in_unknown_opcodes_get,
2181 .str = "dot3in_pause_frames",
2182 .getter = mlxsw_reg_ppcnt_dot3in_pause_frames_get,
2186 #define MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN \
2187 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_3635_stats)
2189 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_discard_stats[] = {
2191 .str = "discard_ingress_general",
2192 .getter = mlxsw_reg_ppcnt_ingress_general_get,
2195 .str = "discard_ingress_policy_engine",
2196 .getter = mlxsw_reg_ppcnt_ingress_policy_engine_get,
2199 .str = "discard_ingress_vlan_membership",
2200 .getter = mlxsw_reg_ppcnt_ingress_vlan_membership_get,
2203 .str = "discard_ingress_tag_frame_type",
2204 .getter = mlxsw_reg_ppcnt_ingress_tag_frame_type_get,
2207 .str = "discard_egress_vlan_membership",
2208 .getter = mlxsw_reg_ppcnt_egress_vlan_membership_get,
2211 .str = "discard_loopback_filter",
2212 .getter = mlxsw_reg_ppcnt_loopback_filter_get,
2215 .str = "discard_egress_general",
2216 .getter = mlxsw_reg_ppcnt_egress_general_get,
2219 .str = "discard_egress_hoq",
2220 .getter = mlxsw_reg_ppcnt_egress_hoq_get,
2223 .str = "discard_egress_policy_engine",
2224 .getter = mlxsw_reg_ppcnt_egress_policy_engine_get,
2227 .str = "discard_ingress_tx_link_down",
2228 .getter = mlxsw_reg_ppcnt_ingress_tx_link_down_get,
2231 .str = "discard_egress_stp_filter",
2232 .getter = mlxsw_reg_ppcnt_egress_stp_filter_get,
2235 .str = "discard_egress_sll",
2236 .getter = mlxsw_reg_ppcnt_egress_sll_get,
2240 #define MLXSW_SP_PORT_HW_DISCARD_STATS_LEN \
2241 ARRAY_SIZE(mlxsw_sp_port_hw_discard_stats)
2243 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
2245 .str = "rx_octets_prio",
2246 .getter = mlxsw_reg_ppcnt_rx_octets_get,
2249 .str = "rx_frames_prio",
2250 .getter = mlxsw_reg_ppcnt_rx_frames_get,
2253 .str = "tx_octets_prio",
2254 .getter = mlxsw_reg_ppcnt_tx_octets_get,
2257 .str = "tx_frames_prio",
2258 .getter = mlxsw_reg_ppcnt_tx_frames_get,
2261 .str = "rx_pause_prio",
2262 .getter = mlxsw_reg_ppcnt_rx_pause_get,
2265 .str = "rx_pause_duration_prio",
2266 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
2269 .str = "tx_pause_prio",
2270 .getter = mlxsw_reg_ppcnt_tx_pause_get,
2273 .str = "tx_pause_duration_prio",
2274 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
2278 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
2280 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
2282 .str = "tc_transmit_queue_tc",
2283 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_get,
2284 .cells_bytes = true,
2287 .str = "tc_no_buffer_discard_uc_tc",
2288 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
2292 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
2294 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
2295 MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN + \
2296 MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN + \
2297 MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN + \
2298 MLXSW_SP_PORT_HW_DISCARD_STATS_LEN + \
2299 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN * \
2300 IEEE_8021QAZ_MAX_TCS) + \
2301 (MLXSW_SP_PORT_HW_TC_STATS_LEN * \
2304 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
2308 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
2309 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2310 mlxsw_sp_port_hw_prio_stats[i].str, prio);
2311 *p += ETH_GSTRING_LEN;
2315 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
2319 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
2320 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2321 mlxsw_sp_port_hw_tc_stats[i].str, tc);
2322 *p += ETH_GSTRING_LEN;
2326 static void mlxsw_sp_port_get_strings(struct net_device *dev,
2327 u32 stringset, u8 *data)
2332 switch (stringset) {
2334 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
2335 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
2337 p += ETH_GSTRING_LEN;
2340 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN; i++) {
2341 memcpy(p, mlxsw_sp_port_hw_rfc_2863_stats[i].str,
2343 p += ETH_GSTRING_LEN;
2346 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN; i++) {
2347 memcpy(p, mlxsw_sp_port_hw_rfc_2819_stats[i].str,
2349 p += ETH_GSTRING_LEN;
2352 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN; i++) {
2353 memcpy(p, mlxsw_sp_port_hw_rfc_3635_stats[i].str,
2355 p += ETH_GSTRING_LEN;
2358 for (i = 0; i < MLXSW_SP_PORT_HW_DISCARD_STATS_LEN; i++) {
2359 memcpy(p, mlxsw_sp_port_hw_discard_stats[i].str,
2361 p += ETH_GSTRING_LEN;
2364 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2365 mlxsw_sp_port_get_prio_strings(&p, i);
2367 for (i = 0; i < TC_MAX_QUEUE; i++)
2368 mlxsw_sp_port_get_tc_strings(&p, i);
2374 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
2375 enum ethtool_phys_id_state state)
2377 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2378 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2379 char mlcr_pl[MLXSW_REG_MLCR_LEN];
2383 case ETHTOOL_ID_ACTIVE:
2386 case ETHTOOL_ID_INACTIVE:
2393 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
2394 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
2398 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
2399 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
2402 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
2403 *p_hw_stats = mlxsw_sp_port_hw_stats;
2404 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
2406 case MLXSW_REG_PPCNT_RFC_2863_CNT:
2407 *p_hw_stats = mlxsw_sp_port_hw_rfc_2863_stats;
2408 *p_len = MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2410 case MLXSW_REG_PPCNT_RFC_2819_CNT:
2411 *p_hw_stats = mlxsw_sp_port_hw_rfc_2819_stats;
2412 *p_len = MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2414 case MLXSW_REG_PPCNT_RFC_3635_CNT:
2415 *p_hw_stats = mlxsw_sp_port_hw_rfc_3635_stats;
2416 *p_len = MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2418 case MLXSW_REG_PPCNT_DISCARD_CNT:
2419 *p_hw_stats = mlxsw_sp_port_hw_discard_stats;
2420 *p_len = MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2422 case MLXSW_REG_PPCNT_PRIO_CNT:
2423 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
2424 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2426 case MLXSW_REG_PPCNT_TC_CNT:
2427 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
2428 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
2437 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
2438 enum mlxsw_reg_ppcnt_grp grp, int prio,
2439 u64 *data, int data_index)
2441 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2442 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2443 struct mlxsw_sp_port_hw_stats *hw_stats;
2444 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
2448 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
2451 mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
2452 for (i = 0; i < len; i++) {
2453 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
2454 if (!hw_stats[i].cells_bytes)
2456 data[data_index + i] = mlxsw_sp_cells_bytes(mlxsw_sp,
2457 data[data_index + i]);
2461 static void mlxsw_sp_port_get_stats(struct net_device *dev,
2462 struct ethtool_stats *stats, u64 *data)
2464 int i, data_index = 0;
2466 /* IEEE 802.3 Counters */
2467 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
2469 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
2471 /* RFC 2863 Counters */
2472 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2863_CNT, 0,
2474 data_index += MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2476 /* RFC 2819 Counters */
2477 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2819_CNT, 0,
2479 data_index += MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2481 /* RFC 3635 Counters */
2482 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_3635_CNT, 0,
2484 data_index += MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2486 /* Discard Counters */
2487 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_DISCARD_CNT, 0,
2489 data_index += MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2491 /* Per-Priority Counters */
2492 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2493 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
2495 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2498 /* Per-TC Counters */
2499 for (i = 0; i < TC_MAX_QUEUE; i++) {
2500 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
2502 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
2506 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
2510 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
2516 struct mlxsw_sp1_port_link_mode {
2517 enum ethtool_link_mode_bit_indices mask_ethtool;
2522 static const struct mlxsw_sp1_port_link_mode mlxsw_sp1_port_link_mode[] = {
2524 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
2525 .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2529 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
2530 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
2531 .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2532 .speed = SPEED_1000,
2535 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
2536 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2537 .speed = SPEED_10000,
2540 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
2541 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
2542 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
2543 .speed = SPEED_10000,
2546 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2547 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2548 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2549 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
2550 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2551 .speed = SPEED_10000,
2554 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
2555 .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
2556 .speed = SPEED_20000,
2559 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
2560 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2561 .speed = SPEED_40000,
2564 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
2565 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2566 .speed = SPEED_40000,
2569 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
2570 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2571 .speed = SPEED_40000,
2574 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
2575 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2576 .speed = SPEED_40000,
2579 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
2580 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2581 .speed = SPEED_25000,
2584 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
2585 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2586 .speed = SPEED_25000,
2589 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2590 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2591 .speed = SPEED_25000,
2594 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
2595 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2596 .speed = SPEED_50000,
2599 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
2600 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2601 .speed = SPEED_50000,
2604 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
2605 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2606 .speed = SPEED_50000,
2609 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2610 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT,
2611 .speed = SPEED_56000,
2614 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2615 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT,
2616 .speed = SPEED_56000,
2619 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2620 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT,
2621 .speed = SPEED_56000,
2624 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2625 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT,
2626 .speed = SPEED_56000,
2629 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
2630 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2631 .speed = SPEED_100000,
2634 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
2635 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2636 .speed = SPEED_100000,
2639 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
2640 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2641 .speed = SPEED_100000,
2644 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
2645 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2646 .speed = SPEED_100000,
2650 #define MLXSW_SP1_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp1_port_link_mode)
2653 mlxsw_sp1_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
2655 struct ethtool_link_ksettings *cmd)
2657 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2658 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2659 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2660 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2661 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2662 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2663 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2665 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2666 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2667 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2668 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
2669 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
2670 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2674 mlxsw_sp1_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
2675 unsigned long *mode)
2679 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2680 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask)
2681 __set_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2687 mlxsw_sp1_from_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto)
2691 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2692 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask)
2693 return mlxsw_sp1_port_link_mode[i].speed;
2696 return SPEED_UNKNOWN;
2700 mlxsw_sp1_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
2702 struct ethtool_link_ksettings *cmd)
2704 cmd->base.speed = SPEED_UNKNOWN;
2705 cmd->base.duplex = DUPLEX_UNKNOWN;
2710 cmd->base.speed = mlxsw_sp1_from_ptys_speed(mlxsw_sp, ptys_eth_proto);
2711 if (cmd->base.speed != SPEED_UNKNOWN)
2712 cmd->base.duplex = DUPLEX_FULL;
2716 mlxsw_sp1_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp,
2717 const struct ethtool_link_ksettings *cmd)
2722 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2723 if (test_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2724 cmd->link_modes.advertising))
2725 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2730 static u32 mlxsw_sp1_to_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 speed)
2735 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2736 if (speed == mlxsw_sp1_port_link_mode[i].speed)
2737 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2743 mlxsw_sp1_to_ptys_upper_speed(struct mlxsw_sp *mlxsw_sp, u32 upper_speed)
2748 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2749 if (mlxsw_sp1_port_link_mode[i].speed <= upper_speed)
2750 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2756 mlxsw_sp1_port_speed_base(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2759 *base_speed = MLXSW_SP_PORT_BASE_SPEED_25G;
2764 mlxsw_sp1_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
2765 u8 local_port, u32 proto_admin, bool autoneg)
2767 mlxsw_reg_ptys_eth_pack(payload, local_port, proto_admin, autoneg);
2771 mlxsw_sp1_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
2772 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
2773 u32 *p_eth_proto_oper)
2775 mlxsw_reg_ptys_eth_unpack(payload, p_eth_proto_cap, p_eth_proto_admin,
2779 static const struct mlxsw_sp_port_type_speed_ops
2780 mlxsw_sp1_port_type_speed_ops = {
2781 .from_ptys_supported_port = mlxsw_sp1_from_ptys_supported_port,
2782 .from_ptys_link = mlxsw_sp1_from_ptys_link,
2783 .from_ptys_speed = mlxsw_sp1_from_ptys_speed,
2784 .from_ptys_speed_duplex = mlxsw_sp1_from_ptys_speed_duplex,
2785 .to_ptys_advert_link = mlxsw_sp1_to_ptys_advert_link,
2786 .to_ptys_speed = mlxsw_sp1_to_ptys_speed,
2787 .to_ptys_upper_speed = mlxsw_sp1_to_ptys_upper_speed,
2788 .port_speed_base = mlxsw_sp1_port_speed_base,
2789 .reg_ptys_eth_pack = mlxsw_sp1_reg_ptys_eth_pack,
2790 .reg_ptys_eth_unpack = mlxsw_sp1_reg_ptys_eth_unpack,
2793 static const enum ethtool_link_mode_bit_indices
2794 mlxsw_sp2_mask_ethtool_sgmii_100m[] = {
2795 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2798 #define MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN \
2799 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_sgmii_100m)
2801 static const enum ethtool_link_mode_bit_indices
2802 mlxsw_sp2_mask_ethtool_1000base_x_sgmii[] = {
2803 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2804 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2807 #define MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN \
2808 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_1000base_x_sgmii)
2810 static const enum ethtool_link_mode_bit_indices
2811 mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii[] = {
2812 ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
2815 #define MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN \
2816 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii)
2818 static const enum ethtool_link_mode_bit_indices
2819 mlxsw_sp2_mask_ethtool_5gbase_r[] = {
2820 ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
2823 #define MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN \
2824 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_5gbase_r)
2826 static const enum ethtool_link_mode_bit_indices
2827 mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g[] = {
2828 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2829 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2830 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
2831 ETHTOOL_LINK_MODE_10000baseCR_Full_BIT,
2832 ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
2833 ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
2834 ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
2837 #define MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN \
2838 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g)
2840 static const enum ethtool_link_mode_bit_indices
2841 mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g[] = {
2842 ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2843 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2844 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2845 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2848 #define MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN \
2849 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g)
2851 static const enum ethtool_link_mode_bit_indices
2852 mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr[] = {
2853 ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2854 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2855 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2858 #define MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN \
2859 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr)
2861 static const enum ethtool_link_mode_bit_indices
2862 mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2[] = {
2863 ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2864 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2865 ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2868 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN \
2869 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2)
2871 static const enum ethtool_link_mode_bit_indices
2872 mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr[] = {
2873 ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
2874 ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
2875 ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
2876 ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
2877 ETHTOOL_LINK_MODE_50000baseDR_Full_BIT,
2880 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN \
2881 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr)
2883 static const enum ethtool_link_mode_bit_indices
2884 mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4[] = {
2885 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2886 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2887 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2888 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2891 #define MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN \
2892 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4)
2894 static const enum ethtool_link_mode_bit_indices
2895 mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2[] = {
2896 ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT,
2897 ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT,
2898 ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT,
2899 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
2900 ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT,
2903 #define MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN \
2904 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2)
2906 static const enum ethtool_link_mode_bit_indices
2907 mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4[] = {
2908 ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT,
2909 ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT,
2910 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
2911 ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT,
2912 ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT,
2915 #define MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN \
2916 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4)
2918 struct mlxsw_sp2_port_link_mode {
2919 const enum ethtool_link_mode_bit_indices *mask_ethtool;
2925 static const struct mlxsw_sp2_port_link_mode mlxsw_sp2_port_link_mode[] = {
2927 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_SGMII_100M,
2928 .mask_ethtool = mlxsw_sp2_mask_ethtool_sgmii_100m,
2929 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN,
2933 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_1000BASE_X_SGMII,
2934 .mask_ethtool = mlxsw_sp2_mask_ethtool_1000base_x_sgmii,
2935 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN,
2936 .speed = SPEED_1000,
2939 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_2_5GBASE_X_2_5GMII,
2940 .mask_ethtool = mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii,
2941 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN,
2942 .speed = SPEED_2500,
2945 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_5GBASE_R,
2946 .mask_ethtool = mlxsw_sp2_mask_ethtool_5gbase_r,
2947 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN,
2948 .speed = SPEED_5000,
2951 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XFI_XAUI_1_10G,
2952 .mask_ethtool = mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g,
2953 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN,
2954 .speed = SPEED_10000,
2957 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XLAUI_4_XLPPI_4_40G,
2958 .mask_ethtool = mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g,
2959 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN,
2960 .speed = SPEED_40000,
2963 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_25GAUI_1_25GBASE_CR_KR,
2964 .mask_ethtool = mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr,
2965 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN,
2966 .speed = SPEED_25000,
2969 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_2_LAUI_2_50GBASE_CR2_KR2,
2970 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2,
2971 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN,
2972 .speed = SPEED_50000,
2975 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_1_LAUI_1_50GBASE_CR_KR,
2976 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr,
2977 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN,
2978 .speed = SPEED_50000,
2981 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_CAUI_4_100GBASE_CR4_KR4,
2982 .mask_ethtool = mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4,
2983 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN,
2984 .speed = SPEED_100000,
2987 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_100GAUI_2_100GBASE_CR2_KR2,
2988 .mask_ethtool = mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2,
2989 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN,
2990 .speed = SPEED_100000,
2993 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_200GAUI_4_200GBASE_CR4_KR4,
2994 .mask_ethtool = mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4,
2995 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN,
2996 .speed = SPEED_200000,
3000 #define MLXSW_SP2_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp2_port_link_mode)
3003 mlxsw_sp2_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
3005 struct ethtool_link_ksettings *cmd)
3007 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
3008 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
3012 mlxsw_sp2_set_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
3013 unsigned long *mode)
3017 for (i = 0; i < link_mode->m_ethtool_len; i++)
3018 __set_bit(link_mode->mask_ethtool[i], mode);
3022 mlxsw_sp2_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
3023 unsigned long *mode)
3027 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3028 if (ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask)
3029 mlxsw_sp2_set_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3035 mlxsw_sp2_from_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto)
3039 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3040 if (ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask)
3041 return mlxsw_sp2_port_link_mode[i].speed;
3044 return SPEED_UNKNOWN;
3048 mlxsw_sp2_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
3050 struct ethtool_link_ksettings *cmd)
3052 cmd->base.speed = SPEED_UNKNOWN;
3053 cmd->base.duplex = DUPLEX_UNKNOWN;
3058 cmd->base.speed = mlxsw_sp2_from_ptys_speed(mlxsw_sp, ptys_eth_proto);
3059 if (cmd->base.speed != SPEED_UNKNOWN)
3060 cmd->base.duplex = DUPLEX_FULL;
3064 mlxsw_sp2_test_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
3065 const unsigned long *mode)
3070 for (i = 0; i < link_mode->m_ethtool_len; i++) {
3071 if (test_bit(link_mode->mask_ethtool[i], mode))
3075 return cnt == link_mode->m_ethtool_len;
3079 mlxsw_sp2_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp,
3080 const struct ethtool_link_ksettings *cmd)
3085 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3086 if (mlxsw_sp2_test_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3087 cmd->link_modes.advertising))
3088 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3093 static u32 mlxsw_sp2_to_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 speed)
3098 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3099 if (speed == mlxsw_sp2_port_link_mode[i].speed)
3100 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3106 mlxsw_sp2_to_ptys_upper_speed(struct mlxsw_sp *mlxsw_sp, u32 upper_speed)
3111 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3112 if (mlxsw_sp2_port_link_mode[i].speed <= upper_speed)
3113 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3119 mlxsw_sp2_port_speed_base(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3122 char ptys_pl[MLXSW_REG_PTYS_LEN];
3126 /* In Spectrum-2, the speed of 1x can change from port to port, so query
3129 mlxsw_reg_ptys_ext_eth_pack(ptys_pl, local_port, 0, false);
3130 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3133 mlxsw_reg_ptys_ext_eth_unpack(ptys_pl, ð_proto_cap, NULL, NULL);
3136 MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_1_LAUI_1_50GBASE_CR_KR) {
3137 *base_speed = MLXSW_SP_PORT_BASE_SPEED_50G;
3142 MLXSW_REG_PTYS_EXT_ETH_SPEED_25GAUI_1_25GBASE_CR_KR) {
3143 *base_speed = MLXSW_SP_PORT_BASE_SPEED_25G;
3151 mlxsw_sp2_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
3152 u8 local_port, u32 proto_admin,
3155 mlxsw_reg_ptys_ext_eth_pack(payload, local_port, proto_admin, autoneg);
3159 mlxsw_sp2_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
3160 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
3161 u32 *p_eth_proto_oper)
3163 mlxsw_reg_ptys_ext_eth_unpack(payload, p_eth_proto_cap,
3164 p_eth_proto_admin, p_eth_proto_oper);
3167 static const struct mlxsw_sp_port_type_speed_ops
3168 mlxsw_sp2_port_type_speed_ops = {
3169 .from_ptys_supported_port = mlxsw_sp2_from_ptys_supported_port,
3170 .from_ptys_link = mlxsw_sp2_from_ptys_link,
3171 .from_ptys_speed = mlxsw_sp2_from_ptys_speed,
3172 .from_ptys_speed_duplex = mlxsw_sp2_from_ptys_speed_duplex,
3173 .to_ptys_advert_link = mlxsw_sp2_to_ptys_advert_link,
3174 .to_ptys_speed = mlxsw_sp2_to_ptys_speed,
3175 .to_ptys_upper_speed = mlxsw_sp2_to_ptys_upper_speed,
3176 .port_speed_base = mlxsw_sp2_port_speed_base,
3177 .reg_ptys_eth_pack = mlxsw_sp2_reg_ptys_eth_pack,
3178 .reg_ptys_eth_unpack = mlxsw_sp2_reg_ptys_eth_unpack,
3182 mlxsw_sp_port_get_link_supported(struct mlxsw_sp *mlxsw_sp, u32 eth_proto_cap,
3183 struct ethtool_link_ksettings *cmd)
3185 const struct mlxsw_sp_port_type_speed_ops *ops;
3187 ops = mlxsw_sp->port_type_speed_ops;
3189 ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
3190 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
3191 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
3193 ops->from_ptys_supported_port(mlxsw_sp, eth_proto_cap, cmd);
3194 ops->from_ptys_link(mlxsw_sp, eth_proto_cap, cmd->link_modes.supported);
3198 mlxsw_sp_port_get_link_advertise(struct mlxsw_sp *mlxsw_sp,
3199 u32 eth_proto_admin, bool autoneg,
3200 struct ethtool_link_ksettings *cmd)
3202 const struct mlxsw_sp_port_type_speed_ops *ops;
3204 ops = mlxsw_sp->port_type_speed_ops;
3209 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
3210 ops->from_ptys_link(mlxsw_sp, eth_proto_admin,
3211 cmd->link_modes.advertising);
3215 mlxsw_sp_port_connector_port(enum mlxsw_reg_ptys_connector_type connector_type)
3217 switch (connector_type) {
3218 case MLXSW_REG_PTYS_CONNECTOR_TYPE_UNKNOWN_OR_NO_CONNECTOR:
3220 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_NONE:
3222 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_TP:
3224 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_AUI:
3226 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_BNC:
3228 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_MII:
3230 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_FIBRE:
3232 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_DA:
3234 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_OTHER:
3242 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
3243 struct ethtool_link_ksettings *cmd)
3245 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
3246 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3247 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3248 const struct mlxsw_sp_port_type_speed_ops *ops;
3249 char ptys_pl[MLXSW_REG_PTYS_LEN];
3254 ops = mlxsw_sp->port_type_speed_ops;
3256 autoneg = mlxsw_sp_port->link.autoneg;
3257 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3259 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3262 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
3263 ð_proto_admin, ð_proto_oper);
3265 mlxsw_sp_port_get_link_supported(mlxsw_sp, eth_proto_cap, cmd);
3267 mlxsw_sp_port_get_link_advertise(mlxsw_sp, eth_proto_admin, autoneg,
3270 cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
3271 connector_type = mlxsw_reg_ptys_connector_type_get(ptys_pl);
3272 cmd->base.port = mlxsw_sp_port_connector_port(connector_type);
3273 ops->from_ptys_speed_duplex(mlxsw_sp, netif_carrier_ok(dev),
3274 eth_proto_oper, cmd);
3280 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
3281 const struct ethtool_link_ksettings *cmd)
3283 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3284 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3285 const struct mlxsw_sp_port_type_speed_ops *ops;
3286 char ptys_pl[MLXSW_REG_PTYS_LEN];
3287 u32 eth_proto_cap, eth_proto_new;
3291 ops = mlxsw_sp->port_type_speed_ops;
3293 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3295 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3298 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap, NULL, NULL);
3300 autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
3301 if (!autoneg && cmd->base.speed == SPEED_56000) {
3302 netdev_err(dev, "56G not supported with autoneg off\n");
3305 eth_proto_new = autoneg ?
3306 ops->to_ptys_advert_link(mlxsw_sp, cmd) :
3307 ops->to_ptys_speed(mlxsw_sp, cmd->base.speed);
3309 eth_proto_new = eth_proto_new & eth_proto_cap;
3310 if (!eth_proto_new) {
3311 netdev_err(dev, "No supported speed requested\n");
3315 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3316 eth_proto_new, autoneg);
3317 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3321 mlxsw_sp_port->link.autoneg = autoneg;
3323 if (!netif_running(dev))
3326 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3327 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
3332 static int mlxsw_sp_get_module_info(struct net_device *netdev,
3333 struct ethtool_modinfo *modinfo)
3335 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3336 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3339 err = mlxsw_env_get_module_info(mlxsw_sp->core,
3340 mlxsw_sp_port->mapping.module,
3346 static int mlxsw_sp_get_module_eeprom(struct net_device *netdev,
3347 struct ethtool_eeprom *ee,
3350 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3351 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3354 err = mlxsw_env_get_module_eeprom(netdev, mlxsw_sp->core,
3355 mlxsw_sp_port->mapping.module, ee,
3362 mlxsw_sp_get_ts_info(struct net_device *netdev, struct ethtool_ts_info *info)
3364 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3365 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3367 return mlxsw_sp->ptp_ops->get_ts_info(mlxsw_sp, info);
3370 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
3371 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
3372 .get_link = ethtool_op_get_link,
3373 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
3374 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
3375 .get_strings = mlxsw_sp_port_get_strings,
3376 .set_phys_id = mlxsw_sp_port_set_phys_id,
3377 .get_ethtool_stats = mlxsw_sp_port_get_stats,
3378 .get_sset_count = mlxsw_sp_port_get_sset_count,
3379 .get_link_ksettings = mlxsw_sp_port_get_link_ksettings,
3380 .set_link_ksettings = mlxsw_sp_port_set_link_ksettings,
3381 .get_module_info = mlxsw_sp_get_module_info,
3382 .get_module_eeprom = mlxsw_sp_get_module_eeprom,
3383 .get_ts_info = mlxsw_sp_get_ts_info,
3387 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
3389 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3390 const struct mlxsw_sp_port_type_speed_ops *ops;
3391 char ptys_pl[MLXSW_REG_PTYS_LEN];
3392 u32 eth_proto_admin;
3397 ops = mlxsw_sp->port_type_speed_ops;
3399 err = ops->port_speed_base(mlxsw_sp, mlxsw_sp_port->local_port,
3403 upper_speed = base_speed * width;
3405 eth_proto_admin = ops->to_ptys_upper_speed(mlxsw_sp, upper_speed);
3406 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3407 eth_proto_admin, mlxsw_sp_port->link.autoneg);
3408 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3411 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
3412 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
3413 bool dwrr, u8 dwrr_weight)
3415 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3416 char qeec_pl[MLXSW_REG_QEEC_LEN];
3418 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3420 mlxsw_reg_qeec_de_set(qeec_pl, true);
3421 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
3422 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
3423 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3426 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
3427 enum mlxsw_reg_qeec_hr hr, u8 index,
3428 u8 next_index, u32 maxrate)
3430 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3431 char qeec_pl[MLXSW_REG_QEEC_LEN];
3433 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3435 mlxsw_reg_qeec_mase_set(qeec_pl, true);
3436 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
3437 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3440 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
3441 enum mlxsw_reg_qeec_hr hr, u8 index,
3442 u8 next_index, u32 minrate)
3444 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3445 char qeec_pl[MLXSW_REG_QEEC_LEN];
3447 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3449 mlxsw_reg_qeec_mise_set(qeec_pl, true);
3450 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
3452 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3455 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
3456 u8 switch_prio, u8 tclass)
3458 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3459 char qtct_pl[MLXSW_REG_QTCT_LEN];
3461 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
3463 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
3466 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
3470 /* Setup the elements hierarcy, so that each TC is linked to
3471 * one subgroup, which are all member in the same group.
3473 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3474 MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
3478 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3479 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3480 MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
3485 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3486 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3487 MLXSW_REG_QEEC_HIERARCY_TC, i, i,
3492 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3493 MLXSW_REG_QEEC_HIERARCY_TC,
3500 /* Make sure the max shaper is disabled in all hierarchies that support
3501 * it. Note that this disables ptps (PTP shaper), but that is intended
3502 * for the initial configuration.
3504 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3505 MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
3506 MLXSW_REG_QEEC_MAS_DIS);
3509 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3510 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3511 MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
3513 MLXSW_REG_QEEC_MAS_DIS);
3517 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3518 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3519 MLXSW_REG_QEEC_HIERARCY_TC,
3521 MLXSW_REG_QEEC_MAS_DIS);
3525 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3526 MLXSW_REG_QEEC_HIERARCY_TC,
3528 MLXSW_REG_QEEC_MAS_DIS);
3533 /* Configure the min shaper for multicast TCs. */
3534 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3535 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
3536 MLXSW_REG_QEEC_HIERARCY_TC,
3538 MLXSW_REG_QEEC_MIS_MIN);
3543 /* Map all priorities to traffic class 0. */
3544 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3545 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
3553 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
3556 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3557 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
3559 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
3560 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
3563 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3564 bool split, u8 module, u8 width, u8 lane)
3566 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
3567 struct mlxsw_sp_port *mlxsw_sp_port;
3568 struct net_device *dev;
3571 err = mlxsw_core_port_init(mlxsw_sp->core, local_port,
3572 module + 1, split, lane / width,
3574 sizeof(mlxsw_sp->base_mac));
3576 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
3581 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
3584 goto err_alloc_etherdev;
3586 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
3587 mlxsw_sp_port = netdev_priv(dev);
3588 mlxsw_sp_port->dev = dev;
3589 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
3590 mlxsw_sp_port->local_port = local_port;
3591 mlxsw_sp_port->pvid = MLXSW_SP_DEFAULT_VID;
3592 mlxsw_sp_port->split = split;
3593 mlxsw_sp_port->mapping.module = module;
3594 mlxsw_sp_port->mapping.width = width;
3595 mlxsw_sp_port->mapping.lane = lane;
3596 mlxsw_sp_port->link.autoneg = 1;
3597 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
3598 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
3600 mlxsw_sp_port->pcpu_stats =
3601 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
3602 if (!mlxsw_sp_port->pcpu_stats) {
3604 goto err_alloc_stats;
3607 mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
3609 if (!mlxsw_sp_port->sample) {
3611 goto err_alloc_sample;
3614 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
3615 &update_stats_cache);
3617 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
3618 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
3620 err = mlxsw_sp_port_module_map(mlxsw_sp_port, module, width, lane);
3622 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
3623 mlxsw_sp_port->local_port);
3624 goto err_port_module_map;
3627 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
3629 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
3630 mlxsw_sp_port->local_port);
3631 goto err_port_swid_set;
3634 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
3636 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
3637 mlxsw_sp_port->local_port);
3638 goto err_dev_addr_init;
3641 netif_carrier_off(dev);
3643 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
3644 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
3645 dev->hw_features |= NETIF_F_HW_TC | NETIF_F_LOOPBACK;
3648 dev->max_mtu = ETH_MAX_MTU;
3650 /* Each packet needs to have a Tx header (metadata) on top all other
3653 dev->needed_headroom = MLXSW_TXHDR_LEN;
3655 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
3657 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
3658 mlxsw_sp_port->local_port);
3659 goto err_port_system_port_mapping_set;
3662 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
3664 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
3665 mlxsw_sp_port->local_port);
3666 goto err_port_speed_by_width_set;
3669 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
3671 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
3672 mlxsw_sp_port->local_port);
3673 goto err_port_mtu_set;
3676 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3678 goto err_port_admin_status_set;
3680 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
3682 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
3683 mlxsw_sp_port->local_port);
3684 goto err_port_buffers_init;
3687 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
3689 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
3690 mlxsw_sp_port->local_port);
3691 goto err_port_ets_init;
3694 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
3696 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
3697 mlxsw_sp_port->local_port);
3698 goto err_port_tc_mc_mode;
3701 /* ETS and buffers must be initialized before DCB. */
3702 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
3704 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
3705 mlxsw_sp_port->local_port);
3706 goto err_port_dcb_init;
3709 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
3711 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
3712 mlxsw_sp_port->local_port);
3713 goto err_port_fids_init;
3716 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
3718 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
3719 mlxsw_sp_port->local_port);
3720 goto err_port_qdiscs_init;
3723 err = mlxsw_sp_port_nve_init(mlxsw_sp_port);
3725 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize NVE\n",
3726 mlxsw_sp_port->local_port);
3727 goto err_port_nve_init;
3730 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
3732 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set PVID\n",
3733 mlxsw_sp_port->local_port);
3734 goto err_port_pvid_set;
3737 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
3738 MLXSW_SP_DEFAULT_VID);
3739 if (IS_ERR(mlxsw_sp_port_vlan)) {
3740 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
3741 mlxsw_sp_port->local_port);
3742 err = PTR_ERR(mlxsw_sp_port_vlan);
3743 goto err_port_vlan_create;
3745 mlxsw_sp_port->default_vlan = mlxsw_sp_port_vlan;
3747 INIT_DELAYED_WORK(&mlxsw_sp_port->ptp.shaper_dw,
3748 mlxsw_sp->ptp_ops->shaper_work);
3750 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
3751 err = register_netdev(dev);
3753 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
3754 mlxsw_sp_port->local_port);
3755 goto err_register_netdev;
3758 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
3759 mlxsw_sp_port, dev);
3760 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
3763 err_register_netdev:
3764 mlxsw_sp->ports[local_port] = NULL;
3765 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
3766 err_port_vlan_create:
3768 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3770 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3771 err_port_qdiscs_init:
3772 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3774 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3776 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3777 err_port_tc_mc_mode:
3779 err_port_buffers_init:
3780 err_port_admin_status_set:
3782 err_port_speed_by_width_set:
3783 err_port_system_port_mapping_set:
3785 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3787 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3788 err_port_module_map:
3789 kfree(mlxsw_sp_port->sample);
3791 free_percpu(mlxsw_sp_port->pcpu_stats);
3795 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3799 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3801 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3803 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
3804 cancel_delayed_work_sync(&mlxsw_sp_port->ptp.shaper_dw);
3805 mlxsw_sp_port_ptp_clear(mlxsw_sp_port);
3806 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
3807 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
3808 mlxsw_sp->ports[local_port] = NULL;
3809 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, true);
3810 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3811 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3812 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3813 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3814 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3815 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3816 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3817 kfree(mlxsw_sp_port->sample);
3818 free_percpu(mlxsw_sp_port->pcpu_stats);
3819 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
3820 free_netdev(mlxsw_sp_port->dev);
3821 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3824 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3826 return mlxsw_sp->ports[local_port] != NULL;
3829 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
3833 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
3834 if (mlxsw_sp_port_created(mlxsw_sp, i))
3835 mlxsw_sp_port_remove(mlxsw_sp, i);
3836 kfree(mlxsw_sp->port_to_module);
3837 kfree(mlxsw_sp->ports);
3840 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
3842 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
3843 u8 module, width, lane;
3848 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
3849 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
3850 if (!mlxsw_sp->ports)
3853 mlxsw_sp->port_to_module = kmalloc_array(max_ports, sizeof(int),
3855 if (!mlxsw_sp->port_to_module) {
3857 goto err_port_to_module_alloc;
3860 for (i = 1; i < max_ports; i++) {
3861 /* Mark as invalid */
3862 mlxsw_sp->port_to_module[i] = -1;
3864 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
3867 goto err_port_module_info_get;
3870 mlxsw_sp->port_to_module[i] = module;
3871 err = mlxsw_sp_port_create(mlxsw_sp, i, false,
3872 module, width, lane);
3874 goto err_port_create;
3879 err_port_module_info_get:
3880 for (i--; i >= 1; i--)
3881 if (mlxsw_sp_port_created(mlxsw_sp, i))
3882 mlxsw_sp_port_remove(mlxsw_sp, i);
3883 kfree(mlxsw_sp->port_to_module);
3884 err_port_to_module_alloc:
3885 kfree(mlxsw_sp->ports);
3889 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
3891 u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
3893 return local_port - offset;
3896 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
3897 u8 module, unsigned int count, u8 offset)
3899 u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
3902 for (i = 0; i < count; i++) {
3903 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i * offset,
3904 true, module, width, i * width);
3906 goto err_port_create;
3912 for (i--; i >= 0; i--)
3913 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
3914 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
3918 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
3919 u8 base_port, unsigned int count)
3921 u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
3924 /* Split by four means we need to re-create two ports, otherwise
3929 for (i = 0; i < count; i++) {
3930 local_port = base_port + i * 2;
3931 if (mlxsw_sp->port_to_module[local_port] < 0)
3933 module = mlxsw_sp->port_to_module[local_port];
3935 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
3940 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
3942 struct netlink_ext_ack *extack)
3944 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3945 u8 local_ports_in_1x, local_ports_in_2x, offset;
3946 struct mlxsw_sp_port *mlxsw_sp_port;
3947 u8 module, cur_width, base_port;
3951 if (!MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_1X) ||
3952 !MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_2X))
3955 local_ports_in_1x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_1X);
3956 local_ports_in_2x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_2X);
3958 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3959 if (!mlxsw_sp_port) {
3960 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3962 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
3966 module = mlxsw_sp_port->mapping.module;
3967 cur_width = mlxsw_sp_port->mapping.width;
3969 if (count != 2 && count != 4) {
3970 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
3971 NL_SET_ERR_MSG_MOD(extack, "Port can only be split into 2 or 4 ports");
3975 if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
3976 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
3977 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split further");
3981 /* Make sure we have enough slave (even) ports for the split. */
3983 offset = local_ports_in_2x;
3984 base_port = local_port;
3985 if (mlxsw_sp->ports[base_port + local_ports_in_2x]) {
3986 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3987 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
3991 offset = local_ports_in_1x;
3992 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3993 if (mlxsw_sp->ports[base_port + 1] ||
3994 mlxsw_sp->ports[base_port + 3]) {
3995 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3996 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
4001 for (i = 0; i < count; i++)
4002 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4003 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4005 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count,
4008 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
4009 goto err_port_split_create;
4014 err_port_split_create:
4015 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
4019 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port,
4020 struct netlink_ext_ack *extack)
4022 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4023 u8 local_ports_in_1x, local_ports_in_2x, offset;
4024 struct mlxsw_sp_port *mlxsw_sp_port;
4025 u8 cur_width, base_port;
4029 if (!MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_1X) ||
4030 !MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_2X))
4033 local_ports_in_1x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_1X);
4034 local_ports_in_2x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_2X);
4036 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4037 if (!mlxsw_sp_port) {
4038 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
4040 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
4044 if (!mlxsw_sp_port->split) {
4045 netdev_err(mlxsw_sp_port->dev, "Port was not split\n");
4046 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
4050 cur_width = mlxsw_sp_port->mapping.width;
4051 count = cur_width == 1 ? 4 : 2;
4054 offset = local_ports_in_2x;
4056 offset = local_ports_in_1x;
4058 base_port = mlxsw_sp_cluster_base_port_get(local_port);
4060 /* Determine which ports to remove. */
4061 if (count == 2 && local_port >= base_port + 2)
4062 base_port = base_port + 2;
4064 for (i = 0; i < count; i++)
4065 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4066 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4068 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
4073 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
4074 char *pude_pl, void *priv)
4076 struct mlxsw_sp *mlxsw_sp = priv;
4077 struct mlxsw_sp_port *mlxsw_sp_port;
4078 enum mlxsw_reg_pude_oper_status status;
4081 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
4082 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4086 status = mlxsw_reg_pude_oper_status_get(pude_pl);
4087 if (status == MLXSW_PORT_OPER_STATUS_UP) {
4088 netdev_info(mlxsw_sp_port->dev, "link up\n");
4089 netif_carrier_on(mlxsw_sp_port->dev);
4090 mlxsw_core_schedule_dw(&mlxsw_sp_port->ptp.shaper_dw, 0);
4092 netdev_info(mlxsw_sp_port->dev, "link down\n");
4093 netif_carrier_off(mlxsw_sp_port->dev);
4097 static void mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp *mlxsw_sp,
4098 char *mtpptr_pl, bool ingress)
4104 local_port = mlxsw_reg_mtpptr_local_port_get(mtpptr_pl);
4105 num_rec = mlxsw_reg_mtpptr_num_rec_get(mtpptr_pl);
4106 for (i = 0; i < num_rec; i++) {
4112 mlxsw_reg_mtpptr_unpack(mtpptr_pl, i, &message_type,
4113 &domain_number, &sequence_id,
4115 mlxsw_sp1_ptp_got_timestamp(mlxsw_sp, ingress, local_port,
4116 message_type, domain_number,
4117 sequence_id, timestamp);
4121 static void mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info *reg,
4122 char *mtpptr_pl, void *priv)
4124 struct mlxsw_sp *mlxsw_sp = priv;
4126 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, true);
4129 static void mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info *reg,
4130 char *mtpptr_pl, void *priv)
4132 struct mlxsw_sp *mlxsw_sp = priv;
4134 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, false);
4137 void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
4138 u8 local_port, void *priv)
4140 struct mlxsw_sp *mlxsw_sp = priv;
4141 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4142 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
4144 if (unlikely(!mlxsw_sp_port)) {
4145 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
4150 skb->dev = mlxsw_sp_port->dev;
4152 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
4153 u64_stats_update_begin(&pcpu_stats->syncp);
4154 pcpu_stats->rx_packets++;
4155 pcpu_stats->rx_bytes += skb->len;
4156 u64_stats_update_end(&pcpu_stats->syncp);
4158 skb->protocol = eth_type_trans(skb, skb->dev);
4159 netif_receive_skb(skb);
4162 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
4165 skb->offload_fwd_mark = 1;
4166 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4169 static void mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff *skb,
4170 u8 local_port, void *priv)
4172 skb->offload_l3_fwd_mark = 1;
4173 skb->offload_fwd_mark = 1;
4174 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4177 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
4180 struct mlxsw_sp *mlxsw_sp = priv;
4181 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4182 struct psample_group *psample_group;
4185 if (unlikely(!mlxsw_sp_port)) {
4186 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
4190 if (unlikely(!mlxsw_sp_port->sample)) {
4191 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
4196 size = mlxsw_sp_port->sample->truncate ?
4197 mlxsw_sp_port->sample->trunc_size : skb->len;
4200 psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
4203 psample_sample_packet(psample_group, skb, size,
4204 mlxsw_sp_port->dev->ifindex, 0,
4205 mlxsw_sp_port->sample->rate);
4212 static void mlxsw_sp_rx_listener_ptp(struct sk_buff *skb, u8 local_port,
4215 struct mlxsw_sp *mlxsw_sp = priv;
4217 mlxsw_sp->ptp_ops->receive(mlxsw_sp, skb, local_port);
4220 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4221 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
4222 _is_ctrl, SP_##_trap_group, DISCARD)
4224 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4225 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
4226 _is_ctrl, SP_##_trap_group, DISCARD)
4228 #define MLXSW_SP_RXL_L3_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4229 MLXSW_RXL(mlxsw_sp_rx_listener_l3_mark_func, _trap_id, _action, \
4230 _is_ctrl, SP_##_trap_group, DISCARD)
4232 #define MLXSW_SP_EVENTL(_func, _trap_id) \
4233 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
4235 static const struct mlxsw_listener mlxsw_sp_listener[] = {
4237 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
4239 MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
4240 MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
4241 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, LLDP, TRAP_TO_CPU,
4242 false, SP_LLDP, DISCARD),
4243 MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
4244 MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
4245 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
4246 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
4247 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
4248 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
4249 MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
4250 MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
4251 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, IP2ME, false),
4252 MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY, MIRROR_TO_CPU, IPV6_MLD,
4254 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4256 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE, TRAP_TO_CPU, IPV6_MLD,
4258 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4261 MLXSW_SP_RXL_MARK(MTUERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4262 MLXSW_SP_RXL_MARK(TTLERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4263 MLXSW_SP_RXL_L3_MARK(LBERROR, MIRROR_TO_CPU, LBERROR, false),
4264 MLXSW_SP_RXL_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
4265 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
4267 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP, false),
4268 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
4269 MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, TRAP_TO_CPU, ROUTER_EXP, false),
4270 MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, TRAP_TO_CPU, ROUTER_EXP,
4272 MLXSW_SP_RXL_MARK(IPV4_OSPF, TRAP_TO_CPU, OSPF, false),
4273 MLXSW_SP_RXL_MARK(IPV6_OSPF, TRAP_TO_CPU, OSPF, false),
4274 MLXSW_SP_RXL_MARK(IPV6_DHCP, TRAP_TO_CPU, DHCP, false),
4275 MLXSW_SP_RXL_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
4276 MLXSW_SP_RXL_MARK(IPV4_BGP, TRAP_TO_CPU, BGP, false),
4277 MLXSW_SP_RXL_MARK(IPV6_BGP, TRAP_TO_CPU, BGP, false),
4278 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4280 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4282 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4284 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4286 MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, TRAP_TO_CPU, IPV6_ND, false),
4287 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
4289 MLXSW_SP_RXL_MARK(HOST_MISS_IPV4, TRAP_TO_CPU, HOST_MISS, false),
4290 MLXSW_SP_RXL_MARK(HOST_MISS_IPV6, TRAP_TO_CPU, HOST_MISS, false),
4291 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, TRAP_TO_CPU, ROUTER_EXP, false),
4292 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, TRAP_TO_CPU, ROUTER_EXP, false),
4293 MLXSW_SP_RXL_MARK(IPIP_DECAP_ERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4294 MLXSW_SP_RXL_MARK(DECAP_ECN0, TRAP_TO_CPU, ROUTER_EXP, false),
4295 MLXSW_SP_RXL_MARK(IPV4_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
4296 MLXSW_SP_RXL_MARK(IPV6_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
4297 /* PKT Sample trap */
4298 MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
4299 false, SP_IP2ME, DISCARD),
4301 MLXSW_SP_RXL_NO_MARK(ACL0, TRAP_TO_CPU, IP2ME, false),
4302 /* Multicast Router Traps */
4303 MLXSW_SP_RXL_MARK(IPV4_PIM, TRAP_TO_CPU, PIM, false),
4304 MLXSW_SP_RXL_MARK(IPV6_PIM, TRAP_TO_CPU, PIM, false),
4305 MLXSW_SP_RXL_MARK(RPF, TRAP_TO_CPU, RPF, false),
4306 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
4307 MLXSW_SP_RXL_L3_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
4309 MLXSW_SP_RXL_MARK(NVE_ENCAP_ARP, TRAP_TO_CPU, ARP, false),
4310 MLXSW_SP_RXL_NO_MARK(NVE_DECAP_ARP, TRAP_TO_CPU, ARP, false),
4312 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, PTP0, TRAP_TO_CPU,
4313 false, SP_PTP0, DISCARD),
4314 MLXSW_SP_RXL_NO_MARK(PTP1, TRAP_TO_CPU, PTP1, false),
4317 static const struct mlxsw_listener mlxsw_sp1_listener[] = {
4319 MLXSW_EVENTL(mlxsw_sp1_ptp_egr_fifo_event_func, PTP_EGR_FIFO, SP_PTP0),
4320 MLXSW_EVENTL(mlxsw_sp1_ptp_ing_fifo_event_func, PTP_ING_FIFO, SP_PTP0),
4323 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
4325 char qpcr_pl[MLXSW_REG_QPCR_LEN];
4326 enum mlxsw_reg_qpcr_ir_units ir_units;
4327 int max_cpu_policers;
4333 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
4336 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4338 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
4339 for (i = 0; i < max_cpu_policers; i++) {
4342 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4343 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4344 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4345 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4346 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4347 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4348 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4352 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4353 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4357 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4358 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4359 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4360 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4361 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4362 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4363 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4364 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4368 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4372 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4376 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4384 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
4386 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
4394 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
4396 char htgt_pl[MLXSW_REG_HTGT_LEN];
4397 enum mlxsw_reg_htgt_trap_group i;
4398 int max_cpu_policers;
4399 int max_trap_groups;
4404 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
4407 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
4408 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4410 for (i = 0; i < max_trap_groups; i++) {
4413 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4414 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4415 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4416 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4417 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4418 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4422 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4423 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4427 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4428 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4429 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4433 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4434 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4435 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4436 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4440 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4441 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4442 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4443 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4444 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4448 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
4449 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
4450 tc = MLXSW_REG_HTGT_DEFAULT_TC;
4451 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
4457 if (max_cpu_policers <= policer_id &&
4458 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
4461 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
4462 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4470 static int mlxsw_sp_traps_register(struct mlxsw_sp *mlxsw_sp,
4471 const struct mlxsw_listener listeners[],
4472 size_t listeners_count)
4477 for (i = 0; i < listeners_count; i++) {
4478 err = mlxsw_core_trap_register(mlxsw_sp->core,
4482 goto err_listener_register;
4487 err_listener_register:
4488 for (i--; i >= 0; i--) {
4489 mlxsw_core_trap_unregister(mlxsw_sp->core,
4496 static void mlxsw_sp_traps_unregister(struct mlxsw_sp *mlxsw_sp,
4497 const struct mlxsw_listener listeners[],
4498 size_t listeners_count)
4502 for (i = 0; i < listeners_count; i++) {
4503 mlxsw_core_trap_unregister(mlxsw_sp->core,
4509 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
4513 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
4517 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
4521 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp_listener,
4522 ARRAY_SIZE(mlxsw_sp_listener));
4526 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp->listeners,
4527 mlxsw_sp->listeners_count);
4529 goto err_extra_traps_init;
4533 err_extra_traps_init:
4534 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4535 ARRAY_SIZE(mlxsw_sp_listener));
4539 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
4541 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp->listeners,
4542 mlxsw_sp->listeners_count);
4543 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4544 ARRAY_SIZE(mlxsw_sp_listener));
4547 #define MLXSW_SP_LAG_SEED_INIT 0xcafecafe
4549 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
4551 char slcr_pl[MLXSW_REG_SLCR_LEN];
4555 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac),
4556 MLXSW_SP_LAG_SEED_INIT);
4557 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
4558 MLXSW_REG_SLCR_LAG_HASH_DMAC |
4559 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
4560 MLXSW_REG_SLCR_LAG_HASH_VLANID |
4561 MLXSW_REG_SLCR_LAG_HASH_SIP |
4562 MLXSW_REG_SLCR_LAG_HASH_DIP |
4563 MLXSW_REG_SLCR_LAG_HASH_SPORT |
4564 MLXSW_REG_SLCR_LAG_HASH_DPORT |
4565 MLXSW_REG_SLCR_LAG_HASH_IPPROTO, seed);
4566 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
4570 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
4571 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
4574 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
4575 sizeof(struct mlxsw_sp_upper),
4577 if (!mlxsw_sp->lags)
4583 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
4585 kfree(mlxsw_sp->lags);
4588 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
4590 char htgt_pl[MLXSW_REG_HTGT_LEN];
4592 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
4593 MLXSW_REG_HTGT_INVALID_POLICER,
4594 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
4595 MLXSW_REG_HTGT_DEFAULT_TC);
4596 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4599 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
4600 .clock_init = mlxsw_sp1_ptp_clock_init,
4601 .clock_fini = mlxsw_sp1_ptp_clock_fini,
4602 .init = mlxsw_sp1_ptp_init,
4603 .fini = mlxsw_sp1_ptp_fini,
4604 .receive = mlxsw_sp1_ptp_receive,
4605 .transmitted = mlxsw_sp1_ptp_transmitted,
4606 .hwtstamp_get = mlxsw_sp1_ptp_hwtstamp_get,
4607 .hwtstamp_set = mlxsw_sp1_ptp_hwtstamp_set,
4608 .shaper_work = mlxsw_sp1_ptp_shaper_work,
4609 .get_ts_info = mlxsw_sp1_ptp_get_ts_info,
4612 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
4613 .clock_init = mlxsw_sp2_ptp_clock_init,
4614 .clock_fini = mlxsw_sp2_ptp_clock_fini,
4615 .init = mlxsw_sp2_ptp_init,
4616 .fini = mlxsw_sp2_ptp_fini,
4617 .receive = mlxsw_sp2_ptp_receive,
4618 .transmitted = mlxsw_sp2_ptp_transmitted,
4619 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
4620 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
4621 .shaper_work = mlxsw_sp2_ptp_shaper_work,
4622 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
4625 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
4626 unsigned long event, void *ptr);
4628 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
4629 const struct mlxsw_bus_info *mlxsw_bus_info)
4631 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4634 mlxsw_sp->core = mlxsw_core;
4635 mlxsw_sp->bus_info = mlxsw_bus_info;
4637 err = mlxsw_sp_fw_rev_validate(mlxsw_sp);
4641 err = mlxsw_sp_base_mac_get(mlxsw_sp);
4643 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
4647 err = mlxsw_sp_kvdl_init(mlxsw_sp);
4649 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
4653 err = mlxsw_sp_fids_init(mlxsw_sp);
4655 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
4659 err = mlxsw_sp_traps_init(mlxsw_sp);
4661 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
4662 goto err_traps_init;
4665 err = mlxsw_sp_buffers_init(mlxsw_sp);
4667 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
4668 goto err_buffers_init;
4671 err = mlxsw_sp_lag_init(mlxsw_sp);
4673 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
4677 /* Initialize SPAN before router and switchdev, so that those components
4678 * can call mlxsw_sp_span_respin().
4680 err = mlxsw_sp_span_init(mlxsw_sp);
4682 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
4686 err = mlxsw_sp_switchdev_init(mlxsw_sp);
4688 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
4689 goto err_switchdev_init;
4692 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
4694 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
4695 goto err_counter_pool_init;
4698 err = mlxsw_sp_afa_init(mlxsw_sp);
4700 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
4704 err = mlxsw_sp_nve_init(mlxsw_sp);
4706 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize NVE\n");
4710 err = mlxsw_sp_acl_init(mlxsw_sp);
4712 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
4716 err = mlxsw_sp_router_init(mlxsw_sp);
4718 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
4719 goto err_router_init;
4722 if (mlxsw_sp->bus_info->read_frc_capable) {
4723 /* NULL is a valid return value from clock_init */
4725 mlxsw_sp->ptp_ops->clock_init(mlxsw_sp,
4726 mlxsw_sp->bus_info->dev);
4727 if (IS_ERR(mlxsw_sp->clock)) {
4728 err = PTR_ERR(mlxsw_sp->clock);
4729 dev_err(mlxsw_sp->bus_info->dev, "Failed to init ptp clock\n");
4730 goto err_ptp_clock_init;
4734 if (mlxsw_sp->clock) {
4735 /* NULL is a valid return value from ptp_ops->init */
4736 mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
4737 if (IS_ERR(mlxsw_sp->ptp_state)) {
4738 err = PTR_ERR(mlxsw_sp->ptp_state);
4739 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
4744 /* Initialize netdevice notifier after router and SPAN is initialized,
4745 * so that the event handler can use router structures and call SPAN
4748 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
4749 err = register_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4751 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
4752 goto err_netdev_notifier;
4755 err = mlxsw_sp_dpipe_init(mlxsw_sp);
4757 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
4758 goto err_dpipe_init;
4761 err = mlxsw_sp_ports_create(mlxsw_sp);
4763 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
4764 goto err_ports_create;
4770 mlxsw_sp_dpipe_fini(mlxsw_sp);
4772 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4773 err_netdev_notifier:
4774 if (mlxsw_sp->clock)
4775 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
4777 if (mlxsw_sp->clock)
4778 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
4780 mlxsw_sp_router_fini(mlxsw_sp);
4782 mlxsw_sp_acl_fini(mlxsw_sp);
4784 mlxsw_sp_nve_fini(mlxsw_sp);
4786 mlxsw_sp_afa_fini(mlxsw_sp);
4788 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4789 err_counter_pool_init:
4790 mlxsw_sp_switchdev_fini(mlxsw_sp);
4792 mlxsw_sp_span_fini(mlxsw_sp);
4794 mlxsw_sp_lag_fini(mlxsw_sp);
4796 mlxsw_sp_buffers_fini(mlxsw_sp);
4798 mlxsw_sp_traps_fini(mlxsw_sp);
4800 mlxsw_sp_fids_fini(mlxsw_sp);
4802 mlxsw_sp_kvdl_fini(mlxsw_sp);
4806 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
4807 const struct mlxsw_bus_info *mlxsw_bus_info)
4809 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4811 mlxsw_sp->req_rev = &mlxsw_sp1_fw_rev;
4812 mlxsw_sp->fw_filename = MLXSW_SP1_FW_FILENAME;
4813 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
4814 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
4815 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
4816 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
4817 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
4818 mlxsw_sp->nve_ops_arr = mlxsw_sp1_nve_ops_arr;
4819 mlxsw_sp->mac_mask = mlxsw_sp1_mac_mask;
4820 mlxsw_sp->rif_ops_arr = mlxsw_sp1_rif_ops_arr;
4821 mlxsw_sp->sb_vals = &mlxsw_sp1_sb_vals;
4822 mlxsw_sp->port_type_speed_ops = &mlxsw_sp1_port_type_speed_ops;
4823 mlxsw_sp->ptp_ops = &mlxsw_sp1_ptp_ops;
4824 mlxsw_sp->listeners = mlxsw_sp1_listener;
4825 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp1_listener);
4827 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
4830 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
4831 const struct mlxsw_bus_info *mlxsw_bus_info)
4833 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4835 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
4836 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
4837 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
4838 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
4839 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
4840 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
4841 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
4842 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
4843 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
4844 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
4845 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
4847 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
4850 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
4852 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4854 mlxsw_sp_ports_remove(mlxsw_sp);
4855 mlxsw_sp_dpipe_fini(mlxsw_sp);
4856 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4857 if (mlxsw_sp->clock) {
4858 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
4859 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
4861 mlxsw_sp_router_fini(mlxsw_sp);
4862 mlxsw_sp_acl_fini(mlxsw_sp);
4863 mlxsw_sp_nve_fini(mlxsw_sp);
4864 mlxsw_sp_afa_fini(mlxsw_sp);
4865 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4866 mlxsw_sp_switchdev_fini(mlxsw_sp);
4867 mlxsw_sp_span_fini(mlxsw_sp);
4868 mlxsw_sp_lag_fini(mlxsw_sp);
4869 mlxsw_sp_buffers_fini(mlxsw_sp);
4870 mlxsw_sp_traps_fini(mlxsw_sp);
4871 mlxsw_sp_fids_fini(mlxsw_sp);
4872 mlxsw_sp_kvdl_fini(mlxsw_sp);
4875 /* Per-FID flood tables are used for both "true" 802.1D FIDs and emulated
4878 #define MLXSW_SP_FID_FLOOD_TABLE_SIZE (MLXSW_SP_FID_8021D_MAX + \
4881 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
4883 .max_mid = MLXSW_SP_MID_MAX,
4884 .used_flood_tables = 1,
4885 .used_flood_mode = 1,
4887 .max_fid_flood_tables = 3,
4888 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
4889 .used_max_ib_mc = 1,
4893 .used_kvd_sizes = 1,
4894 .kvd_hash_single_parts = 59,
4895 .kvd_hash_double_parts = 41,
4896 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
4900 .type = MLXSW_PORT_SWID_TYPE_ETH,
4905 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
4907 .max_mid = MLXSW_SP_MID_MAX,
4908 .used_flood_tables = 1,
4909 .used_flood_mode = 1,
4911 .max_fid_flood_tables = 3,
4912 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
4913 .used_max_ib_mc = 1,
4920 .type = MLXSW_PORT_SWID_TYPE_ETH,
4926 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
4927 struct devlink_resource_size_params *kvd_size_params,
4928 struct devlink_resource_size_params *linear_size_params,
4929 struct devlink_resource_size_params *hash_double_size_params,
4930 struct devlink_resource_size_params *hash_single_size_params)
4932 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4933 KVD_SINGLE_MIN_SIZE);
4934 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4935 KVD_DOUBLE_MIN_SIZE);
4936 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4937 u32 linear_size_min = 0;
4939 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
4940 MLXSW_SP_KVD_GRANULARITY,
4941 DEVLINK_RESOURCE_UNIT_ENTRY);
4942 devlink_resource_size_params_init(linear_size_params, linear_size_min,
4943 kvd_size - single_size_min -
4945 MLXSW_SP_KVD_GRANULARITY,
4946 DEVLINK_RESOURCE_UNIT_ENTRY);
4947 devlink_resource_size_params_init(hash_double_size_params,
4949 kvd_size - single_size_min -
4951 MLXSW_SP_KVD_GRANULARITY,
4952 DEVLINK_RESOURCE_UNIT_ENTRY);
4953 devlink_resource_size_params_init(hash_single_size_params,
4955 kvd_size - double_size_min -
4957 MLXSW_SP_KVD_GRANULARITY,
4958 DEVLINK_RESOURCE_UNIT_ENTRY);
4961 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
4963 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4964 struct devlink_resource_size_params hash_single_size_params;
4965 struct devlink_resource_size_params hash_double_size_params;
4966 struct devlink_resource_size_params linear_size_params;
4967 struct devlink_resource_size_params kvd_size_params;
4968 u32 kvd_size, single_size, double_size, linear_size;
4969 const struct mlxsw_config_profile *profile;
4972 profile = &mlxsw_sp1_config_profile;
4973 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
4976 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
4977 &linear_size_params,
4978 &hash_double_size_params,
4979 &hash_single_size_params);
4981 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4982 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
4983 kvd_size, MLXSW_SP_RESOURCE_KVD,
4984 DEVLINK_RESOURCE_ID_PARENT_TOP,
4989 linear_size = profile->kvd_linear_size;
4990 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
4992 MLXSW_SP_RESOURCE_KVD_LINEAR,
4993 MLXSW_SP_RESOURCE_KVD,
4994 &linear_size_params);
4998 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
5002 double_size = kvd_size - linear_size;
5003 double_size *= profile->kvd_hash_double_parts;
5004 double_size /= profile->kvd_hash_double_parts +
5005 profile->kvd_hash_single_parts;
5006 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
5007 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
5009 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
5010 MLXSW_SP_RESOURCE_KVD,
5011 &hash_double_size_params);
5015 single_size = kvd_size - double_size - linear_size;
5016 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
5018 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
5019 MLXSW_SP_RESOURCE_KVD,
5020 &hash_single_size_params);
5027 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
5029 return mlxsw_sp1_resources_kvd_register(mlxsw_core);
5032 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
5037 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
5038 const struct mlxsw_config_profile *profile,
5039 u64 *p_single_size, u64 *p_double_size,
5042 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5046 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5047 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
5050 /* The hash part is what left of the kvd without the
5051 * linear part. It is split to the single size and
5052 * double size by the parts ratio from the profile.
5053 * Both sizes must be a multiplications of the
5054 * granularity from the profile. In case the user
5055 * provided the sizes they are obtained via devlink.
5057 err = devlink_resource_size_get(devlink,
5058 MLXSW_SP_RESOURCE_KVD_LINEAR,
5061 *p_linear_size = profile->kvd_linear_size;
5063 err = devlink_resource_size_get(devlink,
5064 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
5067 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5069 double_size *= profile->kvd_hash_double_parts;
5070 double_size /= profile->kvd_hash_double_parts +
5071 profile->kvd_hash_single_parts;
5072 *p_double_size = rounddown(double_size,
5073 MLXSW_SP_KVD_GRANULARITY);
5076 err = devlink_resource_size_get(devlink,
5077 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
5080 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5081 *p_double_size - *p_linear_size;
5083 /* Check results are legal. */
5084 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5085 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
5086 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
5093 mlxsw_sp_devlink_param_fw_load_policy_validate(struct devlink *devlink, u32 id,
5094 union devlink_param_value val,
5095 struct netlink_ext_ack *extack)
5097 if ((val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER) &&
5098 (val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)) {
5099 NL_SET_ERR_MSG_MOD(extack, "'fw_load_policy' must be 'driver' or 'flash'");
5106 static const struct devlink_param mlxsw_sp_devlink_params[] = {
5107 DEVLINK_PARAM_GENERIC(FW_LOAD_POLICY,
5108 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
5110 mlxsw_sp_devlink_param_fw_load_policy_validate),
5113 static int mlxsw_sp_params_register(struct mlxsw_core *mlxsw_core)
5115 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5116 union devlink_param_value value;
5119 err = devlink_params_register(devlink, mlxsw_sp_devlink_params,
5120 ARRAY_SIZE(mlxsw_sp_devlink_params));
5124 value.vu8 = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER;
5125 devlink_param_driverinit_value_set(devlink,
5126 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5131 static void mlxsw_sp_params_unregister(struct mlxsw_core *mlxsw_core)
5133 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5134 mlxsw_sp_devlink_params,
5135 ARRAY_SIZE(mlxsw_sp_devlink_params));
5139 mlxsw_sp_params_acl_region_rehash_intrvl_get(struct devlink *devlink, u32 id,
5140 struct devlink_param_gset_ctx *ctx)
5142 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5143 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5145 ctx->val.vu32 = mlxsw_sp_acl_region_rehash_intrvl_get(mlxsw_sp);
5150 mlxsw_sp_params_acl_region_rehash_intrvl_set(struct devlink *devlink, u32 id,
5151 struct devlink_param_gset_ctx *ctx)
5153 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5154 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5156 return mlxsw_sp_acl_region_rehash_intrvl_set(mlxsw_sp, ctx->val.vu32);
5159 static const struct devlink_param mlxsw_sp2_devlink_params[] = {
5160 DEVLINK_PARAM_DRIVER(MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5161 "acl_region_rehash_interval",
5162 DEVLINK_PARAM_TYPE_U32,
5163 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
5164 mlxsw_sp_params_acl_region_rehash_intrvl_get,
5165 mlxsw_sp_params_acl_region_rehash_intrvl_set,
5169 static int mlxsw_sp2_params_register(struct mlxsw_core *mlxsw_core)
5171 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5172 union devlink_param_value value;
5175 err = mlxsw_sp_params_register(mlxsw_core);
5179 err = devlink_params_register(devlink, mlxsw_sp2_devlink_params,
5180 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5182 goto err_devlink_params_register;
5185 devlink_param_driverinit_value_set(devlink,
5186 MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5190 err_devlink_params_register:
5191 mlxsw_sp_params_unregister(mlxsw_core);
5195 static void mlxsw_sp2_params_unregister(struct mlxsw_core *mlxsw_core)
5197 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5198 mlxsw_sp2_devlink_params,
5199 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5200 mlxsw_sp_params_unregister(mlxsw_core);
5203 static void mlxsw_sp_ptp_transmitted(struct mlxsw_core *mlxsw_core,
5204 struct sk_buff *skb, u8 local_port)
5206 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5208 skb_pull(skb, MLXSW_TXHDR_LEN);
5209 mlxsw_sp->ptp_ops->transmitted(mlxsw_sp, skb, local_port);
5212 static struct mlxsw_driver mlxsw_sp1_driver = {
5213 .kind = mlxsw_sp1_driver_name,
5214 .priv_size = sizeof(struct mlxsw_sp),
5215 .init = mlxsw_sp1_init,
5216 .fini = mlxsw_sp_fini,
5217 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5218 .port_split = mlxsw_sp_port_split,
5219 .port_unsplit = mlxsw_sp_port_unsplit,
5220 .sb_pool_get = mlxsw_sp_sb_pool_get,
5221 .sb_pool_set = mlxsw_sp_sb_pool_set,
5222 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5223 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5224 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5225 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5226 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5227 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5228 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5229 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5230 .flash_update = mlxsw_sp_flash_update,
5231 .txhdr_construct = mlxsw_sp_txhdr_construct,
5232 .resources_register = mlxsw_sp1_resources_register,
5233 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
5234 .params_register = mlxsw_sp_params_register,
5235 .params_unregister = mlxsw_sp_params_unregister,
5236 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5237 .txhdr_len = MLXSW_TXHDR_LEN,
5238 .profile = &mlxsw_sp1_config_profile,
5239 .res_query_enabled = true,
5242 static struct mlxsw_driver mlxsw_sp2_driver = {
5243 .kind = mlxsw_sp2_driver_name,
5244 .priv_size = sizeof(struct mlxsw_sp),
5245 .init = mlxsw_sp2_init,
5246 .fini = mlxsw_sp_fini,
5247 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5248 .port_split = mlxsw_sp_port_split,
5249 .port_unsplit = mlxsw_sp_port_unsplit,
5250 .sb_pool_get = mlxsw_sp_sb_pool_get,
5251 .sb_pool_set = mlxsw_sp_sb_pool_set,
5252 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5253 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5254 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5255 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5256 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5257 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5258 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5259 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5260 .flash_update = mlxsw_sp_flash_update,
5261 .txhdr_construct = mlxsw_sp_txhdr_construct,
5262 .resources_register = mlxsw_sp2_resources_register,
5263 .params_register = mlxsw_sp2_params_register,
5264 .params_unregister = mlxsw_sp2_params_unregister,
5265 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5266 .txhdr_len = MLXSW_TXHDR_LEN,
5267 .profile = &mlxsw_sp2_config_profile,
5268 .res_query_enabled = true,
5271 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
5273 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
5276 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
5278 struct mlxsw_sp_port **p_mlxsw_sp_port = data;
5281 if (mlxsw_sp_port_dev_check(lower_dev)) {
5282 *p_mlxsw_sp_port = netdev_priv(lower_dev);
5289 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
5291 struct mlxsw_sp_port *mlxsw_sp_port;
5293 if (mlxsw_sp_port_dev_check(dev))
5294 return netdev_priv(dev);
5296 mlxsw_sp_port = NULL;
5297 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
5299 return mlxsw_sp_port;
5302 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
5304 struct mlxsw_sp_port *mlxsw_sp_port;
5306 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
5307 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
5310 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
5312 struct mlxsw_sp_port *mlxsw_sp_port;
5314 if (mlxsw_sp_port_dev_check(dev))
5315 return netdev_priv(dev);
5317 mlxsw_sp_port = NULL;
5318 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
5321 return mlxsw_sp_port;
5324 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
5326 struct mlxsw_sp_port *mlxsw_sp_port;
5329 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
5331 dev_hold(mlxsw_sp_port->dev);
5333 return mlxsw_sp_port;
5336 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
5338 dev_put(mlxsw_sp_port->dev);
5342 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
5343 struct net_device *lag_dev)
5345 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
5346 struct net_device *upper_dev;
5347 struct list_head *iter;
5349 if (netif_is_bridge_port(lag_dev))
5350 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
5352 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
5353 if (!netif_is_bridge_port(upper_dev))
5355 br_dev = netdev_master_upper_dev_get(upper_dev);
5356 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
5360 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5362 char sldr_pl[MLXSW_REG_SLDR_LEN];
5364 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
5365 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5368 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5370 char sldr_pl[MLXSW_REG_SLDR_LEN];
5372 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
5373 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5376 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
5377 u16 lag_id, u8 port_index)
5379 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5380 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5382 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
5383 lag_id, port_index);
5384 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5387 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
5390 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5391 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5393 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
5395 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5398 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
5401 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5402 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5404 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
5406 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5409 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
5412 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5413 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5415 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
5417 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5420 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5421 struct net_device *lag_dev,
5424 struct mlxsw_sp_upper *lag;
5425 int free_lag_id = -1;
5429 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
5430 for (i = 0; i < max_lag; i++) {
5431 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
5432 if (lag->ref_count) {
5433 if (lag->dev == lag_dev) {
5437 } else if (free_lag_id < 0) {
5441 if (free_lag_id < 0)
5443 *p_lag_id = free_lag_id;
5448 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
5449 struct net_device *lag_dev,
5450 struct netdev_lag_upper_info *lag_upper_info,
5451 struct netlink_ext_ack *extack)
5455 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
5456 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported LAG devices");
5459 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
5460 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
5466 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5467 u16 lag_id, u8 *p_port_index)
5469 u64 max_lag_members;
5472 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
5474 for (i = 0; i < max_lag_members; i++) {
5475 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
5483 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
5484 struct net_device *lag_dev)
5486 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5487 struct mlxsw_sp_upper *lag;
5492 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
5495 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5496 if (!lag->ref_count) {
5497 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
5503 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
5506 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
5508 goto err_col_port_add;
5510 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
5511 mlxsw_sp_port->local_port);
5512 mlxsw_sp_port->lag_id = lag_id;
5513 mlxsw_sp_port->lagged = 1;
5516 /* Port is no longer usable as a router interface */
5517 if (mlxsw_sp_port->default_vlan->fid)
5518 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
5523 if (!lag->ref_count)
5524 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
5528 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
5529 struct net_device *lag_dev)
5531 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5532 u16 lag_id = mlxsw_sp_port->lag_id;
5533 struct mlxsw_sp_upper *lag;
5535 if (!mlxsw_sp_port->lagged)
5537 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5538 WARN_ON(lag->ref_count == 0);
5540 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
5542 /* Any VLANs configured on the port are no longer valid */
5543 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, false);
5544 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port->default_vlan);
5545 /* Make the LAG and its directly linked uppers leave bridges they
5548 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
5550 if (lag->ref_count == 1)
5551 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
5553 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
5554 mlxsw_sp_port->local_port);
5555 mlxsw_sp_port->lagged = 0;
5558 /* Make sure untagged frames are allowed to ingress */
5559 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
5562 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
5565 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5566 char sldr_pl[MLXSW_REG_SLDR_LEN];
5568 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
5569 mlxsw_sp_port->local_port);
5570 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5573 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
5576 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5577 char sldr_pl[MLXSW_REG_SLDR_LEN];
5579 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
5580 mlxsw_sp_port->local_port);
5581 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5585 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
5589 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
5590 mlxsw_sp_port->lag_id);
5594 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5596 goto err_dist_port_add;
5601 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5606 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
5610 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
5611 mlxsw_sp_port->lag_id);
5615 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
5616 mlxsw_sp_port->lag_id);
5618 goto err_col_port_disable;
5622 err_col_port_disable:
5623 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5627 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
5628 struct netdev_lag_lower_state_info *info)
5630 if (info->tx_enabled)
5631 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
5633 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
5636 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
5639 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5640 enum mlxsw_reg_spms_state spms_state;
5645 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
5646 MLXSW_REG_SPMS_STATE_DISCARDING;
5648 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
5651 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
5653 for (vid = 0; vid < VLAN_N_VID; vid++)
5654 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
5656 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
5661 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
5666 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
5669 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
5671 goto err_port_stp_set;
5672 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
5675 goto err_port_vlan_set;
5677 for (; vid <= VLAN_N_VID - 1; vid++) {
5678 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
5681 goto err_vid_learning_set;
5686 err_vid_learning_set:
5687 for (vid--; vid >= 1; vid--)
5688 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
5690 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
5692 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
5696 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
5700 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
5701 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
5704 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
5706 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
5707 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
5710 static bool mlxsw_sp_bridge_has_multiple_vxlans(struct net_device *br_dev)
5712 unsigned int num_vxlans = 0;
5713 struct net_device *dev;
5714 struct list_head *iter;
5716 netdev_for_each_lower_dev(br_dev, dev, iter) {
5717 if (netif_is_vxlan(dev))
5721 return num_vxlans > 1;
5724 static bool mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device *br_dev)
5726 DECLARE_BITMAP(vlans, VLAN_N_VID) = {0};
5727 struct net_device *dev;
5728 struct list_head *iter;
5730 netdev_for_each_lower_dev(br_dev, dev, iter) {
5734 if (!netif_is_vxlan(dev))
5737 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
5741 if (test_and_set_bit(pvid, vlans))
5748 static bool mlxsw_sp_bridge_vxlan_is_valid(struct net_device *br_dev,
5749 struct netlink_ext_ack *extack)
5751 if (br_multicast_enabled(br_dev)) {
5752 NL_SET_ERR_MSG_MOD(extack, "Multicast can not be enabled on a bridge with a VxLAN device");
5756 if (!br_vlan_enabled(br_dev) &&
5757 mlxsw_sp_bridge_has_multiple_vxlans(br_dev)) {
5758 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices are not supported in a VLAN-unaware bridge");
5762 if (br_vlan_enabled(br_dev) &&
5763 !mlxsw_sp_bridge_vxlan_vlan_is_valid(br_dev)) {
5764 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices cannot have the same VLAN as PVID and egress untagged");
5771 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
5772 struct net_device *dev,
5773 unsigned long event, void *ptr)
5775 struct netdev_notifier_changeupper_info *info;
5776 struct mlxsw_sp_port *mlxsw_sp_port;
5777 struct netlink_ext_ack *extack;
5778 struct net_device *upper_dev;
5779 struct mlxsw_sp *mlxsw_sp;
5782 mlxsw_sp_port = netdev_priv(dev);
5783 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5785 extack = netdev_notifier_info_to_extack(&info->info);
5788 case NETDEV_PRECHANGEUPPER:
5789 upper_dev = info->upper_dev;
5790 if (!is_vlan_dev(upper_dev) &&
5791 !netif_is_lag_master(upper_dev) &&
5792 !netif_is_bridge_master(upper_dev) &&
5793 !netif_is_ovs_master(upper_dev) &&
5794 !netif_is_macvlan(upper_dev)) {
5795 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5800 if (netif_is_bridge_master(upper_dev) &&
5801 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
5802 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
5803 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5805 if (netdev_has_any_upper_dev(upper_dev) &&
5806 (!netif_is_bridge_master(upper_dev) ||
5807 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
5809 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
5812 if (netif_is_lag_master(upper_dev) &&
5813 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
5814 info->upper_info, extack))
5816 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
5817 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
5820 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
5821 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
5822 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
5825 if (netif_is_macvlan(upper_dev) &&
5826 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, lower_dev)) {
5827 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
5830 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
5831 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
5834 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
5835 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
5839 case NETDEV_CHANGEUPPER:
5840 upper_dev = info->upper_dev;
5841 if (netif_is_bridge_master(upper_dev)) {
5843 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5848 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5851 } else if (netif_is_lag_master(upper_dev)) {
5852 if (info->linking) {
5853 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
5856 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
5857 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
5860 } else if (netif_is_ovs_master(upper_dev)) {
5862 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
5864 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
5865 } else if (netif_is_macvlan(upper_dev)) {
5867 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5868 } else if (is_vlan_dev(upper_dev)) {
5869 struct net_device *br_dev;
5871 if (!netif_is_bridge_port(upper_dev))
5875 br_dev = netdev_master_upper_dev_get(upper_dev);
5876 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
5885 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
5886 unsigned long event, void *ptr)
5888 struct netdev_notifier_changelowerstate_info *info;
5889 struct mlxsw_sp_port *mlxsw_sp_port;
5892 mlxsw_sp_port = netdev_priv(dev);
5896 case NETDEV_CHANGELOWERSTATE:
5897 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
5898 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
5899 info->lower_state_info);
5901 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
5909 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
5910 struct net_device *port_dev,
5911 unsigned long event, void *ptr)
5914 case NETDEV_PRECHANGEUPPER:
5915 case NETDEV_CHANGEUPPER:
5916 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
5918 case NETDEV_CHANGELOWERSTATE:
5919 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
5926 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
5927 unsigned long event, void *ptr)
5929 struct net_device *dev;
5930 struct list_head *iter;
5933 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5934 if (mlxsw_sp_port_dev_check(dev)) {
5935 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
5945 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
5946 struct net_device *dev,
5947 unsigned long event, void *ptr,
5950 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
5951 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5952 struct netdev_notifier_changeupper_info *info = ptr;
5953 struct netlink_ext_ack *extack;
5954 struct net_device *upper_dev;
5957 extack = netdev_notifier_info_to_extack(&info->info);
5960 case NETDEV_PRECHANGEUPPER:
5961 upper_dev = info->upper_dev;
5962 if (!netif_is_bridge_master(upper_dev) &&
5963 !netif_is_macvlan(upper_dev)) {
5964 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5969 if (netif_is_bridge_master(upper_dev) &&
5970 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
5971 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
5972 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5974 if (netdev_has_any_upper_dev(upper_dev) &&
5975 (!netif_is_bridge_master(upper_dev) ||
5976 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
5978 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
5981 if (netif_is_macvlan(upper_dev) &&
5982 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan_dev)) {
5983 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
5987 case NETDEV_CHANGEUPPER:
5988 upper_dev = info->upper_dev;
5989 if (netif_is_bridge_master(upper_dev)) {
5991 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5996 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5999 } else if (netif_is_macvlan(upper_dev)) {
6001 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6012 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
6013 struct net_device *lag_dev,
6014 unsigned long event,
6017 struct net_device *dev;
6018 struct list_head *iter;
6021 netdev_for_each_lower_dev(lag_dev, dev, iter) {
6022 if (mlxsw_sp_port_dev_check(dev)) {
6023 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
6034 static int mlxsw_sp_netdevice_bridge_vlan_event(struct net_device *vlan_dev,
6035 struct net_device *br_dev,
6036 unsigned long event, void *ptr,
6039 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
6040 struct netdev_notifier_changeupper_info *info = ptr;
6041 struct netlink_ext_ack *extack;
6042 struct net_device *upper_dev;
6047 extack = netdev_notifier_info_to_extack(&info->info);
6050 case NETDEV_PRECHANGEUPPER:
6051 upper_dev = info->upper_dev;
6052 if (!netif_is_macvlan(upper_dev)) {
6053 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6058 if (netif_is_macvlan(upper_dev) &&
6059 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan_dev)) {
6060 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6064 case NETDEV_CHANGEUPPER:
6065 upper_dev = info->upper_dev;
6068 if (netif_is_macvlan(upper_dev))
6069 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6076 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
6077 unsigned long event, void *ptr)
6079 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
6080 u16 vid = vlan_dev_vlan_id(vlan_dev);
6082 if (mlxsw_sp_port_dev_check(real_dev))
6083 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
6085 else if (netif_is_lag_master(real_dev))
6086 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
6089 else if (netif_is_bridge_master(real_dev))
6090 return mlxsw_sp_netdevice_bridge_vlan_event(vlan_dev, real_dev,
6096 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
6097 unsigned long event, void *ptr)
6099 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(br_dev);
6100 struct netdev_notifier_changeupper_info *info = ptr;
6101 struct netlink_ext_ack *extack;
6102 struct net_device *upper_dev;
6107 extack = netdev_notifier_info_to_extack(&info->info);
6110 case NETDEV_PRECHANGEUPPER:
6111 upper_dev = info->upper_dev;
6112 if (!is_vlan_dev(upper_dev) && !netif_is_macvlan(upper_dev)) {
6113 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6118 if (netif_is_macvlan(upper_dev) &&
6119 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev)) {
6120 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6124 case NETDEV_CHANGEUPPER:
6125 upper_dev = info->upper_dev;
6128 if (is_vlan_dev(upper_dev))
6129 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
6130 if (netif_is_macvlan(upper_dev))
6131 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6138 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
6139 unsigned long event, void *ptr)
6141 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
6142 struct netdev_notifier_changeupper_info *info = ptr;
6143 struct netlink_ext_ack *extack;
6145 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
6148 extack = netdev_notifier_info_to_extack(&info->info);
6150 /* VRF enslavement is handled in mlxsw_sp_netdevice_vrf_event() */
6151 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6156 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
6158 struct netdev_notifier_changeupper_info *info = ptr;
6160 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
6162 return netif_is_l3_master(info->upper_dev);
6165 static int mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp *mlxsw_sp,
6166 struct net_device *dev,
6167 unsigned long event, void *ptr)
6169 struct netdev_notifier_changeupper_info *cu_info;
6170 struct netdev_notifier_info *info = ptr;
6171 struct netlink_ext_ack *extack;
6172 struct net_device *upper_dev;
6174 extack = netdev_notifier_info_to_extack(info);
6177 case NETDEV_CHANGEUPPER:
6178 cu_info = container_of(info,
6179 struct netdev_notifier_changeupper_info,
6181 upper_dev = cu_info->upper_dev;
6182 if (!netif_is_bridge_master(upper_dev))
6184 if (!mlxsw_sp_lower_get(upper_dev))
6186 if (!mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
6188 if (cu_info->linking) {
6189 if (!netif_running(dev))
6191 /* When the bridge is VLAN-aware, the VNI of the VxLAN
6192 * device needs to be mapped to a VLAN, but at this
6193 * point no VLANs are configured on the VxLAN device
6195 if (br_vlan_enabled(upper_dev))
6197 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev,
6200 /* VLANs were already flushed, which triggered the
6203 if (br_vlan_enabled(upper_dev))
6205 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6209 upper_dev = netdev_master_upper_dev_get(dev);
6212 if (!netif_is_bridge_master(upper_dev))
6214 if (!mlxsw_sp_lower_get(upper_dev))
6216 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev, dev, 0,
6219 upper_dev = netdev_master_upper_dev_get(dev);
6222 if (!netif_is_bridge_master(upper_dev))
6224 if (!mlxsw_sp_lower_get(upper_dev))
6226 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6233 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
6234 unsigned long event, void *ptr)
6236 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6237 struct mlxsw_sp_span_entry *span_entry;
6238 struct mlxsw_sp *mlxsw_sp;
6241 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
6242 if (event == NETDEV_UNREGISTER) {
6243 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
6245 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
6247 mlxsw_sp_span_respin(mlxsw_sp);
6249 if (netif_is_vxlan(dev))
6250 err = mlxsw_sp_netdevice_vxlan_event(mlxsw_sp, dev, event, ptr);
6251 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
6252 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
6254 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
6255 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
6257 else if (event == NETDEV_PRE_CHANGEADDR ||
6258 event == NETDEV_CHANGEADDR ||
6259 event == NETDEV_CHANGEMTU)
6260 err = mlxsw_sp_netdevice_router_port_event(dev, event, ptr);
6261 else if (mlxsw_sp_is_vrf_event(event, ptr))
6262 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
6263 else if (mlxsw_sp_port_dev_check(dev))
6264 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
6265 else if (netif_is_lag_master(dev))
6266 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
6267 else if (is_vlan_dev(dev))
6268 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
6269 else if (netif_is_bridge_master(dev))
6270 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
6271 else if (netif_is_macvlan(dev))
6272 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
6274 return notifier_from_errno(err);
6277 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
6278 .notifier_call = mlxsw_sp_inetaddr_valid_event,
6281 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
6282 .notifier_call = mlxsw_sp_inet6addr_valid_event,
6285 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
6286 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
6290 static struct pci_driver mlxsw_sp1_pci_driver = {
6291 .name = mlxsw_sp1_driver_name,
6292 .id_table = mlxsw_sp1_pci_id_table,
6295 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
6296 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
6300 static struct pci_driver mlxsw_sp2_pci_driver = {
6301 .name = mlxsw_sp2_driver_name,
6302 .id_table = mlxsw_sp2_pci_id_table,
6305 static int __init mlxsw_sp_module_init(void)
6309 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6310 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6312 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
6314 goto err_sp1_core_driver_register;
6316 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
6318 goto err_sp2_core_driver_register;
6320 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
6322 goto err_sp1_pci_driver_register;
6324 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
6326 goto err_sp2_pci_driver_register;
6330 err_sp2_pci_driver_register:
6331 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
6332 err_sp1_pci_driver_register:
6333 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6334 err_sp2_core_driver_register:
6335 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6336 err_sp1_core_driver_register:
6337 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6338 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6342 static void __exit mlxsw_sp_module_exit(void)
6344 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
6345 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
6346 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6347 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6348 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6349 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6352 module_init(mlxsw_sp_module_init);
6353 module_exit(mlxsw_sp_module_exit);
6355 MODULE_LICENSE("Dual BSD/GPL");
6356 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
6357 MODULE_DESCRIPTION("Mellanox Spectrum driver");
6358 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
6359 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);