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/types.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/device.h>
10 #include <linux/skbuff.h>
11 #include <linux/if_vlan.h>
12 #include <linux/if_bridge.h>
13 #include <linux/workqueue.h>
14 #include <linux/jiffies.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <net/switchdev.h>
18 #include <net/vxlan.h>
20 #include "spectrum_span.h"
21 #include "spectrum_switchdev.h"
26 struct mlxsw_sp_bridge_ops;
28 struct mlxsw_sp_bridge {
29 struct mlxsw_sp *mlxsw_sp;
31 struct delayed_work dw;
32 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
33 unsigned int interval; /* ms */
35 #define MLXSW_SP_MIN_AGEING_TIME 10
36 #define MLXSW_SP_MAX_AGEING_TIME 1000000
37 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
39 bool vlan_enabled_exists;
40 struct list_head bridges_list;
41 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
42 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
43 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
44 const struct mlxsw_sp_bridge_ops *bridge_8021ad_ops;
47 struct mlxsw_sp_bridge_device {
48 struct net_device *dev;
49 struct list_head list;
50 struct list_head ports_list;
51 struct list_head mids_list;
55 const struct mlxsw_sp_bridge_ops *ops;
58 struct mlxsw_sp_bridge_port {
59 struct net_device *dev;
60 struct mlxsw_sp_bridge_device *bridge_device;
61 struct list_head list;
62 struct list_head vlans_list;
63 unsigned int ref_count;
74 struct mlxsw_sp_bridge_vlan {
75 struct list_head list;
76 struct list_head port_vlan_list;
80 struct mlxsw_sp_bridge_ops {
81 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
82 struct mlxsw_sp_bridge_port *bridge_port,
83 struct mlxsw_sp_port *mlxsw_sp_port,
84 struct netlink_ext_ack *extack);
85 void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
86 struct mlxsw_sp_bridge_port *bridge_port,
87 struct mlxsw_sp_port *mlxsw_sp_port);
88 int (*vxlan_join)(struct mlxsw_sp_bridge_device *bridge_device,
89 const struct net_device *vxlan_dev, u16 vid,
90 struct netlink_ext_ack *extack);
92 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
93 u16 vid, struct netlink_ext_ack *extack);
95 (*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
97 u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
98 const struct mlxsw_sp_fid *fid);
101 struct mlxsw_sp_switchdev_ops {
102 void (*init)(struct mlxsw_sp *mlxsw_sp);
106 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
107 struct mlxsw_sp_bridge_port *bridge_port,
111 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
112 struct mlxsw_sp_bridge_port *bridge_port);
115 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
116 struct mlxsw_sp_bridge_device
120 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
121 struct mlxsw_sp_bridge_port *bridge_port,
124 static struct mlxsw_sp_bridge_device *
125 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
126 const struct net_device *br_dev)
128 struct mlxsw_sp_bridge_device *bridge_device;
130 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
131 if (bridge_device->dev == br_dev)
132 return bridge_device;
137 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
138 const struct net_device *br_dev)
140 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
143 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
144 struct netdev_nested_priv *priv)
146 struct mlxsw_sp *mlxsw_sp = priv->data;
148 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
152 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
153 struct net_device *dev)
155 struct netdev_nested_priv priv = {
156 .data = (void *)mlxsw_sp,
159 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
160 netdev_walk_all_upper_dev_rcu(dev,
161 mlxsw_sp_bridge_device_upper_rif_destroy,
165 static int mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge *bridge,
166 struct net_device *br_dev,
167 struct netlink_ext_ack *extack)
169 struct net_device *dev, *stop_dev;
170 struct list_head *iter;
173 netdev_for_each_lower_dev(br_dev, dev, iter) {
174 if (netif_is_vxlan(dev) && netif_running(dev)) {
175 err = mlxsw_sp_bridge_vxlan_join(bridge->mlxsw_sp,
188 netdev_for_each_lower_dev(br_dev, dev, iter) {
189 if (netif_is_vxlan(dev) && netif_running(dev)) {
192 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
198 static void mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge *bridge,
199 struct net_device *br_dev)
201 struct net_device *dev;
202 struct list_head *iter;
204 netdev_for_each_lower_dev(br_dev, dev, iter) {
205 if (netif_is_vxlan(dev) && netif_running(dev))
206 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
210 static struct mlxsw_sp_bridge_device *
211 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
212 struct net_device *br_dev,
213 struct netlink_ext_ack *extack)
215 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
216 struct mlxsw_sp_bridge_device *bridge_device;
217 bool vlan_enabled = br_vlan_enabled(br_dev);
220 if (vlan_enabled && bridge->vlan_enabled_exists) {
221 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
222 NL_SET_ERR_MSG_MOD(extack, "Only one VLAN-aware bridge is supported");
223 return ERR_PTR(-EINVAL);
226 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 bridge_device->dev = br_dev;
231 bridge_device->vlan_enabled = vlan_enabled;
232 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
233 bridge_device->mrouter = br_multicast_router(br_dev);
234 INIT_LIST_HEAD(&bridge_device->ports_list);
238 bridge->vlan_enabled_exists = true;
239 br_vlan_get_proto(br_dev, &proto);
240 if (proto == ETH_P_8021AD)
241 bridge_device->ops = bridge->bridge_8021ad_ops;
243 bridge_device->ops = bridge->bridge_8021q_ops;
245 bridge_device->ops = bridge->bridge_8021d_ops;
247 INIT_LIST_HEAD(&bridge_device->mids_list);
248 list_add(&bridge_device->list, &bridge->bridges_list);
250 /* It is possible we already have VXLAN devices enslaved to the bridge.
251 * In which case, we need to replay their configuration as if they were
252 * just now enslaved to the bridge.
254 err = mlxsw_sp_bridge_device_vxlan_init(bridge, br_dev, extack);
258 return bridge_device;
261 list_del(&bridge_device->list);
262 if (bridge_device->vlan_enabled)
263 bridge->vlan_enabled_exists = false;
264 kfree(bridge_device);
269 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
270 struct mlxsw_sp_bridge_device *bridge_device)
272 mlxsw_sp_bridge_device_vxlan_fini(bridge, bridge_device->dev);
273 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
275 list_del(&bridge_device->list);
276 if (bridge_device->vlan_enabled)
277 bridge->vlan_enabled_exists = false;
278 WARN_ON(!list_empty(&bridge_device->ports_list));
279 WARN_ON(!list_empty(&bridge_device->mids_list));
280 kfree(bridge_device);
283 static struct mlxsw_sp_bridge_device *
284 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
285 struct net_device *br_dev,
286 struct netlink_ext_ack *extack)
288 struct mlxsw_sp_bridge_device *bridge_device;
290 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
292 return bridge_device;
294 return mlxsw_sp_bridge_device_create(bridge, br_dev, extack);
298 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
299 struct mlxsw_sp_bridge_device *bridge_device)
301 if (list_empty(&bridge_device->ports_list))
302 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
305 static struct mlxsw_sp_bridge_port *
306 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
307 const struct net_device *brport_dev)
309 struct mlxsw_sp_bridge_port *bridge_port;
311 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
312 if (bridge_port->dev == brport_dev)
319 struct mlxsw_sp_bridge_port *
320 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
321 struct net_device *brport_dev)
323 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
324 struct mlxsw_sp_bridge_device *bridge_device;
329 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
333 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
336 static struct mlxsw_sp_bridge_port *
337 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
338 struct net_device *brport_dev)
340 struct mlxsw_sp_bridge_port *bridge_port;
341 struct mlxsw_sp_port *mlxsw_sp_port;
343 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
347 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
348 bridge_port->lagged = mlxsw_sp_port->lagged;
349 if (bridge_port->lagged)
350 bridge_port->lag_id = mlxsw_sp_port->lag_id;
352 bridge_port->system_port = mlxsw_sp_port->local_port;
353 bridge_port->dev = brport_dev;
354 bridge_port->bridge_device = bridge_device;
355 bridge_port->stp_state = BR_STATE_DISABLED;
356 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
358 INIT_LIST_HEAD(&bridge_port->vlans_list);
359 list_add(&bridge_port->list, &bridge_device->ports_list);
360 bridge_port->ref_count = 1;
366 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
368 list_del(&bridge_port->list);
369 WARN_ON(!list_empty(&bridge_port->vlans_list));
373 static struct mlxsw_sp_bridge_port *
374 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
375 struct net_device *brport_dev,
376 struct netlink_ext_ack *extack)
378 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
379 struct mlxsw_sp_bridge_device *bridge_device;
380 struct mlxsw_sp_bridge_port *bridge_port;
383 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
385 bridge_port->ref_count++;
389 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev, extack);
390 if (IS_ERR(bridge_device))
391 return ERR_CAST(bridge_device);
393 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
396 goto err_bridge_port_create;
401 err_bridge_port_create:
402 mlxsw_sp_bridge_device_put(bridge, bridge_device);
406 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
407 struct mlxsw_sp_bridge_port *bridge_port)
409 struct mlxsw_sp_bridge_device *bridge_device;
411 if (--bridge_port->ref_count != 0)
413 bridge_device = bridge_port->bridge_device;
414 mlxsw_sp_bridge_port_destroy(bridge_port);
415 mlxsw_sp_bridge_device_put(bridge, bridge_device);
418 static struct mlxsw_sp_port_vlan *
419 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
420 const struct mlxsw_sp_bridge_device *
424 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
426 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
428 if (!mlxsw_sp_port_vlan->bridge_port)
430 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
433 if (bridge_device->vlan_enabled &&
434 mlxsw_sp_port_vlan->vid != vid)
436 return mlxsw_sp_port_vlan;
442 static struct mlxsw_sp_port_vlan*
443 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
446 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
448 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
450 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
452 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
453 return mlxsw_sp_port_vlan;
459 static struct mlxsw_sp_bridge_vlan *
460 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
463 struct mlxsw_sp_bridge_vlan *bridge_vlan;
465 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
466 if (bridge_vlan->vid == vid)
473 static struct mlxsw_sp_bridge_vlan *
474 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
476 struct mlxsw_sp_bridge_vlan *bridge_vlan;
478 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
482 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
483 bridge_vlan->vid = vid;
484 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
490 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
492 list_del(&bridge_vlan->list);
493 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
497 static struct mlxsw_sp_bridge_vlan *
498 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
500 struct mlxsw_sp_bridge_vlan *bridge_vlan;
502 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
506 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
509 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
511 if (list_empty(&bridge_vlan->port_vlan_list))
512 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
516 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
517 struct mlxsw_sp_bridge_vlan *bridge_vlan,
520 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
522 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
524 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
526 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
527 bridge_vlan->vid, state);
533 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
534 struct net_device *orig_dev,
537 struct mlxsw_sp_bridge_port *bridge_port;
538 struct mlxsw_sp_bridge_vlan *bridge_vlan;
541 /* It's possible we failed to enslave the port, yet this
542 * operation is executed due to it being deferred.
544 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
549 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
550 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
553 goto err_port_bridge_vlan_stp_set;
556 bridge_port->stp_state = state;
560 err_port_bridge_vlan_stp_set:
561 list_for_each_entry_continue_reverse(bridge_vlan,
562 &bridge_port->vlans_list, list)
563 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
564 bridge_port->stp_state);
569 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
570 struct mlxsw_sp_bridge_vlan *bridge_vlan,
571 enum mlxsw_sp_flood_type packet_type,
574 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
576 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
578 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
580 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
582 mlxsw_sp_port->local_port,
590 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
591 struct mlxsw_sp_bridge_port *bridge_port,
592 enum mlxsw_sp_flood_type packet_type,
595 struct mlxsw_sp_bridge_vlan *bridge_vlan;
598 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
599 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
604 goto err_port_bridge_vlan_flood_set;
609 err_port_bridge_vlan_flood_set:
610 list_for_each_entry_continue_reverse(bridge_vlan,
611 &bridge_port->vlans_list, list)
612 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
613 packet_type, !member);
618 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
619 struct mlxsw_sp_bridge_vlan *bridge_vlan,
622 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
623 u16 vid = bridge_vlan->vid;
625 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
627 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
629 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
636 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
637 struct mlxsw_sp_bridge_port *bridge_port,
640 struct mlxsw_sp_bridge_vlan *bridge_vlan;
643 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
644 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
647 goto err_port_bridge_vlan_learning_set;
652 err_port_bridge_vlan_learning_set:
653 list_for_each_entry_continue_reverse(bridge_vlan,
654 &bridge_port->vlans_list, list)
655 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
661 mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
662 struct switchdev_brport_flags flags)
664 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
670 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
671 struct net_device *orig_dev,
672 struct switchdev_brport_flags flags)
674 struct mlxsw_sp_bridge_port *bridge_port;
677 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
682 if (flags.mask & BR_FLOOD) {
683 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
685 MLXSW_SP_FLOOD_TYPE_UC,
686 flags.val & BR_FLOOD);
691 if (flags.mask & BR_LEARNING) {
692 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port,
694 flags.val & BR_LEARNING);
699 if (bridge_port->bridge_device->multicast_enabled)
702 if (flags.mask & BR_MCAST_FLOOD) {
703 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
705 MLXSW_SP_FLOOD_TYPE_MC,
706 flags.val & BR_MCAST_FLOOD);
712 memcpy(&bridge_port->flags, &flags.val, sizeof(flags.val));
716 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
718 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
721 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
722 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
725 mlxsw_sp->bridge->ageing_time = ageing_time;
729 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
730 unsigned long ageing_clock_t)
732 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
733 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
734 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
736 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
737 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
740 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
743 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
744 struct net_device *orig_dev,
747 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
748 struct mlxsw_sp_bridge_device *bridge_device;
750 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
751 if (WARN_ON(!bridge_device))
754 if (bridge_device->vlan_enabled == vlan_enabled)
757 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
761 static int mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port *mlxsw_sp_port,
762 struct net_device *orig_dev,
765 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
766 struct mlxsw_sp_bridge_device *bridge_device;
768 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
769 if (WARN_ON(!bridge_device))
772 netdev_err(bridge_device->dev, "VLAN protocol can't be changed on existing bridge\n");
776 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
777 struct net_device *orig_dev,
778 bool is_port_mrouter)
780 struct mlxsw_sp_bridge_port *bridge_port;
783 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
788 if (!bridge_port->bridge_device->multicast_enabled)
791 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
792 MLXSW_SP_FLOOD_TYPE_MC,
797 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
800 bridge_port->mrouter = is_port_mrouter;
804 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
806 const struct mlxsw_sp_bridge_device *bridge_device;
808 bridge_device = bridge_port->bridge_device;
809 return bridge_device->multicast_enabled ? bridge_port->mrouter :
810 bridge_port->flags & BR_MCAST_FLOOD;
813 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
814 struct net_device *orig_dev,
817 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
818 struct mlxsw_sp_bridge_device *bridge_device;
819 struct mlxsw_sp_bridge_port *bridge_port;
822 /* It's possible we failed to enslave the port, yet this
823 * operation is executed due to it being deferred.
825 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
829 if (bridge_device->multicast_enabled != !mc_disabled) {
830 bridge_device->multicast_enabled = !mc_disabled;
831 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
835 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
836 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
837 bool member = mlxsw_sp_mc_flood(bridge_port);
839 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
841 packet_type, member);
846 bridge_device->multicast_enabled = !mc_disabled;
851 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
852 u16 mid_idx, bool add)
857 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
861 mlxsw_reg_smid_pack(smid_pl, mid_idx,
862 mlxsw_sp_router_port(mlxsw_sp), add);
863 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
869 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
870 struct mlxsw_sp_bridge_device *bridge_device,
873 struct mlxsw_sp_mid *mid;
875 list_for_each_entry(mid, &bridge_device->mids_list, list)
876 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
880 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
881 struct net_device *orig_dev,
884 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
885 struct mlxsw_sp_bridge_device *bridge_device;
887 /* It's possible we failed to enslave the port, yet this
888 * operation is executed due to it being deferred.
890 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
894 if (bridge_device->mrouter != is_mrouter)
895 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
897 bridge_device->mrouter = is_mrouter;
901 static int mlxsw_sp_port_attr_set(struct net_device *dev, const void *ctx,
902 const struct switchdev_attr *attr,
903 struct netlink_ext_ack *extack)
905 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
909 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
910 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port,
914 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
915 err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
916 attr->u.brport_flags);
918 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
919 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port,
921 attr->u.brport_flags);
923 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
924 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port,
925 attr->u.ageing_time);
927 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
928 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port,
930 attr->u.vlan_filtering);
932 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL:
933 err = mlxsw_sp_port_attr_br_vlan_proto_set(mlxsw_sp_port,
935 attr->u.vlan_protocol);
937 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
938 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port,
942 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
943 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port,
945 attr->u.mc_disabled);
947 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
948 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port,
957 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
963 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
964 struct mlxsw_sp_bridge_port *bridge_port,
965 struct netlink_ext_ack *extack)
967 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
968 struct mlxsw_sp_bridge_device *bridge_device;
969 u8 local_port = mlxsw_sp_port->local_port;
970 u16 vid = mlxsw_sp_port_vlan->vid;
971 struct mlxsw_sp_fid *fid;
974 bridge_device = bridge_port->bridge_device;
975 fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
979 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
980 bridge_port->flags & BR_FLOOD);
982 goto err_fid_uc_flood_set;
984 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
985 mlxsw_sp_mc_flood(bridge_port));
987 goto err_fid_mc_flood_set;
989 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
992 goto err_fid_bc_flood_set;
994 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
996 goto err_fid_port_vid_map;
998 mlxsw_sp_port_vlan->fid = fid;
1002 err_fid_port_vid_map:
1003 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1004 err_fid_bc_flood_set:
1005 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1006 err_fid_mc_flood_set:
1007 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1008 err_fid_uc_flood_set:
1009 mlxsw_sp_fid_put(fid);
1014 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1016 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1017 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1018 u8 local_port = mlxsw_sp_port->local_port;
1019 u16 vid = mlxsw_sp_port_vlan->vid;
1021 mlxsw_sp_port_vlan->fid = NULL;
1022 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
1023 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1024 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1025 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1026 mlxsw_sp_fid_put(fid);
1030 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1031 u16 vid, bool is_pvid)
1035 else if (mlxsw_sp_port->pvid == vid)
1036 return 0; /* Dis-allow untagged packets */
1038 return mlxsw_sp_port->pvid;
1042 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1043 struct mlxsw_sp_bridge_port *bridge_port,
1044 struct netlink_ext_ack *extack)
1046 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1047 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1048 u16 vid = mlxsw_sp_port_vlan->vid;
1051 /* No need to continue if only VLAN flags were changed */
1052 if (mlxsw_sp_port_vlan->bridge_port)
1055 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
1060 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1061 bridge_port->flags & BR_LEARNING);
1063 goto err_port_vid_learning_set;
1065 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1066 bridge_port->stp_state);
1068 goto err_port_vid_stp_set;
1070 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1073 goto err_bridge_vlan_get;
1076 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1077 &bridge_vlan->port_vlan_list);
1079 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1080 bridge_port->dev, extack);
1081 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1085 err_bridge_vlan_get:
1086 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1087 err_port_vid_stp_set:
1088 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1089 err_port_vid_learning_set:
1090 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1095 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1097 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1098 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1099 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1100 struct mlxsw_sp_bridge_port *bridge_port;
1101 u16 vid = mlxsw_sp_port_vlan->vid;
1102 bool last_port, last_vlan;
1104 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1105 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1108 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1109 last_vlan = list_is_singular(&bridge_port->vlans_list);
1110 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1111 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1113 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1114 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1115 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1116 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1118 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1120 mlxsw_sp_fid_index(fid));
1122 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1124 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1126 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1127 mlxsw_sp_port_vlan->bridge_port = NULL;
1131 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1132 struct mlxsw_sp_bridge_port *bridge_port,
1133 u16 vid, bool is_untagged, bool is_pvid,
1134 struct netlink_ext_ack *extack)
1136 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1137 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1138 u16 old_pvid = mlxsw_sp_port->pvid;
1142 /* The only valid scenario in which a port-vlan already exists, is if
1143 * the VLAN flags were changed and the port-vlan is associated with the
1144 * correct bridge port
1146 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1147 if (mlxsw_sp_port_vlan &&
1148 mlxsw_sp_port_vlan->bridge_port != bridge_port)
1151 if (!mlxsw_sp_port_vlan) {
1152 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1154 if (IS_ERR(mlxsw_sp_port_vlan))
1155 return PTR_ERR(mlxsw_sp_port_vlan);
1158 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1161 goto err_port_vlan_set;
1163 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1164 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1166 goto err_port_pvid_set;
1168 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1171 goto err_port_vlan_bridge_join;
1175 err_port_vlan_bridge_join:
1176 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto);
1178 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1180 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1185 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1186 const struct net_device *br_dev,
1187 const struct switchdev_obj_port_vlan *vlan)
1191 pvid = mlxsw_sp_rif_vid(mlxsw_sp, br_dev);
1195 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1196 if (vlan->vid != pvid) {
1197 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1201 if (vlan->vid == pvid) {
1202 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1210 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1211 const struct switchdev_obj_port_vlan *vlan,
1212 struct netlink_ext_ack *extack)
1214 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1215 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1216 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1217 struct net_device *orig_dev = vlan->obj.orig_dev;
1218 struct mlxsw_sp_bridge_port *bridge_port;
1220 if (netif_is_bridge_master(orig_dev)) {
1223 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1224 br_vlan_enabled(orig_dev))
1225 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1232 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1233 if (WARN_ON(!bridge_port))
1236 if (!bridge_port->bridge_device->vlan_enabled)
1239 return mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1240 vlan->vid, flag_untagged,
1244 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1246 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1247 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1251 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1252 struct mlxsw_sp_bridge_port *bridge_port,
1255 bool lagged = bridge_port->lagged;
1256 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1259 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1260 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1261 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1262 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1264 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1267 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1269 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1270 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1273 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1275 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1276 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1279 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1280 const char *mac, u16 fid,
1281 enum mlxsw_sp_l3proto proto,
1282 const union mlxsw_sp_l3addr *addr,
1283 bool adding, bool dynamic)
1285 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1292 case MLXSW_SP_L3_PROTO_IPV4:
1293 uip = be32_to_cpu(addr->addr4);
1294 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1296 case MLXSW_SP_L3_PROTO_IPV6:
1302 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1306 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1307 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1308 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1309 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1311 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1312 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1316 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1324 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1325 const char *mac, u16 fid, bool adding,
1326 enum mlxsw_reg_sfd_rec_action action,
1327 enum mlxsw_reg_sfd_rec_policy policy)
1333 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1337 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1338 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1339 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1340 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1344 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1352 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1353 const char *mac, u16 fid, bool adding,
1356 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1357 MLXSW_REG_SFD_REC_ACTION_NOP,
1358 mlxsw_sp_sfd_rec_policy(dynamic));
1361 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1364 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1365 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1366 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1369 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1370 const char *mac, u16 fid, u16 lag_vid,
1371 bool adding, bool dynamic)
1377 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1381 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1382 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1383 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1385 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1386 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1390 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1399 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1400 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1402 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1403 struct net_device *orig_dev = fdb_info->info.dev;
1404 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1405 struct mlxsw_sp_bridge_device *bridge_device;
1406 struct mlxsw_sp_bridge_port *bridge_port;
1409 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1413 bridge_device = bridge_port->bridge_device;
1414 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1417 if (!mlxsw_sp_port_vlan)
1420 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1421 vid = mlxsw_sp_port_vlan->vid;
1423 if (!bridge_port->lagged)
1424 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1425 bridge_port->system_port,
1426 fdb_info->addr, fid_index,
1429 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1430 bridge_port->lag_id,
1431 fdb_info->addr, fid_index,
1432 vid, adding, false);
1435 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1436 u16 fid, u16 mid_idx, bool adding)
1442 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1446 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1447 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1448 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1449 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1450 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1454 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1462 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1464 bool set_router_port)
1469 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1473 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1474 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1475 if (mlxsw_sp->ports[i])
1476 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1479 mlxsw_reg_smid_port_mask_set(smid_pl,
1480 mlxsw_sp_router_port(mlxsw_sp), 1);
1482 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1483 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1485 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1488 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1493 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1494 u16 mid_idx, bool add)
1496 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1500 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1504 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1505 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1511 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1512 const unsigned char *addr,
1515 struct mlxsw_sp_mid *mid;
1517 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1518 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1525 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1526 struct mlxsw_sp_bridge_port *bridge_port,
1527 unsigned long *ports_bitmap)
1529 struct mlxsw_sp_port *mlxsw_sp_port;
1530 u64 max_lag_members, i;
1533 if (!bridge_port->lagged) {
1534 set_bit(bridge_port->system_port, ports_bitmap);
1536 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1538 lag_id = bridge_port->lag_id;
1539 for (i = 0; i < max_lag_members; i++) {
1540 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1543 set_bit(mlxsw_sp_port->local_port,
1550 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1551 struct mlxsw_sp_bridge_device *bridge_device,
1552 struct mlxsw_sp *mlxsw_sp)
1554 struct mlxsw_sp_bridge_port *bridge_port;
1556 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1557 if (bridge_port->mrouter) {
1558 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1566 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1567 struct mlxsw_sp_mid *mid,
1568 struct mlxsw_sp_bridge_device *bridge_device)
1576 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1578 if (mid_idx == MLXSW_SP_MID_MAX)
1581 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1582 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1583 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1587 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1588 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1591 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1592 bridge_device->mrouter);
1593 kfree(flood_bitmap);
1597 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1602 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1607 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1608 struct mlxsw_sp_mid *mid)
1613 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1615 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1620 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1621 struct mlxsw_sp_bridge_device *bridge_device,
1622 const unsigned char *addr,
1625 struct mlxsw_sp_mid *mid;
1628 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1632 alloc_size = sizeof(unsigned long) *
1633 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1635 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1636 if (!mid->ports_in_mid)
1637 goto err_ports_in_mid_alloc;
1639 ether_addr_copy(mid->addr, addr);
1643 if (!bridge_device->multicast_enabled)
1646 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1647 goto err_write_mdb_entry;
1650 list_add_tail(&mid->list, &bridge_device->mids_list);
1653 err_write_mdb_entry:
1654 kfree(mid->ports_in_mid);
1655 err_ports_in_mid_alloc:
1660 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1661 struct mlxsw_sp_mid *mid)
1663 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1666 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1667 if (bitmap_empty(mid->ports_in_mid,
1668 mlxsw_core_max_ports(mlxsw_sp->core))) {
1669 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1670 list_del(&mid->list);
1671 kfree(mid->ports_in_mid);
1677 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1678 const struct switchdev_obj_port_mdb *mdb)
1680 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1681 struct net_device *orig_dev = mdb->obj.orig_dev;
1682 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1683 struct net_device *dev = mlxsw_sp_port->dev;
1684 struct mlxsw_sp_bridge_device *bridge_device;
1685 struct mlxsw_sp_bridge_port *bridge_port;
1686 struct mlxsw_sp_mid *mid;
1690 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1694 bridge_device = bridge_port->bridge_device;
1695 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1698 if (!mlxsw_sp_port_vlan)
1701 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1703 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1705 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1708 netdev_err(dev, "Unable to allocate MC group\n");
1712 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1714 if (!bridge_device->multicast_enabled)
1717 if (bridge_port->mrouter)
1720 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1722 netdev_err(dev, "Unable to set SMID\n");
1729 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1734 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1735 struct mlxsw_sp_bridge_device
1738 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1739 struct mlxsw_sp_mid *mid;
1742 mc_enabled = bridge_device->multicast_enabled;
1744 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1746 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1749 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1754 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1755 struct mlxsw_sp_bridge_port *bridge_port,
1758 struct mlxsw_sp_bridge_device *bridge_device;
1759 struct mlxsw_sp_mid *mid;
1761 bridge_device = bridge_port->bridge_device;
1763 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1764 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1765 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1769 static int mlxsw_sp_port_obj_add(struct net_device *dev, const void *ctx,
1770 const struct switchdev_obj *obj,
1771 struct netlink_ext_ack *extack)
1773 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1774 const struct switchdev_obj_port_vlan *vlan;
1778 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1779 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1781 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, extack);
1783 /* The event is emitted before the changes are actually
1784 * applied to the bridge. Therefore schedule the respin
1785 * call for later, so that the respin logic sees the
1786 * updated bridge state.
1788 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1790 case SWITCHDEV_OBJ_ID_PORT_MDB:
1791 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1792 SWITCHDEV_OBJ_PORT_MDB(obj));
1803 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1804 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1806 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1807 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1810 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1811 if (WARN_ON(!mlxsw_sp_port_vlan))
1814 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1815 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1816 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1817 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1818 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1821 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1822 const struct switchdev_obj_port_vlan *vlan)
1824 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1825 struct net_device *orig_dev = vlan->obj.orig_dev;
1826 struct mlxsw_sp_bridge_port *bridge_port;
1828 if (netif_is_bridge_master(orig_dev))
1831 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1832 if (WARN_ON(!bridge_port))
1835 if (!bridge_port->bridge_device->vlan_enabled)
1838 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vlan->vid);
1844 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1845 struct mlxsw_sp_bridge_port *bridge_port,
1846 struct mlxsw_sp_mid *mid)
1848 struct net_device *dev = mlxsw_sp_port->dev;
1851 if (bridge_port->bridge_device->multicast_enabled &&
1852 !bridge_port->mrouter) {
1853 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1855 netdev_err(dev, "Unable to remove port from SMID\n");
1858 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1860 netdev_err(dev, "Unable to remove MC SFD\n");
1865 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1866 const struct switchdev_obj_port_mdb *mdb)
1868 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1869 struct net_device *orig_dev = mdb->obj.orig_dev;
1870 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1871 struct mlxsw_sp_bridge_device *bridge_device;
1872 struct net_device *dev = mlxsw_sp_port->dev;
1873 struct mlxsw_sp_bridge_port *bridge_port;
1874 struct mlxsw_sp_mid *mid;
1877 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1881 bridge_device = bridge_port->bridge_device;
1882 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1885 if (!mlxsw_sp_port_vlan)
1888 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1890 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1892 netdev_err(dev, "Unable to remove port from MC DB\n");
1896 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1900 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1901 struct mlxsw_sp_bridge_port *bridge_port)
1903 struct mlxsw_sp_bridge_device *bridge_device;
1904 struct mlxsw_sp_mid *mid, *tmp;
1906 bridge_device = bridge_port->bridge_device;
1908 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1909 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1910 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1912 } else if (bridge_device->multicast_enabled &&
1913 bridge_port->mrouter) {
1914 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1919 static int mlxsw_sp_port_obj_del(struct net_device *dev, const void *ctx,
1920 const struct switchdev_obj *obj)
1922 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1926 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1927 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1928 SWITCHDEV_OBJ_PORT_VLAN(obj));
1930 case SWITCHDEV_OBJ_ID_PORT_MDB:
1931 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1932 SWITCHDEV_OBJ_PORT_MDB(obj));
1939 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1944 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1947 struct mlxsw_sp_port *mlxsw_sp_port;
1948 u64 max_lag_members;
1951 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1953 for (i = 0; i < max_lag_members; i++) {
1954 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1956 return mlxsw_sp_port;
1962 mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port *bridge_port,
1963 struct mlxsw_sp_port *mlxsw_sp_port,
1964 struct netlink_ext_ack *extack)
1966 if (is_vlan_dev(bridge_port->dev)) {
1967 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1971 /* Port is no longer usable as a router interface */
1972 if (mlxsw_sp_port->default_vlan->fid)
1973 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
1979 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1980 struct mlxsw_sp_bridge_port *bridge_port,
1981 struct mlxsw_sp_port *mlxsw_sp_port,
1982 struct netlink_ext_ack *extack)
1984 return mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
1989 mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port *mlxsw_sp_port)
1991 /* Make sure untagged frames are allowed to ingress */
1992 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
1997 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1998 struct mlxsw_sp_bridge_port *bridge_port,
1999 struct mlxsw_sp_port *mlxsw_sp_port)
2001 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2005 mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2006 const struct net_device *vxlan_dev,
2007 u16 vid, u16 ethertype,
2008 struct netlink_ext_ack *extack)
2010 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2011 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2012 struct mlxsw_sp_nve_params params = {
2013 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2014 .vni = vxlan->cfg.vni,
2016 .ethertype = ethertype,
2018 struct mlxsw_sp_fid *fid;
2021 /* If the VLAN is 0, we need to find the VLAN that is configured as
2022 * PVID and egress untagged on the bridge port of the VxLAN device.
2023 * It is possible no such VLAN exists
2026 err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
2031 fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2033 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1Q FID");
2034 return PTR_ERR(fid);
2037 if (mlxsw_sp_fid_vni_is_set(fid)) {
2038 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2040 goto err_vni_exists;
2043 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2045 goto err_nve_fid_enable;
2051 mlxsw_sp_fid_put(fid);
2056 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2057 const struct net_device *vxlan_dev, u16 vid,
2058 struct netlink_ext_ack *extack)
2060 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2061 vid, ETH_P_8021Q, extack);
2064 static struct net_device *
2065 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2067 struct net_device *dev;
2068 struct list_head *iter;
2070 netdev_for_each_lower_dev(br_dev, dev, iter) {
2074 if (!netif_is_vxlan(dev))
2077 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2078 if (err || pvid != vid)
2087 static struct mlxsw_sp_fid *
2088 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2089 u16 vid, struct netlink_ext_ack *extack)
2091 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2093 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2096 static struct mlxsw_sp_fid *
2097 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2100 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2102 return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2106 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2107 const struct mlxsw_sp_fid *fid)
2109 return mlxsw_sp_fid_8021q_vid(fid);
2112 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2113 .port_join = mlxsw_sp_bridge_8021q_port_join,
2114 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2115 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2116 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2117 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2118 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2122 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2123 const struct net_device *br_dev)
2125 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2127 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2129 if (mlxsw_sp_port_vlan->bridge_port &&
2130 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2139 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2140 struct mlxsw_sp_bridge_port *bridge_port,
2141 struct mlxsw_sp_port *mlxsw_sp_port,
2142 struct netlink_ext_ack *extack)
2144 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2145 struct net_device *dev = bridge_port->dev;
2148 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2149 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2150 if (WARN_ON(!mlxsw_sp_port_vlan))
2153 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2154 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2158 /* Port is no longer usable as a router interface */
2159 if (mlxsw_sp_port_vlan->fid)
2160 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2162 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2167 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2168 struct mlxsw_sp_bridge_port *bridge_port,
2169 struct mlxsw_sp_port *mlxsw_sp_port)
2171 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2172 struct net_device *dev = bridge_port->dev;
2175 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2176 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2177 if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2180 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2184 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2185 const struct net_device *vxlan_dev, u16 vid,
2186 struct netlink_ext_ack *extack)
2188 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2189 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2190 struct mlxsw_sp_nve_params params = {
2191 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2192 .vni = vxlan->cfg.vni,
2194 .ethertype = ETH_P_8021Q,
2196 struct mlxsw_sp_fid *fid;
2199 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2201 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1D FID");
2205 if (mlxsw_sp_fid_vni_is_set(fid)) {
2206 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2208 goto err_vni_exists;
2211 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2213 goto err_nve_fid_enable;
2219 mlxsw_sp_fid_put(fid);
2223 static struct mlxsw_sp_fid *
2224 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2225 u16 vid, struct netlink_ext_ack *extack)
2227 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2229 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2232 static struct mlxsw_sp_fid *
2233 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2236 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2238 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2242 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2246 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2247 const struct mlxsw_sp_fid *fid)
2252 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2253 .port_join = mlxsw_sp_bridge_8021d_port_join,
2254 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2255 .vxlan_join = mlxsw_sp_bridge_8021d_vxlan_join,
2256 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2257 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2258 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2262 mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2263 struct mlxsw_sp_bridge_port *bridge_port,
2264 struct mlxsw_sp_port *mlxsw_sp_port,
2265 struct netlink_ext_ack *extack)
2269 err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, false);
2273 err = mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2276 goto err_bridge_vlan_aware_port_join;
2280 err_bridge_vlan_aware_port_join:
2281 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2286 mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2287 struct mlxsw_sp_bridge_port *bridge_port,
2288 struct mlxsw_sp_port *mlxsw_sp_port)
2290 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2291 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2295 mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2296 const struct net_device *vxlan_dev, u16 vid,
2297 struct netlink_ext_ack *extack)
2299 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2300 vid, ETH_P_8021AD, extack);
2303 static const struct mlxsw_sp_bridge_ops mlxsw_sp1_bridge_8021ad_ops = {
2304 .port_join = mlxsw_sp_bridge_8021ad_port_join,
2305 .port_leave = mlxsw_sp_bridge_8021ad_port_leave,
2306 .vxlan_join = mlxsw_sp_bridge_8021ad_vxlan_join,
2307 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2308 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2309 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2313 mlxsw_sp2_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2314 struct mlxsw_sp_bridge_port *bridge_port,
2315 struct mlxsw_sp_port *mlxsw_sp_port,
2316 struct netlink_ext_ack *extack)
2320 /* The EtherType of decapsulated packets is determined at the egress
2321 * port to allow 802.1d and 802.1ad bridges with VXLAN devices to
2324 err = mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021AD);
2328 err = mlxsw_sp_bridge_8021ad_port_join(bridge_device, bridge_port,
2329 mlxsw_sp_port, extack);
2331 goto err_bridge_8021ad_port_join;
2335 err_bridge_8021ad_port_join:
2336 mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2341 mlxsw_sp2_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2342 struct mlxsw_sp_bridge_port *bridge_port,
2343 struct mlxsw_sp_port *mlxsw_sp_port)
2345 mlxsw_sp_bridge_8021ad_port_leave(bridge_device, bridge_port,
2347 mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2350 static const struct mlxsw_sp_bridge_ops mlxsw_sp2_bridge_8021ad_ops = {
2351 .port_join = mlxsw_sp2_bridge_8021ad_port_join,
2352 .port_leave = mlxsw_sp2_bridge_8021ad_port_leave,
2353 .vxlan_join = mlxsw_sp_bridge_8021ad_vxlan_join,
2354 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2355 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2356 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2359 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2360 struct net_device *brport_dev,
2361 struct net_device *br_dev,
2362 struct netlink_ext_ack *extack)
2364 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2365 struct mlxsw_sp_bridge_device *bridge_device;
2366 struct mlxsw_sp_bridge_port *bridge_port;
2369 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev,
2371 if (IS_ERR(bridge_port))
2372 return PTR_ERR(bridge_port);
2373 bridge_device = bridge_port->bridge_device;
2375 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2376 mlxsw_sp_port, extack);
2383 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2387 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2388 struct net_device *brport_dev,
2389 struct net_device *br_dev)
2391 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2392 struct mlxsw_sp_bridge_device *bridge_device;
2393 struct mlxsw_sp_bridge_port *bridge_port;
2395 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2398 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2402 bridge_device->ops->port_leave(bridge_device, bridge_port,
2404 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2407 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2408 const struct net_device *br_dev,
2409 const struct net_device *vxlan_dev, u16 vid,
2410 struct netlink_ext_ack *extack)
2412 struct mlxsw_sp_bridge_device *bridge_device;
2414 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2415 if (WARN_ON(!bridge_device))
2418 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2422 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2423 const struct net_device *vxlan_dev)
2425 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2426 struct mlxsw_sp_fid *fid;
2428 /* If the VxLAN device is down, then the FID does not have a VNI */
2429 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2433 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2434 /* Drop both the reference we just took during lookup and the reference
2435 * the VXLAN device took.
2437 mlxsw_sp_fid_put(fid);
2438 mlxsw_sp_fid_put(fid);
2442 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2443 enum mlxsw_sp_l3proto *proto,
2444 union mlxsw_sp_l3addr *addr)
2446 if (vxlan_addr->sa.sa_family == AF_INET) {
2447 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2448 *proto = MLXSW_SP_L3_PROTO_IPV4;
2450 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2451 *proto = MLXSW_SP_L3_PROTO_IPV6;
2456 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2457 const union mlxsw_sp_l3addr *addr,
2458 union vxlan_addr *vxlan_addr)
2461 case MLXSW_SP_L3_PROTO_IPV4:
2462 vxlan_addr->sa.sa_family = AF_INET;
2463 vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2465 case MLXSW_SP_L3_PROTO_IPV6:
2466 vxlan_addr->sa.sa_family = AF_INET6;
2467 vxlan_addr->sin6.sin6_addr = addr->addr6;
2472 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2474 enum mlxsw_sp_l3proto proto,
2475 union mlxsw_sp_l3addr *addr,
2476 __be32 vni, bool adding)
2478 struct switchdev_notifier_vxlan_fdb_info info;
2479 struct vxlan_dev *vxlan = netdev_priv(dev);
2480 enum switchdev_notifier_type type;
2482 type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2483 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2484 mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2485 info.remote_port = vxlan->cfg.dst_port;
2486 info.remote_vni = vni;
2487 info.remote_ifindex = 0;
2488 ether_addr_copy(info.eth_addr, mac);
2490 info.offloaded = adding;
2491 call_switchdev_notifiers(type, dev, &info.info, NULL);
2494 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2496 enum mlxsw_sp_l3proto proto,
2497 union mlxsw_sp_l3addr *addr,
2501 if (netif_is_vxlan(dev))
2502 mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2507 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2508 const char *mac, u16 vid,
2509 struct net_device *dev, bool offloaded)
2511 struct switchdev_notifier_fdb_info info;
2515 info.offloaded = offloaded;
2516 call_switchdev_notifiers(type, dev, &info.info, NULL);
2519 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2520 char *sfn_pl, int rec_index,
2523 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2524 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2525 struct mlxsw_sp_bridge_device *bridge_device;
2526 struct mlxsw_sp_bridge_port *bridge_port;
2527 struct mlxsw_sp_port *mlxsw_sp_port;
2528 enum switchdev_notifier_type type;
2532 bool do_notification = true;
2535 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2537 if (WARN_ON_ONCE(local_port >= max_ports))
2539 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2540 if (!mlxsw_sp_port) {
2541 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2545 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2548 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2549 if (!mlxsw_sp_port_vlan) {
2550 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2554 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2556 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2560 bridge_device = bridge_port->bridge_device;
2561 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2564 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2567 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2571 if (!do_notification)
2573 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2574 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2580 do_notification = false;
2584 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2585 char *sfn_pl, int rec_index,
2588 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2589 struct mlxsw_sp_bridge_device *bridge_device;
2590 struct mlxsw_sp_bridge_port *bridge_port;
2591 struct mlxsw_sp_port *mlxsw_sp_port;
2592 enum switchdev_notifier_type type;
2597 bool do_notification = true;
2600 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2601 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2602 if (!mlxsw_sp_port) {
2603 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2607 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2610 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2611 if (!mlxsw_sp_port_vlan) {
2612 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2616 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2618 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2622 bridge_device = bridge_port->bridge_device;
2623 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2624 lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2625 mlxsw_sp_port_vlan->vid : 0;
2628 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2631 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2635 if (!do_notification)
2637 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2638 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2644 do_notification = false;
2649 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2650 const struct mlxsw_sp_fid *fid,
2652 struct net_device **nve_dev,
2653 u16 *p_vid, __be32 *p_vni)
2655 struct mlxsw_sp_bridge_device *bridge_device;
2656 struct net_device *br_dev, *dev;
2660 err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2664 err = mlxsw_sp_fid_vni(fid, p_vni);
2668 dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
2673 if (!netif_running(dev))
2676 if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2679 if (adding && netif_is_vxlan(dev)) {
2680 struct vxlan_dev *vxlan = netdev_priv(dev);
2682 if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2686 br_dev = netdev_master_upper_dev_get(dev);
2690 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2694 *p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2699 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2704 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2705 enum switchdev_notifier_type type;
2706 struct net_device *nve_dev;
2707 union mlxsw_sp_l3addr addr;
2708 struct mlxsw_sp_fid *fid;
2715 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2718 fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2720 goto err_fid_lookup;
2722 err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2723 (enum mlxsw_sp_l3proto) sfn_proto,
2726 goto err_ip_resolve;
2728 err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2729 &nve_dev, &vid, &vni);
2731 goto err_fdb_process;
2733 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2734 (enum mlxsw_sp_l3proto) sfn_proto,
2735 &addr, adding, true);
2739 mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2740 (enum mlxsw_sp_l3proto) sfn_proto,
2741 &addr, vni, adding);
2743 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2744 SWITCHDEV_FDB_DEL_TO_BRIDGE;
2745 mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2747 mlxsw_sp_fid_put(fid);
2754 mlxsw_sp_fid_put(fid);
2756 /* Remove an FDB entry in case we cannot process it. Otherwise the
2757 * device will keep sending the same notification over and over again.
2759 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2760 (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2764 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2765 char *sfn_pl, int rec_index)
2767 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2768 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2769 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2772 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2773 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2776 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2777 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2780 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2781 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2784 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2785 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2788 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2789 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2795 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp,
2798 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2799 unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval;
2801 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2802 msecs_to_jiffies(interval));
2805 #define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10
2807 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2809 struct mlxsw_sp_bridge *bridge;
2810 struct mlxsw_sp *mlxsw_sp;
2817 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2821 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2822 mlxsw_sp = bridge->mlxsw_sp;
2825 queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION;
2826 while (queries > 0) {
2827 mlxsw_reg_sfn_pack(sfn_pl);
2828 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2830 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2833 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2834 for (i = 0; i < num_rec; i++)
2835 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2836 if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT)
2844 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries);
2847 struct mlxsw_sp_switchdev_event_work {
2848 struct work_struct work;
2850 struct switchdev_notifier_fdb_info fdb_info;
2851 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2853 struct net_device *dev;
2854 unsigned long event;
2858 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2859 struct mlxsw_sp_switchdev_event_work *
2861 struct mlxsw_sp_fid *fid, __be32 vni)
2863 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2864 struct switchdev_notifier_fdb_info *fdb_info;
2865 struct net_device *dev = switchdev_work->dev;
2866 enum mlxsw_sp_l3proto proto;
2867 union mlxsw_sp_l3addr addr;
2870 fdb_info = &switchdev_work->fdb_info;
2871 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2875 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2878 switch (switchdev_work->event) {
2879 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2880 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2881 vxlan_fdb_info.eth_addr,
2882 mlxsw_sp_fid_index(fid),
2883 proto, &addr, true, false);
2886 vxlan_fdb_info.offloaded = true;
2887 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2888 &vxlan_fdb_info.info, NULL);
2889 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2890 vxlan_fdb_info.eth_addr,
2891 fdb_info->vid, dev, true);
2893 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2894 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2895 vxlan_fdb_info.eth_addr,
2896 mlxsw_sp_fid_index(fid),
2897 proto, &addr, false,
2899 vxlan_fdb_info.offloaded = false;
2900 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2901 &vxlan_fdb_info.info, NULL);
2907 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2910 struct mlxsw_sp_bridge_device *bridge_device;
2911 struct net_device *dev = switchdev_work->dev;
2912 struct net_device *br_dev;
2913 struct mlxsw_sp *mlxsw_sp;
2914 struct mlxsw_sp_fid *fid;
2918 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2919 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2922 if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
2923 (!switchdev_work->fdb_info.added_by_user ||
2924 switchdev_work->fdb_info.is_local))
2927 if (!netif_running(dev))
2929 br_dev = netdev_master_upper_dev_get(dev);
2932 if (!netif_is_bridge_master(br_dev))
2934 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2937 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2941 fid = bridge_device->ops->fid_lookup(bridge_device,
2942 switchdev_work->fdb_info.vid);
2946 err = mlxsw_sp_fid_vni(fid, &vni);
2950 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2954 mlxsw_sp_fid_put(fid);
2957 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2959 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2960 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2961 struct net_device *dev = switchdev_work->dev;
2962 struct switchdev_notifier_fdb_info *fdb_info;
2963 struct mlxsw_sp_port *mlxsw_sp_port;
2967 if (netif_is_vxlan(dev)) {
2968 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2972 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2976 switch (switchdev_work->event) {
2977 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2978 fdb_info = &switchdev_work->fdb_info;
2979 if (!fdb_info->added_by_user || fdb_info->is_local)
2981 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2984 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2986 fdb_info->vid, dev, true);
2988 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2989 fdb_info = &switchdev_work->fdb_info;
2990 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2992 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
2993 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2994 /* These events are only used to potentially update an existing
3000 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
3004 kfree(switchdev_work->fdb_info.addr);
3005 kfree(switchdev_work);
3010 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
3011 struct mlxsw_sp_switchdev_event_work *
3014 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3015 struct mlxsw_sp_bridge_device *bridge_device;
3016 struct net_device *dev = switchdev_work->dev;
3017 u8 all_zeros_mac[ETH_ALEN] = { 0 };
3018 enum mlxsw_sp_l3proto proto;
3019 union mlxsw_sp_l3addr addr;
3020 struct net_device *br_dev;
3021 struct mlxsw_sp_fid *fid;
3025 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3026 br_dev = netdev_master_upper_dev_get(dev);
3028 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3032 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3036 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3039 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3040 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
3042 mlxsw_sp_fid_put(fid);
3045 vxlan_fdb_info->offloaded = true;
3046 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3047 &vxlan_fdb_info->info, NULL);
3048 mlxsw_sp_fid_put(fid);
3052 /* The device has a single FDB table, whereas Linux has two - one
3053 * in the bridge driver and another in the VxLAN driver. We only
3054 * program an entry to the device if the MAC points to the VxLAN
3055 * device in the bridge's FDB table
3057 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3058 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
3059 goto err_br_fdb_find;
3061 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3062 mlxsw_sp_fid_index(fid), proto,
3063 &addr, true, false);
3065 goto err_fdb_tunnel_uc_op;
3066 vxlan_fdb_info->offloaded = true;
3067 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3068 &vxlan_fdb_info->info, NULL);
3069 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3070 vxlan_fdb_info->eth_addr, vid, dev, true);
3072 mlxsw_sp_fid_put(fid);
3076 err_fdb_tunnel_uc_op:
3078 mlxsw_sp_fid_put(fid);
3082 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
3083 struct mlxsw_sp_switchdev_event_work *
3086 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3087 struct mlxsw_sp_bridge_device *bridge_device;
3088 struct net_device *dev = switchdev_work->dev;
3089 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3090 u8 all_zeros_mac[ETH_ALEN] = { 0 };
3091 enum mlxsw_sp_l3proto proto;
3092 union mlxsw_sp_l3addr addr;
3093 struct mlxsw_sp_fid *fid;
3096 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3098 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3102 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3106 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3109 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3110 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3111 mlxsw_sp_fid_put(fid);
3115 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3116 mlxsw_sp_fid_index(fid), proto, &addr,
3118 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3119 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3120 vxlan_fdb_info->eth_addr, vid, dev, false);
3122 mlxsw_sp_fid_put(fid);
3125 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3127 struct mlxsw_sp_switchdev_event_work *switchdev_work =
3128 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3129 struct net_device *dev = switchdev_work->dev;
3130 struct mlxsw_sp *mlxsw_sp;
3131 struct net_device *br_dev;
3135 if (!netif_running(dev))
3137 br_dev = netdev_master_upper_dev_get(dev);
3140 if (!netif_is_bridge_master(br_dev))
3142 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3146 switch (switchdev_work->event) {
3147 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3148 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3150 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3151 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3157 kfree(switchdev_work);
3162 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3164 struct switchdev_notifier_info *info)
3166 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3167 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3168 struct vxlan_config *cfg = &vxlan->cfg;
3169 struct netlink_ext_ack *extack;
3171 extack = switchdev_notifier_info_to_extack(info);
3172 vxlan_fdb_info = container_of(info,
3173 struct switchdev_notifier_vxlan_fdb_info,
3176 if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3177 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3180 if (vxlan_fdb_info->remote_vni != cfg->vni ||
3181 vxlan_fdb_info->vni != cfg->vni) {
3182 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3185 if (vxlan_fdb_info->remote_ifindex) {
3186 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3189 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3190 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3193 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3194 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3198 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3203 /* Called under rcu_read_lock() */
3204 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3205 unsigned long event, void *ptr)
3207 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3208 struct mlxsw_sp_switchdev_event_work *switchdev_work;
3209 struct switchdev_notifier_fdb_info *fdb_info;
3210 struct switchdev_notifier_info *info = ptr;
3211 struct net_device *br_dev;
3214 if (event == SWITCHDEV_PORT_ATTR_SET) {
3215 err = switchdev_handle_port_attr_set(dev, ptr,
3216 mlxsw_sp_port_dev_check,
3217 mlxsw_sp_port_attr_set);
3218 return notifier_from_errno(err);
3221 /* Tunnel devices are not our uppers, so check their master instead */
3222 br_dev = netdev_master_upper_dev_get_rcu(dev);
3225 if (!netif_is_bridge_master(br_dev))
3227 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3230 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3231 if (!switchdev_work)
3234 switchdev_work->dev = dev;
3235 switchdev_work->event = event;
3238 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3239 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3240 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3241 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3242 fdb_info = container_of(info,
3243 struct switchdev_notifier_fdb_info,
3245 INIT_WORK(&switchdev_work->work,
3246 mlxsw_sp_switchdev_bridge_fdb_event_work);
3247 memcpy(&switchdev_work->fdb_info, ptr,
3248 sizeof(switchdev_work->fdb_info));
3249 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3250 if (!switchdev_work->fdb_info.addr)
3251 goto err_addr_alloc;
3252 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3254 /* Take a reference on the device. This can be either
3255 * upper device containig mlxsw_sp_port or just a
3260 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3261 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3262 INIT_WORK(&switchdev_work->work,
3263 mlxsw_sp_switchdev_vxlan_fdb_event_work);
3264 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3267 goto err_vxlan_work_prepare;
3271 kfree(switchdev_work);
3275 mlxsw_core_schedule_work(&switchdev_work->work);
3279 err_vxlan_work_prepare:
3281 kfree(switchdev_work);
3285 struct notifier_block mlxsw_sp_switchdev_notifier = {
3286 .notifier_call = mlxsw_sp_switchdev_event,
3290 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3291 struct mlxsw_sp_bridge_device *bridge_device,
3292 const struct net_device *vxlan_dev, u16 vid,
3293 bool flag_untagged, bool flag_pvid,
3294 struct netlink_ext_ack *extack)
3296 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3297 __be32 vni = vxlan->cfg.vni;
3298 struct mlxsw_sp_fid *fid;
3302 /* We cannot have the same VLAN as PVID and egress untagged on multiple
3303 * VxLAN devices. Note that we get this notification before the VLAN is
3304 * actually added to the bridge's database, so it is not possible for
3305 * the lookup function to return 'vxlan_dev'
3307 if (flag_untagged && flag_pvid &&
3308 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3309 NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3313 if (!netif_running(vxlan_dev))
3316 /* First case: FID is not associated with this VNI, but the new VLAN
3317 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3320 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3322 if (!flag_untagged || !flag_pvid)
3324 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev,
3328 /* Second case: FID is associated with the VNI and the VLAN associated
3329 * with the FID is the same as the notified VLAN. This means the flags
3330 * (PVID / egress untagged) were toggled and that NVE should be
3331 * disabled on the FID
3333 old_vid = mlxsw_sp_fid_8021q_vid(fid);
3334 if (vid == old_vid) {
3335 if (WARN_ON(flag_untagged && flag_pvid)) {
3336 mlxsw_sp_fid_put(fid);
3339 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3340 mlxsw_sp_fid_put(fid);
3344 /* Third case: A new VLAN was configured on the VxLAN device, but this
3345 * VLAN is not PVID, so there is nothing to do.
3348 mlxsw_sp_fid_put(fid);
3352 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3353 * mapped to the VNI should be unmapped
3355 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3356 mlxsw_sp_fid_put(fid);
3358 /* Fifth case: The new VLAN is also egress untagged, which means the
3359 * VLAN needs to be mapped to the VNI
3364 err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack);
3366 goto err_vxlan_join;
3371 bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL);
3376 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3377 struct mlxsw_sp_bridge_device *bridge_device,
3378 const struct net_device *vxlan_dev, u16 vid)
3380 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3381 __be32 vni = vxlan->cfg.vni;
3382 struct mlxsw_sp_fid *fid;
3384 if (!netif_running(vxlan_dev))
3387 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3391 /* A different VLAN than the one mapped to the VNI is deleted */
3392 if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3395 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3398 mlxsw_sp_fid_put(fid);
3402 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3403 struct switchdev_notifier_port_obj_info *
3406 struct switchdev_obj_port_vlan *vlan =
3407 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3408 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3409 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3410 struct mlxsw_sp_bridge_device *bridge_device;
3411 struct netlink_ext_ack *extack;
3412 struct mlxsw_sp *mlxsw_sp;
3413 struct net_device *br_dev;
3415 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3416 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3420 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3424 port_obj_info->handled = true;
3426 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3430 if (!bridge_device->vlan_enabled)
3433 return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3434 vxlan_dev, vlan->vid,
3440 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3441 struct switchdev_notifier_port_obj_info *
3444 struct switchdev_obj_port_vlan *vlan =
3445 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3446 struct mlxsw_sp_bridge_device *bridge_device;
3447 struct mlxsw_sp *mlxsw_sp;
3448 struct net_device *br_dev;
3450 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3454 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3458 port_obj_info->handled = true;
3460 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3464 if (!bridge_device->vlan_enabled)
3467 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev,
3472 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3473 struct switchdev_notifier_port_obj_info *
3478 switch (port_obj_info->obj->id) {
3479 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3480 err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3491 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3492 struct switchdev_notifier_port_obj_info *
3495 switch (port_obj_info->obj->id) {
3496 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3497 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3504 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3505 unsigned long event, void *ptr)
3507 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3511 case SWITCHDEV_PORT_OBJ_ADD:
3512 if (netif_is_vxlan(dev))
3513 err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3515 err = switchdev_handle_port_obj_add(dev, ptr,
3516 mlxsw_sp_port_dev_check,
3517 mlxsw_sp_port_obj_add);
3518 return notifier_from_errno(err);
3519 case SWITCHDEV_PORT_OBJ_DEL:
3520 if (netif_is_vxlan(dev))
3521 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3523 err = switchdev_handle_port_obj_del(dev, ptr,
3524 mlxsw_sp_port_dev_check,
3525 mlxsw_sp_port_obj_del);
3526 return notifier_from_errno(err);
3527 case SWITCHDEV_PORT_ATTR_SET:
3528 err = switchdev_handle_port_attr_set(dev, ptr,
3529 mlxsw_sp_port_dev_check,
3530 mlxsw_sp_port_attr_set);
3531 return notifier_from_errno(err);
3537 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3538 .notifier_call = mlxsw_sp_switchdev_blocking_event,
3542 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3544 return bridge_port->stp_state;
3547 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3549 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3550 struct notifier_block *nb;
3553 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3555 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3559 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3561 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3565 nb = &mlxsw_sp_switchdev_blocking_notifier;
3566 err = register_switchdev_blocking_notifier(nb);
3568 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3569 goto err_register_switchdev_blocking_notifier;
3572 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3573 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3574 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false);
3577 err_register_switchdev_blocking_notifier:
3578 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3582 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3584 struct notifier_block *nb;
3586 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3588 nb = &mlxsw_sp_switchdev_blocking_notifier;
3589 unregister_switchdev_blocking_notifier(nb);
3591 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3594 static void mlxsw_sp1_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3596 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp1_bridge_8021ad_ops;
3599 const struct mlxsw_sp_switchdev_ops mlxsw_sp1_switchdev_ops = {
3600 .init = mlxsw_sp1_switchdev_init,
3603 static void mlxsw_sp2_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3605 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp2_bridge_8021ad_ops;
3608 const struct mlxsw_sp_switchdev_ops mlxsw_sp2_switchdev_ops = {
3609 .init = mlxsw_sp2_switchdev_init,
3612 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3614 struct mlxsw_sp_bridge *bridge;
3616 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3619 mlxsw_sp->bridge = bridge;
3620 bridge->mlxsw_sp = mlxsw_sp;
3622 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3624 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3625 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3627 mlxsw_sp->switchdev_ops->init(mlxsw_sp);
3629 return mlxsw_sp_fdb_init(mlxsw_sp);
3632 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3634 mlxsw_sp_fdb_fini(mlxsw_sp);
3635 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3636 kfree(mlxsw_sp->bridge);