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 <linux/log2.h>
26 #include <net/switchdev.h>
27 #include <net/pkt_cls.h>
28 #include <net/tc_act/tc_mirred.h>
29 #include <net/netevent.h>
30 #include <net/tc_act/tc_sample.h>
31 #include <net/addrconf.h>
41 #include "spectrum_cnt.h"
42 #include "spectrum_dpipe.h"
43 #include "spectrum_acl_flex_actions.h"
44 #include "spectrum_span.h"
45 #include "spectrum_ptp.h"
46 #include "../mlxfw/mlxfw.h"
48 #define MLXSW_SP1_FWREV_MAJOR 13
49 #define MLXSW_SP1_FWREV_MINOR 2000
50 #define MLXSW_SP1_FWREV_SUBMINOR 2714
51 #define MLXSW_SP1_FWREV_CAN_RESET_MINOR 1702
53 static const struct mlxsw_fw_rev mlxsw_sp1_fw_rev = {
54 .major = MLXSW_SP1_FWREV_MAJOR,
55 .minor = MLXSW_SP1_FWREV_MINOR,
56 .subminor = MLXSW_SP1_FWREV_SUBMINOR,
57 .can_reset_minor = MLXSW_SP1_FWREV_CAN_RESET_MINOR,
60 #define MLXSW_SP1_FW_FILENAME \
61 "mellanox/mlxsw_spectrum-" __stringify(MLXSW_SP1_FWREV_MAJOR) \
62 "." __stringify(MLXSW_SP1_FWREV_MINOR) \
63 "." __stringify(MLXSW_SP1_FWREV_SUBMINOR) ".mfa2"
65 #define MLXSW_SP2_FWREV_MAJOR 29
66 #define MLXSW_SP2_FWREV_MINOR 2000
67 #define MLXSW_SP2_FWREV_SUBMINOR 2714
69 static const struct mlxsw_fw_rev mlxsw_sp2_fw_rev = {
70 .major = MLXSW_SP2_FWREV_MAJOR,
71 .minor = MLXSW_SP2_FWREV_MINOR,
72 .subminor = MLXSW_SP2_FWREV_SUBMINOR,
75 #define MLXSW_SP2_FW_FILENAME \
76 "mellanox/mlxsw_spectrum2-" __stringify(MLXSW_SP2_FWREV_MAJOR) \
77 "." __stringify(MLXSW_SP2_FWREV_MINOR) \
78 "." __stringify(MLXSW_SP2_FWREV_SUBMINOR) ".mfa2"
80 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
81 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
82 static const char mlxsw_sp3_driver_name[] = "mlxsw_spectrum3";
83 static const char mlxsw_sp_driver_version[] = "1.0";
85 static const unsigned char mlxsw_sp1_mac_mask[ETH_ALEN] = {
86 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00
88 static const unsigned char mlxsw_sp2_mac_mask[ETH_ALEN] = {
89 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
96 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
99 * Packet control type.
100 * 0 - Ethernet control (e.g. EMADs, LACP)
103 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
106 * Packet protocol type. Must be set to 1 (Ethernet).
108 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
110 /* tx_hdr_rx_is_router
111 * Packet is sent from the router. Valid for data packets only.
113 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
116 * Indicates if the 'fid' field is valid and should be used for
117 * forwarding lookup. Valid for data packets only.
119 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
122 * Switch partition ID. Must be set to 0.
124 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
126 /* tx_hdr_control_tclass
127 * Indicates if the packet should use the control TClass and not one
128 * of the data TClasses.
130 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
133 * Egress TClass to be used on the egress device on the egress port.
135 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
138 * Destination local port for unicast packets.
139 * Destination multicast ID for multicast packets.
141 * Control packets are directed to a specific egress port, while data
142 * packets are transmitted through the CPU port (0) into the switch partition,
143 * where forwarding rules are applied.
145 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
148 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
149 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
150 * Valid for data packets only.
152 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
156 * 6 - Control packets
158 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
160 struct mlxsw_sp_mlxfw_dev {
161 struct mlxfw_dev mlxfw_dev;
162 struct mlxsw_sp *mlxsw_sp;
165 struct mlxsw_sp_ptp_ops {
166 struct mlxsw_sp_ptp_clock *
167 (*clock_init)(struct mlxsw_sp *mlxsw_sp, struct device *dev);
168 void (*clock_fini)(struct mlxsw_sp_ptp_clock *clock);
170 struct mlxsw_sp_ptp_state *(*init)(struct mlxsw_sp *mlxsw_sp);
171 void (*fini)(struct mlxsw_sp_ptp_state *ptp_state);
173 /* Notify a driver that a packet that might be PTP was received. Driver
174 * is responsible for freeing the passed-in SKB.
176 void (*receive)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
179 /* Notify a driver that a timestamped packet was transmitted. Driver
180 * is responsible for freeing the passed-in SKB.
182 void (*transmitted)(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
185 int (*hwtstamp_get)(struct mlxsw_sp_port *mlxsw_sp_port,
186 struct hwtstamp_config *config);
187 int (*hwtstamp_set)(struct mlxsw_sp_port *mlxsw_sp_port,
188 struct hwtstamp_config *config);
189 void (*shaper_work)(struct work_struct *work);
190 int (*get_ts_info)(struct mlxsw_sp *mlxsw_sp,
191 struct ethtool_ts_info *info);
192 int (*get_stats_count)(void);
193 void (*get_stats_strings)(u8 **p);
194 void (*get_stats)(struct mlxsw_sp_port *mlxsw_sp_port,
195 u64 *data, int data_index);
198 struct mlxsw_sp_span_ops {
199 u32 (*buffsize_get)(int mtu, u32 speed);
202 static int mlxsw_sp_component_query(struct mlxfw_dev *mlxfw_dev,
203 u16 component_index, u32 *p_max_size,
204 u8 *p_align_bits, u16 *p_max_write_size)
206 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
207 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
208 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
209 char mcqi_pl[MLXSW_REG_MCQI_LEN];
212 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
213 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcqi), mcqi_pl);
216 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
219 *p_align_bits = max_t(u8, *p_align_bits, 2);
220 *p_max_write_size = min_t(u16, *p_max_write_size,
221 MLXSW_REG_MCDA_MAX_DATA_LEN);
225 static int mlxsw_sp_fsm_lock(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle)
227 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
228 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
230 char mcc_pl[MLXSW_REG_MCC_LEN];
234 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
235 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
239 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
240 if (control_state != MLXFW_FSM_STATE_IDLE)
243 mlxsw_reg_mcc_pack(mcc_pl,
244 MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
246 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
249 static int mlxsw_sp_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
250 u32 fwhandle, u16 component_index,
253 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
254 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
255 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
256 char mcc_pl[MLXSW_REG_MCC_LEN];
258 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
259 component_index, fwhandle, component_size);
260 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
263 static int mlxsw_sp_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
264 u32 fwhandle, u8 *data, u16 size,
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 mcda_pl[MLXSW_REG_MCDA_LEN];
272 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
273 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcda), mcda_pl);
276 static int mlxsw_sp_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
277 u32 fwhandle, u16 component_index)
279 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
280 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
281 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
282 char mcc_pl[MLXSW_REG_MCC_LEN];
284 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
285 component_index, fwhandle, 0);
286 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
289 static int mlxsw_sp_fsm_activate(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
291 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
292 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
293 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
294 char mcc_pl[MLXSW_REG_MCC_LEN];
296 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE, 0,
298 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
301 static int mlxsw_sp_fsm_query_state(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
302 enum mlxfw_fsm_state *fsm_state,
303 enum mlxfw_fsm_state_err *fsm_state_err)
305 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
306 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
307 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
308 char mcc_pl[MLXSW_REG_MCC_LEN];
313 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
314 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
318 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
319 *fsm_state = control_state;
320 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
321 MLXFW_FSM_STATE_ERR_MAX);
325 static void mlxsw_sp_fsm_cancel(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
327 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
328 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
329 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
330 char mcc_pl[MLXSW_REG_MCC_LEN];
332 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL, 0,
334 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
337 static void mlxsw_sp_fsm_release(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
339 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
340 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
341 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
342 char mcc_pl[MLXSW_REG_MCC_LEN];
344 mlxsw_reg_mcc_pack(mcc_pl,
345 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE, 0,
347 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
350 static const struct mlxfw_dev_ops mlxsw_sp_mlxfw_dev_ops = {
351 .component_query = mlxsw_sp_component_query,
352 .fsm_lock = mlxsw_sp_fsm_lock,
353 .fsm_component_update = mlxsw_sp_fsm_component_update,
354 .fsm_block_download = mlxsw_sp_fsm_block_download,
355 .fsm_component_verify = mlxsw_sp_fsm_component_verify,
356 .fsm_activate = mlxsw_sp_fsm_activate,
357 .fsm_query_state = mlxsw_sp_fsm_query_state,
358 .fsm_cancel = mlxsw_sp_fsm_cancel,
359 .fsm_release = mlxsw_sp_fsm_release,
362 static int mlxsw_sp_firmware_flash(struct mlxsw_sp *mlxsw_sp,
363 const struct firmware *firmware,
364 struct netlink_ext_ack *extack)
366 struct mlxsw_sp_mlxfw_dev mlxsw_sp_mlxfw_dev = {
368 .ops = &mlxsw_sp_mlxfw_dev_ops,
369 .psid = mlxsw_sp->bus_info->psid,
370 .psid_size = strlen(mlxsw_sp->bus_info->psid),
371 .devlink = priv_to_devlink(mlxsw_sp->core),
377 mlxsw_core_fw_flash_start(mlxsw_sp->core);
378 err = mlxfw_firmware_flash(&mlxsw_sp_mlxfw_dev.mlxfw_dev,
380 mlxsw_core_fw_flash_end(mlxsw_sp->core);
385 static int mlxsw_sp_fw_rev_validate(struct mlxsw_sp *mlxsw_sp)
387 const struct mlxsw_fw_rev *rev = &mlxsw_sp->bus_info->fw_rev;
388 const struct mlxsw_fw_rev *req_rev = mlxsw_sp->req_rev;
389 const char *fw_filename = mlxsw_sp->fw_filename;
390 union devlink_param_value value;
391 const struct firmware *firmware;
394 /* Don't check if driver does not require it */
395 if (!req_rev || !fw_filename)
398 /* Don't check if devlink 'fw_load_policy' param is 'flash' */
399 err = devlink_param_driverinit_value_get(priv_to_devlink(mlxsw_sp->core),
400 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
404 if (value.vu8 == DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)
407 /* Validate driver & FW are compatible */
408 if (rev->major != req_rev->major) {
409 WARN(1, "Mismatch in major FW version [%d:%d] is never expected; Please contact support\n",
410 rev->major, req_rev->major);
413 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, req_rev))
416 dev_err(mlxsw_sp->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
417 rev->major, rev->minor, rev->subminor, req_rev->major,
418 req_rev->minor, req_rev->subminor);
419 dev_info(mlxsw_sp->bus_info->dev, "Flashing firmware using file %s\n",
422 err = request_firmware_direct(&firmware, fw_filename,
423 mlxsw_sp->bus_info->dev);
425 dev_err(mlxsw_sp->bus_info->dev, "Could not request firmware file %s\n",
430 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, NULL);
431 release_firmware(firmware);
433 dev_err(mlxsw_sp->bus_info->dev, "Could not upgrade firmware\n");
435 /* On FW flash success, tell the caller FW reset is needed
436 * if current FW supports it.
438 if (rev->minor >= req_rev->can_reset_minor)
439 return err ? err : -EAGAIN;
444 static int mlxsw_sp_flash_update(struct mlxsw_core *mlxsw_core,
445 const char *file_name, const char *component,
446 struct netlink_ext_ack *extack)
448 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
449 const struct firmware *firmware;
455 err = request_firmware_direct(&firmware, file_name,
456 mlxsw_sp->bus_info->dev);
459 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware, extack);
460 release_firmware(firmware);
465 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
466 unsigned int counter_index, u64 *packets,
469 char mgpc_pl[MLXSW_REG_MGPC_LEN];
472 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
473 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
474 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
478 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
480 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
484 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
485 unsigned int counter_index)
487 char mgpc_pl[MLXSW_REG_MGPC_LEN];
489 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
490 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
491 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
494 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
495 unsigned int *p_counter_index)
499 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
503 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
505 goto err_counter_clear;
509 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
514 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
515 unsigned int counter_index)
517 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
521 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
522 const struct mlxsw_tx_info *tx_info)
524 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
526 memset(txhdr, 0, MLXSW_TXHDR_LEN);
528 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
529 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
530 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
531 mlxsw_tx_hdr_swid_set(txhdr, 0);
532 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
533 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
534 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
537 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
540 case BR_STATE_FORWARDING:
541 return MLXSW_REG_SPMS_STATE_FORWARDING;
542 case BR_STATE_LEARNING:
543 return MLXSW_REG_SPMS_STATE_LEARNING;
544 case BR_STATE_LISTENING: /* fall-through */
545 case BR_STATE_DISABLED: /* fall-through */
546 case BR_STATE_BLOCKING:
547 return MLXSW_REG_SPMS_STATE_DISCARDING;
553 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
556 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
557 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
561 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
564 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
565 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
567 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
572 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
574 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
577 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
580 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
584 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
585 bool enable, u32 rate)
587 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
588 char mpsc_pl[MLXSW_REG_MPSC_LEN];
590 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
591 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
594 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
597 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
598 char paos_pl[MLXSW_REG_PAOS_LEN];
600 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
601 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
602 MLXSW_PORT_ADMIN_STATUS_DOWN);
603 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
606 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
609 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
610 char ppad_pl[MLXSW_REG_PPAD_LEN];
612 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
613 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
614 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
617 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
619 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
620 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
622 ether_addr_copy(addr, mlxsw_sp->base_mac);
623 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
624 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
627 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
629 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
630 char pmtu_pl[MLXSW_REG_PMTU_LEN];
634 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
635 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
636 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
639 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
644 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
645 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
648 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
650 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
651 char pspa_pl[MLXSW_REG_PSPA_LEN];
653 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
654 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
657 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
659 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
660 char svpe_pl[MLXSW_REG_SVPE_LEN];
662 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
663 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
666 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
669 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
673 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
676 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
678 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
683 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
686 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
687 char spvid_pl[MLXSW_REG_SPVID_LEN];
689 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
690 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
693 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
696 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
697 char spaft_pl[MLXSW_REG_SPAFT_LEN];
699 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
700 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
703 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
708 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
712 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
715 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
717 goto err_port_allow_untagged_set;
720 mlxsw_sp_port->pvid = vid;
723 err_port_allow_untagged_set:
724 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
729 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
731 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
732 char sspr_pl[MLXSW_REG_SSPR_LEN];
734 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
735 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
739 mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, u8 local_port,
740 struct mlxsw_sp_port_mapping *port_mapping)
742 char pmlp_pl[MLXSW_REG_PMLP_LEN];
749 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
750 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
753 module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
754 width = mlxsw_reg_pmlp_width_get(pmlp_pl);
755 separate_rxtx = mlxsw_reg_pmlp_rxtx_get(pmlp_pl);
757 if (width && !is_power_of_2(width)) {
758 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: width value is not power of 2\n",
763 for (i = 0; i < width; i++) {
764 if (mlxsw_reg_pmlp_module_get(pmlp_pl, i) != module) {
765 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: contains multiple modules\n",
770 mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) !=
771 mlxsw_reg_pmlp_rx_lane_get(pmlp_pl, i)) {
772 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are different\n",
776 if (mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) != i) {
777 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are not sequential\n",
783 port_mapping->module = module;
784 port_mapping->width = width;
785 port_mapping->lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
789 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port)
791 struct mlxsw_sp_port_mapping *port_mapping = &mlxsw_sp_port->mapping;
792 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
793 char pmlp_pl[MLXSW_REG_PMLP_LEN];
796 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
797 mlxsw_reg_pmlp_width_set(pmlp_pl, port_mapping->width);
798 for (i = 0; i < port_mapping->width; i++) {
799 mlxsw_reg_pmlp_module_set(pmlp_pl, i, port_mapping->module);
800 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, port_mapping->lane + i); /* Rx & Tx */
803 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
806 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
808 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
809 char pmlp_pl[MLXSW_REG_PMLP_LEN];
811 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
812 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
813 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
816 static int mlxsw_sp_port_open(struct net_device *dev)
818 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
821 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
824 netif_start_queue(dev);
828 static int mlxsw_sp_port_stop(struct net_device *dev)
830 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
832 netif_stop_queue(dev);
833 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
836 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
837 struct net_device *dev)
839 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
840 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
841 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
842 const struct mlxsw_tx_info tx_info = {
843 .local_port = mlxsw_sp_port->local_port,
849 if (skb_cow_head(skb, MLXSW_TXHDR_LEN)) {
850 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
851 dev_kfree_skb_any(skb);
855 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
857 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
858 return NETDEV_TX_BUSY;
860 if (eth_skb_pad(skb)) {
861 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
865 mlxsw_sp_txhdr_construct(skb, &tx_info);
866 /* TX header is consumed by HW on the way so we shouldn't count its
867 * bytes as being sent.
869 len = skb->len - MLXSW_TXHDR_LEN;
871 /* Due to a race we might fail here because of a full queue. In that
872 * unlikely case we simply drop the packet.
874 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
877 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
878 u64_stats_update_begin(&pcpu_stats->syncp);
879 pcpu_stats->tx_packets++;
880 pcpu_stats->tx_bytes += len;
881 u64_stats_update_end(&pcpu_stats->syncp);
883 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
884 dev_kfree_skb_any(skb);
889 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
893 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
895 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
896 struct sockaddr *addr = p;
899 if (!is_valid_ether_addr(addr->sa_data))
900 return -EADDRNOTAVAIL;
902 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
905 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
909 static u16 mlxsw_sp_pg_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp,
912 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
915 #define MLXSW_SP_CELL_FACTOR 2 /* 2 * cell_size / (IPG + cell_size + 1) */
917 static u16 mlxsw_sp_pfc_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
920 delay = mlxsw_sp_bytes_cells(mlxsw_sp, DIV_ROUND_UP(delay,
922 return MLXSW_SP_CELL_FACTOR * delay + mlxsw_sp_bytes_cells(mlxsw_sp,
926 /* Maximum delay buffer needed in case of PAUSE frames, in bytes.
927 * Assumes 100m cable and maximum MTU.
929 #define MLXSW_SP_PAUSE_DELAY 58752
931 static u16 mlxsw_sp_pg_buf_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
932 u16 delay, bool pfc, bool pause)
935 return mlxsw_sp_pfc_delay_get(mlxsw_sp, mtu, delay);
937 return mlxsw_sp_bytes_cells(mlxsw_sp, MLXSW_SP_PAUSE_DELAY);
942 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres,
946 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
948 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
952 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
953 u8 *prio_tc, bool pause_en,
954 struct ieee_pfc *my_pfc)
956 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
957 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
958 u16 delay = !!my_pfc ? my_pfc->delay : 0;
959 char pbmc_pl[MLXSW_REG_PBMC_LEN];
960 u32 taken_headroom_cells = 0;
961 u32 max_headroom_cells;
964 max_headroom_cells = mlxsw_sp_sb_max_headroom_cells(mlxsw_sp);
966 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
967 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
971 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
972 bool configure = false;
979 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
980 if (prio_tc[j] == i) {
981 pfc = pfc_en & BIT(j);
990 lossy = !(pfc || pause_en);
991 thres_cells = mlxsw_sp_pg_buf_threshold_get(mlxsw_sp, mtu);
992 delay_cells = mlxsw_sp_pg_buf_delay_get(mlxsw_sp, mtu, delay,
994 total_cells = thres_cells + delay_cells;
996 taken_headroom_cells += total_cells;
997 if (taken_headroom_cells > max_headroom_cells)
1000 mlxsw_sp_pg_buf_pack(pbmc_pl, i, total_cells,
1001 thres_cells, lossy);
1004 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
1007 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
1008 int mtu, bool pause_en)
1010 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
1011 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
1012 struct ieee_pfc *my_pfc;
1015 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
1016 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
1018 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
1022 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
1024 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1025 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1028 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
1031 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
1033 goto err_span_port_mtu_update;
1034 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
1036 goto err_port_mtu_set;
1041 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
1042 err_span_port_mtu_update:
1043 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1048 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
1049 struct rtnl_link_stats64 *stats)
1051 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1052 struct mlxsw_sp_port_pcpu_stats *p;
1053 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1058 for_each_possible_cpu(i) {
1059 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
1061 start = u64_stats_fetch_begin_irq(&p->syncp);
1062 rx_packets = p->rx_packets;
1063 rx_bytes = p->rx_bytes;
1064 tx_packets = p->tx_packets;
1065 tx_bytes = p->tx_bytes;
1066 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1068 stats->rx_packets += rx_packets;
1069 stats->rx_bytes += rx_bytes;
1070 stats->tx_packets += tx_packets;
1071 stats->tx_bytes += tx_bytes;
1072 /* tx_dropped is u32, updated without syncp protection. */
1073 tx_dropped += p->tx_dropped;
1075 stats->tx_dropped = tx_dropped;
1079 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
1082 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1089 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
1093 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1094 return mlxsw_sp_port_get_sw_stats64(dev, sp);
1100 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
1101 int prio, char *ppcnt_pl)
1103 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1104 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1106 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1107 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1110 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
1111 struct rtnl_link_stats64 *stats)
1113 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1116 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
1122 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
1124 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
1126 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
1128 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
1130 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
1132 stats->rx_crc_errors =
1133 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
1134 stats->rx_frame_errors =
1135 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
1137 stats->rx_length_errors = (
1138 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
1139 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
1140 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
1142 stats->rx_errors = (stats->rx_crc_errors +
1143 stats->rx_frame_errors + stats->rx_length_errors);
1150 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
1151 struct mlxsw_sp_port_xstats *xstats)
1153 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1156 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
1159 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
1161 for (i = 0; i < TC_MAX_QUEUE; i++) {
1162 err = mlxsw_sp_port_get_stats_raw(dev,
1163 MLXSW_REG_PPCNT_TC_CONG_TC,
1166 xstats->wred_drop[i] =
1167 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
1169 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
1174 xstats->backlog[i] =
1175 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1176 xstats->tail_drop[i] =
1177 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
1180 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1181 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
1186 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
1187 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
1191 static void update_stats_cache(struct work_struct *work)
1193 struct mlxsw_sp_port *mlxsw_sp_port =
1194 container_of(work, struct mlxsw_sp_port,
1195 periodic_hw_stats.update_dw.work);
1197 if (!netif_carrier_ok(mlxsw_sp_port->dev))
1198 /* Note: mlxsw_sp_port_down_wipe_counters() clears the cache as
1199 * necessary when port goes down.
1203 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
1204 &mlxsw_sp_port->periodic_hw_stats.stats);
1205 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
1206 &mlxsw_sp_port->periodic_hw_stats.xstats);
1209 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1210 MLXSW_HW_STATS_UPDATE_TIME);
1213 /* Return the stats from a cache that is updated periodically,
1214 * as this function might get called in an atomic context.
1217 mlxsw_sp_port_get_stats64(struct net_device *dev,
1218 struct rtnl_link_stats64 *stats)
1220 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1222 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
1225 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1226 u16 vid_begin, u16 vid_end,
1227 bool is_member, bool untagged)
1229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1233 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1237 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1238 vid_end, is_member, untagged);
1239 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1244 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1245 u16 vid_end, bool is_member, bool untagged)
1250 for (vid = vid_begin; vid <= vid_end;
1251 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1252 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1255 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1256 is_member, untagged);
1264 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1267 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1269 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1270 &mlxsw_sp_port->vlans_list, list) {
1271 if (!flush_default &&
1272 mlxsw_sp_port_vlan->vid == MLXSW_SP_DEFAULT_VID)
1274 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1279 mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1281 if (mlxsw_sp_port_vlan->bridge_port)
1282 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1283 else if (mlxsw_sp_port_vlan->fid)
1284 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1287 struct mlxsw_sp_port_vlan *
1288 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1290 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1291 bool untagged = vid == MLXSW_SP_DEFAULT_VID;
1294 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1295 if (mlxsw_sp_port_vlan)
1296 return ERR_PTR(-EEXIST);
1298 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1300 return ERR_PTR(err);
1302 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1303 if (!mlxsw_sp_port_vlan) {
1305 goto err_port_vlan_alloc;
1308 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1309 mlxsw_sp_port_vlan->vid = vid;
1310 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1312 return mlxsw_sp_port_vlan;
1314 err_port_vlan_alloc:
1315 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1316 return ERR_PTR(err);
1319 void mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1321 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1322 u16 vid = mlxsw_sp_port_vlan->vid;
1324 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port_vlan);
1325 list_del(&mlxsw_sp_port_vlan->list);
1326 kfree(mlxsw_sp_port_vlan);
1327 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1330 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1331 __be16 __always_unused proto, u16 vid)
1333 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1335 /* VLAN 0 is added to HW filter when device goes up, but it is
1336 * reserved in our case, so simply return.
1341 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid));
1344 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1345 __be16 __always_unused proto, u16 vid)
1347 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1348 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1350 /* VLAN 0 is removed from HW filter when device goes down, but
1351 * it is reserved in our case, so simply return.
1356 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1357 if (!mlxsw_sp_port_vlan)
1359 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1364 static struct mlxsw_sp_port_mall_tc_entry *
1365 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1366 unsigned long cookie) {
1367 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1369 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1370 if (mall_tc_entry->cookie == cookie)
1371 return mall_tc_entry;
1377 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1378 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1379 const struct flow_action_entry *act,
1382 enum mlxsw_sp_span_type span_type;
1385 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1389 mirror->ingress = ingress;
1390 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1391 return mlxsw_sp_span_mirror_add(mlxsw_sp_port, act->dev, span_type,
1392 true, &mirror->span_id);
1396 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1397 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1399 enum mlxsw_sp_span_type span_type;
1401 span_type = mirror->ingress ?
1402 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1403 mlxsw_sp_span_mirror_del(mlxsw_sp_port, mirror->span_id,
1408 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1409 struct tc_cls_matchall_offload *cls,
1410 const struct flow_action_entry *act,
1415 if (!mlxsw_sp_port->sample)
1417 if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1418 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1421 if (act->sample.rate > MLXSW_REG_MPSC_RATE_MAX) {
1422 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1426 rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1427 act->sample.psample_group);
1428 mlxsw_sp_port->sample->truncate = act->sample.truncate;
1429 mlxsw_sp_port->sample->trunc_size = act->sample.trunc_size;
1430 mlxsw_sp_port->sample->rate = act->sample.rate;
1432 err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, act->sample.rate);
1434 goto err_port_sample_set;
1437 err_port_sample_set:
1438 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1443 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1445 if (!mlxsw_sp_port->sample)
1448 mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1449 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1452 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1453 struct tc_cls_matchall_offload *f,
1456 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1457 __be16 protocol = f->common.protocol;
1458 struct flow_action_entry *act;
1461 if (!flow_offload_has_one_action(&f->rule->action)) {
1462 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1466 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1469 mall_tc_entry->cookie = f->cookie;
1471 act = &f->rule->action.entries[0];
1473 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
1474 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1476 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1477 mirror = &mall_tc_entry->mirror;
1478 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1481 } else if (act->id == FLOW_ACTION_SAMPLE &&
1482 protocol == htons(ETH_P_ALL)) {
1483 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1484 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, f,
1491 goto err_add_action;
1493 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1497 kfree(mall_tc_entry);
1501 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1502 struct tc_cls_matchall_offload *f)
1504 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1506 mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1508 if (!mall_tc_entry) {
1509 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1512 list_del(&mall_tc_entry->list);
1514 switch (mall_tc_entry->type) {
1515 case MLXSW_SP_PORT_MALL_MIRROR:
1516 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1517 &mall_tc_entry->mirror);
1519 case MLXSW_SP_PORT_MALL_SAMPLE:
1520 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1526 kfree(mall_tc_entry);
1529 static int mlxsw_sp_setup_tc_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1530 struct tc_cls_matchall_offload *f,
1533 switch (f->command) {
1534 case TC_CLSMATCHALL_REPLACE:
1535 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, f,
1537 case TC_CLSMATCHALL_DESTROY:
1538 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, f);
1546 mlxsw_sp_setup_tc_cls_flower(struct mlxsw_sp_acl_block *acl_block,
1547 struct flow_cls_offload *f)
1549 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_acl_block_mlxsw_sp(acl_block);
1551 switch (f->command) {
1552 case FLOW_CLS_REPLACE:
1553 return mlxsw_sp_flower_replace(mlxsw_sp, acl_block, f);
1554 case FLOW_CLS_DESTROY:
1555 mlxsw_sp_flower_destroy(mlxsw_sp, acl_block, f);
1557 case FLOW_CLS_STATS:
1558 return mlxsw_sp_flower_stats(mlxsw_sp, acl_block, f);
1559 case FLOW_CLS_TMPLT_CREATE:
1560 return mlxsw_sp_flower_tmplt_create(mlxsw_sp, acl_block, f);
1561 case FLOW_CLS_TMPLT_DESTROY:
1562 mlxsw_sp_flower_tmplt_destroy(mlxsw_sp, acl_block, f);
1569 static int mlxsw_sp_setup_tc_block_cb_matchall(enum tc_setup_type type,
1571 void *cb_priv, bool ingress)
1573 struct mlxsw_sp_port *mlxsw_sp_port = cb_priv;
1576 case TC_SETUP_CLSMATCHALL:
1577 if (!tc_cls_can_offload_and_chain0(mlxsw_sp_port->dev,
1581 return mlxsw_sp_setup_tc_cls_matchall(mlxsw_sp_port, type_data,
1583 case TC_SETUP_CLSFLOWER:
1590 static int mlxsw_sp_setup_tc_block_cb_matchall_ig(enum tc_setup_type type,
1594 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1598 static int mlxsw_sp_setup_tc_block_cb_matchall_eg(enum tc_setup_type type,
1602 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1606 static int mlxsw_sp_setup_tc_block_cb_flower(enum tc_setup_type type,
1607 void *type_data, void *cb_priv)
1609 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1612 case TC_SETUP_CLSMATCHALL:
1614 case TC_SETUP_CLSFLOWER:
1615 if (mlxsw_sp_acl_block_disabled(acl_block))
1618 return mlxsw_sp_setup_tc_cls_flower(acl_block, type_data);
1624 static void mlxsw_sp_tc_block_flower_release(void *cb_priv)
1626 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1628 mlxsw_sp_acl_block_destroy(acl_block);
1631 static LIST_HEAD(mlxsw_sp_block_cb_list);
1634 mlxsw_sp_setup_tc_block_flower_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1635 struct flow_block_offload *f, bool ingress)
1637 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1638 struct mlxsw_sp_acl_block *acl_block;
1639 struct flow_block_cb *block_cb;
1640 bool register_block = false;
1643 block_cb = flow_block_cb_lookup(f->block,
1644 mlxsw_sp_setup_tc_block_cb_flower,
1647 acl_block = mlxsw_sp_acl_block_create(mlxsw_sp, f->net);
1650 block_cb = flow_block_cb_alloc(mlxsw_sp_setup_tc_block_cb_flower,
1651 mlxsw_sp, acl_block,
1652 mlxsw_sp_tc_block_flower_release);
1653 if (IS_ERR(block_cb)) {
1654 mlxsw_sp_acl_block_destroy(acl_block);
1655 err = PTR_ERR(block_cb);
1656 goto err_cb_register;
1658 register_block = true;
1660 acl_block = flow_block_cb_priv(block_cb);
1662 flow_block_cb_incref(block_cb);
1663 err = mlxsw_sp_acl_block_bind(mlxsw_sp, acl_block,
1664 mlxsw_sp_port, ingress, f->extack);
1666 goto err_block_bind;
1669 mlxsw_sp_port->ing_acl_block = acl_block;
1671 mlxsw_sp_port->eg_acl_block = acl_block;
1673 if (register_block) {
1674 flow_block_cb_add(block_cb, f);
1675 list_add_tail(&block_cb->driver_list, &mlxsw_sp_block_cb_list);
1681 if (!flow_block_cb_decref(block_cb))
1682 flow_block_cb_free(block_cb);
1688 mlxsw_sp_setup_tc_block_flower_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1689 struct flow_block_offload *f, bool ingress)
1691 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1692 struct mlxsw_sp_acl_block *acl_block;
1693 struct flow_block_cb *block_cb;
1696 block_cb = flow_block_cb_lookup(f->block,
1697 mlxsw_sp_setup_tc_block_cb_flower,
1703 mlxsw_sp_port->ing_acl_block = NULL;
1705 mlxsw_sp_port->eg_acl_block = NULL;
1707 acl_block = flow_block_cb_priv(block_cb);
1708 err = mlxsw_sp_acl_block_unbind(mlxsw_sp, acl_block,
1709 mlxsw_sp_port, ingress);
1710 if (!err && !flow_block_cb_decref(block_cb)) {
1711 flow_block_cb_remove(block_cb, f);
1712 list_del(&block_cb->driver_list);
1716 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1717 struct flow_block_offload *f)
1719 struct flow_block_cb *block_cb;
1720 flow_setup_cb_t *cb;
1724 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1725 cb = mlxsw_sp_setup_tc_block_cb_matchall_ig;
1727 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1728 cb = mlxsw_sp_setup_tc_block_cb_matchall_eg;
1734 f->driver_block_list = &mlxsw_sp_block_cb_list;
1736 switch (f->command) {
1737 case FLOW_BLOCK_BIND:
1738 if (flow_block_cb_is_busy(cb, mlxsw_sp_port,
1739 &mlxsw_sp_block_cb_list))
1742 block_cb = flow_block_cb_alloc(cb, mlxsw_sp_port,
1743 mlxsw_sp_port, NULL);
1744 if (IS_ERR(block_cb))
1745 return PTR_ERR(block_cb);
1746 err = mlxsw_sp_setup_tc_block_flower_bind(mlxsw_sp_port, f,
1749 flow_block_cb_free(block_cb);
1752 flow_block_cb_add(block_cb, f);
1753 list_add_tail(&block_cb->driver_list, &mlxsw_sp_block_cb_list);
1755 case FLOW_BLOCK_UNBIND:
1756 mlxsw_sp_setup_tc_block_flower_unbind(mlxsw_sp_port,
1758 block_cb = flow_block_cb_lookup(f->block, cb, mlxsw_sp_port);
1762 flow_block_cb_remove(block_cb, f);
1763 list_del(&block_cb->driver_list);
1770 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1773 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1776 case TC_SETUP_BLOCK:
1777 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1778 case TC_SETUP_QDISC_RED:
1779 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1780 case TC_SETUP_QDISC_PRIO:
1781 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1782 case TC_SETUP_QDISC_ETS:
1783 return mlxsw_sp_setup_tc_ets(mlxsw_sp_port, type_data);
1784 case TC_SETUP_QDISC_TBF:
1785 return mlxsw_sp_setup_tc_tbf(mlxsw_sp_port, type_data);
1792 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1794 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1797 if (mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->ing_acl_block) ||
1798 mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->eg_acl_block) ||
1799 !list_empty(&mlxsw_sp_port->mall_tc_list)) {
1800 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1803 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->ing_acl_block);
1804 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->eg_acl_block);
1806 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->ing_acl_block);
1807 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->eg_acl_block);
1812 static int mlxsw_sp_feature_loopback(struct net_device *dev, bool enable)
1814 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1815 char pplr_pl[MLXSW_REG_PPLR_LEN];
1818 if (netif_running(dev))
1819 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1821 mlxsw_reg_pplr_pack(pplr_pl, mlxsw_sp_port->local_port, enable);
1822 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pplr),
1825 if (netif_running(dev))
1826 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1831 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1833 static int mlxsw_sp_handle_feature(struct net_device *dev,
1834 netdev_features_t wanted_features,
1835 netdev_features_t feature,
1836 mlxsw_sp_feature_handler feature_handler)
1838 netdev_features_t changes = wanted_features ^ dev->features;
1839 bool enable = !!(wanted_features & feature);
1842 if (!(changes & feature))
1845 err = feature_handler(dev, enable);
1847 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1848 enable ? "Enable" : "Disable", &feature, err);
1853 dev->features |= feature;
1855 dev->features &= ~feature;
1859 static int mlxsw_sp_set_features(struct net_device *dev,
1860 netdev_features_t features)
1862 netdev_features_t oper_features = dev->features;
1865 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1866 mlxsw_sp_feature_hw_tc);
1867 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_LOOPBACK,
1868 mlxsw_sp_feature_loopback);
1871 dev->features = oper_features;
1878 static struct devlink_port *
1879 mlxsw_sp_port_get_devlink_port(struct net_device *dev)
1881 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1882 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1884 return mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
1885 mlxsw_sp_port->local_port);
1888 static int mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1891 struct hwtstamp_config config;
1894 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1897 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port,
1902 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1908 static int mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1911 struct hwtstamp_config config;
1914 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_get(mlxsw_sp_port,
1919 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1925 static inline void mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port *mlxsw_sp_port)
1927 struct hwtstamp_config config = {0};
1929 mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port, &config);
1933 mlxsw_sp_port_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1935 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1939 return mlxsw_sp_port_hwtstamp_set(mlxsw_sp_port, ifr);
1941 return mlxsw_sp_port_hwtstamp_get(mlxsw_sp_port, ifr);
1947 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1948 .ndo_open = mlxsw_sp_port_open,
1949 .ndo_stop = mlxsw_sp_port_stop,
1950 .ndo_start_xmit = mlxsw_sp_port_xmit,
1951 .ndo_setup_tc = mlxsw_sp_setup_tc,
1952 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1953 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1954 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1955 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1956 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1957 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1958 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1959 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1960 .ndo_set_features = mlxsw_sp_set_features,
1961 .ndo_get_devlink_port = mlxsw_sp_port_get_devlink_port,
1962 .ndo_do_ioctl = mlxsw_sp_port_ioctl,
1965 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1966 struct ethtool_drvinfo *drvinfo)
1968 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1969 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1971 strlcpy(drvinfo->driver, mlxsw_sp->bus_info->device_kind,
1972 sizeof(drvinfo->driver));
1973 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1974 sizeof(drvinfo->version));
1975 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1977 mlxsw_sp->bus_info->fw_rev.major,
1978 mlxsw_sp->bus_info->fw_rev.minor,
1979 mlxsw_sp->bus_info->fw_rev.subminor);
1980 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1981 sizeof(drvinfo->bus_info));
1984 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1985 struct ethtool_pauseparam *pause)
1987 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1989 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1990 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1993 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1994 struct ethtool_pauseparam *pause)
1996 char pfcc_pl[MLXSW_REG_PFCC_LEN];
1998 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1999 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
2000 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
2002 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
2006 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
2007 struct ethtool_pauseparam *pause)
2009 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2010 bool pause_en = pause->tx_pause || pause->rx_pause;
2013 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
2014 netdev_err(dev, "PFC already enabled on port\n");
2018 if (pause->autoneg) {
2019 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
2023 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
2025 netdev_err(dev, "Failed to configure port's headroom\n");
2029 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
2031 netdev_err(dev, "Failed to set PAUSE parameters\n");
2032 goto err_port_pause_configure;
2035 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
2036 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
2040 err_port_pause_configure:
2041 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
2042 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
2046 struct mlxsw_sp_port_hw_stats {
2047 char str[ETH_GSTRING_LEN];
2048 u64 (*getter)(const char *payload);
2052 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
2054 .str = "a_frames_transmitted_ok",
2055 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
2058 .str = "a_frames_received_ok",
2059 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
2062 .str = "a_frame_check_sequence_errors",
2063 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
2066 .str = "a_alignment_errors",
2067 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
2070 .str = "a_octets_transmitted_ok",
2071 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
2074 .str = "a_octets_received_ok",
2075 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
2078 .str = "a_multicast_frames_xmitted_ok",
2079 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
2082 .str = "a_broadcast_frames_xmitted_ok",
2083 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
2086 .str = "a_multicast_frames_received_ok",
2087 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
2090 .str = "a_broadcast_frames_received_ok",
2091 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
2094 .str = "a_in_range_length_errors",
2095 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
2098 .str = "a_out_of_range_length_field",
2099 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
2102 .str = "a_frame_too_long_errors",
2103 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
2106 .str = "a_symbol_error_during_carrier",
2107 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
2110 .str = "a_mac_control_frames_transmitted",
2111 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
2114 .str = "a_mac_control_frames_received",
2115 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
2118 .str = "a_unsupported_opcodes_received",
2119 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
2122 .str = "a_pause_mac_ctrl_frames_received",
2123 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
2126 .str = "a_pause_mac_ctrl_frames_xmitted",
2127 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
2131 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
2133 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2863_stats[] = {
2135 .str = "if_in_discards",
2136 .getter = mlxsw_reg_ppcnt_if_in_discards_get,
2139 .str = "if_out_discards",
2140 .getter = mlxsw_reg_ppcnt_if_out_discards_get,
2143 .str = "if_out_errors",
2144 .getter = mlxsw_reg_ppcnt_if_out_errors_get,
2148 #define MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN \
2149 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2863_stats)
2151 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_2819_stats[] = {
2153 .str = "ether_stats_undersize_pkts",
2154 .getter = mlxsw_reg_ppcnt_ether_stats_undersize_pkts_get,
2157 .str = "ether_stats_oversize_pkts",
2158 .getter = mlxsw_reg_ppcnt_ether_stats_oversize_pkts_get,
2161 .str = "ether_stats_fragments",
2162 .getter = mlxsw_reg_ppcnt_ether_stats_fragments_get,
2165 .str = "ether_pkts64octets",
2166 .getter = mlxsw_reg_ppcnt_ether_stats_pkts64octets_get,
2169 .str = "ether_pkts65to127octets",
2170 .getter = mlxsw_reg_ppcnt_ether_stats_pkts65to127octets_get,
2173 .str = "ether_pkts128to255octets",
2174 .getter = mlxsw_reg_ppcnt_ether_stats_pkts128to255octets_get,
2177 .str = "ether_pkts256to511octets",
2178 .getter = mlxsw_reg_ppcnt_ether_stats_pkts256to511octets_get,
2181 .str = "ether_pkts512to1023octets",
2182 .getter = mlxsw_reg_ppcnt_ether_stats_pkts512to1023octets_get,
2185 .str = "ether_pkts1024to1518octets",
2186 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1024to1518octets_get,
2189 .str = "ether_pkts1519to2047octets",
2190 .getter = mlxsw_reg_ppcnt_ether_stats_pkts1519to2047octets_get,
2193 .str = "ether_pkts2048to4095octets",
2194 .getter = mlxsw_reg_ppcnt_ether_stats_pkts2048to4095octets_get,
2197 .str = "ether_pkts4096to8191octets",
2198 .getter = mlxsw_reg_ppcnt_ether_stats_pkts4096to8191octets_get,
2201 .str = "ether_pkts8192to10239octets",
2202 .getter = mlxsw_reg_ppcnt_ether_stats_pkts8192to10239octets_get,
2206 #define MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN \
2207 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_2819_stats)
2209 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_rfc_3635_stats[] = {
2211 .str = "dot3stats_fcs_errors",
2212 .getter = mlxsw_reg_ppcnt_dot3stats_fcs_errors_get,
2215 .str = "dot3stats_symbol_errors",
2216 .getter = mlxsw_reg_ppcnt_dot3stats_symbol_errors_get,
2219 .str = "dot3control_in_unknown_opcodes",
2220 .getter = mlxsw_reg_ppcnt_dot3control_in_unknown_opcodes_get,
2223 .str = "dot3in_pause_frames",
2224 .getter = mlxsw_reg_ppcnt_dot3in_pause_frames_get,
2228 #define MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN \
2229 ARRAY_SIZE(mlxsw_sp_port_hw_rfc_3635_stats)
2231 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_ext_stats[] = {
2233 .str = "ecn_marked",
2234 .getter = mlxsw_reg_ppcnt_ecn_marked_get,
2238 #define MLXSW_SP_PORT_HW_EXT_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_ext_stats)
2240 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_discard_stats[] = {
2242 .str = "discard_ingress_general",
2243 .getter = mlxsw_reg_ppcnt_ingress_general_get,
2246 .str = "discard_ingress_policy_engine",
2247 .getter = mlxsw_reg_ppcnt_ingress_policy_engine_get,
2250 .str = "discard_ingress_vlan_membership",
2251 .getter = mlxsw_reg_ppcnt_ingress_vlan_membership_get,
2254 .str = "discard_ingress_tag_frame_type",
2255 .getter = mlxsw_reg_ppcnt_ingress_tag_frame_type_get,
2258 .str = "discard_egress_vlan_membership",
2259 .getter = mlxsw_reg_ppcnt_egress_vlan_membership_get,
2262 .str = "discard_loopback_filter",
2263 .getter = mlxsw_reg_ppcnt_loopback_filter_get,
2266 .str = "discard_egress_general",
2267 .getter = mlxsw_reg_ppcnt_egress_general_get,
2270 .str = "discard_egress_hoq",
2271 .getter = mlxsw_reg_ppcnt_egress_hoq_get,
2274 .str = "discard_egress_policy_engine",
2275 .getter = mlxsw_reg_ppcnt_egress_policy_engine_get,
2278 .str = "discard_ingress_tx_link_down",
2279 .getter = mlxsw_reg_ppcnt_ingress_tx_link_down_get,
2282 .str = "discard_egress_stp_filter",
2283 .getter = mlxsw_reg_ppcnt_egress_stp_filter_get,
2286 .str = "discard_egress_sll",
2287 .getter = mlxsw_reg_ppcnt_egress_sll_get,
2291 #define MLXSW_SP_PORT_HW_DISCARD_STATS_LEN \
2292 ARRAY_SIZE(mlxsw_sp_port_hw_discard_stats)
2294 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
2296 .str = "rx_octets_prio",
2297 .getter = mlxsw_reg_ppcnt_rx_octets_get,
2300 .str = "rx_frames_prio",
2301 .getter = mlxsw_reg_ppcnt_rx_frames_get,
2304 .str = "tx_octets_prio",
2305 .getter = mlxsw_reg_ppcnt_tx_octets_get,
2308 .str = "tx_frames_prio",
2309 .getter = mlxsw_reg_ppcnt_tx_frames_get,
2312 .str = "rx_pause_prio",
2313 .getter = mlxsw_reg_ppcnt_rx_pause_get,
2316 .str = "rx_pause_duration_prio",
2317 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
2320 .str = "tx_pause_prio",
2321 .getter = mlxsw_reg_ppcnt_tx_pause_get,
2324 .str = "tx_pause_duration_prio",
2325 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
2329 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
2331 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
2333 .str = "tc_transmit_queue_tc",
2334 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_get,
2335 .cells_bytes = true,
2338 .str = "tc_no_buffer_discard_uc_tc",
2339 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
2343 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
2345 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
2346 MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN + \
2347 MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN + \
2348 MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN + \
2349 MLXSW_SP_PORT_HW_EXT_STATS_LEN + \
2350 MLXSW_SP_PORT_HW_DISCARD_STATS_LEN + \
2351 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN * \
2352 IEEE_8021QAZ_MAX_TCS) + \
2353 (MLXSW_SP_PORT_HW_TC_STATS_LEN * \
2356 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
2360 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
2361 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2362 mlxsw_sp_port_hw_prio_stats[i].str, prio);
2363 *p += ETH_GSTRING_LEN;
2367 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
2371 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
2372 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
2373 mlxsw_sp_port_hw_tc_stats[i].str, tc);
2374 *p += ETH_GSTRING_LEN;
2378 static void mlxsw_sp_port_get_strings(struct net_device *dev,
2379 u32 stringset, u8 *data)
2381 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2385 switch (stringset) {
2387 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
2388 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
2390 p += ETH_GSTRING_LEN;
2393 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN; i++) {
2394 memcpy(p, mlxsw_sp_port_hw_rfc_2863_stats[i].str,
2396 p += ETH_GSTRING_LEN;
2399 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN; i++) {
2400 memcpy(p, mlxsw_sp_port_hw_rfc_2819_stats[i].str,
2402 p += ETH_GSTRING_LEN;
2405 for (i = 0; i < MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN; i++) {
2406 memcpy(p, mlxsw_sp_port_hw_rfc_3635_stats[i].str,
2408 p += ETH_GSTRING_LEN;
2411 for (i = 0; i < MLXSW_SP_PORT_HW_EXT_STATS_LEN; i++) {
2412 memcpy(p, mlxsw_sp_port_hw_ext_stats[i].str,
2414 p += ETH_GSTRING_LEN;
2417 for (i = 0; i < MLXSW_SP_PORT_HW_DISCARD_STATS_LEN; i++) {
2418 memcpy(p, mlxsw_sp_port_hw_discard_stats[i].str,
2420 p += ETH_GSTRING_LEN;
2423 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2424 mlxsw_sp_port_get_prio_strings(&p, i);
2426 for (i = 0; i < TC_MAX_QUEUE; i++)
2427 mlxsw_sp_port_get_tc_strings(&p, i);
2429 mlxsw_sp_port->mlxsw_sp->ptp_ops->get_stats_strings(&p);
2434 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
2435 enum ethtool_phys_id_state state)
2437 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2438 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2439 char mlcr_pl[MLXSW_REG_MLCR_LEN];
2443 case ETHTOOL_ID_ACTIVE:
2446 case ETHTOOL_ID_INACTIVE:
2453 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
2454 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
2458 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
2459 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
2462 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
2463 *p_hw_stats = mlxsw_sp_port_hw_stats;
2464 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
2466 case MLXSW_REG_PPCNT_RFC_2863_CNT:
2467 *p_hw_stats = mlxsw_sp_port_hw_rfc_2863_stats;
2468 *p_len = MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2470 case MLXSW_REG_PPCNT_RFC_2819_CNT:
2471 *p_hw_stats = mlxsw_sp_port_hw_rfc_2819_stats;
2472 *p_len = MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2474 case MLXSW_REG_PPCNT_RFC_3635_CNT:
2475 *p_hw_stats = mlxsw_sp_port_hw_rfc_3635_stats;
2476 *p_len = MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2478 case MLXSW_REG_PPCNT_EXT_CNT:
2479 *p_hw_stats = mlxsw_sp_port_hw_ext_stats;
2480 *p_len = MLXSW_SP_PORT_HW_EXT_STATS_LEN;
2482 case MLXSW_REG_PPCNT_DISCARD_CNT:
2483 *p_hw_stats = mlxsw_sp_port_hw_discard_stats;
2484 *p_len = MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2486 case MLXSW_REG_PPCNT_PRIO_CNT:
2487 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
2488 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2490 case MLXSW_REG_PPCNT_TC_CNT:
2491 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
2492 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
2501 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
2502 enum mlxsw_reg_ppcnt_grp grp, int prio,
2503 u64 *data, int data_index)
2505 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2506 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2507 struct mlxsw_sp_port_hw_stats *hw_stats;
2508 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
2512 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
2515 mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
2516 for (i = 0; i < len; i++) {
2517 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
2518 if (!hw_stats[i].cells_bytes)
2520 data[data_index + i] = mlxsw_sp_cells_bytes(mlxsw_sp,
2521 data[data_index + i]);
2525 static void mlxsw_sp_port_get_stats(struct net_device *dev,
2526 struct ethtool_stats *stats, u64 *data)
2528 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2529 int i, data_index = 0;
2531 /* IEEE 802.3 Counters */
2532 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
2534 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
2536 /* RFC 2863 Counters */
2537 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2863_CNT, 0,
2539 data_index += MLXSW_SP_PORT_HW_RFC_2863_STATS_LEN;
2541 /* RFC 2819 Counters */
2542 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_2819_CNT, 0,
2544 data_index += MLXSW_SP_PORT_HW_RFC_2819_STATS_LEN;
2546 /* RFC 3635 Counters */
2547 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_RFC_3635_CNT, 0,
2549 data_index += MLXSW_SP_PORT_HW_RFC_3635_STATS_LEN;
2551 /* Extended Counters */
2552 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
2554 data_index += MLXSW_SP_PORT_HW_EXT_STATS_LEN;
2556 /* Discard Counters */
2557 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_DISCARD_CNT, 0,
2559 data_index += MLXSW_SP_PORT_HW_DISCARD_STATS_LEN;
2561 /* Per-Priority Counters */
2562 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2563 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
2565 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2568 /* Per-TC Counters */
2569 for (i = 0; i < TC_MAX_QUEUE; i++) {
2570 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
2572 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
2576 mlxsw_sp_port->mlxsw_sp->ptp_ops->get_stats(mlxsw_sp_port,
2578 data_index += mlxsw_sp_port->mlxsw_sp->ptp_ops->get_stats_count();
2581 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
2583 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2587 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN +
2588 mlxsw_sp_port->mlxsw_sp->ptp_ops->get_stats_count();
2594 struct mlxsw_sp1_port_link_mode {
2595 enum ethtool_link_mode_bit_indices mask_ethtool;
2600 static const struct mlxsw_sp1_port_link_mode mlxsw_sp1_port_link_mode[] = {
2602 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
2603 .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2607 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
2608 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
2609 .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2610 .speed = SPEED_1000,
2613 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
2614 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2615 .speed = SPEED_10000,
2618 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
2619 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
2620 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
2621 .speed = SPEED_10000,
2624 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2625 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2626 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2627 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
2628 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2629 .speed = SPEED_10000,
2632 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
2633 .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
2634 .speed = SPEED_20000,
2637 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
2638 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2639 .speed = SPEED_40000,
2642 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
2643 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2644 .speed = SPEED_40000,
2647 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
2648 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2649 .speed = SPEED_40000,
2652 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
2653 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2654 .speed = SPEED_40000,
2657 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
2658 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2659 .speed = SPEED_25000,
2662 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
2663 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2664 .speed = SPEED_25000,
2667 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2668 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2669 .speed = SPEED_25000,
2672 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
2673 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2674 .speed = SPEED_50000,
2677 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
2678 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2679 .speed = SPEED_50000,
2682 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
2683 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2684 .speed = SPEED_50000,
2687 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
2688 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2689 .speed = SPEED_100000,
2692 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
2693 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2694 .speed = SPEED_100000,
2697 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
2698 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2699 .speed = SPEED_100000,
2702 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
2703 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2704 .speed = SPEED_100000,
2708 #define MLXSW_SP1_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp1_port_link_mode)
2711 mlxsw_sp1_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
2713 struct ethtool_link_ksettings *cmd)
2715 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2716 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2717 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2718 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2719 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2720 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2721 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2723 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2724 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2725 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2726 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
2727 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
2728 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2732 mlxsw_sp1_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
2733 u8 width, unsigned long *mode)
2737 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2738 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask)
2739 __set_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2745 mlxsw_sp1_from_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto)
2749 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2750 if (ptys_eth_proto & mlxsw_sp1_port_link_mode[i].mask)
2751 return mlxsw_sp1_port_link_mode[i].speed;
2754 return SPEED_UNKNOWN;
2758 mlxsw_sp1_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
2760 struct ethtool_link_ksettings *cmd)
2762 cmd->base.speed = SPEED_UNKNOWN;
2763 cmd->base.duplex = DUPLEX_UNKNOWN;
2768 cmd->base.speed = mlxsw_sp1_from_ptys_speed(mlxsw_sp, ptys_eth_proto);
2769 if (cmd->base.speed != SPEED_UNKNOWN)
2770 cmd->base.duplex = DUPLEX_FULL;
2774 mlxsw_sp1_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp, u8 width,
2775 const struct ethtool_link_ksettings *cmd)
2780 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2781 if (test_bit(mlxsw_sp1_port_link_mode[i].mask_ethtool,
2782 cmd->link_modes.advertising))
2783 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2788 static u32 mlxsw_sp1_to_ptys_speed(struct mlxsw_sp *mlxsw_sp, u8 width,
2794 for (i = 0; i < MLXSW_SP1_PORT_LINK_MODE_LEN; i++) {
2795 if (speed == mlxsw_sp1_port_link_mode[i].speed)
2796 ptys_proto |= mlxsw_sp1_port_link_mode[i].mask;
2802 mlxsw_sp1_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
2803 u8 local_port, u32 proto_admin, bool autoneg)
2805 mlxsw_reg_ptys_eth_pack(payload, local_port, proto_admin, autoneg);
2809 mlxsw_sp1_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
2810 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
2811 u32 *p_eth_proto_oper)
2813 mlxsw_reg_ptys_eth_unpack(payload, p_eth_proto_cap, p_eth_proto_admin,
2817 static const struct mlxsw_sp_port_type_speed_ops
2818 mlxsw_sp1_port_type_speed_ops = {
2819 .from_ptys_supported_port = mlxsw_sp1_from_ptys_supported_port,
2820 .from_ptys_link = mlxsw_sp1_from_ptys_link,
2821 .from_ptys_speed = mlxsw_sp1_from_ptys_speed,
2822 .from_ptys_speed_duplex = mlxsw_sp1_from_ptys_speed_duplex,
2823 .to_ptys_advert_link = mlxsw_sp1_to_ptys_advert_link,
2824 .to_ptys_speed = mlxsw_sp1_to_ptys_speed,
2825 .reg_ptys_eth_pack = mlxsw_sp1_reg_ptys_eth_pack,
2826 .reg_ptys_eth_unpack = mlxsw_sp1_reg_ptys_eth_unpack,
2829 static const enum ethtool_link_mode_bit_indices
2830 mlxsw_sp2_mask_ethtool_sgmii_100m[] = {
2831 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2834 #define MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN \
2835 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_sgmii_100m)
2837 static const enum ethtool_link_mode_bit_indices
2838 mlxsw_sp2_mask_ethtool_1000base_x_sgmii[] = {
2839 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2840 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2843 #define MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN \
2844 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_1000base_x_sgmii)
2846 static const enum ethtool_link_mode_bit_indices
2847 mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii[] = {
2848 ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
2851 #define MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN \
2852 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii)
2854 static const enum ethtool_link_mode_bit_indices
2855 mlxsw_sp2_mask_ethtool_5gbase_r[] = {
2856 ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
2859 #define MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN \
2860 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_5gbase_r)
2862 static const enum ethtool_link_mode_bit_indices
2863 mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g[] = {
2864 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2865 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2866 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
2867 ETHTOOL_LINK_MODE_10000baseCR_Full_BIT,
2868 ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
2869 ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
2870 ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
2873 #define MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN \
2874 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g)
2876 static const enum ethtool_link_mode_bit_indices
2877 mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g[] = {
2878 ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2879 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2880 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2881 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2884 #define MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN \
2885 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g)
2887 static const enum ethtool_link_mode_bit_indices
2888 mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr[] = {
2889 ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2890 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2891 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2894 #define MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN \
2895 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr)
2897 static const enum ethtool_link_mode_bit_indices
2898 mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2[] = {
2899 ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2900 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2901 ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2904 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN \
2905 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2)
2907 static const enum ethtool_link_mode_bit_indices
2908 mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr[] = {
2909 ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
2910 ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
2911 ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
2912 ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
2913 ETHTOOL_LINK_MODE_50000baseDR_Full_BIT,
2916 #define MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN \
2917 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr)
2919 static const enum ethtool_link_mode_bit_indices
2920 mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4[] = {
2921 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2922 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2923 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2924 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2927 #define MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN \
2928 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4)
2930 static const enum ethtool_link_mode_bit_indices
2931 mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2[] = {
2932 ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT,
2933 ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT,
2934 ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT,
2935 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
2936 ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT,
2939 #define MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN \
2940 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2)
2942 static const enum ethtool_link_mode_bit_indices
2943 mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4[] = {
2944 ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT,
2945 ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT,
2946 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
2947 ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT,
2948 ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT,
2951 #define MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN \
2952 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4)
2954 static const enum ethtool_link_mode_bit_indices
2955 mlxsw_sp2_mask_ethtool_400gaui_8[] = {
2956 ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT,
2957 ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT,
2958 ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
2959 ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT,
2960 ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT,
2963 #define MLXSW_SP2_MASK_ETHTOOL_400GAUI_8_LEN \
2964 ARRAY_SIZE(mlxsw_sp2_mask_ethtool_400gaui_8)
2966 #define MLXSW_SP_PORT_MASK_WIDTH_1X BIT(0)
2967 #define MLXSW_SP_PORT_MASK_WIDTH_2X BIT(1)
2968 #define MLXSW_SP_PORT_MASK_WIDTH_4X BIT(2)
2969 #define MLXSW_SP_PORT_MASK_WIDTH_8X BIT(3)
2971 static u8 mlxsw_sp_port_mask_width_get(u8 width)
2975 return MLXSW_SP_PORT_MASK_WIDTH_1X;
2977 return MLXSW_SP_PORT_MASK_WIDTH_2X;
2979 return MLXSW_SP_PORT_MASK_WIDTH_4X;
2981 return MLXSW_SP_PORT_MASK_WIDTH_8X;
2988 struct mlxsw_sp2_port_link_mode {
2989 const enum ethtool_link_mode_bit_indices *mask_ethtool;
2996 static const struct mlxsw_sp2_port_link_mode mlxsw_sp2_port_link_mode[] = {
2998 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_SGMII_100M,
2999 .mask_ethtool = mlxsw_sp2_mask_ethtool_sgmii_100m,
3000 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_SGMII_100M_LEN,
3001 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3002 MLXSW_SP_PORT_MASK_WIDTH_2X |
3003 MLXSW_SP_PORT_MASK_WIDTH_4X |
3004 MLXSW_SP_PORT_MASK_WIDTH_8X,
3008 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_1000BASE_X_SGMII,
3009 .mask_ethtool = mlxsw_sp2_mask_ethtool_1000base_x_sgmii,
3010 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_1000BASE_X_SGMII_LEN,
3011 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3012 MLXSW_SP_PORT_MASK_WIDTH_2X |
3013 MLXSW_SP_PORT_MASK_WIDTH_4X |
3014 MLXSW_SP_PORT_MASK_WIDTH_8X,
3015 .speed = SPEED_1000,
3018 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_2_5GBASE_X_2_5GMII,
3019 .mask_ethtool = mlxsw_sp2_mask_ethtool_2_5gbase_x_2_5gmii,
3020 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_2_5GBASE_X_2_5GMII_LEN,
3021 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3022 MLXSW_SP_PORT_MASK_WIDTH_2X |
3023 MLXSW_SP_PORT_MASK_WIDTH_4X |
3024 MLXSW_SP_PORT_MASK_WIDTH_8X,
3025 .speed = SPEED_2500,
3028 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_5GBASE_R,
3029 .mask_ethtool = mlxsw_sp2_mask_ethtool_5gbase_r,
3030 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_5GBASE_R_LEN,
3031 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3032 MLXSW_SP_PORT_MASK_WIDTH_2X |
3033 MLXSW_SP_PORT_MASK_WIDTH_4X |
3034 MLXSW_SP_PORT_MASK_WIDTH_8X,
3035 .speed = SPEED_5000,
3038 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XFI_XAUI_1_10G,
3039 .mask_ethtool = mlxsw_sp2_mask_ethtool_xfi_xaui_1_10g,
3040 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XFI_XAUI_1_10G_LEN,
3041 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3042 MLXSW_SP_PORT_MASK_WIDTH_2X |
3043 MLXSW_SP_PORT_MASK_WIDTH_4X |
3044 MLXSW_SP_PORT_MASK_WIDTH_8X,
3045 .speed = SPEED_10000,
3048 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_XLAUI_4_XLPPI_4_40G,
3049 .mask_ethtool = mlxsw_sp2_mask_ethtool_xlaui_4_xlppi_4_40g,
3050 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_XLAUI_4_XLPPI_4_40G_LEN,
3051 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_4X |
3052 MLXSW_SP_PORT_MASK_WIDTH_8X,
3053 .speed = SPEED_40000,
3056 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_25GAUI_1_25GBASE_CR_KR,
3057 .mask_ethtool = mlxsw_sp2_mask_ethtool_25gaui_1_25gbase_cr_kr,
3058 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_25GAUI_1_25GBASE_CR_KR_LEN,
3059 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X |
3060 MLXSW_SP_PORT_MASK_WIDTH_2X |
3061 MLXSW_SP_PORT_MASK_WIDTH_4X |
3062 MLXSW_SP_PORT_MASK_WIDTH_8X,
3063 .speed = SPEED_25000,
3066 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_2_LAUI_2_50GBASE_CR2_KR2,
3067 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_2_laui_2_50gbase_cr2_kr2,
3068 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_2_LAUI_2_50GBASE_CR2_KR2_LEN,
3069 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_2X |
3070 MLXSW_SP_PORT_MASK_WIDTH_4X |
3071 MLXSW_SP_PORT_MASK_WIDTH_8X,
3072 .speed = SPEED_50000,
3075 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_50GAUI_1_LAUI_1_50GBASE_CR_KR,
3076 .mask_ethtool = mlxsw_sp2_mask_ethtool_50gaui_1_laui_1_50gbase_cr_kr,
3077 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_50GAUI_1_LAUI_1_50GBASE_CR_KR_LEN,
3078 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_1X,
3079 .speed = SPEED_50000,
3082 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_CAUI_4_100GBASE_CR4_KR4,
3083 .mask_ethtool = mlxsw_sp2_mask_ethtool_caui_4_100gbase_cr4_kr4,
3084 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_CAUI_4_100GBASE_CR4_KR4_LEN,
3085 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_4X |
3086 MLXSW_SP_PORT_MASK_WIDTH_8X,
3087 .speed = SPEED_100000,
3090 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_100GAUI_2_100GBASE_CR2_KR2,
3091 .mask_ethtool = mlxsw_sp2_mask_ethtool_100gaui_2_100gbase_cr2_kr2,
3092 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_100GAUI_2_100GBASE_CR2_KR2_LEN,
3093 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_2X,
3094 .speed = SPEED_100000,
3097 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_200GAUI_4_200GBASE_CR4_KR4,
3098 .mask_ethtool = mlxsw_sp2_mask_ethtool_200gaui_4_200gbase_cr4_kr4,
3099 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_200GAUI_4_200GBASE_CR4_KR4_LEN,
3100 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_4X |
3101 MLXSW_SP_PORT_MASK_WIDTH_8X,
3102 .speed = SPEED_200000,
3105 .mask = MLXSW_REG_PTYS_EXT_ETH_SPEED_400GAUI_8,
3106 .mask_ethtool = mlxsw_sp2_mask_ethtool_400gaui_8,
3107 .m_ethtool_len = MLXSW_SP2_MASK_ETHTOOL_400GAUI_8_LEN,
3108 .mask_width = MLXSW_SP_PORT_MASK_WIDTH_8X,
3109 .speed = SPEED_400000,
3113 #define MLXSW_SP2_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp2_port_link_mode)
3116 mlxsw_sp2_from_ptys_supported_port(struct mlxsw_sp *mlxsw_sp,
3118 struct ethtool_link_ksettings *cmd)
3120 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
3121 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
3125 mlxsw_sp2_set_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
3126 unsigned long *mode)
3130 for (i = 0; i < link_mode->m_ethtool_len; i++)
3131 __set_bit(link_mode->mask_ethtool[i], mode);
3135 mlxsw_sp2_from_ptys_link(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto,
3136 u8 width, unsigned long *mode)
3138 u8 mask_width = mlxsw_sp_port_mask_width_get(width);
3141 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3142 if ((ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask) &&
3143 (mask_width & mlxsw_sp2_port_link_mode[i].mask_width))
3144 mlxsw_sp2_set_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3150 mlxsw_sp2_from_ptys_speed(struct mlxsw_sp *mlxsw_sp, u32 ptys_eth_proto)
3154 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3155 if (ptys_eth_proto & mlxsw_sp2_port_link_mode[i].mask)
3156 return mlxsw_sp2_port_link_mode[i].speed;
3159 return SPEED_UNKNOWN;
3163 mlxsw_sp2_from_ptys_speed_duplex(struct mlxsw_sp *mlxsw_sp, bool carrier_ok,
3165 struct ethtool_link_ksettings *cmd)
3167 cmd->base.speed = SPEED_UNKNOWN;
3168 cmd->base.duplex = DUPLEX_UNKNOWN;
3173 cmd->base.speed = mlxsw_sp2_from_ptys_speed(mlxsw_sp, ptys_eth_proto);
3174 if (cmd->base.speed != SPEED_UNKNOWN)
3175 cmd->base.duplex = DUPLEX_FULL;
3179 mlxsw_sp2_test_bit_ethtool(const struct mlxsw_sp2_port_link_mode *link_mode,
3180 const unsigned long *mode)
3185 for (i = 0; i < link_mode->m_ethtool_len; i++) {
3186 if (test_bit(link_mode->mask_ethtool[i], mode))
3190 return cnt == link_mode->m_ethtool_len;
3194 mlxsw_sp2_to_ptys_advert_link(struct mlxsw_sp *mlxsw_sp, u8 width,
3195 const struct ethtool_link_ksettings *cmd)
3197 u8 mask_width = mlxsw_sp_port_mask_width_get(width);
3201 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3202 if ((mask_width & mlxsw_sp2_port_link_mode[i].mask_width) &&
3203 mlxsw_sp2_test_bit_ethtool(&mlxsw_sp2_port_link_mode[i],
3204 cmd->link_modes.advertising))
3205 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3210 static u32 mlxsw_sp2_to_ptys_speed(struct mlxsw_sp *mlxsw_sp,
3211 u8 width, u32 speed)
3213 u8 mask_width = mlxsw_sp_port_mask_width_get(width);
3217 for (i = 0; i < MLXSW_SP2_PORT_LINK_MODE_LEN; i++) {
3218 if ((speed == mlxsw_sp2_port_link_mode[i].speed) &&
3219 (mask_width & mlxsw_sp2_port_link_mode[i].mask_width))
3220 ptys_proto |= mlxsw_sp2_port_link_mode[i].mask;
3226 mlxsw_sp2_reg_ptys_eth_pack(struct mlxsw_sp *mlxsw_sp, char *payload,
3227 u8 local_port, u32 proto_admin,
3230 mlxsw_reg_ptys_ext_eth_pack(payload, local_port, proto_admin, autoneg);
3234 mlxsw_sp2_reg_ptys_eth_unpack(struct mlxsw_sp *mlxsw_sp, char *payload,
3235 u32 *p_eth_proto_cap, u32 *p_eth_proto_admin,
3236 u32 *p_eth_proto_oper)
3238 mlxsw_reg_ptys_ext_eth_unpack(payload, p_eth_proto_cap,
3239 p_eth_proto_admin, p_eth_proto_oper);
3242 static const struct mlxsw_sp_port_type_speed_ops
3243 mlxsw_sp2_port_type_speed_ops = {
3244 .from_ptys_supported_port = mlxsw_sp2_from_ptys_supported_port,
3245 .from_ptys_link = mlxsw_sp2_from_ptys_link,
3246 .from_ptys_speed = mlxsw_sp2_from_ptys_speed,
3247 .from_ptys_speed_duplex = mlxsw_sp2_from_ptys_speed_duplex,
3248 .to_ptys_advert_link = mlxsw_sp2_to_ptys_advert_link,
3249 .to_ptys_speed = mlxsw_sp2_to_ptys_speed,
3250 .reg_ptys_eth_pack = mlxsw_sp2_reg_ptys_eth_pack,
3251 .reg_ptys_eth_unpack = mlxsw_sp2_reg_ptys_eth_unpack,
3255 mlxsw_sp_port_get_link_supported(struct mlxsw_sp *mlxsw_sp, u32 eth_proto_cap,
3256 u8 width, struct ethtool_link_ksettings *cmd)
3258 const struct mlxsw_sp_port_type_speed_ops *ops;
3260 ops = mlxsw_sp->port_type_speed_ops;
3262 ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
3263 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
3264 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
3266 ops->from_ptys_supported_port(mlxsw_sp, eth_proto_cap, cmd);
3267 ops->from_ptys_link(mlxsw_sp, eth_proto_cap, width,
3268 cmd->link_modes.supported);
3272 mlxsw_sp_port_get_link_advertise(struct mlxsw_sp *mlxsw_sp,
3273 u32 eth_proto_admin, bool autoneg, u8 width,
3274 struct ethtool_link_ksettings *cmd)
3276 const struct mlxsw_sp_port_type_speed_ops *ops;
3278 ops = mlxsw_sp->port_type_speed_ops;
3283 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
3284 ops->from_ptys_link(mlxsw_sp, eth_proto_admin, width,
3285 cmd->link_modes.advertising);
3289 mlxsw_sp_port_connector_port(enum mlxsw_reg_ptys_connector_type connector_type)
3291 switch (connector_type) {
3292 case MLXSW_REG_PTYS_CONNECTOR_TYPE_UNKNOWN_OR_NO_CONNECTOR:
3294 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_NONE:
3296 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_TP:
3298 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_AUI:
3300 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_BNC:
3302 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_MII:
3304 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_FIBRE:
3306 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_DA:
3308 case MLXSW_REG_PTYS_CONNECTOR_TYPE_PORT_OTHER:
3316 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
3317 struct ethtool_link_ksettings *cmd)
3319 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
3320 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3321 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3322 const struct mlxsw_sp_port_type_speed_ops *ops;
3323 char ptys_pl[MLXSW_REG_PTYS_LEN];
3328 ops = mlxsw_sp->port_type_speed_ops;
3330 autoneg = mlxsw_sp_port->link.autoneg;
3331 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3333 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3336 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
3337 ð_proto_admin, ð_proto_oper);
3339 mlxsw_sp_port_get_link_supported(mlxsw_sp, eth_proto_cap,
3340 mlxsw_sp_port->mapping.width, cmd);
3342 mlxsw_sp_port_get_link_advertise(mlxsw_sp, eth_proto_admin, autoneg,
3343 mlxsw_sp_port->mapping.width, cmd);
3345 cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
3346 connector_type = mlxsw_reg_ptys_connector_type_get(ptys_pl);
3347 cmd->base.port = mlxsw_sp_port_connector_port(connector_type);
3348 ops->from_ptys_speed_duplex(mlxsw_sp, netif_carrier_ok(dev),
3349 eth_proto_oper, cmd);
3355 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
3356 const struct ethtool_link_ksettings *cmd)
3358 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
3359 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3360 const struct mlxsw_sp_port_type_speed_ops *ops;
3361 char ptys_pl[MLXSW_REG_PTYS_LEN];
3362 u32 eth_proto_cap, eth_proto_new;
3366 ops = mlxsw_sp->port_type_speed_ops;
3368 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3370 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3373 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap, NULL, NULL);
3375 autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
3376 eth_proto_new = autoneg ?
3377 ops->to_ptys_advert_link(mlxsw_sp, mlxsw_sp_port->mapping.width,
3379 ops->to_ptys_speed(mlxsw_sp, mlxsw_sp_port->mapping.width,
3382 eth_proto_new = eth_proto_new & eth_proto_cap;
3383 if (!eth_proto_new) {
3384 netdev_err(dev, "No supported speed requested\n");
3388 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3389 eth_proto_new, autoneg);
3390 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3394 mlxsw_sp_port->link.autoneg = autoneg;
3396 if (!netif_running(dev))
3399 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3400 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
3405 static int mlxsw_sp_get_module_info(struct net_device *netdev,
3406 struct ethtool_modinfo *modinfo)
3408 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3409 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3412 err = mlxsw_env_get_module_info(mlxsw_sp->core,
3413 mlxsw_sp_port->mapping.module,
3419 static int mlxsw_sp_get_module_eeprom(struct net_device *netdev,
3420 struct ethtool_eeprom *ee,
3423 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3424 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3427 err = mlxsw_env_get_module_eeprom(netdev, mlxsw_sp->core,
3428 mlxsw_sp_port->mapping.module, ee,
3435 mlxsw_sp_get_ts_info(struct net_device *netdev, struct ethtool_ts_info *info)
3437 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
3438 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3440 return mlxsw_sp->ptp_ops->get_ts_info(mlxsw_sp, info);
3443 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
3444 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
3445 .get_link = ethtool_op_get_link,
3446 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
3447 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
3448 .get_strings = mlxsw_sp_port_get_strings,
3449 .set_phys_id = mlxsw_sp_port_set_phys_id,
3450 .get_ethtool_stats = mlxsw_sp_port_get_stats,
3451 .get_sset_count = mlxsw_sp_port_get_sset_count,
3452 .get_link_ksettings = mlxsw_sp_port_get_link_ksettings,
3453 .set_link_ksettings = mlxsw_sp_port_set_link_ksettings,
3454 .get_module_info = mlxsw_sp_get_module_info,
3455 .get_module_eeprom = mlxsw_sp_get_module_eeprom,
3456 .get_ts_info = mlxsw_sp_get_ts_info,
3460 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port)
3462 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3463 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
3464 const struct mlxsw_sp_port_type_speed_ops *ops;
3465 char ptys_pl[MLXSW_REG_PTYS_LEN];
3468 ops = mlxsw_sp->port_type_speed_ops;
3470 /* Set advertised speeds to supported speeds. */
3471 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3473 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3477 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
3478 ð_proto_admin, ð_proto_oper);
3479 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
3480 eth_proto_cap, mlxsw_sp_port->link.autoneg);
3481 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3484 int mlxsw_sp_port_speed_get(struct mlxsw_sp_port *mlxsw_sp_port, u32 *speed)
3486 const struct mlxsw_sp_port_type_speed_ops *port_type_speed_ops;
3487 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3488 char ptys_pl[MLXSW_REG_PTYS_LEN];
3492 port_type_speed_ops = mlxsw_sp->port_type_speed_ops;
3493 port_type_speed_ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl,
3494 mlxsw_sp_port->local_port, 0,
3496 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
3499 port_type_speed_ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, NULL, NULL,
3501 *speed = port_type_speed_ops->from_ptys_speed(mlxsw_sp, eth_proto_oper);
3505 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
3506 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
3507 bool dwrr, u8 dwrr_weight)
3509 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3510 char qeec_pl[MLXSW_REG_QEEC_LEN];
3512 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3514 mlxsw_reg_qeec_de_set(qeec_pl, true);
3515 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
3516 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
3517 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3520 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
3521 enum mlxsw_reg_qeec_hr hr, u8 index,
3522 u8 next_index, u32 maxrate, u8 burst_size)
3524 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3525 char qeec_pl[MLXSW_REG_QEEC_LEN];
3527 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3529 mlxsw_reg_qeec_mase_set(qeec_pl, true);
3530 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
3531 mlxsw_reg_qeec_max_shaper_bs_set(qeec_pl, burst_size);
3532 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3535 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
3536 enum mlxsw_reg_qeec_hr hr, u8 index,
3537 u8 next_index, u32 minrate)
3539 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3540 char qeec_pl[MLXSW_REG_QEEC_LEN];
3542 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3544 mlxsw_reg_qeec_mise_set(qeec_pl, true);
3545 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
3547 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3550 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
3551 u8 switch_prio, u8 tclass)
3553 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3554 char qtct_pl[MLXSW_REG_QTCT_LEN];
3556 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
3558 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
3561 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
3565 /* Setup the elements hierarcy, so that each TC is linked to
3566 * one subgroup, which are all member in the same group.
3568 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3569 MLXSW_REG_QEEC_HR_GROUP, 0, 0, false, 0);
3572 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3573 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3574 MLXSW_REG_QEEC_HR_SUBGROUP, i,
3579 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3580 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3581 MLXSW_REG_QEEC_HR_TC, i, i,
3586 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3587 MLXSW_REG_QEEC_HR_TC,
3594 /* Make sure the max shaper is disabled in all hierarchies that support
3595 * it. Note that this disables ptps (PTP shaper), but that is intended
3596 * for the initial configuration.
3598 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3599 MLXSW_REG_QEEC_HR_PORT, 0, 0,
3600 MLXSW_REG_QEEC_MAS_DIS, 0);
3603 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3604 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3605 MLXSW_REG_QEEC_HR_SUBGROUP,
3607 MLXSW_REG_QEEC_MAS_DIS, 0);
3611 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3612 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3613 MLXSW_REG_QEEC_HR_TC,
3615 MLXSW_REG_QEEC_MAS_DIS, 0);
3619 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3620 MLXSW_REG_QEEC_HR_TC,
3622 MLXSW_REG_QEEC_MAS_DIS, 0);
3627 /* Configure the min shaper for multicast TCs. */
3628 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3629 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
3630 MLXSW_REG_QEEC_HR_TC,
3632 MLXSW_REG_QEEC_MIS_MIN);
3637 /* Map all priorities to traffic class 0. */
3638 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3639 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
3647 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
3650 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3651 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
3653 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
3654 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
3657 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3658 u8 split_base_local_port,
3659 struct mlxsw_sp_port_mapping *port_mapping)
3661 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
3662 bool split = !!split_base_local_port;
3663 struct mlxsw_sp_port *mlxsw_sp_port;
3664 struct net_device *dev;
3667 err = mlxsw_core_port_init(mlxsw_sp->core, local_port,
3668 port_mapping->module + 1, split,
3669 port_mapping->lane / port_mapping->width,
3671 sizeof(mlxsw_sp->base_mac));
3673 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
3678 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
3681 goto err_alloc_etherdev;
3683 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
3684 dev_net_set(dev, mlxsw_sp_net(mlxsw_sp));
3685 mlxsw_sp_port = netdev_priv(dev);
3686 mlxsw_sp_port->dev = dev;
3687 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
3688 mlxsw_sp_port->local_port = local_port;
3689 mlxsw_sp_port->pvid = MLXSW_SP_DEFAULT_VID;
3690 mlxsw_sp_port->split = split;
3691 mlxsw_sp_port->split_base_local_port = split_base_local_port;
3692 mlxsw_sp_port->mapping = *port_mapping;
3693 mlxsw_sp_port->link.autoneg = 1;
3694 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
3695 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
3697 mlxsw_sp_port->pcpu_stats =
3698 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
3699 if (!mlxsw_sp_port->pcpu_stats) {
3701 goto err_alloc_stats;
3704 mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
3706 if (!mlxsw_sp_port->sample) {
3708 goto err_alloc_sample;
3711 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
3712 &update_stats_cache);
3714 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
3715 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
3717 err = mlxsw_sp_port_module_map(mlxsw_sp_port);
3719 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
3720 mlxsw_sp_port->local_port);
3721 goto err_port_module_map;
3724 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
3726 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
3727 mlxsw_sp_port->local_port);
3728 goto err_port_swid_set;
3731 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
3733 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
3734 mlxsw_sp_port->local_port);
3735 goto err_dev_addr_init;
3738 netif_carrier_off(dev);
3740 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
3741 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
3742 dev->hw_features |= NETIF_F_HW_TC | NETIF_F_LOOPBACK;
3745 dev->max_mtu = ETH_MAX_MTU;
3747 /* Each packet needs to have a Tx header (metadata) on top all other
3750 dev->needed_headroom = MLXSW_TXHDR_LEN;
3752 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
3754 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
3755 mlxsw_sp_port->local_port);
3756 goto err_port_system_port_mapping_set;
3759 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port);
3761 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
3762 mlxsw_sp_port->local_port);
3763 goto err_port_speed_by_width_set;
3766 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
3768 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
3769 mlxsw_sp_port->local_port);
3770 goto err_port_mtu_set;
3773 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3775 goto err_port_admin_status_set;
3777 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
3779 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
3780 mlxsw_sp_port->local_port);
3781 goto err_port_buffers_init;
3784 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
3786 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
3787 mlxsw_sp_port->local_port);
3788 goto err_port_ets_init;
3791 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
3793 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
3794 mlxsw_sp_port->local_port);
3795 goto err_port_tc_mc_mode;
3798 /* ETS and buffers must be initialized before DCB. */
3799 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
3801 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
3802 mlxsw_sp_port->local_port);
3803 goto err_port_dcb_init;
3806 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
3808 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
3809 mlxsw_sp_port->local_port);
3810 goto err_port_fids_init;
3813 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
3815 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
3816 mlxsw_sp_port->local_port);
3817 goto err_port_qdiscs_init;
3820 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 0, VLAN_N_VID - 1, false,
3823 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to clear VLAN filter\n",
3824 mlxsw_sp_port->local_port);
3825 goto err_port_vlan_clear;
3828 err = mlxsw_sp_port_nve_init(mlxsw_sp_port);
3830 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize NVE\n",
3831 mlxsw_sp_port->local_port);
3832 goto err_port_nve_init;
3835 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
3837 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set PVID\n",
3838 mlxsw_sp_port->local_port);
3839 goto err_port_pvid_set;
3842 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
3843 MLXSW_SP_DEFAULT_VID);
3844 if (IS_ERR(mlxsw_sp_port_vlan)) {
3845 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
3846 mlxsw_sp_port->local_port);
3847 err = PTR_ERR(mlxsw_sp_port_vlan);
3848 goto err_port_vlan_create;
3850 mlxsw_sp_port->default_vlan = mlxsw_sp_port_vlan;
3852 INIT_DELAYED_WORK(&mlxsw_sp_port->ptp.shaper_dw,
3853 mlxsw_sp->ptp_ops->shaper_work);
3854 INIT_DELAYED_WORK(&mlxsw_sp_port->span.speed_update_dw,
3855 mlxsw_sp_span_speed_update_work);
3857 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
3858 err = register_netdev(dev);
3860 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
3861 mlxsw_sp_port->local_port);
3862 goto err_register_netdev;
3865 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
3866 mlxsw_sp_port, dev);
3867 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
3870 err_register_netdev:
3871 mlxsw_sp->ports[local_port] = NULL;
3872 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
3873 err_port_vlan_create:
3875 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3877 err_port_vlan_clear:
3878 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3879 err_port_qdiscs_init:
3880 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3882 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3884 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3885 err_port_tc_mc_mode:
3887 err_port_buffers_init:
3888 err_port_admin_status_set:
3890 err_port_speed_by_width_set:
3891 err_port_system_port_mapping_set:
3893 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3895 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3896 err_port_module_map:
3897 kfree(mlxsw_sp_port->sample);
3899 free_percpu(mlxsw_sp_port->pcpu_stats);
3903 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3907 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3909 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3911 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
3912 cancel_delayed_work_sync(&mlxsw_sp_port->span.speed_update_dw);
3913 cancel_delayed_work_sync(&mlxsw_sp_port->ptp.shaper_dw);
3914 mlxsw_sp_port_ptp_clear(mlxsw_sp_port);
3915 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
3916 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
3917 mlxsw_sp->ports[local_port] = NULL;
3918 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, true);
3919 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
3920 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3921 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3922 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3923 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
3924 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3925 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3926 kfree(mlxsw_sp_port->sample);
3927 free_percpu(mlxsw_sp_port->pcpu_stats);
3928 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
3929 free_netdev(mlxsw_sp_port->dev);
3930 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3933 static int mlxsw_sp_cpu_port_create(struct mlxsw_sp *mlxsw_sp)
3935 struct mlxsw_sp_port *mlxsw_sp_port;
3938 mlxsw_sp_port = kzalloc(sizeof(*mlxsw_sp_port), GFP_KERNEL);
3942 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
3943 mlxsw_sp_port->local_port = MLXSW_PORT_CPU_PORT;
3945 err = mlxsw_core_cpu_port_init(mlxsw_sp->core,
3948 sizeof(mlxsw_sp->base_mac));
3950 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize core CPU port\n");
3951 goto err_core_cpu_port_init;
3954 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = mlxsw_sp_port;
3957 err_core_cpu_port_init:
3958 kfree(mlxsw_sp_port);
3962 static void mlxsw_sp_cpu_port_remove(struct mlxsw_sp *mlxsw_sp)
3964 struct mlxsw_sp_port *mlxsw_sp_port =
3965 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT];
3967 mlxsw_core_cpu_port_fini(mlxsw_sp->core);
3968 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = NULL;
3969 kfree(mlxsw_sp_port);
3972 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3974 return mlxsw_sp->ports[local_port] != NULL;
3977 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
3981 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
3982 if (mlxsw_sp_port_created(mlxsw_sp, i))
3983 mlxsw_sp_port_remove(mlxsw_sp, i);
3984 mlxsw_sp_cpu_port_remove(mlxsw_sp);
3985 kfree(mlxsw_sp->ports);
3988 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
3990 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
3991 struct mlxsw_sp_port_mapping *port_mapping;
3996 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
3997 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
3998 if (!mlxsw_sp->ports)
4001 err = mlxsw_sp_cpu_port_create(mlxsw_sp);
4003 goto err_cpu_port_create;
4005 for (i = 1; i < max_ports; i++) {
4006 port_mapping = mlxsw_sp->port_mapping[i];
4009 err = mlxsw_sp_port_create(mlxsw_sp, i, 0, port_mapping);
4011 goto err_port_create;
4016 for (i--; i >= 1; i--)
4017 if (mlxsw_sp_port_created(mlxsw_sp, i))
4018 mlxsw_sp_port_remove(mlxsw_sp, i);
4019 mlxsw_sp_cpu_port_remove(mlxsw_sp);
4020 err_cpu_port_create:
4021 kfree(mlxsw_sp->ports);
4025 static int mlxsw_sp_port_module_info_init(struct mlxsw_sp *mlxsw_sp)
4027 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
4028 struct mlxsw_sp_port_mapping port_mapping;
4032 mlxsw_sp->port_mapping = kcalloc(max_ports,
4033 sizeof(struct mlxsw_sp_port_mapping *),
4035 if (!mlxsw_sp->port_mapping)
4038 for (i = 1; i < max_ports; i++) {
4039 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &port_mapping);
4041 goto err_port_module_info_get;
4042 if (!port_mapping.width)
4045 mlxsw_sp->port_mapping[i] = kmemdup(&port_mapping,
4046 sizeof(port_mapping),
4048 if (!mlxsw_sp->port_mapping[i]) {
4050 goto err_port_module_info_dup;
4055 err_port_module_info_get:
4056 err_port_module_info_dup:
4057 for (i--; i >= 1; i--)
4058 kfree(mlxsw_sp->port_mapping[i]);
4059 kfree(mlxsw_sp->port_mapping);
4063 static void mlxsw_sp_port_module_info_fini(struct mlxsw_sp *mlxsw_sp)
4067 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
4068 kfree(mlxsw_sp->port_mapping[i]);
4069 kfree(mlxsw_sp->port_mapping);
4072 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port, unsigned int max_width)
4074 u8 offset = (local_port - 1) % max_width;
4076 return local_port - offset;
4080 mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
4081 struct mlxsw_sp_port_mapping *port_mapping,
4082 unsigned int count, u8 offset)
4084 struct mlxsw_sp_port_mapping split_port_mapping;
4087 split_port_mapping = *port_mapping;
4088 split_port_mapping.width /= count;
4089 for (i = 0; i < count; i++) {
4090 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i * offset,
4091 base_port, &split_port_mapping);
4093 goto err_port_create;
4094 split_port_mapping.lane += split_port_mapping.width;
4100 for (i--; i >= 0; i--)
4101 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4102 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4106 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
4108 unsigned int count, u8 offset)
4110 struct mlxsw_sp_port_mapping *port_mapping;
4113 /* Go over original unsplit ports in the gap and recreate them. */
4114 for (i = 0; i < count * offset; i++) {
4115 port_mapping = mlxsw_sp->port_mapping[base_port + i];
4118 mlxsw_sp_port_create(mlxsw_sp, base_port + i, 0, port_mapping);
4122 static int mlxsw_sp_local_ports_offset(struct mlxsw_core *mlxsw_core,
4124 unsigned int max_width)
4126 enum mlxsw_res_id local_ports_in_x_res_id;
4127 int split_width = max_width / count;
4129 if (split_width == 1)
4130 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_1X;
4131 else if (split_width == 2)
4132 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_2X;
4133 else if (split_width == 4)
4134 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_4X;
4138 if (!mlxsw_core_res_valid(mlxsw_core, local_ports_in_x_res_id))
4140 return mlxsw_core_res_get(mlxsw_core, local_ports_in_x_res_id);
4143 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
4145 struct netlink_ext_ack *extack)
4147 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4148 struct mlxsw_sp_port_mapping port_mapping;
4149 struct mlxsw_sp_port *mlxsw_sp_port;
4156 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4157 if (!mlxsw_sp_port) {
4158 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
4160 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
4164 /* Split ports cannot be split. */
4165 if (mlxsw_sp_port->split) {
4166 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
4167 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split further");
4171 max_width = mlxsw_core_module_max_width(mlxsw_core,
4172 mlxsw_sp_port->mapping.module);
4173 if (max_width < 0) {
4174 netdev_err(mlxsw_sp_port->dev, "Cannot get max width of port module\n");
4175 NL_SET_ERR_MSG_MOD(extack, "Cannot get max width of port module");
4179 /* Split port with non-max and 1 module width cannot be split. */
4180 if (mlxsw_sp_port->mapping.width != max_width || max_width == 1) {
4181 netdev_err(mlxsw_sp_port->dev, "Port cannot be split\n");
4182 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split");
4186 if (count == 1 || !is_power_of_2(count) || count > max_width) {
4187 netdev_err(mlxsw_sp_port->dev, "Invalid split count\n");
4188 NL_SET_ERR_MSG_MOD(extack, "Invalid split count");
4192 offset = mlxsw_sp_local_ports_offset(mlxsw_core, count, max_width);
4194 netdev_err(mlxsw_sp_port->dev, "Cannot obtain local port offset\n");
4195 NL_SET_ERR_MSG_MOD(extack, "Cannot obtain local port offset");
4199 /* Only in case max split is being done, the local port and
4200 * base port may differ.
4202 base_port = count == max_width ?
4203 mlxsw_sp_cluster_base_port_get(local_port, max_width) :
4206 for (i = 0; i < count * offset; i++) {
4207 /* Expect base port to exist and also the one in the middle in
4208 * case of maximal split count.
4210 if (i == 0 || (count == max_width && i == count / 2))
4213 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i)) {
4214 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
4215 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
4220 port_mapping = mlxsw_sp_port->mapping;
4222 for (i = 0; i < count; i++)
4223 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4224 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4226 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, &port_mapping,
4229 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
4230 goto err_port_split_create;
4235 err_port_split_create:
4236 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count, offset);
4240 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port,
4241 struct netlink_ext_ack *extack)
4243 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4244 struct mlxsw_sp_port *mlxsw_sp_port;
4251 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4252 if (!mlxsw_sp_port) {
4253 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
4255 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
4259 if (!mlxsw_sp_port->split) {
4260 netdev_err(mlxsw_sp_port->dev, "Port was not split\n");
4261 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
4265 max_width = mlxsw_core_module_max_width(mlxsw_core,
4266 mlxsw_sp_port->mapping.module);
4267 if (max_width < 0) {
4268 netdev_err(mlxsw_sp_port->dev, "Cannot get max width of port module\n");
4269 NL_SET_ERR_MSG_MOD(extack, "Cannot get max width of port module");
4273 count = max_width / mlxsw_sp_port->mapping.width;
4275 offset = mlxsw_sp_local_ports_offset(mlxsw_core, count, max_width);
4276 if (WARN_ON(offset < 0)) {
4277 netdev_err(mlxsw_sp_port->dev, "Cannot obtain local port offset\n");
4278 NL_SET_ERR_MSG_MOD(extack, "Cannot obtain local port offset");
4282 base_port = mlxsw_sp_port->split_base_local_port;
4284 for (i = 0; i < count; i++)
4285 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
4286 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
4288 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count, offset);
4294 mlxsw_sp_port_down_wipe_counters(struct mlxsw_sp_port *mlxsw_sp_port)
4298 for (i = 0; i < TC_MAX_QUEUE; i++)
4299 mlxsw_sp_port->periodic_hw_stats.xstats.backlog[i] = 0;
4302 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
4303 char *pude_pl, void *priv)
4305 struct mlxsw_sp *mlxsw_sp = priv;
4306 struct mlxsw_sp_port *mlxsw_sp_port;
4307 enum mlxsw_reg_pude_oper_status status;
4310 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
4311 mlxsw_sp_port = mlxsw_sp->ports[local_port];
4315 status = mlxsw_reg_pude_oper_status_get(pude_pl);
4316 if (status == MLXSW_PORT_OPER_STATUS_UP) {
4317 netdev_info(mlxsw_sp_port->dev, "link up\n");
4318 netif_carrier_on(mlxsw_sp_port->dev);
4319 mlxsw_core_schedule_dw(&mlxsw_sp_port->ptp.shaper_dw, 0);
4320 mlxsw_core_schedule_dw(&mlxsw_sp_port->span.speed_update_dw, 0);
4322 netdev_info(mlxsw_sp_port->dev, "link down\n");
4323 netif_carrier_off(mlxsw_sp_port->dev);
4324 mlxsw_sp_port_down_wipe_counters(mlxsw_sp_port);
4328 static void mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp *mlxsw_sp,
4329 char *mtpptr_pl, bool ingress)
4335 local_port = mlxsw_reg_mtpptr_local_port_get(mtpptr_pl);
4336 num_rec = mlxsw_reg_mtpptr_num_rec_get(mtpptr_pl);
4337 for (i = 0; i < num_rec; i++) {
4343 mlxsw_reg_mtpptr_unpack(mtpptr_pl, i, &message_type,
4344 &domain_number, &sequence_id,
4346 mlxsw_sp1_ptp_got_timestamp(mlxsw_sp, ingress, local_port,
4347 message_type, domain_number,
4348 sequence_id, timestamp);
4352 static void mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info *reg,
4353 char *mtpptr_pl, void *priv)
4355 struct mlxsw_sp *mlxsw_sp = priv;
4357 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, true);
4360 static void mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info *reg,
4361 char *mtpptr_pl, void *priv)
4363 struct mlxsw_sp *mlxsw_sp = priv;
4365 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, false);
4368 void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
4369 u8 local_port, void *priv)
4371 struct mlxsw_sp *mlxsw_sp = priv;
4372 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4373 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
4375 if (unlikely(!mlxsw_sp_port)) {
4376 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
4381 skb->dev = mlxsw_sp_port->dev;
4383 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
4384 u64_stats_update_begin(&pcpu_stats->syncp);
4385 pcpu_stats->rx_packets++;
4386 pcpu_stats->rx_bytes += skb->len;
4387 u64_stats_update_end(&pcpu_stats->syncp);
4389 skb->protocol = eth_type_trans(skb, skb->dev);
4390 netif_receive_skb(skb);
4393 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
4396 skb->offload_fwd_mark = 1;
4397 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4400 static void mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff *skb,
4401 u8 local_port, void *priv)
4403 skb->offload_l3_fwd_mark = 1;
4404 skb->offload_fwd_mark = 1;
4405 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
4408 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
4411 struct mlxsw_sp *mlxsw_sp = priv;
4412 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
4413 struct psample_group *psample_group;
4416 if (unlikely(!mlxsw_sp_port)) {
4417 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
4421 if (unlikely(!mlxsw_sp_port->sample)) {
4422 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
4427 size = mlxsw_sp_port->sample->truncate ?
4428 mlxsw_sp_port->sample->trunc_size : skb->len;
4431 psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
4434 psample_sample_packet(psample_group, skb, size,
4435 mlxsw_sp_port->dev->ifindex, 0,
4436 mlxsw_sp_port->sample->rate);
4443 static void mlxsw_sp_rx_listener_ptp(struct sk_buff *skb, u8 local_port,
4446 struct mlxsw_sp *mlxsw_sp = priv;
4448 mlxsw_sp->ptp_ops->receive(mlxsw_sp, skb, local_port);
4451 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4452 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
4453 _is_ctrl, SP_##_trap_group, DISCARD)
4455 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4456 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
4457 _is_ctrl, SP_##_trap_group, DISCARD)
4459 #define MLXSW_SP_RXL_L3_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
4460 MLXSW_RXL(mlxsw_sp_rx_listener_l3_mark_func, _trap_id, _action, \
4461 _is_ctrl, SP_##_trap_group, DISCARD)
4463 #define MLXSW_SP_EVENTL(_func, _trap_id) \
4464 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
4466 static const struct mlxsw_listener mlxsw_sp_listener[] = {
4468 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
4470 MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
4471 MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
4472 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, LLDP, TRAP_TO_CPU,
4473 false, SP_LLDP, DISCARD),
4474 MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
4475 MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
4476 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
4477 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
4478 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
4479 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
4480 MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
4481 MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
4482 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, IP2ME, false),
4483 MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY, MIRROR_TO_CPU, IPV6_MLD,
4485 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4487 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE, TRAP_TO_CPU, IPV6_MLD,
4489 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
4492 MLXSW_SP_RXL_L3_MARK(LBERROR, MIRROR_TO_CPU, LBERROR, false),
4493 MLXSW_SP_RXL_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
4494 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
4496 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP, false),
4497 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
4498 MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, TRAP_TO_CPU, ROUTER_EXP, false),
4499 MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, TRAP_TO_CPU, ROUTER_EXP,
4501 MLXSW_SP_RXL_MARK(IPV4_OSPF, TRAP_TO_CPU, OSPF, false),
4502 MLXSW_SP_RXL_MARK(IPV6_OSPF, TRAP_TO_CPU, OSPF, false),
4503 MLXSW_SP_RXL_MARK(IPV6_DHCP, TRAP_TO_CPU, DHCP, false),
4504 MLXSW_SP_RXL_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
4505 MLXSW_SP_RXL_MARK(IPV4_BGP, TRAP_TO_CPU, BGP, false),
4506 MLXSW_SP_RXL_MARK(IPV6_BGP, TRAP_TO_CPU, BGP, false),
4507 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4509 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4511 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
4513 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
4515 MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, TRAP_TO_CPU, IPV6_ND, false),
4516 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
4518 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, TRAP_TO_CPU, ROUTER_EXP, false),
4519 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, TRAP_TO_CPU, ROUTER_EXP, false),
4520 MLXSW_SP_RXL_MARK(IPV4_VRRP, TRAP_TO_CPU, VRRP, false),
4521 MLXSW_SP_RXL_MARK(IPV6_VRRP, TRAP_TO_CPU, VRRP, false),
4522 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_CLASS_E, FORWARD,
4524 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_MC_DMAC, FORWARD,
4526 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_DIP, FORWARD,
4528 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_DIP_LINK_LOCAL, FORWARD,
4530 /* PKT Sample trap */
4531 MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
4532 false, SP_IP2ME, DISCARD),
4534 MLXSW_SP_RXL_NO_MARK(ACL0, TRAP_TO_CPU, IP2ME, false),
4535 /* Multicast Router Traps */
4536 MLXSW_SP_RXL_MARK(IPV4_PIM, TRAP_TO_CPU, PIM, false),
4537 MLXSW_SP_RXL_MARK(IPV6_PIM, TRAP_TO_CPU, PIM, false),
4538 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
4539 MLXSW_SP_RXL_L3_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
4541 MLXSW_SP_RXL_MARK(NVE_ENCAP_ARP, TRAP_TO_CPU, ARP, false),
4542 MLXSW_SP_RXL_NO_MARK(NVE_DECAP_ARP, TRAP_TO_CPU, ARP, false),
4544 MLXSW_RXL(mlxsw_sp_rx_listener_ptp, PTP0, TRAP_TO_CPU,
4545 false, SP_PTP0, DISCARD),
4546 MLXSW_SP_RXL_NO_MARK(PTP1, TRAP_TO_CPU, PTP1, false),
4549 static const struct mlxsw_listener mlxsw_sp1_listener[] = {
4551 MLXSW_EVENTL(mlxsw_sp1_ptp_egr_fifo_event_func, PTP_EGR_FIFO, SP_PTP0),
4552 MLXSW_EVENTL(mlxsw_sp1_ptp_ing_fifo_event_func, PTP_ING_FIFO, SP_PTP0),
4555 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
4557 char qpcr_pl[MLXSW_REG_QPCR_LEN];
4558 enum mlxsw_reg_qpcr_ir_units ir_units;
4559 int max_cpu_policers;
4565 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
4568 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4570 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
4571 for (i = 0; i < max_cpu_policers; i++) {
4574 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4575 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4576 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4577 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4578 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4579 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4580 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4584 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4585 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4589 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4590 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4591 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4592 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4593 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4594 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4595 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4596 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4600 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4604 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4608 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4612 case MLXSW_REG_HTGT_TRAP_GROUP_SP_VRRP:
4620 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
4622 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
4630 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
4632 char htgt_pl[MLXSW_REG_HTGT_LEN];
4633 enum mlxsw_reg_htgt_trap_group i;
4634 int max_cpu_policers;
4635 int max_trap_groups;
4640 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
4643 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
4644 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
4646 for (i = 0; i < max_trap_groups; i++) {
4649 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
4650 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
4651 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
4652 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
4653 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
4654 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0:
4655 case MLXSW_REG_HTGT_TRAP_GROUP_SP_VRRP:
4659 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
4660 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
4664 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
4665 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
4666 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
4670 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
4671 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
4672 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
4673 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1:
4677 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
4678 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
4679 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
4680 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
4681 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR:
4685 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
4686 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
4687 tc = MLXSW_REG_HTGT_DEFAULT_TC;
4688 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
4694 if (max_cpu_policers <= policer_id &&
4695 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
4698 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
4699 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4707 static int mlxsw_sp_traps_register(struct mlxsw_sp *mlxsw_sp,
4708 const struct mlxsw_listener listeners[],
4709 size_t listeners_count)
4714 for (i = 0; i < listeners_count; i++) {
4715 err = mlxsw_core_trap_register(mlxsw_sp->core,
4719 goto err_listener_register;
4724 err_listener_register:
4725 for (i--; i >= 0; i--) {
4726 mlxsw_core_trap_unregister(mlxsw_sp->core,
4733 static void mlxsw_sp_traps_unregister(struct mlxsw_sp *mlxsw_sp,
4734 const struct mlxsw_listener listeners[],
4735 size_t listeners_count)
4739 for (i = 0; i < listeners_count; i++) {
4740 mlxsw_core_trap_unregister(mlxsw_sp->core,
4746 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
4750 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
4754 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
4758 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp_listener,
4759 ARRAY_SIZE(mlxsw_sp_listener));
4763 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp->listeners,
4764 mlxsw_sp->listeners_count);
4766 goto err_extra_traps_init;
4770 err_extra_traps_init:
4771 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4772 ARRAY_SIZE(mlxsw_sp_listener));
4776 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
4778 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp->listeners,
4779 mlxsw_sp->listeners_count);
4780 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
4781 ARRAY_SIZE(mlxsw_sp_listener));
4784 #define MLXSW_SP_LAG_SEED_INIT 0xcafecafe
4786 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
4788 char slcr_pl[MLXSW_REG_SLCR_LEN];
4792 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac),
4793 MLXSW_SP_LAG_SEED_INIT);
4794 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
4795 MLXSW_REG_SLCR_LAG_HASH_DMAC |
4796 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
4797 MLXSW_REG_SLCR_LAG_HASH_VLANID |
4798 MLXSW_REG_SLCR_LAG_HASH_SIP |
4799 MLXSW_REG_SLCR_LAG_HASH_DIP |
4800 MLXSW_REG_SLCR_LAG_HASH_SPORT |
4801 MLXSW_REG_SLCR_LAG_HASH_DPORT |
4802 MLXSW_REG_SLCR_LAG_HASH_IPPROTO, seed);
4803 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
4807 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
4808 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
4811 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
4812 sizeof(struct mlxsw_sp_upper),
4814 if (!mlxsw_sp->lags)
4820 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
4822 kfree(mlxsw_sp->lags);
4825 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
4827 char htgt_pl[MLXSW_REG_HTGT_LEN];
4829 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
4830 MLXSW_REG_HTGT_INVALID_POLICER,
4831 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
4832 MLXSW_REG_HTGT_DEFAULT_TC);
4833 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
4836 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
4837 .clock_init = mlxsw_sp1_ptp_clock_init,
4838 .clock_fini = mlxsw_sp1_ptp_clock_fini,
4839 .init = mlxsw_sp1_ptp_init,
4840 .fini = mlxsw_sp1_ptp_fini,
4841 .receive = mlxsw_sp1_ptp_receive,
4842 .transmitted = mlxsw_sp1_ptp_transmitted,
4843 .hwtstamp_get = mlxsw_sp1_ptp_hwtstamp_get,
4844 .hwtstamp_set = mlxsw_sp1_ptp_hwtstamp_set,
4845 .shaper_work = mlxsw_sp1_ptp_shaper_work,
4846 .get_ts_info = mlxsw_sp1_ptp_get_ts_info,
4847 .get_stats_count = mlxsw_sp1_get_stats_count,
4848 .get_stats_strings = mlxsw_sp1_get_stats_strings,
4849 .get_stats = mlxsw_sp1_get_stats,
4852 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
4853 .clock_init = mlxsw_sp2_ptp_clock_init,
4854 .clock_fini = mlxsw_sp2_ptp_clock_fini,
4855 .init = mlxsw_sp2_ptp_init,
4856 .fini = mlxsw_sp2_ptp_fini,
4857 .receive = mlxsw_sp2_ptp_receive,
4858 .transmitted = mlxsw_sp2_ptp_transmitted,
4859 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
4860 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
4861 .shaper_work = mlxsw_sp2_ptp_shaper_work,
4862 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
4863 .get_stats_count = mlxsw_sp2_get_stats_count,
4864 .get_stats_strings = mlxsw_sp2_get_stats_strings,
4865 .get_stats = mlxsw_sp2_get_stats,
4868 static u32 mlxsw_sp1_span_buffsize_get(int mtu, u32 speed)
4873 static const struct mlxsw_sp_span_ops mlxsw_sp1_span_ops = {
4874 .buffsize_get = mlxsw_sp1_span_buffsize_get,
4877 #define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38
4878 #define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50
4880 static u32 __mlxsw_sp_span_buffsize_get(int mtu, u32 speed, u32 buffer_factor)
4882 return 3 * mtu + buffer_factor * speed / 1000;
4885 static u32 mlxsw_sp2_span_buffsize_get(int mtu, u32 speed)
4887 int factor = MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR;
4889 return __mlxsw_sp_span_buffsize_get(mtu, speed, factor);
4892 static const struct mlxsw_sp_span_ops mlxsw_sp2_span_ops = {
4893 .buffsize_get = mlxsw_sp2_span_buffsize_get,
4896 static u32 mlxsw_sp3_span_buffsize_get(int mtu, u32 speed)
4898 int factor = MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR;
4900 return __mlxsw_sp_span_buffsize_get(mtu, speed, factor);
4903 static const struct mlxsw_sp_span_ops mlxsw_sp3_span_ops = {
4904 .buffsize_get = mlxsw_sp3_span_buffsize_get,
4907 u32 mlxsw_sp_span_buffsize_get(struct mlxsw_sp *mlxsw_sp, int mtu, u32 speed)
4909 u32 buffsize = mlxsw_sp->span_ops->buffsize_get(speed, mtu);
4911 return mlxsw_sp_bytes_cells(mlxsw_sp, buffsize) + 1;
4914 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
4915 unsigned long event, void *ptr);
4917 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
4918 const struct mlxsw_bus_info *mlxsw_bus_info,
4919 struct netlink_ext_ack *extack)
4921 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4924 mlxsw_sp->core = mlxsw_core;
4925 mlxsw_sp->bus_info = mlxsw_bus_info;
4927 err = mlxsw_sp_fw_rev_validate(mlxsw_sp);
4931 mlxsw_core_emad_string_tlv_enable(mlxsw_core);
4933 err = mlxsw_sp_base_mac_get(mlxsw_sp);
4935 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
4939 err = mlxsw_sp_kvdl_init(mlxsw_sp);
4941 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
4945 err = mlxsw_sp_fids_init(mlxsw_sp);
4947 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
4951 err = mlxsw_sp_traps_init(mlxsw_sp);
4953 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
4954 goto err_traps_init;
4957 err = mlxsw_sp_devlink_traps_init(mlxsw_sp);
4959 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize devlink traps\n");
4960 goto err_devlink_traps_init;
4963 err = mlxsw_sp_buffers_init(mlxsw_sp);
4965 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
4966 goto err_buffers_init;
4969 err = mlxsw_sp_lag_init(mlxsw_sp);
4971 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
4975 /* Initialize SPAN before router and switchdev, so that those components
4976 * can call mlxsw_sp_span_respin().
4978 err = mlxsw_sp_span_init(mlxsw_sp);
4980 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
4984 err = mlxsw_sp_switchdev_init(mlxsw_sp);
4986 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
4987 goto err_switchdev_init;
4990 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
4992 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
4993 goto err_counter_pool_init;
4996 err = mlxsw_sp_afa_init(mlxsw_sp);
4998 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
5002 err = mlxsw_sp_nve_init(mlxsw_sp);
5004 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize NVE\n");
5008 err = mlxsw_sp_acl_init(mlxsw_sp);
5010 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
5014 err = mlxsw_sp_router_init(mlxsw_sp, extack);
5016 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
5017 goto err_router_init;
5020 if (mlxsw_sp->bus_info->read_frc_capable) {
5021 /* NULL is a valid return value from clock_init */
5023 mlxsw_sp->ptp_ops->clock_init(mlxsw_sp,
5024 mlxsw_sp->bus_info->dev);
5025 if (IS_ERR(mlxsw_sp->clock)) {
5026 err = PTR_ERR(mlxsw_sp->clock);
5027 dev_err(mlxsw_sp->bus_info->dev, "Failed to init ptp clock\n");
5028 goto err_ptp_clock_init;
5032 if (mlxsw_sp->clock) {
5033 /* NULL is a valid return value from ptp_ops->init */
5034 mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
5035 if (IS_ERR(mlxsw_sp->ptp_state)) {
5036 err = PTR_ERR(mlxsw_sp->ptp_state);
5037 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
5042 /* Initialize netdevice notifier after router and SPAN is initialized,
5043 * so that the event handler can use router structures and call SPAN
5046 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
5047 err = register_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
5048 &mlxsw_sp->netdevice_nb);
5050 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
5051 goto err_netdev_notifier;
5054 err = mlxsw_sp_dpipe_init(mlxsw_sp);
5056 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
5057 goto err_dpipe_init;
5060 err = mlxsw_sp_port_module_info_init(mlxsw_sp);
5062 dev_err(mlxsw_sp->bus_info->dev, "Failed to init port module info\n");
5063 goto err_port_module_info_init;
5066 err = mlxsw_sp_ports_create(mlxsw_sp);
5068 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
5069 goto err_ports_create;
5075 mlxsw_sp_port_module_info_fini(mlxsw_sp);
5076 err_port_module_info_init:
5077 mlxsw_sp_dpipe_fini(mlxsw_sp);
5079 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
5080 &mlxsw_sp->netdevice_nb);
5081 err_netdev_notifier:
5082 if (mlxsw_sp->clock)
5083 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
5085 if (mlxsw_sp->clock)
5086 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
5088 mlxsw_sp_router_fini(mlxsw_sp);
5090 mlxsw_sp_acl_fini(mlxsw_sp);
5092 mlxsw_sp_nve_fini(mlxsw_sp);
5094 mlxsw_sp_afa_fini(mlxsw_sp);
5096 mlxsw_sp_counter_pool_fini(mlxsw_sp);
5097 err_counter_pool_init:
5098 mlxsw_sp_switchdev_fini(mlxsw_sp);
5100 mlxsw_sp_span_fini(mlxsw_sp);
5102 mlxsw_sp_lag_fini(mlxsw_sp);
5104 mlxsw_sp_buffers_fini(mlxsw_sp);
5106 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
5107 err_devlink_traps_init:
5108 mlxsw_sp_traps_fini(mlxsw_sp);
5110 mlxsw_sp_fids_fini(mlxsw_sp);
5112 mlxsw_sp_kvdl_fini(mlxsw_sp);
5116 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
5117 const struct mlxsw_bus_info *mlxsw_bus_info,
5118 struct netlink_ext_ack *extack)
5120 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5122 mlxsw_sp->req_rev = &mlxsw_sp1_fw_rev;
5123 mlxsw_sp->fw_filename = MLXSW_SP1_FW_FILENAME;
5124 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
5125 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
5126 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
5127 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
5128 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
5129 mlxsw_sp->nve_ops_arr = mlxsw_sp1_nve_ops_arr;
5130 mlxsw_sp->mac_mask = mlxsw_sp1_mac_mask;
5131 mlxsw_sp->rif_ops_arr = mlxsw_sp1_rif_ops_arr;
5132 mlxsw_sp->sb_vals = &mlxsw_sp1_sb_vals;
5133 mlxsw_sp->port_type_speed_ops = &mlxsw_sp1_port_type_speed_ops;
5134 mlxsw_sp->ptp_ops = &mlxsw_sp1_ptp_ops;
5135 mlxsw_sp->span_ops = &mlxsw_sp1_span_ops;
5136 mlxsw_sp->listeners = mlxsw_sp1_listener;
5137 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp1_listener);
5138 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP1;
5140 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
5143 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
5144 const struct mlxsw_bus_info *mlxsw_bus_info,
5145 struct netlink_ext_ack *extack)
5147 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5149 mlxsw_sp->req_rev = &mlxsw_sp2_fw_rev;
5150 mlxsw_sp->fw_filename = MLXSW_SP2_FW_FILENAME;
5151 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
5152 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
5153 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
5154 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
5155 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
5156 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
5157 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
5158 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
5159 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
5160 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
5161 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
5162 mlxsw_sp->span_ops = &mlxsw_sp2_span_ops;
5163 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP2;
5165 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
5168 static int mlxsw_sp3_init(struct mlxsw_core *mlxsw_core,
5169 const struct mlxsw_bus_info *mlxsw_bus_info,
5170 struct netlink_ext_ack *extack)
5172 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5174 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
5175 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
5176 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
5177 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
5178 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
5179 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
5180 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
5181 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
5182 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
5183 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
5184 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
5185 mlxsw_sp->span_ops = &mlxsw_sp3_span_ops;
5186 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP3;
5188 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
5191 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
5193 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5195 mlxsw_sp_ports_remove(mlxsw_sp);
5196 mlxsw_sp_port_module_info_fini(mlxsw_sp);
5197 mlxsw_sp_dpipe_fini(mlxsw_sp);
5198 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
5199 &mlxsw_sp->netdevice_nb);
5200 if (mlxsw_sp->clock) {
5201 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
5202 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
5204 mlxsw_sp_router_fini(mlxsw_sp);
5205 mlxsw_sp_acl_fini(mlxsw_sp);
5206 mlxsw_sp_nve_fini(mlxsw_sp);
5207 mlxsw_sp_afa_fini(mlxsw_sp);
5208 mlxsw_sp_counter_pool_fini(mlxsw_sp);
5209 mlxsw_sp_switchdev_fini(mlxsw_sp);
5210 mlxsw_sp_span_fini(mlxsw_sp);
5211 mlxsw_sp_lag_fini(mlxsw_sp);
5212 mlxsw_sp_buffers_fini(mlxsw_sp);
5213 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
5214 mlxsw_sp_traps_fini(mlxsw_sp);
5215 mlxsw_sp_fids_fini(mlxsw_sp);
5216 mlxsw_sp_kvdl_fini(mlxsw_sp);
5219 /* Per-FID flood tables are used for both "true" 802.1D FIDs and emulated
5222 #define MLXSW_SP_FID_FLOOD_TABLE_SIZE (MLXSW_SP_FID_8021D_MAX + \
5225 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
5227 .max_mid = MLXSW_SP_MID_MAX,
5228 .used_flood_tables = 1,
5229 .used_flood_mode = 1,
5231 .max_fid_flood_tables = 3,
5232 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
5233 .used_max_ib_mc = 1,
5237 .used_kvd_sizes = 1,
5238 .kvd_hash_single_parts = 59,
5239 .kvd_hash_double_parts = 41,
5240 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
5244 .type = MLXSW_PORT_SWID_TYPE_ETH,
5249 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
5251 .max_mid = MLXSW_SP_MID_MAX,
5252 .used_flood_tables = 1,
5253 .used_flood_mode = 1,
5255 .max_fid_flood_tables = 3,
5256 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
5257 .used_max_ib_mc = 1,
5264 .type = MLXSW_PORT_SWID_TYPE_ETH,
5270 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
5271 struct devlink_resource_size_params *kvd_size_params,
5272 struct devlink_resource_size_params *linear_size_params,
5273 struct devlink_resource_size_params *hash_double_size_params,
5274 struct devlink_resource_size_params *hash_single_size_params)
5276 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
5277 KVD_SINGLE_MIN_SIZE);
5278 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
5279 KVD_DOUBLE_MIN_SIZE);
5280 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
5281 u32 linear_size_min = 0;
5283 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
5284 MLXSW_SP_KVD_GRANULARITY,
5285 DEVLINK_RESOURCE_UNIT_ENTRY);
5286 devlink_resource_size_params_init(linear_size_params, linear_size_min,
5287 kvd_size - single_size_min -
5289 MLXSW_SP_KVD_GRANULARITY,
5290 DEVLINK_RESOURCE_UNIT_ENTRY);
5291 devlink_resource_size_params_init(hash_double_size_params,
5293 kvd_size - single_size_min -
5295 MLXSW_SP_KVD_GRANULARITY,
5296 DEVLINK_RESOURCE_UNIT_ENTRY);
5297 devlink_resource_size_params_init(hash_single_size_params,
5299 kvd_size - double_size_min -
5301 MLXSW_SP_KVD_GRANULARITY,
5302 DEVLINK_RESOURCE_UNIT_ENTRY);
5305 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
5307 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5308 struct devlink_resource_size_params hash_single_size_params;
5309 struct devlink_resource_size_params hash_double_size_params;
5310 struct devlink_resource_size_params linear_size_params;
5311 struct devlink_resource_size_params kvd_size_params;
5312 u32 kvd_size, single_size, double_size, linear_size;
5313 const struct mlxsw_config_profile *profile;
5316 profile = &mlxsw_sp1_config_profile;
5317 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
5320 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
5321 &linear_size_params,
5322 &hash_double_size_params,
5323 &hash_single_size_params);
5325 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
5326 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
5327 kvd_size, MLXSW_SP_RESOURCE_KVD,
5328 DEVLINK_RESOURCE_ID_PARENT_TOP,
5333 linear_size = profile->kvd_linear_size;
5334 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
5336 MLXSW_SP_RESOURCE_KVD_LINEAR,
5337 MLXSW_SP_RESOURCE_KVD,
5338 &linear_size_params);
5342 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
5346 double_size = kvd_size - linear_size;
5347 double_size *= profile->kvd_hash_double_parts;
5348 double_size /= profile->kvd_hash_double_parts +
5349 profile->kvd_hash_single_parts;
5350 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
5351 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
5353 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
5354 MLXSW_SP_RESOURCE_KVD,
5355 &hash_double_size_params);
5359 single_size = kvd_size - double_size - linear_size;
5360 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
5362 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
5363 MLXSW_SP_RESOURCE_KVD,
5364 &hash_single_size_params);
5371 static int mlxsw_sp2_resources_kvd_register(struct mlxsw_core *mlxsw_core)
5373 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5374 struct devlink_resource_size_params kvd_size_params;
5377 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
5380 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
5381 devlink_resource_size_params_init(&kvd_size_params, kvd_size, kvd_size,
5382 MLXSW_SP_KVD_GRANULARITY,
5383 DEVLINK_RESOURCE_UNIT_ENTRY);
5385 return devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
5386 kvd_size, MLXSW_SP_RESOURCE_KVD,
5387 DEVLINK_RESOURCE_ID_PARENT_TOP,
5391 static int mlxsw_sp_resources_span_register(struct mlxsw_core *mlxsw_core)
5393 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5394 struct devlink_resource_size_params span_size_params;
5397 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_SPAN))
5400 max_span = MLXSW_CORE_RES_GET(mlxsw_core, MAX_SPAN);
5401 devlink_resource_size_params_init(&span_size_params, max_span, max_span,
5402 1, DEVLINK_RESOURCE_UNIT_ENTRY);
5404 return devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_SPAN,
5405 max_span, MLXSW_SP_RESOURCE_SPAN,
5406 DEVLINK_RESOURCE_ID_PARENT_TOP,
5410 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
5414 err = mlxsw_sp1_resources_kvd_register(mlxsw_core);
5418 err = mlxsw_sp_resources_span_register(mlxsw_core);
5420 goto err_resources_span_register;
5424 err_resources_span_register:
5425 devlink_resources_unregister(priv_to_devlink(mlxsw_core), NULL);
5429 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
5433 err = mlxsw_sp2_resources_kvd_register(mlxsw_core);
5437 err = mlxsw_sp_resources_span_register(mlxsw_core);
5439 goto err_resources_span_register;
5443 err_resources_span_register:
5444 devlink_resources_unregister(priv_to_devlink(mlxsw_core), NULL);
5448 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
5449 const struct mlxsw_config_profile *profile,
5450 u64 *p_single_size, u64 *p_double_size,
5453 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5457 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5458 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
5461 /* The hash part is what left of the kvd without the
5462 * linear part. It is split to the single size and
5463 * double size by the parts ratio from the profile.
5464 * Both sizes must be a multiplications of the
5465 * granularity from the profile. In case the user
5466 * provided the sizes they are obtained via devlink.
5468 err = devlink_resource_size_get(devlink,
5469 MLXSW_SP_RESOURCE_KVD_LINEAR,
5472 *p_linear_size = profile->kvd_linear_size;
5474 err = devlink_resource_size_get(devlink,
5475 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
5478 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5480 double_size *= profile->kvd_hash_double_parts;
5481 double_size /= profile->kvd_hash_double_parts +
5482 profile->kvd_hash_single_parts;
5483 *p_double_size = rounddown(double_size,
5484 MLXSW_SP_KVD_GRANULARITY);
5487 err = devlink_resource_size_get(devlink,
5488 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
5491 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
5492 *p_double_size - *p_linear_size;
5494 /* Check results are legal. */
5495 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
5496 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
5497 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
5504 mlxsw_sp_devlink_param_fw_load_policy_validate(struct devlink *devlink, u32 id,
5505 union devlink_param_value val,
5506 struct netlink_ext_ack *extack)
5508 if ((val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER) &&
5509 (val.vu8 != DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH)) {
5510 NL_SET_ERR_MSG_MOD(extack, "'fw_load_policy' must be 'driver' or 'flash'");
5517 static const struct devlink_param mlxsw_sp_devlink_params[] = {
5518 DEVLINK_PARAM_GENERIC(FW_LOAD_POLICY,
5519 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
5521 mlxsw_sp_devlink_param_fw_load_policy_validate),
5524 static int mlxsw_sp_params_register(struct mlxsw_core *mlxsw_core)
5526 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5527 union devlink_param_value value;
5530 err = devlink_params_register(devlink, mlxsw_sp_devlink_params,
5531 ARRAY_SIZE(mlxsw_sp_devlink_params));
5535 value.vu8 = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER;
5536 devlink_param_driverinit_value_set(devlink,
5537 DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5542 static void mlxsw_sp_params_unregister(struct mlxsw_core *mlxsw_core)
5544 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5545 mlxsw_sp_devlink_params,
5546 ARRAY_SIZE(mlxsw_sp_devlink_params));
5550 mlxsw_sp_params_acl_region_rehash_intrvl_get(struct devlink *devlink, u32 id,
5551 struct devlink_param_gset_ctx *ctx)
5553 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5554 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5556 ctx->val.vu32 = mlxsw_sp_acl_region_rehash_intrvl_get(mlxsw_sp);
5561 mlxsw_sp_params_acl_region_rehash_intrvl_set(struct devlink *devlink, u32 id,
5562 struct devlink_param_gset_ctx *ctx)
5564 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
5565 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5567 return mlxsw_sp_acl_region_rehash_intrvl_set(mlxsw_sp, ctx->val.vu32);
5570 static const struct devlink_param mlxsw_sp2_devlink_params[] = {
5571 DEVLINK_PARAM_DRIVER(MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5572 "acl_region_rehash_interval",
5573 DEVLINK_PARAM_TYPE_U32,
5574 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
5575 mlxsw_sp_params_acl_region_rehash_intrvl_get,
5576 mlxsw_sp_params_acl_region_rehash_intrvl_set,
5580 static int mlxsw_sp2_params_register(struct mlxsw_core *mlxsw_core)
5582 struct devlink *devlink = priv_to_devlink(mlxsw_core);
5583 union devlink_param_value value;
5586 err = mlxsw_sp_params_register(mlxsw_core);
5590 err = devlink_params_register(devlink, mlxsw_sp2_devlink_params,
5591 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5593 goto err_devlink_params_register;
5596 devlink_param_driverinit_value_set(devlink,
5597 MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
5601 err_devlink_params_register:
5602 mlxsw_sp_params_unregister(mlxsw_core);
5606 static void mlxsw_sp2_params_unregister(struct mlxsw_core *mlxsw_core)
5608 devlink_params_unregister(priv_to_devlink(mlxsw_core),
5609 mlxsw_sp2_devlink_params,
5610 ARRAY_SIZE(mlxsw_sp2_devlink_params));
5611 mlxsw_sp_params_unregister(mlxsw_core);
5614 static void mlxsw_sp_ptp_transmitted(struct mlxsw_core *mlxsw_core,
5615 struct sk_buff *skb, u8 local_port)
5617 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
5619 skb_pull(skb, MLXSW_TXHDR_LEN);
5620 mlxsw_sp->ptp_ops->transmitted(mlxsw_sp, skb, local_port);
5623 static struct mlxsw_driver mlxsw_sp1_driver = {
5624 .kind = mlxsw_sp1_driver_name,
5625 .priv_size = sizeof(struct mlxsw_sp),
5626 .init = mlxsw_sp1_init,
5627 .fini = mlxsw_sp_fini,
5628 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5629 .port_split = mlxsw_sp_port_split,
5630 .port_unsplit = mlxsw_sp_port_unsplit,
5631 .sb_pool_get = mlxsw_sp_sb_pool_get,
5632 .sb_pool_set = mlxsw_sp_sb_pool_set,
5633 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5634 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5635 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5636 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5637 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5638 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5639 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5640 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5641 .flash_update = mlxsw_sp_flash_update,
5642 .trap_init = mlxsw_sp_trap_init,
5643 .trap_fini = mlxsw_sp_trap_fini,
5644 .trap_action_set = mlxsw_sp_trap_action_set,
5645 .trap_group_init = mlxsw_sp_trap_group_init,
5646 .txhdr_construct = mlxsw_sp_txhdr_construct,
5647 .resources_register = mlxsw_sp1_resources_register,
5648 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
5649 .params_register = mlxsw_sp_params_register,
5650 .params_unregister = mlxsw_sp_params_unregister,
5651 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5652 .txhdr_len = MLXSW_TXHDR_LEN,
5653 .profile = &mlxsw_sp1_config_profile,
5654 .res_query_enabled = true,
5657 static struct mlxsw_driver mlxsw_sp2_driver = {
5658 .kind = mlxsw_sp2_driver_name,
5659 .priv_size = sizeof(struct mlxsw_sp),
5660 .init = mlxsw_sp2_init,
5661 .fini = mlxsw_sp_fini,
5662 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5663 .port_split = mlxsw_sp_port_split,
5664 .port_unsplit = mlxsw_sp_port_unsplit,
5665 .sb_pool_get = mlxsw_sp_sb_pool_get,
5666 .sb_pool_set = mlxsw_sp_sb_pool_set,
5667 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5668 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5669 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5670 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5671 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5672 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5673 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5674 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5675 .flash_update = mlxsw_sp_flash_update,
5676 .trap_init = mlxsw_sp_trap_init,
5677 .trap_fini = mlxsw_sp_trap_fini,
5678 .trap_action_set = mlxsw_sp_trap_action_set,
5679 .trap_group_init = mlxsw_sp_trap_group_init,
5680 .txhdr_construct = mlxsw_sp_txhdr_construct,
5681 .resources_register = mlxsw_sp2_resources_register,
5682 .params_register = mlxsw_sp2_params_register,
5683 .params_unregister = mlxsw_sp2_params_unregister,
5684 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5685 .txhdr_len = MLXSW_TXHDR_LEN,
5686 .profile = &mlxsw_sp2_config_profile,
5687 .res_query_enabled = true,
5690 static struct mlxsw_driver mlxsw_sp3_driver = {
5691 .kind = mlxsw_sp3_driver_name,
5692 .priv_size = sizeof(struct mlxsw_sp),
5693 .init = mlxsw_sp3_init,
5694 .fini = mlxsw_sp_fini,
5695 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
5696 .port_split = mlxsw_sp_port_split,
5697 .port_unsplit = mlxsw_sp_port_unsplit,
5698 .sb_pool_get = mlxsw_sp_sb_pool_get,
5699 .sb_pool_set = mlxsw_sp_sb_pool_set,
5700 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
5701 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
5702 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
5703 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
5704 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
5705 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
5706 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
5707 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
5708 .flash_update = mlxsw_sp_flash_update,
5709 .trap_init = mlxsw_sp_trap_init,
5710 .trap_fini = mlxsw_sp_trap_fini,
5711 .trap_action_set = mlxsw_sp_trap_action_set,
5712 .trap_group_init = mlxsw_sp_trap_group_init,
5713 .txhdr_construct = mlxsw_sp_txhdr_construct,
5714 .resources_register = mlxsw_sp2_resources_register,
5715 .params_register = mlxsw_sp2_params_register,
5716 .params_unregister = mlxsw_sp2_params_unregister,
5717 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
5718 .txhdr_len = MLXSW_TXHDR_LEN,
5719 .profile = &mlxsw_sp2_config_profile,
5720 .res_query_enabled = true,
5723 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
5725 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
5728 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
5730 struct mlxsw_sp_port **p_mlxsw_sp_port = data;
5733 if (mlxsw_sp_port_dev_check(lower_dev)) {
5734 *p_mlxsw_sp_port = netdev_priv(lower_dev);
5741 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
5743 struct mlxsw_sp_port *mlxsw_sp_port;
5745 if (mlxsw_sp_port_dev_check(dev))
5746 return netdev_priv(dev);
5748 mlxsw_sp_port = NULL;
5749 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
5751 return mlxsw_sp_port;
5754 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
5756 struct mlxsw_sp_port *mlxsw_sp_port;
5758 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
5759 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
5762 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
5764 struct mlxsw_sp_port *mlxsw_sp_port;
5766 if (mlxsw_sp_port_dev_check(dev))
5767 return netdev_priv(dev);
5769 mlxsw_sp_port = NULL;
5770 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
5773 return mlxsw_sp_port;
5776 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
5778 struct mlxsw_sp_port *mlxsw_sp_port;
5781 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
5783 dev_hold(mlxsw_sp_port->dev);
5785 return mlxsw_sp_port;
5788 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
5790 dev_put(mlxsw_sp_port->dev);
5794 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
5795 struct net_device *lag_dev)
5797 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
5798 struct net_device *upper_dev;
5799 struct list_head *iter;
5801 if (netif_is_bridge_port(lag_dev))
5802 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
5804 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
5805 if (!netif_is_bridge_port(upper_dev))
5807 br_dev = netdev_master_upper_dev_get(upper_dev);
5808 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
5812 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5814 char sldr_pl[MLXSW_REG_SLDR_LEN];
5816 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
5817 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5820 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
5822 char sldr_pl[MLXSW_REG_SLDR_LEN];
5824 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
5825 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
5828 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
5829 u16 lag_id, u8 port_index)
5831 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5832 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5834 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
5835 lag_id, port_index);
5836 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5839 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
5842 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5843 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5845 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
5847 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5850 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
5853 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5854 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5856 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
5858 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5861 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
5864 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5865 char slcor_pl[MLXSW_REG_SLCOR_LEN];
5867 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
5869 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
5872 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5873 struct net_device *lag_dev,
5876 struct mlxsw_sp_upper *lag;
5877 int free_lag_id = -1;
5881 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
5882 for (i = 0; i < max_lag; i++) {
5883 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
5884 if (lag->ref_count) {
5885 if (lag->dev == lag_dev) {
5889 } else if (free_lag_id < 0) {
5893 if (free_lag_id < 0)
5895 *p_lag_id = free_lag_id;
5900 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
5901 struct net_device *lag_dev,
5902 struct netdev_lag_upper_info *lag_upper_info,
5903 struct netlink_ext_ack *extack)
5907 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
5908 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported LAG devices");
5911 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
5912 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
5918 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
5919 u16 lag_id, u8 *p_port_index)
5921 u64 max_lag_members;
5924 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
5926 for (i = 0; i < max_lag_members; i++) {
5927 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
5935 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
5936 struct net_device *lag_dev)
5938 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5939 struct mlxsw_sp_upper *lag;
5944 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
5947 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5948 if (!lag->ref_count) {
5949 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
5955 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
5958 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
5960 goto err_col_port_add;
5962 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
5963 mlxsw_sp_port->local_port);
5964 mlxsw_sp_port->lag_id = lag_id;
5965 mlxsw_sp_port->lagged = 1;
5968 /* Port is no longer usable as a router interface */
5969 if (mlxsw_sp_port->default_vlan->fid)
5970 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
5975 if (!lag->ref_count)
5976 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
5980 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
5981 struct net_device *lag_dev)
5983 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5984 u16 lag_id = mlxsw_sp_port->lag_id;
5985 struct mlxsw_sp_upper *lag;
5987 if (!mlxsw_sp_port->lagged)
5989 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
5990 WARN_ON(lag->ref_count == 0);
5992 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
5994 /* Any VLANs configured on the port are no longer valid */
5995 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, false);
5996 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port->default_vlan);
5997 /* Make the LAG and its directly linked uppers leave bridges they
6000 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
6002 if (lag->ref_count == 1)
6003 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
6005 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
6006 mlxsw_sp_port->local_port);
6007 mlxsw_sp_port->lagged = 0;
6010 /* Make sure untagged frames are allowed to ingress */
6011 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
6014 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
6017 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
6018 char sldr_pl[MLXSW_REG_SLDR_LEN];
6020 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
6021 mlxsw_sp_port->local_port);
6022 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
6025 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
6028 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
6029 char sldr_pl[MLXSW_REG_SLDR_LEN];
6031 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
6032 mlxsw_sp_port->local_port);
6033 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
6037 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
6041 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
6042 mlxsw_sp_port->lag_id);
6046 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
6048 goto err_dist_port_add;
6053 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
6058 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
6062 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
6063 mlxsw_sp_port->lag_id);
6067 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
6068 mlxsw_sp_port->lag_id);
6070 goto err_col_port_disable;
6074 err_col_port_disable:
6075 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
6079 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
6080 struct netdev_lag_lower_state_info *info)
6082 if (info->tx_enabled)
6083 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
6085 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
6088 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
6091 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
6092 enum mlxsw_reg_spms_state spms_state;
6097 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
6098 MLXSW_REG_SPMS_STATE_DISCARDING;
6100 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
6103 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
6105 for (vid = 0; vid < VLAN_N_VID; vid++)
6106 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
6108 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
6113 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
6118 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
6121 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
6123 goto err_port_stp_set;
6124 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
6127 goto err_port_vlan_set;
6129 for (; vid <= VLAN_N_VID - 1; vid++) {
6130 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
6133 goto err_vid_learning_set;
6138 err_vid_learning_set:
6139 for (vid--; vid >= 1; vid--)
6140 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
6142 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
6144 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
6148 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
6152 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
6153 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
6156 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
6158 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
6159 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
6162 static bool mlxsw_sp_bridge_has_multiple_vxlans(struct net_device *br_dev)
6164 unsigned int num_vxlans = 0;
6165 struct net_device *dev;
6166 struct list_head *iter;
6168 netdev_for_each_lower_dev(br_dev, dev, iter) {
6169 if (netif_is_vxlan(dev))
6173 return num_vxlans > 1;
6176 static bool mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device *br_dev)
6178 DECLARE_BITMAP(vlans, VLAN_N_VID) = {0};
6179 struct net_device *dev;
6180 struct list_head *iter;
6182 netdev_for_each_lower_dev(br_dev, dev, iter) {
6186 if (!netif_is_vxlan(dev))
6189 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
6193 if (test_and_set_bit(pvid, vlans))
6200 static bool mlxsw_sp_bridge_vxlan_is_valid(struct net_device *br_dev,
6201 struct netlink_ext_ack *extack)
6203 if (br_multicast_enabled(br_dev)) {
6204 NL_SET_ERR_MSG_MOD(extack, "Multicast can not be enabled on a bridge with a VxLAN device");
6208 if (!br_vlan_enabled(br_dev) &&
6209 mlxsw_sp_bridge_has_multiple_vxlans(br_dev)) {
6210 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices are not supported in a VLAN-unaware bridge");
6214 if (br_vlan_enabled(br_dev) &&
6215 !mlxsw_sp_bridge_vxlan_vlan_is_valid(br_dev)) {
6216 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices cannot have the same VLAN as PVID and egress untagged");
6223 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
6224 struct net_device *dev,
6225 unsigned long event, void *ptr)
6227 struct netdev_notifier_changeupper_info *info;
6228 struct mlxsw_sp_port *mlxsw_sp_port;
6229 struct netlink_ext_ack *extack;
6230 struct net_device *upper_dev;
6231 struct mlxsw_sp *mlxsw_sp;
6234 mlxsw_sp_port = netdev_priv(dev);
6235 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
6237 extack = netdev_notifier_info_to_extack(&info->info);
6240 case NETDEV_PRECHANGEUPPER:
6241 upper_dev = info->upper_dev;
6242 if (!is_vlan_dev(upper_dev) &&
6243 !netif_is_lag_master(upper_dev) &&
6244 !netif_is_bridge_master(upper_dev) &&
6245 !netif_is_ovs_master(upper_dev) &&
6246 !netif_is_macvlan(upper_dev)) {
6247 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6252 if (netif_is_bridge_master(upper_dev) &&
6253 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
6254 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
6255 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
6257 if (netdev_has_any_upper_dev(upper_dev) &&
6258 (!netif_is_bridge_master(upper_dev) ||
6259 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
6261 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
6264 if (netif_is_lag_master(upper_dev) &&
6265 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
6266 info->upper_info, extack))
6268 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
6269 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
6272 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
6273 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
6274 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
6277 if (netif_is_macvlan(upper_dev) &&
6278 !mlxsw_sp_rif_exists(mlxsw_sp, lower_dev)) {
6279 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6282 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
6283 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
6286 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
6287 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
6291 case NETDEV_CHANGEUPPER:
6292 upper_dev = info->upper_dev;
6293 if (netif_is_bridge_master(upper_dev)) {
6295 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
6300 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
6303 } else if (netif_is_lag_master(upper_dev)) {
6304 if (info->linking) {
6305 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
6308 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
6309 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
6312 } else if (netif_is_ovs_master(upper_dev)) {
6314 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
6316 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
6317 } else if (netif_is_macvlan(upper_dev)) {
6319 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6320 } else if (is_vlan_dev(upper_dev)) {
6321 struct net_device *br_dev;
6323 if (!netif_is_bridge_port(upper_dev))
6327 br_dev = netdev_master_upper_dev_get(upper_dev);
6328 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
6337 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
6338 unsigned long event, void *ptr)
6340 struct netdev_notifier_changelowerstate_info *info;
6341 struct mlxsw_sp_port *mlxsw_sp_port;
6344 mlxsw_sp_port = netdev_priv(dev);
6348 case NETDEV_CHANGELOWERSTATE:
6349 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
6350 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
6351 info->lower_state_info);
6353 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
6361 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
6362 struct net_device *port_dev,
6363 unsigned long event, void *ptr)
6366 case NETDEV_PRECHANGEUPPER:
6367 case NETDEV_CHANGEUPPER:
6368 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
6370 case NETDEV_CHANGELOWERSTATE:
6371 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
6378 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
6379 unsigned long event, void *ptr)
6381 struct net_device *dev;
6382 struct list_head *iter;
6385 netdev_for_each_lower_dev(lag_dev, dev, iter) {
6386 if (mlxsw_sp_port_dev_check(dev)) {
6387 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
6397 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
6398 struct net_device *dev,
6399 unsigned long event, void *ptr,
6402 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
6403 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
6404 struct netdev_notifier_changeupper_info *info = ptr;
6405 struct netlink_ext_ack *extack;
6406 struct net_device *upper_dev;
6409 extack = netdev_notifier_info_to_extack(&info->info);
6412 case NETDEV_PRECHANGEUPPER:
6413 upper_dev = info->upper_dev;
6414 if (!netif_is_bridge_master(upper_dev) &&
6415 !netif_is_macvlan(upper_dev)) {
6416 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6421 if (netif_is_bridge_master(upper_dev) &&
6422 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
6423 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
6424 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
6426 if (netdev_has_any_upper_dev(upper_dev) &&
6427 (!netif_is_bridge_master(upper_dev) ||
6428 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
6430 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
6433 if (netif_is_macvlan(upper_dev) &&
6434 !mlxsw_sp_rif_exists(mlxsw_sp, vlan_dev)) {
6435 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6439 case NETDEV_CHANGEUPPER:
6440 upper_dev = info->upper_dev;
6441 if (netif_is_bridge_master(upper_dev)) {
6443 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
6448 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
6451 } else if (netif_is_macvlan(upper_dev)) {
6453 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6464 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
6465 struct net_device *lag_dev,
6466 unsigned long event,
6469 struct net_device *dev;
6470 struct list_head *iter;
6473 netdev_for_each_lower_dev(lag_dev, dev, iter) {
6474 if (mlxsw_sp_port_dev_check(dev)) {
6475 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
6486 static int mlxsw_sp_netdevice_bridge_vlan_event(struct net_device *vlan_dev,
6487 struct net_device *br_dev,
6488 unsigned long event, void *ptr,
6491 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
6492 struct netdev_notifier_changeupper_info *info = ptr;
6493 struct netlink_ext_ack *extack;
6494 struct net_device *upper_dev;
6499 extack = netdev_notifier_info_to_extack(&info->info);
6502 case NETDEV_PRECHANGEUPPER:
6503 upper_dev = info->upper_dev;
6504 if (!netif_is_macvlan(upper_dev)) {
6505 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6510 if (netif_is_macvlan(upper_dev) &&
6511 !mlxsw_sp_rif_exists(mlxsw_sp, vlan_dev)) {
6512 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6516 case NETDEV_CHANGEUPPER:
6517 upper_dev = info->upper_dev;
6520 if (netif_is_macvlan(upper_dev))
6521 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6528 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
6529 unsigned long event, void *ptr)
6531 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
6532 u16 vid = vlan_dev_vlan_id(vlan_dev);
6534 if (mlxsw_sp_port_dev_check(real_dev))
6535 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
6537 else if (netif_is_lag_master(real_dev))
6538 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
6541 else if (netif_is_bridge_master(real_dev))
6542 return mlxsw_sp_netdevice_bridge_vlan_event(vlan_dev, real_dev,
6548 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
6549 unsigned long event, void *ptr)
6551 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(br_dev);
6552 struct netdev_notifier_changeupper_info *info = ptr;
6553 struct netlink_ext_ack *extack;
6554 struct net_device *upper_dev;
6559 extack = netdev_notifier_info_to_extack(&info->info);
6562 case NETDEV_PRECHANGEUPPER:
6563 upper_dev = info->upper_dev;
6564 if (!is_vlan_dev(upper_dev) && !netif_is_macvlan(upper_dev)) {
6565 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6570 if (netif_is_macvlan(upper_dev) &&
6571 !mlxsw_sp_rif_exists(mlxsw_sp, br_dev)) {
6572 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
6576 case NETDEV_CHANGEUPPER:
6577 upper_dev = info->upper_dev;
6580 if (is_vlan_dev(upper_dev))
6581 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
6582 if (netif_is_macvlan(upper_dev))
6583 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
6590 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
6591 unsigned long event, void *ptr)
6593 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
6594 struct netdev_notifier_changeupper_info *info = ptr;
6595 struct netlink_ext_ack *extack;
6597 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
6600 extack = netdev_notifier_info_to_extack(&info->info);
6602 /* VRF enslavement is handled in mlxsw_sp_netdevice_vrf_event() */
6603 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
6608 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
6610 struct netdev_notifier_changeupper_info *info = ptr;
6612 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
6614 return netif_is_l3_master(info->upper_dev);
6617 static int mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp *mlxsw_sp,
6618 struct net_device *dev,
6619 unsigned long event, void *ptr)
6621 struct netdev_notifier_changeupper_info *cu_info;
6622 struct netdev_notifier_info *info = ptr;
6623 struct netlink_ext_ack *extack;
6624 struct net_device *upper_dev;
6626 extack = netdev_notifier_info_to_extack(info);
6629 case NETDEV_CHANGEUPPER:
6630 cu_info = container_of(info,
6631 struct netdev_notifier_changeupper_info,
6633 upper_dev = cu_info->upper_dev;
6634 if (!netif_is_bridge_master(upper_dev))
6636 if (!mlxsw_sp_lower_get(upper_dev))
6638 if (!mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
6640 if (cu_info->linking) {
6641 if (!netif_running(dev))
6643 /* When the bridge is VLAN-aware, the VNI of the VxLAN
6644 * device needs to be mapped to a VLAN, but at this
6645 * point no VLANs are configured on the VxLAN device
6647 if (br_vlan_enabled(upper_dev))
6649 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev,
6652 /* VLANs were already flushed, which triggered the
6655 if (br_vlan_enabled(upper_dev))
6657 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6661 upper_dev = netdev_master_upper_dev_get(dev);
6664 if (!netif_is_bridge_master(upper_dev))
6666 if (!mlxsw_sp_lower_get(upper_dev))
6668 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev, dev, 0,
6671 upper_dev = netdev_master_upper_dev_get(dev);
6674 if (!netif_is_bridge_master(upper_dev))
6676 if (!mlxsw_sp_lower_get(upper_dev))
6678 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
6685 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
6686 unsigned long event, void *ptr)
6688 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6689 struct mlxsw_sp_span_entry *span_entry;
6690 struct mlxsw_sp *mlxsw_sp;
6693 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
6694 if (event == NETDEV_UNREGISTER) {
6695 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
6697 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
6699 mlxsw_sp_span_respin(mlxsw_sp);
6701 if (netif_is_vxlan(dev))
6702 err = mlxsw_sp_netdevice_vxlan_event(mlxsw_sp, dev, event, ptr);
6703 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
6704 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
6706 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
6707 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
6709 else if (event == NETDEV_PRE_CHANGEADDR ||
6710 event == NETDEV_CHANGEADDR ||
6711 event == NETDEV_CHANGEMTU)
6712 err = mlxsw_sp_netdevice_router_port_event(dev, event, ptr);
6713 else if (mlxsw_sp_is_vrf_event(event, ptr))
6714 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
6715 else if (mlxsw_sp_port_dev_check(dev))
6716 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
6717 else if (netif_is_lag_master(dev))
6718 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
6719 else if (is_vlan_dev(dev))
6720 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
6721 else if (netif_is_bridge_master(dev))
6722 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
6723 else if (netif_is_macvlan(dev))
6724 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
6726 return notifier_from_errno(err);
6729 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
6730 .notifier_call = mlxsw_sp_inetaddr_valid_event,
6733 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
6734 .notifier_call = mlxsw_sp_inet6addr_valid_event,
6737 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
6738 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
6742 static struct pci_driver mlxsw_sp1_pci_driver = {
6743 .name = mlxsw_sp1_driver_name,
6744 .id_table = mlxsw_sp1_pci_id_table,
6747 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
6748 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
6752 static struct pci_driver mlxsw_sp2_pci_driver = {
6753 .name = mlxsw_sp2_driver_name,
6754 .id_table = mlxsw_sp2_pci_id_table,
6757 static const struct pci_device_id mlxsw_sp3_pci_id_table[] = {
6758 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM3), 0},
6762 static struct pci_driver mlxsw_sp3_pci_driver = {
6763 .name = mlxsw_sp3_driver_name,
6764 .id_table = mlxsw_sp3_pci_id_table,
6767 static int __init mlxsw_sp_module_init(void)
6771 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6772 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6774 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
6776 goto err_sp1_core_driver_register;
6778 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
6780 goto err_sp2_core_driver_register;
6782 err = mlxsw_core_driver_register(&mlxsw_sp3_driver);
6784 goto err_sp3_core_driver_register;
6786 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
6788 goto err_sp1_pci_driver_register;
6790 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
6792 goto err_sp2_pci_driver_register;
6794 err = mlxsw_pci_driver_register(&mlxsw_sp3_pci_driver);
6796 goto err_sp3_pci_driver_register;
6800 err_sp3_pci_driver_register:
6801 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
6802 err_sp2_pci_driver_register:
6803 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
6804 err_sp1_pci_driver_register:
6805 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
6806 err_sp3_core_driver_register:
6807 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6808 err_sp2_core_driver_register:
6809 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6810 err_sp1_core_driver_register:
6811 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6812 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6816 static void __exit mlxsw_sp_module_exit(void)
6818 mlxsw_pci_driver_unregister(&mlxsw_sp3_pci_driver);
6819 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
6820 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
6821 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
6822 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
6823 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
6824 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
6825 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
6828 module_init(mlxsw_sp_module_init);
6829 module_exit(mlxsw_sp_module_exit);
6831 MODULE_LICENSE("Dual BSD/GPL");
6832 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
6833 MODULE_DESCRIPTION("Mellanox Spectrum driver");
6834 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
6835 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);
6836 MODULE_DEVICE_TABLE(pci, mlxsw_sp3_pci_id_table);
6837 MODULE_FIRMWARE(MLXSW_SP1_FW_FILENAME);
6838 MODULE_FIRMWARE(MLXSW_SP2_FW_FILENAME);