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);
102 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
103 struct mlxsw_sp_bridge_port *bridge_port,
107 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
108 struct mlxsw_sp_bridge_port *bridge_port);
111 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
112 struct mlxsw_sp_bridge_device
116 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
117 struct mlxsw_sp_bridge_port *bridge_port,
120 static struct mlxsw_sp_bridge_device *
121 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
122 const struct net_device *br_dev)
124 struct mlxsw_sp_bridge_device *bridge_device;
126 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
127 if (bridge_device->dev == br_dev)
128 return bridge_device;
133 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
134 const struct net_device *br_dev)
136 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
139 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
140 struct netdev_nested_priv *priv)
142 struct mlxsw_sp *mlxsw_sp = priv->data;
144 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
148 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
149 struct net_device *dev)
151 struct netdev_nested_priv priv = {
152 .data = (void *)mlxsw_sp,
155 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
156 netdev_walk_all_upper_dev_rcu(dev,
157 mlxsw_sp_bridge_device_upper_rif_destroy,
161 static int mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge *bridge,
162 struct net_device *br_dev,
163 struct netlink_ext_ack *extack)
165 struct net_device *dev, *stop_dev;
166 struct list_head *iter;
169 netdev_for_each_lower_dev(br_dev, dev, iter) {
170 if (netif_is_vxlan(dev) && netif_running(dev)) {
171 err = mlxsw_sp_bridge_vxlan_join(bridge->mlxsw_sp,
184 netdev_for_each_lower_dev(br_dev, dev, iter) {
185 if (netif_is_vxlan(dev) && netif_running(dev)) {
188 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
194 static void mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge *bridge,
195 struct net_device *br_dev)
197 struct net_device *dev;
198 struct list_head *iter;
200 netdev_for_each_lower_dev(br_dev, dev, iter) {
201 if (netif_is_vxlan(dev) && netif_running(dev))
202 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
206 static struct mlxsw_sp_bridge_device *
207 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
208 struct net_device *br_dev,
209 struct netlink_ext_ack *extack)
211 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
212 struct mlxsw_sp_bridge_device *bridge_device;
213 bool vlan_enabled = br_vlan_enabled(br_dev);
216 if (vlan_enabled && bridge->vlan_enabled_exists) {
217 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
218 NL_SET_ERR_MSG_MOD(extack, "Only one VLAN-aware bridge is supported");
219 return ERR_PTR(-EINVAL);
222 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
224 return ERR_PTR(-ENOMEM);
226 bridge_device->dev = br_dev;
227 bridge_device->vlan_enabled = vlan_enabled;
228 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
229 bridge_device->mrouter = br_multicast_router(br_dev);
230 INIT_LIST_HEAD(&bridge_device->ports_list);
234 bridge->vlan_enabled_exists = true;
235 br_vlan_get_proto(br_dev, &proto);
236 if (proto == ETH_P_8021AD)
237 bridge_device->ops = bridge->bridge_8021ad_ops;
239 bridge_device->ops = bridge->bridge_8021q_ops;
241 bridge_device->ops = bridge->bridge_8021d_ops;
243 INIT_LIST_HEAD(&bridge_device->mids_list);
244 list_add(&bridge_device->list, &bridge->bridges_list);
246 /* It is possible we already have VXLAN devices enslaved to the bridge.
247 * In which case, we need to replay their configuration as if they were
248 * just now enslaved to the bridge.
250 err = mlxsw_sp_bridge_device_vxlan_init(bridge, br_dev, extack);
254 return bridge_device;
257 list_del(&bridge_device->list);
258 if (bridge_device->vlan_enabled)
259 bridge->vlan_enabled_exists = false;
260 kfree(bridge_device);
265 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
266 struct mlxsw_sp_bridge_device *bridge_device)
268 mlxsw_sp_bridge_device_vxlan_fini(bridge, bridge_device->dev);
269 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
271 list_del(&bridge_device->list);
272 if (bridge_device->vlan_enabled)
273 bridge->vlan_enabled_exists = false;
274 WARN_ON(!list_empty(&bridge_device->ports_list));
275 WARN_ON(!list_empty(&bridge_device->mids_list));
276 kfree(bridge_device);
279 static struct mlxsw_sp_bridge_device *
280 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
281 struct net_device *br_dev,
282 struct netlink_ext_ack *extack)
284 struct mlxsw_sp_bridge_device *bridge_device;
286 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
288 return bridge_device;
290 return mlxsw_sp_bridge_device_create(bridge, br_dev, extack);
294 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
295 struct mlxsw_sp_bridge_device *bridge_device)
297 if (list_empty(&bridge_device->ports_list))
298 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
301 static struct mlxsw_sp_bridge_port *
302 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
303 const struct net_device *brport_dev)
305 struct mlxsw_sp_bridge_port *bridge_port;
307 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
308 if (bridge_port->dev == brport_dev)
315 struct mlxsw_sp_bridge_port *
316 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
317 struct net_device *brport_dev)
319 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
320 struct mlxsw_sp_bridge_device *bridge_device;
325 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
329 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
332 static struct mlxsw_sp_bridge_port *
333 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
334 struct net_device *brport_dev)
336 struct mlxsw_sp_bridge_port *bridge_port;
337 struct mlxsw_sp_port *mlxsw_sp_port;
339 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
343 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
344 bridge_port->lagged = mlxsw_sp_port->lagged;
345 if (bridge_port->lagged)
346 bridge_port->lag_id = mlxsw_sp_port->lag_id;
348 bridge_port->system_port = mlxsw_sp_port->local_port;
349 bridge_port->dev = brport_dev;
350 bridge_port->bridge_device = bridge_device;
351 bridge_port->stp_state = BR_STATE_DISABLED;
352 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
354 INIT_LIST_HEAD(&bridge_port->vlans_list);
355 list_add(&bridge_port->list, &bridge_device->ports_list);
356 bridge_port->ref_count = 1;
362 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
364 list_del(&bridge_port->list);
365 WARN_ON(!list_empty(&bridge_port->vlans_list));
369 static struct mlxsw_sp_bridge_port *
370 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
371 struct net_device *brport_dev,
372 struct netlink_ext_ack *extack)
374 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
375 struct mlxsw_sp_bridge_device *bridge_device;
376 struct mlxsw_sp_bridge_port *bridge_port;
379 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
381 bridge_port->ref_count++;
385 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev, extack);
386 if (IS_ERR(bridge_device))
387 return ERR_CAST(bridge_device);
389 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
392 goto err_bridge_port_create;
397 err_bridge_port_create:
398 mlxsw_sp_bridge_device_put(bridge, bridge_device);
402 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
403 struct mlxsw_sp_bridge_port *bridge_port)
405 struct mlxsw_sp_bridge_device *bridge_device;
407 if (--bridge_port->ref_count != 0)
409 bridge_device = bridge_port->bridge_device;
410 mlxsw_sp_bridge_port_destroy(bridge_port);
411 mlxsw_sp_bridge_device_put(bridge, bridge_device);
414 static struct mlxsw_sp_port_vlan *
415 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
416 const struct mlxsw_sp_bridge_device *
420 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
422 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
424 if (!mlxsw_sp_port_vlan->bridge_port)
426 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
429 if (bridge_device->vlan_enabled &&
430 mlxsw_sp_port_vlan->vid != vid)
432 return mlxsw_sp_port_vlan;
438 static struct mlxsw_sp_port_vlan*
439 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
442 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
444 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
446 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
448 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
449 return mlxsw_sp_port_vlan;
455 static struct mlxsw_sp_bridge_vlan *
456 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
459 struct mlxsw_sp_bridge_vlan *bridge_vlan;
461 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
462 if (bridge_vlan->vid == vid)
469 static struct mlxsw_sp_bridge_vlan *
470 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
472 struct mlxsw_sp_bridge_vlan *bridge_vlan;
474 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
478 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
479 bridge_vlan->vid = vid;
480 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
486 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
488 list_del(&bridge_vlan->list);
489 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
493 static struct mlxsw_sp_bridge_vlan *
494 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
496 struct mlxsw_sp_bridge_vlan *bridge_vlan;
498 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
502 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
505 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
507 if (list_empty(&bridge_vlan->port_vlan_list))
508 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
512 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
513 struct mlxsw_sp_bridge_vlan *bridge_vlan,
516 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
518 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
520 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
522 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
523 bridge_vlan->vid, state);
529 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
530 struct net_device *orig_dev,
533 struct mlxsw_sp_bridge_port *bridge_port;
534 struct mlxsw_sp_bridge_vlan *bridge_vlan;
537 /* It's possible we failed to enslave the port, yet this
538 * operation is executed due to it being deferred.
540 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
545 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
546 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
549 goto err_port_bridge_vlan_stp_set;
552 bridge_port->stp_state = state;
556 err_port_bridge_vlan_stp_set:
557 list_for_each_entry_continue_reverse(bridge_vlan,
558 &bridge_port->vlans_list, list)
559 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
560 bridge_port->stp_state);
565 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
566 struct mlxsw_sp_bridge_vlan *bridge_vlan,
567 enum mlxsw_sp_flood_type packet_type,
570 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
572 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
574 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
576 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
578 mlxsw_sp_port->local_port,
586 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
587 struct mlxsw_sp_bridge_port *bridge_port,
588 enum mlxsw_sp_flood_type packet_type,
591 struct mlxsw_sp_bridge_vlan *bridge_vlan;
594 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
595 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
600 goto err_port_bridge_vlan_flood_set;
605 err_port_bridge_vlan_flood_set:
606 list_for_each_entry_continue_reverse(bridge_vlan,
607 &bridge_port->vlans_list, list)
608 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
609 packet_type, !member);
614 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
615 struct mlxsw_sp_bridge_vlan *bridge_vlan,
618 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
619 u16 vid = bridge_vlan->vid;
621 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
623 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
625 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
632 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
633 struct mlxsw_sp_bridge_port *bridge_port,
636 struct mlxsw_sp_bridge_vlan *bridge_vlan;
639 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
640 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
643 goto err_port_bridge_vlan_learning_set;
648 err_port_bridge_vlan_learning_set:
649 list_for_each_entry_continue_reverse(bridge_vlan,
650 &bridge_port->vlans_list, list)
651 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
656 static int mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port
658 unsigned long brport_flags)
660 if (brport_flags & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
666 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
667 struct net_device *orig_dev,
668 unsigned long brport_flags)
670 struct mlxsw_sp_bridge_port *bridge_port;
673 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
678 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
679 MLXSW_SP_FLOOD_TYPE_UC,
680 brport_flags & BR_FLOOD);
684 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
685 brport_flags & BR_LEARNING);
689 if (bridge_port->bridge_device->multicast_enabled)
692 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
693 MLXSW_SP_FLOOD_TYPE_MC,
700 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
704 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
706 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
709 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
710 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
713 mlxsw_sp->bridge->ageing_time = ageing_time;
717 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
718 unsigned long ageing_clock_t)
720 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
721 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
722 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
724 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
725 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
728 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
731 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
732 struct net_device *orig_dev,
735 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
736 struct mlxsw_sp_bridge_device *bridge_device;
738 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
739 if (WARN_ON(!bridge_device))
742 if (bridge_device->vlan_enabled == vlan_enabled)
745 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
749 static int mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port *mlxsw_sp_port,
750 struct net_device *orig_dev,
753 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
754 struct mlxsw_sp_bridge_device *bridge_device;
756 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
757 if (WARN_ON(!bridge_device))
760 netdev_err(bridge_device->dev, "VLAN protocol can't be changed on existing bridge\n");
764 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
765 struct net_device *orig_dev,
766 bool is_port_mrouter)
768 struct mlxsw_sp_bridge_port *bridge_port;
771 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
776 if (!bridge_port->bridge_device->multicast_enabled)
779 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
780 MLXSW_SP_FLOOD_TYPE_MC,
785 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
788 bridge_port->mrouter = is_port_mrouter;
792 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
794 const struct mlxsw_sp_bridge_device *bridge_device;
796 bridge_device = bridge_port->bridge_device;
797 return bridge_device->multicast_enabled ? bridge_port->mrouter :
798 bridge_port->flags & BR_MCAST_FLOOD;
801 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
802 struct net_device *orig_dev,
805 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
806 struct mlxsw_sp_bridge_device *bridge_device;
807 struct mlxsw_sp_bridge_port *bridge_port;
810 /* It's possible we failed to enslave the port, yet this
811 * operation is executed due to it being deferred.
813 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
817 if (bridge_device->multicast_enabled != !mc_disabled) {
818 bridge_device->multicast_enabled = !mc_disabled;
819 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
823 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
824 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
825 bool member = mlxsw_sp_mc_flood(bridge_port);
827 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
829 packet_type, member);
834 bridge_device->multicast_enabled = !mc_disabled;
839 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
840 u16 mid_idx, bool add)
845 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
849 mlxsw_reg_smid_pack(smid_pl, mid_idx,
850 mlxsw_sp_router_port(mlxsw_sp), add);
851 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
857 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
858 struct mlxsw_sp_bridge_device *bridge_device,
861 struct mlxsw_sp_mid *mid;
863 list_for_each_entry(mid, &bridge_device->mids_list, list)
864 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
868 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
869 struct net_device *orig_dev,
872 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
873 struct mlxsw_sp_bridge_device *bridge_device;
875 /* It's possible we failed to enslave the port, yet this
876 * operation is executed due to it being deferred.
878 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
882 if (bridge_device->mrouter != is_mrouter)
883 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
885 bridge_device->mrouter = is_mrouter;
889 static int mlxsw_sp_port_attr_set(struct net_device *dev,
890 const struct switchdev_attr *attr)
892 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
896 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
897 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port,
901 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
902 err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
903 attr->u.brport_flags);
905 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
906 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port,
908 attr->u.brport_flags);
910 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
911 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port,
912 attr->u.ageing_time);
914 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
915 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port,
917 attr->u.vlan_filtering);
919 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL:
920 err = mlxsw_sp_port_attr_br_vlan_proto_set(mlxsw_sp_port,
922 attr->u.vlan_protocol);
924 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
925 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port,
929 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
930 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port,
932 attr->u.mc_disabled);
934 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
935 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port,
944 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
950 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
951 struct mlxsw_sp_bridge_port *bridge_port,
952 struct netlink_ext_ack *extack)
954 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
955 struct mlxsw_sp_bridge_device *bridge_device;
956 u8 local_port = mlxsw_sp_port->local_port;
957 u16 vid = mlxsw_sp_port_vlan->vid;
958 struct mlxsw_sp_fid *fid;
961 bridge_device = bridge_port->bridge_device;
962 fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
966 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
967 bridge_port->flags & BR_FLOOD);
969 goto err_fid_uc_flood_set;
971 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
972 mlxsw_sp_mc_flood(bridge_port));
974 goto err_fid_mc_flood_set;
976 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
979 goto err_fid_bc_flood_set;
981 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
983 goto err_fid_port_vid_map;
985 mlxsw_sp_port_vlan->fid = fid;
989 err_fid_port_vid_map:
990 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
991 err_fid_bc_flood_set:
992 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
993 err_fid_mc_flood_set:
994 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
995 err_fid_uc_flood_set:
996 mlxsw_sp_fid_put(fid);
1001 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1003 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1004 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1005 u8 local_port = mlxsw_sp_port->local_port;
1006 u16 vid = mlxsw_sp_port_vlan->vid;
1008 mlxsw_sp_port_vlan->fid = NULL;
1009 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
1010 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1011 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1012 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1013 mlxsw_sp_fid_put(fid);
1017 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1018 u16 vid, bool is_pvid)
1022 else if (mlxsw_sp_port->pvid == vid)
1023 return 0; /* Dis-allow untagged packets */
1025 return mlxsw_sp_port->pvid;
1029 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1030 struct mlxsw_sp_bridge_port *bridge_port,
1031 struct netlink_ext_ack *extack)
1033 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1034 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1035 u16 vid = mlxsw_sp_port_vlan->vid;
1038 /* No need to continue if only VLAN flags were changed */
1039 if (mlxsw_sp_port_vlan->bridge_port)
1042 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
1047 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1048 bridge_port->flags & BR_LEARNING);
1050 goto err_port_vid_learning_set;
1052 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1053 bridge_port->stp_state);
1055 goto err_port_vid_stp_set;
1057 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1060 goto err_bridge_vlan_get;
1063 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1064 &bridge_vlan->port_vlan_list);
1066 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1067 bridge_port->dev, extack);
1068 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1072 err_bridge_vlan_get:
1073 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1074 err_port_vid_stp_set:
1075 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1076 err_port_vid_learning_set:
1077 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1082 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1084 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1085 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1086 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1087 struct mlxsw_sp_bridge_port *bridge_port;
1088 u16 vid = mlxsw_sp_port_vlan->vid;
1089 bool last_port, last_vlan;
1091 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1092 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1095 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1096 last_vlan = list_is_singular(&bridge_port->vlans_list);
1097 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1098 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1100 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1101 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1102 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1103 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1105 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1107 mlxsw_sp_fid_index(fid));
1109 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1111 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1113 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1114 mlxsw_sp_port_vlan->bridge_port = NULL;
1118 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1119 struct mlxsw_sp_bridge_port *bridge_port,
1120 u16 vid, bool is_untagged, bool is_pvid,
1121 struct netlink_ext_ack *extack)
1123 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1124 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1125 u16 old_pvid = mlxsw_sp_port->pvid;
1129 /* The only valid scenario in which a port-vlan already exists, is if
1130 * the VLAN flags were changed and the port-vlan is associated with the
1131 * correct bridge port
1133 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1134 if (mlxsw_sp_port_vlan &&
1135 mlxsw_sp_port_vlan->bridge_port != bridge_port)
1138 if (!mlxsw_sp_port_vlan) {
1139 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1141 if (IS_ERR(mlxsw_sp_port_vlan))
1142 return PTR_ERR(mlxsw_sp_port_vlan);
1145 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1148 goto err_port_vlan_set;
1150 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1151 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1153 goto err_port_pvid_set;
1155 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1158 goto err_port_vlan_bridge_join;
1162 err_port_vlan_bridge_join:
1163 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto);
1165 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1167 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1172 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1173 const struct net_device *br_dev,
1174 const struct switchdev_obj_port_vlan *vlan)
1178 pvid = mlxsw_sp_rif_vid(mlxsw_sp, br_dev);
1182 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1183 if (vlan->vid != pvid) {
1184 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1188 if (vlan->vid == pvid) {
1189 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1197 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1198 const struct switchdev_obj_port_vlan *vlan,
1199 struct netlink_ext_ack *extack)
1201 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1202 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1203 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1204 struct net_device *orig_dev = vlan->obj.orig_dev;
1205 struct mlxsw_sp_bridge_port *bridge_port;
1207 if (netif_is_bridge_master(orig_dev)) {
1210 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1211 br_vlan_enabled(orig_dev))
1212 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1219 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1220 if (WARN_ON(!bridge_port))
1223 if (!bridge_port->bridge_device->vlan_enabled)
1226 return mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1227 vlan->vid, flag_untagged,
1231 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1233 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1234 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1238 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1239 struct mlxsw_sp_bridge_port *bridge_port,
1242 bool lagged = bridge_port->lagged;
1243 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1246 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1247 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1248 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1249 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1251 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1254 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1256 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1257 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1260 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1262 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1263 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1266 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1267 const char *mac, u16 fid,
1268 enum mlxsw_sp_l3proto proto,
1269 const union mlxsw_sp_l3addr *addr,
1270 bool adding, bool dynamic)
1272 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1279 case MLXSW_SP_L3_PROTO_IPV4:
1280 uip = be32_to_cpu(addr->addr4);
1281 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1283 case MLXSW_SP_L3_PROTO_IPV6:
1289 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1293 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1294 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1295 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1296 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1298 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1299 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1303 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1311 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1312 const char *mac, u16 fid, bool adding,
1313 enum mlxsw_reg_sfd_rec_action action,
1314 enum mlxsw_reg_sfd_rec_policy policy)
1320 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1324 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1325 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1326 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1327 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1331 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1339 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1340 const char *mac, u16 fid, bool adding,
1343 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1344 MLXSW_REG_SFD_REC_ACTION_NOP,
1345 mlxsw_sp_sfd_rec_policy(dynamic));
1348 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1351 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1352 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1353 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1356 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1357 const char *mac, u16 fid, u16 lag_vid,
1358 bool adding, bool dynamic)
1364 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1368 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1369 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1370 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1372 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1373 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1377 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1386 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1387 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1389 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1390 struct net_device *orig_dev = fdb_info->info.dev;
1391 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1392 struct mlxsw_sp_bridge_device *bridge_device;
1393 struct mlxsw_sp_bridge_port *bridge_port;
1396 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1400 bridge_device = bridge_port->bridge_device;
1401 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1404 if (!mlxsw_sp_port_vlan)
1407 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1408 vid = mlxsw_sp_port_vlan->vid;
1410 if (!bridge_port->lagged)
1411 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1412 bridge_port->system_port,
1413 fdb_info->addr, fid_index,
1416 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1417 bridge_port->lag_id,
1418 fdb_info->addr, fid_index,
1419 vid, adding, false);
1422 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1423 u16 fid, u16 mid_idx, bool adding)
1429 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1433 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1434 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1435 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1436 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1437 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1441 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1449 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1451 bool set_router_port)
1456 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1460 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1461 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1462 if (mlxsw_sp->ports[i])
1463 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1466 mlxsw_reg_smid_port_mask_set(smid_pl,
1467 mlxsw_sp_router_port(mlxsw_sp), 1);
1469 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1470 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1472 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1475 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1480 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1481 u16 mid_idx, bool add)
1483 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1487 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1491 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1492 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1498 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1499 const unsigned char *addr,
1502 struct mlxsw_sp_mid *mid;
1504 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1505 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1512 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1513 struct mlxsw_sp_bridge_port *bridge_port,
1514 unsigned long *ports_bitmap)
1516 struct mlxsw_sp_port *mlxsw_sp_port;
1517 u64 max_lag_members, i;
1520 if (!bridge_port->lagged) {
1521 set_bit(bridge_port->system_port, ports_bitmap);
1523 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1525 lag_id = bridge_port->lag_id;
1526 for (i = 0; i < max_lag_members; i++) {
1527 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1530 set_bit(mlxsw_sp_port->local_port,
1537 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1538 struct mlxsw_sp_bridge_device *bridge_device,
1539 struct mlxsw_sp *mlxsw_sp)
1541 struct mlxsw_sp_bridge_port *bridge_port;
1543 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1544 if (bridge_port->mrouter) {
1545 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1553 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1554 struct mlxsw_sp_mid *mid,
1555 struct mlxsw_sp_bridge_device *bridge_device)
1563 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1565 if (mid_idx == MLXSW_SP_MID_MAX)
1568 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1569 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1570 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1574 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1575 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1578 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1579 bridge_device->mrouter);
1580 kfree(flood_bitmap);
1584 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1589 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1594 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1595 struct mlxsw_sp_mid *mid)
1600 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1602 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1607 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1608 struct mlxsw_sp_bridge_device *bridge_device,
1609 const unsigned char *addr,
1612 struct mlxsw_sp_mid *mid;
1615 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1619 alloc_size = sizeof(unsigned long) *
1620 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1622 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1623 if (!mid->ports_in_mid)
1624 goto err_ports_in_mid_alloc;
1626 ether_addr_copy(mid->addr, addr);
1630 if (!bridge_device->multicast_enabled)
1633 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1634 goto err_write_mdb_entry;
1637 list_add_tail(&mid->list, &bridge_device->mids_list);
1640 err_write_mdb_entry:
1641 kfree(mid->ports_in_mid);
1642 err_ports_in_mid_alloc:
1647 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1648 struct mlxsw_sp_mid *mid)
1650 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1653 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1654 if (bitmap_empty(mid->ports_in_mid,
1655 mlxsw_core_max_ports(mlxsw_sp->core))) {
1656 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1657 list_del(&mid->list);
1658 kfree(mid->ports_in_mid);
1664 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1665 const struct switchdev_obj_port_mdb *mdb)
1667 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1668 struct net_device *orig_dev = mdb->obj.orig_dev;
1669 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1670 struct net_device *dev = mlxsw_sp_port->dev;
1671 struct mlxsw_sp_bridge_device *bridge_device;
1672 struct mlxsw_sp_bridge_port *bridge_port;
1673 struct mlxsw_sp_mid *mid;
1677 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1681 bridge_device = bridge_port->bridge_device;
1682 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1685 if (!mlxsw_sp_port_vlan)
1688 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1690 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1692 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1695 netdev_err(dev, "Unable to allocate MC group\n");
1699 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1701 if (!bridge_device->multicast_enabled)
1704 if (bridge_port->mrouter)
1707 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1709 netdev_err(dev, "Unable to set SMID\n");
1716 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1721 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1722 struct mlxsw_sp_bridge_device
1725 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1726 struct mlxsw_sp_mid *mid;
1729 mc_enabled = bridge_device->multicast_enabled;
1731 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1733 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1736 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1741 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1742 struct mlxsw_sp_bridge_port *bridge_port,
1745 struct mlxsw_sp_bridge_device *bridge_device;
1746 struct mlxsw_sp_mid *mid;
1748 bridge_device = bridge_port->bridge_device;
1750 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1751 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1752 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1756 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1757 const struct switchdev_obj *obj,
1758 struct netlink_ext_ack *extack)
1760 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1761 const struct switchdev_obj_port_vlan *vlan;
1765 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1766 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1768 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, extack);
1770 /* The event is emitted before the changes are actually
1771 * applied to the bridge. Therefore schedule the respin
1772 * call for later, so that the respin logic sees the
1773 * updated bridge state.
1775 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1777 case SWITCHDEV_OBJ_ID_PORT_MDB:
1778 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1779 SWITCHDEV_OBJ_PORT_MDB(obj));
1790 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1791 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1793 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1794 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1797 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1798 if (WARN_ON(!mlxsw_sp_port_vlan))
1801 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1802 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1803 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1804 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1805 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1808 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1809 const struct switchdev_obj_port_vlan *vlan)
1811 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1812 struct net_device *orig_dev = vlan->obj.orig_dev;
1813 struct mlxsw_sp_bridge_port *bridge_port;
1815 if (netif_is_bridge_master(orig_dev))
1818 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1819 if (WARN_ON(!bridge_port))
1822 if (!bridge_port->bridge_device->vlan_enabled)
1825 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vlan->vid);
1831 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1832 struct mlxsw_sp_bridge_port *bridge_port,
1833 struct mlxsw_sp_mid *mid)
1835 struct net_device *dev = mlxsw_sp_port->dev;
1838 if (bridge_port->bridge_device->multicast_enabled &&
1839 !bridge_port->mrouter) {
1840 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1842 netdev_err(dev, "Unable to remove port from SMID\n");
1845 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1847 netdev_err(dev, "Unable to remove MC SFD\n");
1852 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1853 const struct switchdev_obj_port_mdb *mdb)
1855 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1856 struct net_device *orig_dev = mdb->obj.orig_dev;
1857 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1858 struct mlxsw_sp_bridge_device *bridge_device;
1859 struct net_device *dev = mlxsw_sp_port->dev;
1860 struct mlxsw_sp_bridge_port *bridge_port;
1861 struct mlxsw_sp_mid *mid;
1864 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1868 bridge_device = bridge_port->bridge_device;
1869 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1872 if (!mlxsw_sp_port_vlan)
1875 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1877 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1879 netdev_err(dev, "Unable to remove port from MC DB\n");
1883 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1887 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1888 struct mlxsw_sp_bridge_port *bridge_port)
1890 struct mlxsw_sp_bridge_device *bridge_device;
1891 struct mlxsw_sp_mid *mid, *tmp;
1893 bridge_device = bridge_port->bridge_device;
1895 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1896 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1897 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1899 } else if (bridge_device->multicast_enabled &&
1900 bridge_port->mrouter) {
1901 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1906 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1907 const struct switchdev_obj *obj)
1909 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1913 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1914 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1915 SWITCHDEV_OBJ_PORT_VLAN(obj));
1917 case SWITCHDEV_OBJ_ID_PORT_MDB:
1918 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1919 SWITCHDEV_OBJ_PORT_MDB(obj));
1926 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1931 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1934 struct mlxsw_sp_port *mlxsw_sp_port;
1935 u64 max_lag_members;
1938 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1940 for (i = 0; i < max_lag_members; i++) {
1941 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1943 return mlxsw_sp_port;
1949 mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port *bridge_port,
1950 struct mlxsw_sp_port *mlxsw_sp_port,
1951 struct netlink_ext_ack *extack)
1953 if (is_vlan_dev(bridge_port->dev)) {
1954 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1958 /* Port is no longer usable as a router interface */
1959 if (mlxsw_sp_port->default_vlan->fid)
1960 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
1966 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1967 struct mlxsw_sp_bridge_port *bridge_port,
1968 struct mlxsw_sp_port *mlxsw_sp_port,
1969 struct netlink_ext_ack *extack)
1971 return mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
1976 mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port *mlxsw_sp_port)
1978 /* Make sure untagged frames are allowed to ingress */
1979 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
1984 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1985 struct mlxsw_sp_bridge_port *bridge_port,
1986 struct mlxsw_sp_port *mlxsw_sp_port)
1988 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
1992 mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
1993 const struct net_device *vxlan_dev,
1994 u16 vid, u16 ethertype,
1995 struct netlink_ext_ack *extack)
1997 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1998 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
1999 struct mlxsw_sp_nve_params params = {
2000 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2001 .vni = vxlan->cfg.vni,
2003 .ethertype = ethertype,
2005 struct mlxsw_sp_fid *fid;
2008 /* If the VLAN is 0, we need to find the VLAN that is configured as
2009 * PVID and egress untagged on the bridge port of the VxLAN device.
2010 * It is possible no such VLAN exists
2013 err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
2018 fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2020 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1Q FID");
2021 return PTR_ERR(fid);
2024 if (mlxsw_sp_fid_vni_is_set(fid)) {
2025 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2027 goto err_vni_exists;
2030 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2032 goto err_nve_fid_enable;
2038 mlxsw_sp_fid_put(fid);
2043 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2044 const struct net_device *vxlan_dev, u16 vid,
2045 struct netlink_ext_ack *extack)
2047 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2048 vid, ETH_P_8021Q, extack);
2051 static struct net_device *
2052 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2054 struct net_device *dev;
2055 struct list_head *iter;
2057 netdev_for_each_lower_dev(br_dev, dev, iter) {
2061 if (!netif_is_vxlan(dev))
2064 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2065 if (err || pvid != vid)
2074 static struct mlxsw_sp_fid *
2075 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2076 u16 vid, struct netlink_ext_ack *extack)
2078 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2080 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2083 static struct mlxsw_sp_fid *
2084 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2087 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2089 return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2093 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2094 const struct mlxsw_sp_fid *fid)
2096 return mlxsw_sp_fid_8021q_vid(fid);
2099 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2100 .port_join = mlxsw_sp_bridge_8021q_port_join,
2101 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2102 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2103 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2104 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2105 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2109 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2110 const struct net_device *br_dev)
2112 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2114 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2116 if (mlxsw_sp_port_vlan->bridge_port &&
2117 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2126 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2127 struct mlxsw_sp_bridge_port *bridge_port,
2128 struct mlxsw_sp_port *mlxsw_sp_port,
2129 struct netlink_ext_ack *extack)
2131 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2132 struct net_device *dev = bridge_port->dev;
2135 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2136 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2137 if (WARN_ON(!mlxsw_sp_port_vlan))
2140 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2141 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2145 /* Port is no longer usable as a router interface */
2146 if (mlxsw_sp_port_vlan->fid)
2147 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2149 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2154 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2155 struct mlxsw_sp_bridge_port *bridge_port,
2156 struct mlxsw_sp_port *mlxsw_sp_port)
2158 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2159 struct net_device *dev = bridge_port->dev;
2162 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2163 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2164 if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2167 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2171 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2172 const struct net_device *vxlan_dev, u16 vid,
2173 struct netlink_ext_ack *extack)
2175 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2176 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2177 struct mlxsw_sp_nve_params params = {
2178 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2179 .vni = vxlan->cfg.vni,
2181 .ethertype = ETH_P_8021Q,
2183 struct mlxsw_sp_fid *fid;
2186 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2188 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1D FID");
2192 if (mlxsw_sp_fid_vni_is_set(fid)) {
2193 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2195 goto err_vni_exists;
2198 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2200 goto err_nve_fid_enable;
2206 mlxsw_sp_fid_put(fid);
2210 static struct mlxsw_sp_fid *
2211 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2212 u16 vid, struct netlink_ext_ack *extack)
2214 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2216 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2219 static struct mlxsw_sp_fid *
2220 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2223 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2225 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2229 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2233 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2234 const struct mlxsw_sp_fid *fid)
2239 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2240 .port_join = mlxsw_sp_bridge_8021d_port_join,
2241 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2242 .vxlan_join = mlxsw_sp_bridge_8021d_vxlan_join,
2243 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2244 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2245 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2249 mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2250 struct mlxsw_sp_bridge_port *bridge_port,
2251 struct mlxsw_sp_port *mlxsw_sp_port,
2252 struct netlink_ext_ack *extack)
2256 err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, false);
2260 err = mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2263 goto err_bridge_vlan_aware_port_join;
2267 err_bridge_vlan_aware_port_join:
2268 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2273 mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2274 struct mlxsw_sp_bridge_port *bridge_port,
2275 struct mlxsw_sp_port *mlxsw_sp_port)
2277 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2278 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2282 mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2283 const struct net_device *vxlan_dev, u16 vid,
2284 struct netlink_ext_ack *extack)
2286 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2287 vid, ETH_P_8021AD, extack);
2290 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021ad_ops = {
2291 .port_join = mlxsw_sp_bridge_8021ad_port_join,
2292 .port_leave = mlxsw_sp_bridge_8021ad_port_leave,
2293 .vxlan_join = mlxsw_sp_bridge_8021ad_vxlan_join,
2294 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2295 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2296 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2299 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2300 struct net_device *brport_dev,
2301 struct net_device *br_dev,
2302 struct netlink_ext_ack *extack)
2304 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2305 struct mlxsw_sp_bridge_device *bridge_device;
2306 struct mlxsw_sp_bridge_port *bridge_port;
2309 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev,
2311 if (IS_ERR(bridge_port))
2312 return PTR_ERR(bridge_port);
2313 bridge_device = bridge_port->bridge_device;
2315 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2316 mlxsw_sp_port, extack);
2323 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2327 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2328 struct net_device *brport_dev,
2329 struct net_device *br_dev)
2331 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2332 struct mlxsw_sp_bridge_device *bridge_device;
2333 struct mlxsw_sp_bridge_port *bridge_port;
2335 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2338 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2342 bridge_device->ops->port_leave(bridge_device, bridge_port,
2344 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2347 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2348 const struct net_device *br_dev,
2349 const struct net_device *vxlan_dev, u16 vid,
2350 struct netlink_ext_ack *extack)
2352 struct mlxsw_sp_bridge_device *bridge_device;
2354 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2355 if (WARN_ON(!bridge_device))
2358 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2362 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2363 const struct net_device *vxlan_dev)
2365 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2366 struct mlxsw_sp_fid *fid;
2368 /* If the VxLAN device is down, then the FID does not have a VNI */
2369 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2373 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2374 /* Drop both the reference we just took during lookup and the reference
2375 * the VXLAN device took.
2377 mlxsw_sp_fid_put(fid);
2378 mlxsw_sp_fid_put(fid);
2382 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2383 enum mlxsw_sp_l3proto *proto,
2384 union mlxsw_sp_l3addr *addr)
2386 if (vxlan_addr->sa.sa_family == AF_INET) {
2387 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2388 *proto = MLXSW_SP_L3_PROTO_IPV4;
2390 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2391 *proto = MLXSW_SP_L3_PROTO_IPV6;
2396 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2397 const union mlxsw_sp_l3addr *addr,
2398 union vxlan_addr *vxlan_addr)
2401 case MLXSW_SP_L3_PROTO_IPV4:
2402 vxlan_addr->sa.sa_family = AF_INET;
2403 vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2405 case MLXSW_SP_L3_PROTO_IPV6:
2406 vxlan_addr->sa.sa_family = AF_INET6;
2407 vxlan_addr->sin6.sin6_addr = addr->addr6;
2412 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2414 enum mlxsw_sp_l3proto proto,
2415 union mlxsw_sp_l3addr *addr,
2416 __be32 vni, bool adding)
2418 struct switchdev_notifier_vxlan_fdb_info info;
2419 struct vxlan_dev *vxlan = netdev_priv(dev);
2420 enum switchdev_notifier_type type;
2422 type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2423 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2424 mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2425 info.remote_port = vxlan->cfg.dst_port;
2426 info.remote_vni = vni;
2427 info.remote_ifindex = 0;
2428 ether_addr_copy(info.eth_addr, mac);
2430 info.offloaded = adding;
2431 call_switchdev_notifiers(type, dev, &info.info, NULL);
2434 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2436 enum mlxsw_sp_l3proto proto,
2437 union mlxsw_sp_l3addr *addr,
2441 if (netif_is_vxlan(dev))
2442 mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2447 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2448 const char *mac, u16 vid,
2449 struct net_device *dev, bool offloaded)
2451 struct switchdev_notifier_fdb_info info;
2455 info.offloaded = offloaded;
2456 call_switchdev_notifiers(type, dev, &info.info, NULL);
2459 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2460 char *sfn_pl, int rec_index,
2463 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2464 struct mlxsw_sp_bridge_device *bridge_device;
2465 struct mlxsw_sp_bridge_port *bridge_port;
2466 struct mlxsw_sp_port *mlxsw_sp_port;
2467 enum switchdev_notifier_type type;
2471 bool do_notification = true;
2474 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2475 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2476 if (!mlxsw_sp_port) {
2477 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2481 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2484 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2485 if (!mlxsw_sp_port_vlan) {
2486 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2490 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2492 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2496 bridge_device = bridge_port->bridge_device;
2497 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2500 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2503 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2507 if (!do_notification)
2509 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2510 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2516 do_notification = false;
2520 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2521 char *sfn_pl, int rec_index,
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;
2533 bool do_notification = true;
2536 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2537 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2538 if (!mlxsw_sp_port) {
2539 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2543 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2546 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2547 if (!mlxsw_sp_port_vlan) {
2548 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2552 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2554 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2558 bridge_device = bridge_port->bridge_device;
2559 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2560 lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2561 mlxsw_sp_port_vlan->vid : 0;
2564 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
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;
2585 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2586 const struct mlxsw_sp_fid *fid,
2588 struct net_device **nve_dev,
2589 u16 *p_vid, __be32 *p_vni)
2591 struct mlxsw_sp_bridge_device *bridge_device;
2592 struct net_device *br_dev, *dev;
2596 err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2600 err = mlxsw_sp_fid_vni(fid, p_vni);
2604 dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
2609 if (!netif_running(dev))
2612 if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2615 if (adding && netif_is_vxlan(dev)) {
2616 struct vxlan_dev *vxlan = netdev_priv(dev);
2618 if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2622 br_dev = netdev_master_upper_dev_get(dev);
2626 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2630 *p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2635 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2640 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2641 enum switchdev_notifier_type type;
2642 struct net_device *nve_dev;
2643 union mlxsw_sp_l3addr addr;
2644 struct mlxsw_sp_fid *fid;
2651 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2654 fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2656 goto err_fid_lookup;
2658 err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2659 (enum mlxsw_sp_l3proto) sfn_proto,
2662 goto err_ip_resolve;
2664 err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2665 &nve_dev, &vid, &vni);
2667 goto err_fdb_process;
2669 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2670 (enum mlxsw_sp_l3proto) sfn_proto,
2671 &addr, adding, true);
2675 mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2676 (enum mlxsw_sp_l3proto) sfn_proto,
2677 &addr, vni, adding);
2679 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2680 SWITCHDEV_FDB_DEL_TO_BRIDGE;
2681 mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2683 mlxsw_sp_fid_put(fid);
2690 mlxsw_sp_fid_put(fid);
2692 /* Remove an FDB entry in case we cannot process it. Otherwise the
2693 * device will keep sending the same notification over and over again.
2695 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2696 (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2700 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2701 char *sfn_pl, int rec_index)
2703 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2704 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2705 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2708 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2709 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2712 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2713 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2716 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2717 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2720 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2721 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2724 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2725 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2731 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp,
2734 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2735 unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval;
2737 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2738 msecs_to_jiffies(interval));
2741 #define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10
2743 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2745 struct mlxsw_sp_bridge *bridge;
2746 struct mlxsw_sp *mlxsw_sp;
2753 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2757 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2758 mlxsw_sp = bridge->mlxsw_sp;
2761 queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION;
2762 while (queries > 0) {
2763 mlxsw_reg_sfn_pack(sfn_pl);
2764 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2766 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2769 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2770 for (i = 0; i < num_rec; i++)
2771 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2772 if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT)
2780 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries);
2783 struct mlxsw_sp_switchdev_event_work {
2784 struct work_struct work;
2786 struct switchdev_notifier_fdb_info fdb_info;
2787 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2789 struct net_device *dev;
2790 unsigned long event;
2794 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2795 struct mlxsw_sp_switchdev_event_work *
2797 struct mlxsw_sp_fid *fid, __be32 vni)
2799 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2800 struct switchdev_notifier_fdb_info *fdb_info;
2801 struct net_device *dev = switchdev_work->dev;
2802 enum mlxsw_sp_l3proto proto;
2803 union mlxsw_sp_l3addr addr;
2806 fdb_info = &switchdev_work->fdb_info;
2807 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2811 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2814 switch (switchdev_work->event) {
2815 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2816 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2817 vxlan_fdb_info.eth_addr,
2818 mlxsw_sp_fid_index(fid),
2819 proto, &addr, true, false);
2822 vxlan_fdb_info.offloaded = true;
2823 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2824 &vxlan_fdb_info.info, NULL);
2825 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2826 vxlan_fdb_info.eth_addr,
2827 fdb_info->vid, dev, true);
2829 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2830 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2831 vxlan_fdb_info.eth_addr,
2832 mlxsw_sp_fid_index(fid),
2833 proto, &addr, false,
2835 vxlan_fdb_info.offloaded = false;
2836 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2837 &vxlan_fdb_info.info, NULL);
2843 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2846 struct mlxsw_sp_bridge_device *bridge_device;
2847 struct net_device *dev = switchdev_work->dev;
2848 struct net_device *br_dev;
2849 struct mlxsw_sp *mlxsw_sp;
2850 struct mlxsw_sp_fid *fid;
2854 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2855 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2858 if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
2859 !switchdev_work->fdb_info.added_by_user)
2862 if (!netif_running(dev))
2864 br_dev = netdev_master_upper_dev_get(dev);
2867 if (!netif_is_bridge_master(br_dev))
2869 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2872 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2876 fid = bridge_device->ops->fid_lookup(bridge_device,
2877 switchdev_work->fdb_info.vid);
2881 err = mlxsw_sp_fid_vni(fid, &vni);
2885 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2889 mlxsw_sp_fid_put(fid);
2892 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2894 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2895 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2896 struct net_device *dev = switchdev_work->dev;
2897 struct switchdev_notifier_fdb_info *fdb_info;
2898 struct mlxsw_sp_port *mlxsw_sp_port;
2902 if (netif_is_vxlan(dev)) {
2903 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2907 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2911 switch (switchdev_work->event) {
2912 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2913 fdb_info = &switchdev_work->fdb_info;
2914 if (!fdb_info->added_by_user)
2916 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2919 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2921 fdb_info->vid, dev, true);
2923 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2924 fdb_info = &switchdev_work->fdb_info;
2925 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2927 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
2928 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2929 /* These events are only used to potentially update an existing
2935 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2939 kfree(switchdev_work->fdb_info.addr);
2940 kfree(switchdev_work);
2945 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
2946 struct mlxsw_sp_switchdev_event_work *
2949 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2950 struct mlxsw_sp_bridge_device *bridge_device;
2951 struct net_device *dev = switchdev_work->dev;
2952 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2953 enum mlxsw_sp_l3proto proto;
2954 union mlxsw_sp_l3addr addr;
2955 struct net_device *br_dev;
2956 struct mlxsw_sp_fid *fid;
2960 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2961 br_dev = netdev_master_upper_dev_get(dev);
2963 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2967 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2971 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2974 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2975 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
2977 mlxsw_sp_fid_put(fid);
2980 vxlan_fdb_info->offloaded = true;
2981 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2982 &vxlan_fdb_info->info, NULL);
2983 mlxsw_sp_fid_put(fid);
2987 /* The device has a single FDB table, whereas Linux has two - one
2988 * in the bridge driver and another in the VxLAN driver. We only
2989 * program an entry to the device if the MAC points to the VxLAN
2990 * device in the bridge's FDB table
2992 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2993 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
2994 goto err_br_fdb_find;
2996 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2997 mlxsw_sp_fid_index(fid), proto,
2998 &addr, true, false);
3000 goto err_fdb_tunnel_uc_op;
3001 vxlan_fdb_info->offloaded = true;
3002 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3003 &vxlan_fdb_info->info, NULL);
3004 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3005 vxlan_fdb_info->eth_addr, vid, dev, true);
3007 mlxsw_sp_fid_put(fid);
3011 err_fdb_tunnel_uc_op:
3013 mlxsw_sp_fid_put(fid);
3017 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
3018 struct mlxsw_sp_switchdev_event_work *
3021 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3022 struct mlxsw_sp_bridge_device *bridge_device;
3023 struct net_device *dev = switchdev_work->dev;
3024 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3025 u8 all_zeros_mac[ETH_ALEN] = { 0 };
3026 enum mlxsw_sp_l3proto proto;
3027 union mlxsw_sp_l3addr addr;
3028 struct mlxsw_sp_fid *fid;
3031 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3033 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3037 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3041 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3044 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3045 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3046 mlxsw_sp_fid_put(fid);
3050 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3051 mlxsw_sp_fid_index(fid), proto, &addr,
3053 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3054 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3055 vxlan_fdb_info->eth_addr, vid, dev, false);
3057 mlxsw_sp_fid_put(fid);
3060 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3062 struct mlxsw_sp_switchdev_event_work *switchdev_work =
3063 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3064 struct net_device *dev = switchdev_work->dev;
3065 struct mlxsw_sp *mlxsw_sp;
3066 struct net_device *br_dev;
3070 if (!netif_running(dev))
3072 br_dev = netdev_master_upper_dev_get(dev);
3075 if (!netif_is_bridge_master(br_dev))
3077 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3081 switch (switchdev_work->event) {
3082 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3083 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3085 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3086 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3092 kfree(switchdev_work);
3097 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3099 struct switchdev_notifier_info *info)
3101 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3102 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3103 struct vxlan_config *cfg = &vxlan->cfg;
3104 struct netlink_ext_ack *extack;
3106 extack = switchdev_notifier_info_to_extack(info);
3107 vxlan_fdb_info = container_of(info,
3108 struct switchdev_notifier_vxlan_fdb_info,
3111 if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3112 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3115 if (vxlan_fdb_info->remote_vni != cfg->vni ||
3116 vxlan_fdb_info->vni != cfg->vni) {
3117 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3120 if (vxlan_fdb_info->remote_ifindex) {
3121 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3124 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3125 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3128 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3129 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3133 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3138 /* Called under rcu_read_lock() */
3139 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3140 unsigned long event, void *ptr)
3142 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3143 struct mlxsw_sp_switchdev_event_work *switchdev_work;
3144 struct switchdev_notifier_fdb_info *fdb_info;
3145 struct switchdev_notifier_info *info = ptr;
3146 struct net_device *br_dev;
3149 if (event == SWITCHDEV_PORT_ATTR_SET) {
3150 err = switchdev_handle_port_attr_set(dev, ptr,
3151 mlxsw_sp_port_dev_check,
3152 mlxsw_sp_port_attr_set);
3153 return notifier_from_errno(err);
3156 /* Tunnel devices are not our uppers, so check their master instead */
3157 br_dev = netdev_master_upper_dev_get_rcu(dev);
3160 if (!netif_is_bridge_master(br_dev))
3162 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3165 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3166 if (!switchdev_work)
3169 switchdev_work->dev = dev;
3170 switchdev_work->event = event;
3173 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3174 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3175 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3176 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3177 fdb_info = container_of(info,
3178 struct switchdev_notifier_fdb_info,
3180 INIT_WORK(&switchdev_work->work,
3181 mlxsw_sp_switchdev_bridge_fdb_event_work);
3182 memcpy(&switchdev_work->fdb_info, ptr,
3183 sizeof(switchdev_work->fdb_info));
3184 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3185 if (!switchdev_work->fdb_info.addr)
3186 goto err_addr_alloc;
3187 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3189 /* Take a reference on the device. This can be either
3190 * upper device containig mlxsw_sp_port or just a
3195 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3196 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3197 INIT_WORK(&switchdev_work->work,
3198 mlxsw_sp_switchdev_vxlan_fdb_event_work);
3199 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3202 goto err_vxlan_work_prepare;
3206 kfree(switchdev_work);
3210 mlxsw_core_schedule_work(&switchdev_work->work);
3214 err_vxlan_work_prepare:
3216 kfree(switchdev_work);
3220 struct notifier_block mlxsw_sp_switchdev_notifier = {
3221 .notifier_call = mlxsw_sp_switchdev_event,
3225 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3226 struct mlxsw_sp_bridge_device *bridge_device,
3227 const struct net_device *vxlan_dev, u16 vid,
3228 bool flag_untagged, bool flag_pvid,
3229 struct netlink_ext_ack *extack)
3231 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3232 __be32 vni = vxlan->cfg.vni;
3233 struct mlxsw_sp_fid *fid;
3237 /* We cannot have the same VLAN as PVID and egress untagged on multiple
3238 * VxLAN devices. Note that we get this notification before the VLAN is
3239 * actually added to the bridge's database, so it is not possible for
3240 * the lookup function to return 'vxlan_dev'
3242 if (flag_untagged && flag_pvid &&
3243 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3244 NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3248 if (!netif_running(vxlan_dev))
3251 /* First case: FID is not associated with this VNI, but the new VLAN
3252 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3255 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3257 if (!flag_untagged || !flag_pvid)
3259 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev,
3263 /* Second case: FID is associated with the VNI and the VLAN associated
3264 * with the FID is the same as the notified VLAN. This means the flags
3265 * (PVID / egress untagged) were toggled and that NVE should be
3266 * disabled on the FID
3268 old_vid = mlxsw_sp_fid_8021q_vid(fid);
3269 if (vid == old_vid) {
3270 if (WARN_ON(flag_untagged && flag_pvid)) {
3271 mlxsw_sp_fid_put(fid);
3274 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3275 mlxsw_sp_fid_put(fid);
3279 /* Third case: A new VLAN was configured on the VxLAN device, but this
3280 * VLAN is not PVID, so there is nothing to do.
3283 mlxsw_sp_fid_put(fid);
3287 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3288 * mapped to the VNI should be unmapped
3290 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3291 mlxsw_sp_fid_put(fid);
3293 /* Fifth case: The new VLAN is also egress untagged, which means the
3294 * VLAN needs to be mapped to the VNI
3299 err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack);
3301 goto err_vxlan_join;
3306 bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL);
3311 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3312 struct mlxsw_sp_bridge_device *bridge_device,
3313 const struct net_device *vxlan_dev, u16 vid)
3315 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3316 __be32 vni = vxlan->cfg.vni;
3317 struct mlxsw_sp_fid *fid;
3319 if (!netif_running(vxlan_dev))
3322 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3326 /* A different VLAN than the one mapped to the VNI is deleted */
3327 if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3330 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3333 mlxsw_sp_fid_put(fid);
3337 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3338 struct switchdev_notifier_port_obj_info *
3341 struct switchdev_obj_port_vlan *vlan =
3342 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3343 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3344 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3345 struct mlxsw_sp_bridge_device *bridge_device;
3346 struct netlink_ext_ack *extack;
3347 struct mlxsw_sp *mlxsw_sp;
3348 struct net_device *br_dev;
3350 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3351 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3355 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3359 port_obj_info->handled = true;
3361 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3365 if (!bridge_device->vlan_enabled)
3368 return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3369 vxlan_dev, vlan->vid,
3375 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3376 struct switchdev_notifier_port_obj_info *
3379 struct switchdev_obj_port_vlan *vlan =
3380 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3381 struct mlxsw_sp_bridge_device *bridge_device;
3382 struct mlxsw_sp *mlxsw_sp;
3383 struct net_device *br_dev;
3385 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3389 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3393 port_obj_info->handled = true;
3395 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3399 if (!bridge_device->vlan_enabled)
3402 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev,
3407 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3408 struct switchdev_notifier_port_obj_info *
3413 switch (port_obj_info->obj->id) {
3414 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3415 err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3426 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3427 struct switchdev_notifier_port_obj_info *
3430 switch (port_obj_info->obj->id) {
3431 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3432 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3439 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3440 unsigned long event, void *ptr)
3442 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3446 case SWITCHDEV_PORT_OBJ_ADD:
3447 if (netif_is_vxlan(dev))
3448 err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3450 err = switchdev_handle_port_obj_add(dev, ptr,
3451 mlxsw_sp_port_dev_check,
3452 mlxsw_sp_port_obj_add);
3453 return notifier_from_errno(err);
3454 case SWITCHDEV_PORT_OBJ_DEL:
3455 if (netif_is_vxlan(dev))
3456 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3458 err = switchdev_handle_port_obj_del(dev, ptr,
3459 mlxsw_sp_port_dev_check,
3460 mlxsw_sp_port_obj_del);
3461 return notifier_from_errno(err);
3462 case SWITCHDEV_PORT_ATTR_SET:
3463 err = switchdev_handle_port_attr_set(dev, ptr,
3464 mlxsw_sp_port_dev_check,
3465 mlxsw_sp_port_attr_set);
3466 return notifier_from_errno(err);
3472 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3473 .notifier_call = mlxsw_sp_switchdev_blocking_event,
3477 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3479 return bridge_port->stp_state;
3482 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3484 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3485 struct notifier_block *nb;
3488 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3490 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3494 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3496 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3500 nb = &mlxsw_sp_switchdev_blocking_notifier;
3501 err = register_switchdev_blocking_notifier(nb);
3503 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3504 goto err_register_switchdev_blocking_notifier;
3507 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3508 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3509 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false);
3512 err_register_switchdev_blocking_notifier:
3513 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3517 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3519 struct notifier_block *nb;
3521 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3523 nb = &mlxsw_sp_switchdev_blocking_notifier;
3524 unregister_switchdev_blocking_notifier(nb);
3526 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3529 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3531 struct mlxsw_sp_bridge *bridge;
3533 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3536 mlxsw_sp->bridge = bridge;
3537 bridge->mlxsw_sp = mlxsw_sp;
3539 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3541 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3542 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3543 bridge->bridge_8021ad_ops = &mlxsw_sp_bridge_8021ad_ops;
3545 return mlxsw_sp_fdb_init(mlxsw_sp);
3548 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3550 mlxsw_sp_fdb_fini(mlxsw_sp);
3551 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3552 kfree(mlxsw_sp->bridge);