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 \
62 "mellanox/mlxsw_spectrum-" __stringify(MLXSW_SP1_FWREV_MAJOR) \
63 "." __stringify(MLXSW_SP1_FWREV_MINOR) \
64 "." __stringify(MLXSW_SP1_FWREV_SUBMINOR) ".mfa2"
66 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
67 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
68 static const char mlxsw_sp_driver_version[] = "1.0";
70 static const unsigned char mlxsw_sp1_mac_mask[ETH_ALEN] = {
71 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00
73 static const unsigned char mlxsw_sp2_mac_mask[ETH_ALEN] = {
74 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
81 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
84 * Packet control type.
85 * 0 - Ethernet control (e.g. EMADs, LACP)
88 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
91 * Packet protocol type. Must be set to 1 (Ethernet).
93 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
95 /* tx_hdr_rx_is_router
96 * Packet is sent from the router. Valid for data packets only.
98 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
101 * Indicates if the 'fid' field is valid and should be used for
102 * forwarding lookup. Valid for data packets only.
104 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
107 * Switch partition ID. Must be set to 0.
109 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
111 /* tx_hdr_control_tclass
112 * Indicates if the packet should use the control TClass and not one
113 * of the data TClasses.
115 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
118 * Egress TClass to be used on the egress device on the egress port.
120 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
123 * Destination local port for unicast packets.
124 * Destination multicast ID for multicast packets.
126 * Control packets are directed to a specific egress port, while data
127 * packets are transmitted through the CPU port (0) into the switch partition,
128 * where forwarding rules are applied.
130 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
133 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
134 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
135 * Valid for data packets only.
137 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
141 * 6 - Control packets
143 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
145 struct mlxsw_sp_mlxfw_dev {
146 struct mlxfw_dev mlxfw_dev;
147 struct mlxsw_sp *mlxsw_sp;
150 struct mlxsw_sp_ptp_ops {
151 struct mlxsw_sp_ptp_clock *
152 (*clock_init)(struct mlxsw_sp *mlxsw_sp, struct device *dev);
153 void (*clock_fini)(struct mlxsw_sp_ptp_clock *clock);
155 struct mlxsw_sp_ptp_state *(*init)(struct mlxsw_sp *mlxsw_sp);
156 void (*fini)(struct mlxsw_sp_ptp_state *ptp_state);
158 /* Notify a driver that a packet that might be PTP was received. Driver
159 * is responsible for freeing the passed-in SKB.
161 void (*receive)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
164 /* Notify a driver that a timestamped packet was transmitted. Driver
165 * is responsible for freeing the passed-in SKB.
167 void (*transmitted)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
170 int (*hwtstamp_get)(struct mlxsw_sp_port *mlxsw_sp_port,
171 struct hwtstamp_config *config);
172 int (*hwtstamp_set)(struct mlxsw_sp_port *mlxsw_sp_port,
173 struct hwtstamp_config *config);
174 int (*get_ts_info)(struct mlxsw_sp *mlxsw_sp,
175 struct ethtool_ts_info *info);
178 static int mlxsw_sp_component_query(struct mlxfw_dev *mlxfw_dev,
179 u16 component_index, u32 *p_max_size,
180 u8 *p_align_bits, u16 *p_max_write_size)
182 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
183 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
184 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
185 char mcqi_pl[MLXSW_REG_MCQI_LEN];
188 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
189 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcqi), mcqi_pl);
192 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
195 *p_align_bits = max_t(u8, *p_align_bits, 2);
196 *p_max_write_size = min_t(u16, *p_max_write_size,
197 MLXSW_REG_MCDA_MAX_DATA_LEN);
201 static int mlxsw_sp_fsm_lock(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle)
203 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
204 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
205 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
206 char mcc_pl[MLXSW_REG_MCC_LEN];
210 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
211 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
215 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
216 if (control_state != MLXFW_FSM_STATE_IDLE)
219 mlxsw_reg_mcc_pack(mcc_pl,
220 MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
222 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
225 static int mlxsw_sp_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
226 u32 fwhandle, u16 component_index,
229 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
230 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
231 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
232 char mcc_pl[MLXSW_REG_MCC_LEN];
234 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
235 component_index, fwhandle, component_size);
236 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
239 static int mlxsw_sp_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
240 u32 fwhandle, u8 *data, u16 size,
243 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
244 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
245 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
246 char mcda_pl[MLXSW_REG_MCDA_LEN];
248 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
249 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcda), mcda_pl);
252 static int mlxsw_sp_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
253 u32 fwhandle, u16 component_index)
255 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
256 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
257 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
258 char mcc_pl[MLXSW_REG_MCC_LEN];
260 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
261 component_index, fwhandle, 0);
262 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
265 static int mlxsw_sp_fsm_activate(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
267 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
268 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
269 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
270 char mcc_pl[MLXSW_REG_MCC_LEN];
272 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE, 0,
274 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
277 static int mlxsw_sp_fsm_query_state(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
278 enum mlxfw_fsm_state *fsm_state,
279 enum mlxfw_fsm_state_err *fsm_state_err)
281 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
282 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
283 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
284 char mcc_pl[MLXSW_REG_MCC_LEN];
289 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
290 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
294 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
295 *fsm_state = control_state;
296 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
297 MLXFW_FSM_STATE_ERR_MAX);
301 static void mlxsw_sp_fsm_cancel(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
303 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
304 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
305 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
306 char mcc_pl[MLXSW_REG_MCC_LEN];
308 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL, 0,
310 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
313 static void mlxsw_sp_fsm_release(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
315 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
316 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
317 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
318 char mcc_pl[MLXSW_REG_MCC_LEN];
320 mlxsw_reg_mcc_pack(mcc_pl,
321 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE, 0,
323 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
326 static void mlxsw_sp_status_notify(struct mlxfw_dev *mlxfw_dev,
327 const char *msg, const char *comp_name,
328 u32 done_bytes, u32 total_bytes)
330 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
331 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
332 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
334 devlink_flash_update_status_notify(priv_to_devlink(mlxsw_sp->core),
336 done_bytes, total_bytes);
339 static const struct mlxfw_dev_ops mlxsw_sp_mlxfw_dev_ops = {
340 .component_query = mlxsw_sp_component_query,
341 .fsm_lock = mlxsw_sp_fsm_lock,
342 .fsm_component_update = mlxsw_sp_fsm_component_update,
343 .fsm_block_download = mlxsw_sp_fsm_block_download,
344 .fsm_component_verify = mlxsw_sp_fsm_component_verify,
345 .fsm_activate = mlxsw_sp_fsm_activate,
346 .fsm_query_state = mlxsw_sp_fsm_query_state,
347 .fsm_cancel = mlxsw_sp_fsm_cancel,
348 .fsm_release = mlxsw_sp_fsm_release,
349 .status_notify = mlxsw_sp_status_notify,
352 static int mlxsw_sp_firmware_flash(struct mlxsw_sp *mlxsw_sp,
353 const struct firmware *firmware,
354 struct netlink_ext_ack *extack)
356 struct mlxsw_sp_mlxfw_dev mlxsw_sp_mlxfw_dev = {
358 .ops = &mlxsw_sp_mlxfw_dev_ops,
359 .psid = mlxsw_sp->bus_info->psid,
360 .psid_size = strlen(mlxsw_sp->bus_info->psid),
366 mlxsw_core_fw_flash_start(mlxsw_sp->core);
367 devlink_flash_update_begin_notify(priv_to_devlink(mlxsw_sp->core));
368 err = mlxfw_firmware_flash(&mlxsw_sp_mlxfw_dev.mlxfw_dev,
370 devlink_flash_update_end_notify(priv_to_devlink(mlxsw_sp->core));
371 mlxsw_core_fw_flash_end(mlxsw_sp->core);
376 static int mlxsw_sp_fw_rev_validate(struct mlxsw_sp *mlxsw_sp)
378 const struct mlxsw_fw_rev *rev = &mlxsw_sp->bus_info->fw_rev;
379 const struct mlxsw_fw_rev *req_rev = mlxsw_sp->req_rev;
380 const char *fw_filename = mlxsw_sp->fw_filename;
381 union devlink_param_value value;
382 const struct firmware *firmware;
385 /* Don't check if driver does not require it */
386 if (!req_rev || !fw_filename)
389 /* Don't check if devlink 'fw_load_policy' param is 'flash' */
390 err = devlink_param_driverinit_value_get(priv_to_devlink(mlxsw_sp->core),
391 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
395 if (value.vu8 == DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)
398 /* Validate driver & FW are compatible */
399 if (rev->major != req_rev->major) {
400 WARN(1, "Mismatch in major FW version [%d:%d] is never expected; Please contact support\n",
401 rev->major, req_rev->major);
404 if (MLXSW_SP_FWREV_MINOR_TO_BRANCH(rev->minor) ==
405 MLXSW_SP_FWREV_MINOR_TO_BRANCH(req_rev->minor) &&
406 (rev->minor > req_rev->minor ||
407 (rev->minor == req_rev->minor &&
408 rev->subminor >= req_rev->subminor)))
411 dev_info(mlxsw_sp->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver\n",
412 rev->major, rev->minor, rev->subminor);
413 dev_info(mlxsw_sp->bus_info->dev, "Flashing firmware using file %s\n",
416 err = request_firmware_direct(&firmware, fw_filename,
417 mlxsw_sp->bus_info->dev);
419 dev_err(mlxsw_sp->bus_info->dev, "Could not request firmware file %s\n",
424 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, NULL);
425 release_firmware(firmware);
427 dev_err(mlxsw_sp->bus_info->dev, "Could not upgrade firmware\n");
429 /* On FW flash success, tell the caller FW reset is needed
430 * if current FW supports it.
432 if (rev->minor >= req_rev->can_reset_minor)
433 return err ? err : -EAGAIN;
438 static int mlxsw_sp_flash_update(struct mlxsw_core *mlxsw_core,
439 const char *file_name, const char *component,
440 struct netlink_ext_ack *extack)
442 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
443 const struct firmware *firmware;
449 err = request_firmware_direct(&firmware, file_name,
450 mlxsw_sp->bus_info->dev);
453 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, extack);
454 release_firmware(firmware);
459 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
460 unsigned int counter_index, u64 *packets,
463 char mgpc_pl[MLXSW_REG_MGPC_LEN];
466 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
467 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
468 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
472 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
474 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
478 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
479 unsigned int counter_index)
481 char mgpc_pl[MLXSW_REG_MGPC_LEN];
483 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
484 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
485 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
488 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
489 unsigned int *p_counter_index)
493 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
497 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
499 goto err_counter_clear;
503 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
508 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
509 unsigned int counter_index)
511 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
515 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
516 const struct mlxsw_tx_info *tx_info)
518 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
520 memset(txhdr, 0, MLXSW_TXHDR_LEN);
522 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
523 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
524 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
525 mlxsw_tx_hdr_swid_set(txhdr, 0);
526 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
527 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
528 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
531 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
534 case BR_STATE_FORWARDING:
535 return MLXSW_REG_SPMS_STATE_FORWARDING;
536 case BR_STATE_LEARNING:
537 return MLXSW_REG_SPMS_STATE_LEARNING;
538 case BR_STATE_LISTENING: /* fall-through */
539 case BR_STATE_DISABLED: /* fall-through */
540 case BR_STATE_BLOCKING:
541 return MLXSW_REG_SPMS_STATE_DISCARDING;
547 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
550 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
551 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
555 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
558 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
559 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
561 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
566 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
568 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
571 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
574 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
578 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
579 bool enable, u32 rate)
581 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
582 char mpsc_pl[MLXSW_REG_MPSC_LEN];
584 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
585 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
588 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
591 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
592 char paos_pl[MLXSW_REG_PAOS_LEN];
594 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
595 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
596 MLXSW_PORT_ADMIN_STATUS_DOWN);
597 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
600 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
603 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
604 char ppad_pl[MLXSW_REG_PPAD_LEN];
606 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
607 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
608 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
611 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
613 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
614 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
616 ether_addr_copy(addr, mlxsw_sp->base_mac);
617 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
618 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
621 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
623 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
624 char pmtu_pl[MLXSW_REG_PMTU_LEN];
628 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
629 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
630 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
633 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
638 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
639 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
642 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
644 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
645 char pspa_pl[MLXSW_REG_PSPA_LEN];
647 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
648 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
651 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
653 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
654 char svpe_pl[MLXSW_REG_SVPE_LEN];
656 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
657 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
660 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
663 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
667 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
670 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
672 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
677 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
680 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
681 char spvid_pl[MLXSW_REG_SPVID_LEN];
683 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
684 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
687 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
690 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
691 char spaft_pl[MLXSW_REG_SPAFT_LEN];
693 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
694 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
697 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
702 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
706 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
709 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
711 goto err_port_allow_untagged_set;
714 mlxsw_sp_port->pvid = vid;
717 err_port_allow_untagged_set:
718 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
723 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
725 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
726 char sspr_pl[MLXSW_REG_SSPR_LEN];
728 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
729 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
732 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
733 u8 local_port, u8 *p_module,
734 u8 *p_width, u8 *p_lane)
736 char pmlp_pl[MLXSW_REG_PMLP_LEN];
739 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
740 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
743 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
744 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
745 *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
749 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port,
750 u8 module, u8 width, u8 lane)
752 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
753 char pmlp_pl[MLXSW_REG_PMLP_LEN];
756 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
757 mlxsw_reg_pmlp_width_set(pmlp_pl, width);
758 for (i = 0; i < width; i++) {
759 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
760 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */
763 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
766 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
768 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
769 char pmlp_pl[MLXSW_REG_PMLP_LEN];
771 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
772 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
773 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
776 static int mlxsw_sp_port_open(struct net_device *dev)
778 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
781 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
784 netif_start_queue(dev);
788 static int mlxsw_sp_port_stop(struct net_device *dev)
790 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
792 netif_stop_queue(dev);
793 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
796 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
797 struct net_device *dev)
799 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
800 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
801 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
802 const struct mlxsw_tx_info tx_info = {
803 .local_port = mlxsw_sp_port->local_port,
809 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
811 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
812 return NETDEV_TX_BUSY;
814 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
815 struct sk_buff *skb_orig = skb;
817 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
819 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
820 dev_kfree_skb_any(skb_orig);
823 dev_consume_skb_any(skb_orig);
826 if (eth_skb_pad(skb)) {
827 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
831 mlxsw_sp_txhdr_construct(skb, &tx_info);
832 /* TX header is consumed by HW on the way so we shouldn't count its
833 * bytes as being sent.
835 len = skb->len - MLXSW_TXHDR_LEN;
837 /* Due to a race we might fail here because of a full queue. In that
838 * unlikely case we simply drop the packet.
840 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
843 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
844 u64_stats_update_begin(&pcpu_stats->syncp);
845 pcpu_stats->tx_packets++;
846 pcpu_stats->tx_bytes += len;
847 u64_stats_update_end(&pcpu_stats->syncp);
849 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
850 dev_kfree_skb_any(skb);
855 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
859 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
861 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
862 struct sockaddr *addr = p;
865 if (!is_valid_ether_addr(addr->sa_data))
866 return -EADDRNOTAVAIL;
868 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
871 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
875 static u16 mlxsw_sp_pg_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp,
878 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
881 #define MLXSW_SP_CELL_FACTOR 2 /* 2 * cell_size / (IPG + cell_size + 1) */
883 static u16 mlxsw_sp_pfc_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
886 delay = mlxsw_sp_bytes_cells(mlxsw_sp, DIV_ROUND_UP(delay,
888 return MLXSW_SP_CELL_FACTOR * delay + mlxsw_sp_bytes_cells(mlxsw_sp,
892 /* Maximum delay buffer needed in case of PAUSE frames, in bytes.
893 * Assumes 100m cable and maximum MTU.
895 #define MLXSW_SP_PAUSE_DELAY 58752
897 static u16 mlxsw_sp_pg_buf_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
898 u16 delay, bool pfc, bool pause)
901 return mlxsw_sp_pfc_delay_get(mlxsw_sp, mtu, delay);
903 return mlxsw_sp_bytes_cells(mlxsw_sp, MLXSW_SP_PAUSE_DELAY);
908 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres,
912 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
914 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
918 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
919 u8 *prio_tc, bool pause_en,
920 struct ieee_pfc *my_pfc)
922 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
923 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
924 u16 delay = !!my_pfc ? my_pfc->delay : 0;
925 char pbmc_pl[MLXSW_REG_PBMC_LEN];
926 u32 taken_headroom_cells = 0;
927 u32 max_headroom_cells;
930 max_headroom_cells = mlxsw_sp_sb_max_headroom_cells(mlxsw_sp);
932 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
933 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
937 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
938 bool configure = false;
945 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
946 if (prio_tc[j] == i) {
947 pfc = pfc_en & BIT(j);
956 lossy = !(pfc || pause_en);
957 thres_cells = mlxsw_sp_pg_buf_threshold_get(mlxsw_sp, mtu);
958 delay_cells = mlxsw_sp_pg_buf_delay_get(mlxsw_sp, mtu, delay,
960 total_cells = thres_cells + delay_cells;
962 taken_headroom_cells += total_cells;
963 if (taken_headroom_cells > max_headroom_cells)
966 mlxsw_sp_pg_buf_pack(pbmc_pl, i, total_cells,
970 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
973 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
974 int mtu, bool pause_en)
976 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
977 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
978 struct ieee_pfc *my_pfc;
981 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
982 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
984 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
988 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
990 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
991 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
994 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
997 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
999 goto err_span_port_mtu_update;
1000 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
1002 goto err_port_mtu_set;
1007 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
1008 err_span_port_mtu_update:
1009 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1014 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
1015 struct rtnl_link_stats64 *stats)
1017 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1018 struct mlxsw_sp_port_pcpu_stats *p;
1019 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1024 for_each_possible_cpu(i) {
1025 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
1027 start = u64_stats_fetch_begin_irq(&p->syncp);
1028 rx_packets = p->rx_packets;
1029 rx_bytes = p->rx_bytes;
1030 tx_packets = p->tx_packets;
1031 tx_bytes = p->tx_bytes;
1032 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1034 stats->rx_packets += rx_packets;
1035 stats->rx_bytes += rx_bytes;
1036 stats->tx_packets += tx_packets;
1037 stats->tx_bytes += tx_bytes;
1038 /* tx_dropped is u32, updated without syncp protection. */
1039 tx_dropped += p->tx_dropped;
1041 stats->tx_dropped = tx_dropped;
1045 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
1048 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1055 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
1059 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1060 return mlxsw_sp_port_get_sw_stats64(dev, sp);
1066 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
1067 int prio, char *ppcnt_pl)
1069 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1070 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1072 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1073 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1076 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
1077 struct rtnl_link_stats64 *stats)
1079 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1082 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
1088 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
1090 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
1092 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
1094 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
1096 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
1098 stats->rx_crc_errors =
1099 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
1100 stats->rx_frame_errors =
1101 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
1103 stats->rx_length_errors = (
1104 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
1105 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
1106 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
1108 stats->rx_errors = (stats->rx_crc_errors +
1109 stats->rx_frame_errors + stats->rx_length_errors);
1116 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
1117 struct mlxsw_sp_port_xstats *xstats)
1119 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1122 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
1125 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
1127 for (i = 0; i < TC_MAX_QUEUE; i++) {
1128 err = mlxsw_sp_port_get_stats_raw(dev,
1129 MLXSW_REG_PPCNT_TC_CONG_TC,
1132 xstats->wred_drop[i] =
1133 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
1135 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
1140 xstats->backlog[i] =
1141 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1142 xstats->tail_drop[i] =
1143 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
1146 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1147 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
1152 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
1153 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
1157 static void update_stats_cache(struct work_struct *work)
1159 struct mlxsw_sp_port *mlxsw_sp_port =
1160 container_of(work, struct mlxsw_sp_port,
1161 periodic_hw_stats.update_dw.work);
1163 if (!netif_carrier_ok(mlxsw_sp_port->dev))
1166 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
1167 &mlxsw_sp_port->periodic_hw_stats.stats);
1168 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
1169 &mlxsw_sp_port->periodic_hw_stats.xstats);
1172 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1173 MLXSW_HW_STATS_UPDATE_TIME);
1176 /* Return the stats from a cache that is updated periodically,
1177 * as this function might get called in an atomic context.
1180 mlxsw_sp_port_get_stats64(struct net_device *dev,
1181 struct rtnl_link_stats64 *stats)
1183 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1185 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
1188 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1189 u16 vid_begin, u16 vid_end,
1190 bool is_member, bool untagged)
1192 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1196 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1200 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1201 vid_end, is_member, untagged);
1202 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1207 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1208 u16 vid_end, bool is_member, bool untagged)
1213 for (vid = vid_begin; vid <= vid_end;
1214 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1215 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1218 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1219 is_member, untagged);
1227 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1230 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1232 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1233 &mlxsw_sp_port->vlans_list, list) {
1234 if (!flush_default &&
1235 mlxsw_sp_port_vlan->vid == MLXSW_SP_DEFAULT_VID)
1237 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1242 mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1244 if (mlxsw_sp_port_vlan->bridge_port)
1245 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1246 else if (mlxsw_sp_port_vlan->fid)
1247 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1250 struct mlxsw_sp_port_vlan *
1251 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1253 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1254 bool untagged = vid == MLXSW_SP_DEFAULT_VID;
1257 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1258 if (mlxsw_sp_port_vlan)
1259 return ERR_PTR(-EEXIST);
1261 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1263 return ERR_PTR(err);
1265 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1266 if (!mlxsw_sp_port_vlan) {
1268 goto err_port_vlan_alloc;
1271 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1272 mlxsw_sp_port_vlan->vid = vid;
1273 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1275 return mlxsw_sp_port_vlan;
1277 err_port_vlan_alloc:
1278 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1279 return ERR_PTR(err);
1282 void mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1284 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1285 u16 vid = mlxsw_sp_port_vlan->vid;
1287 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port_vlan);
1288 list_del(&mlxsw_sp_port_vlan->list);
1289 kfree(mlxsw_sp_port_vlan);
1290 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1293 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1294 __be16 __always_unused proto, u16 vid)
1296 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1298 /* VLAN 0 is added to HW filter when device goes up, but it is
1299 * reserved in our case, so simply return.
1304 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid));
1307 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1308 __be16 __always_unused proto, u16 vid)
1310 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1311 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1313 /* VLAN 0 is removed from HW filter when device goes down, but
1314 * it is reserved in our case, so simply return.
1319 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1320 if (!mlxsw_sp_port_vlan)
1322 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1327 static struct mlxsw_sp_port_mall_tc_entry *
1328 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1329 unsigned long cookie) {
1330 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1332 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1333 if (mall_tc_entry->cookie == cookie)
1334 return mall_tc_entry;
1340 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1341 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1342 const struct flow_action_entry *act,
1345 enum mlxsw_sp_span_type span_type;
1348 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1352 mirror->ingress = ingress;
1353 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1354 return mlxsw_sp_span_mirror_add(mlxsw_sp_port, act->dev, span_type,
1355 true, &mirror->span_id);
1359 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1360 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1362 enum mlxsw_sp_span_type span_type;
1364 span_type = mirror->ingress ?
1365 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1366 mlxsw_sp_span_mirror_del(mlxsw_sp_port, mirror->span_id,
1371 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1372 struct tc_cls_matchall_offload *cls,
1373 const struct flow_action_entry *act,
1378 if (!mlxsw_sp_port->sample)
1380 if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1381 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1384 if (act->sample.rate > MLXSW_REG_MPSC_RATE_MAX) {
1385 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1389 rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1390 act->sample.psample_group);
1391 mlxsw_sp_port->sample->truncate = act->sample.truncate;
1392 mlxsw_sp_port->sample->trunc_size = act->sample.trunc_size;
1393 mlxsw_sp_port->sample->rate = act->sample.rate;
1395 err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, act->sample.rate);
1397 goto err_port_sample_set;
1400 err_port_sample_set:
1401 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1406 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1408 if (!mlxsw_sp_port->sample)
1411 mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1412 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1415 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1416 struct tc_cls_matchall_offload *f,
1419 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1420 __be16 protocol = f->common.protocol;
1421 struct flow_action_entry *act;
1424 if (!flow_offload_has_one_action(&f->rule->action)) {
1425 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1429 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1432 mall_tc_entry->cookie = f->cookie;
1434 act = &f->rule->action.entries[0];
1436 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
1437 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1439 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1440 mirror = &mall_tc_entry->mirror;
1441 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1444 } else if (act->id == FLOW_ACTION_SAMPLE &&
1445 protocol == htons(ETH_P_ALL)) {
1446 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1447 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, f,
1454 goto err_add_action;
1456 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1460 kfree(mall_tc_entry);
1464 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1465 struct tc_cls_matchall_offload *f)
1467 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1469 mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1471 if (!mall_tc_entry) {
1472 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1475 list_del(&mall_tc_entry->list);
1477 switch (mall_tc_entry->type) {
1478 case MLXSW_SP_PORT_MALL_MIRROR:
1479 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1480 &mall_tc_entry->mirror);
1482 case MLXSW_SP_PORT_MALL_SAMPLE:
1483 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1489 kfree(mall_tc_entry);
1492 static int mlxsw_sp_setup_tc_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1493 struct tc_cls_matchall_offload *f,
1496 switch (f->command) {
1497 case TC_CLSMATCHALL_REPLACE:
1498 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, f,
1500 case TC_CLSMATCHALL_DESTROY:
1501 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, f);
1509 mlxsw_sp_setup_tc_cls_flower(struct mlxsw_sp_acl_block *acl_block,
1510 struct tc_cls_flower_offload *f)
1512 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_acl_block_mlxsw_sp(acl_block);
1514 switch (f->command) {
1515 case TC_CLSFLOWER_REPLACE:
1516 return mlxsw_sp_flower_replace(mlxsw_sp, acl_block, f);
1517 case TC_CLSFLOWER_DESTROY:
1518 mlxsw_sp_flower_destroy(mlxsw_sp, acl_block, f);
1520 case TC_CLSFLOWER_STATS:
1521 return mlxsw_sp_flower_stats(mlxsw_sp, acl_block, f);
1522 case TC_CLSFLOWER_TMPLT_CREATE:
1523 return mlxsw_sp_flower_tmplt_create(mlxsw_sp, acl_block, f);
1524 case TC_CLSFLOWER_TMPLT_DESTROY:
1525 mlxsw_sp_flower_tmplt_destroy(mlxsw_sp, acl_block, f);
1532 static int mlxsw_sp_setup_tc_block_cb_matchall(enum tc_setup_type type,
1534 void *cb_priv, bool ingress)
1536 struct mlxsw_sp_port *mlxsw_sp_port = cb_priv;
1539 case TC_SETUP_CLSMATCHALL:
1540 if (!tc_cls_can_offload_and_chain0(mlxsw_sp_port->dev,
1544 return mlxsw_sp_setup_tc_cls_matchall(mlxsw_sp_port, type_data,
1546 case TC_SETUP_CLSFLOWER:
1553 static int mlxsw_sp_setup_tc_block_cb_matchall_ig(enum tc_setup_type type,
1557 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1561 static int mlxsw_sp_setup_tc_block_cb_matchall_eg(enum tc_setup_type type,
1565 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1569 static int mlxsw_sp_setup_tc_block_cb_flower(enum tc_setup_type type,
1570 void *type_data, void *cb_priv)
1572 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1575 case TC_SETUP_CLSMATCHALL:
1577 case TC_SETUP_CLSFLOWER:
1578 if (mlxsw_sp_acl_block_disabled(acl_block))
1581 return mlxsw_sp_setup_tc_cls_flower(acl_block, type_data);
1588 mlxsw_sp_setup_tc_block_flower_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1589 struct tcf_block *block, bool ingress,
1590 struct netlink_ext_ack *extack)
1592 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1593 struct mlxsw_sp_acl_block *acl_block;
1594 struct tcf_block_cb *block_cb;
1597 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1600 acl_block = mlxsw_sp_acl_block_create(mlxsw_sp, block->net);
1603 block_cb = __tcf_block_cb_register(block,
1604 mlxsw_sp_setup_tc_block_cb_flower,
1605 mlxsw_sp, acl_block, extack);
1606 if (IS_ERR(block_cb)) {
1607 err = PTR_ERR(block_cb);
1608 goto err_cb_register;
1611 acl_block = tcf_block_cb_priv(block_cb);
1613 tcf_block_cb_incref(block_cb);
1614 err = mlxsw_sp_acl_block_bind(mlxsw_sp, acl_block,
1615 mlxsw_sp_port, ingress);
1617 goto err_block_bind;
1620 mlxsw_sp_port->ing_acl_block = acl_block;
1622 mlxsw_sp_port->eg_acl_block = acl_block;
1627 if (!tcf_block_cb_decref(block_cb)) {
1628 __tcf_block_cb_unregister(block, block_cb);
1630 mlxsw_sp_acl_block_destroy(acl_block);
1636 mlxsw_sp_setup_tc_block_flower_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1637 struct tcf_block *block, bool ingress)
1639 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1640 struct mlxsw_sp_acl_block *acl_block;
1641 struct tcf_block_cb *block_cb;
1644 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1650 mlxsw_sp_port->ing_acl_block = NULL;
1652 mlxsw_sp_port->eg_acl_block = NULL;
1654 acl_block = tcf_block_cb_priv(block_cb);
1655 err = mlxsw_sp_acl_block_unbind(mlxsw_sp, acl_block,
1656 mlxsw_sp_port, ingress);
1657 if (!err && !tcf_block_cb_decref(block_cb)) {
1658 __tcf_block_cb_unregister(block, block_cb);
1659 mlxsw_sp_acl_block_destroy(acl_block);
1663 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1664 struct tc_block_offload *f)
1670 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1671 cb = mlxsw_sp_setup_tc_block_cb_matchall_ig;
1673 } else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1674 cb = mlxsw_sp_setup_tc_block_cb_matchall_eg;
1680 switch (f->command) {
1682 err = tcf_block_cb_register(f->block, cb, mlxsw_sp_port,
1683 mlxsw_sp_port, f->extack);
1686 err = mlxsw_sp_setup_tc_block_flower_bind(mlxsw_sp_port,
1690 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1694 case TC_BLOCK_UNBIND:
1695 mlxsw_sp_setup_tc_block_flower_unbind(mlxsw_sp_port,
1697 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1704 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1707 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1710 case TC_SETUP_BLOCK:
1711 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1712 case TC_SETUP_QDISC_RED:
1713 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1714 case TC_SETUP_QDISC_PRIO:
1715 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1722 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1724 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1727 if (mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->ing_acl_block) ||
1728 mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->eg_acl_block) ||
1729 !list_empty(&mlxsw_sp_port->mall_tc_list)) {
1730 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1733 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->ing_acl_block);
1734 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->eg_acl_block);
1736 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->ing_acl_block);
1737 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->eg_acl_block);
1742 static int mlxsw_sp_feature_loopback(struct net_device *dev, bool enable)
1744 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1745 char pplr_pl[MLXSW_REG_PPLR_LEN];
1748 if (netif_running(dev))
1749 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1751 mlxsw_reg_pplr_pack(pplr_pl, mlxsw_sp_port->local_port, enable);
1752 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pplr),
1755 if (netif_running(dev))
1756 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1761 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1763 static int mlxsw_sp_handle_feature(struct net_device *dev,
1764 netdev_features_t wanted_features,
1765 netdev_features_t feature,
1766 mlxsw_sp_feature_handler feature_handler)
1768 netdev_features_t changes = wanted_features ^ dev->features;
1769 bool enable = !!(wanted_features & feature);
1772 if (!(changes & feature))
1775 err = feature_handler(dev, enable);
1777 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1778 enable ? "Enable" : "Disable", &feature, err);
1783 dev->features |= feature;
1785 dev->features &= ~feature;
1789 static int mlxsw_sp_set_features(struct net_device *dev,
1790 netdev_features_t features)
1792 netdev_features_t oper_features = dev->features;
1795 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1796 mlxsw_sp_feature_hw_tc);
1797 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_LOOPBACK,
1798 mlxsw_sp_feature_loopback);
1801 dev->features = oper_features;
1808 static struct devlink_port *
1809 mlxsw_sp_port_get_devlink_port(struct net_device *dev)
1811 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1812 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1814 return mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
1815 mlxsw_sp_port->local_port);
1818 static int mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1821 struct hwtstamp_config config;
1824 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1827 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port,
1832 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1838 static int mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1841 struct hwtstamp_config config;
1844 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_get(mlxsw_sp_port,
1849 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1855 static inline void mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port *mlxsw_sp_port)
1857 struct hwtstamp_config config = {0};
1859 mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port, &config);
1863 mlxsw_sp_port_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1865 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1869 return mlxsw_sp_port_hwtstamp_set(mlxsw_sp_port, ifr);
1871 return mlxsw_sp_port_hwtstamp_get(mlxsw_sp_port, ifr);
1877 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1878 .ndo_open = mlxsw_sp_port_open,
1879 .ndo_stop = mlxsw_sp_port_stop,
1880 .ndo_start_xmit = mlxsw_sp_port_xmit,
1881 .ndo_setup_tc = mlxsw_sp_setup_tc,
1882 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1883 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1884 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1885 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1886 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1887 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1888 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1889 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1890 .ndo_set_features = mlxsw_sp_set_features,
1891 .ndo_get_devlink_port = mlxsw_sp_port_get_devlink_port,
1892 .ndo_do_ioctl = mlxsw_sp_port_ioctl,
1895 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1896 struct ethtool_drvinfo *drvinfo)
1898 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1899 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1901 strlcpy(drvinfo->driver, mlxsw_sp->bus_info->device_kind,
1902 sizeof(drvinfo->driver));
1903 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1904 sizeof(drvinfo->version));
1905 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1907 mlxsw_sp->bus_info->fw_rev.major,
1908 mlxsw_sp->bus_info->fw_rev.minor,
1909 mlxsw_sp->bus_info->fw_rev.subminor);
1910 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1911 sizeof(drvinfo->bus_info));
1914 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1915 struct ethtool_pauseparam *pause)
1917 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1919 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1920 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1923 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1924 struct ethtool_pauseparam *pause)
1926 char pfcc_pl[MLXSW_REG_PFCC_LEN];
1928 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1929 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
1930 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
1932 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
1936 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
1937 struct ethtool_pauseparam *pause)
1939 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1940 bool pause_en = pause->tx_pause || pause->rx_pause;
1943 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
1944 netdev_err(dev, "PFC already enabled on port\n");
1948 if (pause->autoneg) {
1949 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
1953 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1955 netdev_err(dev, "Failed to configure port's headroom\n");
1959 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
1961 netdev_err(dev, "Failed to set PAUSE parameters\n");
1962 goto err_port_pause_configure;
1965 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
1966 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
1970 err_port_pause_configure:
1971 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1972 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1976 struct mlxsw_sp_port_hw_stats {
1977 char str[ETH_GSTRING_LEN];
1978 u64 (*getter)(const char *payload);
1982 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
1984 .str = "a_frames_transmitted_ok",
1985 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
1988 .str = "a_frames_received_ok",
1989 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
1992 .str = "a_frame_check_sequence_errors",
1993 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
1996 .str = "a_alignment_errors",
1997 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
2000 .str = "a_octets_transmitted_ok",
2001 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
2004 .str = "a_octets_received_ok",
2005 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
2008 .str = "a_multicast_frames_xmitted_ok",
2009 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
2012 .str = "a_broadcast_frames_xmitted_ok",
2013 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
2016 .str = "a_multicast_frames_received_ok",
2017 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
2020 .str = "a_broadcast_frames_received_ok",
2021 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
2024 .str = "a_in_range_length_errors",
2025 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
2028 .str = "a_out_of_range_length_field",
2029 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
2032 .str = "a_frame_too_long_errors",
2033 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
2036 .str = "a_symbol_error_during_carrier",
2037 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
2040 .str = "a_mac_control_frames_transmitted",
2041 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
2044 .str = "a_mac_control_frames_received",
2045 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
2048 .str = "a_unsupported_opcodes_received",
2049 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
2052 .str = "a_pause_mac_ctrl_frames_received",
2053 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
2056 .str = "a_pause_mac_ctrl_frames_xmitted",
2057 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
2061 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
2063 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2863_stats[] = {
2065 .str = "if_in_discards",
2066 .getter = mlxsw_reg_ppcnt_if_in_discards_get,
2069 .str = "if_out_discards",
2070 .getter = mlxsw_reg_ppcnt_if_out_discards_get,
2073 .str = "if_out_errors",
2074 .getter = mlxsw_reg_ppcnt_if_out_errors_get,
2078 #define MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN \
2079 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2863_stats)
2081 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2819_stats[] = {
2083 .str = "ether_stats_undersize_pkts",
2084 .getter = mlxsw_reg_ppcnt_ether_stats_undersize_pkts_get,
2087 .str = "ether_stats_oversize_pkts",
2088 .getter = mlxsw_reg_ppcnt_ether_stats_oversize_pkts_get,
2091 .str = "ether_stats_fragments",
2092 .getter = mlxsw_reg_ppcnt_ether_stats_fragments_get,
2095 .str = "ether_pkts64octets",
2096 .getter = mlxsw_reg_ppcnt_ether_stats_pkts64octets_get,
2099 .str = "ether_pkts65to127octets",
2100 .getter = mlxsw_reg_ppcnt_ether_stats_pkts65to127octets_get,
2103 .str = "ether_pkts128to255octets",
2104 .getter = mlxsw_reg_ppcnt_ether_stats_pkts128to255octets_get,
2107 .str = "ether_pkts256to511octets",
2108 .getter = mlxsw_reg_ppcnt_ether_stats_pkts256to511octets_get,
2111 .str = "ether_pkts512to1023octets",
2112 .getter = mlxsw_reg_ppcnt_ether_stats_pkts512to1023octets_get,
2115 .str = "ether_pkts1024to1518octets",
2116 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1024to1518octets_get,
2119 .str = "ether_pkts1519to2047octets",
2120 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1519to2047octets_get,
2123 .str = "ether_pkts2048to4095octets",
2124 .getter = mlxsw_reg_ppcnt_ether_stats_pkts2048to4095octets_get,
2127 .str = "ether_pkts4096to8191octets",
2128 .getter = mlxsw_reg_ppcnt_ether_stats_pkts4096to8191octets_get,
2131 .str = "ether_pkts8192to10239octets",
2132 .getter = mlxsw_reg_ppcnt_ether_stats_pkts8192to10239octets_get,
2136 #define MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN \
2137 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2819_stats)
2139 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_3635_stats[] = {
2141 .str = "dot3stats_fcs_errors",
2142 .getter = mlxsw_reg_ppcnt_dot3stats_fcs_errors_get,
2145 .str = "dot3stats_symbol_errors",
2146 .getter = mlxsw_reg_ppcnt_dot3stats_symbol_errors_get,
2149 .str = "dot3control_in_unknown_opcodes",
2150 .getter = mlxsw_reg_ppcnt_dot3control_in_unknown_opcodes_get,
2153 .str = "dot3in_pause_frames",
2154 .getter = mlxsw_reg_ppcnt_dot3in_pause_frames_get,
2158 #define MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN \
2159 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_3635_stats)
2161 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_discard_stats[] = {
2163 .str = "discard_ingress_general",
2164 .getter = mlxsw_reg_ppcnt_ingress_general_get,
2167 .str = "discard_ingress_policy_engine",
2168 .getter = mlxsw_reg_ppcnt_ingress_policy_engine_get,
2171 .str = "discard_ingress_vlan_membership",
2172 .getter = mlxsw_reg_ppcnt_ingress_vlan_membership_get,
2175 .str = "discard_ingress_tag_frame_type",
2176 .getter = mlxsw_reg_ppcnt_ingress_tag_frame_type_get,
2179 .str = "discard_egress_vlan_membership",
2180 .getter = mlxsw_reg_ppcnt_egress_vlan_membership_get,
2183 .str = "discard_loopback_filter",
2184 .getter = mlxsw_reg_ppcnt_loopback_filter_get,
2187 .str = "discard_egress_general",
2188 .getter = mlxsw_reg_ppcnt_egress_general_get,
2191 .str = "discard_egress_hoq",
2192 .getter = mlxsw_reg_ppcnt_egress_hoq_get,
2195 .str = "discard_egress_policy_engine",
2196 .getter = mlxsw_reg_ppcnt_egress_policy_engine_get,
2199 .str = "discard_ingress_tx_link_down",
2200 .getter = mlxsw_reg_ppcnt_ingress_tx_link_down_get,
2203 .str = "discard_egress_stp_filter",
2204 .getter = mlxsw_reg_ppcnt_egress_stp_filter_get,
2207 .str = "discard_egress_sll",
2208 .getter = mlxsw_reg_ppcnt_egress_sll_get,
2212 #define MLXSW_SP_PORT_HW_DISCARD_STATS_LEN \
2213 ARRAY_SIZE(mlxsw_sp_port_hw_discard_stats)
2215 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
2217 .str = "rx_octets_prio",
2218 .getter = mlxsw_reg_ppcnt_rx_octets_get,
2221 .str = "rx_frames_prio",
2222 .getter = mlxsw_reg_ppcnt_rx_frames_get,
2225 .str = "tx_octets_prio",
2226 .getter = mlxsw_reg_ppcnt_tx_octets_get,
2229 .str = "tx_frames_prio",
2230 .getter = mlxsw_reg_ppcnt_tx_frames_get,
2233 .str = "rx_pause_prio",
2234 .getter = mlxsw_reg_ppcnt_rx_pause_get,
2237 .str = "rx_pause_duration_prio",
2238 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
2241 .str = "tx_pause_prio",
2242 .getter = mlxsw_reg_ppcnt_tx_pause_get,
2245 .str = "tx_pause_duration_prio",
2246 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
2250 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
2252 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
2254 .str = "tc_transmit_queue_tc",
2255 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_get,
2256 .cells_bytes = true,
2259 .str = "tc_no_buffer_discard_uc_tc",
2260 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
2264 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
2266 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
2267 MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN + \
2268 MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN + \
2269 MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN + \
2270 MLXSW_SP_PORT_HW_DISCARD_STATS_LEN + \
2271 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN * \
2272 IEEE_8021QAZ_MAX_TCS) + \
2273 (MLXSW_SP_PORT_HW_TC_STATS_LEN * \
2276 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
2280 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
2281 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2282 mlxsw_sp_port_hw_prio_stats[i].str, prio);
2283 *p += ETH_GSTRING_LEN;
2287 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
2291 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
2292 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2293 mlxsw_sp_port_hw_tc_stats[i].str, tc);
2294 *p += ETH_GSTRING_LEN;
2298 static void mlxsw_sp_port_get_strings(struct net_device *dev,
2299 u32 stringset, u8 *data)
2304 switch (stringset) {
2306 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
2307 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
2309 p += ETH_GSTRING_LEN;
2312 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN; i++) {
2313 memcpy(p, mlxsw_sp_port_hw_rfc_2863_stats[i].str,
2315 p += ETH_GSTRING_LEN;
2318 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN; i++) {
2319 memcpy(p, mlxsw_sp_port_hw_rfc_2819_stats[i].str,
2321 p += ETH_GSTRING_LEN;
2324 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN; i++) {
2325 memcpy(p, mlxsw_sp_port_hw_rfc_3635_stats[i].str,
2327 p += ETH_GSTRING_LEN;
2330 for (i = 0; i < MLXSW_SP_PORT_HW_DISCARD_STATS_LEN; i++) {
2331 memcpy(p, mlxsw_sp_port_hw_discard_stats[i].str,
2333 p += ETH_GSTRING_LEN;
2336 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2337 mlxsw_sp_port_get_prio_strings(&p, i);
2339 for (i = 0; i < TC_MAX_QUEUE; i++)
2340 mlxsw_sp_port_get_tc_strings(&p, i);
2346 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
2347 enum ethtool_phys_id_state state)
2349 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2350 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2351 char mlcr_pl[MLXSW_REG_MLCR_LEN];
2355 case ETHTOOL_ID_ACTIVE:
2358 case ETHTOOL_ID_INACTIVE:
2365 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
2366 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
2370 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
2371 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
2374 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
2375 *p_hw_stats = mlxsw_sp_port_hw_stats;
2376 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
2378 case MLXSW_REG_PPCNT_RFC_2863_CNT:
2379 *p_hw_stats = mlxsw_sp_port_hw_rfc_2863_stats;
2380 *p_len = MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2382 case MLXSW_REG_PPCNT_RFC_2819_CNT:
2383 *p_hw_stats = mlxsw_sp_port_hw_rfc_2819_stats;
2384 *p_len = MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2386 case MLXSW_REG_PPCNT_RFC_3635_CNT:
2387 *p_hw_stats = mlxsw_sp_port_hw_rfc_3635_stats;
2388 *p_len = MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2390 case MLXSW_REG_PPCNT_DISCARD_CNT:
2391 *p_hw_stats = mlxsw_sp_port_hw_discard_stats;
2392 *p_len = MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2394 case MLXSW_REG_PPCNT_PRIO_CNT:
2395 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
2396 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2398 case MLXSW_REG_PPCNT_TC_CNT:
2399 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
2400 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
2409 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
2410 enum mlxsw_reg_ppcnt_grp grp, int prio,
2411 u64 *data, int data_index)
2413 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2414 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2415 struct mlxsw_sp_port_hw_stats *hw_stats;
2416 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
2420 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
2423 mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
2424 for (i = 0; i < len; i++) {
2425 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
2426 if (!hw_stats[i].cells_bytes)
2428 data[data_index + i] = mlxsw_sp_cells_bytes(mlxsw_sp,
2429 data[data_index + i]);
2433 static void mlxsw_sp_port_get_stats(struct net_device *dev,
2434 struct ethtool_stats *stats, u64 *data)
2436 int i, data_index = 0;
2438 /* IEEE 802.3 Counters */
2439 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
2441 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
2443 /* RFC 2863 Counters */
2444 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2863_CNT, 0,
2446 data_index += MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2448 /* RFC 2819 Counters */
2449 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2819_CNT, 0,
2451 data_index += MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2453 /* RFC 3635 Counters */
2454 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_3635_CNT, 0,
2456 data_index += MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2458 /* Discard Counters */
2459 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_DISCARD_CNT, 0,
2461 data_index += MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2463 /* Per-Priority Counters */
2464 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2465 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
2467 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2470 /* Per-TC Counters */
2471 for (i = 0; i < TC_MAX_QUEUE; i++) {
2472 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
2474 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
2478 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
2482 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
2488 struct mlxsw_sp1_port_link_mode {
2489 enum ethtool_link_mode_bit_indices mask_ethtool;
2494 static const struct mlxsw_sp1_port_link_mode mlxsw_sp1_port_link_mode[] = {
2496 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
2497 .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2501 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
2502 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
2503 .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2504 .speed = SPEED_1000,
2507 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
2508 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2509 .speed = SPEED_10000,
2512 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
2513 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
2514 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
2515 .speed = SPEED_10000,
2518 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2519 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2520 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2521 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
2522 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2523 .speed = SPEED_10000,
2526 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
2527 .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
2528 .speed = SPEED_20000,
2531 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
2532 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2533 .speed = SPEED_40000,
2536 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
2537 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2538 .speed = SPEED_40000,
2541 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
2542 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2543 .speed = SPEED_40000,
2546 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
2547 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2548 .speed = SPEED_40000,
2551 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
2552 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2553 .speed = SPEED_25000,
2556 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
2557 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2558 .speed = SPEED_25000,
2561 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2562 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2563 .speed = SPEED_25000,
2566 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
2567 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2568 .speed = SPEED_50000,
2571 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
2572 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2573 .speed = SPEED_50000,
2576 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
2577 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2578 .speed = SPEED_50000,
2581 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2582 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT,
2583 .speed = SPEED_56000,
2586 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2587 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT,
2588 .speed = SPEED_56000,
2591 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2592 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT,
2593 .speed = SPEED_56000,
2596 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2597 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT,
2598 .speed = SPEED_56000,
2601 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
2602 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2603 .speed = SPEED_100000,
2606 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
2607 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2608 .speed = SPEED_100000,
2611 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
2612 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2613 .speed = SPEED_100000,
2616 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
2617 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2618 .speed = SPEED_100000,
2622 #define MLXSW_SP1_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp1_port_link_mode)
2625 mlxsw_sp1_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
2627 struct ethtool_link_ksettings *cmd)
2629 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2630 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2631 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2632 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2633 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2634 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2635 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2637 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2638 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2639 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2640 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
2641 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
2642 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2646 mlxsw_sp1_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
2647 unsigned long *mode)
2651 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2652 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask)
2653 __set_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2659 mlxsw_sp1_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
2661 struct ethtool_link_ksettings *cmd)
2663 u32 speed = SPEED_UNKNOWN;
2664 u8 duplex = DUPLEX_UNKNOWN;
2670 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2671 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask) {
2672 speed = mlxsw_sp1_port_link_mode[i].speed;
2673 duplex = DUPLEX_FULL;
2678 cmd->base.speed = speed;
2679 cmd->base.duplex = duplex;
2683 mlxsw_sp1_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp,
2684 const struct ethtool_link_ksettings *cmd)
2689 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2690 if (test_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2691 cmd->link_modes.advertising))
2692 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2697 static u32 mlxsw_sp1_to_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 speed)
2702 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2703 if (speed == mlxsw_sp1_port_link_mode[i].speed)
2704 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2710 mlxsw_sp1_to_ptys_upper_speed(struct mlxsw_sp *mlxsw_sp, u32 upper_speed)
2715 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2716 if (mlxsw_sp1_port_link_mode[i].speed <= upper_speed)
2717 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2723 mlxsw_sp1_port_speed_base(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2726 *base_speed = MLXSW_SP_PORT_BASE_SPEED_25G;
2731 mlxsw_sp1_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
2732 u8 local_port, u32 proto_admin, bool autoneg)
2734 mlxsw_reg_ptys_eth_pack(payload, local_port, proto_admin, autoneg);
2738 mlxsw_sp1_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
2739 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
2740 u32 *p_eth_proto_oper)
2742 mlxsw_reg_ptys_eth_unpack(payload, p_eth_proto_cap, p_eth_proto_admin,
2746 static const struct mlxsw_sp_port_type_speed_ops
2747 mlxsw_sp1_port_type_speed_ops = {
2748 .from_ptys_supported_port = mlxsw_sp1_from_ptys_supported_port,
2749 .from_ptys_link = mlxsw_sp1_from_ptys_link,
2750 .from_ptys_speed_duplex = mlxsw_sp1_from_ptys_speed_duplex,
2751 .to_ptys_advert_link = mlxsw_sp1_to_ptys_advert_link,
2752 .to_ptys_speed = mlxsw_sp1_to_ptys_speed,
2753 .to_ptys_upper_speed = mlxsw_sp1_to_ptys_upper_speed,
2754 .port_speed_base = mlxsw_sp1_port_speed_base,
2755 .reg_ptys_eth_pack = mlxsw_sp1_reg_ptys_eth_pack,
2756 .reg_ptys_eth_unpack = mlxsw_sp1_reg_ptys_eth_unpack,
2759 static const enum ethtool_link_mode_bit_indices
2760 mlxsw_sp2_mask_ethtool_sgmii_100m[] = {
2761 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2764 #define MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN \
2765 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_sgmii_100m)
2767 static const enum ethtool_link_mode_bit_indices
2768 mlxsw_sp2_mask_ethtool_1000base_x_sgmii[] = {
2769 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2770 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2773 #define MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN \
2774 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_1000base_x_sgmii)
2776 static const enum ethtool_link_mode_bit_indices
2777 mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii[] = {
2778 ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
2781 #define MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN \
2782 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii)
2784 static const enum ethtool_link_mode_bit_indices
2785 mlxsw_sp2_mask_ethtool_5gbase_r[] = {
2786 ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
2789 #define MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN \
2790 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_5gbase_r)
2792 static const enum ethtool_link_mode_bit_indices
2793 mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g[] = {
2794 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2795 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2796 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
2797 ETHTOOL_LINK_MODE_10000baseCR_Full_BIT,
2798 ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
2799 ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
2800 ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
2803 #define MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN \
2804 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g)
2806 static const enum ethtool_link_mode_bit_indices
2807 mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g[] = {
2808 ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2809 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2810 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2811 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2814 #define MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN \
2815 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g)
2817 static const enum ethtool_link_mode_bit_indices
2818 mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr[] = {
2819 ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2820 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2821 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2824 #define MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN \
2825 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr)
2827 static const enum ethtool_link_mode_bit_indices
2828 mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2[] = {
2829 ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2830 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2831 ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2834 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN \
2835 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2)
2837 static const enum ethtool_link_mode_bit_indices
2838 mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr[] = {
2839 ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
2840 ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
2841 ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
2842 ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
2843 ETHTOOL_LINK_MODE_50000baseDR_Full_BIT,
2846 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN \
2847 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr)
2849 static const enum ethtool_link_mode_bit_indices
2850 mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4[] = {
2851 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2852 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2853 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2854 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2857 #define MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN \
2858 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4)
2860 static const enum ethtool_link_mode_bit_indices
2861 mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2[] = {
2862 ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT,
2863 ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT,
2864 ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT,
2865 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
2866 ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT,
2869 #define MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN \
2870 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2)
2872 static const enum ethtool_link_mode_bit_indices
2873 mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4[] = {
2874 ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT,
2875 ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT,
2876 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
2877 ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT,
2878 ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT,
2881 #define MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN \
2882 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4)
2884 struct mlxsw_sp2_port_link_mode {
2885 const enum ethtool_link_mode_bit_indices *mask_ethtool;
2891 static const struct mlxsw_sp2_port_link_mode mlxsw_sp2_port_link_mode[] = {
2893 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_SGMII_100M,
2894 .mask_ethtool = mlxsw_sp2_mask_ethtool_sgmii_100m,
2895 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN,
2899 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_1000BASE_X_SGMII,
2900 .mask_ethtool = mlxsw_sp2_mask_ethtool_1000base_x_sgmii,
2901 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN,
2902 .speed = SPEED_1000,
2905 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_2_5GBASE_X_2_5GMII,
2906 .mask_ethtool = mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii,
2907 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN,
2908 .speed = SPEED_2500,
2911 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_5GBASE_R,
2912 .mask_ethtool = mlxsw_sp2_mask_ethtool_5gbase_r,
2913 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN,
2914 .speed = SPEED_5000,
2917 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XFI_XAUI_1_10G,
2918 .mask_ethtool = mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g,
2919 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN,
2920 .speed = SPEED_10000,
2923 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XLAUI_4_XLPPI_4_40G,
2924 .mask_ethtool = mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g,
2925 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN,
2926 .speed = SPEED_40000,
2929 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_25GAUI_1_25GBASE_CR_KR,
2930 .mask_ethtool = mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr,
2931 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN,
2932 .speed = SPEED_25000,
2935 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_2_LAUI_2_50GBASE_CR2_KR2,
2936 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2,
2937 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN,
2938 .speed = SPEED_50000,
2941 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_1_LAUI_1_50GBASE_CR_KR,
2942 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr,
2943 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN,
2944 .speed = SPEED_50000,
2947 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_CAUI_4_100GBASE_CR4_KR4,
2948 .mask_ethtool = mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4,
2949 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN,
2950 .speed = SPEED_100000,
2953 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_100GAUI_2_100GBASE_CR2_KR2,
2954 .mask_ethtool = mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2,
2955 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN,
2956 .speed = SPEED_100000,
2959 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_200GAUI_4_200GBASE_CR4_KR4,
2960 .mask_ethtool = mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4,
2961 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN,
2962 .speed = SPEED_200000,
2966 #define MLXSW_SP2_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp2_port_link_mode)
2969 mlxsw_sp2_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
2971 struct ethtool_link_ksettings *cmd)
2973 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2974 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2978 mlxsw_sp2_set_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
2979 unsigned long *mode)
2983 for (i = 0; i < link_mode->m_ethtool_len; i++)
2984 __set_bit(link_mode->mask_ethtool[i], mode);
2988 mlxsw_sp2_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
2989 unsigned long *mode)
2993 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
2994 if (ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask)
2995 mlxsw_sp2_set_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3001 mlxsw_sp2_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
3003 struct ethtool_link_ksettings *cmd)
3005 u32 speed = SPEED_UNKNOWN;
3006 u8 duplex = DUPLEX_UNKNOWN;
3012 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3013 if (ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask) {
3014 speed = mlxsw_sp2_port_link_mode[i].speed;
3015 duplex = DUPLEX_FULL;
3020 cmd->base.speed = speed;
3021 cmd->base.duplex = duplex;
3025 mlxsw_sp2_test_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
3026 const unsigned long *mode)
3031 for (i = 0; i < link_mode->m_ethtool_len; i++) {
3032 if (test_bit(link_mode->mask_ethtool[i], mode))
3036 return cnt == link_mode->m_ethtool_len;
3040 mlxsw_sp2_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp,
3041 const struct ethtool_link_ksettings *cmd)
3046 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3047 if (mlxsw_sp2_test_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3048 cmd->link_modes.advertising))
3049 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3054 static u32 mlxsw_sp2_to_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 speed)
3059 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3060 if (speed == mlxsw_sp2_port_link_mode[i].speed)
3061 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3067 mlxsw_sp2_to_ptys_upper_speed(struct mlxsw_sp *mlxsw_sp, u32 upper_speed)
3072 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3073 if (mlxsw_sp2_port_link_mode[i].speed <= upper_speed)
3074 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3080 mlxsw_sp2_port_speed_base(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3083 char ptys_pl[MLXSW_REG_PTYS_LEN];
3087 /* In Spectrum-2, the speed of 1x can change from port to port, so query
3090 mlxsw_reg_ptys_ext_eth_pack(ptys_pl, local_port, 0, false);
3091 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3094 mlxsw_reg_ptys_ext_eth_unpack(ptys_pl, ð_proto_cap, NULL, NULL);
3097 MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_1_LAUI_1_50GBASE_CR_KR) {
3098 *base_speed = MLXSW_SP_PORT_BASE_SPEED_50G;
3103 MLXSW_REG_PTYS_EXT_ETH_SPEED_25GAUI_1_25GBASE_CR_KR) {
3104 *base_speed = MLXSW_SP_PORT_BASE_SPEED_25G;
3112 mlxsw_sp2_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
3113 u8 local_port, u32 proto_admin,
3116 mlxsw_reg_ptys_ext_eth_pack(payload, local_port, proto_admin, autoneg);
3120 mlxsw_sp2_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
3121 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
3122 u32 *p_eth_proto_oper)
3124 mlxsw_reg_ptys_ext_eth_unpack(payload, p_eth_proto_cap,
3125 p_eth_proto_admin, p_eth_proto_oper);
3128 static const struct mlxsw_sp_port_type_speed_ops
3129 mlxsw_sp2_port_type_speed_ops = {
3130 .from_ptys_supported_port = mlxsw_sp2_from_ptys_supported_port,
3131 .from_ptys_link = mlxsw_sp2_from_ptys_link,
3132 .from_ptys_speed_duplex = mlxsw_sp2_from_ptys_speed_duplex,
3133 .to_ptys_advert_link = mlxsw_sp2_to_ptys_advert_link,
3134 .to_ptys_speed = mlxsw_sp2_to_ptys_speed,
3135 .to_ptys_upper_speed = mlxsw_sp2_to_ptys_upper_speed,
3136 .port_speed_base = mlxsw_sp2_port_speed_base,
3137 .reg_ptys_eth_pack = mlxsw_sp2_reg_ptys_eth_pack,
3138 .reg_ptys_eth_unpack = mlxsw_sp2_reg_ptys_eth_unpack,
3142 mlxsw_sp_port_get_link_supported(struct mlxsw_sp *mlxsw_sp, u32 eth_proto_cap,
3143 struct ethtool_link_ksettings *cmd)
3145 const struct mlxsw_sp_port_type_speed_ops *ops;
3147 ops = mlxsw_sp->port_type_speed_ops;
3149 ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
3150 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
3151 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
3153 ops->from_ptys_supported_port(mlxsw_sp, eth_proto_cap, cmd);
3154 ops->from_ptys_link(mlxsw_sp, eth_proto_cap, cmd->link_modes.supported);
3158 mlxsw_sp_port_get_link_advertise(struct mlxsw_sp *mlxsw_sp,
3159 u32 eth_proto_admin, bool autoneg,
3160 struct ethtool_link_ksettings *cmd)
3162 const struct mlxsw_sp_port_type_speed_ops *ops;
3164 ops = mlxsw_sp->port_type_speed_ops;
3169 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
3170 ops->from_ptys_link(mlxsw_sp, eth_proto_admin,
3171 cmd->link_modes.advertising);
3175 mlxsw_sp_port_connector_port(enum mlxsw_reg_ptys_connector_type connector_type)
3177 switch (connector_type) {
3178 case MLXSW_REG_PTYS_CONNECTOR_TYPE_UNKNOWN_OR_NO_CONNECTOR:
3180 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_NONE:
3182 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_TP:
3184 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_AUI:
3186 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_BNC:
3188 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_MII:
3190 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_FIBRE:
3192 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_DA:
3194 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_OTHER:
3202 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
3203 struct ethtool_link_ksettings *cmd)
3205 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
3206 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3207 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3208 const struct mlxsw_sp_port_type_speed_ops *ops;
3209 char ptys_pl[MLXSW_REG_PTYS_LEN];
3214 ops = mlxsw_sp->port_type_speed_ops;
3216 autoneg = mlxsw_sp_port->link.autoneg;
3217 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3219 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3222 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
3223 ð_proto_admin, ð_proto_oper);
3225 mlxsw_sp_port_get_link_supported(mlxsw_sp, eth_proto_cap, cmd);
3227 mlxsw_sp_port_get_link_advertise(mlxsw_sp, eth_proto_admin, autoneg,
3230 cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
3231 connector_type = mlxsw_reg_ptys_connector_type_get(ptys_pl);
3232 cmd->base.port = mlxsw_sp_port_connector_port(connector_type);
3233 ops->from_ptys_speed_duplex(mlxsw_sp, netif_carrier_ok(dev),
3234 eth_proto_oper, cmd);
3240 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
3241 const struct ethtool_link_ksettings *cmd)
3243 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3244 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3245 const struct mlxsw_sp_port_type_speed_ops *ops;
3246 char ptys_pl[MLXSW_REG_PTYS_LEN];
3247 u32 eth_proto_cap, eth_proto_new;
3251 ops = mlxsw_sp->port_type_speed_ops;
3253 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3255 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3258 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap, NULL, NULL);
3260 autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
3261 if (!autoneg && cmd->base.speed == SPEED_56000) {
3262 netdev_err(dev, "56G not supported with autoneg off\n");
3265 eth_proto_new = autoneg ?
3266 ops->to_ptys_advert_link(mlxsw_sp, cmd) :
3267 ops->to_ptys_speed(mlxsw_sp, cmd->base.speed);
3269 eth_proto_new = eth_proto_new & eth_proto_cap;
3270 if (!eth_proto_new) {
3271 netdev_err(dev, "No supported speed requested\n");
3275 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3276 eth_proto_new, autoneg);
3277 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3281 mlxsw_sp_port->link.autoneg = autoneg;
3283 if (!netif_running(dev))
3286 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3287 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
3292 static int mlxsw_sp_get_module_info(struct net_device *netdev,
3293 struct ethtool_modinfo *modinfo)
3295 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3296 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3299 err = mlxsw_env_get_module_info(mlxsw_sp->core,
3300 mlxsw_sp_port->mapping.module,
3306 static int mlxsw_sp_get_module_eeprom(struct net_device *netdev,
3307 struct ethtool_eeprom *ee,
3310 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3311 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3314 err = mlxsw_env_get_module_eeprom(netdev, mlxsw_sp->core,
3315 mlxsw_sp_port->mapping.module, ee,
3322 mlxsw_sp_get_ts_info(struct net_device *netdev, struct ethtool_ts_info *info)
3324 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3325 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3327 return mlxsw_sp->ptp_ops->get_ts_info(mlxsw_sp, info);
3330 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
3331 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
3332 .get_link = ethtool_op_get_link,
3333 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
3334 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
3335 .get_strings = mlxsw_sp_port_get_strings,
3336 .set_phys_id = mlxsw_sp_port_set_phys_id,
3337 .get_ethtool_stats = mlxsw_sp_port_get_stats,
3338 .get_sset_count = mlxsw_sp_port_get_sset_count,
3339 .get_link_ksettings = mlxsw_sp_port_get_link_ksettings,
3340 .set_link_ksettings = mlxsw_sp_port_set_link_ksettings,
3341 .get_module_info = mlxsw_sp_get_module_info,
3342 .get_module_eeprom = mlxsw_sp_get_module_eeprom,
3343 .get_ts_info = mlxsw_sp_get_ts_info,
3347 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
3349 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3350 const struct mlxsw_sp_port_type_speed_ops *ops;
3351 char ptys_pl[MLXSW_REG_PTYS_LEN];
3352 u32 eth_proto_admin;
3357 ops = mlxsw_sp->port_type_speed_ops;
3359 err = ops->port_speed_base(mlxsw_sp, mlxsw_sp_port->local_port,
3363 upper_speed = base_speed * width;
3365 eth_proto_admin = ops->to_ptys_upper_speed(mlxsw_sp, upper_speed);
3366 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3367 eth_proto_admin, mlxsw_sp_port->link.autoneg);
3368 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3371 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
3372 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
3373 bool dwrr, u8 dwrr_weight)
3375 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3376 char qeec_pl[MLXSW_REG_QEEC_LEN];
3378 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3380 mlxsw_reg_qeec_de_set(qeec_pl, true);
3381 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
3382 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
3383 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3386 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
3387 enum mlxsw_reg_qeec_hr hr, u8 index,
3388 u8 next_index, u32 maxrate)
3390 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3391 char qeec_pl[MLXSW_REG_QEEC_LEN];
3393 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3395 mlxsw_reg_qeec_mase_set(qeec_pl, true);
3396 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
3397 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3400 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
3401 enum mlxsw_reg_qeec_hr hr, u8 index,
3402 u8 next_index, u32 minrate)
3404 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3405 char qeec_pl[MLXSW_REG_QEEC_LEN];
3407 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3409 mlxsw_reg_qeec_mise_set(qeec_pl, true);
3410 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
3412 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3415 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
3416 u8 switch_prio, u8 tclass)
3418 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3419 char qtct_pl[MLXSW_REG_QTCT_LEN];
3421 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
3423 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
3426 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
3430 /* Setup the elements hierarcy, so that each TC is linked to
3431 * one subgroup, which are all member in the same group.
3433 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3434 MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
3438 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3439 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3440 MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
3445 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3446 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3447 MLXSW_REG_QEEC_HIERARCY_TC, i, i,
3452 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3453 MLXSW_REG_QEEC_HIERARCY_TC,
3460 /* Make sure the max shaper is disabled in all hierarchies that support
3461 * it. Note that this disables ptps (PTP shaper), but that is intended
3462 * for the initial configuration.
3464 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3465 MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
3466 MLXSW_REG_QEEC_MAS_DIS);
3469 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3470 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3471 MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
3473 MLXSW_REG_QEEC_MAS_DIS);
3477 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3478 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3479 MLXSW_REG_QEEC_HIERARCY_TC,
3481 MLXSW_REG_QEEC_MAS_DIS);
3485 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3486 MLXSW_REG_QEEC_HIERARCY_TC,
3488 MLXSW_REG_QEEC_MAS_DIS);
3493 /* Configure the min shaper for multicast TCs. */
3494 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3495 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
3496 MLXSW_REG_QEEC_HIERARCY_TC,
3498 MLXSW_REG_QEEC_MIS_MIN);
3503 /* Map all priorities to traffic class 0. */
3504 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3505 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
3513 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
3516 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3517 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
3519 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
3520 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
3523 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3524 bool split, u8 module, u8 width, u8 lane)
3526 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
3527 struct mlxsw_sp_port *mlxsw_sp_port;
3528 struct net_device *dev;
3531 err = mlxsw_core_port_init(mlxsw_sp->core, local_port,
3532 module + 1, split, lane / width,
3534 sizeof(mlxsw_sp->base_mac));
3536 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
3541 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
3544 goto err_alloc_etherdev;
3546 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
3547 mlxsw_sp_port = netdev_priv(dev);
3548 mlxsw_sp_port->dev = dev;
3549 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
3550 mlxsw_sp_port->local_port = local_port;
3551 mlxsw_sp_port->pvid = MLXSW_SP_DEFAULT_VID;
3552 mlxsw_sp_port->split = split;
3553 mlxsw_sp_port->mapping.module = module;
3554 mlxsw_sp_port->mapping.width = width;
3555 mlxsw_sp_port->mapping.lane = lane;
3556 mlxsw_sp_port->link.autoneg = 1;
3557 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
3558 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
3560 mlxsw_sp_port->pcpu_stats =
3561 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
3562 if (!mlxsw_sp_port->pcpu_stats) {
3564 goto err_alloc_stats;
3567 mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
3569 if (!mlxsw_sp_port->sample) {
3571 goto err_alloc_sample;
3574 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
3575 &update_stats_cache);
3577 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
3578 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
3580 err = mlxsw_sp_port_module_map(mlxsw_sp_port, module, width, lane);
3582 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
3583 mlxsw_sp_port->local_port);
3584 goto err_port_module_map;
3587 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
3589 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
3590 mlxsw_sp_port->local_port);
3591 goto err_port_swid_set;
3594 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
3596 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
3597 mlxsw_sp_port->local_port);
3598 goto err_dev_addr_init;
3601 netif_carrier_off(dev);
3603 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
3604 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
3605 dev->hw_features |= NETIF_F_HW_TC | NETIF_F_LOOPBACK;
3608 dev->max_mtu = ETH_MAX_MTU;
3610 /* Each packet needs to have a Tx header (metadata) on top all other
3613 dev->needed_headroom = MLXSW_TXHDR_LEN;
3615 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
3617 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
3618 mlxsw_sp_port->local_port);
3619 goto err_port_system_port_mapping_set;
3622 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
3624 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
3625 mlxsw_sp_port->local_port);
3626 goto err_port_speed_by_width_set;
3629 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
3631 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
3632 mlxsw_sp_port->local_port);
3633 goto err_port_mtu_set;
3636 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3638 goto err_port_admin_status_set;
3640 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
3642 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
3643 mlxsw_sp_port->local_port);
3644 goto err_port_buffers_init;
3647 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
3649 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
3650 mlxsw_sp_port->local_port);
3651 goto err_port_ets_init;
3654 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
3656 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
3657 mlxsw_sp_port->local_port);
3658 goto err_port_tc_mc_mode;
3661 /* ETS and buffers must be initialized before DCB. */
3662 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
3664 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
3665 mlxsw_sp_port->local_port);
3666 goto err_port_dcb_init;
3669 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
3671 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
3672 mlxsw_sp_port->local_port);
3673 goto err_port_fids_init;
3676 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
3678 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
3679 mlxsw_sp_port->local_port);
3680 goto err_port_qdiscs_init;
3683 err = mlxsw_sp_port_nve_init(mlxsw_sp_port);
3685 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize NVE\n",
3686 mlxsw_sp_port->local_port);
3687 goto err_port_nve_init;
3690 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
3692 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set PVID\n",
3693 mlxsw_sp_port->local_port);
3694 goto err_port_pvid_set;
3697 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
3698 MLXSW_SP_DEFAULT_VID);
3699 if (IS_ERR(mlxsw_sp_port_vlan)) {
3700 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
3701 mlxsw_sp_port->local_port);
3702 err = PTR_ERR(mlxsw_sp_port_vlan);
3703 goto err_port_vlan_create;
3705 mlxsw_sp_port->default_vlan = mlxsw_sp_port_vlan;
3707 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
3708 err = register_netdev(dev);
3710 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
3711 mlxsw_sp_port->local_port);
3712 goto err_register_netdev;
3715 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
3716 mlxsw_sp_port, dev);
3717 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
3720 err_register_netdev:
3721 mlxsw_sp->ports[local_port] = NULL;
3722 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
3723 err_port_vlan_create:
3725 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3727 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3728 err_port_qdiscs_init:
3729 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3731 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3733 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3734 err_port_tc_mc_mode:
3736 err_port_buffers_init:
3737 err_port_admin_status_set:
3739 err_port_speed_by_width_set:
3740 err_port_system_port_mapping_set:
3742 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3744 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3745 err_port_module_map:
3746 kfree(mlxsw_sp_port->sample);
3748 free_percpu(mlxsw_sp_port->pcpu_stats);
3752 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3756 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3758 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3760 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
3761 mlxsw_sp_port_ptp_clear(mlxsw_sp_port);
3762 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
3763 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
3764 mlxsw_sp->ports[local_port] = NULL;
3765 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, true);
3766 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3767 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3768 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3769 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3770 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3771 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3772 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3773 kfree(mlxsw_sp_port->sample);
3774 free_percpu(mlxsw_sp_port->pcpu_stats);
3775 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
3776 free_netdev(mlxsw_sp_port->dev);
3777 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3780 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3782 return mlxsw_sp->ports[local_port] != NULL;
3785 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
3789 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
3790 if (mlxsw_sp_port_created(mlxsw_sp, i))
3791 mlxsw_sp_port_remove(mlxsw_sp, i);
3792 kfree(mlxsw_sp->port_to_module);
3793 kfree(mlxsw_sp->ports);
3796 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
3798 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
3799 u8 module, width, lane;
3804 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
3805 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
3806 if (!mlxsw_sp->ports)
3809 mlxsw_sp->port_to_module = kmalloc_array(max_ports, sizeof(int),
3811 if (!mlxsw_sp->port_to_module) {
3813 goto err_port_to_module_alloc;
3816 for (i = 1; i < max_ports; i++) {
3817 /* Mark as invalid */
3818 mlxsw_sp->port_to_module[i] = -1;
3820 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
3823 goto err_port_module_info_get;
3826 mlxsw_sp->port_to_module[i] = module;
3827 err = mlxsw_sp_port_create(mlxsw_sp, i, false,
3828 module, width, lane);
3830 goto err_port_create;
3835 err_port_module_info_get:
3836 for (i--; i >= 1; i--)
3837 if (mlxsw_sp_port_created(mlxsw_sp, i))
3838 mlxsw_sp_port_remove(mlxsw_sp, i);
3839 kfree(mlxsw_sp->port_to_module);
3840 err_port_to_module_alloc:
3841 kfree(mlxsw_sp->ports);
3845 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
3847 u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
3849 return local_port - offset;
3852 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
3853 u8 module, unsigned int count, u8 offset)
3855 u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
3858 for (i = 0; i < count; i++) {
3859 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i * offset,
3860 true, module, width, i * width);
3862 goto err_port_create;
3868 for (i--; i >= 0; i--)
3869 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
3870 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
3874 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
3875 u8 base_port, unsigned int count)
3877 u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
3880 /* Split by four means we need to re-create two ports, otherwise
3885 for (i = 0; i < count; i++) {
3886 local_port = base_port + i * 2;
3887 if (mlxsw_sp->port_to_module[local_port] < 0)
3889 module = mlxsw_sp->port_to_module[local_port];
3891 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
3896 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
3898 struct netlink_ext_ack *extack)
3900 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3901 u8 local_ports_in_1x, local_ports_in_2x, offset;
3902 struct mlxsw_sp_port *mlxsw_sp_port;
3903 u8 module, cur_width, base_port;
3907 if (!MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_1X) ||
3908 !MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_2X))
3911 local_ports_in_1x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_1X);
3912 local_ports_in_2x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_2X);
3914 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3915 if (!mlxsw_sp_port) {
3916 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3918 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
3922 module = mlxsw_sp_port->mapping.module;
3923 cur_width = mlxsw_sp_port->mapping.width;
3925 if (count != 2 && count != 4) {
3926 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
3927 NL_SET_ERR_MSG_MOD(extack, "Port can only be split into 2 or 4 ports");
3931 if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
3932 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
3933 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split further");
3937 /* Make sure we have enough slave (even) ports for the split. */
3939 offset = local_ports_in_2x;
3940 base_port = local_port;
3941 if (mlxsw_sp->ports[base_port + local_ports_in_2x]) {
3942 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3943 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
3947 offset = local_ports_in_1x;
3948 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3949 if (mlxsw_sp->ports[base_port + 1] ||
3950 mlxsw_sp->ports[base_port + 3]) {
3951 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3952 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
3957 for (i = 0; i < count; i++)
3958 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
3959 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
3961 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count,
3964 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
3965 goto err_port_split_create;
3970 err_port_split_create:
3971 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
3975 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port,
3976 struct netlink_ext_ack *extack)
3978 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3979 u8 local_ports_in_1x, local_ports_in_2x, offset;
3980 struct mlxsw_sp_port *mlxsw_sp_port;
3981 u8 cur_width, base_port;
3985 if (!MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_1X) ||
3986 !MLXSW_CORE_RES_VALID(mlxsw_core, LOCAL_PORTS_IN_2X))
3989 local_ports_in_1x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_1X);
3990 local_ports_in_2x = MLXSW_CORE_RES_GET(mlxsw_core, LOCAL_PORTS_IN_2X);
3992 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3993 if (!mlxsw_sp_port) {
3994 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3996 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
4000 if (!mlxsw_sp_port->split) {
4001 netdev_err(mlxsw_sp_port->dev, "Port was not split\n");
4002 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
4006 cur_width = mlxsw_sp_port->mapping.width;
4007 count = cur_width == 1 ? 4 : 2;
4010 offset = local_ports_in_2x;
4012 offset = local_ports_in_1x;
4014 base_port = mlxsw_sp_cluster_base_port_get(local_port);
4016 /* Determine which ports to remove. */
4017 if (count == 2 && local_port >= base_port + 2)
4018 base_port = base_port + 2;
4020 for (i = 0; i < count; i++)
4021 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4022 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4024 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
4029 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
4030 char *pude_pl, void *priv)
4032 struct mlxsw_sp *mlxsw_sp = priv;
4033 struct mlxsw_sp_port *mlxsw_sp_port;
4034 enum mlxsw_reg_pude_oper_status status;
4037 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
4038 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4042 status = mlxsw_reg_pude_oper_status_get(pude_pl);
4043 if (status == MLXSW_PORT_OPER_STATUS_UP) {
4044 netdev_info(mlxsw_sp_port->dev, "link up\n");
4045 netif_carrier_on(mlxsw_sp_port->dev);
4047 netdev_info(mlxsw_sp_port->dev, "link down\n");
4048 netif_carrier_off(mlxsw_sp_port->dev);
4052 static void mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp *mlxsw_sp,
4053 char *mtpptr_pl, bool ingress)
4059 local_port = mlxsw_reg_mtpptr_local_port_get(mtpptr_pl);
4060 num_rec = mlxsw_reg_mtpptr_num_rec_get(mtpptr_pl);
4061 for (i = 0; i < num_rec; i++) {
4067 mlxsw_reg_mtpptr_unpack(mtpptr_pl, i, &message_type,
4068 &domain_number, &sequence_id,
4070 mlxsw_sp1_ptp_got_timestamp(mlxsw_sp, ingress, local_port,
4071 message_type, domain_number,
4072 sequence_id, timestamp);
4076 static void mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info *reg,
4077 char *mtpptr_pl, void *priv)
4079 struct mlxsw_sp *mlxsw_sp = priv;
4081 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, true);
4084 static void mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info *reg,
4085 char *mtpptr_pl, void *priv)
4087 struct mlxsw_sp *mlxsw_sp = priv;
4089 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, false);
4092 void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
4093 u8 local_port, void *priv)
4095 struct mlxsw_sp *mlxsw_sp = priv;
4096 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4097 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
4099 if (unlikely(!mlxsw_sp_port)) {
4100 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
4105 skb->dev = mlxsw_sp_port->dev;
4107 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
4108 u64_stats_update_begin(&pcpu_stats->syncp);
4109 pcpu_stats->rx_packets++;
4110 pcpu_stats->rx_bytes += skb->len;
4111 u64_stats_update_end(&pcpu_stats->syncp);
4113 skb->protocol = eth_type_trans(skb, skb->dev);
4114 netif_receive_skb(skb);
4117 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
4120 skb->offload_fwd_mark = 1;
4121 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4124 static void mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff *skb,
4125 u8 local_port, void *priv)
4127 skb->offload_l3_fwd_mark = 1;
4128 skb->offload_fwd_mark = 1;
4129 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4132 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
4135 struct mlxsw_sp *mlxsw_sp = priv;
4136 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4137 struct psample_group *psample_group;
4140 if (unlikely(!mlxsw_sp_port)) {
4141 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
4145 if (unlikely(!mlxsw_sp_port->sample)) {
4146 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
4151 size = mlxsw_sp_port->sample->truncate ?
4152 mlxsw_sp_port->sample->trunc_size : skb->len;
4155 psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
4158 psample_sample_packet(psample_group, skb, size,
4159 mlxsw_sp_port->dev->ifindex, 0,
4160 mlxsw_sp_port->sample->rate);
4167 static void mlxsw_sp_rx_listener_ptp(struct sk_buff *skb, u8 local_port,
4170 struct mlxsw_sp *mlxsw_sp = priv;
4172 mlxsw_sp->ptp_ops->receive(mlxsw_sp, skb, local_port);
4175 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4176 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
4177 _is_ctrl, SP_##_trap_group, DISCARD)
4179 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4180 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
4181 _is_ctrl, SP_##_trap_group, DISCARD)
4183 #define MLXSW_SP_RXL_L3_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4184 MLXSW_RXL(mlxsw_sp_rx_listener_l3_mark_func, _trap_id, _action, \
4185 _is_ctrl, SP_##_trap_group, DISCARD)
4187 #define MLXSW_SP_EVENTL(_func, _trap_id) \
4188 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
4190 static const struct mlxsw_listener mlxsw_sp_listener[] = {
4192 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
4194 MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
4195 MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
4196 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, LLDP, TRAP_TO_CPU,
4197 false, SP_LLDP, DISCARD),
4198 MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
4199 MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
4200 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
4201 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
4202 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
4203 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
4204 MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
4205 MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
4206 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, IP2ME, false),
4207 MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY, MIRROR_TO_CPU, IPV6_MLD,
4209 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4211 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE, TRAP_TO_CPU, IPV6_MLD,
4213 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4216 MLXSW_SP_RXL_MARK(MTUERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4217 MLXSW_SP_RXL_MARK(TTLERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4218 MLXSW_SP_RXL_L3_MARK(LBERROR, MIRROR_TO_CPU, LBERROR, false),
4219 MLXSW_SP_RXL_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
4220 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
4222 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP, false),
4223 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
4224 MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, TRAP_TO_CPU, ROUTER_EXP, false),
4225 MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, TRAP_TO_CPU, ROUTER_EXP,
4227 MLXSW_SP_RXL_MARK(IPV4_OSPF, TRAP_TO_CPU, OSPF, false),
4228 MLXSW_SP_RXL_MARK(IPV6_OSPF, TRAP_TO_CPU, OSPF, false),
4229 MLXSW_SP_RXL_MARK(IPV6_DHCP, TRAP_TO_CPU, DHCP, false),
4230 MLXSW_SP_RXL_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
4231 MLXSW_SP_RXL_MARK(IPV4_BGP, TRAP_TO_CPU, BGP, false),
4232 MLXSW_SP_RXL_MARK(IPV6_BGP, TRAP_TO_CPU, BGP, false),
4233 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4235 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4237 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4239 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4241 MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, TRAP_TO_CPU, IPV6_ND, false),
4242 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
4244 MLXSW_SP_RXL_MARK(HOST_MISS_IPV4, TRAP_TO_CPU, HOST_MISS, false),
4245 MLXSW_SP_RXL_MARK(HOST_MISS_IPV6, TRAP_TO_CPU, HOST_MISS, false),
4246 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, TRAP_TO_CPU, ROUTER_EXP, false),
4247 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, TRAP_TO_CPU, ROUTER_EXP, false),
4248 MLXSW_SP_RXL_MARK(IPIP_DECAP_ERROR, TRAP_TO_CPU, ROUTER_EXP, false),
4249 MLXSW_SP_RXL_MARK(DECAP_ECN0, TRAP_TO_CPU, ROUTER_EXP, false),
4250 MLXSW_SP_RXL_MARK(IPV4_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
4251 MLXSW_SP_RXL_MARK(IPV6_VRRP, TRAP_TO_CPU, ROUTER_EXP, false),
4252 /* PKT Sample trap */
4253 MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
4254 false, SP_IP2ME, DISCARD),
4256 MLXSW_SP_RXL_NO_MARK(ACL0, TRAP_TO_CPU, IP2ME, false),
4257 /* Multicast Router Traps */
4258 MLXSW_SP_RXL_MARK(IPV4_PIM, TRAP_TO_CPU, PIM, false),
4259 MLXSW_SP_RXL_MARK(IPV6_PIM, TRAP_TO_CPU, PIM, false),
4260 MLXSW_SP_RXL_MARK(RPF, TRAP_TO_CPU, RPF, false),
4261 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
4262 MLXSW_SP_RXL_L3_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
4264 MLXSW_SP_RXL_MARK(NVE_ENCAP_ARP, TRAP_TO_CPU, ARP, false),
4265 MLXSW_SP_RXL_NO_MARK(NVE_DECAP_ARP, TRAP_TO_CPU, ARP, false),
4267 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, PTP0, TRAP_TO_CPU,
4268 false, SP_PTP0, DISCARD),
4269 MLXSW_SP_RXL_NO_MARK(PTP1, TRAP_TO_CPU, PTP1, false),
4272 static const struct mlxsw_listener mlxsw_sp1_listener[] = {
4274 MLXSW_EVENTL(mlxsw_sp1_ptp_egr_fifo_event_func, PTP_EGR_FIFO, SP_PTP0),
4275 MLXSW_EVENTL(mlxsw_sp1_ptp_ing_fifo_event_func, PTP_ING_FIFO, SP_PTP0),
4278 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
4280 char qpcr_pl[MLXSW_REG_QPCR_LEN];
4281 enum mlxsw_reg_qpcr_ir_units ir_units;
4282 int max_cpu_policers;
4288 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
4291 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4293 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
4294 for (i = 0; i < max_cpu_policers; i++) {
4297 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4298 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4299 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4300 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4301 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4302 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4303 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4307 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4308 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4312 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4313 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4314 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4315 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4316 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4317 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4318 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4319 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4323 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4327 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4331 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4339 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
4341 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
4349 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
4351 char htgt_pl[MLXSW_REG_HTGT_LEN];
4352 enum mlxsw_reg_htgt_trap_group i;
4353 int max_cpu_policers;
4354 int max_trap_groups;
4359 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
4362 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
4363 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4365 for (i = 0; i < max_trap_groups; i++) {
4368 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4369 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4370 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4371 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4372 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4373 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4377 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4378 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4382 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4383 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4384 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4388 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4389 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4390 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4391 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4395 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4396 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4397 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4398 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4399 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4403 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
4404 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
4405 tc = MLXSW_REG_HTGT_DEFAULT_TC;
4406 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
4412 if (max_cpu_policers <= policer_id &&
4413 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
4416 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
4417 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4425 static int mlxsw_sp_traps_register(struct mlxsw_sp *mlxsw_sp,
4426 const struct mlxsw_listener listeners[],
4427 size_t listeners_count)
4432 for (i = 0; i < listeners_count; i++) {
4433 err = mlxsw_core_trap_register(mlxsw_sp->core,
4437 goto err_listener_register;
4442 err_listener_register:
4443 for (i--; i >= 0; i--) {
4444 mlxsw_core_trap_unregister(mlxsw_sp->core,
4451 static void mlxsw_sp_traps_unregister(struct mlxsw_sp *mlxsw_sp,
4452 const struct mlxsw_listener listeners[],
4453 size_t listeners_count)
4457 for (i = 0; i < listeners_count; i++) {
4458 mlxsw_core_trap_unregister(mlxsw_sp->core,
4464 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
4468 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
4472 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
4476 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp_listener,
4477 ARRAY_SIZE(mlxsw_sp_listener));
4481 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp->listeners,
4482 mlxsw_sp->listeners_count);
4484 goto err_extra_traps_init;
4488 err_extra_traps_init:
4489 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4490 ARRAY_SIZE(mlxsw_sp_listener));
4494 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
4496 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp->listeners,
4497 mlxsw_sp->listeners_count);
4498 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4499 ARRAY_SIZE(mlxsw_sp_listener));
4502 #define MLXSW_SP_LAG_SEED_INIT 0xcafecafe
4504 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
4506 char slcr_pl[MLXSW_REG_SLCR_LEN];
4510 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac),
4511 MLXSW_SP_LAG_SEED_INIT);
4512 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
4513 MLXSW_REG_SLCR_LAG_HASH_DMAC |
4514 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
4515 MLXSW_REG_SLCR_LAG_HASH_VLANID |
4516 MLXSW_REG_SLCR_LAG_HASH_SIP |
4517 MLXSW_REG_SLCR_LAG_HASH_DIP |
4518 MLXSW_REG_SLCR_LAG_HASH_SPORT |
4519 MLXSW_REG_SLCR_LAG_HASH_DPORT |
4520 MLXSW_REG_SLCR_LAG_HASH_IPPROTO, seed);
4521 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
4525 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
4526 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
4529 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
4530 sizeof(struct mlxsw_sp_upper),
4532 if (!mlxsw_sp->lags)
4538 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
4540 kfree(mlxsw_sp->lags);
4543 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
4545 char htgt_pl[MLXSW_REG_HTGT_LEN];
4547 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
4548 MLXSW_REG_HTGT_INVALID_POLICER,
4549 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
4550 MLXSW_REG_HTGT_DEFAULT_TC);
4551 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4554 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
4555 .clock_init = mlxsw_sp1_ptp_clock_init,
4556 .clock_fini = mlxsw_sp1_ptp_clock_fini,
4557 .init = mlxsw_sp1_ptp_init,
4558 .fini = mlxsw_sp1_ptp_fini,
4559 .receive = mlxsw_sp1_ptp_receive,
4560 .transmitted = mlxsw_sp1_ptp_transmitted,
4561 .hwtstamp_get = mlxsw_sp1_ptp_hwtstamp_get,
4562 .hwtstamp_set = mlxsw_sp1_ptp_hwtstamp_set,
4563 .get_ts_info = mlxsw_sp1_ptp_get_ts_info,
4566 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
4567 .clock_init = mlxsw_sp2_ptp_clock_init,
4568 .clock_fini = mlxsw_sp2_ptp_clock_fini,
4569 .init = mlxsw_sp2_ptp_init,
4570 .fini = mlxsw_sp2_ptp_fini,
4571 .receive = mlxsw_sp2_ptp_receive,
4572 .transmitted = mlxsw_sp2_ptp_transmitted,
4573 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
4574 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
4575 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
4578 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
4579 unsigned long event, void *ptr);
4581 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
4582 const struct mlxsw_bus_info *mlxsw_bus_info)
4584 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4587 mlxsw_sp->core = mlxsw_core;
4588 mlxsw_sp->bus_info = mlxsw_bus_info;
4590 err = mlxsw_sp_fw_rev_validate(mlxsw_sp);
4594 err = mlxsw_sp_base_mac_get(mlxsw_sp);
4596 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
4600 err = mlxsw_sp_kvdl_init(mlxsw_sp);
4602 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
4606 err = mlxsw_sp_fids_init(mlxsw_sp);
4608 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
4612 err = mlxsw_sp_traps_init(mlxsw_sp);
4614 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
4615 goto err_traps_init;
4618 err = mlxsw_sp_buffers_init(mlxsw_sp);
4620 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
4621 goto err_buffers_init;
4624 err = mlxsw_sp_lag_init(mlxsw_sp);
4626 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
4630 /* Initialize SPAN before router and switchdev, so that those components
4631 * can call mlxsw_sp_span_respin().
4633 err = mlxsw_sp_span_init(mlxsw_sp);
4635 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
4639 err = mlxsw_sp_switchdev_init(mlxsw_sp);
4641 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
4642 goto err_switchdev_init;
4645 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
4647 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
4648 goto err_counter_pool_init;
4651 err = mlxsw_sp_afa_init(mlxsw_sp);
4653 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
4657 err = mlxsw_sp_nve_init(mlxsw_sp);
4659 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize NVE\n");
4663 err = mlxsw_sp_acl_init(mlxsw_sp);
4665 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
4669 err = mlxsw_sp_router_init(mlxsw_sp);
4671 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
4672 goto err_router_init;
4675 if (mlxsw_sp->bus_info->read_frc_capable) {
4676 /* NULL is a valid return value from clock_init */
4678 mlxsw_sp->ptp_ops->clock_init(mlxsw_sp,
4679 mlxsw_sp->bus_info->dev);
4680 if (IS_ERR(mlxsw_sp->clock)) {
4681 err = PTR_ERR(mlxsw_sp->clock);
4682 dev_err(mlxsw_sp->bus_info->dev, "Failed to init ptp clock\n");
4683 goto err_ptp_clock_init;
4687 if (mlxsw_sp->clock) {
4688 /* NULL is a valid return value from ptp_ops->init */
4689 mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
4690 if (IS_ERR(mlxsw_sp->ptp_state)) {
4691 err = PTR_ERR(mlxsw_sp->ptp_state);
4692 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
4697 /* Initialize netdevice notifier after router and SPAN is initialized,
4698 * so that the event handler can use router structures and call SPAN
4701 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
4702 err = register_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4704 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
4705 goto err_netdev_notifier;
4708 err = mlxsw_sp_dpipe_init(mlxsw_sp);
4710 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
4711 goto err_dpipe_init;
4714 err = mlxsw_sp_ports_create(mlxsw_sp);
4716 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
4717 goto err_ports_create;
4723 mlxsw_sp_dpipe_fini(mlxsw_sp);
4725 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4726 err_netdev_notifier:
4727 if (mlxsw_sp->clock)
4728 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
4730 if (mlxsw_sp->clock)
4731 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
4733 mlxsw_sp_router_fini(mlxsw_sp);
4735 mlxsw_sp_acl_fini(mlxsw_sp);
4737 mlxsw_sp_nve_fini(mlxsw_sp);
4739 mlxsw_sp_afa_fini(mlxsw_sp);
4741 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4742 err_counter_pool_init:
4743 mlxsw_sp_switchdev_fini(mlxsw_sp);
4745 mlxsw_sp_span_fini(mlxsw_sp);
4747 mlxsw_sp_lag_fini(mlxsw_sp);
4749 mlxsw_sp_buffers_fini(mlxsw_sp);
4751 mlxsw_sp_traps_fini(mlxsw_sp);
4753 mlxsw_sp_fids_fini(mlxsw_sp);
4755 mlxsw_sp_kvdl_fini(mlxsw_sp);
4759 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
4760 const struct mlxsw_bus_info *mlxsw_bus_info)
4762 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4764 mlxsw_sp->req_rev = &mlxsw_sp1_fw_rev;
4765 mlxsw_sp->fw_filename = MLXSW_SP1_FW_FILENAME;
4766 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
4767 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
4768 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
4769 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
4770 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
4771 mlxsw_sp->nve_ops_arr = mlxsw_sp1_nve_ops_arr;
4772 mlxsw_sp->mac_mask = mlxsw_sp1_mac_mask;
4773 mlxsw_sp->rif_ops_arr = mlxsw_sp1_rif_ops_arr;
4774 mlxsw_sp->sb_vals = &mlxsw_sp1_sb_vals;
4775 mlxsw_sp->port_type_speed_ops = &mlxsw_sp1_port_type_speed_ops;
4776 mlxsw_sp->ptp_ops = &mlxsw_sp1_ptp_ops;
4777 mlxsw_sp->listeners = mlxsw_sp1_listener;
4778 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp1_listener);
4780 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
4783 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
4784 const struct mlxsw_bus_info *mlxsw_bus_info)
4786 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4788 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
4789 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
4790 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
4791 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
4792 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
4793 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
4794 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
4795 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
4796 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
4797 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
4798 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
4800 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info);
4803 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
4805 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4807 mlxsw_sp_ports_remove(mlxsw_sp);
4808 mlxsw_sp_dpipe_fini(mlxsw_sp);
4809 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4810 if (mlxsw_sp->clock) {
4811 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
4812 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
4814 mlxsw_sp_router_fini(mlxsw_sp);
4815 mlxsw_sp_acl_fini(mlxsw_sp);
4816 mlxsw_sp_nve_fini(mlxsw_sp);
4817 mlxsw_sp_afa_fini(mlxsw_sp);
4818 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4819 mlxsw_sp_switchdev_fini(mlxsw_sp);
4820 mlxsw_sp_span_fini(mlxsw_sp);
4821 mlxsw_sp_lag_fini(mlxsw_sp);
4822 mlxsw_sp_buffers_fini(mlxsw_sp);
4823 mlxsw_sp_traps_fini(mlxsw_sp);
4824 mlxsw_sp_fids_fini(mlxsw_sp);
4825 mlxsw_sp_kvdl_fini(mlxsw_sp);
4828 /* Per-FID flood tables are used for both "true" 802.1D FIDs and emulated
4831 #define MLXSW_SP_FID_FLOOD_TABLE_SIZE (MLXSW_SP_FID_8021D_MAX + \
4834 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
4836 .max_mid = MLXSW_SP_MID_MAX,
4837 .used_flood_tables = 1,
4838 .used_flood_mode = 1,
4840 .max_fid_flood_tables = 3,
4841 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
4842 .used_max_ib_mc = 1,
4846 .used_kvd_sizes = 1,
4847 .kvd_hash_single_parts = 59,
4848 .kvd_hash_double_parts = 41,
4849 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
4853 .type = MLXSW_PORT_SWID_TYPE_ETH,
4858 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
4860 .max_mid = MLXSW_SP_MID_MAX,
4861 .used_flood_tables = 1,
4862 .used_flood_mode = 1,
4864 .max_fid_flood_tables = 3,
4865 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
4866 .used_max_ib_mc = 1,
4873 .type = MLXSW_PORT_SWID_TYPE_ETH,
4879 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
4880 struct devlink_resource_size_params *kvd_size_params,
4881 struct devlink_resource_size_params *linear_size_params,
4882 struct devlink_resource_size_params *hash_double_size_params,
4883 struct devlink_resource_size_params *hash_single_size_params)
4885 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4886 KVD_SINGLE_MIN_SIZE);
4887 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4888 KVD_DOUBLE_MIN_SIZE);
4889 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4890 u32 linear_size_min = 0;
4892 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
4893 MLXSW_SP_KVD_GRANULARITY,
4894 DEVLINK_RESOURCE_UNIT_ENTRY);
4895 devlink_resource_size_params_init(linear_size_params, linear_size_min,
4896 kvd_size - single_size_min -
4898 MLXSW_SP_KVD_GRANULARITY,
4899 DEVLINK_RESOURCE_UNIT_ENTRY);
4900 devlink_resource_size_params_init(hash_double_size_params,
4902 kvd_size - single_size_min -
4904 MLXSW_SP_KVD_GRANULARITY,
4905 DEVLINK_RESOURCE_UNIT_ENTRY);
4906 devlink_resource_size_params_init(hash_single_size_params,
4908 kvd_size - double_size_min -
4910 MLXSW_SP_KVD_GRANULARITY,
4911 DEVLINK_RESOURCE_UNIT_ENTRY);
4914 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
4916 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4917 struct devlink_resource_size_params hash_single_size_params;
4918 struct devlink_resource_size_params hash_double_size_params;
4919 struct devlink_resource_size_params linear_size_params;
4920 struct devlink_resource_size_params kvd_size_params;
4921 u32 kvd_size, single_size, double_size, linear_size;
4922 const struct mlxsw_config_profile *profile;
4925 profile = &mlxsw_sp1_config_profile;
4926 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
4929 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
4930 &linear_size_params,
4931 &hash_double_size_params,
4932 &hash_single_size_params);
4934 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4935 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
4936 kvd_size, MLXSW_SP_RESOURCE_KVD,
4937 DEVLINK_RESOURCE_ID_PARENT_TOP,
4942 linear_size = profile->kvd_linear_size;
4943 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
4945 MLXSW_SP_RESOURCE_KVD_LINEAR,
4946 MLXSW_SP_RESOURCE_KVD,
4947 &linear_size_params);
4951 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
4955 double_size = kvd_size - linear_size;
4956 double_size *= profile->kvd_hash_double_parts;
4957 double_size /= profile->kvd_hash_double_parts +
4958 profile->kvd_hash_single_parts;
4959 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
4960 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
4962 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
4963 MLXSW_SP_RESOURCE_KVD,
4964 &hash_double_size_params);
4968 single_size = kvd_size - double_size - linear_size;
4969 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
4971 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
4972 MLXSW_SP_RESOURCE_KVD,
4973 &hash_single_size_params);
4980 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
4982 return mlxsw_sp1_resources_kvd_register(mlxsw_core);
4985 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
4990 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
4991 const struct mlxsw_config_profile *profile,
4992 u64 *p_single_size, u64 *p_double_size,
4995 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4999 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5000 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
5003 /* The hash part is what left of the kvd without the
5004 * linear part. It is split to the single size and
5005 * double size by the parts ratio from the profile.
5006 * Both sizes must be a multiplications of the
5007 * granularity from the profile. In case the user
5008 * provided the sizes they are obtained via devlink.
5010 err = devlink_resource_size_get(devlink,
5011 MLXSW_SP_RESOURCE_KVD_LINEAR,
5014 *p_linear_size = profile->kvd_linear_size;
5016 err = devlink_resource_size_get(devlink,
5017 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
5020 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5022 double_size *= profile->kvd_hash_double_parts;
5023 double_size /= profile->kvd_hash_double_parts +
5024 profile->kvd_hash_single_parts;
5025 *p_double_size = rounddown(double_size,
5026 MLXSW_SP_KVD_GRANULARITY);
5029 err = devlink_resource_size_get(devlink,
5030 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
5033 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5034 *p_double_size - *p_linear_size;
5036 /* Check results are legal. */
5037 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5038 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
5039 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
5046 mlxsw_sp_devlink_param_fw_load_policy_validate(struct devlink *devlink, u32 id,
5047 union devlink_param_value val,
5048 struct netlink_ext_ack *extack)
5050 if ((val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER) &&
5051 (val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)) {
5052 NL_SET_ERR_MSG_MOD(extack, "'fw_load_policy' must be 'driver' or 'flash'");
5059 static const struct devlink_param mlxsw_sp_devlink_params[] = {
5060 DEVLINK_PARAM_GENERIC(FW_LOAD_POLICY,
5061 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
5063 mlxsw_sp_devlink_param_fw_load_policy_validate),
5066 static int mlxsw_sp_params_register(struct mlxsw_core *mlxsw_core)
5068 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5069 union devlink_param_value value;
5072 err = devlink_params_register(devlink, mlxsw_sp_devlink_params,
5073 ARRAY_SIZE(mlxsw_sp_devlink_params));
5077 value.vu8 = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER;
5078 devlink_param_driverinit_value_set(devlink,
5079 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5084 static void mlxsw_sp_params_unregister(struct mlxsw_core *mlxsw_core)
5086 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5087 mlxsw_sp_devlink_params,
5088 ARRAY_SIZE(mlxsw_sp_devlink_params));
5092 mlxsw_sp_params_acl_region_rehash_intrvl_get(struct devlink *devlink, u32 id,
5093 struct devlink_param_gset_ctx *ctx)
5095 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5096 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5098 ctx->val.vu32 = mlxsw_sp_acl_region_rehash_intrvl_get(mlxsw_sp);
5103 mlxsw_sp_params_acl_region_rehash_intrvl_set(struct devlink *devlink, u32 id,
5104 struct devlink_param_gset_ctx *ctx)
5106 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5107 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5109 return mlxsw_sp_acl_region_rehash_intrvl_set(mlxsw_sp, ctx->val.vu32);
5112 static const struct devlink_param mlxsw_sp2_devlink_params[] = {
5113 DEVLINK_PARAM_DRIVER(MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5114 "acl_region_rehash_interval",
5115 DEVLINK_PARAM_TYPE_U32,
5116 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
5117 mlxsw_sp_params_acl_region_rehash_intrvl_get,
5118 mlxsw_sp_params_acl_region_rehash_intrvl_set,
5122 static int mlxsw_sp2_params_register(struct mlxsw_core *mlxsw_core)
5124 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5125 union devlink_param_value value;
5128 err = mlxsw_sp_params_register(mlxsw_core);
5132 err = devlink_params_register(devlink, mlxsw_sp2_devlink_params,
5133 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5135 goto err_devlink_params_register;
5138 devlink_param_driverinit_value_set(devlink,
5139 MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5143 err_devlink_params_register:
5144 mlxsw_sp_params_unregister(mlxsw_core);
5148 static void mlxsw_sp2_params_unregister(struct mlxsw_core *mlxsw_core)
5150 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5151 mlxsw_sp2_devlink_params,
5152 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5153 mlxsw_sp_params_unregister(mlxsw_core);
5156 static void mlxsw_sp_ptp_transmitted(struct mlxsw_core *mlxsw_core,
5157 struct sk_buff *skb, u8 local_port)
5159 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5161 skb_pull(skb, MLXSW_TXHDR_LEN);
5162 mlxsw_sp->ptp_ops->transmitted(mlxsw_sp, skb, local_port);
5165 static struct mlxsw_driver mlxsw_sp1_driver = {
5166 .kind = mlxsw_sp1_driver_name,
5167 .priv_size = sizeof(struct mlxsw_sp),
5168 .init = mlxsw_sp1_init,
5169 .fini = mlxsw_sp_fini,
5170 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5171 .port_split = mlxsw_sp_port_split,
5172 .port_unsplit = mlxsw_sp_port_unsplit,
5173 .sb_pool_get = mlxsw_sp_sb_pool_get,
5174 .sb_pool_set = mlxsw_sp_sb_pool_set,
5175 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5176 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5177 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5178 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5179 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5180 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5181 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5182 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5183 .flash_update = mlxsw_sp_flash_update,
5184 .txhdr_construct = mlxsw_sp_txhdr_construct,
5185 .resources_register = mlxsw_sp1_resources_register,
5186 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
5187 .params_register = mlxsw_sp_params_register,
5188 .params_unregister = mlxsw_sp_params_unregister,
5189 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5190 .txhdr_len = MLXSW_TXHDR_LEN,
5191 .profile = &mlxsw_sp1_config_profile,
5192 .res_query_enabled = true,
5195 static struct mlxsw_driver mlxsw_sp2_driver = {
5196 .kind = mlxsw_sp2_driver_name,
5197 .priv_size = sizeof(struct mlxsw_sp),
5198 .init = mlxsw_sp2_init,
5199 .fini = mlxsw_sp_fini,
5200 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5201 .port_split = mlxsw_sp_port_split,
5202 .port_unsplit = mlxsw_sp_port_unsplit,
5203 .sb_pool_get = mlxsw_sp_sb_pool_get,
5204 .sb_pool_set = mlxsw_sp_sb_pool_set,
5205 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5206 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5207 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5208 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5209 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5210 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5211 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5212 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5213 .flash_update = mlxsw_sp_flash_update,
5214 .txhdr_construct = mlxsw_sp_txhdr_construct,
5215 .resources_register = mlxsw_sp2_resources_register,
5216 .params_register = mlxsw_sp2_params_register,
5217 .params_unregister = mlxsw_sp2_params_unregister,
5218 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5219 .txhdr_len = MLXSW_TXHDR_LEN,
5220 .profile = &mlxsw_sp2_config_profile,
5221 .res_query_enabled = true,
5224 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
5226 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
5229 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
5231 struct mlxsw_sp_port **p_mlxsw_sp_port = data;
5234 if (mlxsw_sp_port_dev_check(lower_dev)) {
5235 *p_mlxsw_sp_port = netdev_priv(lower_dev);
5242 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
5244 struct mlxsw_sp_port *mlxsw_sp_port;
5246 if (mlxsw_sp_port_dev_check(dev))
5247 return netdev_priv(dev);
5249 mlxsw_sp_port = NULL;
5250 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
5252 return mlxsw_sp_port;
5255 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
5257 struct mlxsw_sp_port *mlxsw_sp_port;
5259 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
5260 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
5263 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
5265 struct mlxsw_sp_port *mlxsw_sp_port;
5267 if (mlxsw_sp_port_dev_check(dev))
5268 return netdev_priv(dev);
5270 mlxsw_sp_port = NULL;
5271 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
5274 return mlxsw_sp_port;
5277 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
5279 struct mlxsw_sp_port *mlxsw_sp_port;
5282 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
5284 dev_hold(mlxsw_sp_port->dev);
5286 return mlxsw_sp_port;
5289 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
5291 dev_put(mlxsw_sp_port->dev);
5295 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
5296 struct net_device *lag_dev)
5298 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
5299 struct net_device *upper_dev;
5300 struct list_head *iter;
5302 if (netif_is_bridge_port(lag_dev))
5303 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
5305 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
5306 if (!netif_is_bridge_port(upper_dev))
5308 br_dev = netdev_master_upper_dev_get(upper_dev);
5309 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
5313 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5315 char sldr_pl[MLXSW_REG_SLDR_LEN];
5317 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
5318 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5321 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5323 char sldr_pl[MLXSW_REG_SLDR_LEN];
5325 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
5326 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5329 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
5330 u16 lag_id, u8 port_index)
5332 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5333 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5335 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
5336 lag_id, port_index);
5337 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5340 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
5343 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5344 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5346 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
5348 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5351 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
5354 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5355 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5357 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
5359 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5362 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
5365 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5366 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5368 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
5370 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5373 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5374 struct net_device *lag_dev,
5377 struct mlxsw_sp_upper *lag;
5378 int free_lag_id = -1;
5382 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
5383 for (i = 0; i < max_lag; i++) {
5384 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
5385 if (lag->ref_count) {
5386 if (lag->dev == lag_dev) {
5390 } else if (free_lag_id < 0) {
5394 if (free_lag_id < 0)
5396 *p_lag_id = free_lag_id;
5401 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
5402 struct net_device *lag_dev,
5403 struct netdev_lag_upper_info *lag_upper_info,
5404 struct netlink_ext_ack *extack)
5408 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
5409 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported LAG devices");
5412 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
5413 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
5419 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5420 u16 lag_id, u8 *p_port_index)
5422 u64 max_lag_members;
5425 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
5427 for (i = 0; i < max_lag_members; i++) {
5428 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
5436 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
5437 struct net_device *lag_dev)
5439 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5440 struct mlxsw_sp_upper *lag;
5445 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
5448 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5449 if (!lag->ref_count) {
5450 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
5456 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
5459 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
5461 goto err_col_port_add;
5463 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
5464 mlxsw_sp_port->local_port);
5465 mlxsw_sp_port->lag_id = lag_id;
5466 mlxsw_sp_port->lagged = 1;
5469 /* Port is no longer usable as a router interface */
5470 if (mlxsw_sp_port->default_vlan->fid)
5471 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
5476 if (!lag->ref_count)
5477 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
5481 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
5482 struct net_device *lag_dev)
5484 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5485 u16 lag_id = mlxsw_sp_port->lag_id;
5486 struct mlxsw_sp_upper *lag;
5488 if (!mlxsw_sp_port->lagged)
5490 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5491 WARN_ON(lag->ref_count == 0);
5493 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
5495 /* Any VLANs configured on the port are no longer valid */
5496 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, false);
5497 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port->default_vlan);
5498 /* Make the LAG and its directly linked uppers leave bridges they
5501 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
5503 if (lag->ref_count == 1)
5504 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
5506 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
5507 mlxsw_sp_port->local_port);
5508 mlxsw_sp_port->lagged = 0;
5511 /* Make sure untagged frames are allowed to ingress */
5512 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
5515 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
5518 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5519 char sldr_pl[MLXSW_REG_SLDR_LEN];
5521 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
5522 mlxsw_sp_port->local_port);
5523 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5526 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
5529 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5530 char sldr_pl[MLXSW_REG_SLDR_LEN];
5532 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
5533 mlxsw_sp_port->local_port);
5534 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5538 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
5542 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
5543 mlxsw_sp_port->lag_id);
5547 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5549 goto err_dist_port_add;
5554 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5559 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
5563 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
5564 mlxsw_sp_port->lag_id);
5568 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
5569 mlxsw_sp_port->lag_id);
5571 goto err_col_port_disable;
5575 err_col_port_disable:
5576 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
5580 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
5581 struct netdev_lag_lower_state_info *info)
5583 if (info->tx_enabled)
5584 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
5586 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
5589 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
5592 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5593 enum mlxsw_reg_spms_state spms_state;
5598 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
5599 MLXSW_REG_SPMS_STATE_DISCARDING;
5601 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
5604 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
5606 for (vid = 0; vid < VLAN_N_VID; vid++)
5607 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
5609 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
5614 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
5619 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
5622 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
5624 goto err_port_stp_set;
5625 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
5628 goto err_port_vlan_set;
5630 for (; vid <= VLAN_N_VID - 1; vid++) {
5631 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
5634 goto err_vid_learning_set;
5639 err_vid_learning_set:
5640 for (vid--; vid >= 1; vid--)
5641 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
5643 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
5645 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
5649 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
5653 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
5654 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
5657 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
5659 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
5660 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
5663 static bool mlxsw_sp_bridge_has_multiple_vxlans(struct net_device *br_dev)
5665 unsigned int num_vxlans = 0;
5666 struct net_device *dev;
5667 struct list_head *iter;
5669 netdev_for_each_lower_dev(br_dev, dev, iter) {
5670 if (netif_is_vxlan(dev))
5674 return num_vxlans > 1;
5677 static bool mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device *br_dev)
5679 DECLARE_BITMAP(vlans, VLAN_N_VID) = {0};
5680 struct net_device *dev;
5681 struct list_head *iter;
5683 netdev_for_each_lower_dev(br_dev, dev, iter) {
5687 if (!netif_is_vxlan(dev))
5690 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
5694 if (test_and_set_bit(pvid, vlans))
5701 static bool mlxsw_sp_bridge_vxlan_is_valid(struct net_device *br_dev,
5702 struct netlink_ext_ack *extack)
5704 if (br_multicast_enabled(br_dev)) {
5705 NL_SET_ERR_MSG_MOD(extack, "Multicast can not be enabled on a bridge with a VxLAN device");
5709 if (!br_vlan_enabled(br_dev) &&
5710 mlxsw_sp_bridge_has_multiple_vxlans(br_dev)) {
5711 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices are not supported in a VLAN-unaware bridge");
5715 if (br_vlan_enabled(br_dev) &&
5716 !mlxsw_sp_bridge_vxlan_vlan_is_valid(br_dev)) {
5717 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices cannot have the same VLAN as PVID and egress untagged");
5724 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
5725 struct net_device *dev,
5726 unsigned long event, void *ptr)
5728 struct netdev_notifier_changeupper_info *info;
5729 struct mlxsw_sp_port *mlxsw_sp_port;
5730 struct netlink_ext_ack *extack;
5731 struct net_device *upper_dev;
5732 struct mlxsw_sp *mlxsw_sp;
5735 mlxsw_sp_port = netdev_priv(dev);
5736 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5738 extack = netdev_notifier_info_to_extack(&info->info);
5741 case NETDEV_PRECHANGEUPPER:
5742 upper_dev = info->upper_dev;
5743 if (!is_vlan_dev(upper_dev) &&
5744 !netif_is_lag_master(upper_dev) &&
5745 !netif_is_bridge_master(upper_dev) &&
5746 !netif_is_ovs_master(upper_dev) &&
5747 !netif_is_macvlan(upper_dev)) {
5748 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5753 if (netif_is_bridge_master(upper_dev) &&
5754 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
5755 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
5756 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5758 if (netdev_has_any_upper_dev(upper_dev) &&
5759 (!netif_is_bridge_master(upper_dev) ||
5760 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
5762 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
5765 if (netif_is_lag_master(upper_dev) &&
5766 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
5767 info->upper_info, extack))
5769 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
5770 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
5773 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
5774 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
5775 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
5778 if (netif_is_macvlan(upper_dev) &&
5779 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, lower_dev)) {
5780 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
5783 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
5784 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
5787 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
5788 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
5792 case NETDEV_CHANGEUPPER:
5793 upper_dev = info->upper_dev;
5794 if (netif_is_bridge_master(upper_dev)) {
5796 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5801 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5804 } else if (netif_is_lag_master(upper_dev)) {
5805 if (info->linking) {
5806 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
5809 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
5810 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
5813 } else if (netif_is_ovs_master(upper_dev)) {
5815 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
5817 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
5818 } else if (netif_is_macvlan(upper_dev)) {
5820 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5821 } else if (is_vlan_dev(upper_dev)) {
5822 struct net_device *br_dev;
5824 if (!netif_is_bridge_port(upper_dev))
5828 br_dev = netdev_master_upper_dev_get(upper_dev);
5829 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
5838 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
5839 unsigned long event, void *ptr)
5841 struct netdev_notifier_changelowerstate_info *info;
5842 struct mlxsw_sp_port *mlxsw_sp_port;
5845 mlxsw_sp_port = netdev_priv(dev);
5849 case NETDEV_CHANGELOWERSTATE:
5850 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
5851 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
5852 info->lower_state_info);
5854 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
5862 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
5863 struct net_device *port_dev,
5864 unsigned long event, void *ptr)
5867 case NETDEV_PRECHANGEUPPER:
5868 case NETDEV_CHANGEUPPER:
5869 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
5871 case NETDEV_CHANGELOWERSTATE:
5872 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
5879 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
5880 unsigned long event, void *ptr)
5882 struct net_device *dev;
5883 struct list_head *iter;
5886 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5887 if (mlxsw_sp_port_dev_check(dev)) {
5888 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
5898 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
5899 struct net_device *dev,
5900 unsigned long event, void *ptr,
5903 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
5904 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5905 struct netdev_notifier_changeupper_info *info = ptr;
5906 struct netlink_ext_ack *extack;
5907 struct net_device *upper_dev;
5910 extack = netdev_notifier_info_to_extack(&info->info);
5913 case NETDEV_PRECHANGEUPPER:
5914 upper_dev = info->upper_dev;
5915 if (!netif_is_bridge_master(upper_dev) &&
5916 !netif_is_macvlan(upper_dev)) {
5917 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5922 if (netif_is_bridge_master(upper_dev) &&
5923 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
5924 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
5925 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5927 if (netdev_has_any_upper_dev(upper_dev) &&
5928 (!netif_is_bridge_master(upper_dev) ||
5929 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
5931 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
5934 if (netif_is_macvlan(upper_dev) &&
5935 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan_dev)) {
5936 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
5940 case NETDEV_CHANGEUPPER:
5941 upper_dev = info->upper_dev;
5942 if (netif_is_bridge_master(upper_dev)) {
5944 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5949 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5952 } else if (netif_is_macvlan(upper_dev)) {
5954 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5965 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
5966 struct net_device *lag_dev,
5967 unsigned long event,
5970 struct net_device *dev;
5971 struct list_head *iter;
5974 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5975 if (mlxsw_sp_port_dev_check(dev)) {
5976 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
5987 static int mlxsw_sp_netdevice_bridge_vlan_event(struct net_device *vlan_dev,
5988 struct net_device *br_dev,
5989 unsigned long event, void *ptr,
5992 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
5993 struct netdev_notifier_changeupper_info *info = ptr;
5994 struct netlink_ext_ack *extack;
5995 struct net_device *upper_dev;
6000 extack = netdev_notifier_info_to_extack(&info->info);
6003 case NETDEV_PRECHANGEUPPER:
6004 upper_dev = info->upper_dev;
6005 if (!netif_is_macvlan(upper_dev)) {
6006 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6011 if (netif_is_macvlan(upper_dev) &&
6012 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan_dev)) {
6013 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6017 case NETDEV_CHANGEUPPER:
6018 upper_dev = info->upper_dev;
6021 if (netif_is_macvlan(upper_dev))
6022 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6029 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
6030 unsigned long event, void *ptr)
6032 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
6033 u16 vid = vlan_dev_vlan_id(vlan_dev);
6035 if (mlxsw_sp_port_dev_check(real_dev))
6036 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
6038 else if (netif_is_lag_master(real_dev))
6039 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
6042 else if (netif_is_bridge_master(real_dev))
6043 return mlxsw_sp_netdevice_bridge_vlan_event(vlan_dev, real_dev,
6049 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
6050 unsigned long event, void *ptr)
6052 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(br_dev);
6053 struct netdev_notifier_changeupper_info *info = ptr;
6054 struct netlink_ext_ack *extack;
6055 struct net_device *upper_dev;
6060 extack = netdev_notifier_info_to_extack(&info->info);
6063 case NETDEV_PRECHANGEUPPER:
6064 upper_dev = info->upper_dev;
6065 if (!is_vlan_dev(upper_dev) && !netif_is_macvlan(upper_dev)) {
6066 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6071 if (netif_is_macvlan(upper_dev) &&
6072 !mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev)) {
6073 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6077 case NETDEV_CHANGEUPPER:
6078 upper_dev = info->upper_dev;
6081 if (is_vlan_dev(upper_dev))
6082 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
6083 if (netif_is_macvlan(upper_dev))
6084 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6091 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
6092 unsigned long event, void *ptr)
6094 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
6095 struct netdev_notifier_changeupper_info *info = ptr;
6096 struct netlink_ext_ack *extack;
6098 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
6101 extack = netdev_notifier_info_to_extack(&info->info);
6103 /* VRF enslavement is handled in mlxsw_sp_netdevice_vrf_event() */
6104 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6109 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
6111 struct netdev_notifier_changeupper_info *info = ptr;
6113 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
6115 return netif_is_l3_master(info->upper_dev);
6118 static int mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp *mlxsw_sp,
6119 struct net_device *dev,
6120 unsigned long event, void *ptr)
6122 struct netdev_notifier_changeupper_info *cu_info;
6123 struct netdev_notifier_info *info = ptr;
6124 struct netlink_ext_ack *extack;
6125 struct net_device *upper_dev;
6127 extack = netdev_notifier_info_to_extack(info);
6130 case NETDEV_CHANGEUPPER:
6131 cu_info = container_of(info,
6132 struct netdev_notifier_changeupper_info,
6134 upper_dev = cu_info->upper_dev;
6135 if (!netif_is_bridge_master(upper_dev))
6137 if (!mlxsw_sp_lower_get(upper_dev))
6139 if (!mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
6141 if (cu_info->linking) {
6142 if (!netif_running(dev))
6144 /* When the bridge is VLAN-aware, the VNI of the VxLAN
6145 * device needs to be mapped to a VLAN, but at this
6146 * point no VLANs are configured on the VxLAN device
6148 if (br_vlan_enabled(upper_dev))
6150 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev,
6153 /* VLANs were already flushed, which triggered the
6156 if (br_vlan_enabled(upper_dev))
6158 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6162 upper_dev = netdev_master_upper_dev_get(dev);
6165 if (!netif_is_bridge_master(upper_dev))
6167 if (!mlxsw_sp_lower_get(upper_dev))
6169 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev, dev, 0,
6172 upper_dev = netdev_master_upper_dev_get(dev);
6175 if (!netif_is_bridge_master(upper_dev))
6177 if (!mlxsw_sp_lower_get(upper_dev))
6179 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6186 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
6187 unsigned long event, void *ptr)
6189 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6190 struct mlxsw_sp_span_entry *span_entry;
6191 struct mlxsw_sp *mlxsw_sp;
6194 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
6195 if (event == NETDEV_UNREGISTER) {
6196 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
6198 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
6200 mlxsw_sp_span_respin(mlxsw_sp);
6202 if (netif_is_vxlan(dev))
6203 err = mlxsw_sp_netdevice_vxlan_event(mlxsw_sp, dev, event, ptr);
6204 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
6205 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
6207 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
6208 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
6210 else if (event == NETDEV_PRE_CHANGEADDR ||
6211 event == NETDEV_CHANGEADDR ||
6212 event == NETDEV_CHANGEMTU)
6213 err = mlxsw_sp_netdevice_router_port_event(dev, event, ptr);
6214 else if (mlxsw_sp_is_vrf_event(event, ptr))
6215 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
6216 else if (mlxsw_sp_port_dev_check(dev))
6217 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
6218 else if (netif_is_lag_master(dev))
6219 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
6220 else if (is_vlan_dev(dev))
6221 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
6222 else if (netif_is_bridge_master(dev))
6223 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
6224 else if (netif_is_macvlan(dev))
6225 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
6227 return notifier_from_errno(err);
6230 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
6231 .notifier_call = mlxsw_sp_inetaddr_valid_event,
6234 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
6235 .notifier_call = mlxsw_sp_inet6addr_valid_event,
6238 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
6239 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
6243 static struct pci_driver mlxsw_sp1_pci_driver = {
6244 .name = mlxsw_sp1_driver_name,
6245 .id_table = mlxsw_sp1_pci_id_table,
6248 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
6249 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
6253 static struct pci_driver mlxsw_sp2_pci_driver = {
6254 .name = mlxsw_sp2_driver_name,
6255 .id_table = mlxsw_sp2_pci_id_table,
6258 static int __init mlxsw_sp_module_init(void)
6262 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6263 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6265 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
6267 goto err_sp1_core_driver_register;
6269 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
6271 goto err_sp2_core_driver_register;
6273 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
6275 goto err_sp1_pci_driver_register;
6277 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
6279 goto err_sp2_pci_driver_register;
6283 err_sp2_pci_driver_register:
6284 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
6285 err_sp1_pci_driver_register:
6286 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6287 err_sp2_core_driver_register:
6288 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6289 err_sp1_core_driver_register:
6290 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6291 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6295 static void __exit mlxsw_sp_module_exit(void)
6297 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
6298 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
6299 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6300 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6301 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6302 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6305 module_init(mlxsw_sp_module_init);
6306 module_exit(mlxsw_sp_module_exit);
6308 MODULE_LICENSE("Dual BSD/GPL");
6309 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
6310 MODULE_DESCRIPTION("Mellanox Spectrum driver");
6311 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
6312 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);
6313 MODULE_FIRMWARE(MLXSW_SP1_FW_FILENAME);