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;
46 struct mlxsw_sp_bridge_device {
47 struct net_device *dev;
48 struct list_head list;
49 struct list_head ports_list;
50 struct list_head mids_list;
54 const struct mlxsw_sp_bridge_ops *ops;
57 struct mlxsw_sp_bridge_port {
58 struct net_device *dev;
59 struct mlxsw_sp_bridge_device *bridge_device;
60 struct list_head list;
61 struct list_head vlans_list;
62 unsigned int ref_count;
73 struct mlxsw_sp_bridge_vlan {
74 struct list_head list;
75 struct list_head port_vlan_list;
79 struct mlxsw_sp_bridge_ops {
80 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
81 struct mlxsw_sp_bridge_port *bridge_port,
82 struct mlxsw_sp_port *mlxsw_sp_port,
83 struct netlink_ext_ack *extack);
84 void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
85 struct mlxsw_sp_bridge_port *bridge_port,
86 struct mlxsw_sp_port *mlxsw_sp_port);
87 int (*vxlan_join)(struct mlxsw_sp_bridge_device *bridge_device,
88 const struct net_device *vxlan_dev, u16 vid,
89 struct netlink_ext_ack *extack);
91 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
92 u16 vid, struct netlink_ext_ack *extack);
94 (*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
96 u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
97 const struct mlxsw_sp_fid *fid);
101 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
102 struct mlxsw_sp_bridge_port *bridge_port,
106 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
107 struct mlxsw_sp_bridge_port *bridge_port);
110 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
111 struct mlxsw_sp_bridge_device
115 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
116 struct mlxsw_sp_bridge_port *bridge_port,
119 static struct mlxsw_sp_bridge_device *
120 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
121 const struct net_device *br_dev)
123 struct mlxsw_sp_bridge_device *bridge_device;
125 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
126 if (bridge_device->dev == br_dev)
127 return bridge_device;
132 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
133 const struct net_device *br_dev)
135 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
138 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
141 struct mlxsw_sp *mlxsw_sp = data;
143 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
147 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
148 struct net_device *dev)
150 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
151 netdev_walk_all_upper_dev_rcu(dev,
152 mlxsw_sp_bridge_device_upper_rif_destroy,
156 static struct mlxsw_sp_bridge_device *
157 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
158 struct net_device *br_dev)
160 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
161 struct mlxsw_sp_bridge_device *bridge_device;
162 bool vlan_enabled = br_vlan_enabled(br_dev);
164 if (vlan_enabled && bridge->vlan_enabled_exists) {
165 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
166 return ERR_PTR(-EINVAL);
169 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
171 return ERR_PTR(-ENOMEM);
173 bridge_device->dev = br_dev;
174 bridge_device->vlan_enabled = vlan_enabled;
175 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
176 bridge_device->mrouter = br_multicast_router(br_dev);
177 INIT_LIST_HEAD(&bridge_device->ports_list);
179 bridge->vlan_enabled_exists = true;
180 bridge_device->ops = bridge->bridge_8021q_ops;
182 bridge_device->ops = bridge->bridge_8021d_ops;
184 INIT_LIST_HEAD(&bridge_device->mids_list);
185 list_add(&bridge_device->list, &bridge->bridges_list);
187 return bridge_device;
191 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
192 struct mlxsw_sp_bridge_device *bridge_device)
194 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
196 list_del(&bridge_device->list);
197 if (bridge_device->vlan_enabled)
198 bridge->vlan_enabled_exists = false;
199 WARN_ON(!list_empty(&bridge_device->ports_list));
200 WARN_ON(!list_empty(&bridge_device->mids_list));
201 kfree(bridge_device);
204 static struct mlxsw_sp_bridge_device *
205 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
206 struct net_device *br_dev)
208 struct mlxsw_sp_bridge_device *bridge_device;
210 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
212 return bridge_device;
214 return mlxsw_sp_bridge_device_create(bridge, br_dev);
218 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
219 struct mlxsw_sp_bridge_device *bridge_device)
221 if (list_empty(&bridge_device->ports_list))
222 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
225 static struct mlxsw_sp_bridge_port *
226 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
227 const struct net_device *brport_dev)
229 struct mlxsw_sp_bridge_port *bridge_port;
231 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
232 if (bridge_port->dev == brport_dev)
239 struct mlxsw_sp_bridge_port *
240 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
241 struct net_device *brport_dev)
243 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
244 struct mlxsw_sp_bridge_device *bridge_device;
249 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
253 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
256 static struct mlxsw_sp_bridge_port *
257 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
258 struct net_device *brport_dev)
260 struct mlxsw_sp_bridge_port *bridge_port;
261 struct mlxsw_sp_port *mlxsw_sp_port;
263 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
267 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
268 bridge_port->lagged = mlxsw_sp_port->lagged;
269 if (bridge_port->lagged)
270 bridge_port->lag_id = mlxsw_sp_port->lag_id;
272 bridge_port->system_port = mlxsw_sp_port->local_port;
273 bridge_port->dev = brport_dev;
274 bridge_port->bridge_device = bridge_device;
275 bridge_port->stp_state = BR_STATE_DISABLED;
276 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
278 INIT_LIST_HEAD(&bridge_port->vlans_list);
279 list_add(&bridge_port->list, &bridge_device->ports_list);
280 bridge_port->ref_count = 1;
286 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
288 list_del(&bridge_port->list);
289 WARN_ON(!list_empty(&bridge_port->vlans_list));
293 static struct mlxsw_sp_bridge_port *
294 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
295 struct net_device *brport_dev)
297 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
298 struct mlxsw_sp_bridge_device *bridge_device;
299 struct mlxsw_sp_bridge_port *bridge_port;
302 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
304 bridge_port->ref_count++;
308 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
309 if (IS_ERR(bridge_device))
310 return ERR_CAST(bridge_device);
312 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
315 goto err_bridge_port_create;
320 err_bridge_port_create:
321 mlxsw_sp_bridge_device_put(bridge, bridge_device);
325 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
326 struct mlxsw_sp_bridge_port *bridge_port)
328 struct mlxsw_sp_bridge_device *bridge_device;
330 if (--bridge_port->ref_count != 0)
332 bridge_device = bridge_port->bridge_device;
333 mlxsw_sp_bridge_port_destroy(bridge_port);
334 mlxsw_sp_bridge_device_put(bridge, bridge_device);
337 static struct mlxsw_sp_port_vlan *
338 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
339 const struct mlxsw_sp_bridge_device *
343 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
345 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
347 if (!mlxsw_sp_port_vlan->bridge_port)
349 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
352 if (bridge_device->vlan_enabled &&
353 mlxsw_sp_port_vlan->vid != vid)
355 return mlxsw_sp_port_vlan;
361 static struct mlxsw_sp_port_vlan*
362 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
365 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
367 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
369 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
371 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
372 return mlxsw_sp_port_vlan;
378 static struct mlxsw_sp_bridge_vlan *
379 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
382 struct mlxsw_sp_bridge_vlan *bridge_vlan;
384 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
385 if (bridge_vlan->vid == vid)
392 static struct mlxsw_sp_bridge_vlan *
393 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
395 struct mlxsw_sp_bridge_vlan *bridge_vlan;
397 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
401 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
402 bridge_vlan->vid = vid;
403 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
409 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
411 list_del(&bridge_vlan->list);
412 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
416 static struct mlxsw_sp_bridge_vlan *
417 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
419 struct mlxsw_sp_bridge_vlan *bridge_vlan;
421 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
425 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
428 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
430 if (list_empty(&bridge_vlan->port_vlan_list))
431 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
435 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
436 struct mlxsw_sp_bridge_vlan *bridge_vlan,
439 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
441 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
443 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
445 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
446 bridge_vlan->vid, state);
452 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
453 struct switchdev_trans *trans,
454 struct net_device *orig_dev,
457 struct mlxsw_sp_bridge_port *bridge_port;
458 struct mlxsw_sp_bridge_vlan *bridge_vlan;
461 if (switchdev_trans_ph_prepare(trans))
464 /* It's possible we failed to enslave the port, yet this
465 * operation is executed due to it being deferred.
467 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
472 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
473 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
476 goto err_port_bridge_vlan_stp_set;
479 bridge_port->stp_state = state;
483 err_port_bridge_vlan_stp_set:
484 list_for_each_entry_continue_reverse(bridge_vlan,
485 &bridge_port->vlans_list, list)
486 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
487 bridge_port->stp_state);
492 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
493 struct mlxsw_sp_bridge_vlan *bridge_vlan,
494 enum mlxsw_sp_flood_type packet_type,
497 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
499 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
501 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
503 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
505 mlxsw_sp_port->local_port,
513 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
514 struct mlxsw_sp_bridge_port *bridge_port,
515 enum mlxsw_sp_flood_type packet_type,
518 struct mlxsw_sp_bridge_vlan *bridge_vlan;
521 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
522 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
527 goto err_port_bridge_vlan_flood_set;
532 err_port_bridge_vlan_flood_set:
533 list_for_each_entry_continue_reverse(bridge_vlan,
534 &bridge_port->vlans_list, list)
535 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
536 packet_type, !member);
541 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
542 struct mlxsw_sp_bridge_vlan *bridge_vlan,
545 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
546 u16 vid = bridge_vlan->vid;
548 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
550 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
552 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
559 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
560 struct mlxsw_sp_bridge_port *bridge_port,
563 struct mlxsw_sp_bridge_vlan *bridge_vlan;
566 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
567 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
570 goto err_port_bridge_vlan_learning_set;
575 err_port_bridge_vlan_learning_set:
576 list_for_each_entry_continue_reverse(bridge_vlan,
577 &bridge_port->vlans_list, list)
578 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
583 static int mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port
585 struct switchdev_trans *trans,
586 unsigned long brport_flags)
588 if (brport_flags & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
594 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
595 struct switchdev_trans *trans,
596 struct net_device *orig_dev,
597 unsigned long brport_flags)
599 struct mlxsw_sp_bridge_port *bridge_port;
602 if (switchdev_trans_ph_prepare(trans))
605 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
610 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
611 MLXSW_SP_FLOOD_TYPE_UC,
612 brport_flags & BR_FLOOD);
616 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
617 brport_flags & BR_LEARNING);
621 if (bridge_port->bridge_device->multicast_enabled)
624 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
625 MLXSW_SP_FLOOD_TYPE_MC,
632 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
636 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
638 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
641 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
642 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
645 mlxsw_sp->bridge->ageing_time = ageing_time;
649 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
650 struct switchdev_trans *trans,
651 unsigned long ageing_clock_t)
653 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
654 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
655 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
657 if (switchdev_trans_ph_prepare(trans)) {
658 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
659 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
665 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
668 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
669 struct switchdev_trans *trans,
670 struct net_device *orig_dev,
673 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
674 struct mlxsw_sp_bridge_device *bridge_device;
676 if (!switchdev_trans_ph_prepare(trans))
679 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
680 if (WARN_ON(!bridge_device))
683 if (bridge_device->vlan_enabled == vlan_enabled)
686 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
690 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
691 struct switchdev_trans *trans,
692 struct net_device *orig_dev,
693 bool is_port_mrouter)
695 struct mlxsw_sp_bridge_port *bridge_port;
698 if (switchdev_trans_ph_prepare(trans))
701 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
706 if (!bridge_port->bridge_device->multicast_enabled)
709 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
710 MLXSW_SP_FLOOD_TYPE_MC,
715 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
718 bridge_port->mrouter = is_port_mrouter;
722 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
724 const struct mlxsw_sp_bridge_device *bridge_device;
726 bridge_device = bridge_port->bridge_device;
727 return bridge_device->multicast_enabled ? bridge_port->mrouter :
728 bridge_port->flags & BR_MCAST_FLOOD;
731 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
732 struct switchdev_trans *trans,
733 struct net_device *orig_dev,
736 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
737 struct mlxsw_sp_bridge_device *bridge_device;
738 struct mlxsw_sp_bridge_port *bridge_port;
741 if (switchdev_trans_ph_prepare(trans))
744 /* It's possible we failed to enslave the port, yet this
745 * operation is executed due to it being deferred.
747 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
751 if (bridge_device->multicast_enabled != !mc_disabled) {
752 bridge_device->multicast_enabled = !mc_disabled;
753 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
757 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
758 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
759 bool member = mlxsw_sp_mc_flood(bridge_port);
761 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
763 packet_type, member);
768 bridge_device->multicast_enabled = !mc_disabled;
773 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
774 u16 mid_idx, bool add)
779 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
783 mlxsw_reg_smid_pack(smid_pl, mid_idx,
784 mlxsw_sp_router_port(mlxsw_sp), add);
785 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
791 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
792 struct mlxsw_sp_bridge_device *bridge_device,
795 struct mlxsw_sp_mid *mid;
797 list_for_each_entry(mid, &bridge_device->mids_list, list)
798 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
802 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
803 struct switchdev_trans *trans,
804 struct net_device *orig_dev,
807 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
808 struct mlxsw_sp_bridge_device *bridge_device;
810 if (switchdev_trans_ph_prepare(trans))
813 /* It's possible we failed to enslave the port, yet this
814 * operation is executed due to it being deferred.
816 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
820 if (bridge_device->mrouter != is_mrouter)
821 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
823 bridge_device->mrouter = is_mrouter;
827 static int mlxsw_sp_port_attr_set(struct net_device *dev,
828 const struct switchdev_attr *attr,
829 struct switchdev_trans *trans)
831 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
835 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
836 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
840 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
841 err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
843 attr->u.brport_flags);
845 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
846 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
848 attr->u.brport_flags);
850 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
851 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
852 attr->u.ageing_time);
854 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
855 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
857 attr->u.vlan_filtering);
859 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
860 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
864 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
865 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
867 attr->u.mc_disabled);
869 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
870 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
879 if (switchdev_trans_ph_commit(trans))
880 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
886 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
887 struct mlxsw_sp_bridge_port *bridge_port,
888 struct netlink_ext_ack *extack)
890 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
891 struct mlxsw_sp_bridge_device *bridge_device;
892 u8 local_port = mlxsw_sp_port->local_port;
893 u16 vid = mlxsw_sp_port_vlan->vid;
894 struct mlxsw_sp_fid *fid;
897 bridge_device = bridge_port->bridge_device;
898 fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
902 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
903 bridge_port->flags & BR_FLOOD);
905 goto err_fid_uc_flood_set;
907 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
908 mlxsw_sp_mc_flood(bridge_port));
910 goto err_fid_mc_flood_set;
912 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
915 goto err_fid_bc_flood_set;
917 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
919 goto err_fid_port_vid_map;
921 mlxsw_sp_port_vlan->fid = fid;
925 err_fid_port_vid_map:
926 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
927 err_fid_bc_flood_set:
928 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
929 err_fid_mc_flood_set:
930 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
931 err_fid_uc_flood_set:
932 mlxsw_sp_fid_put(fid);
937 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
939 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
940 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
941 u8 local_port = mlxsw_sp_port->local_port;
942 u16 vid = mlxsw_sp_port_vlan->vid;
944 mlxsw_sp_port_vlan->fid = NULL;
945 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
946 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
947 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
948 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
949 mlxsw_sp_fid_put(fid);
953 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
954 u16 vid, bool is_pvid)
958 else if (mlxsw_sp_port->pvid == vid)
959 return 0; /* Dis-allow untagged packets */
961 return mlxsw_sp_port->pvid;
965 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
966 struct mlxsw_sp_bridge_port *bridge_port,
967 struct netlink_ext_ack *extack)
969 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
970 struct mlxsw_sp_bridge_vlan *bridge_vlan;
971 u16 vid = mlxsw_sp_port_vlan->vid;
974 /* No need to continue if only VLAN flags were changed */
975 if (mlxsw_sp_port_vlan->bridge_port)
978 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
983 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
984 bridge_port->flags & BR_LEARNING);
986 goto err_port_vid_learning_set;
988 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
989 bridge_port->stp_state);
991 goto err_port_vid_stp_set;
993 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
996 goto err_bridge_vlan_get;
999 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1000 &bridge_vlan->port_vlan_list);
1002 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1004 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1008 err_bridge_vlan_get:
1009 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1010 err_port_vid_stp_set:
1011 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1012 err_port_vid_learning_set:
1013 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1018 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1020 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1021 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1022 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1023 struct mlxsw_sp_bridge_port *bridge_port;
1024 u16 vid = mlxsw_sp_port_vlan->vid;
1025 bool last_port, last_vlan;
1027 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1028 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1031 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1032 last_vlan = list_is_singular(&bridge_port->vlans_list);
1033 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1034 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1036 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1037 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1038 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1039 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1041 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1043 mlxsw_sp_fid_index(fid));
1045 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1047 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1049 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1050 mlxsw_sp_port_vlan->bridge_port = NULL;
1054 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1055 struct mlxsw_sp_bridge_port *bridge_port,
1056 u16 vid, bool is_untagged, bool is_pvid,
1057 struct netlink_ext_ack *extack)
1059 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1060 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1061 u16 old_pvid = mlxsw_sp_port->pvid;
1064 /* The only valid scenario in which a port-vlan already exists, is if
1065 * the VLAN flags were changed and the port-vlan is associated with the
1066 * correct bridge port
1068 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1069 if (mlxsw_sp_port_vlan &&
1070 mlxsw_sp_port_vlan->bridge_port != bridge_port)
1073 if (!mlxsw_sp_port_vlan) {
1074 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1076 if (IS_ERR(mlxsw_sp_port_vlan))
1077 return PTR_ERR(mlxsw_sp_port_vlan);
1080 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1083 goto err_port_vlan_set;
1085 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1087 goto err_port_pvid_set;
1089 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1092 goto err_port_vlan_bridge_join;
1096 err_port_vlan_bridge_join:
1097 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1099 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1101 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1106 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1107 const struct net_device *br_dev,
1108 const struct switchdev_obj_port_vlan *vlan)
1110 struct mlxsw_sp_rif *rif;
1111 struct mlxsw_sp_fid *fid;
1115 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev);
1118 fid = mlxsw_sp_rif_fid(rif);
1119 pvid = mlxsw_sp_fid_8021q_vid(fid);
1121 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1122 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1124 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1129 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1138 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1139 const struct switchdev_obj_port_vlan *vlan,
1140 struct switchdev_trans *trans,
1141 struct netlink_ext_ack *extack)
1143 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1144 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1145 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1146 struct net_device *orig_dev = vlan->obj.orig_dev;
1147 struct mlxsw_sp_bridge_port *bridge_port;
1150 if (netif_is_bridge_master(orig_dev)) {
1153 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1154 br_vlan_enabled(orig_dev) &&
1155 switchdev_trans_ph_prepare(trans))
1156 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1163 if (switchdev_trans_ph_commit(trans))
1166 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1167 if (WARN_ON(!bridge_port))
1170 if (!bridge_port->bridge_device->vlan_enabled)
1173 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1176 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1186 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1188 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1189 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1193 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1194 struct mlxsw_sp_bridge_port *bridge_port,
1197 bool lagged = bridge_port->lagged;
1198 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1201 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1202 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1203 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1204 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1206 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1209 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1211 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1212 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1215 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1217 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1218 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1221 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1222 const char *mac, u16 fid,
1223 enum mlxsw_sp_l3proto proto,
1224 const union mlxsw_sp_l3addr *addr,
1225 bool adding, bool dynamic)
1227 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1234 case MLXSW_SP_L3_PROTO_IPV4:
1235 uip = be32_to_cpu(addr->addr4);
1236 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1238 case MLXSW_SP_L3_PROTO_IPV6: /* fall through */
1244 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1248 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1249 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1250 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1251 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1253 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1254 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1258 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1266 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1267 const char *mac, u16 fid, bool adding,
1268 enum mlxsw_reg_sfd_rec_action action,
1269 enum mlxsw_reg_sfd_rec_policy policy)
1275 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1279 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1280 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1281 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1282 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1286 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1294 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1295 const char *mac, u16 fid, bool adding,
1298 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1299 MLXSW_REG_SFD_REC_ACTION_NOP,
1300 mlxsw_sp_sfd_rec_policy(dynamic));
1303 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1306 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1307 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1308 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1311 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1312 const char *mac, u16 fid, u16 lag_vid,
1313 bool adding, bool dynamic)
1319 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1323 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1324 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1325 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1327 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1328 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1332 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1341 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1342 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1344 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1345 struct net_device *orig_dev = fdb_info->info.dev;
1346 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1347 struct mlxsw_sp_bridge_device *bridge_device;
1348 struct mlxsw_sp_bridge_port *bridge_port;
1351 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1355 bridge_device = bridge_port->bridge_device;
1356 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1359 if (!mlxsw_sp_port_vlan)
1362 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1363 vid = mlxsw_sp_port_vlan->vid;
1365 if (!bridge_port->lagged)
1366 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1367 bridge_port->system_port,
1368 fdb_info->addr, fid_index,
1371 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1372 bridge_port->lag_id,
1373 fdb_info->addr, fid_index,
1374 vid, adding, false);
1377 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1378 u16 fid, u16 mid_idx, bool adding)
1384 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1388 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1389 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1390 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1391 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1392 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1396 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1404 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1406 bool set_router_port)
1411 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1415 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1416 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1417 if (mlxsw_sp->ports[i])
1418 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1421 mlxsw_reg_smid_port_mask_set(smid_pl,
1422 mlxsw_sp_router_port(mlxsw_sp), 1);
1424 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1425 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1427 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1430 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1435 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1436 u16 mid_idx, bool add)
1438 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1442 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1446 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1447 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1453 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1454 const unsigned char *addr,
1457 struct mlxsw_sp_mid *mid;
1459 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1460 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1467 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1468 struct mlxsw_sp_bridge_port *bridge_port,
1469 unsigned long *ports_bitmap)
1471 struct mlxsw_sp_port *mlxsw_sp_port;
1472 u64 max_lag_members, i;
1475 if (!bridge_port->lagged) {
1476 set_bit(bridge_port->system_port, ports_bitmap);
1478 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1480 lag_id = bridge_port->lag_id;
1481 for (i = 0; i < max_lag_members; i++) {
1482 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1485 set_bit(mlxsw_sp_port->local_port,
1492 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1493 struct mlxsw_sp_bridge_device *bridge_device,
1494 struct mlxsw_sp *mlxsw_sp)
1496 struct mlxsw_sp_bridge_port *bridge_port;
1498 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1499 if (bridge_port->mrouter) {
1500 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1508 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1509 struct mlxsw_sp_mid *mid,
1510 struct mlxsw_sp_bridge_device *bridge_device)
1518 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1520 if (mid_idx == MLXSW_SP_MID_MAX)
1523 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1524 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1525 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1529 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1530 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1533 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1534 bridge_device->mrouter);
1535 kfree(flood_bitmap);
1539 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1544 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1549 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1550 struct mlxsw_sp_mid *mid)
1555 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1557 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1562 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1563 struct mlxsw_sp_bridge_device *bridge_device,
1564 const unsigned char *addr,
1567 struct mlxsw_sp_mid *mid;
1570 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1574 alloc_size = sizeof(unsigned long) *
1575 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1577 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1578 if (!mid->ports_in_mid)
1579 goto err_ports_in_mid_alloc;
1581 ether_addr_copy(mid->addr, addr);
1585 if (!bridge_device->multicast_enabled)
1588 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1589 goto err_write_mdb_entry;
1592 list_add_tail(&mid->list, &bridge_device->mids_list);
1595 err_write_mdb_entry:
1596 kfree(mid->ports_in_mid);
1597 err_ports_in_mid_alloc:
1602 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1603 struct mlxsw_sp_mid *mid)
1605 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1608 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1609 if (bitmap_empty(mid->ports_in_mid,
1610 mlxsw_core_max_ports(mlxsw_sp->core))) {
1611 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1612 list_del(&mid->list);
1613 kfree(mid->ports_in_mid);
1619 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1620 const struct switchdev_obj_port_mdb *mdb,
1621 struct switchdev_trans *trans)
1623 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1624 struct net_device *orig_dev = mdb->obj.orig_dev;
1625 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1626 struct net_device *dev = mlxsw_sp_port->dev;
1627 struct mlxsw_sp_bridge_device *bridge_device;
1628 struct mlxsw_sp_bridge_port *bridge_port;
1629 struct mlxsw_sp_mid *mid;
1633 if (switchdev_trans_ph_commit(trans))
1636 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1640 bridge_device = bridge_port->bridge_device;
1641 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1644 if (!mlxsw_sp_port_vlan)
1647 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1649 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1651 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1654 netdev_err(dev, "Unable to allocate MC group\n");
1658 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1660 if (!bridge_device->multicast_enabled)
1663 if (bridge_port->mrouter)
1666 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1668 netdev_err(dev, "Unable to set SMID\n");
1675 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1680 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1681 struct mlxsw_sp_bridge_device
1684 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1685 struct mlxsw_sp_mid *mid;
1688 mc_enabled = bridge_device->multicast_enabled;
1690 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1692 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1695 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1700 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1701 struct mlxsw_sp_bridge_port *bridge_port,
1704 struct mlxsw_sp_bridge_device *bridge_device;
1705 struct mlxsw_sp_mid *mid;
1707 bridge_device = bridge_port->bridge_device;
1709 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1710 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1711 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1715 struct mlxsw_sp_span_respin_work {
1716 struct work_struct work;
1717 struct mlxsw_sp *mlxsw_sp;
1720 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1722 struct mlxsw_sp_span_respin_work *respin_work =
1723 container_of(work, struct mlxsw_sp_span_respin_work, work);
1726 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1731 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1733 struct mlxsw_sp_span_respin_work *respin_work;
1735 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1739 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1740 respin_work->mlxsw_sp = mlxsw_sp;
1742 mlxsw_core_schedule_work(&respin_work->work);
1745 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1746 const struct switchdev_obj *obj,
1747 struct switchdev_trans *trans,
1748 struct netlink_ext_ack *extack)
1750 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1751 const struct switchdev_obj_port_vlan *vlan;
1755 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1756 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1757 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans,
1760 if (switchdev_trans_ph_prepare(trans)) {
1761 /* The event is emitted before the changes are actually
1762 * applied to the bridge. Therefore schedule the respin
1763 * call for later, so that the respin logic sees the
1764 * updated bridge state.
1766 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1769 case SWITCHDEV_OBJ_ID_PORT_MDB:
1770 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1771 SWITCHDEV_OBJ_PORT_MDB(obj),
1783 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1784 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1786 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1787 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1789 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1790 if (WARN_ON(!mlxsw_sp_port_vlan))
1793 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1794 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1795 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1796 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1799 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1800 const struct switchdev_obj_port_vlan *vlan)
1802 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1803 struct net_device *orig_dev = vlan->obj.orig_dev;
1804 struct mlxsw_sp_bridge_port *bridge_port;
1807 if (netif_is_bridge_master(orig_dev))
1810 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1811 if (WARN_ON(!bridge_port))
1814 if (!bridge_port->bridge_device->vlan_enabled)
1817 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1818 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1824 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1825 struct mlxsw_sp_bridge_port *bridge_port,
1826 struct mlxsw_sp_mid *mid)
1828 struct net_device *dev = mlxsw_sp_port->dev;
1831 if (bridge_port->bridge_device->multicast_enabled &&
1832 !bridge_port->mrouter) {
1833 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1835 netdev_err(dev, "Unable to remove port from SMID\n");
1838 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1840 netdev_err(dev, "Unable to remove MC SFD\n");
1845 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1846 const struct switchdev_obj_port_mdb *mdb)
1848 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1849 struct net_device *orig_dev = mdb->obj.orig_dev;
1850 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1851 struct mlxsw_sp_bridge_device *bridge_device;
1852 struct net_device *dev = mlxsw_sp_port->dev;
1853 struct mlxsw_sp_bridge_port *bridge_port;
1854 struct mlxsw_sp_mid *mid;
1857 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1861 bridge_device = bridge_port->bridge_device;
1862 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1865 if (!mlxsw_sp_port_vlan)
1868 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1870 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1872 netdev_err(dev, "Unable to remove port from MC DB\n");
1876 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1880 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1881 struct mlxsw_sp_bridge_port *bridge_port)
1883 struct mlxsw_sp_bridge_device *bridge_device;
1884 struct mlxsw_sp_mid *mid, *tmp;
1886 bridge_device = bridge_port->bridge_device;
1888 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1889 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1890 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1892 } else if (bridge_device->multicast_enabled &&
1893 bridge_port->mrouter) {
1894 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1899 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1900 const struct switchdev_obj *obj)
1902 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1906 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1907 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1908 SWITCHDEV_OBJ_PORT_VLAN(obj));
1910 case SWITCHDEV_OBJ_ID_PORT_MDB:
1911 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1912 SWITCHDEV_OBJ_PORT_MDB(obj));
1919 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1924 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1927 struct mlxsw_sp_port *mlxsw_sp_port;
1928 u64 max_lag_members;
1931 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1933 for (i = 0; i < max_lag_members; i++) {
1934 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1936 return mlxsw_sp_port;
1942 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1943 struct mlxsw_sp_bridge_port *bridge_port,
1944 struct mlxsw_sp_port *mlxsw_sp_port,
1945 struct netlink_ext_ack *extack)
1947 if (is_vlan_dev(bridge_port->dev)) {
1948 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1952 /* Port is no longer usable as a router interface */
1953 if (mlxsw_sp_port->default_vlan->fid)
1954 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
1960 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1961 struct mlxsw_sp_bridge_port *bridge_port,
1962 struct mlxsw_sp_port *mlxsw_sp_port)
1964 /* Make sure untagged frames are allowed to ingress */
1965 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID);
1969 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
1970 const struct net_device *vxlan_dev, u16 vid,
1971 struct netlink_ext_ack *extack)
1973 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1974 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
1975 struct mlxsw_sp_nve_params params = {
1976 .type = MLXSW_SP_NVE_TYPE_VXLAN,
1977 .vni = vxlan->cfg.vni,
1980 struct mlxsw_sp_fid *fid;
1983 /* If the VLAN is 0, we need to find the VLAN that is configured as
1984 * PVID and egress untagged on the bridge port of the VxLAN device.
1985 * It is possible no such VLAN exists
1988 err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
1993 /* If no other port is member in the VLAN, then the FID does not exist.
1994 * NVE will be enabled on the FID once a port joins the VLAN
1996 fid = mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2000 if (mlxsw_sp_fid_vni_is_set(fid)) {
2001 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2003 goto err_vni_exists;
2006 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2008 goto err_nve_fid_enable;
2010 /* The tunnel port does not hold a reference on the FID. Only
2011 * local ports and the router port
2013 mlxsw_sp_fid_put(fid);
2019 mlxsw_sp_fid_put(fid);
2023 static struct net_device *
2024 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2026 struct net_device *dev;
2027 struct list_head *iter;
2029 netdev_for_each_lower_dev(br_dev, dev, iter) {
2033 if (!netif_is_vxlan(dev))
2036 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2037 if (err || pvid != vid)
2046 static struct mlxsw_sp_fid *
2047 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2048 u16 vid, struct netlink_ext_ack *extack)
2050 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2051 struct net_device *vxlan_dev;
2052 struct mlxsw_sp_fid *fid;
2055 fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2059 if (mlxsw_sp_fid_vni_is_set(fid))
2062 /* Find the VxLAN device that has the specified VLAN configured as
2063 * PVID and egress untagged. There can be at most one such device
2065 vxlan_dev = mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev,
2070 if (!netif_running(vxlan_dev))
2073 err = mlxsw_sp_bridge_8021q_vxlan_join(bridge_device, vxlan_dev, vid,
2076 goto err_vxlan_join;
2081 mlxsw_sp_fid_put(fid);
2082 return ERR_PTR(err);
2085 static struct mlxsw_sp_fid *
2086 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2089 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2091 return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2095 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2096 const struct mlxsw_sp_fid *fid)
2098 return mlxsw_sp_fid_8021q_vid(fid);
2101 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2102 .port_join = mlxsw_sp_bridge_8021q_port_join,
2103 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2104 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2105 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2106 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2107 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2111 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2112 const struct net_device *br_dev)
2114 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2116 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2118 if (mlxsw_sp_port_vlan->bridge_port &&
2119 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2128 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2129 struct mlxsw_sp_bridge_port *bridge_port,
2130 struct mlxsw_sp_port *mlxsw_sp_port,
2131 struct netlink_ext_ack *extack)
2133 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2134 struct net_device *dev = bridge_port->dev;
2137 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2138 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2139 if (WARN_ON(!mlxsw_sp_port_vlan))
2142 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2143 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2147 /* Port is no longer usable as a router interface */
2148 if (mlxsw_sp_port_vlan->fid)
2149 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2151 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2156 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2157 struct mlxsw_sp_bridge_port *bridge_port,
2158 struct mlxsw_sp_port *mlxsw_sp_port)
2160 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2161 struct net_device *dev = bridge_port->dev;
2164 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2165 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2166 if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2169 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2173 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2174 const struct net_device *vxlan_dev, u16 vid,
2175 struct netlink_ext_ack *extack)
2177 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2178 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2179 struct mlxsw_sp_nve_params params = {
2180 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2181 .vni = vxlan->cfg.vni,
2184 struct mlxsw_sp_fid *fid;
2187 fid = mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2189 NL_SET_ERR_MSG_MOD(extack, "Did not find a corresponding FID");
2193 if (mlxsw_sp_fid_vni_is_set(fid)) {
2194 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2196 goto err_vni_exists;
2199 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2201 goto err_nve_fid_enable;
2203 /* The tunnel port does not hold a reference on the FID. Only
2204 * local ports and the router port
2206 mlxsw_sp_fid_put(fid);
2212 mlxsw_sp_fid_put(fid);
2216 static struct mlxsw_sp_fid *
2217 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2218 u16 vid, struct netlink_ext_ack *extack)
2220 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2221 struct net_device *vxlan_dev;
2222 struct mlxsw_sp_fid *fid;
2225 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2229 if (mlxsw_sp_fid_vni_is_set(fid))
2232 vxlan_dev = mlxsw_sp_bridge_vxlan_dev_find(bridge_device->dev);
2236 if (!netif_running(vxlan_dev))
2239 err = mlxsw_sp_bridge_8021d_vxlan_join(bridge_device, vxlan_dev, 0,
2242 goto err_vxlan_join;
2247 mlxsw_sp_fid_put(fid);
2248 return ERR_PTR(err);
2251 static struct mlxsw_sp_fid *
2252 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2255 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2257 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2261 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2265 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2266 const struct mlxsw_sp_fid *fid)
2271 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2272 .port_join = mlxsw_sp_bridge_8021d_port_join,
2273 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2274 .vxlan_join = mlxsw_sp_bridge_8021d_vxlan_join,
2275 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2276 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2277 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2280 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2281 struct net_device *brport_dev,
2282 struct net_device *br_dev,
2283 struct netlink_ext_ack *extack)
2285 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2286 struct mlxsw_sp_bridge_device *bridge_device;
2287 struct mlxsw_sp_bridge_port *bridge_port;
2290 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2291 if (IS_ERR(bridge_port))
2292 return PTR_ERR(bridge_port);
2293 bridge_device = bridge_port->bridge_device;
2295 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2296 mlxsw_sp_port, extack);
2303 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2307 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2308 struct net_device *brport_dev,
2309 struct net_device *br_dev)
2311 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2312 struct mlxsw_sp_bridge_device *bridge_device;
2313 struct mlxsw_sp_bridge_port *bridge_port;
2315 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2318 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2322 bridge_device->ops->port_leave(bridge_device, bridge_port,
2324 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2327 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2328 const struct net_device *br_dev,
2329 const struct net_device *vxlan_dev, u16 vid,
2330 struct netlink_ext_ack *extack)
2332 struct mlxsw_sp_bridge_device *bridge_device;
2334 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2335 if (WARN_ON(!bridge_device))
2338 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2342 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2343 const struct net_device *vxlan_dev)
2345 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2346 struct mlxsw_sp_fid *fid;
2348 /* If the VxLAN device is down, then the FID does not have a VNI */
2349 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2353 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2354 mlxsw_sp_fid_put(fid);
2357 struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
2358 const struct net_device *br_dev,
2360 struct netlink_ext_ack *extack)
2362 struct mlxsw_sp_bridge_device *bridge_device;
2364 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2365 if (WARN_ON(!bridge_device))
2366 return ERR_PTR(-EINVAL);
2368 return bridge_device->ops->fid_get(bridge_device, vid, extack);
2372 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2373 enum mlxsw_sp_l3proto *proto,
2374 union mlxsw_sp_l3addr *addr)
2376 if (vxlan_addr->sa.sa_family == AF_INET) {
2377 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2378 *proto = MLXSW_SP_L3_PROTO_IPV4;
2380 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2381 *proto = MLXSW_SP_L3_PROTO_IPV6;
2386 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2387 const union mlxsw_sp_l3addr *addr,
2388 union vxlan_addr *vxlan_addr)
2391 case MLXSW_SP_L3_PROTO_IPV4:
2392 vxlan_addr->sa.sa_family = AF_INET;
2393 vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2395 case MLXSW_SP_L3_PROTO_IPV6:
2396 vxlan_addr->sa.sa_family = AF_INET6;
2397 vxlan_addr->sin6.sin6_addr = addr->addr6;
2402 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2404 enum mlxsw_sp_l3proto proto,
2405 union mlxsw_sp_l3addr *addr,
2406 __be32 vni, bool adding)
2408 struct switchdev_notifier_vxlan_fdb_info info;
2409 struct vxlan_dev *vxlan = netdev_priv(dev);
2410 enum switchdev_notifier_type type;
2412 type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2413 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2414 mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2415 info.remote_port = vxlan->cfg.dst_port;
2416 info.remote_vni = vni;
2417 info.remote_ifindex = 0;
2418 ether_addr_copy(info.eth_addr, mac);
2420 info.offloaded = adding;
2421 call_switchdev_notifiers(type, dev, &info.info, NULL);
2424 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2426 enum mlxsw_sp_l3proto proto,
2427 union mlxsw_sp_l3addr *addr,
2431 if (netif_is_vxlan(dev))
2432 mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2437 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2438 const char *mac, u16 vid,
2439 struct net_device *dev, bool offloaded)
2441 struct switchdev_notifier_fdb_info info;
2445 info.offloaded = offloaded;
2446 call_switchdev_notifiers(type, dev, &info.info, NULL);
2449 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2450 char *sfn_pl, int rec_index,
2453 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2454 struct mlxsw_sp_bridge_device *bridge_device;
2455 struct mlxsw_sp_bridge_port *bridge_port;
2456 struct mlxsw_sp_port *mlxsw_sp_port;
2457 enum switchdev_notifier_type type;
2461 bool do_notification = true;
2464 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2465 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2466 if (!mlxsw_sp_port) {
2467 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2471 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2472 if (!mlxsw_sp_port_vlan) {
2473 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2477 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2479 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2483 bridge_device = bridge_port->bridge_device;
2484 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2487 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2490 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2494 if (!do_notification)
2496 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2497 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2503 do_notification = false;
2507 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2508 char *sfn_pl, int rec_index,
2511 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2512 struct mlxsw_sp_bridge_device *bridge_device;
2513 struct mlxsw_sp_bridge_port *bridge_port;
2514 struct mlxsw_sp_port *mlxsw_sp_port;
2515 enum switchdev_notifier_type type;
2520 bool do_notification = true;
2523 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2524 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2525 if (!mlxsw_sp_port) {
2526 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2530 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2531 if (!mlxsw_sp_port_vlan) {
2532 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2536 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2538 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2542 bridge_device = bridge_port->bridge_device;
2543 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2544 lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2545 mlxsw_sp_port_vlan->vid : 0;
2548 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2551 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2555 if (!do_notification)
2557 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2558 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2564 do_notification = false;
2569 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2570 const struct mlxsw_sp_fid *fid,
2572 struct net_device **nve_dev,
2573 u16 *p_vid, __be32 *p_vni)
2575 struct mlxsw_sp_bridge_device *bridge_device;
2576 struct net_device *br_dev, *dev;
2580 err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2584 err = mlxsw_sp_fid_vni(fid, p_vni);
2588 dev = __dev_get_by_index(&init_net, nve_ifindex);
2593 if (!netif_running(dev))
2596 if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2599 if (adding && netif_is_vxlan(dev)) {
2600 struct vxlan_dev *vxlan = netdev_priv(dev);
2602 if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2606 br_dev = netdev_master_upper_dev_get(dev);
2610 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2614 *p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2619 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2624 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2625 enum switchdev_notifier_type type;
2626 struct net_device *nve_dev;
2627 union mlxsw_sp_l3addr addr;
2628 struct mlxsw_sp_fid *fid;
2635 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2638 fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2640 goto err_fid_lookup;
2642 err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2643 (enum mlxsw_sp_l3proto) sfn_proto,
2646 goto err_ip_resolve;
2648 err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2649 &nve_dev, &vid, &vni);
2651 goto err_fdb_process;
2653 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2654 (enum mlxsw_sp_l3proto) sfn_proto,
2655 &addr, adding, true);
2659 mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2660 (enum mlxsw_sp_l3proto) sfn_proto,
2661 &addr, vni, adding);
2663 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2664 SWITCHDEV_FDB_DEL_TO_BRIDGE;
2665 mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2667 mlxsw_sp_fid_put(fid);
2674 mlxsw_sp_fid_put(fid);
2676 /* Remove an FDB entry in case we cannot process it. Otherwise the
2677 * device will keep sending the same notification over and over again.
2679 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2680 (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2684 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2685 char *sfn_pl, int rec_index)
2687 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2688 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2689 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2692 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2693 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2696 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2697 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2700 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2701 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2704 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2705 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2708 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2709 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2715 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2717 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2719 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2720 msecs_to_jiffies(bridge->fdb_notify.interval));
2723 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2725 struct mlxsw_sp_bridge *bridge;
2726 struct mlxsw_sp *mlxsw_sp;
2732 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2736 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2737 mlxsw_sp = bridge->mlxsw_sp;
2740 mlxsw_reg_sfn_pack(sfn_pl);
2741 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2743 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2746 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2747 for (i = 0; i < num_rec; i++)
2748 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2753 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2756 struct mlxsw_sp_switchdev_event_work {
2757 struct work_struct work;
2759 struct switchdev_notifier_fdb_info fdb_info;
2760 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2762 struct net_device *dev;
2763 unsigned long event;
2767 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2768 struct mlxsw_sp_switchdev_event_work *
2770 struct mlxsw_sp_fid *fid, __be32 vni)
2772 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2773 struct switchdev_notifier_fdb_info *fdb_info;
2774 struct net_device *dev = switchdev_work->dev;
2775 enum mlxsw_sp_l3proto proto;
2776 union mlxsw_sp_l3addr addr;
2779 fdb_info = &switchdev_work->fdb_info;
2780 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2784 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2787 switch (switchdev_work->event) {
2788 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2789 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2790 vxlan_fdb_info.eth_addr,
2791 mlxsw_sp_fid_index(fid),
2792 proto, &addr, true, false);
2795 vxlan_fdb_info.offloaded = true;
2796 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2797 &vxlan_fdb_info.info, NULL);
2798 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2799 vxlan_fdb_info.eth_addr,
2800 fdb_info->vid, dev, true);
2802 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2803 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2804 vxlan_fdb_info.eth_addr,
2805 mlxsw_sp_fid_index(fid),
2806 proto, &addr, false,
2808 vxlan_fdb_info.offloaded = false;
2809 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2810 &vxlan_fdb_info.info, NULL);
2816 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2819 struct mlxsw_sp_bridge_device *bridge_device;
2820 struct net_device *dev = switchdev_work->dev;
2821 struct net_device *br_dev;
2822 struct mlxsw_sp *mlxsw_sp;
2823 struct mlxsw_sp_fid *fid;
2827 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2828 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2831 if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
2832 !switchdev_work->fdb_info.added_by_user)
2835 if (!netif_running(dev))
2837 br_dev = netdev_master_upper_dev_get(dev);
2840 if (!netif_is_bridge_master(br_dev))
2842 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2845 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2849 fid = bridge_device->ops->fid_lookup(bridge_device,
2850 switchdev_work->fdb_info.vid);
2854 err = mlxsw_sp_fid_vni(fid, &vni);
2858 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2862 mlxsw_sp_fid_put(fid);
2865 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2867 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2868 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2869 struct net_device *dev = switchdev_work->dev;
2870 struct switchdev_notifier_fdb_info *fdb_info;
2871 struct mlxsw_sp_port *mlxsw_sp_port;
2875 if (netif_is_vxlan(dev)) {
2876 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2880 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2884 switch (switchdev_work->event) {
2885 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2886 fdb_info = &switchdev_work->fdb_info;
2887 if (!fdb_info->added_by_user)
2889 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2892 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2894 fdb_info->vid, dev, true);
2896 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2897 fdb_info = &switchdev_work->fdb_info;
2898 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2900 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2901 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2902 /* These events are only used to potentially update an existing
2908 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2912 kfree(switchdev_work->fdb_info.addr);
2913 kfree(switchdev_work);
2918 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
2919 struct mlxsw_sp_switchdev_event_work *
2922 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2923 struct mlxsw_sp_bridge_device *bridge_device;
2924 struct net_device *dev = switchdev_work->dev;
2925 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2926 enum mlxsw_sp_l3proto proto;
2927 union mlxsw_sp_l3addr addr;
2928 struct net_device *br_dev;
2929 struct mlxsw_sp_fid *fid;
2933 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2934 br_dev = netdev_master_upper_dev_get(dev);
2936 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2940 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2944 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2947 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2948 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
2950 mlxsw_sp_fid_put(fid);
2953 vxlan_fdb_info->offloaded = true;
2954 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2955 &vxlan_fdb_info->info, NULL);
2956 mlxsw_sp_fid_put(fid);
2960 /* The device has a single FDB table, whereas Linux has two - one
2961 * in the bridge driver and another in the VxLAN driver. We only
2962 * program an entry to the device if the MAC points to the VxLAN
2963 * device in the bridge's FDB table
2965 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2966 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
2967 goto err_br_fdb_find;
2969 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2970 mlxsw_sp_fid_index(fid), proto,
2971 &addr, true, false);
2973 goto err_fdb_tunnel_uc_op;
2974 vxlan_fdb_info->offloaded = true;
2975 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2976 &vxlan_fdb_info->info, NULL);
2977 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2978 vxlan_fdb_info->eth_addr, vid, dev, true);
2980 mlxsw_sp_fid_put(fid);
2984 err_fdb_tunnel_uc_op:
2986 mlxsw_sp_fid_put(fid);
2990 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
2991 struct mlxsw_sp_switchdev_event_work *
2994 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2995 struct mlxsw_sp_bridge_device *bridge_device;
2996 struct net_device *dev = switchdev_work->dev;
2997 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2998 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2999 enum mlxsw_sp_l3proto proto;
3000 union mlxsw_sp_l3addr addr;
3001 struct mlxsw_sp_fid *fid;
3004 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3006 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3010 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3014 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3017 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3018 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3019 mlxsw_sp_fid_put(fid);
3023 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3024 mlxsw_sp_fid_index(fid), proto, &addr,
3026 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3027 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3028 vxlan_fdb_info->eth_addr, vid, dev, false);
3030 mlxsw_sp_fid_put(fid);
3033 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3035 struct mlxsw_sp_switchdev_event_work *switchdev_work =
3036 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3037 struct net_device *dev = switchdev_work->dev;
3038 struct mlxsw_sp *mlxsw_sp;
3039 struct net_device *br_dev;
3043 if (!netif_running(dev))
3045 br_dev = netdev_master_upper_dev_get(dev);
3048 if (!netif_is_bridge_master(br_dev))
3050 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3054 switch (switchdev_work->event) {
3055 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3056 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3058 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3059 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3065 kfree(switchdev_work);
3070 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3072 struct switchdev_notifier_info *info)
3074 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3075 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3076 struct vxlan_config *cfg = &vxlan->cfg;
3077 struct netlink_ext_ack *extack;
3079 extack = switchdev_notifier_info_to_extack(info);
3080 vxlan_fdb_info = container_of(info,
3081 struct switchdev_notifier_vxlan_fdb_info,
3084 if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3085 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3088 if (vxlan_fdb_info->remote_vni != cfg->vni ||
3089 vxlan_fdb_info->vni != cfg->vni) {
3090 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3093 if (vxlan_fdb_info->remote_ifindex) {
3094 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3097 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3098 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3101 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3102 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3106 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3111 /* Called under rcu_read_lock() */
3112 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3113 unsigned long event, void *ptr)
3115 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3116 struct mlxsw_sp_switchdev_event_work *switchdev_work;
3117 struct switchdev_notifier_fdb_info *fdb_info;
3118 struct switchdev_notifier_info *info = ptr;
3119 struct net_device *br_dev;
3122 if (event == SWITCHDEV_PORT_ATTR_SET) {
3123 err = switchdev_handle_port_attr_set(dev, ptr,
3124 mlxsw_sp_port_dev_check,
3125 mlxsw_sp_port_attr_set);
3126 return notifier_from_errno(err);
3129 /* Tunnel devices are not our uppers, so check their master instead */
3130 br_dev = netdev_master_upper_dev_get_rcu(dev);
3133 if (!netif_is_bridge_master(br_dev))
3135 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3138 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3139 if (!switchdev_work)
3142 switchdev_work->dev = dev;
3143 switchdev_work->event = event;
3146 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
3147 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
3148 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
3149 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3150 fdb_info = container_of(info,
3151 struct switchdev_notifier_fdb_info,
3153 INIT_WORK(&switchdev_work->work,
3154 mlxsw_sp_switchdev_bridge_fdb_event_work);
3155 memcpy(&switchdev_work->fdb_info, ptr,
3156 sizeof(switchdev_work->fdb_info));
3157 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3158 if (!switchdev_work->fdb_info.addr)
3159 goto err_addr_alloc;
3160 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3162 /* Take a reference on the device. This can be either
3163 * upper device containig mlxsw_sp_port or just a
3168 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE: /* fall through */
3169 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3170 INIT_WORK(&switchdev_work->work,
3171 mlxsw_sp_switchdev_vxlan_fdb_event_work);
3172 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3175 goto err_vxlan_work_prepare;
3179 kfree(switchdev_work);
3183 mlxsw_core_schedule_work(&switchdev_work->work);
3187 err_vxlan_work_prepare:
3189 kfree(switchdev_work);
3193 struct notifier_block mlxsw_sp_switchdev_notifier = {
3194 .notifier_call = mlxsw_sp_switchdev_event,
3198 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3199 struct mlxsw_sp_bridge_device *bridge_device,
3200 const struct net_device *vxlan_dev, u16 vid,
3201 bool flag_untagged, bool flag_pvid,
3202 struct netlink_ext_ack *extack)
3204 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3205 __be32 vni = vxlan->cfg.vni;
3206 struct mlxsw_sp_fid *fid;
3210 /* We cannot have the same VLAN as PVID and egress untagged on multiple
3211 * VxLAN devices. Note that we get this notification before the VLAN is
3212 * actually added to the bridge's database, so it is not possible for
3213 * the lookup function to return 'vxlan_dev'
3215 if (flag_untagged && flag_pvid &&
3216 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3217 NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3221 if (!netif_running(vxlan_dev))
3224 /* First case: FID is not associated with this VNI, but the new VLAN
3225 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3228 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3230 if (!flag_untagged || !flag_pvid)
3232 return mlxsw_sp_bridge_8021q_vxlan_join(bridge_device,
3233 vxlan_dev, vid, extack);
3236 /* Second case: FID is associated with the VNI and the VLAN associated
3237 * with the FID is the same as the notified VLAN. This means the flags
3238 * (PVID / egress untagged) were toggled and that NVE should be
3239 * disabled on the FID
3241 old_vid = mlxsw_sp_fid_8021q_vid(fid);
3242 if (vid == old_vid) {
3243 if (WARN_ON(flag_untagged && flag_pvid)) {
3244 mlxsw_sp_fid_put(fid);
3247 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3248 mlxsw_sp_fid_put(fid);
3252 /* Third case: A new VLAN was configured on the VxLAN device, but this
3253 * VLAN is not PVID, so there is nothing to do.
3256 mlxsw_sp_fid_put(fid);
3260 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3261 * mapped to the VNI should be unmapped
3263 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3264 mlxsw_sp_fid_put(fid);
3266 /* Fifth case: The new VLAN is also egress untagged, which means the
3267 * VLAN needs to be mapped to the VNI
3272 err = mlxsw_sp_bridge_8021q_vxlan_join(bridge_device, vxlan_dev, vid,
3275 goto err_vxlan_join;
3280 mlxsw_sp_bridge_8021q_vxlan_join(bridge_device, vxlan_dev, old_vid,
3286 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3287 struct mlxsw_sp_bridge_device *bridge_device,
3288 const struct net_device *vxlan_dev, u16 vid)
3290 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3291 __be32 vni = vxlan->cfg.vni;
3292 struct mlxsw_sp_fid *fid;
3294 if (!netif_running(vxlan_dev))
3297 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3301 /* A different VLAN than the one mapped to the VNI is deleted */
3302 if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3305 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3308 mlxsw_sp_fid_put(fid);
3312 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3313 struct switchdev_notifier_port_obj_info *
3316 struct switchdev_obj_port_vlan *vlan =
3317 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3318 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3319 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3320 struct switchdev_trans *trans = port_obj_info->trans;
3321 struct mlxsw_sp_bridge_device *bridge_device;
3322 struct netlink_ext_ack *extack;
3323 struct mlxsw_sp *mlxsw_sp;
3324 struct net_device *br_dev;
3327 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3328 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3332 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3336 port_obj_info->handled = true;
3338 if (switchdev_trans_ph_commit(trans))
3341 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3345 if (!bridge_device->vlan_enabled)
3348 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
3351 err = mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3363 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3364 struct switchdev_notifier_port_obj_info *
3367 struct switchdev_obj_port_vlan *vlan =
3368 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3369 struct mlxsw_sp_bridge_device *bridge_device;
3370 struct mlxsw_sp *mlxsw_sp;
3371 struct net_device *br_dev;
3374 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3378 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3382 port_obj_info->handled = true;
3384 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3388 if (!bridge_device->vlan_enabled)
3391 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
3392 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device,
3397 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3398 struct switchdev_notifier_port_obj_info *
3403 switch (port_obj_info->obj->id) {
3404 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3405 err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3416 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3417 struct switchdev_notifier_port_obj_info *
3420 switch (port_obj_info->obj->id) {
3421 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3422 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3429 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3430 unsigned long event, void *ptr)
3432 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3436 case SWITCHDEV_PORT_OBJ_ADD:
3437 if (netif_is_vxlan(dev))
3438 err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3440 err = switchdev_handle_port_obj_add(dev, ptr,
3441 mlxsw_sp_port_dev_check,
3442 mlxsw_sp_port_obj_add);
3443 return notifier_from_errno(err);
3444 case SWITCHDEV_PORT_OBJ_DEL:
3445 if (netif_is_vxlan(dev))
3446 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3448 err = switchdev_handle_port_obj_del(dev, ptr,
3449 mlxsw_sp_port_dev_check,
3450 mlxsw_sp_port_obj_del);
3451 return notifier_from_errno(err);
3452 case SWITCHDEV_PORT_ATTR_SET:
3453 err = switchdev_handle_port_attr_set(dev, ptr,
3454 mlxsw_sp_port_dev_check,
3455 mlxsw_sp_port_attr_set);
3456 return notifier_from_errno(err);
3462 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3463 .notifier_call = mlxsw_sp_switchdev_blocking_event,
3467 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3469 return bridge_port->stp_state;
3472 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3474 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3475 struct notifier_block *nb;
3478 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3480 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3484 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3486 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3490 nb = &mlxsw_sp_switchdev_blocking_notifier;
3491 err = register_switchdev_blocking_notifier(nb);
3493 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3494 goto err_register_switchdev_blocking_notifier;
3497 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3498 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3499 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
3502 err_register_switchdev_blocking_notifier:
3503 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3507 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3509 struct notifier_block *nb;
3511 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3513 nb = &mlxsw_sp_switchdev_blocking_notifier;
3514 unregister_switchdev_blocking_notifier(nb);
3516 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3519 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3521 struct mlxsw_sp_bridge *bridge;
3523 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3526 mlxsw_sp->bridge = bridge;
3527 bridge->mlxsw_sp = mlxsw_sp;
3529 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3531 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3532 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3534 return mlxsw_sp_fdb_init(mlxsw_sp);
3537 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3539 mlxsw_sp_fdb_fini(mlxsw_sp);
3540 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3541 kfree(mlxsw_sp->bridge);