2 * drivers/net/ethernet/mellanox/mlxsw/spectrum.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/slab.h>
44 #include <linux/device.h>
45 #include <linux/skbuff.h>
46 #include <linux/if_vlan.h>
47 #include <linux/if_bridge.h>
48 #include <linux/workqueue.h>
49 #include <linux/jiffies.h>
50 #include <linux/bitops.h>
51 #include <linux/list.h>
52 #include <net/switchdev.h>
53 #include <generated/utsrelease.h>
62 static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum";
63 static const char mlxsw_sp_driver_version[] = "1.0";
69 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
72 * Packet control type.
73 * 0 - Ethernet control (e.g. EMADs, LACP)
76 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
79 * Packet protocol type. Must be set to 1 (Ethernet).
81 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
83 /* tx_hdr_rx_is_router
84 * Packet is sent from the router. Valid for data packets only.
86 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
89 * Indicates if the 'fid' field is valid and should be used for
90 * forwarding lookup. Valid for data packets only.
92 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
95 * Switch partition ID. Must be set to 0.
97 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
99 /* tx_hdr_control_tclass
100 * Indicates if the packet should use the control TClass and not one
101 * of the data TClasses.
103 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
106 * Egress TClass to be used on the egress device on the egress port.
108 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
111 * Destination local port for unicast packets.
112 * Destination multicast ID for multicast packets.
114 * Control packets are directed to a specific egress port, while data
115 * packets are transmitted through the CPU port (0) into the switch partition,
116 * where forwarding rules are applied.
118 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
121 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
122 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
123 * Valid for data packets only.
125 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
129 * 6 - Control packets
131 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
133 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
134 const struct mlxsw_tx_info *tx_info)
136 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
138 memset(txhdr, 0, MLXSW_TXHDR_LEN);
140 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
141 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
142 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
143 mlxsw_tx_hdr_swid_set(txhdr, 0);
144 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
145 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
146 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
149 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
151 char spad_pl[MLXSW_REG_SPAD_LEN];
154 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
157 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
161 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
164 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
165 char paos_pl[MLXSW_REG_PAOS_LEN];
167 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
168 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
169 MLXSW_PORT_ADMIN_STATUS_DOWN);
170 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
173 static int mlxsw_sp_port_oper_status_get(struct mlxsw_sp_port *mlxsw_sp_port,
176 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
177 char paos_pl[MLXSW_REG_PAOS_LEN];
181 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port, 0);
182 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
185 oper_status = mlxsw_reg_paos_oper_status_get(paos_pl);
186 *p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP ? true : false;
190 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
193 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
194 char ppad_pl[MLXSW_REG_PPAD_LEN];
196 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
197 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
198 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
201 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
203 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
204 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
206 ether_addr_copy(addr, mlxsw_sp->base_mac);
207 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
208 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
211 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
212 u16 vid, enum mlxsw_reg_spms_state state)
214 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
218 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
221 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
222 mlxsw_reg_spms_vid_pack(spms_pl, vid, state);
223 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
228 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
230 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
231 char pmtu_pl[MLXSW_REG_PMTU_LEN];
235 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
236 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
237 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
240 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
245 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
246 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
249 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
251 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
252 char pspa_pl[MLXSW_REG_PSPA_LEN];
254 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
255 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
258 static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
261 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
262 char svpe_pl[MLXSW_REG_SVPE_LEN];
264 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
265 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
268 int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port *mlxsw_sp_port,
269 enum mlxsw_reg_svfa_mt mt, bool valid, u16 fid,
272 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
273 char svfa_pl[MLXSW_REG_SVFA_LEN];
275 mlxsw_reg_svfa_pack(svfa_pl, mlxsw_sp_port->local_port, mt, valid,
277 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
280 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
281 u16 vid, bool learn_enable)
283 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
287 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
290 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
292 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
298 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
300 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
301 char sspr_pl[MLXSW_REG_SSPR_LEN];
303 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
304 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
307 static int mlxsw_sp_port_module_check(struct mlxsw_sp_port *mlxsw_sp_port,
310 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
311 char pmlp_pl[MLXSW_REG_PMLP_LEN];
314 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
315 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
318 *p_usable = mlxsw_reg_pmlp_width_get(pmlp_pl) ? true : false;
322 static int mlxsw_sp_port_open(struct net_device *dev)
324 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
327 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
330 netif_start_queue(dev);
334 static int mlxsw_sp_port_stop(struct net_device *dev)
336 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
338 netif_stop_queue(dev);
339 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
342 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
343 struct net_device *dev)
345 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
346 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
347 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
348 const struct mlxsw_tx_info tx_info = {
349 .local_port = mlxsw_sp_port->local_port,
355 if (mlxsw_core_skb_transmit_busy(mlxsw_sp, &tx_info))
356 return NETDEV_TX_BUSY;
358 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
359 struct sk_buff *skb_orig = skb;
361 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
363 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
364 dev_kfree_skb_any(skb_orig);
369 if (eth_skb_pad(skb)) {
370 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
374 mlxsw_sp_txhdr_construct(skb, &tx_info);
376 /* Due to a race we might fail here because of a full queue. In that
377 * unlikely case we simply drop the packet.
379 err = mlxsw_core_skb_transmit(mlxsw_sp, skb, &tx_info);
382 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
383 u64_stats_update_begin(&pcpu_stats->syncp);
384 pcpu_stats->tx_packets++;
385 pcpu_stats->tx_bytes += len;
386 u64_stats_update_end(&pcpu_stats->syncp);
388 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
389 dev_kfree_skb_any(skb);
394 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
398 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
400 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
401 struct sockaddr *addr = p;
404 if (!is_valid_ether_addr(addr->sa_data))
405 return -EADDRNOTAVAIL;
407 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
410 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
414 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
416 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
419 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
426 static struct rtnl_link_stats64 *
427 mlxsw_sp_port_get_stats64(struct net_device *dev,
428 struct rtnl_link_stats64 *stats)
430 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
431 struct mlxsw_sp_port_pcpu_stats *p;
432 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
437 for_each_possible_cpu(i) {
438 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
440 start = u64_stats_fetch_begin_irq(&p->syncp);
441 rx_packets = p->rx_packets;
442 rx_bytes = p->rx_bytes;
443 tx_packets = p->tx_packets;
444 tx_bytes = p->tx_bytes;
445 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
447 stats->rx_packets += rx_packets;
448 stats->rx_bytes += rx_bytes;
449 stats->tx_packets += tx_packets;
450 stats->tx_bytes += tx_bytes;
451 /* tx_dropped is u32, updated without syncp protection. */
452 tx_dropped += p->tx_dropped;
454 stats->tx_dropped = tx_dropped;
458 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
459 u16 vid_end, bool is_member, bool untagged)
461 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
465 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
469 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
470 vid_end, is_member, untagged);
471 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
476 static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
478 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
479 u16 vid, last_visited_vid;
482 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
483 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, vid,
486 last_visited_vid = vid;
487 goto err_port_vid_to_fid_set;
491 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
493 last_visited_vid = VLAN_N_VID;
494 goto err_port_vid_to_fid_set;
499 err_port_vid_to_fid_set:
500 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
501 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, vid,
506 static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
508 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
512 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
516 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
517 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false,
526 static struct mlxsw_sp_vfid *
527 mlxsw_sp_vfid_find(const struct mlxsw_sp *mlxsw_sp, u16 vid)
529 struct mlxsw_sp_vfid *vfid;
531 list_for_each_entry(vfid, &mlxsw_sp->port_vfids.list, list) {
532 if (vfid->vid == vid)
539 static u16 mlxsw_sp_avail_vfid_get(const struct mlxsw_sp *mlxsw_sp)
541 return find_first_zero_bit(mlxsw_sp->port_vfids.mapped,
542 MLXSW_SP_VFID_PORT_MAX);
545 static int __mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp, u16 vfid)
547 u16 fid = mlxsw_sp_vfid_to_fid(vfid);
548 char sfmr_pl[MLXSW_REG_SFMR_LEN];
550 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, 0);
551 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
554 static void __mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp, u16 vfid)
556 u16 fid = mlxsw_sp_vfid_to_fid(vfid);
557 char sfmr_pl[MLXSW_REG_SFMR_LEN];
559 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID, fid, 0);
560 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
563 static struct mlxsw_sp_vfid *mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp,
566 struct device *dev = mlxsw_sp->bus_info->dev;
567 struct mlxsw_sp_vfid *vfid;
571 n_vfid = mlxsw_sp_avail_vfid_get(mlxsw_sp);
572 if (n_vfid == MLXSW_SP_VFID_PORT_MAX) {
573 dev_err(dev, "No available vFIDs\n");
574 return ERR_PTR(-ERANGE);
577 err = __mlxsw_sp_vfid_create(mlxsw_sp, n_vfid);
579 dev_err(dev, "Failed to create vFID=%d\n", n_vfid);
583 vfid = kzalloc(sizeof(*vfid), GFP_KERNEL);
585 goto err_allocate_vfid;
590 list_add(&vfid->list, &mlxsw_sp->port_vfids.list);
591 set_bit(n_vfid, mlxsw_sp->port_vfids.mapped);
596 __mlxsw_sp_vfid_destroy(mlxsw_sp, n_vfid);
597 return ERR_PTR(-ENOMEM);
600 static void mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp,
601 struct mlxsw_sp_vfid *vfid)
603 clear_bit(vfid->vfid, mlxsw_sp->port_vfids.mapped);
604 list_del(&vfid->list);
606 __mlxsw_sp_vfid_destroy(mlxsw_sp, vfid->vfid);
611 static struct mlxsw_sp_port *
612 mlxsw_sp_port_vport_create(struct mlxsw_sp_port *mlxsw_sp_port,
613 struct mlxsw_sp_vfid *vfid)
615 struct mlxsw_sp_port *mlxsw_sp_vport;
617 mlxsw_sp_vport = kzalloc(sizeof(*mlxsw_sp_vport), GFP_KERNEL);
621 /* dev will be set correctly after the VLAN device is linked
622 * with the real device. In case of bridge SELF invocation, dev
625 mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
626 mlxsw_sp_vport->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
627 mlxsw_sp_vport->local_port = mlxsw_sp_port->local_port;
628 mlxsw_sp_vport->stp_state = BR_STATE_FORWARDING;
629 mlxsw_sp_vport->lagged = mlxsw_sp_port->lagged;
630 mlxsw_sp_vport->lag_id = mlxsw_sp_port->lag_id;
631 mlxsw_sp_vport->vport.vfid = vfid;
632 mlxsw_sp_vport->vport.vid = vfid->vid;
634 list_add(&mlxsw_sp_vport->vport.list, &mlxsw_sp_port->vports_list);
636 return mlxsw_sp_vport;
639 static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_vport)
641 list_del(&mlxsw_sp_vport->vport.list);
642 kfree(mlxsw_sp_vport);
645 int mlxsw_sp_port_add_vid(struct net_device *dev, __be16 __always_unused proto,
648 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
649 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
650 struct mlxsw_sp_port *mlxsw_sp_vport;
651 struct mlxsw_sp_vfid *vfid;
654 /* VLAN 0 is added to HW filter when device goes up, but it is
655 * reserved in our case, so simply return.
660 if (mlxsw_sp_port_vport_find(mlxsw_sp_port, vid)) {
661 netdev_warn(dev, "VID=%d already configured\n", vid);
665 vfid = mlxsw_sp_vfid_find(mlxsw_sp, vid);
667 vfid = mlxsw_sp_vfid_create(mlxsw_sp, vid);
669 netdev_err(dev, "Failed to create vFID for VID=%d\n",
671 return PTR_ERR(vfid);
675 mlxsw_sp_vport = mlxsw_sp_port_vport_create(mlxsw_sp_port, vfid);
676 if (!mlxsw_sp_vport) {
677 netdev_err(dev, "Failed to create vPort for VID=%d\n", vid);
679 goto err_port_vport_create;
682 if (!vfid->nr_vports) {
683 err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid,
686 netdev_err(dev, "Failed to setup flooding for vFID=%d\n",
688 goto err_vport_flood_set;
692 /* When adding the first VLAN interface on a bridged port we need to
693 * transition all the active 802.1Q bridge VLANs to use explicit
694 * {Port, VID} to FID mappings and set the port's mode to Virtual mode.
696 if (list_is_singular(&mlxsw_sp_port->vports_list)) {
697 err = mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port);
699 netdev_err(dev, "Failed to set to Virtual mode\n");
700 goto err_port_vp_mode_trans;
704 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
705 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
707 mlxsw_sp_vfid_to_fid(vfid->vfid),
710 netdev_err(dev, "Failed to map {Port, VID=%d} to vFID=%d\n",
712 goto err_port_vid_to_fid_set;
715 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
717 netdev_err(dev, "Failed to disable learning for VID=%d\n", vid);
718 goto err_port_vid_learning_set;
721 err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, true, false);
723 netdev_err(dev, "Failed to set VLAN membership for VID=%d\n",
725 goto err_port_add_vid;
728 err = mlxsw_sp_port_stp_state_set(mlxsw_sp_vport, vid,
729 MLXSW_REG_SPMS_STATE_FORWARDING);
731 netdev_err(dev, "Failed to set STP state for VID=%d\n", vid);
732 goto err_port_stp_state_set;
739 err_port_stp_state_set:
740 mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false);
742 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
743 err_port_vid_learning_set:
744 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
745 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, false,
746 mlxsw_sp_vfid_to_fid(vfid->vfid), vid);
747 err_port_vid_to_fid_set:
748 if (list_is_singular(&mlxsw_sp_port->vports_list))
749 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
750 err_port_vp_mode_trans:
751 if (!vfid->nr_vports)
752 mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false,
755 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
756 err_port_vport_create:
757 if (!vfid->nr_vports)
758 mlxsw_sp_vfid_destroy(mlxsw_sp, vfid);
762 int mlxsw_sp_port_kill_vid(struct net_device *dev,
763 __be16 __always_unused proto, u16 vid)
765 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
766 struct mlxsw_sp_port *mlxsw_sp_vport;
767 struct mlxsw_sp_vfid *vfid;
770 /* VLAN 0 is removed from HW filter when device goes down, but
771 * it is reserved in our case, so simply return.
776 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
777 if (!mlxsw_sp_vport) {
778 netdev_warn(dev, "VID=%d does not exist\n", vid);
782 vfid = mlxsw_sp_vport->vport.vfid;
784 err = mlxsw_sp_port_stp_state_set(mlxsw_sp_vport, vid,
785 MLXSW_REG_SPMS_STATE_DISCARDING);
787 netdev_err(dev, "Failed to set STP state for VID=%d\n", vid);
791 err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false);
793 netdev_err(dev, "Failed to set VLAN membership for VID=%d\n",
798 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
800 netdev_err(dev, "Failed to enable learning for VID=%d\n", vid);
804 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
805 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
807 mlxsw_sp_vfid_to_fid(vfid->vfid),
810 netdev_err(dev, "Failed to invalidate {Port, VID=%d} to vFID=%d mapping\n",
815 /* When removing the last VLAN interface on a bridged port we need to
816 * transition all active 802.1Q bridge VLANs to use VID to FID
817 * mappings and set port's mode to VLAN mode.
819 if (list_is_singular(&mlxsw_sp_port->vports_list)) {
820 err = mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
822 netdev_err(dev, "Failed to set to VLAN mode\n");
828 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
830 /* Destroy the vFID if no vPorts are assigned to it anymore. */
831 if (!vfid->nr_vports)
832 mlxsw_sp_vfid_destroy(mlxsw_sp_port->mlxsw_sp, vfid);
837 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
838 .ndo_open = mlxsw_sp_port_open,
839 .ndo_stop = mlxsw_sp_port_stop,
840 .ndo_start_xmit = mlxsw_sp_port_xmit,
841 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
842 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
843 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
844 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
845 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
846 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
847 .ndo_fdb_add = switchdev_port_fdb_add,
848 .ndo_fdb_del = switchdev_port_fdb_del,
849 .ndo_fdb_dump = switchdev_port_fdb_dump,
850 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
851 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
852 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
855 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
856 struct ethtool_drvinfo *drvinfo)
858 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
859 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
861 strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver));
862 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
863 sizeof(drvinfo->version));
864 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
866 mlxsw_sp->bus_info->fw_rev.major,
867 mlxsw_sp->bus_info->fw_rev.minor,
868 mlxsw_sp->bus_info->fw_rev.subminor);
869 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
870 sizeof(drvinfo->bus_info));
873 struct mlxsw_sp_port_hw_stats {
874 char str[ETH_GSTRING_LEN];
875 u64 (*getter)(char *payload);
878 static const struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
880 .str = "a_frames_transmitted_ok",
881 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
884 .str = "a_frames_received_ok",
885 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
888 .str = "a_frame_check_sequence_errors",
889 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
892 .str = "a_alignment_errors",
893 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
896 .str = "a_octets_transmitted_ok",
897 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
900 .str = "a_octets_received_ok",
901 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
904 .str = "a_multicast_frames_xmitted_ok",
905 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
908 .str = "a_broadcast_frames_xmitted_ok",
909 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
912 .str = "a_multicast_frames_received_ok",
913 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
916 .str = "a_broadcast_frames_received_ok",
917 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
920 .str = "a_in_range_length_errors",
921 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
924 .str = "a_out_of_range_length_field",
925 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
928 .str = "a_frame_too_long_errors",
929 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
932 .str = "a_symbol_error_during_carrier",
933 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
936 .str = "a_mac_control_frames_transmitted",
937 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
940 .str = "a_mac_control_frames_received",
941 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
944 .str = "a_unsupported_opcodes_received",
945 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
948 .str = "a_pause_mac_ctrl_frames_received",
949 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
952 .str = "a_pause_mac_ctrl_frames_xmitted",
953 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
957 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
959 static void mlxsw_sp_port_get_strings(struct net_device *dev,
960 u32 stringset, u8 *data)
967 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
968 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
970 p += ETH_GSTRING_LEN;
976 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
977 enum ethtool_phys_id_state state)
979 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
980 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
981 char mlcr_pl[MLXSW_REG_MLCR_LEN];
985 case ETHTOOL_ID_ACTIVE:
988 case ETHTOOL_ID_INACTIVE:
995 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
996 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
999 static void mlxsw_sp_port_get_stats(struct net_device *dev,
1000 struct ethtool_stats *stats, u64 *data)
1002 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1003 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1004 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1008 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port);
1009 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1010 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++)
1011 data[i] = !err ? mlxsw_sp_port_hw_stats[i].getter(ppcnt_pl) : 0;
1014 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
1018 return MLXSW_SP_PORT_HW_STATS_LEN;
1024 struct mlxsw_sp_port_link_mode {
1031 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = {
1033 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
1034 .supported = SUPPORTED_100baseT_Full,
1035 .advertised = ADVERTISED_100baseT_Full,
1039 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX,
1043 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
1044 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
1045 .supported = SUPPORTED_1000baseKX_Full,
1046 .advertised = ADVERTISED_1000baseKX_Full,
1050 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
1051 .supported = SUPPORTED_10000baseT_Full,
1052 .advertised = ADVERTISED_10000baseT_Full,
1056 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
1057 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
1058 .supported = SUPPORTED_10000baseKX4_Full,
1059 .advertised = ADVERTISED_10000baseKX4_Full,
1063 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1064 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1065 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1066 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
1067 .supported = SUPPORTED_10000baseKR_Full,
1068 .advertised = ADVERTISED_10000baseKR_Full,
1072 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
1073 .supported = SUPPORTED_20000baseKR2_Full,
1074 .advertised = ADVERTISED_20000baseKR2_Full,
1078 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
1079 .supported = SUPPORTED_40000baseCR4_Full,
1080 .advertised = ADVERTISED_40000baseCR4_Full,
1084 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
1085 .supported = SUPPORTED_40000baseKR4_Full,
1086 .advertised = ADVERTISED_40000baseKR4_Full,
1090 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
1091 .supported = SUPPORTED_40000baseSR4_Full,
1092 .advertised = ADVERTISED_40000baseSR4_Full,
1096 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
1097 .supported = SUPPORTED_40000baseLR4_Full,
1098 .advertised = ADVERTISED_40000baseLR4_Full,
1102 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
1103 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
1104 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
1108 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
1109 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
1110 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
1114 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1115 .supported = SUPPORTED_56000baseKR4_Full,
1116 .advertised = ADVERTISED_56000baseKR4_Full,
1120 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
1121 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1122 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
1123 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
1128 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
1130 static u32 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto)
1132 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1133 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1134 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1135 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1136 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1137 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1138 return SUPPORTED_FIBRE;
1140 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1141 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1142 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1143 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
1144 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
1145 return SUPPORTED_Backplane;
1149 static u32 mlxsw_sp_from_ptys_supported_link(u32 ptys_eth_proto)
1154 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1155 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
1156 modes |= mlxsw_sp_port_link_mode[i].supported;
1161 static u32 mlxsw_sp_from_ptys_advert_link(u32 ptys_eth_proto)
1166 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1167 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
1168 modes |= mlxsw_sp_port_link_mode[i].advertised;
1173 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
1174 struct ethtool_cmd *cmd)
1176 u32 speed = SPEED_UNKNOWN;
1177 u8 duplex = DUPLEX_UNKNOWN;
1183 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1184 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) {
1185 speed = mlxsw_sp_port_link_mode[i].speed;
1186 duplex = DUPLEX_FULL;
1191 ethtool_cmd_speed_set(cmd, speed);
1192 cmd->duplex = duplex;
1195 static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto)
1197 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1198 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1199 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1200 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1203 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1204 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1205 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
1208 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1209 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1210 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1211 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
1217 static int mlxsw_sp_port_get_settings(struct net_device *dev,
1218 struct ethtool_cmd *cmd)
1220 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1221 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1222 char ptys_pl[MLXSW_REG_PTYS_LEN];
1224 u32 eth_proto_admin;
1228 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
1229 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1231 netdev_err(dev, "Failed to get proto");
1234 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap,
1235 ð_proto_admin, ð_proto_oper);
1237 cmd->supported = mlxsw_sp_from_ptys_supported_port(eth_proto_cap) |
1238 mlxsw_sp_from_ptys_supported_link(eth_proto_cap) |
1239 SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1240 cmd->advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_admin);
1241 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev),
1242 eth_proto_oper, cmd);
1244 eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
1245 cmd->port = mlxsw_sp_port_connector_port(eth_proto_oper);
1246 cmd->lp_advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_oper);
1248 cmd->transceiver = XCVR_INTERNAL;
1252 static u32 mlxsw_sp_to_ptys_advert_link(u32 advertising)
1257 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1258 if (advertising & mlxsw_sp_port_link_mode[i].advertised)
1259 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
1264 static u32 mlxsw_sp_to_ptys_speed(u32 speed)
1269 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1270 if (speed == mlxsw_sp_port_link_mode[i].speed)
1271 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
1276 static int mlxsw_sp_port_set_settings(struct net_device *dev,
1277 struct ethtool_cmd *cmd)
1279 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1280 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1281 char ptys_pl[MLXSW_REG_PTYS_LEN];
1285 u32 eth_proto_admin;
1289 speed = ethtool_cmd_speed(cmd);
1291 eth_proto_new = cmd->autoneg == AUTONEG_ENABLE ?
1292 mlxsw_sp_to_ptys_advert_link(cmd->advertising) :
1293 mlxsw_sp_to_ptys_speed(speed);
1295 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
1296 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1298 netdev_err(dev, "Failed to get proto");
1301 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap, ð_proto_admin, NULL);
1303 eth_proto_new = eth_proto_new & eth_proto_cap;
1304 if (!eth_proto_new) {
1305 netdev_err(dev, "Not supported proto admin requested");
1308 if (eth_proto_new == eth_proto_admin)
1311 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, eth_proto_new);
1312 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1314 netdev_err(dev, "Failed to set proto admin");
1318 err = mlxsw_sp_port_oper_status_get(mlxsw_sp_port, &is_up);
1320 netdev_err(dev, "Failed to get oper status");
1326 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1328 netdev_err(dev, "Failed to set admin status");
1332 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1334 netdev_err(dev, "Failed to set admin status");
1341 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
1342 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
1343 .get_link = ethtool_op_get_link,
1344 .get_strings = mlxsw_sp_port_get_strings,
1345 .set_phys_id = mlxsw_sp_port_set_phys_id,
1346 .get_ethtool_stats = mlxsw_sp_port_get_stats,
1347 .get_sset_count = mlxsw_sp_port_get_sset_count,
1348 .get_settings = mlxsw_sp_port_get_settings,
1349 .set_settings = mlxsw_sp_port_set_settings,
1352 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port)
1354 struct mlxsw_sp_port *mlxsw_sp_port;
1355 struct net_device *dev;
1360 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
1363 mlxsw_sp_port = netdev_priv(dev);
1364 mlxsw_sp_port->dev = dev;
1365 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
1366 mlxsw_sp_port->local_port = local_port;
1367 mlxsw_sp_port->learning = 1;
1368 mlxsw_sp_port->learning_sync = 1;
1369 mlxsw_sp_port->uc_flood = 1;
1370 mlxsw_sp_port->pvid = 1;
1371 bytes = DIV_ROUND_UP(VLAN_N_VID, BITS_PER_BYTE);
1372 mlxsw_sp_port->active_vlans = kzalloc(bytes, GFP_KERNEL);
1373 if (!mlxsw_sp_port->active_vlans) {
1375 goto err_port_active_vlans_alloc;
1377 INIT_LIST_HEAD(&mlxsw_sp_port->vports_list);
1379 mlxsw_sp_port->pcpu_stats =
1380 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
1381 if (!mlxsw_sp_port->pcpu_stats) {
1383 goto err_alloc_stats;
1386 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
1387 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
1389 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
1391 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
1392 mlxsw_sp_port->local_port);
1393 goto err_dev_addr_init;
1396 netif_carrier_off(dev);
1398 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
1399 NETIF_F_HW_VLAN_CTAG_FILTER;
1401 /* Each packet needs to have a Tx header (metadata) on top all other
1404 dev->hard_header_len += MLXSW_TXHDR_LEN;
1406 err = mlxsw_sp_port_module_check(mlxsw_sp_port, &usable);
1408 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to check module\n",
1409 mlxsw_sp_port->local_port);
1410 goto err_port_module_check;
1414 dev_dbg(mlxsw_sp->bus_info->dev, "Port %d: Not usable, skipping initialization\n",
1415 mlxsw_sp_port->local_port);
1416 goto port_not_usable;
1419 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
1421 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1422 mlxsw_sp_port->local_port);
1423 goto err_port_system_port_mapping_set;
1426 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
1428 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
1429 mlxsw_sp_port->local_port);
1430 goto err_port_swid_set;
1433 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
1435 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
1436 mlxsw_sp_port->local_port);
1437 goto err_port_mtu_set;
1440 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1442 goto err_port_admin_status_set;
1444 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
1446 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
1447 mlxsw_sp_port->local_port);
1448 goto err_port_buffers_init;
1451 mlxsw_sp_port_switchdev_init(mlxsw_sp_port);
1452 err = register_netdev(dev);
1454 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
1455 mlxsw_sp_port->local_port);
1456 goto err_register_netdev;
1459 err = mlxsw_sp_port_vlan_init(mlxsw_sp_port);
1461 goto err_port_vlan_init;
1463 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
1467 unregister_netdev(dev);
1468 err_register_netdev:
1469 err_port_buffers_init:
1470 err_port_admin_status_set:
1473 err_port_system_port_mapping_set:
1475 err_port_module_check:
1477 free_percpu(mlxsw_sp_port->pcpu_stats);
1479 kfree(mlxsw_sp_port->active_vlans);
1480 err_port_active_vlans_alloc:
1485 static void mlxsw_sp_port_vports_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1487 struct net_device *dev = mlxsw_sp_port->dev;
1488 struct mlxsw_sp_port *mlxsw_sp_vport, *tmp;
1490 list_for_each_entry_safe(mlxsw_sp_vport, tmp,
1491 &mlxsw_sp_port->vports_list, vport.list) {
1492 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1494 /* vPorts created for VLAN devices should already be gone
1495 * by now, since we unregistered the port netdev.
1497 WARN_ON(is_vlan_dev(mlxsw_sp_vport->dev));
1498 mlxsw_sp_port_kill_vid(dev, 0, vid);
1502 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
1504 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
1508 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
1509 mlxsw_sp_port_vports_fini(mlxsw_sp_port);
1510 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
1511 free_percpu(mlxsw_sp_port->pcpu_stats);
1512 kfree(mlxsw_sp_port->active_vlans);
1513 free_netdev(mlxsw_sp_port->dev);
1516 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
1520 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
1521 mlxsw_sp_port_remove(mlxsw_sp, i);
1522 kfree(mlxsw_sp->ports);
1525 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
1531 alloc_size = sizeof(struct mlxsw_sp_port *) * MLXSW_PORT_MAX_PORTS;
1532 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
1533 if (!mlxsw_sp->ports)
1536 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) {
1537 err = mlxsw_sp_port_create(mlxsw_sp, i);
1539 goto err_port_create;
1544 for (i--; i >= 1; i--)
1545 mlxsw_sp_port_remove(mlxsw_sp, i);
1546 kfree(mlxsw_sp->ports);
1550 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
1551 char *pude_pl, void *priv)
1553 struct mlxsw_sp *mlxsw_sp = priv;
1554 struct mlxsw_sp_port *mlxsw_sp_port;
1555 enum mlxsw_reg_pude_oper_status status;
1558 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
1559 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1560 if (!mlxsw_sp_port) {
1561 dev_warn(mlxsw_sp->bus_info->dev, "Port %d: Link event received for non-existent port\n",
1566 status = mlxsw_reg_pude_oper_status_get(pude_pl);
1567 if (status == MLXSW_PORT_OPER_STATUS_UP) {
1568 netdev_info(mlxsw_sp_port->dev, "link up\n");
1569 netif_carrier_on(mlxsw_sp_port->dev);
1571 netdev_info(mlxsw_sp_port->dev, "link down\n");
1572 netif_carrier_off(mlxsw_sp_port->dev);
1576 static struct mlxsw_event_listener mlxsw_sp_pude_event = {
1577 .func = mlxsw_sp_pude_event_func,
1578 .trap_id = MLXSW_TRAP_ID_PUDE,
1581 static int mlxsw_sp_event_register(struct mlxsw_sp *mlxsw_sp,
1582 enum mlxsw_event_trap_id trap_id)
1584 struct mlxsw_event_listener *el;
1585 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1589 case MLXSW_TRAP_ID_PUDE:
1590 el = &mlxsw_sp_pude_event;
1593 err = mlxsw_core_event_listener_register(mlxsw_sp->core, el, mlxsw_sp);
1597 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id);
1598 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
1600 goto err_event_trap_set;
1605 mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp);
1609 static void mlxsw_sp_event_unregister(struct mlxsw_sp *mlxsw_sp,
1610 enum mlxsw_event_trap_id trap_id)
1612 struct mlxsw_event_listener *el;
1615 case MLXSW_TRAP_ID_PUDE:
1616 el = &mlxsw_sp_pude_event;
1619 mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp);
1622 static void mlxsw_sp_rx_listener_func(struct sk_buff *skb, u8 local_port,
1625 struct mlxsw_sp *mlxsw_sp = priv;
1626 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
1627 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
1629 if (unlikely(!mlxsw_sp_port)) {
1630 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
1635 skb->dev = mlxsw_sp_port->dev;
1637 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
1638 u64_stats_update_begin(&pcpu_stats->syncp);
1639 pcpu_stats->rx_packets++;
1640 pcpu_stats->rx_bytes += skb->len;
1641 u64_stats_update_end(&pcpu_stats->syncp);
1643 skb->protocol = eth_type_trans(skb, skb->dev);
1644 netif_receive_skb(skb);
1647 static const struct mlxsw_rx_listener mlxsw_sp_rx_listener[] = {
1649 .func = mlxsw_sp_rx_listener_func,
1650 .local_port = MLXSW_PORT_DONT_CARE,
1651 .trap_id = MLXSW_TRAP_ID_FDB_MC,
1653 /* Traps for specific L2 packet types, not trapped as FDB MC */
1655 .func = mlxsw_sp_rx_listener_func,
1656 .local_port = MLXSW_PORT_DONT_CARE,
1657 .trap_id = MLXSW_TRAP_ID_STP,
1660 .func = mlxsw_sp_rx_listener_func,
1661 .local_port = MLXSW_PORT_DONT_CARE,
1662 .trap_id = MLXSW_TRAP_ID_LACP,
1665 .func = mlxsw_sp_rx_listener_func,
1666 .local_port = MLXSW_PORT_DONT_CARE,
1667 .trap_id = MLXSW_TRAP_ID_EAPOL,
1670 .func = mlxsw_sp_rx_listener_func,
1671 .local_port = MLXSW_PORT_DONT_CARE,
1672 .trap_id = MLXSW_TRAP_ID_LLDP,
1675 .func = mlxsw_sp_rx_listener_func,
1676 .local_port = MLXSW_PORT_DONT_CARE,
1677 .trap_id = MLXSW_TRAP_ID_MMRP,
1680 .func = mlxsw_sp_rx_listener_func,
1681 .local_port = MLXSW_PORT_DONT_CARE,
1682 .trap_id = MLXSW_TRAP_ID_MVRP,
1685 .func = mlxsw_sp_rx_listener_func,
1686 .local_port = MLXSW_PORT_DONT_CARE,
1687 .trap_id = MLXSW_TRAP_ID_RPVST,
1690 .func = mlxsw_sp_rx_listener_func,
1691 .local_port = MLXSW_PORT_DONT_CARE,
1692 .trap_id = MLXSW_TRAP_ID_DHCP,
1695 .func = mlxsw_sp_rx_listener_func,
1696 .local_port = MLXSW_PORT_DONT_CARE,
1697 .trap_id = MLXSW_TRAP_ID_IGMP_QUERY,
1700 .func = mlxsw_sp_rx_listener_func,
1701 .local_port = MLXSW_PORT_DONT_CARE,
1702 .trap_id = MLXSW_TRAP_ID_IGMP_V1_REPORT,
1705 .func = mlxsw_sp_rx_listener_func,
1706 .local_port = MLXSW_PORT_DONT_CARE,
1707 .trap_id = MLXSW_TRAP_ID_IGMP_V2_REPORT,
1710 .func = mlxsw_sp_rx_listener_func,
1711 .local_port = MLXSW_PORT_DONT_CARE,
1712 .trap_id = MLXSW_TRAP_ID_IGMP_V2_LEAVE,
1715 .func = mlxsw_sp_rx_listener_func,
1716 .local_port = MLXSW_PORT_DONT_CARE,
1717 .trap_id = MLXSW_TRAP_ID_IGMP_V3_REPORT,
1721 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
1723 char htgt_pl[MLXSW_REG_HTGT_LEN];
1724 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1728 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX);
1729 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl);
1733 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL);
1734 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl);
1738 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) {
1739 err = mlxsw_core_rx_listener_register(mlxsw_sp->core,
1740 &mlxsw_sp_rx_listener[i],
1743 goto err_rx_listener_register;
1745 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU,
1746 mlxsw_sp_rx_listener[i].trap_id);
1747 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
1749 goto err_rx_trap_set;
1754 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
1755 &mlxsw_sp_rx_listener[i],
1757 err_rx_listener_register:
1758 for (i--; i >= 0; i--) {
1759 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
1760 mlxsw_sp_rx_listener[i].trap_id);
1761 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
1763 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
1764 &mlxsw_sp_rx_listener[i],
1770 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
1772 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1775 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) {
1776 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
1777 mlxsw_sp_rx_listener[i].trap_id);
1778 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
1780 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
1781 &mlxsw_sp_rx_listener[i],
1786 static int __mlxsw_sp_flood_init(struct mlxsw_core *mlxsw_core,
1787 enum mlxsw_reg_sfgc_type type,
1788 enum mlxsw_reg_sfgc_bridge_type bridge_type)
1790 enum mlxsw_flood_table_type table_type;
1791 enum mlxsw_sp_flood_table flood_table;
1792 char sfgc_pl[MLXSW_REG_SFGC_LEN];
1794 if (bridge_type == MLXSW_REG_SFGC_BRIDGE_TYPE_VFID)
1795 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
1797 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
1799 if (type == MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST)
1800 flood_table = MLXSW_SP_FLOOD_TABLE_UC;
1802 flood_table = MLXSW_SP_FLOOD_TABLE_BM;
1804 mlxsw_reg_sfgc_pack(sfgc_pl, type, bridge_type, table_type,
1806 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(sfgc), sfgc_pl);
1809 static int mlxsw_sp_flood_init(struct mlxsw_sp *mlxsw_sp)
1813 for (type = 0; type < MLXSW_REG_SFGC_TYPE_MAX; type++) {
1814 if (type == MLXSW_REG_SFGC_TYPE_RESERVED)
1817 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
1818 MLXSW_REG_SFGC_BRIDGE_TYPE_VFID);
1822 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
1823 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID);
1831 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
1833 char slcr_pl[MLXSW_REG_SLCR_LEN];
1835 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
1836 MLXSW_REG_SLCR_LAG_HASH_DMAC |
1837 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
1838 MLXSW_REG_SLCR_LAG_HASH_VLANID |
1839 MLXSW_REG_SLCR_LAG_HASH_SIP |
1840 MLXSW_REG_SLCR_LAG_HASH_DIP |
1841 MLXSW_REG_SLCR_LAG_HASH_SPORT |
1842 MLXSW_REG_SLCR_LAG_HASH_DPORT |
1843 MLXSW_REG_SLCR_LAG_HASH_IPPROTO);
1844 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
1847 static int mlxsw_sp_init(void *priv, struct mlxsw_core *mlxsw_core,
1848 const struct mlxsw_bus_info *mlxsw_bus_info)
1850 struct mlxsw_sp *mlxsw_sp = priv;
1853 mlxsw_sp->core = mlxsw_core;
1854 mlxsw_sp->bus_info = mlxsw_bus_info;
1855 INIT_LIST_HEAD(&mlxsw_sp->port_vfids.list);
1856 INIT_LIST_HEAD(&mlxsw_sp->br_vfids.list);
1858 err = mlxsw_sp_base_mac_get(mlxsw_sp);
1860 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
1864 err = mlxsw_sp_ports_create(mlxsw_sp);
1866 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
1870 err = mlxsw_sp_event_register(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
1872 dev_err(mlxsw_sp->bus_info->dev, "Failed to register for PUDE events\n");
1873 goto err_event_register;
1876 err = mlxsw_sp_traps_init(mlxsw_sp);
1878 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps for RX\n");
1879 goto err_rx_listener_register;
1882 err = mlxsw_sp_flood_init(mlxsw_sp);
1884 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize flood tables\n");
1885 goto err_flood_init;
1888 err = mlxsw_sp_buffers_init(mlxsw_sp);
1890 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
1891 goto err_buffers_init;
1894 err = mlxsw_sp_lag_init(mlxsw_sp);
1896 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
1900 err = mlxsw_sp_switchdev_init(mlxsw_sp);
1902 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
1903 goto err_switchdev_init;
1912 mlxsw_sp_traps_fini(mlxsw_sp);
1913 err_rx_listener_register:
1914 mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
1916 mlxsw_sp_ports_remove(mlxsw_sp);
1920 static void mlxsw_sp_fini(void *priv)
1922 struct mlxsw_sp *mlxsw_sp = priv;
1924 mlxsw_sp_switchdev_fini(mlxsw_sp);
1925 mlxsw_sp_traps_fini(mlxsw_sp);
1926 mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
1927 mlxsw_sp_ports_remove(mlxsw_sp);
1930 static struct mlxsw_config_profile mlxsw_sp_config_profile = {
1931 .used_max_vepa_channels = 1,
1932 .max_vepa_channels = 0,
1934 .max_lag = MLXSW_SP_LAG_MAX,
1935 .used_max_port_per_lag = 1,
1936 .max_port_per_lag = MLXSW_SP_PORT_PER_LAG_MAX,
1941 .used_max_system_port = 1,
1942 .max_system_port = 64,
1943 .used_max_vlan_groups = 1,
1944 .max_vlan_groups = 127,
1945 .used_max_regions = 1,
1947 .used_flood_tables = 1,
1948 .used_flood_mode = 1,
1950 .max_fid_offset_flood_tables = 2,
1951 .fid_offset_flood_table_size = VLAN_N_VID - 1,
1952 .max_fid_flood_tables = 2,
1953 .fid_flood_table_size = MLXSW_SP_VFID_MAX,
1954 .used_max_ib_mc = 1,
1961 .type = MLXSW_PORT_SWID_TYPE_ETH,
1966 static struct mlxsw_driver mlxsw_sp_driver = {
1967 .kind = MLXSW_DEVICE_KIND_SPECTRUM,
1968 .owner = THIS_MODULE,
1969 .priv_size = sizeof(struct mlxsw_sp),
1970 .init = mlxsw_sp_init,
1971 .fini = mlxsw_sp_fini,
1972 .txhdr_construct = mlxsw_sp_txhdr_construct,
1973 .txhdr_len = MLXSW_TXHDR_LEN,
1974 .profile = &mlxsw_sp_config_profile,
1977 static bool mlxsw_sp_port_dev_check(const struct net_device *dev)
1979 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
1982 static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port)
1984 struct net_device *dev = mlxsw_sp_port->dev;
1987 /* When port is not bridged untagged packets are tagged with
1988 * PVID=VID=1, thereby creating an implicit VLAN interface in
1989 * the device. Remove it and let bridge code take care of its
1992 err = mlxsw_sp_port_kill_vid(dev, 0, 1);
1994 netdev_err(dev, "Failed to remove VID 1\n");
1999 static int mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port)
2001 struct net_device *dev = mlxsw_sp_port->dev;
2004 /* Add implicit VLAN interface in the device, so that untagged
2005 * packets will be classified to the default vFID.
2007 err = mlxsw_sp_port_add_vid(dev, 0, 1);
2009 netdev_err(dev, "Failed to add VID 1\n");
2014 static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp *mlxsw_sp,
2015 struct net_device *br_dev)
2017 return !mlxsw_sp->master_bridge.dev ||
2018 mlxsw_sp->master_bridge.dev == br_dev;
2021 static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp *mlxsw_sp,
2022 struct net_device *br_dev)
2024 mlxsw_sp->master_bridge.dev = br_dev;
2025 mlxsw_sp->master_bridge.ref_count++;
2028 static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp *mlxsw_sp,
2029 struct net_device *br_dev)
2031 if (--mlxsw_sp->master_bridge.ref_count == 0)
2032 mlxsw_sp->master_bridge.dev = NULL;
2035 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
2037 char sldr_pl[MLXSW_REG_SLDR_LEN];
2039 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
2040 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
2043 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
2045 char sldr_pl[MLXSW_REG_SLDR_LEN];
2047 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
2048 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
2051 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
2052 u16 lag_id, u8 port_index)
2054 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2055 char slcor_pl[MLXSW_REG_SLCOR_LEN];
2057 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
2058 lag_id, port_index);
2059 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
2062 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
2065 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2066 char slcor_pl[MLXSW_REG_SLCOR_LEN];
2068 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
2070 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
2073 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
2076 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2077 char slcor_pl[MLXSW_REG_SLCOR_LEN];
2079 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
2081 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
2084 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
2087 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2088 char slcor_pl[MLXSW_REG_SLCOR_LEN];
2090 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
2092 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
2095 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
2096 struct net_device *lag_dev,
2099 struct mlxsw_sp_upper *lag;
2100 int free_lag_id = -1;
2103 for (i = 0; i < MLXSW_SP_LAG_MAX; i++) {
2104 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
2105 if (lag->ref_count) {
2106 if (lag->dev == lag_dev) {
2110 } else if (free_lag_id < 0) {
2114 if (free_lag_id < 0)
2116 *p_lag_id = free_lag_id;
2121 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
2122 struct net_device *lag_dev,
2123 struct netdev_lag_upper_info *lag_upper_info)
2127 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0)
2129 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2134 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
2135 u16 lag_id, u8 *p_port_index)
2139 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
2140 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
2148 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
2149 struct net_device *lag_dev)
2151 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2152 struct mlxsw_sp_upper *lag;
2157 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
2160 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
2161 if (!lag->ref_count) {
2162 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
2168 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
2171 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
2173 goto err_col_port_add;
2174 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id);
2176 goto err_col_port_enable;
2178 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
2179 mlxsw_sp_port->local_port);
2180 mlxsw_sp_port->lag_id = lag_id;
2181 mlxsw_sp_port->lagged = 1;
2186 if (!lag->ref_count)
2187 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
2188 err_col_port_enable:
2189 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
2193 static int mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2194 struct net_device *lag_dev)
2196 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2197 struct mlxsw_sp_upper *lag;
2198 u16 lag_id = mlxsw_sp_port->lag_id;
2201 if (!mlxsw_sp_port->lagged)
2203 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
2204 WARN_ON(lag->ref_count == 0);
2206 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id);
2209 err = mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
2213 if (lag->ref_count == 1) {
2214 err = mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
2219 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
2220 mlxsw_sp_port->local_port);
2221 mlxsw_sp_port->lagged = 0;
2226 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
2229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2230 char sldr_pl[MLXSW_REG_SLDR_LEN];
2232 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
2233 mlxsw_sp_port->local_port);
2234 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
2237 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
2240 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2241 char sldr_pl[MLXSW_REG_SLDR_LEN];
2243 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
2244 mlxsw_sp_port->local_port);
2245 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
2248 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port,
2249 bool lag_tx_enabled)
2252 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port,
2253 mlxsw_sp_port->lag_id);
2255 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
2256 mlxsw_sp_port->lag_id);
2259 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
2260 struct netdev_lag_lower_state_info *info)
2262 return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled);
2265 static int mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport,
2266 struct net_device *br_dev);
2268 static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port *mlxsw_sp_port,
2269 struct net_device *vlan_dev)
2271 struct mlxsw_sp_port *mlxsw_sp_vport;
2272 u16 vid = vlan_dev_vlan_id(vlan_dev);
2274 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
2275 if (!mlxsw_sp_vport) {
2276 WARN_ON(!mlxsw_sp_vport);
2280 mlxsw_sp_vport->dev = vlan_dev;
2285 static int mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port *mlxsw_sp_port,
2286 struct net_device *vlan_dev)
2288 struct mlxsw_sp_port *mlxsw_sp_vport;
2289 u16 vid = vlan_dev_vlan_id(vlan_dev);
2291 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
2292 if (!mlxsw_sp_vport) {
2293 WARN_ON(!mlxsw_sp_vport);
2297 /* When removing a VLAN device while still bridged we should first
2298 * remove it from the bridge, as we receive the bridge's notification
2299 * when the vPort is already gone.
2301 if (mlxsw_sp_vport->bridged) {
2302 struct net_device *br_dev;
2304 br_dev = mlxsw_sp_vport_br_get(mlxsw_sp_vport);
2305 mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport, br_dev);
2308 mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
2313 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *dev,
2314 unsigned long event, void *ptr)
2316 struct netdev_notifier_changeupper_info *info;
2317 struct mlxsw_sp_port *mlxsw_sp_port;
2318 struct net_device *upper_dev;
2319 struct mlxsw_sp *mlxsw_sp;
2322 mlxsw_sp_port = netdev_priv(dev);
2323 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2327 case NETDEV_PRECHANGEUPPER:
2328 upper_dev = info->upper_dev;
2329 if (!info->master || !info->linking)
2331 /* HW limitation forbids to put ports to multiple bridges. */
2332 if (netif_is_bridge_master(upper_dev) &&
2333 !mlxsw_sp_master_bridge_check(mlxsw_sp, upper_dev))
2335 if (netif_is_lag_master(upper_dev) &&
2336 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
2340 case NETDEV_CHANGEUPPER:
2341 upper_dev = info->upper_dev;
2342 if (is_vlan_dev(upper_dev)) {
2343 if (info->linking) {
2344 err = mlxsw_sp_port_vlan_link(mlxsw_sp_port,
2347 netdev_err(dev, "Failed to link VLAN device\n");
2351 err = mlxsw_sp_port_vlan_unlink(mlxsw_sp_port,
2354 netdev_err(dev, "Failed to unlink VLAN device\n");
2358 } else if (netif_is_bridge_master(upper_dev)) {
2359 if (info->linking) {
2360 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port);
2362 netdev_err(dev, "Failed to join bridge\n");
2363 mlxsw_sp_master_bridge_inc(mlxsw_sp, upper_dev);
2364 mlxsw_sp_port->bridged = 1;
2366 err = mlxsw_sp_port_bridge_leave(mlxsw_sp_port);
2368 netdev_err(dev, "Failed to leave bridge\n");
2369 mlxsw_sp_port->bridged = 0;
2370 mlxsw_sp_master_bridge_dec(mlxsw_sp, upper_dev);
2372 } else if (netif_is_lag_master(upper_dev)) {
2373 if (info->linking) {
2374 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
2377 netdev_err(dev, "Failed to join link aggregation\n");
2381 err = mlxsw_sp_port_lag_leave(mlxsw_sp_port,
2384 netdev_err(dev, "Failed to leave link aggregation\n");
2395 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
2396 unsigned long event, void *ptr)
2398 struct netdev_notifier_changelowerstate_info *info;
2399 struct mlxsw_sp_port *mlxsw_sp_port;
2402 mlxsw_sp_port = netdev_priv(dev);
2406 case NETDEV_CHANGELOWERSTATE:
2407 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
2408 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
2409 info->lower_state_info);
2411 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
2419 static int mlxsw_sp_netdevice_port_event(struct net_device *dev,
2420 unsigned long event, void *ptr)
2423 case NETDEV_PRECHANGEUPPER:
2424 case NETDEV_CHANGEUPPER:
2425 return mlxsw_sp_netdevice_port_upper_event(dev, event, ptr);
2426 case NETDEV_CHANGELOWERSTATE:
2427 return mlxsw_sp_netdevice_port_lower_event(dev, event, ptr);
2433 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
2434 unsigned long event, void *ptr)
2436 struct net_device *dev;
2437 struct list_head *iter;
2440 netdev_for_each_lower_dev(lag_dev, dev, iter) {
2441 if (mlxsw_sp_port_dev_check(dev)) {
2442 ret = mlxsw_sp_netdevice_port_event(dev, event, ptr);
2443 if (ret == NOTIFY_BAD)
2451 static struct mlxsw_sp_vfid *
2452 mlxsw_sp_br_vfid_find(const struct mlxsw_sp *mlxsw_sp,
2453 const struct net_device *br_dev)
2455 struct mlxsw_sp_vfid *vfid;
2457 list_for_each_entry(vfid, &mlxsw_sp->br_vfids.list, list) {
2458 if (vfid->br_dev == br_dev)
2465 static u16 mlxsw_sp_vfid_to_br_vfid(u16 vfid)
2467 return vfid - MLXSW_SP_VFID_PORT_MAX;
2470 static u16 mlxsw_sp_br_vfid_to_vfid(u16 br_vfid)
2472 return MLXSW_SP_VFID_PORT_MAX + br_vfid;
2475 static u16 mlxsw_sp_avail_br_vfid_get(const struct mlxsw_sp *mlxsw_sp)
2477 return find_first_zero_bit(mlxsw_sp->br_vfids.mapped,
2478 MLXSW_SP_VFID_BR_MAX);
2481 static struct mlxsw_sp_vfid *mlxsw_sp_br_vfid_create(struct mlxsw_sp *mlxsw_sp,
2482 struct net_device *br_dev)
2484 struct device *dev = mlxsw_sp->bus_info->dev;
2485 struct mlxsw_sp_vfid *vfid;
2489 n_vfid = mlxsw_sp_br_vfid_to_vfid(mlxsw_sp_avail_br_vfid_get(mlxsw_sp));
2490 if (n_vfid == MLXSW_SP_VFID_MAX) {
2491 dev_err(dev, "No available vFIDs\n");
2492 return ERR_PTR(-ERANGE);
2495 err = __mlxsw_sp_vfid_create(mlxsw_sp, n_vfid);
2497 dev_err(dev, "Failed to create vFID=%d\n", n_vfid);
2498 return ERR_PTR(err);
2501 vfid = kzalloc(sizeof(*vfid), GFP_KERNEL);
2503 goto err_allocate_vfid;
2505 vfid->vfid = n_vfid;
2506 vfid->br_dev = br_dev;
2508 list_add(&vfid->list, &mlxsw_sp->br_vfids.list);
2509 set_bit(mlxsw_sp_vfid_to_br_vfid(n_vfid), mlxsw_sp->br_vfids.mapped);
2514 __mlxsw_sp_vfid_destroy(mlxsw_sp, n_vfid);
2515 return ERR_PTR(-ENOMEM);
2518 static void mlxsw_sp_br_vfid_destroy(struct mlxsw_sp *mlxsw_sp,
2519 struct mlxsw_sp_vfid *vfid)
2521 u16 br_vfid = mlxsw_sp_vfid_to_br_vfid(vfid->vfid);
2523 clear_bit(br_vfid, mlxsw_sp->br_vfids.mapped);
2524 list_del(&vfid->list);
2526 __mlxsw_sp_vfid_destroy(mlxsw_sp, vfid->vfid);
2531 static int mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport,
2532 struct net_device *br_dev)
2534 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2535 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
2536 struct net_device *dev = mlxsw_sp_vport->dev;
2537 struct mlxsw_sp_vfid *vfid, *new_vfid;
2540 vfid = mlxsw_sp_br_vfid_find(mlxsw_sp, br_dev);
2546 /* We need a vFID to go back to after leaving the bridge's vFID. */
2547 new_vfid = mlxsw_sp_vfid_find(mlxsw_sp, vid);
2549 new_vfid = mlxsw_sp_vfid_create(mlxsw_sp, vid);
2550 if (IS_ERR(new_vfid)) {
2551 netdev_err(dev, "Failed to create vFID for VID=%d\n",
2553 return PTR_ERR(new_vfid);
2557 /* Invalidate existing {Port, VID} to vFID mapping and create a new
2558 * one for the new vFID.
2560 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
2561 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
2563 mlxsw_sp_vfid_to_fid(vfid->vfid),
2566 netdev_err(dev, "Failed to invalidate {Port, VID} to vFID=%d mapping\n",
2568 goto err_port_vid_to_fid_invalidate;
2571 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
2572 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
2574 mlxsw_sp_vfid_to_fid(new_vfid->vfid),
2577 netdev_err(dev, "Failed to map {Port, VID} to vFID=%d\n",
2579 goto err_port_vid_to_fid_validate;
2582 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
2584 netdev_err(dev, "Failed to disable learning\n");
2585 goto err_port_vid_learning_set;
2588 err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false,
2591 netdev_err(dev, "Failed clear to clear flooding\n");
2592 goto err_vport_flood_set;
2595 /* Switch between the vFIDs and destroy the old one if needed. */
2596 new_vfid->nr_vports++;
2597 mlxsw_sp_vport->vport.vfid = new_vfid;
2599 if (!vfid->nr_vports)
2600 mlxsw_sp_br_vfid_destroy(mlxsw_sp, vfid);
2602 mlxsw_sp_vport->learning = 0;
2603 mlxsw_sp_vport->learning_sync = 0;
2604 mlxsw_sp_vport->uc_flood = 0;
2605 mlxsw_sp_vport->bridged = 0;
2609 err_vport_flood_set:
2610 err_port_vid_learning_set:
2611 err_port_vid_to_fid_validate:
2612 err_port_vid_to_fid_invalidate:
2613 /* Rollback vFID only if new. */
2614 if (!new_vfid->nr_vports)
2615 mlxsw_sp_vfid_destroy(mlxsw_sp, new_vfid);
2619 static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port *mlxsw_sp_vport,
2620 struct net_device *br_dev)
2622 struct mlxsw_sp_vfid *old_vfid = mlxsw_sp_vport->vport.vfid;
2623 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2624 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
2625 struct net_device *dev = mlxsw_sp_vport->dev;
2626 struct mlxsw_sp_vfid *vfid;
2629 vfid = mlxsw_sp_br_vfid_find(mlxsw_sp, br_dev);
2631 vfid = mlxsw_sp_br_vfid_create(mlxsw_sp, br_dev);
2633 netdev_err(dev, "Failed to create bridge vFID\n");
2634 return PTR_ERR(vfid);
2638 err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, true, false);
2640 netdev_err(dev, "Failed to setup flooding for vFID=%d\n",
2642 goto err_port_flood_set;
2645 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
2647 netdev_err(dev, "Failed to enable learning\n");
2648 goto err_port_vid_learning_set;
2651 /* We need to invalidate existing {Port, VID} to vFID mapping and
2652 * create a new one for the bridge's vFID.
2654 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
2655 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
2657 mlxsw_sp_vfid_to_fid(old_vfid->vfid),
2660 netdev_err(dev, "Failed to invalidate {Port, VID} to vFID=%d mapping\n",
2662 goto err_port_vid_to_fid_invalidate;
2665 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
2666 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID,
2668 mlxsw_sp_vfid_to_fid(vfid->vfid),
2671 netdev_err(dev, "Failed to map {Port, VID} to vFID=%d\n",
2673 goto err_port_vid_to_fid_validate;
2676 /* Switch between the vFIDs and destroy the old one if needed. */
2678 mlxsw_sp_vport->vport.vfid = vfid;
2679 old_vfid->nr_vports--;
2680 if (!old_vfid->nr_vports)
2681 mlxsw_sp_vfid_destroy(mlxsw_sp, old_vfid);
2683 mlxsw_sp_vport->learning = 1;
2684 mlxsw_sp_vport->learning_sync = 1;
2685 mlxsw_sp_vport->uc_flood = 1;
2686 mlxsw_sp_vport->bridged = 1;
2690 err_port_vid_to_fid_validate:
2691 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport,
2692 MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, false,
2693 mlxsw_sp_vfid_to_fid(old_vfid->vfid), vid);
2694 err_port_vid_to_fid_invalidate:
2695 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
2696 err_port_vid_learning_set:
2697 mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false, false);
2699 if (!vfid->nr_vports)
2700 mlxsw_sp_br_vfid_destroy(mlxsw_sp, vfid);
2705 mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port *mlxsw_sp_port,
2706 const struct net_device *br_dev)
2708 struct mlxsw_sp_port *mlxsw_sp_vport;
2710 list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list,
2712 if (mlxsw_sp_vport_br_get(mlxsw_sp_vport) == br_dev)
2719 static int mlxsw_sp_netdevice_vport_event(struct net_device *dev,
2720 unsigned long event, void *ptr,
2723 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2724 struct netdev_notifier_changeupper_info *info = ptr;
2725 struct mlxsw_sp_port *mlxsw_sp_vport;
2726 struct net_device *upper_dev;
2729 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
2732 case NETDEV_PRECHANGEUPPER:
2733 upper_dev = info->upper_dev;
2734 if (!info->master || !info->linking)
2736 if (!netif_is_bridge_master(upper_dev))
2738 /* We can't have multiple VLAN interfaces configured on
2739 * the same port and being members in the same bridge.
2741 if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port,
2745 case NETDEV_CHANGEUPPER:
2746 upper_dev = info->upper_dev;
2749 if (info->linking) {
2750 if (!mlxsw_sp_vport) {
2751 WARN_ON(!mlxsw_sp_vport);
2754 err = mlxsw_sp_vport_bridge_join(mlxsw_sp_vport,
2757 netdev_err(dev, "Failed to join bridge\n");
2761 /* We ignore bridge's unlinking notifications if vPort
2762 * is gone, since we already left the bridge when the
2763 * VLAN device was unlinked from the real device.
2765 if (!mlxsw_sp_vport)
2767 err = mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport,
2770 netdev_err(dev, "Failed to leave bridge\n");
2779 static int mlxsw_sp_netdevice_lag_vport_event(struct net_device *lag_dev,
2780 unsigned long event, void *ptr,
2783 struct net_device *dev;
2784 struct list_head *iter;
2787 netdev_for_each_lower_dev(lag_dev, dev, iter) {
2788 if (mlxsw_sp_port_dev_check(dev)) {
2789 ret = mlxsw_sp_netdevice_vport_event(dev, event, ptr,
2791 if (ret == NOTIFY_BAD)
2799 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
2800 unsigned long event, void *ptr)
2802 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
2803 u16 vid = vlan_dev_vlan_id(vlan_dev);
2805 if (mlxsw_sp_port_dev_check(real_dev))
2806 return mlxsw_sp_netdevice_vport_event(real_dev, event, ptr,
2808 else if (netif_is_lag_master(real_dev))
2809 return mlxsw_sp_netdevice_lag_vport_event(real_dev, event, ptr,
2815 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
2816 unsigned long event, void *ptr)
2818 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2820 if (mlxsw_sp_port_dev_check(dev))
2821 return mlxsw_sp_netdevice_port_event(dev, event, ptr);
2823 if (netif_is_lag_master(dev))
2824 return mlxsw_sp_netdevice_lag_event(dev, event, ptr);
2826 if (is_vlan_dev(dev))
2827 return mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
2832 static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly = {
2833 .notifier_call = mlxsw_sp_netdevice_event,
2836 static int __init mlxsw_sp_module_init(void)
2840 register_netdevice_notifier(&mlxsw_sp_netdevice_nb);
2841 err = mlxsw_core_driver_register(&mlxsw_sp_driver);
2843 goto err_core_driver_register;
2846 err_core_driver_register:
2847 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
2851 static void __exit mlxsw_sp_module_exit(void)
2853 mlxsw_core_driver_unregister(&mlxsw_sp_driver);
2854 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
2857 module_init(mlxsw_sp_module_init);
2858 module_exit(mlxsw_sp_module_exit);
2860 MODULE_LICENSE("Dual BSD/GPL");
2861 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2862 MODULE_DESCRIPTION("Mellanox Spectrum driver");
2863 MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SPECTRUM);