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/netevent.h>
29 #include <net/addrconf.h>
39 #include "spectrum_cnt.h"
40 #include "spectrum_dpipe.h"
41 #include "spectrum_acl_flex_actions.h"
42 #include "spectrum_span.h"
43 #include "spectrum_ptp.h"
44 #include "spectrum_trap.h"
46 #define MLXSW_SP1_FWREV_MAJOR 13
47 #define MLXSW_SP1_FWREV_MINOR 2008
48 #define MLXSW_SP1_FWREV_SUBMINOR 1310
49 #define MLXSW_SP1_FWREV_CAN_RESET_MINOR 1702
51 static const struct mlxsw_fw_rev mlxsw_sp1_fw_rev = {
52 .major = MLXSW_SP1_FWREV_MAJOR,
53 .minor = MLXSW_SP1_FWREV_MINOR,
54 .subminor = MLXSW_SP1_FWREV_SUBMINOR,
55 .can_reset_minor = MLXSW_SP1_FWREV_CAN_RESET_MINOR,
58 #define MLXSW_SP1_FW_FILENAME \
59 "mellanox/mlxsw_spectrum-" __stringify(MLXSW_SP1_FWREV_MAJOR) \
60 "." __stringify(MLXSW_SP1_FWREV_MINOR) \
61 "." __stringify(MLXSW_SP1_FWREV_SUBMINOR) ".mfa2"
63 #define MLXSW_SP2_FWREV_MAJOR 29
64 #define MLXSW_SP2_FWREV_MINOR 2008
65 #define MLXSW_SP2_FWREV_SUBMINOR 1310
67 static const struct mlxsw_fw_rev mlxsw_sp2_fw_rev = {
68 .major = MLXSW_SP2_FWREV_MAJOR,
69 .minor = MLXSW_SP2_FWREV_MINOR,
70 .subminor = MLXSW_SP2_FWREV_SUBMINOR,
73 #define MLXSW_SP2_FW_FILENAME \
74 "mellanox/mlxsw_spectrum2-" __stringify(MLXSW_SP2_FWREV_MAJOR) \
75 "." __stringify(MLXSW_SP2_FWREV_MINOR) \
76 "." __stringify(MLXSW_SP2_FWREV_SUBMINOR) ".mfa2"
78 #define MLXSW_SP3_FWREV_MAJOR 30
79 #define MLXSW_SP3_FWREV_MINOR 2008
80 #define MLXSW_SP3_FWREV_SUBMINOR 1310
82 static const struct mlxsw_fw_rev mlxsw_sp3_fw_rev = {
83 .major = MLXSW_SP3_FWREV_MAJOR,
84 .minor = MLXSW_SP3_FWREV_MINOR,
85 .subminor = MLXSW_SP3_FWREV_SUBMINOR,
88 #define MLXSW_SP3_FW_FILENAME \
89 "mellanox/mlxsw_spectrum3-" __stringify(MLXSW_SP3_FWREV_MAJOR) \
90 "." __stringify(MLXSW_SP3_FWREV_MINOR) \
91 "." __stringify(MLXSW_SP3_FWREV_SUBMINOR) ".mfa2"
93 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
94 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
95 static const char mlxsw_sp3_driver_name[] = "mlxsw_spectrum3";
97 static const unsigned char mlxsw_sp1_mac_mask[ETH_ALEN] = {
98 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00
100 static const unsigned char mlxsw_sp2_mac_mask[ETH_ALEN] = {
101 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
108 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
111 * Packet control type.
112 * 0 - Ethernet control (e.g. EMADs, LACP)
115 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
118 * Packet protocol type. Must be set to 1 (Ethernet).
120 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
122 /* tx_hdr_rx_is_router
123 * Packet is sent from the router. Valid for data packets only.
125 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
128 * Indicates if the 'fid' field is valid and should be used for
129 * forwarding lookup. Valid for data packets only.
131 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
134 * Switch partition ID. Must be set to 0.
136 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
138 /* tx_hdr_control_tclass
139 * Indicates if the packet should use the control TClass and not one
140 * of the data TClasses.
142 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
145 * Egress TClass to be used on the egress device on the egress port.
147 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
150 * Destination local port for unicast packets.
151 * Destination multicast ID for multicast packets.
153 * Control packets are directed to a specific egress port, while data
154 * packets are transmitted through the CPU port (0) into the switch partition,
155 * where forwarding rules are applied.
157 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
160 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
161 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
162 * Valid for data packets only.
164 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
168 * 6 - Control packets
170 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
172 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
173 unsigned int counter_index, u64 *packets,
176 char mgpc_pl[MLXSW_REG_MGPC_LEN];
179 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
180 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
181 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
185 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
187 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
191 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
192 unsigned int counter_index)
194 char mgpc_pl[MLXSW_REG_MGPC_LEN];
196 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
197 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
198 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
201 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
202 unsigned int *p_counter_index)
206 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
210 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
212 goto err_counter_clear;
216 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
221 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
222 unsigned int counter_index)
224 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
228 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
229 const struct mlxsw_tx_info *tx_info)
231 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
233 memset(txhdr, 0, MLXSW_TXHDR_LEN);
235 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
236 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
237 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
238 mlxsw_tx_hdr_swid_set(txhdr, 0);
239 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
240 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
241 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
244 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
247 case BR_STATE_FORWARDING:
248 return MLXSW_REG_SPMS_STATE_FORWARDING;
249 case BR_STATE_LEARNING:
250 return MLXSW_REG_SPMS_STATE_LEARNING;
251 case BR_STATE_LISTENING:
252 case BR_STATE_DISABLED:
253 case BR_STATE_BLOCKING:
254 return MLXSW_REG_SPMS_STATE_DISCARDING;
260 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
263 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
264 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
268 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
271 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
272 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
274 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
279 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
281 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
284 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
287 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
291 int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
294 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
295 char paos_pl[MLXSW_REG_PAOS_LEN];
297 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
298 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
299 MLXSW_PORT_ADMIN_STATUS_DOWN);
300 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
303 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
306 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
307 char ppad_pl[MLXSW_REG_PPAD_LEN];
309 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
310 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
311 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
314 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
316 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
317 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
319 ether_addr_copy(addr, mlxsw_sp->base_mac);
320 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
321 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
324 static int mlxsw_sp_port_max_mtu_get(struct mlxsw_sp_port *mlxsw_sp_port, int *p_max_mtu)
326 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
327 char pmtu_pl[MLXSW_REG_PMTU_LEN];
330 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
331 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
335 *p_max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
339 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
341 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
342 char pmtu_pl[MLXSW_REG_PMTU_LEN];
344 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
345 if (mtu > mlxsw_sp_port->max_mtu)
348 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
349 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
352 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
354 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
355 char pspa_pl[MLXSW_REG_PSPA_LEN];
357 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
358 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
361 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
363 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
364 char svpe_pl[MLXSW_REG_SVPE_LEN];
366 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
367 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
370 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
373 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
377 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
380 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
382 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
387 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
390 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
391 char spvid_pl[MLXSW_REG_SPVID_LEN];
393 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
394 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
397 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
400 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
401 char spaft_pl[MLXSW_REG_SPAFT_LEN];
403 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
404 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
407 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
412 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
416 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
419 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
421 goto err_port_allow_untagged_set;
424 mlxsw_sp_port->pvid = vid;
427 err_port_allow_untagged_set:
428 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
433 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
435 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
436 char sspr_pl[MLXSW_REG_SSPR_LEN];
438 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
439 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
443 mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, u8 local_port,
444 struct mlxsw_sp_port_mapping *port_mapping)
446 char pmlp_pl[MLXSW_REG_PMLP_LEN];
453 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
454 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
457 module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
458 width = mlxsw_reg_pmlp_width_get(pmlp_pl);
459 separate_rxtx = mlxsw_reg_pmlp_rxtx_get(pmlp_pl);
461 if (width && !is_power_of_2(width)) {
462 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: width value is not power of 2\n",
467 for (i = 0; i < width; i++) {
468 if (mlxsw_reg_pmlp_module_get(pmlp_pl, i) != module) {
469 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: contains multiple modules\n",
474 mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) !=
475 mlxsw_reg_pmlp_rx_lane_get(pmlp_pl, i)) {
476 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are different\n",
480 if (mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) != i) {
481 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are not sequential\n",
487 port_mapping->module = module;
488 port_mapping->width = width;
489 port_mapping->lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
493 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port)
495 struct mlxsw_sp_port_mapping *port_mapping = &mlxsw_sp_port->mapping;
496 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
497 char pmlp_pl[MLXSW_REG_PMLP_LEN];
500 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
501 mlxsw_reg_pmlp_width_set(pmlp_pl, port_mapping->width);
502 for (i = 0; i < port_mapping->width; i++) {
503 mlxsw_reg_pmlp_module_set(pmlp_pl, i, port_mapping->module);
504 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, port_mapping->lane + i); /* Rx & Tx */
507 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
510 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
512 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
513 char pmlp_pl[MLXSW_REG_PMLP_LEN];
515 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
516 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
517 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
520 static int mlxsw_sp_port_open(struct net_device *dev)
522 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
525 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
528 netif_start_queue(dev);
532 static int mlxsw_sp_port_stop(struct net_device *dev)
534 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
536 netif_stop_queue(dev);
537 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
540 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
541 struct net_device *dev)
543 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
544 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
545 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
546 const struct mlxsw_tx_info tx_info = {
547 .local_port = mlxsw_sp_port->local_port,
553 if (skb_cow_head(skb, MLXSW_TXHDR_LEN)) {
554 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
555 dev_kfree_skb_any(skb);
559 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
561 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
562 return NETDEV_TX_BUSY;
564 if (eth_skb_pad(skb)) {
565 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
569 mlxsw_sp_txhdr_construct(skb, &tx_info);
570 /* TX header is consumed by HW on the way so we shouldn't count its
571 * bytes as being sent.
573 len = skb->len - MLXSW_TXHDR_LEN;
575 /* Due to a race we might fail here because of a full queue. In that
576 * unlikely case we simply drop the packet.
578 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
581 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
582 u64_stats_update_begin(&pcpu_stats->syncp);
583 pcpu_stats->tx_packets++;
584 pcpu_stats->tx_bytes += len;
585 u64_stats_update_end(&pcpu_stats->syncp);
587 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
588 dev_kfree_skb_any(skb);
593 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
597 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
599 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
600 struct sockaddr *addr = p;
603 if (!is_valid_ether_addr(addr->sa_data))
604 return -EADDRNOTAVAIL;
606 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
609 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
613 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
615 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
616 struct mlxsw_sp_hdroom orig_hdroom;
617 struct mlxsw_sp_hdroom hdroom;
620 orig_hdroom = *mlxsw_sp_port->hdroom;
622 hdroom = orig_hdroom;
624 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
626 err = mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom);
628 netdev_err(dev, "Failed to configure port's headroom\n");
632 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
634 goto err_port_mtu_set;
639 mlxsw_sp_hdroom_configure(mlxsw_sp_port, &orig_hdroom);
644 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
645 struct rtnl_link_stats64 *stats)
647 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
648 struct mlxsw_sp_port_pcpu_stats *p;
649 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
654 for_each_possible_cpu(i) {
655 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
657 start = u64_stats_fetch_begin_irq(&p->syncp);
658 rx_packets = p->rx_packets;
659 rx_bytes = p->rx_bytes;
660 tx_packets = p->tx_packets;
661 tx_bytes = p->tx_bytes;
662 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
664 stats->rx_packets += rx_packets;
665 stats->rx_bytes += rx_bytes;
666 stats->tx_packets += tx_packets;
667 stats->tx_bytes += tx_bytes;
668 /* tx_dropped is u32, updated without syncp protection. */
669 tx_dropped += p->tx_dropped;
671 stats->tx_dropped = tx_dropped;
675 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
678 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
685 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
689 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
690 return mlxsw_sp_port_get_sw_stats64(dev, sp);
696 int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
697 int prio, char *ppcnt_pl)
699 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
700 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
702 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
703 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
706 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
707 struct rtnl_link_stats64 *stats)
709 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
712 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
718 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
720 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
722 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
724 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
726 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
728 stats->rx_crc_errors =
729 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
730 stats->rx_frame_errors =
731 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
733 stats->rx_length_errors = (
734 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
735 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
736 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
738 stats->rx_errors = (stats->rx_crc_errors +
739 stats->rx_frame_errors + stats->rx_length_errors);
746 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
747 struct mlxsw_sp_port_xstats *xstats)
749 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
752 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
755 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
757 for (i = 0; i < TC_MAX_QUEUE; i++) {
758 err = mlxsw_sp_port_get_stats_raw(dev,
759 MLXSW_REG_PPCNT_TC_CONG_TC,
762 xstats->wred_drop[i] =
763 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
765 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
771 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
772 xstats->tail_drop[i] =
773 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
776 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
777 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
782 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
783 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
787 static void update_stats_cache(struct work_struct *work)
789 struct mlxsw_sp_port *mlxsw_sp_port =
790 container_of(work, struct mlxsw_sp_port,
791 periodic_hw_stats.update_dw.work);
793 if (!netif_carrier_ok(mlxsw_sp_port->dev))
794 /* Note: mlxsw_sp_port_down_wipe_counters() clears the cache as
795 * necessary when port goes down.
799 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
800 &mlxsw_sp_port->periodic_hw_stats.stats);
801 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
802 &mlxsw_sp_port->periodic_hw_stats.xstats);
805 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
806 MLXSW_HW_STATS_UPDATE_TIME);
809 /* Return the stats from a cache that is updated periodically,
810 * as this function might get called in an atomic context.
813 mlxsw_sp_port_get_stats64(struct net_device *dev,
814 struct rtnl_link_stats64 *stats)
816 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
818 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
821 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
822 u16 vid_begin, u16 vid_end,
823 bool is_member, bool untagged)
825 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
829 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
833 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
834 vid_end, is_member, untagged);
835 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
840 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
841 u16 vid_end, bool is_member, bool untagged)
846 for (vid = vid_begin; vid <= vid_end;
847 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
848 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
851 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
852 is_member, untagged);
860 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port,
863 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
865 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
866 &mlxsw_sp_port->vlans_list, list) {
867 if (!flush_default &&
868 mlxsw_sp_port_vlan->vid == MLXSW_SP_DEFAULT_VID)
870 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
875 mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
877 if (mlxsw_sp_port_vlan->bridge_port)
878 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
879 else if (mlxsw_sp_port_vlan->fid)
880 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
883 struct mlxsw_sp_port_vlan *
884 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
886 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
887 bool untagged = vid == MLXSW_SP_DEFAULT_VID;
890 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
891 if (mlxsw_sp_port_vlan)
892 return ERR_PTR(-EEXIST);
894 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
898 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
899 if (!mlxsw_sp_port_vlan) {
901 goto err_port_vlan_alloc;
904 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
905 mlxsw_sp_port_vlan->vid = vid;
906 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
908 return mlxsw_sp_port_vlan;
911 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
915 void mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
917 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
918 u16 vid = mlxsw_sp_port_vlan->vid;
920 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port_vlan);
921 list_del(&mlxsw_sp_port_vlan->list);
922 kfree(mlxsw_sp_port_vlan);
923 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
926 static int mlxsw_sp_port_add_vid(struct net_device *dev,
927 __be16 __always_unused proto, u16 vid)
929 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
931 /* VLAN 0 is added to HW filter when device goes up, but it is
932 * reserved in our case, so simply return.
937 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid));
940 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
941 __be16 __always_unused proto, u16 vid)
943 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
944 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
946 /* VLAN 0 is removed from HW filter when device goes down, but
947 * it is reserved in our case, so simply return.
952 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
953 if (!mlxsw_sp_port_vlan)
955 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
960 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
961 struct flow_block_offload *f)
963 switch (f->binder_type) {
964 case FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS:
965 return mlxsw_sp_setup_tc_block_clsact(mlxsw_sp_port, f, true);
966 case FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS:
967 return mlxsw_sp_setup_tc_block_clsact(mlxsw_sp_port, f, false);
968 case FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP:
969 return mlxsw_sp_setup_tc_block_qevent_early_drop(mlxsw_sp_port, f);
975 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
978 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
982 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
983 case TC_SETUP_QDISC_RED:
984 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
985 case TC_SETUP_QDISC_PRIO:
986 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
987 case TC_SETUP_QDISC_ETS:
988 return mlxsw_sp_setup_tc_ets(mlxsw_sp_port, type_data);
989 case TC_SETUP_QDISC_TBF:
990 return mlxsw_sp_setup_tc_tbf(mlxsw_sp_port, type_data);
991 case TC_SETUP_QDISC_FIFO:
992 return mlxsw_sp_setup_tc_fifo(mlxsw_sp_port, type_data);
998 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1000 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1003 if (mlxsw_sp_flow_block_rule_count(mlxsw_sp_port->ing_flow_block) ||
1004 mlxsw_sp_flow_block_rule_count(mlxsw_sp_port->eg_flow_block)) {
1005 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1008 mlxsw_sp_flow_block_disable_inc(mlxsw_sp_port->ing_flow_block);
1009 mlxsw_sp_flow_block_disable_inc(mlxsw_sp_port->eg_flow_block);
1011 mlxsw_sp_flow_block_disable_dec(mlxsw_sp_port->ing_flow_block);
1012 mlxsw_sp_flow_block_disable_dec(mlxsw_sp_port->eg_flow_block);
1017 static int mlxsw_sp_feature_loopback(struct net_device *dev, bool enable)
1019 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1020 char pplr_pl[MLXSW_REG_PPLR_LEN];
1023 if (netif_running(dev))
1024 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1026 mlxsw_reg_pplr_pack(pplr_pl, mlxsw_sp_port->local_port, enable);
1027 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pplr),
1030 if (netif_running(dev))
1031 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1036 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1038 static int mlxsw_sp_handle_feature(struct net_device *dev,
1039 netdev_features_t wanted_features,
1040 netdev_features_t feature,
1041 mlxsw_sp_feature_handler feature_handler)
1043 netdev_features_t changes = wanted_features ^ dev->features;
1044 bool enable = !!(wanted_features & feature);
1047 if (!(changes & feature))
1050 err = feature_handler(dev, enable);
1052 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1053 enable ? "Enable" : "Disable", &feature, err);
1058 dev->features |= feature;
1060 dev->features &= ~feature;
1064 static int mlxsw_sp_set_features(struct net_device *dev,
1065 netdev_features_t features)
1067 netdev_features_t oper_features = dev->features;
1070 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1071 mlxsw_sp_feature_hw_tc);
1072 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_LOOPBACK,
1073 mlxsw_sp_feature_loopback);
1076 dev->features = oper_features;
1083 static struct devlink_port *
1084 mlxsw_sp_port_get_devlink_port(struct net_device *dev)
1086 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1087 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1089 return mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
1090 mlxsw_sp_port->local_port);
1093 static int mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1096 struct hwtstamp_config config;
1099 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1102 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port,
1107 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1113 static int mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1116 struct hwtstamp_config config;
1119 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_get(mlxsw_sp_port,
1124 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1130 static inline void mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port *mlxsw_sp_port)
1132 struct hwtstamp_config config = {0};
1134 mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port, &config);
1138 mlxsw_sp_port_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1140 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1144 return mlxsw_sp_port_hwtstamp_set(mlxsw_sp_port, ifr);
1146 return mlxsw_sp_port_hwtstamp_get(mlxsw_sp_port, ifr);
1152 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1153 .ndo_open = mlxsw_sp_port_open,
1154 .ndo_stop = mlxsw_sp_port_stop,
1155 .ndo_start_xmit = mlxsw_sp_port_xmit,
1156 .ndo_setup_tc = mlxsw_sp_setup_tc,
1157 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1158 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1159 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1160 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1161 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1162 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1163 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1164 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1165 .ndo_set_features = mlxsw_sp_set_features,
1166 .ndo_get_devlink_port = mlxsw_sp_port_get_devlink_port,
1167 .ndo_do_ioctl = mlxsw_sp_port_ioctl,
1171 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port)
1173 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1174 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
1175 const struct mlxsw_sp_port_type_speed_ops *ops;
1176 char ptys_pl[MLXSW_REG_PTYS_LEN];
1177 u32 eth_proto_cap_masked;
1180 ops = mlxsw_sp->port_type_speed_ops;
1182 /* Set advertised speeds to speeds supported by both the driver
1185 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
1187 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1191 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
1192 ð_proto_admin, ð_proto_oper);
1193 eth_proto_cap_masked = ops->ptys_proto_cap_masked_get(eth_proto_cap);
1194 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
1195 eth_proto_cap_masked,
1196 mlxsw_sp_port->link.autoneg);
1197 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1200 int mlxsw_sp_port_speed_get(struct mlxsw_sp_port *mlxsw_sp_port, u32 *speed)
1202 const struct mlxsw_sp_port_type_speed_ops *port_type_speed_ops;
1203 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1204 char ptys_pl[MLXSW_REG_PTYS_LEN];
1208 port_type_speed_ops = mlxsw_sp->port_type_speed_ops;
1209 port_type_speed_ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl,
1210 mlxsw_sp_port->local_port, 0,
1212 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1215 port_type_speed_ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, NULL, NULL,
1217 *speed = port_type_speed_ops->from_ptys_speed(mlxsw_sp, eth_proto_oper);
1221 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
1222 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
1223 bool dwrr, u8 dwrr_weight)
1225 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1226 char qeec_pl[MLXSW_REG_QEEC_LEN];
1228 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1230 mlxsw_reg_qeec_de_set(qeec_pl, true);
1231 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
1232 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
1233 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1236 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
1237 enum mlxsw_reg_qeec_hr hr, u8 index,
1238 u8 next_index, u32 maxrate, u8 burst_size)
1240 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1241 char qeec_pl[MLXSW_REG_QEEC_LEN];
1243 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1245 mlxsw_reg_qeec_mase_set(qeec_pl, true);
1246 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
1247 mlxsw_reg_qeec_max_shaper_bs_set(qeec_pl, burst_size);
1248 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1251 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
1252 enum mlxsw_reg_qeec_hr hr, u8 index,
1253 u8 next_index, u32 minrate)
1255 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1256 char qeec_pl[MLXSW_REG_QEEC_LEN];
1258 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1260 mlxsw_reg_qeec_mise_set(qeec_pl, true);
1261 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
1263 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1266 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
1267 u8 switch_prio, u8 tclass)
1269 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1270 char qtct_pl[MLXSW_REG_QTCT_LEN];
1272 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
1274 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
1277 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
1281 /* Setup the elements hierarcy, so that each TC is linked to
1282 * one subgroup, which are all member in the same group.
1284 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1285 MLXSW_REG_QEEC_HR_GROUP, 0, 0, false, 0);
1288 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1289 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1290 MLXSW_REG_QEEC_HR_SUBGROUP, i,
1295 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1296 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1297 MLXSW_REG_QEEC_HR_TC, i, i,
1302 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1303 MLXSW_REG_QEEC_HR_TC,
1310 /* Make sure the max shaper is disabled in all hierarchies that support
1311 * it. Note that this disables ptps (PTP shaper), but that is intended
1312 * for the initial configuration.
1314 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1315 MLXSW_REG_QEEC_HR_PORT, 0, 0,
1316 MLXSW_REG_QEEC_MAS_DIS, 0);
1319 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1320 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1321 MLXSW_REG_QEEC_HR_SUBGROUP,
1323 MLXSW_REG_QEEC_MAS_DIS, 0);
1327 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1328 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1329 MLXSW_REG_QEEC_HR_TC,
1331 MLXSW_REG_QEEC_MAS_DIS, 0);
1335 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1336 MLXSW_REG_QEEC_HR_TC,
1338 MLXSW_REG_QEEC_MAS_DIS, 0);
1343 /* Configure the min shaper for multicast TCs. */
1344 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1345 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
1346 MLXSW_REG_QEEC_HR_TC,
1348 MLXSW_REG_QEEC_MIS_MIN);
1353 /* Map all priorities to traffic class 0. */
1354 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1355 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
1363 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
1366 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1367 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
1369 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
1370 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
1373 static int mlxsw_sp_port_overheat_init_val_set(struct mlxsw_sp_port *mlxsw_sp_port)
1375 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1376 u8 module = mlxsw_sp_port->mapping.module;
1377 u64 overheat_counter;
1380 err = mlxsw_env_module_overheat_counter_get(mlxsw_sp->core, module,
1385 mlxsw_sp_port->module_overheat_initial_val = overheat_counter;
1389 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1390 u8 split_base_local_port,
1391 struct mlxsw_sp_port_mapping *port_mapping)
1393 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1394 bool split = !!split_base_local_port;
1395 struct mlxsw_sp_port *mlxsw_sp_port;
1396 u32 lanes = port_mapping->width;
1397 struct net_device *dev;
1401 splittable = lanes > 1 && !split;
1402 err = mlxsw_core_port_init(mlxsw_sp->core, local_port,
1403 port_mapping->module + 1, split,
1404 port_mapping->lane / lanes,
1407 sizeof(mlxsw_sp->base_mac));
1409 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
1414 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
1417 goto err_alloc_etherdev;
1419 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
1420 dev_net_set(dev, mlxsw_sp_net(mlxsw_sp));
1421 mlxsw_sp_port = netdev_priv(dev);
1422 mlxsw_sp_port->dev = dev;
1423 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
1424 mlxsw_sp_port->local_port = local_port;
1425 mlxsw_sp_port->pvid = MLXSW_SP_DEFAULT_VID;
1426 mlxsw_sp_port->split = split;
1427 mlxsw_sp_port->split_base_local_port = split_base_local_port;
1428 mlxsw_sp_port->mapping = *port_mapping;
1429 mlxsw_sp_port->link.autoneg = 1;
1430 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
1432 mlxsw_sp_port->pcpu_stats =
1433 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
1434 if (!mlxsw_sp_port->pcpu_stats) {
1436 goto err_alloc_stats;
1439 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1440 &update_stats_cache);
1442 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
1443 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
1445 err = mlxsw_sp_port_module_map(mlxsw_sp_port);
1447 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
1448 mlxsw_sp_port->local_port);
1449 goto err_port_module_map;
1452 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
1454 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
1455 mlxsw_sp_port->local_port);
1456 goto err_port_swid_set;
1459 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
1461 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
1462 mlxsw_sp_port->local_port);
1463 goto err_dev_addr_init;
1466 netif_carrier_off(dev);
1468 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
1469 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1470 dev->hw_features |= NETIF_F_HW_TC | NETIF_F_LOOPBACK;
1473 dev->max_mtu = ETH_MAX_MTU;
1475 /* Each packet needs to have a Tx header (metadata) on top all other
1478 dev->needed_headroom = MLXSW_TXHDR_LEN;
1480 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
1482 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1483 mlxsw_sp_port->local_port);
1484 goto err_port_system_port_mapping_set;
1487 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port);
1489 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
1490 mlxsw_sp_port->local_port);
1491 goto err_port_speed_by_width_set;
1494 err = mlxsw_sp->port_type_speed_ops->ptys_max_speed(mlxsw_sp_port,
1495 &mlxsw_sp_port->max_speed);
1497 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to get maximum speed\n",
1498 mlxsw_sp_port->local_port);
1499 goto err_max_speed_get;
1502 err = mlxsw_sp_port_max_mtu_get(mlxsw_sp_port, &mlxsw_sp_port->max_mtu);
1504 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to get maximum MTU\n",
1505 mlxsw_sp_port->local_port);
1506 goto err_port_max_mtu_get;
1509 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
1511 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
1512 mlxsw_sp_port->local_port);
1513 goto err_port_mtu_set;
1516 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1518 goto err_port_admin_status_set;
1520 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
1522 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
1523 mlxsw_sp_port->local_port);
1524 goto err_port_buffers_init;
1527 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
1529 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
1530 mlxsw_sp_port->local_port);
1531 goto err_port_ets_init;
1534 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
1536 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
1537 mlxsw_sp_port->local_port);
1538 goto err_port_tc_mc_mode;
1541 /* ETS and buffers must be initialized before DCB. */
1542 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
1544 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
1545 mlxsw_sp_port->local_port);
1546 goto err_port_dcb_init;
1549 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
1551 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
1552 mlxsw_sp_port->local_port);
1553 goto err_port_fids_init;
1556 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
1558 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
1559 mlxsw_sp_port->local_port);
1560 goto err_port_qdiscs_init;
1563 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 0, VLAN_N_VID - 1, false,
1566 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to clear VLAN filter\n",
1567 mlxsw_sp_port->local_port);
1568 goto err_port_vlan_clear;
1571 err = mlxsw_sp_port_nve_init(mlxsw_sp_port);
1573 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize NVE\n",
1574 mlxsw_sp_port->local_port);
1575 goto err_port_nve_init;
1578 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
1580 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set PVID\n",
1581 mlxsw_sp_port->local_port);
1582 goto err_port_pvid_set;
1585 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1586 MLXSW_SP_DEFAULT_VID);
1587 if (IS_ERR(mlxsw_sp_port_vlan)) {
1588 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
1589 mlxsw_sp_port->local_port);
1590 err = PTR_ERR(mlxsw_sp_port_vlan);
1591 goto err_port_vlan_create;
1593 mlxsw_sp_port->default_vlan = mlxsw_sp_port_vlan;
1595 INIT_DELAYED_WORK(&mlxsw_sp_port->ptp.shaper_dw,
1596 mlxsw_sp->ptp_ops->shaper_work);
1598 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
1600 err = mlxsw_sp_port_overheat_init_val_set(mlxsw_sp_port);
1602 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set overheat initial value\n",
1603 mlxsw_sp_port->local_port);
1604 goto err_port_overheat_init_val_set;
1607 err = register_netdev(dev);
1609 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
1610 mlxsw_sp_port->local_port);
1611 goto err_register_netdev;
1614 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
1615 mlxsw_sp_port, dev);
1616 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
1619 err_register_netdev:
1620 err_port_overheat_init_val_set:
1621 mlxsw_sp->ports[local_port] = NULL;
1622 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1623 err_port_vlan_create:
1625 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
1627 err_port_vlan_clear:
1628 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
1629 err_port_qdiscs_init:
1630 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
1632 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
1634 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
1635 err_port_tc_mc_mode:
1637 mlxsw_sp_port_buffers_fini(mlxsw_sp_port);
1638 err_port_buffers_init:
1639 err_port_admin_status_set:
1641 err_port_max_mtu_get:
1643 err_port_speed_by_width_set:
1644 err_port_system_port_mapping_set:
1646 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
1648 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
1649 err_port_module_map:
1650 free_percpu(mlxsw_sp_port->pcpu_stats);
1654 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
1658 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
1660 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
1662 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
1663 cancel_delayed_work_sync(&mlxsw_sp_port->ptp.shaper_dw);
1664 mlxsw_sp_port_ptp_clear(mlxsw_sp_port);
1665 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
1666 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
1667 mlxsw_sp->ports[local_port] = NULL;
1668 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, true);
1669 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
1670 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
1671 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
1672 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
1673 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
1674 mlxsw_sp_port_buffers_fini(mlxsw_sp_port);
1675 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
1676 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
1677 free_percpu(mlxsw_sp_port->pcpu_stats);
1678 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
1679 free_netdev(mlxsw_sp_port->dev);
1680 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
1683 static int mlxsw_sp_cpu_port_create(struct mlxsw_sp *mlxsw_sp)
1685 struct mlxsw_sp_port *mlxsw_sp_port;
1688 mlxsw_sp_port = kzalloc(sizeof(*mlxsw_sp_port), GFP_KERNEL);
1692 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
1693 mlxsw_sp_port->local_port = MLXSW_PORT_CPU_PORT;
1695 err = mlxsw_core_cpu_port_init(mlxsw_sp->core,
1698 sizeof(mlxsw_sp->base_mac));
1700 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize core CPU port\n");
1701 goto err_core_cpu_port_init;
1704 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = mlxsw_sp_port;
1707 err_core_cpu_port_init:
1708 kfree(mlxsw_sp_port);
1712 static void mlxsw_sp_cpu_port_remove(struct mlxsw_sp *mlxsw_sp)
1714 struct mlxsw_sp_port *mlxsw_sp_port =
1715 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT];
1717 mlxsw_core_cpu_port_fini(mlxsw_sp->core);
1718 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = NULL;
1719 kfree(mlxsw_sp_port);
1722 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
1724 return mlxsw_sp->ports[local_port] != NULL;
1727 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
1731 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
1732 if (mlxsw_sp_port_created(mlxsw_sp, i))
1733 mlxsw_sp_port_remove(mlxsw_sp, i);
1734 mlxsw_sp_cpu_port_remove(mlxsw_sp);
1735 kfree(mlxsw_sp->ports);
1736 mlxsw_sp->ports = NULL;
1739 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
1741 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1742 struct mlxsw_sp_port_mapping *port_mapping;
1747 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
1748 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
1749 if (!mlxsw_sp->ports)
1752 err = mlxsw_sp_cpu_port_create(mlxsw_sp);
1754 goto err_cpu_port_create;
1756 for (i = 1; i < max_ports; i++) {
1757 port_mapping = mlxsw_sp->port_mapping[i];
1760 err = mlxsw_sp_port_create(mlxsw_sp, i, 0, port_mapping);
1762 goto err_port_create;
1767 for (i--; i >= 1; i--)
1768 if (mlxsw_sp_port_created(mlxsw_sp, i))
1769 mlxsw_sp_port_remove(mlxsw_sp, i);
1770 mlxsw_sp_cpu_port_remove(mlxsw_sp);
1771 err_cpu_port_create:
1772 kfree(mlxsw_sp->ports);
1773 mlxsw_sp->ports = NULL;
1777 static int mlxsw_sp_port_module_info_init(struct mlxsw_sp *mlxsw_sp)
1779 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1780 struct mlxsw_sp_port_mapping port_mapping;
1784 mlxsw_sp->port_mapping = kcalloc(max_ports,
1785 sizeof(struct mlxsw_sp_port_mapping *),
1787 if (!mlxsw_sp->port_mapping)
1790 for (i = 1; i < max_ports; i++) {
1791 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &port_mapping);
1793 goto err_port_module_info_get;
1794 if (!port_mapping.width)
1797 mlxsw_sp->port_mapping[i] = kmemdup(&port_mapping,
1798 sizeof(port_mapping),
1800 if (!mlxsw_sp->port_mapping[i]) {
1802 goto err_port_module_info_dup;
1807 err_port_module_info_get:
1808 err_port_module_info_dup:
1809 for (i--; i >= 1; i--)
1810 kfree(mlxsw_sp->port_mapping[i]);
1811 kfree(mlxsw_sp->port_mapping);
1815 static void mlxsw_sp_port_module_info_fini(struct mlxsw_sp *mlxsw_sp)
1819 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
1820 kfree(mlxsw_sp->port_mapping[i]);
1821 kfree(mlxsw_sp->port_mapping);
1824 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port, unsigned int max_width)
1826 u8 offset = (local_port - 1) % max_width;
1828 return local_port - offset;
1832 mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
1833 struct mlxsw_sp_port_mapping *port_mapping,
1834 unsigned int count, u8 offset)
1836 struct mlxsw_sp_port_mapping split_port_mapping;
1839 split_port_mapping = *port_mapping;
1840 split_port_mapping.width /= count;
1841 for (i = 0; i < count; i++) {
1842 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i * offset,
1843 base_port, &split_port_mapping);
1845 goto err_port_create;
1846 split_port_mapping.lane += split_port_mapping.width;
1852 for (i--; i >= 0; i--)
1853 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
1854 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
1858 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
1860 unsigned int count, u8 offset)
1862 struct mlxsw_sp_port_mapping *port_mapping;
1865 /* Go over original unsplit ports in the gap and recreate them. */
1866 for (i = 0; i < count * offset; i++) {
1867 port_mapping = mlxsw_sp->port_mapping[base_port + i];
1870 mlxsw_sp_port_create(mlxsw_sp, base_port + i, 0, port_mapping);
1874 static int mlxsw_sp_local_ports_offset(struct mlxsw_core *mlxsw_core,
1876 unsigned int max_width)
1878 enum mlxsw_res_id local_ports_in_x_res_id;
1879 int split_width = max_width / count;
1881 if (split_width == 1)
1882 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_1X;
1883 else if (split_width == 2)
1884 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_2X;
1885 else if (split_width == 4)
1886 local_ports_in_x_res_id = MLXSW_RES_ID_LOCAL_PORTS_IN_4X;
1890 if (!mlxsw_core_res_valid(mlxsw_core, local_ports_in_x_res_id))
1892 return mlxsw_core_res_get(mlxsw_core, local_ports_in_x_res_id);
1895 static struct mlxsw_sp_port *
1896 mlxsw_sp_port_get_by_local_port(struct mlxsw_sp *mlxsw_sp, u8 local_port)
1898 if (mlxsw_sp->ports && mlxsw_sp->ports[local_port])
1899 return mlxsw_sp->ports[local_port];
1903 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
1905 struct netlink_ext_ack *extack)
1907 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1908 struct mlxsw_sp_port_mapping port_mapping;
1909 struct mlxsw_sp_port *mlxsw_sp_port;
1916 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
1917 if (!mlxsw_sp_port) {
1918 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
1920 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
1924 max_width = mlxsw_core_module_max_width(mlxsw_core,
1925 mlxsw_sp_port->mapping.module);
1926 if (max_width < 0) {
1927 netdev_err(mlxsw_sp_port->dev, "Cannot get max width of port module\n");
1928 NL_SET_ERR_MSG_MOD(extack, "Cannot get max width of port module");
1932 /* Split port with non-max cannot be split. */
1933 if (mlxsw_sp_port->mapping.width != max_width) {
1934 netdev_err(mlxsw_sp_port->dev, "Port cannot be split\n");
1935 NL_SET_ERR_MSG_MOD(extack, "Port cannot be split");
1939 offset = mlxsw_sp_local_ports_offset(mlxsw_core, count, max_width);
1941 netdev_err(mlxsw_sp_port->dev, "Cannot obtain local port offset\n");
1942 NL_SET_ERR_MSG_MOD(extack, "Cannot obtain local port offset");
1946 /* Only in case max split is being done, the local port and
1947 * base port may differ.
1949 base_port = count == max_width ?
1950 mlxsw_sp_cluster_base_port_get(local_port, max_width) :
1953 for (i = 0; i < count * offset; i++) {
1954 /* Expect base port to exist and also the one in the middle in
1955 * case of maximal split count.
1957 if (i == 0 || (count == max_width && i == count / 2))
1960 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i)) {
1961 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
1962 NL_SET_ERR_MSG_MOD(extack, "Invalid split configuration");
1967 port_mapping = mlxsw_sp_port->mapping;
1969 for (i = 0; i < count; i++)
1970 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
1971 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
1973 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, &port_mapping,
1976 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
1977 goto err_port_split_create;
1982 err_port_split_create:
1983 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count, offset);
1987 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port,
1988 struct netlink_ext_ack *extack)
1990 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1991 struct mlxsw_sp_port *mlxsw_sp_port;
1998 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
1999 if (!mlxsw_sp_port) {
2000 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2002 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
2006 if (!mlxsw_sp_port->split) {
2007 netdev_err(mlxsw_sp_port->dev, "Port was not split\n");
2008 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
2012 max_width = mlxsw_core_module_max_width(mlxsw_core,
2013 mlxsw_sp_port->mapping.module);
2014 if (max_width < 0) {
2015 netdev_err(mlxsw_sp_port->dev, "Cannot get max width of port module\n");
2016 NL_SET_ERR_MSG_MOD(extack, "Cannot get max width of port module");
2020 count = max_width / mlxsw_sp_port->mapping.width;
2022 offset = mlxsw_sp_local_ports_offset(mlxsw_core, count, max_width);
2023 if (WARN_ON(offset < 0)) {
2024 netdev_err(mlxsw_sp_port->dev, "Cannot obtain local port offset\n");
2025 NL_SET_ERR_MSG_MOD(extack, "Cannot obtain local port offset");
2029 base_port = mlxsw_sp_port->split_base_local_port;
2031 for (i = 0; i < count; i++)
2032 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i * offset))
2033 mlxsw_sp_port_remove(mlxsw_sp, base_port + i * offset);
2035 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count, offset);
2041 mlxsw_sp_port_down_wipe_counters(struct mlxsw_sp_port *mlxsw_sp_port)
2045 for (i = 0; i < TC_MAX_QUEUE; i++)
2046 mlxsw_sp_port->periodic_hw_stats.xstats.backlog[i] = 0;
2049 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
2050 char *pude_pl, void *priv)
2052 struct mlxsw_sp *mlxsw_sp = priv;
2053 struct mlxsw_sp_port *mlxsw_sp_port;
2054 enum mlxsw_reg_pude_oper_status status;
2057 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
2058 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2062 status = mlxsw_reg_pude_oper_status_get(pude_pl);
2063 if (status == MLXSW_PORT_OPER_STATUS_UP) {
2064 netdev_info(mlxsw_sp_port->dev, "link up\n");
2065 netif_carrier_on(mlxsw_sp_port->dev);
2066 mlxsw_core_schedule_dw(&mlxsw_sp_port->ptp.shaper_dw, 0);
2068 netdev_info(mlxsw_sp_port->dev, "link down\n");
2069 netif_carrier_off(mlxsw_sp_port->dev);
2070 mlxsw_sp_port_down_wipe_counters(mlxsw_sp_port);
2074 static void mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp *mlxsw_sp,
2075 char *mtpptr_pl, bool ingress)
2081 local_port = mlxsw_reg_mtpptr_local_port_get(mtpptr_pl);
2082 num_rec = mlxsw_reg_mtpptr_num_rec_get(mtpptr_pl);
2083 for (i = 0; i < num_rec; i++) {
2089 mlxsw_reg_mtpptr_unpack(mtpptr_pl, i, &message_type,
2090 &domain_number, &sequence_id,
2092 mlxsw_sp1_ptp_got_timestamp(mlxsw_sp, ingress, local_port,
2093 message_type, domain_number,
2094 sequence_id, timestamp);
2098 static void mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info *reg,
2099 char *mtpptr_pl, void *priv)
2101 struct mlxsw_sp *mlxsw_sp = priv;
2103 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, true);
2106 static void mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info *reg,
2107 char *mtpptr_pl, void *priv)
2109 struct mlxsw_sp *mlxsw_sp = priv;
2111 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, false);
2114 void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
2115 u8 local_port, void *priv)
2117 struct mlxsw_sp *mlxsw_sp = priv;
2118 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2119 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
2121 if (unlikely(!mlxsw_sp_port)) {
2122 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
2127 skb->dev = mlxsw_sp_port->dev;
2129 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
2130 u64_stats_update_begin(&pcpu_stats->syncp);
2131 pcpu_stats->rx_packets++;
2132 pcpu_stats->rx_bytes += skb->len;
2133 u64_stats_update_end(&pcpu_stats->syncp);
2135 skb->protocol = eth_type_trans(skb, skb->dev);
2136 netif_receive_skb(skb);
2139 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
2142 skb->offload_fwd_mark = 1;
2143 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
2146 static void mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff *skb,
2147 u8 local_port, void *priv)
2149 skb->offload_l3_fwd_mark = 1;
2150 skb->offload_fwd_mark = 1;
2151 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
2154 void mlxsw_sp_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
2157 mlxsw_sp->ptp_ops->receive(mlxsw_sp, skb, local_port);
2160 void mlxsw_sp_sample_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
2163 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2164 struct mlxsw_sp_port_sample *sample;
2167 if (unlikely(!mlxsw_sp_port)) {
2168 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
2174 sample = rcu_dereference(mlxsw_sp_port->sample);
2177 size = sample->truncate ? sample->trunc_size : skb->len;
2178 psample_sample_packet(sample->psample_group, skb, size,
2179 mlxsw_sp_port->dev->ifindex, 0, sample->rate);
2186 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2187 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
2188 _is_ctrl, SP_##_trap_group, DISCARD)
2190 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2191 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
2192 _is_ctrl, SP_##_trap_group, DISCARD)
2194 #define MLXSW_SP_RXL_L3_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2195 MLXSW_RXL(mlxsw_sp_rx_listener_l3_mark_func, _trap_id, _action, \
2196 _is_ctrl, SP_##_trap_group, DISCARD)
2198 #define MLXSW_SP_EVENTL(_func, _trap_id) \
2199 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
2201 static const struct mlxsw_listener mlxsw_sp_listener[] = {
2203 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
2205 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, FID_MISS, false),
2207 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
2209 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
2210 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
2212 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_CLASS_E, FORWARD,
2214 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_MC_DMAC, FORWARD,
2216 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_DIP, FORWARD,
2218 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_DIP_LINK_LOCAL, FORWARD,
2220 /* Multicast Router Traps */
2221 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
2222 MLXSW_SP_RXL_L3_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
2224 MLXSW_SP_RXL_MARK(NVE_ENCAP_ARP, TRAP_TO_CPU, NEIGH_DISCOVERY, false),
2227 static const struct mlxsw_listener mlxsw_sp1_listener[] = {
2229 MLXSW_EVENTL(mlxsw_sp1_ptp_egr_fifo_event_func, PTP_EGR_FIFO, SP_PTP0),
2230 MLXSW_EVENTL(mlxsw_sp1_ptp_ing_fifo_event_func, PTP_ING_FIFO, SP_PTP0),
2233 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
2235 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2236 char qpcr_pl[MLXSW_REG_QPCR_LEN];
2237 enum mlxsw_reg_qpcr_ir_units ir_units;
2238 int max_cpu_policers;
2244 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
2247 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2249 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
2250 for (i = 0; i < max_cpu_policers; i++) {
2253 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2254 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
2255 case MLXSW_REG_HTGT_TRAP_GROUP_SP_FID_MISS:
2263 __set_bit(i, mlxsw_sp->trap->policers_usage);
2264 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
2266 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
2274 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
2276 char htgt_pl[MLXSW_REG_HTGT_LEN];
2277 enum mlxsw_reg_htgt_trap_group i;
2278 int max_cpu_policers;
2279 int max_trap_groups;
2284 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
2287 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
2288 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2290 for (i = 0; i < max_trap_groups; i++) {
2293 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2294 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
2295 case MLXSW_REG_HTGT_TRAP_GROUP_SP_FID_MISS:
2299 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
2300 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
2301 tc = MLXSW_REG_HTGT_DEFAULT_TC;
2302 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
2308 if (max_cpu_policers <= policer_id &&
2309 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
2312 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
2313 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2321 static int mlxsw_sp_traps_register(struct mlxsw_sp *mlxsw_sp,
2322 const struct mlxsw_listener listeners[],
2323 size_t listeners_count)
2328 for (i = 0; i < listeners_count; i++) {
2329 err = mlxsw_core_trap_register(mlxsw_sp->core,
2333 goto err_listener_register;
2338 err_listener_register:
2339 for (i--; i >= 0; i--) {
2340 mlxsw_core_trap_unregister(mlxsw_sp->core,
2347 static void mlxsw_sp_traps_unregister(struct mlxsw_sp *mlxsw_sp,
2348 const struct mlxsw_listener listeners[],
2349 size_t listeners_count)
2353 for (i = 0; i < listeners_count; i++) {
2354 mlxsw_core_trap_unregister(mlxsw_sp->core,
2360 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
2362 struct mlxsw_sp_trap *trap;
2366 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_CPU_POLICERS))
2368 max_policers = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_CPU_POLICERS);
2369 trap = kzalloc(struct_size(trap, policers_usage,
2370 BITS_TO_LONGS(max_policers)), GFP_KERNEL);
2373 trap->max_policers = max_policers;
2374 mlxsw_sp->trap = trap;
2376 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
2378 goto err_cpu_policers_set;
2380 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
2382 goto err_trap_groups_set;
2384 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp_listener,
2385 ARRAY_SIZE(mlxsw_sp_listener));
2387 goto err_traps_register;
2389 err = mlxsw_sp_traps_register(mlxsw_sp, mlxsw_sp->listeners,
2390 mlxsw_sp->listeners_count);
2392 goto err_extra_traps_init;
2396 err_extra_traps_init:
2397 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
2398 ARRAY_SIZE(mlxsw_sp_listener));
2400 err_trap_groups_set:
2401 err_cpu_policers_set:
2406 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
2408 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp->listeners,
2409 mlxsw_sp->listeners_count);
2410 mlxsw_sp_traps_unregister(mlxsw_sp, mlxsw_sp_listener,
2411 ARRAY_SIZE(mlxsw_sp_listener));
2412 kfree(mlxsw_sp->trap);
2415 #define MLXSW_SP_LAG_SEED_INIT 0xcafecafe
2417 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
2419 char slcr_pl[MLXSW_REG_SLCR_LEN];
2423 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac),
2424 MLXSW_SP_LAG_SEED_INIT);
2425 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
2426 MLXSW_REG_SLCR_LAG_HASH_DMAC |
2427 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
2428 MLXSW_REG_SLCR_LAG_HASH_VLANID |
2429 MLXSW_REG_SLCR_LAG_HASH_SIP |
2430 MLXSW_REG_SLCR_LAG_HASH_DIP |
2431 MLXSW_REG_SLCR_LAG_HASH_SPORT |
2432 MLXSW_REG_SLCR_LAG_HASH_DPORT |
2433 MLXSW_REG_SLCR_LAG_HASH_IPPROTO, seed);
2434 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
2438 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
2439 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
2442 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
2443 sizeof(struct mlxsw_sp_upper),
2445 if (!mlxsw_sp->lags)
2451 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
2453 kfree(mlxsw_sp->lags);
2456 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
2458 char htgt_pl[MLXSW_REG_HTGT_LEN];
2461 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
2462 MLXSW_REG_HTGT_INVALID_POLICER,
2463 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
2464 MLXSW_REG_HTGT_DEFAULT_TC);
2465 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2469 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_MFDE,
2470 MLXSW_REG_HTGT_INVALID_POLICER,
2471 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
2472 MLXSW_REG_HTGT_DEFAULT_TC);
2473 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2477 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_MTWE,
2478 MLXSW_REG_HTGT_INVALID_POLICER,
2479 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
2480 MLXSW_REG_HTGT_DEFAULT_TC);
2481 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2485 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_PMPE,
2486 MLXSW_REG_HTGT_INVALID_POLICER,
2487 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
2488 MLXSW_REG_HTGT_DEFAULT_TC);
2489 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2492 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
2493 .clock_init = mlxsw_sp1_ptp_clock_init,
2494 .clock_fini = mlxsw_sp1_ptp_clock_fini,
2495 .init = mlxsw_sp1_ptp_init,
2496 .fini = mlxsw_sp1_ptp_fini,
2497 .receive = mlxsw_sp1_ptp_receive,
2498 .transmitted = mlxsw_sp1_ptp_transmitted,
2499 .hwtstamp_get = mlxsw_sp1_ptp_hwtstamp_get,
2500 .hwtstamp_set = mlxsw_sp1_ptp_hwtstamp_set,
2501 .shaper_work = mlxsw_sp1_ptp_shaper_work,
2502 .get_ts_info = mlxsw_sp1_ptp_get_ts_info,
2503 .get_stats_count = mlxsw_sp1_get_stats_count,
2504 .get_stats_strings = mlxsw_sp1_get_stats_strings,
2505 .get_stats = mlxsw_sp1_get_stats,
2508 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
2509 .clock_init = mlxsw_sp2_ptp_clock_init,
2510 .clock_fini = mlxsw_sp2_ptp_clock_fini,
2511 .init = mlxsw_sp2_ptp_init,
2512 .fini = mlxsw_sp2_ptp_fini,
2513 .receive = mlxsw_sp2_ptp_receive,
2514 .transmitted = mlxsw_sp2_ptp_transmitted,
2515 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
2516 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
2517 .shaper_work = mlxsw_sp2_ptp_shaper_work,
2518 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
2519 .get_stats_count = mlxsw_sp2_get_stats_count,
2520 .get_stats_strings = mlxsw_sp2_get_stats_strings,
2521 .get_stats = mlxsw_sp2_get_stats,
2524 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
2525 unsigned long event, void *ptr);
2527 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
2528 const struct mlxsw_bus_info *mlxsw_bus_info,
2529 struct netlink_ext_ack *extack)
2531 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2534 mlxsw_sp->core = mlxsw_core;
2535 mlxsw_sp->bus_info = mlxsw_bus_info;
2537 mlxsw_core_emad_string_tlv_enable(mlxsw_core);
2539 err = mlxsw_sp_base_mac_get(mlxsw_sp);
2541 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
2545 err = mlxsw_sp_kvdl_init(mlxsw_sp);
2547 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
2551 err = mlxsw_sp_fids_init(mlxsw_sp);
2553 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
2557 err = mlxsw_sp_policers_init(mlxsw_sp);
2559 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize policers\n");
2560 goto err_policers_init;
2563 err = mlxsw_sp_traps_init(mlxsw_sp);
2565 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
2566 goto err_traps_init;
2569 err = mlxsw_sp_devlink_traps_init(mlxsw_sp);
2571 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize devlink traps\n");
2572 goto err_devlink_traps_init;
2575 err = mlxsw_sp_buffers_init(mlxsw_sp);
2577 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
2578 goto err_buffers_init;
2581 err = mlxsw_sp_lag_init(mlxsw_sp);
2583 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
2587 /* Initialize SPAN before router and switchdev, so that those components
2588 * can call mlxsw_sp_span_respin().
2590 err = mlxsw_sp_span_init(mlxsw_sp);
2592 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
2596 err = mlxsw_sp_switchdev_init(mlxsw_sp);
2598 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
2599 goto err_switchdev_init;
2602 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
2604 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
2605 goto err_counter_pool_init;
2608 err = mlxsw_sp_afa_init(mlxsw_sp);
2610 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
2614 err = mlxsw_sp_nve_init(mlxsw_sp);
2616 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize NVE\n");
2620 err = mlxsw_sp_acl_init(mlxsw_sp);
2622 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
2626 err = mlxsw_sp_router_init(mlxsw_sp, extack);
2628 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
2629 goto err_router_init;
2632 if (mlxsw_sp->bus_info->read_frc_capable) {
2633 /* NULL is a valid return value from clock_init */
2635 mlxsw_sp->ptp_ops->clock_init(mlxsw_sp,
2636 mlxsw_sp->bus_info->dev);
2637 if (IS_ERR(mlxsw_sp->clock)) {
2638 err = PTR_ERR(mlxsw_sp->clock);
2639 dev_err(mlxsw_sp->bus_info->dev, "Failed to init ptp clock\n");
2640 goto err_ptp_clock_init;
2644 if (mlxsw_sp->clock) {
2645 /* NULL is a valid return value from ptp_ops->init */
2646 mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
2647 if (IS_ERR(mlxsw_sp->ptp_state)) {
2648 err = PTR_ERR(mlxsw_sp->ptp_state);
2649 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
2654 /* Initialize netdevice notifier after router and SPAN is initialized,
2655 * so that the event handler can use router structures and call SPAN
2658 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
2659 err = register_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
2660 &mlxsw_sp->netdevice_nb);
2662 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
2663 goto err_netdev_notifier;
2666 err = mlxsw_sp_dpipe_init(mlxsw_sp);
2668 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
2669 goto err_dpipe_init;
2672 err = mlxsw_sp_port_module_info_init(mlxsw_sp);
2674 dev_err(mlxsw_sp->bus_info->dev, "Failed to init port module info\n");
2675 goto err_port_module_info_init;
2678 err = mlxsw_sp_ports_create(mlxsw_sp);
2680 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
2681 goto err_ports_create;
2687 mlxsw_sp_port_module_info_fini(mlxsw_sp);
2688 err_port_module_info_init:
2689 mlxsw_sp_dpipe_fini(mlxsw_sp);
2691 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
2692 &mlxsw_sp->netdevice_nb);
2693 err_netdev_notifier:
2694 if (mlxsw_sp->clock)
2695 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
2697 if (mlxsw_sp->clock)
2698 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
2700 mlxsw_sp_router_fini(mlxsw_sp);
2702 mlxsw_sp_acl_fini(mlxsw_sp);
2704 mlxsw_sp_nve_fini(mlxsw_sp);
2706 mlxsw_sp_afa_fini(mlxsw_sp);
2708 mlxsw_sp_counter_pool_fini(mlxsw_sp);
2709 err_counter_pool_init:
2710 mlxsw_sp_switchdev_fini(mlxsw_sp);
2712 mlxsw_sp_span_fini(mlxsw_sp);
2714 mlxsw_sp_lag_fini(mlxsw_sp);
2716 mlxsw_sp_buffers_fini(mlxsw_sp);
2718 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
2719 err_devlink_traps_init:
2720 mlxsw_sp_traps_fini(mlxsw_sp);
2722 mlxsw_sp_policers_fini(mlxsw_sp);
2724 mlxsw_sp_fids_fini(mlxsw_sp);
2726 mlxsw_sp_kvdl_fini(mlxsw_sp);
2730 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
2731 const struct mlxsw_bus_info *mlxsw_bus_info,
2732 struct netlink_ext_ack *extack)
2734 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2736 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
2737 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
2738 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
2739 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
2740 mlxsw_sp->acl_rulei_ops = &mlxsw_sp1_acl_rulei_ops;
2741 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
2742 mlxsw_sp->nve_ops_arr = mlxsw_sp1_nve_ops_arr;
2743 mlxsw_sp->mac_mask = mlxsw_sp1_mac_mask;
2744 mlxsw_sp->rif_ops_arr = mlxsw_sp1_rif_ops_arr;
2745 mlxsw_sp->sb_vals = &mlxsw_sp1_sb_vals;
2746 mlxsw_sp->sb_ops = &mlxsw_sp1_sb_ops;
2747 mlxsw_sp->port_type_speed_ops = &mlxsw_sp1_port_type_speed_ops;
2748 mlxsw_sp->ptp_ops = &mlxsw_sp1_ptp_ops;
2749 mlxsw_sp->span_ops = &mlxsw_sp1_span_ops;
2750 mlxsw_sp->policer_core_ops = &mlxsw_sp1_policer_core_ops;
2751 mlxsw_sp->trap_ops = &mlxsw_sp1_trap_ops;
2752 mlxsw_sp->listeners = mlxsw_sp1_listener;
2753 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp1_listener);
2754 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP1;
2756 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
2759 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
2760 const struct mlxsw_bus_info *mlxsw_bus_info,
2761 struct netlink_ext_ack *extack)
2763 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2765 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
2766 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
2767 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
2768 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
2769 mlxsw_sp->acl_rulei_ops = &mlxsw_sp2_acl_rulei_ops;
2770 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
2771 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
2772 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
2773 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
2774 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
2775 mlxsw_sp->sb_ops = &mlxsw_sp2_sb_ops;
2776 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
2777 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
2778 mlxsw_sp->span_ops = &mlxsw_sp2_span_ops;
2779 mlxsw_sp->policer_core_ops = &mlxsw_sp2_policer_core_ops;
2780 mlxsw_sp->trap_ops = &mlxsw_sp2_trap_ops;
2781 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP2;
2783 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
2786 static int mlxsw_sp3_init(struct mlxsw_core *mlxsw_core,
2787 const struct mlxsw_bus_info *mlxsw_bus_info,
2788 struct netlink_ext_ack *extack)
2790 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2792 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
2793 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
2794 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
2795 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
2796 mlxsw_sp->acl_rulei_ops = &mlxsw_sp2_acl_rulei_ops;
2797 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
2798 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
2799 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
2800 mlxsw_sp->rif_ops_arr = mlxsw_sp2_rif_ops_arr;
2801 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
2802 mlxsw_sp->sb_ops = &mlxsw_sp3_sb_ops;
2803 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
2804 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
2805 mlxsw_sp->span_ops = &mlxsw_sp3_span_ops;
2806 mlxsw_sp->policer_core_ops = &mlxsw_sp2_policer_core_ops;
2807 mlxsw_sp->trap_ops = &mlxsw_sp2_trap_ops;
2808 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP3;
2810 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
2813 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
2815 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2817 mlxsw_sp_ports_remove(mlxsw_sp);
2818 mlxsw_sp_port_module_info_fini(mlxsw_sp);
2819 mlxsw_sp_dpipe_fini(mlxsw_sp);
2820 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
2821 &mlxsw_sp->netdevice_nb);
2822 if (mlxsw_sp->clock) {
2823 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
2824 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
2826 mlxsw_sp_router_fini(mlxsw_sp);
2827 mlxsw_sp_acl_fini(mlxsw_sp);
2828 mlxsw_sp_nve_fini(mlxsw_sp);
2829 mlxsw_sp_afa_fini(mlxsw_sp);
2830 mlxsw_sp_counter_pool_fini(mlxsw_sp);
2831 mlxsw_sp_switchdev_fini(mlxsw_sp);
2832 mlxsw_sp_span_fini(mlxsw_sp);
2833 mlxsw_sp_lag_fini(mlxsw_sp);
2834 mlxsw_sp_buffers_fini(mlxsw_sp);
2835 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
2836 mlxsw_sp_traps_fini(mlxsw_sp);
2837 mlxsw_sp_policers_fini(mlxsw_sp);
2838 mlxsw_sp_fids_fini(mlxsw_sp);
2839 mlxsw_sp_kvdl_fini(mlxsw_sp);
2842 /* Per-FID flood tables are used for both "true" 802.1D FIDs and emulated
2845 #define MLXSW_SP_FID_FLOOD_TABLE_SIZE (MLXSW_SP_FID_8021D_MAX + \
2848 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
2850 .max_mid = MLXSW_SP_MID_MAX,
2851 .used_flood_tables = 1,
2852 .used_flood_mode = 1,
2854 .max_fid_flood_tables = 3,
2855 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
2856 .used_max_ib_mc = 1,
2860 .used_kvd_sizes = 1,
2861 .kvd_hash_single_parts = 59,
2862 .kvd_hash_double_parts = 41,
2863 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
2867 .type = MLXSW_PORT_SWID_TYPE_ETH,
2872 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
2874 .max_mid = MLXSW_SP_MID_MAX,
2875 .used_flood_tables = 1,
2876 .used_flood_mode = 1,
2878 .max_fid_flood_tables = 3,
2879 .fid_flood_table_size = MLXSW_SP_FID_FLOOD_TABLE_SIZE,
2880 .used_max_ib_mc = 1,
2887 .type = MLXSW_PORT_SWID_TYPE_ETH,
2893 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
2894 struct devlink_resource_size_params *kvd_size_params,
2895 struct devlink_resource_size_params *linear_size_params,
2896 struct devlink_resource_size_params *hash_double_size_params,
2897 struct devlink_resource_size_params *hash_single_size_params)
2899 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
2900 KVD_SINGLE_MIN_SIZE);
2901 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
2902 KVD_DOUBLE_MIN_SIZE);
2903 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
2904 u32 linear_size_min = 0;
2906 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
2907 MLXSW_SP_KVD_GRANULARITY,
2908 DEVLINK_RESOURCE_UNIT_ENTRY);
2909 devlink_resource_size_params_init(linear_size_params, linear_size_min,
2910 kvd_size - single_size_min -
2912 MLXSW_SP_KVD_GRANULARITY,
2913 DEVLINK_RESOURCE_UNIT_ENTRY);
2914 devlink_resource_size_params_init(hash_double_size_params,
2916 kvd_size - single_size_min -
2918 MLXSW_SP_KVD_GRANULARITY,
2919 DEVLINK_RESOURCE_UNIT_ENTRY);
2920 devlink_resource_size_params_init(hash_single_size_params,
2922 kvd_size - double_size_min -
2924 MLXSW_SP_KVD_GRANULARITY,
2925 DEVLINK_RESOURCE_UNIT_ENTRY);
2928 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
2930 struct devlink *devlink = priv_to_devlink(mlxsw_core);
2931 struct devlink_resource_size_params hash_single_size_params;
2932 struct devlink_resource_size_params hash_double_size_params;
2933 struct devlink_resource_size_params linear_size_params;
2934 struct devlink_resource_size_params kvd_size_params;
2935 u32 kvd_size, single_size, double_size, linear_size;
2936 const struct mlxsw_config_profile *profile;
2939 profile = &mlxsw_sp1_config_profile;
2940 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
2943 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
2944 &linear_size_params,
2945 &hash_double_size_params,
2946 &hash_single_size_params);
2948 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
2949 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
2950 kvd_size, MLXSW_SP_RESOURCE_KVD,
2951 DEVLINK_RESOURCE_ID_PARENT_TOP,
2956 linear_size = profile->kvd_linear_size;
2957 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
2959 MLXSW_SP_RESOURCE_KVD_LINEAR,
2960 MLXSW_SP_RESOURCE_KVD,
2961 &linear_size_params);
2965 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
2969 double_size = kvd_size - linear_size;
2970 double_size *= profile->kvd_hash_double_parts;
2971 double_size /= profile->kvd_hash_double_parts +
2972 profile->kvd_hash_single_parts;
2973 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
2974 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
2976 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
2977 MLXSW_SP_RESOURCE_KVD,
2978 &hash_double_size_params);
2982 single_size = kvd_size - double_size - linear_size;
2983 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
2985 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
2986 MLXSW_SP_RESOURCE_KVD,
2987 &hash_single_size_params);
2994 static int mlxsw_sp2_resources_kvd_register(struct mlxsw_core *mlxsw_core)
2996 struct devlink *devlink = priv_to_devlink(mlxsw_core);
2997 struct devlink_resource_size_params kvd_size_params;
3000 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
3003 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
3004 devlink_resource_size_params_init(&kvd_size_params, kvd_size, kvd_size,
3005 MLXSW_SP_KVD_GRANULARITY,
3006 DEVLINK_RESOURCE_UNIT_ENTRY);
3008 return devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
3009 kvd_size, MLXSW_SP_RESOURCE_KVD,
3010 DEVLINK_RESOURCE_ID_PARENT_TOP,
3014 static int mlxsw_sp_resources_span_register(struct mlxsw_core *mlxsw_core)
3016 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3017 struct devlink_resource_size_params span_size_params;
3020 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_SPAN))
3023 max_span = MLXSW_CORE_RES_GET(mlxsw_core, MAX_SPAN);
3024 devlink_resource_size_params_init(&span_size_params, max_span, max_span,
3025 1, DEVLINK_RESOURCE_UNIT_ENTRY);
3027 return devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_SPAN,
3028 max_span, MLXSW_SP_RESOURCE_SPAN,
3029 DEVLINK_RESOURCE_ID_PARENT_TOP,
3033 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
3037 err = mlxsw_sp1_resources_kvd_register(mlxsw_core);
3041 err = mlxsw_sp_resources_span_register(mlxsw_core);
3043 goto err_resources_span_register;
3045 err = mlxsw_sp_counter_resources_register(mlxsw_core);
3047 goto err_resources_counter_register;
3049 err = mlxsw_sp_policer_resources_register(mlxsw_core);
3051 goto err_resources_counter_register;
3055 err_resources_counter_register:
3056 err_resources_span_register:
3057 devlink_resources_unregister(priv_to_devlink(mlxsw_core), NULL);
3061 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
3065 err = mlxsw_sp2_resources_kvd_register(mlxsw_core);
3069 err = mlxsw_sp_resources_span_register(mlxsw_core);
3071 goto err_resources_span_register;
3073 err = mlxsw_sp_counter_resources_register(mlxsw_core);
3075 goto err_resources_counter_register;
3077 err = mlxsw_sp_policer_resources_register(mlxsw_core);
3079 goto err_resources_counter_register;
3083 err_resources_counter_register:
3084 err_resources_span_register:
3085 devlink_resources_unregister(priv_to_devlink(mlxsw_core), NULL);
3089 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
3090 const struct mlxsw_config_profile *profile,
3091 u64 *p_single_size, u64 *p_double_size,
3094 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3098 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
3099 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
3102 /* The hash part is what left of the kvd without the
3103 * linear part. It is split to the single size and
3104 * double size by the parts ratio from the profile.
3105 * Both sizes must be a multiplications of the
3106 * granularity from the profile. In case the user
3107 * provided the sizes they are obtained via devlink.
3109 err = devlink_resource_size_get(devlink,
3110 MLXSW_SP_RESOURCE_KVD_LINEAR,
3113 *p_linear_size = profile->kvd_linear_size;
3115 err = devlink_resource_size_get(devlink,
3116 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
3119 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
3121 double_size *= profile->kvd_hash_double_parts;
3122 double_size /= profile->kvd_hash_double_parts +
3123 profile->kvd_hash_single_parts;
3124 *p_double_size = rounddown(double_size,
3125 MLXSW_SP_KVD_GRANULARITY);
3128 err = devlink_resource_size_get(devlink,
3129 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
3132 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
3133 *p_double_size - *p_linear_size;
3135 /* Check results are legal. */
3136 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
3137 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
3138 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
3145 mlxsw_sp_params_acl_region_rehash_intrvl_get(struct devlink *devlink, u32 id,
3146 struct devlink_param_gset_ctx *ctx)
3148 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
3149 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3151 ctx->val.vu32 = mlxsw_sp_acl_region_rehash_intrvl_get(mlxsw_sp);
3156 mlxsw_sp_params_acl_region_rehash_intrvl_set(struct devlink *devlink, u32 id,
3157 struct devlink_param_gset_ctx *ctx)
3159 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
3160 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3162 return mlxsw_sp_acl_region_rehash_intrvl_set(mlxsw_sp, ctx->val.vu32);
3165 static const struct devlink_param mlxsw_sp2_devlink_params[] = {
3166 DEVLINK_PARAM_DRIVER(MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
3167 "acl_region_rehash_interval",
3168 DEVLINK_PARAM_TYPE_U32,
3169 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3170 mlxsw_sp_params_acl_region_rehash_intrvl_get,
3171 mlxsw_sp_params_acl_region_rehash_intrvl_set,
3175 static int mlxsw_sp2_params_register(struct mlxsw_core *mlxsw_core)
3177 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3178 union devlink_param_value value;
3181 err = devlink_params_register(devlink, mlxsw_sp2_devlink_params,
3182 ARRAY_SIZE(mlxsw_sp2_devlink_params));
3187 devlink_param_driverinit_value_set(devlink,
3188 MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
3193 static void mlxsw_sp2_params_unregister(struct mlxsw_core *mlxsw_core)
3195 devlink_params_unregister(priv_to_devlink(mlxsw_core),
3196 mlxsw_sp2_devlink_params,
3197 ARRAY_SIZE(mlxsw_sp2_devlink_params));
3200 static void mlxsw_sp_ptp_transmitted(struct mlxsw_core *mlxsw_core,
3201 struct sk_buff *skb, u8 local_port)
3203 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3205 skb_pull(skb, MLXSW_TXHDR_LEN);
3206 mlxsw_sp->ptp_ops->transmitted(mlxsw_sp, skb, local_port);
3209 static struct mlxsw_driver mlxsw_sp1_driver = {
3210 .kind = mlxsw_sp1_driver_name,
3211 .priv_size = sizeof(struct mlxsw_sp),
3212 .fw_req_rev = &mlxsw_sp1_fw_rev,
3213 .fw_filename = MLXSW_SP1_FW_FILENAME,
3214 .init = mlxsw_sp1_init,
3215 .fini = mlxsw_sp_fini,
3216 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
3217 .port_split = mlxsw_sp_port_split,
3218 .port_unsplit = mlxsw_sp_port_unsplit,
3219 .sb_pool_get = mlxsw_sp_sb_pool_get,
3220 .sb_pool_set = mlxsw_sp_sb_pool_set,
3221 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
3222 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
3223 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
3224 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
3225 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
3226 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
3227 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
3228 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
3229 .trap_init = mlxsw_sp_trap_init,
3230 .trap_fini = mlxsw_sp_trap_fini,
3231 .trap_action_set = mlxsw_sp_trap_action_set,
3232 .trap_group_init = mlxsw_sp_trap_group_init,
3233 .trap_group_set = mlxsw_sp_trap_group_set,
3234 .trap_policer_init = mlxsw_sp_trap_policer_init,
3235 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
3236 .trap_policer_set = mlxsw_sp_trap_policer_set,
3237 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
3238 .txhdr_construct = mlxsw_sp_txhdr_construct,
3239 .resources_register = mlxsw_sp1_resources_register,
3240 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
3241 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
3242 .txhdr_len = MLXSW_TXHDR_LEN,
3243 .profile = &mlxsw_sp1_config_profile,
3244 .res_query_enabled = true,
3245 .fw_fatal_enabled = true,
3246 .temp_warn_enabled = true,
3249 static struct mlxsw_driver mlxsw_sp2_driver = {
3250 .kind = mlxsw_sp2_driver_name,
3251 .priv_size = sizeof(struct mlxsw_sp),
3252 .fw_req_rev = &mlxsw_sp2_fw_rev,
3253 .fw_filename = MLXSW_SP2_FW_FILENAME,
3254 .init = mlxsw_sp2_init,
3255 .fini = mlxsw_sp_fini,
3256 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
3257 .port_split = mlxsw_sp_port_split,
3258 .port_unsplit = mlxsw_sp_port_unsplit,
3259 .sb_pool_get = mlxsw_sp_sb_pool_get,
3260 .sb_pool_set = mlxsw_sp_sb_pool_set,
3261 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
3262 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
3263 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
3264 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
3265 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
3266 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
3267 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
3268 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
3269 .trap_init = mlxsw_sp_trap_init,
3270 .trap_fini = mlxsw_sp_trap_fini,
3271 .trap_action_set = mlxsw_sp_trap_action_set,
3272 .trap_group_init = mlxsw_sp_trap_group_init,
3273 .trap_group_set = mlxsw_sp_trap_group_set,
3274 .trap_policer_init = mlxsw_sp_trap_policer_init,
3275 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
3276 .trap_policer_set = mlxsw_sp_trap_policer_set,
3277 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
3278 .txhdr_construct = mlxsw_sp_txhdr_construct,
3279 .resources_register = mlxsw_sp2_resources_register,
3280 .params_register = mlxsw_sp2_params_register,
3281 .params_unregister = mlxsw_sp2_params_unregister,
3282 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
3283 .txhdr_len = MLXSW_TXHDR_LEN,
3284 .profile = &mlxsw_sp2_config_profile,
3285 .res_query_enabled = true,
3286 .fw_fatal_enabled = true,
3287 .temp_warn_enabled = true,
3290 static struct mlxsw_driver mlxsw_sp3_driver = {
3291 .kind = mlxsw_sp3_driver_name,
3292 .priv_size = sizeof(struct mlxsw_sp),
3293 .fw_req_rev = &mlxsw_sp3_fw_rev,
3294 .fw_filename = MLXSW_SP3_FW_FILENAME,
3295 .init = mlxsw_sp3_init,
3296 .fini = mlxsw_sp_fini,
3297 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
3298 .port_split = mlxsw_sp_port_split,
3299 .port_unsplit = mlxsw_sp_port_unsplit,
3300 .sb_pool_get = mlxsw_sp_sb_pool_get,
3301 .sb_pool_set = mlxsw_sp_sb_pool_set,
3302 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
3303 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
3304 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
3305 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
3306 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
3307 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
3308 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
3309 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
3310 .trap_init = mlxsw_sp_trap_init,
3311 .trap_fini = mlxsw_sp_trap_fini,
3312 .trap_action_set = mlxsw_sp_trap_action_set,
3313 .trap_group_init = mlxsw_sp_trap_group_init,
3314 .trap_group_set = mlxsw_sp_trap_group_set,
3315 .trap_policer_init = mlxsw_sp_trap_policer_init,
3316 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
3317 .trap_policer_set = mlxsw_sp_trap_policer_set,
3318 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
3319 .txhdr_construct = mlxsw_sp_txhdr_construct,
3320 .resources_register = mlxsw_sp2_resources_register,
3321 .params_register = mlxsw_sp2_params_register,
3322 .params_unregister = mlxsw_sp2_params_unregister,
3323 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
3324 .txhdr_len = MLXSW_TXHDR_LEN,
3325 .profile = &mlxsw_sp2_config_profile,
3326 .res_query_enabled = true,
3327 .fw_fatal_enabled = true,
3328 .temp_warn_enabled = true,
3331 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
3333 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
3336 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev,
3337 struct netdev_nested_priv *priv)
3341 if (mlxsw_sp_port_dev_check(lower_dev)) {
3342 priv->data = (void *)netdev_priv(lower_dev);
3349 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
3351 struct netdev_nested_priv priv = {
3355 if (mlxsw_sp_port_dev_check(dev))
3356 return netdev_priv(dev);
3358 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &priv);
3360 return (struct mlxsw_sp_port *)priv.data;
3363 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
3365 struct mlxsw_sp_port *mlxsw_sp_port;
3367 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
3368 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
3371 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
3373 struct netdev_nested_priv priv = {
3377 if (mlxsw_sp_port_dev_check(dev))
3378 return netdev_priv(dev);
3380 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
3383 return (struct mlxsw_sp_port *)priv.data;
3386 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
3388 struct mlxsw_sp_port *mlxsw_sp_port;
3391 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
3393 dev_hold(mlxsw_sp_port->dev);
3395 return mlxsw_sp_port;
3398 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
3400 dev_put(mlxsw_sp_port->dev);
3404 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
3405 struct net_device *lag_dev)
3407 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
3408 struct net_device *upper_dev;
3409 struct list_head *iter;
3411 if (netif_is_bridge_port(lag_dev))
3412 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
3414 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
3415 if (!netif_is_bridge_port(upper_dev))
3417 br_dev = netdev_master_upper_dev_get(upper_dev);
3418 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
3422 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
3424 char sldr_pl[MLXSW_REG_SLDR_LEN];
3426 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
3427 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3430 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
3432 char sldr_pl[MLXSW_REG_SLDR_LEN];
3434 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
3435 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3438 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
3439 u16 lag_id, u8 port_index)
3441 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3442 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3444 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
3445 lag_id, port_index);
3446 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3449 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
3452 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3453 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3455 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
3457 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3460 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
3463 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3464 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3466 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
3468 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3471 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
3474 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3475 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3477 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
3479 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3482 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
3483 struct net_device *lag_dev,
3486 struct mlxsw_sp_upper *lag;
3487 int free_lag_id = -1;
3491 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
3492 for (i = 0; i < max_lag; i++) {
3493 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
3494 if (lag->ref_count) {
3495 if (lag->dev == lag_dev) {
3499 } else if (free_lag_id < 0) {
3503 if (free_lag_id < 0)
3505 *p_lag_id = free_lag_id;
3510 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
3511 struct net_device *lag_dev,
3512 struct netdev_lag_upper_info *lag_upper_info,
3513 struct netlink_ext_ack *extack)
3517 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
3518 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported LAG devices");
3521 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
3522 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
3528 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
3529 u16 lag_id, u8 *p_port_index)
3531 u64 max_lag_members;
3534 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
3536 for (i = 0; i < max_lag_members; i++) {
3537 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
3545 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
3546 struct net_device *lag_dev)
3548 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3549 struct mlxsw_sp_upper *lag;
3554 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
3557 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
3558 if (!lag->ref_count) {
3559 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
3565 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
3568 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
3570 goto err_col_port_add;
3572 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
3573 mlxsw_sp_port->local_port);
3574 mlxsw_sp_port->lag_id = lag_id;
3575 mlxsw_sp_port->lagged = 1;
3578 /* Port is no longer usable as a router interface */
3579 if (mlxsw_sp_port->default_vlan->fid)
3580 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
3585 if (!lag->ref_count)
3586 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
3590 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
3591 struct net_device *lag_dev)
3593 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3594 u16 lag_id = mlxsw_sp_port->lag_id;
3595 struct mlxsw_sp_upper *lag;
3597 if (!mlxsw_sp_port->lagged)
3599 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
3600 WARN_ON(lag->ref_count == 0);
3602 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
3604 /* Any VLANs configured on the port are no longer valid */
3605 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, false);
3606 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port->default_vlan);
3607 /* Make the LAG and its directly linked uppers leave bridges they
3610 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
3612 if (lag->ref_count == 1)
3613 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
3615 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
3616 mlxsw_sp_port->local_port);
3617 mlxsw_sp_port->lagged = 0;
3620 /* Make sure untagged frames are allowed to ingress */
3621 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
3624 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
3627 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3628 char sldr_pl[MLXSW_REG_SLDR_LEN];
3630 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
3631 mlxsw_sp_port->local_port);
3632 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3635 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
3638 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3639 char sldr_pl[MLXSW_REG_SLDR_LEN];
3641 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
3642 mlxsw_sp_port->local_port);
3643 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3647 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
3651 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
3652 mlxsw_sp_port->lag_id);
3656 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
3658 goto err_dist_port_add;
3663 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
3668 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
3672 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
3673 mlxsw_sp_port->lag_id);
3677 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
3678 mlxsw_sp_port->lag_id);
3680 goto err_col_port_disable;
3684 err_col_port_disable:
3685 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
3689 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
3690 struct netdev_lag_lower_state_info *info)
3692 if (info->tx_enabled)
3693 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
3695 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
3698 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
3701 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3702 enum mlxsw_reg_spms_state spms_state;
3707 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
3708 MLXSW_REG_SPMS_STATE_DISCARDING;
3710 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
3713 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
3715 for (vid = 0; vid < VLAN_N_VID; vid++)
3716 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
3718 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
3723 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
3728 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
3731 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
3733 goto err_port_stp_set;
3734 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
3737 goto err_port_vlan_set;
3739 for (; vid <= VLAN_N_VID - 1; vid++) {
3740 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
3743 goto err_vid_learning_set;
3748 err_vid_learning_set:
3749 for (vid--; vid >= 1; vid--)
3750 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
3752 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
3754 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
3758 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
3762 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
3763 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
3766 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
3768 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
3769 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
3772 static bool mlxsw_sp_bridge_has_multiple_vxlans(struct net_device *br_dev)
3774 unsigned int num_vxlans = 0;
3775 struct net_device *dev;
3776 struct list_head *iter;
3778 netdev_for_each_lower_dev(br_dev, dev, iter) {
3779 if (netif_is_vxlan(dev))
3783 return num_vxlans > 1;
3786 static bool mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device *br_dev)
3788 DECLARE_BITMAP(vlans, VLAN_N_VID) = {0};
3789 struct net_device *dev;
3790 struct list_head *iter;
3792 netdev_for_each_lower_dev(br_dev, dev, iter) {
3796 if (!netif_is_vxlan(dev))
3799 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
3803 if (test_and_set_bit(pvid, vlans))
3810 static bool mlxsw_sp_bridge_vxlan_is_valid(struct net_device *br_dev,
3811 struct netlink_ext_ack *extack)
3813 if (br_multicast_enabled(br_dev)) {
3814 NL_SET_ERR_MSG_MOD(extack, "Multicast can not be enabled on a bridge with a VxLAN device");
3818 if (!br_vlan_enabled(br_dev) &&
3819 mlxsw_sp_bridge_has_multiple_vxlans(br_dev)) {
3820 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices are not supported in a VLAN-unaware bridge");
3824 if (br_vlan_enabled(br_dev) &&
3825 !mlxsw_sp_bridge_vxlan_vlan_is_valid(br_dev)) {
3826 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices cannot have the same VLAN as PVID and egress untagged");
3833 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
3834 struct net_device *dev,
3835 unsigned long event, void *ptr)
3837 struct netdev_notifier_changeupper_info *info;
3838 struct mlxsw_sp_port *mlxsw_sp_port;
3839 struct netlink_ext_ack *extack;
3840 struct net_device *upper_dev;
3841 struct mlxsw_sp *mlxsw_sp;
3844 mlxsw_sp_port = netdev_priv(dev);
3845 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3847 extack = netdev_notifier_info_to_extack(&info->info);
3850 case NETDEV_PRECHANGEUPPER:
3851 upper_dev = info->upper_dev;
3852 if (!is_vlan_dev(upper_dev) &&
3853 !netif_is_lag_master(upper_dev) &&
3854 !netif_is_bridge_master(upper_dev) &&
3855 !netif_is_ovs_master(upper_dev) &&
3856 !netif_is_macvlan(upper_dev)) {
3857 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
3862 if (netif_is_bridge_master(upper_dev) &&
3863 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
3864 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
3865 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
3867 if (netdev_has_any_upper_dev(upper_dev) &&
3868 (!netif_is_bridge_master(upper_dev) ||
3869 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
3871 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
3874 if (netif_is_lag_master(upper_dev) &&
3875 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
3876 info->upper_info, extack))
3878 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
3879 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
3882 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
3883 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
3884 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
3887 if (netif_is_macvlan(upper_dev) &&
3888 !mlxsw_sp_rif_exists(mlxsw_sp, lower_dev)) {
3889 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
3892 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
3893 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
3896 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
3897 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
3901 case NETDEV_CHANGEUPPER:
3902 upper_dev = info->upper_dev;
3903 if (netif_is_bridge_master(upper_dev)) {
3905 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
3910 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
3913 } else if (netif_is_lag_master(upper_dev)) {
3914 if (info->linking) {
3915 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
3918 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
3919 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
3922 } else if (netif_is_ovs_master(upper_dev)) {
3924 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
3926 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
3927 } else if (netif_is_macvlan(upper_dev)) {
3929 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
3930 } else if (is_vlan_dev(upper_dev)) {
3931 struct net_device *br_dev;
3933 if (!netif_is_bridge_port(upper_dev))
3937 br_dev = netdev_master_upper_dev_get(upper_dev);
3938 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
3947 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
3948 unsigned long event, void *ptr)
3950 struct netdev_notifier_changelowerstate_info *info;
3951 struct mlxsw_sp_port *mlxsw_sp_port;
3954 mlxsw_sp_port = netdev_priv(dev);
3958 case NETDEV_CHANGELOWERSTATE:
3959 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
3960 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
3961 info->lower_state_info);
3963 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
3971 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
3972 struct net_device *port_dev,
3973 unsigned long event, void *ptr)
3976 case NETDEV_PRECHANGEUPPER:
3977 case NETDEV_CHANGEUPPER:
3978 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
3980 case NETDEV_CHANGELOWERSTATE:
3981 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
3988 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
3989 unsigned long event, void *ptr)
3991 struct net_device *dev;
3992 struct list_head *iter;
3995 netdev_for_each_lower_dev(lag_dev, dev, iter) {
3996 if (mlxsw_sp_port_dev_check(dev)) {
3997 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
4007 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
4008 struct net_device *dev,
4009 unsigned long event, void *ptr,
4012 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
4013 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4014 struct netdev_notifier_changeupper_info *info = ptr;
4015 struct netlink_ext_ack *extack;
4016 struct net_device *upper_dev;
4019 extack = netdev_notifier_info_to_extack(&info->info);
4022 case NETDEV_PRECHANGEUPPER:
4023 upper_dev = info->upper_dev;
4024 if (!netif_is_bridge_master(upper_dev) &&
4025 !netif_is_macvlan(upper_dev)) {
4026 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4031 if (netif_is_bridge_master(upper_dev) &&
4032 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
4033 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
4034 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
4036 if (netdev_has_any_upper_dev(upper_dev) &&
4037 (!netif_is_bridge_master(upper_dev) ||
4038 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4040 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a device that already has an upper device is not supported");
4043 if (netif_is_macvlan(upper_dev) &&
4044 !mlxsw_sp_rif_exists(mlxsw_sp, vlan_dev)) {
4045 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4049 case NETDEV_CHANGEUPPER:
4050 upper_dev = info->upper_dev;
4051 if (netif_is_bridge_master(upper_dev)) {
4053 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4058 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
4061 } else if (netif_is_macvlan(upper_dev)) {
4063 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4074 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
4075 struct net_device *lag_dev,
4076 unsigned long event,
4079 struct net_device *dev;
4080 struct list_head *iter;
4083 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4084 if (mlxsw_sp_port_dev_check(dev)) {
4085 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
4096 static int mlxsw_sp_netdevice_bridge_vlan_event(struct net_device *vlan_dev,
4097 struct net_device *br_dev,
4098 unsigned long event, void *ptr,
4101 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
4102 struct netdev_notifier_changeupper_info *info = ptr;
4103 struct netlink_ext_ack *extack;
4104 struct net_device *upper_dev;
4109 extack = netdev_notifier_info_to_extack(&info->info);
4112 case NETDEV_PRECHANGEUPPER:
4113 upper_dev = info->upper_dev;
4114 if (!netif_is_macvlan(upper_dev)) {
4115 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4120 if (netif_is_macvlan(upper_dev) &&
4121 !mlxsw_sp_rif_exists(mlxsw_sp, vlan_dev)) {
4122 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4126 case NETDEV_CHANGEUPPER:
4127 upper_dev = info->upper_dev;
4130 if (netif_is_macvlan(upper_dev))
4131 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4138 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
4139 unsigned long event, void *ptr)
4141 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
4142 u16 vid = vlan_dev_vlan_id(vlan_dev);
4144 if (mlxsw_sp_port_dev_check(real_dev))
4145 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
4147 else if (netif_is_lag_master(real_dev))
4148 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
4151 else if (netif_is_bridge_master(real_dev))
4152 return mlxsw_sp_netdevice_bridge_vlan_event(vlan_dev, real_dev,
4158 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
4159 unsigned long event, void *ptr)
4161 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(br_dev);
4162 struct netdev_notifier_changeupper_info *info = ptr;
4163 struct netlink_ext_ack *extack;
4164 struct net_device *upper_dev;
4169 extack = netdev_notifier_info_to_extack(&info->info);
4172 case NETDEV_PRECHANGEUPPER:
4173 upper_dev = info->upper_dev;
4174 if (!is_vlan_dev(upper_dev) && !netif_is_macvlan(upper_dev)) {
4175 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4180 if (netif_is_macvlan(upper_dev) &&
4181 !mlxsw_sp_rif_exists(mlxsw_sp, br_dev)) {
4182 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
4186 case NETDEV_CHANGEUPPER:
4187 upper_dev = info->upper_dev;
4190 if (is_vlan_dev(upper_dev))
4191 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
4192 if (netif_is_macvlan(upper_dev))
4193 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4200 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
4201 unsigned long event, void *ptr)
4203 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
4204 struct netdev_notifier_changeupper_info *info = ptr;
4205 struct netlink_ext_ack *extack;
4207 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
4210 extack = netdev_notifier_info_to_extack(&info->info);
4212 /* VRF enslavement is handled in mlxsw_sp_netdevice_vrf_event() */
4213 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4218 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
4220 struct netdev_notifier_changeupper_info *info = ptr;
4222 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
4224 return netif_is_l3_master(info->upper_dev);
4227 static int mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp *mlxsw_sp,
4228 struct net_device *dev,
4229 unsigned long event, void *ptr)
4231 struct netdev_notifier_changeupper_info *cu_info;
4232 struct netdev_notifier_info *info = ptr;
4233 struct netlink_ext_ack *extack;
4234 struct net_device *upper_dev;
4236 extack = netdev_notifier_info_to_extack(info);
4239 case NETDEV_CHANGEUPPER:
4240 cu_info = container_of(info,
4241 struct netdev_notifier_changeupper_info,
4243 upper_dev = cu_info->upper_dev;
4244 if (!netif_is_bridge_master(upper_dev))
4246 if (!mlxsw_sp_lower_get(upper_dev))
4248 if (!mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
4250 if (cu_info->linking) {
4251 if (!netif_running(dev))
4253 /* When the bridge is VLAN-aware, the VNI of the VxLAN
4254 * device needs to be mapped to a VLAN, but at this
4255 * point no VLANs are configured on the VxLAN device
4257 if (br_vlan_enabled(upper_dev))
4259 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev,
4262 /* VLANs were already flushed, which triggered the
4265 if (br_vlan_enabled(upper_dev))
4267 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
4271 upper_dev = netdev_master_upper_dev_get(dev);
4274 if (!netif_is_bridge_master(upper_dev))
4276 if (!mlxsw_sp_lower_get(upper_dev))
4278 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev, dev, 0,
4281 upper_dev = netdev_master_upper_dev_get(dev);
4284 if (!netif_is_bridge_master(upper_dev))
4286 if (!mlxsw_sp_lower_get(upper_dev))
4288 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
4295 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
4296 unsigned long event, void *ptr)
4298 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4299 struct mlxsw_sp_span_entry *span_entry;
4300 struct mlxsw_sp *mlxsw_sp;
4303 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
4304 if (event == NETDEV_UNREGISTER) {
4305 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
4307 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
4309 mlxsw_sp_span_respin(mlxsw_sp);
4311 if (netif_is_vxlan(dev))
4312 err = mlxsw_sp_netdevice_vxlan_event(mlxsw_sp, dev, event, ptr);
4313 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
4314 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
4316 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
4317 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
4319 else if (event == NETDEV_PRE_CHANGEADDR ||
4320 event == NETDEV_CHANGEADDR ||
4321 event == NETDEV_CHANGEMTU)
4322 err = mlxsw_sp_netdevice_router_port_event(dev, event, ptr);
4323 else if (mlxsw_sp_is_vrf_event(event, ptr))
4324 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
4325 else if (mlxsw_sp_port_dev_check(dev))
4326 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
4327 else if (netif_is_lag_master(dev))
4328 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
4329 else if (is_vlan_dev(dev))
4330 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
4331 else if (netif_is_bridge_master(dev))
4332 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
4333 else if (netif_is_macvlan(dev))
4334 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
4336 return notifier_from_errno(err);
4339 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
4340 .notifier_call = mlxsw_sp_inetaddr_valid_event,
4343 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
4344 .notifier_call = mlxsw_sp_inet6addr_valid_event,
4347 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
4348 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
4352 static struct pci_driver mlxsw_sp1_pci_driver = {
4353 .name = mlxsw_sp1_driver_name,
4354 .id_table = mlxsw_sp1_pci_id_table,
4357 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
4358 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
4362 static struct pci_driver mlxsw_sp2_pci_driver = {
4363 .name = mlxsw_sp2_driver_name,
4364 .id_table = mlxsw_sp2_pci_id_table,
4367 static const struct pci_device_id mlxsw_sp3_pci_id_table[] = {
4368 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM3), 0},
4372 static struct pci_driver mlxsw_sp3_pci_driver = {
4373 .name = mlxsw_sp3_driver_name,
4374 .id_table = mlxsw_sp3_pci_id_table,
4377 static int __init mlxsw_sp_module_init(void)
4381 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
4382 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
4384 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
4386 goto err_sp1_core_driver_register;
4388 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
4390 goto err_sp2_core_driver_register;
4392 err = mlxsw_core_driver_register(&mlxsw_sp3_driver);
4394 goto err_sp3_core_driver_register;
4396 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
4398 goto err_sp1_pci_driver_register;
4400 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
4402 goto err_sp2_pci_driver_register;
4404 err = mlxsw_pci_driver_register(&mlxsw_sp3_pci_driver);
4406 goto err_sp3_pci_driver_register;
4410 err_sp3_pci_driver_register:
4411 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
4412 err_sp2_pci_driver_register:
4413 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
4414 err_sp1_pci_driver_register:
4415 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
4416 err_sp3_core_driver_register:
4417 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
4418 err_sp2_core_driver_register:
4419 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
4420 err_sp1_core_driver_register:
4421 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
4422 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
4426 static void __exit mlxsw_sp_module_exit(void)
4428 mlxsw_pci_driver_unregister(&mlxsw_sp3_pci_driver);
4429 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
4430 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
4431 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
4432 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
4433 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
4434 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
4435 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
4438 module_init(mlxsw_sp_module_init);
4439 module_exit(mlxsw_sp_module_exit);
4441 MODULE_LICENSE("Dual BSD/GPL");
4442 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4443 MODULE_DESCRIPTION("Mellanox Spectrum driver");
4444 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
4445 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);
4446 MODULE_DEVICE_TABLE(pci, mlxsw_sp3_pci_id_table);
4447 MODULE_FIRMWARE(MLXSW_SP1_FW_FILENAME);
4448 MODULE_FIRMWARE(MLXSW_SP2_FW_FILENAME);
4449 MODULE_FIRMWARE(MLXSW_SP3_FW_FILENAME);