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,
89 struct netlink_ext_ack *extack);
90 void (*vxlan_leave)(struct mlxsw_sp_bridge_device *bridge_device,
91 const struct net_device *vxlan_dev);
93 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
96 (*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
98 u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
99 const struct mlxsw_sp_fid *fid);
103 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
104 struct mlxsw_sp_bridge_port *bridge_port,
108 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
109 struct mlxsw_sp_bridge_port *bridge_port);
112 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
113 struct mlxsw_sp_bridge_device
117 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
118 struct mlxsw_sp_bridge_port *bridge_port,
121 static struct mlxsw_sp_bridge_device *
122 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
123 const struct net_device *br_dev)
125 struct mlxsw_sp_bridge_device *bridge_device;
127 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
128 if (bridge_device->dev == br_dev)
129 return bridge_device;
134 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
135 const struct net_device *br_dev)
137 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
140 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
143 struct mlxsw_sp *mlxsw_sp = data;
145 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
149 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
150 struct net_device *dev)
152 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
153 netdev_walk_all_upper_dev_rcu(dev,
154 mlxsw_sp_bridge_device_upper_rif_destroy,
158 static struct mlxsw_sp_bridge_device *
159 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
160 struct net_device *br_dev)
162 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
163 struct mlxsw_sp_bridge_device *bridge_device;
164 bool vlan_enabled = br_vlan_enabled(br_dev);
166 if (vlan_enabled && bridge->vlan_enabled_exists) {
167 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
168 return ERR_PTR(-EINVAL);
171 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
173 return ERR_PTR(-ENOMEM);
175 bridge_device->dev = br_dev;
176 bridge_device->vlan_enabled = vlan_enabled;
177 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
178 bridge_device->mrouter = br_multicast_router(br_dev);
179 INIT_LIST_HEAD(&bridge_device->ports_list);
181 bridge->vlan_enabled_exists = true;
182 bridge_device->ops = bridge->bridge_8021q_ops;
184 bridge_device->ops = bridge->bridge_8021d_ops;
186 INIT_LIST_HEAD(&bridge_device->mids_list);
187 list_add(&bridge_device->list, &bridge->bridges_list);
189 return bridge_device;
193 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
194 struct mlxsw_sp_bridge_device *bridge_device)
196 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
198 list_del(&bridge_device->list);
199 if (bridge_device->vlan_enabled)
200 bridge->vlan_enabled_exists = false;
201 WARN_ON(!list_empty(&bridge_device->ports_list));
202 WARN_ON(!list_empty(&bridge_device->mids_list));
203 kfree(bridge_device);
206 static struct mlxsw_sp_bridge_device *
207 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
208 struct net_device *br_dev)
210 struct mlxsw_sp_bridge_device *bridge_device;
212 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
214 return bridge_device;
216 return mlxsw_sp_bridge_device_create(bridge, br_dev);
220 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
221 struct mlxsw_sp_bridge_device *bridge_device)
223 if (list_empty(&bridge_device->ports_list))
224 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
227 static struct mlxsw_sp_bridge_port *
228 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
229 const struct net_device *brport_dev)
231 struct mlxsw_sp_bridge_port *bridge_port;
233 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
234 if (bridge_port->dev == brport_dev)
241 struct mlxsw_sp_bridge_port *
242 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
243 struct net_device *brport_dev)
245 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
246 struct mlxsw_sp_bridge_device *bridge_device;
251 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
255 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
258 static struct mlxsw_sp_bridge_port *
259 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
260 struct net_device *brport_dev)
262 struct mlxsw_sp_bridge_port *bridge_port;
263 struct mlxsw_sp_port *mlxsw_sp_port;
265 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
269 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
270 bridge_port->lagged = mlxsw_sp_port->lagged;
271 if (bridge_port->lagged)
272 bridge_port->lag_id = mlxsw_sp_port->lag_id;
274 bridge_port->system_port = mlxsw_sp_port->local_port;
275 bridge_port->dev = brport_dev;
276 bridge_port->bridge_device = bridge_device;
277 bridge_port->stp_state = BR_STATE_DISABLED;
278 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
280 INIT_LIST_HEAD(&bridge_port->vlans_list);
281 list_add(&bridge_port->list, &bridge_device->ports_list);
282 bridge_port->ref_count = 1;
288 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
290 list_del(&bridge_port->list);
291 WARN_ON(!list_empty(&bridge_port->vlans_list));
296 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
299 struct net_device *dev = bridge_port->dev;
300 struct mlxsw_sp *mlxsw_sp;
302 if (is_vlan_dev(dev))
303 mlxsw_sp = mlxsw_sp_lower_get(vlan_dev_real_dev(dev));
305 mlxsw_sp = mlxsw_sp_lower_get(dev);
307 /* In case ports were pulled from out of a bridged LAG, then
308 * it's possible the reference count isn't zero, yet the bridge
309 * port should be destroyed, as it's no longer an upper of ours.
311 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
313 else if (bridge_port->ref_count == 0)
319 static struct mlxsw_sp_bridge_port *
320 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
321 struct net_device *brport_dev)
323 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
324 struct mlxsw_sp_bridge_device *bridge_device;
325 struct mlxsw_sp_bridge_port *bridge_port;
328 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
330 bridge_port->ref_count++;
334 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
335 if (IS_ERR(bridge_device))
336 return ERR_CAST(bridge_device);
338 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
341 goto err_bridge_port_create;
346 err_bridge_port_create:
347 mlxsw_sp_bridge_device_put(bridge, bridge_device);
351 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
352 struct mlxsw_sp_bridge_port *bridge_port)
354 struct mlxsw_sp_bridge_device *bridge_device;
356 bridge_port->ref_count--;
357 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
359 bridge_device = bridge_port->bridge_device;
360 mlxsw_sp_bridge_port_destroy(bridge_port);
361 mlxsw_sp_bridge_device_put(bridge, bridge_device);
364 static struct mlxsw_sp_port_vlan *
365 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
366 const struct mlxsw_sp_bridge_device *
370 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
372 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
374 if (!mlxsw_sp_port_vlan->bridge_port)
376 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
379 if (bridge_device->vlan_enabled &&
380 mlxsw_sp_port_vlan->vid != vid)
382 return mlxsw_sp_port_vlan;
388 static struct mlxsw_sp_port_vlan*
389 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
392 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
394 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
396 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
398 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
399 return mlxsw_sp_port_vlan;
405 static struct mlxsw_sp_bridge_vlan *
406 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
409 struct mlxsw_sp_bridge_vlan *bridge_vlan;
411 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
412 if (bridge_vlan->vid == vid)
419 static struct mlxsw_sp_bridge_vlan *
420 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
422 struct mlxsw_sp_bridge_vlan *bridge_vlan;
424 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
428 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
429 bridge_vlan->vid = vid;
430 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
436 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
438 list_del(&bridge_vlan->list);
439 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
443 static struct mlxsw_sp_bridge_vlan *
444 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
446 struct mlxsw_sp_bridge_vlan *bridge_vlan;
448 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
452 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
455 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
457 if (list_empty(&bridge_vlan->port_vlan_list))
458 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
461 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
462 struct net_device *dev,
463 unsigned long *brport_flags)
465 struct mlxsw_sp_bridge_port *bridge_port;
467 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
468 if (WARN_ON(!bridge_port))
471 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
474 static int mlxsw_sp_port_attr_get(struct net_device *dev,
475 struct switchdev_attr *attr)
477 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
478 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
481 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
482 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
483 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
484 attr->u.ppid.id_len);
486 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
487 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
488 &attr->u.brport_flags);
490 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
491 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
502 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
503 struct mlxsw_sp_bridge_vlan *bridge_vlan,
506 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
508 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
510 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
512 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
513 bridge_vlan->vid, state);
519 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
520 struct switchdev_trans *trans,
521 struct net_device *orig_dev,
524 struct mlxsw_sp_bridge_port *bridge_port;
525 struct mlxsw_sp_bridge_vlan *bridge_vlan;
528 if (switchdev_trans_ph_prepare(trans))
531 /* It's possible we failed to enslave the port, yet this
532 * operation is executed due to it being deferred.
534 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
539 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
540 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
543 goto err_port_bridge_vlan_stp_set;
546 bridge_port->stp_state = state;
550 err_port_bridge_vlan_stp_set:
551 list_for_each_entry_continue_reverse(bridge_vlan,
552 &bridge_port->vlans_list, list)
553 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
554 bridge_port->stp_state);
559 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
560 struct mlxsw_sp_bridge_vlan *bridge_vlan,
561 enum mlxsw_sp_flood_type packet_type,
564 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
566 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
568 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
570 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
572 mlxsw_sp_port->local_port,
580 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
581 struct mlxsw_sp_bridge_port *bridge_port,
582 enum mlxsw_sp_flood_type packet_type,
585 struct mlxsw_sp_bridge_vlan *bridge_vlan;
588 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
589 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
594 goto err_port_bridge_vlan_flood_set;
599 err_port_bridge_vlan_flood_set:
600 list_for_each_entry_continue_reverse(bridge_vlan,
601 &bridge_port->vlans_list, list)
602 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
603 packet_type, !member);
608 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
609 struct mlxsw_sp_bridge_vlan *bridge_vlan,
612 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
613 u16 vid = bridge_vlan->vid;
615 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
617 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
619 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
626 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
627 struct mlxsw_sp_bridge_port *bridge_port,
630 struct mlxsw_sp_bridge_vlan *bridge_vlan;
633 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
634 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
637 goto err_port_bridge_vlan_learning_set;
642 err_port_bridge_vlan_learning_set:
643 list_for_each_entry_continue_reverse(bridge_vlan,
644 &bridge_port->vlans_list, list)
645 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
650 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
651 struct switchdev_trans *trans,
652 struct net_device *orig_dev,
653 unsigned long brport_flags)
655 struct mlxsw_sp_bridge_port *bridge_port;
658 if (switchdev_trans_ph_prepare(trans))
661 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
666 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
667 MLXSW_SP_FLOOD_TYPE_UC,
668 brport_flags & BR_FLOOD);
672 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
673 brport_flags & BR_LEARNING);
677 if (bridge_port->bridge_device->multicast_enabled)
680 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
681 MLXSW_SP_FLOOD_TYPE_MC,
688 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
692 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
694 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
697 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
698 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
701 mlxsw_sp->bridge->ageing_time = ageing_time;
705 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
706 struct switchdev_trans *trans,
707 unsigned long ageing_clock_t)
709 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
710 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
711 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
713 if (switchdev_trans_ph_prepare(trans)) {
714 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
715 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
721 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
724 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
725 struct switchdev_trans *trans,
726 struct net_device *orig_dev,
729 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
730 struct mlxsw_sp_bridge_device *bridge_device;
732 if (!switchdev_trans_ph_prepare(trans))
735 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
736 if (WARN_ON(!bridge_device))
739 if (bridge_device->vlan_enabled == vlan_enabled)
742 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
746 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
747 struct switchdev_trans *trans,
748 struct net_device *orig_dev,
749 bool is_port_mrouter)
751 struct mlxsw_sp_bridge_port *bridge_port;
754 if (switchdev_trans_ph_prepare(trans))
757 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
762 if (!bridge_port->bridge_device->multicast_enabled)
765 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
766 MLXSW_SP_FLOOD_TYPE_MC,
771 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
774 bridge_port->mrouter = is_port_mrouter;
778 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
780 const struct mlxsw_sp_bridge_device *bridge_device;
782 bridge_device = bridge_port->bridge_device;
783 return bridge_device->multicast_enabled ? bridge_port->mrouter :
784 bridge_port->flags & BR_MCAST_FLOOD;
787 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
788 struct switchdev_trans *trans,
789 struct net_device *orig_dev,
792 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
793 struct mlxsw_sp_bridge_device *bridge_device;
794 struct mlxsw_sp_bridge_port *bridge_port;
797 if (switchdev_trans_ph_prepare(trans))
800 /* It's possible we failed to enslave the port, yet this
801 * operation is executed due to it being deferred.
803 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
807 if (bridge_device->multicast_enabled != !mc_disabled) {
808 bridge_device->multicast_enabled = !mc_disabled;
809 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
813 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
814 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
815 bool member = mlxsw_sp_mc_flood(bridge_port);
817 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
819 packet_type, member);
824 bridge_device->multicast_enabled = !mc_disabled;
829 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
830 u16 mid_idx, bool add)
835 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
839 mlxsw_reg_smid_pack(smid_pl, mid_idx,
840 mlxsw_sp_router_port(mlxsw_sp), add);
841 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
847 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
848 struct mlxsw_sp_bridge_device *bridge_device,
851 struct mlxsw_sp_mid *mid;
853 list_for_each_entry(mid, &bridge_device->mids_list, list)
854 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
858 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
859 struct switchdev_trans *trans,
860 struct net_device *orig_dev,
863 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
864 struct mlxsw_sp_bridge_device *bridge_device;
866 if (switchdev_trans_ph_prepare(trans))
869 /* It's possible we failed to enslave the port, yet this
870 * operation is executed due to it being deferred.
872 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
876 if (bridge_device->mrouter != is_mrouter)
877 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
879 bridge_device->mrouter = is_mrouter;
883 static int mlxsw_sp_port_attr_set(struct net_device *dev,
884 const struct switchdev_attr *attr,
885 struct switchdev_trans *trans)
887 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
891 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
892 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
896 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
897 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
899 attr->u.brport_flags);
901 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
902 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
903 attr->u.ageing_time);
905 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
906 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
908 attr->u.vlan_filtering);
910 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
911 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
915 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
916 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
918 attr->u.mc_disabled);
920 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
921 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
930 if (switchdev_trans_ph_commit(trans))
931 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
937 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
938 struct mlxsw_sp_bridge_port *bridge_port)
940 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
941 struct mlxsw_sp_bridge_device *bridge_device;
942 u8 local_port = mlxsw_sp_port->local_port;
943 u16 vid = mlxsw_sp_port_vlan->vid;
944 struct mlxsw_sp_fid *fid;
947 bridge_device = bridge_port->bridge_device;
948 fid = bridge_device->ops->fid_get(bridge_device, vid);
952 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
953 bridge_port->flags & BR_FLOOD);
955 goto err_fid_uc_flood_set;
957 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
958 mlxsw_sp_mc_flood(bridge_port));
960 goto err_fid_mc_flood_set;
962 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
965 goto err_fid_bc_flood_set;
967 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
969 goto err_fid_port_vid_map;
971 mlxsw_sp_port_vlan->fid = fid;
975 err_fid_port_vid_map:
976 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
977 err_fid_bc_flood_set:
978 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
979 err_fid_mc_flood_set:
980 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
981 err_fid_uc_flood_set:
982 mlxsw_sp_fid_put(fid);
987 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
989 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
990 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
991 u8 local_port = mlxsw_sp_port->local_port;
992 u16 vid = mlxsw_sp_port_vlan->vid;
994 mlxsw_sp_port_vlan->fid = NULL;
995 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
996 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
997 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
998 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
999 mlxsw_sp_fid_put(fid);
1003 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1004 u16 vid, bool is_pvid)
1008 else if (mlxsw_sp_port->pvid == vid)
1009 return 0; /* Dis-allow untagged packets */
1011 return mlxsw_sp_port->pvid;
1015 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1016 struct mlxsw_sp_bridge_port *bridge_port)
1018 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1019 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1020 u16 vid = mlxsw_sp_port_vlan->vid;
1023 /* No need to continue if only VLAN flags were changed */
1024 if (mlxsw_sp_port_vlan->bridge_port) {
1025 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1029 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1033 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1034 bridge_port->flags & BR_LEARNING);
1036 goto err_port_vid_learning_set;
1038 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1039 bridge_port->stp_state);
1041 goto err_port_vid_stp_set;
1043 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1046 goto err_bridge_vlan_get;
1049 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1050 &bridge_vlan->port_vlan_list);
1052 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1054 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1058 err_bridge_vlan_get:
1059 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1060 err_port_vid_stp_set:
1061 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1062 err_port_vid_learning_set:
1063 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1068 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1070 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1071 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1072 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1073 struct mlxsw_sp_bridge_port *bridge_port;
1074 u16 vid = mlxsw_sp_port_vlan->vid;
1075 bool last_port, last_vlan;
1077 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1078 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1081 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1082 last_vlan = list_is_singular(&bridge_port->vlans_list);
1083 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1084 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1086 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1087 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1088 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1089 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1091 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1093 mlxsw_sp_fid_index(fid));
1095 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1097 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1099 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1100 mlxsw_sp_port_vlan->bridge_port = NULL;
1104 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1105 struct mlxsw_sp_bridge_port *bridge_port,
1106 u16 vid, bool is_untagged, bool is_pvid)
1108 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1109 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1110 u16 old_pvid = mlxsw_sp_port->pvid;
1113 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1114 if (IS_ERR(mlxsw_sp_port_vlan))
1115 return PTR_ERR(mlxsw_sp_port_vlan);
1117 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1120 goto err_port_vlan_set;
1122 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1124 goto err_port_pvid_set;
1126 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1128 goto err_port_vlan_bridge_join;
1132 err_port_vlan_bridge_join:
1133 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1135 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1137 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1142 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1143 const struct net_device *br_dev,
1144 const struct switchdev_obj_port_vlan *vlan)
1146 struct mlxsw_sp_rif *rif;
1147 struct mlxsw_sp_fid *fid;
1151 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev);
1154 fid = mlxsw_sp_rif_fid(rif);
1155 pvid = mlxsw_sp_fid_8021q_vid(fid);
1157 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1158 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1160 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1165 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1174 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1175 const struct switchdev_obj_port_vlan *vlan,
1176 struct switchdev_trans *trans)
1178 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1179 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1180 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1181 struct net_device *orig_dev = vlan->obj.orig_dev;
1182 struct mlxsw_sp_bridge_port *bridge_port;
1185 if (netif_is_bridge_master(orig_dev)) {
1188 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1189 br_vlan_enabled(orig_dev) &&
1190 switchdev_trans_ph_prepare(trans))
1191 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1198 if (switchdev_trans_ph_prepare(trans))
1201 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1202 if (WARN_ON(!bridge_port))
1205 if (!bridge_port->bridge_device->vlan_enabled)
1208 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1211 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1221 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1223 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1224 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1228 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1229 struct mlxsw_sp_bridge_port *bridge_port,
1232 bool lagged = bridge_port->lagged;
1233 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1236 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1237 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1238 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1239 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1241 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1244 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1246 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1247 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1250 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1252 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1253 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1256 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1257 const char *mac, u16 fid,
1258 enum mlxsw_sp_l3proto proto,
1259 const union mlxsw_sp_l3addr *addr,
1260 bool adding, bool dynamic)
1262 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1269 case MLXSW_SP_L3_PROTO_IPV4:
1270 uip = be32_to_cpu(addr->addr4);
1271 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1273 case MLXSW_SP_L3_PROTO_IPV6: /* fall through */
1279 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1283 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1284 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1285 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1286 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1288 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1289 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1293 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1301 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1302 const char *mac, u16 fid, bool adding,
1303 enum mlxsw_reg_sfd_rec_action action,
1310 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1314 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1315 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1316 mac, fid, action, local_port);
1317 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1318 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1322 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1330 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1331 const char *mac, u16 fid, bool adding,
1334 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1335 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1338 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1341 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1342 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1346 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1347 const char *mac, u16 fid, u16 lag_vid,
1348 bool adding, bool dynamic)
1354 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1358 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1359 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1360 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1362 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1363 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1367 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1376 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1377 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1379 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1380 struct net_device *orig_dev = fdb_info->info.dev;
1381 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1382 struct mlxsw_sp_bridge_device *bridge_device;
1383 struct mlxsw_sp_bridge_port *bridge_port;
1386 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1390 bridge_device = bridge_port->bridge_device;
1391 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1394 if (!mlxsw_sp_port_vlan)
1397 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1398 vid = mlxsw_sp_port_vlan->vid;
1400 if (!bridge_port->lagged)
1401 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1402 bridge_port->system_port,
1403 fdb_info->addr, fid_index,
1406 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1407 bridge_port->lag_id,
1408 fdb_info->addr, fid_index,
1409 vid, adding, false);
1412 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1413 u16 fid, u16 mid_idx, bool adding)
1419 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1423 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1424 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1425 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1426 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1427 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1431 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1439 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1441 bool set_router_port)
1446 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1450 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1451 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1452 if (mlxsw_sp->ports[i])
1453 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1456 mlxsw_reg_smid_port_mask_set(smid_pl,
1457 mlxsw_sp_router_port(mlxsw_sp), 1);
1459 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1460 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1462 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1465 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1470 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1471 u16 mid_idx, bool add)
1473 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1477 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1481 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1482 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1488 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1489 const unsigned char *addr,
1492 struct mlxsw_sp_mid *mid;
1494 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1495 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1502 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1503 struct mlxsw_sp_bridge_port *bridge_port,
1504 unsigned long *ports_bitmap)
1506 struct mlxsw_sp_port *mlxsw_sp_port;
1507 u64 max_lag_members, i;
1510 if (!bridge_port->lagged) {
1511 set_bit(bridge_port->system_port, ports_bitmap);
1513 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1515 lag_id = bridge_port->lag_id;
1516 for (i = 0; i < max_lag_members; i++) {
1517 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1520 set_bit(mlxsw_sp_port->local_port,
1527 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1528 struct mlxsw_sp_bridge_device *bridge_device,
1529 struct mlxsw_sp *mlxsw_sp)
1531 struct mlxsw_sp_bridge_port *bridge_port;
1533 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1534 if (bridge_port->mrouter) {
1535 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1543 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1544 struct mlxsw_sp_mid *mid,
1545 struct mlxsw_sp_bridge_device *bridge_device)
1553 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1555 if (mid_idx == MLXSW_SP_MID_MAX)
1558 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1559 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1560 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1564 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1565 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1568 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1569 bridge_device->mrouter);
1570 kfree(flood_bitmap);
1574 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1579 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1584 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1585 struct mlxsw_sp_mid *mid)
1590 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1592 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1597 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1598 struct mlxsw_sp_bridge_device *bridge_device,
1599 const unsigned char *addr,
1602 struct mlxsw_sp_mid *mid;
1605 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1609 alloc_size = sizeof(unsigned long) *
1610 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1612 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1613 if (!mid->ports_in_mid)
1614 goto err_ports_in_mid_alloc;
1616 ether_addr_copy(mid->addr, addr);
1620 if (!bridge_device->multicast_enabled)
1623 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1624 goto err_write_mdb_entry;
1627 list_add_tail(&mid->list, &bridge_device->mids_list);
1630 err_write_mdb_entry:
1631 kfree(mid->ports_in_mid);
1632 err_ports_in_mid_alloc:
1637 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1638 struct mlxsw_sp_mid *mid)
1640 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1643 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1644 if (bitmap_empty(mid->ports_in_mid,
1645 mlxsw_core_max_ports(mlxsw_sp->core))) {
1646 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1647 list_del(&mid->list);
1648 kfree(mid->ports_in_mid);
1654 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1655 const struct switchdev_obj_port_mdb *mdb,
1656 struct switchdev_trans *trans)
1658 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1659 struct net_device *orig_dev = mdb->obj.orig_dev;
1660 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1661 struct net_device *dev = mlxsw_sp_port->dev;
1662 struct mlxsw_sp_bridge_device *bridge_device;
1663 struct mlxsw_sp_bridge_port *bridge_port;
1664 struct mlxsw_sp_mid *mid;
1668 if (switchdev_trans_ph_prepare(trans))
1671 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1675 bridge_device = bridge_port->bridge_device;
1676 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1679 if (!mlxsw_sp_port_vlan)
1682 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1684 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1686 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1689 netdev_err(dev, "Unable to allocate MC group\n");
1693 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1695 if (!bridge_device->multicast_enabled)
1698 if (bridge_port->mrouter)
1701 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1703 netdev_err(dev, "Unable to set SMID\n");
1710 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1715 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1716 struct mlxsw_sp_bridge_device
1719 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1720 struct mlxsw_sp_mid *mid;
1723 mc_enabled = bridge_device->multicast_enabled;
1725 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1727 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1730 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1735 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1736 struct mlxsw_sp_bridge_port *bridge_port,
1739 struct mlxsw_sp_bridge_device *bridge_device;
1740 struct mlxsw_sp_mid *mid;
1742 bridge_device = bridge_port->bridge_device;
1744 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1745 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1746 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1750 struct mlxsw_sp_span_respin_work {
1751 struct work_struct work;
1752 struct mlxsw_sp *mlxsw_sp;
1755 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1757 struct mlxsw_sp_span_respin_work *respin_work =
1758 container_of(work, struct mlxsw_sp_span_respin_work, work);
1761 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1766 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1768 struct mlxsw_sp_span_respin_work *respin_work;
1770 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1774 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1775 respin_work->mlxsw_sp = mlxsw_sp;
1777 mlxsw_core_schedule_work(&respin_work->work);
1780 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1781 const struct switchdev_obj *obj,
1782 struct switchdev_trans *trans)
1784 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1785 const struct switchdev_obj_port_vlan *vlan;
1789 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1790 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1791 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1793 if (switchdev_trans_ph_prepare(trans)) {
1794 /* The event is emitted before the changes are actually
1795 * applied to the bridge. Therefore schedule the respin
1796 * call for later, so that the respin logic sees the
1797 * updated bridge state.
1799 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1802 case SWITCHDEV_OBJ_ID_PORT_MDB:
1803 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1804 SWITCHDEV_OBJ_PORT_MDB(obj),
1816 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1817 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1819 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1820 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1822 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1823 if (WARN_ON(!mlxsw_sp_port_vlan))
1826 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1827 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1828 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1829 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1832 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1833 const struct switchdev_obj_port_vlan *vlan)
1835 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1836 struct net_device *orig_dev = vlan->obj.orig_dev;
1837 struct mlxsw_sp_bridge_port *bridge_port;
1840 if (netif_is_bridge_master(orig_dev))
1843 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1844 if (WARN_ON(!bridge_port))
1847 if (!bridge_port->bridge_device->vlan_enabled)
1850 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1851 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1857 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1858 struct mlxsw_sp_bridge_port *bridge_port,
1859 struct mlxsw_sp_mid *mid)
1861 struct net_device *dev = mlxsw_sp_port->dev;
1864 if (bridge_port->bridge_device->multicast_enabled &&
1865 !bridge_port->mrouter) {
1866 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1868 netdev_err(dev, "Unable to remove port from SMID\n");
1871 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1873 netdev_err(dev, "Unable to remove MC SFD\n");
1878 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1879 const struct switchdev_obj_port_mdb *mdb)
1881 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1882 struct net_device *orig_dev = mdb->obj.orig_dev;
1883 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1884 struct mlxsw_sp_bridge_device *bridge_device;
1885 struct net_device *dev = mlxsw_sp_port->dev;
1886 struct mlxsw_sp_bridge_port *bridge_port;
1887 struct mlxsw_sp_mid *mid;
1890 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1894 bridge_device = bridge_port->bridge_device;
1895 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1898 if (!mlxsw_sp_port_vlan)
1901 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1903 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1905 netdev_err(dev, "Unable to remove port from MC DB\n");
1909 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1913 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1914 struct mlxsw_sp_bridge_port *bridge_port)
1916 struct mlxsw_sp_bridge_device *bridge_device;
1917 struct mlxsw_sp_mid *mid, *tmp;
1919 bridge_device = bridge_port->bridge_device;
1921 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1922 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1923 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1925 } else if (bridge_device->multicast_enabled &&
1926 bridge_port->mrouter) {
1927 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1932 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1933 const struct switchdev_obj *obj)
1935 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1939 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1940 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1941 SWITCHDEV_OBJ_PORT_VLAN(obj));
1943 case SWITCHDEV_OBJ_ID_PORT_MDB:
1944 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1945 SWITCHDEV_OBJ_PORT_MDB(obj));
1952 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1957 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1960 struct mlxsw_sp_port *mlxsw_sp_port;
1961 u64 max_lag_members;
1964 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1966 for (i = 0; i < max_lag_members; i++) {
1967 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1969 return mlxsw_sp_port;
1974 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1975 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1976 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1977 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1978 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1982 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1983 struct mlxsw_sp_bridge_port *bridge_port,
1984 struct mlxsw_sp_port *mlxsw_sp_port,
1985 struct netlink_ext_ack *extack)
1987 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1989 if (is_vlan_dev(bridge_port->dev)) {
1990 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1994 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1995 if (WARN_ON(!mlxsw_sp_port_vlan))
1998 /* Let VLAN-aware bridge take care of its own VLANs */
1999 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
2005 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2006 struct mlxsw_sp_bridge_port *bridge_port,
2007 struct mlxsw_sp_port *mlxsw_sp_port)
2009 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
2010 /* Make sure untagged frames are allowed to ingress */
2011 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
2015 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2016 const struct net_device *vxlan_dev,
2017 struct netlink_ext_ack *extack)
2024 mlxsw_sp_bridge_8021q_vxlan_leave(struct mlxsw_sp_bridge_device *bridge_device,
2025 const struct net_device *vxlan_dev)
2029 static struct mlxsw_sp_fid *
2030 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2033 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2035 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2038 static struct mlxsw_sp_fid *
2039 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2047 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2048 const struct mlxsw_sp_fid *fid)
2050 return mlxsw_sp_fid_8021q_vid(fid);
2053 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2054 .port_join = mlxsw_sp_bridge_8021q_port_join,
2055 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2056 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2057 .vxlan_leave = mlxsw_sp_bridge_8021q_vxlan_leave,
2058 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2059 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2060 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2064 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2065 const struct net_device *br_dev)
2067 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2069 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2071 if (mlxsw_sp_port_vlan->bridge_port &&
2072 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2081 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2082 struct mlxsw_sp_bridge_port *bridge_port,
2083 struct mlxsw_sp_port *mlxsw_sp_port,
2084 struct netlink_ext_ack *extack)
2086 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2087 struct net_device *dev = bridge_port->dev;
2090 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2091 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2092 if (WARN_ON(!mlxsw_sp_port_vlan))
2095 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2096 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2100 /* Port is no longer usable as a router interface */
2101 if (mlxsw_sp_port_vlan->fid)
2102 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2104 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
2108 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2109 struct mlxsw_sp_bridge_port *bridge_port,
2110 struct mlxsw_sp_port *mlxsw_sp_port)
2112 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2113 struct net_device *dev = bridge_port->dev;
2116 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2117 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2118 if (!mlxsw_sp_port_vlan)
2121 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2125 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2126 const struct net_device *vxlan_dev,
2127 struct netlink_ext_ack *extack)
2129 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2130 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2131 struct mlxsw_sp_nve_params params = {
2132 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2133 .vni = vxlan->cfg.vni,
2136 struct mlxsw_sp_fid *fid;
2139 fid = mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2143 if (mlxsw_sp_fid_vni_is_set(fid)) {
2145 goto err_vni_exists;
2148 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2150 goto err_nve_fid_enable;
2152 /* The tunnel port does not hold a reference on the FID. Only
2153 * local ports and the router port
2155 mlxsw_sp_fid_put(fid);
2161 mlxsw_sp_fid_put(fid);
2166 mlxsw_sp_bridge_8021d_vxlan_leave(struct mlxsw_sp_bridge_device *bridge_device,
2167 const struct net_device *vxlan_dev)
2169 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2170 struct mlxsw_sp_fid *fid;
2172 fid = mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2176 /* If the VxLAN device is down, then the FID does not have a VNI */
2177 if (!mlxsw_sp_fid_vni_is_set(fid))
2180 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2182 mlxsw_sp_fid_put(fid);
2185 static struct mlxsw_sp_fid *
2186 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2189 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2190 struct net_device *vxlan_dev;
2191 struct mlxsw_sp_fid *fid;
2194 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2198 if (mlxsw_sp_fid_vni_is_set(fid))
2201 vxlan_dev = mlxsw_sp_bridge_vxlan_dev_find(bridge_device->dev);
2205 if (!netif_running(vxlan_dev))
2208 err = mlxsw_sp_bridge_8021d_vxlan_join(bridge_device, vxlan_dev, NULL);
2210 goto err_vxlan_join;
2215 mlxsw_sp_fid_put(fid);
2216 return ERR_PTR(err);
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 .vxlan_leave = mlxsw_sp_bridge_8021d_vxlan_leave,
2244 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2245 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2246 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2249 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2250 struct net_device *brport_dev,
2251 struct net_device *br_dev,
2252 struct netlink_ext_ack *extack)
2254 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2255 struct mlxsw_sp_bridge_device *bridge_device;
2256 struct mlxsw_sp_bridge_port *bridge_port;
2259 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2260 if (IS_ERR(bridge_port))
2261 return PTR_ERR(bridge_port);
2262 bridge_device = bridge_port->bridge_device;
2264 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2265 mlxsw_sp_port, extack);
2272 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2276 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2277 struct net_device *brport_dev,
2278 struct net_device *br_dev)
2280 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2281 struct mlxsw_sp_bridge_device *bridge_device;
2282 struct mlxsw_sp_bridge_port *bridge_port;
2284 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2287 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2291 bridge_device->ops->port_leave(bridge_device, bridge_port,
2293 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2296 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2297 const struct net_device *br_dev,
2298 const struct net_device *vxlan_dev,
2299 struct netlink_ext_ack *extack)
2301 struct mlxsw_sp_bridge_device *bridge_device;
2303 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2304 if (WARN_ON(!bridge_device))
2307 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, extack);
2310 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2311 const struct net_device *br_dev,
2312 const struct net_device *vxlan_dev)
2314 struct mlxsw_sp_bridge_device *bridge_device;
2316 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2317 if (WARN_ON(!bridge_device))
2320 bridge_device->ops->vxlan_leave(bridge_device, vxlan_dev);
2324 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2325 const char *mac, u16 vid,
2326 struct net_device *dev, bool offloaded)
2328 struct switchdev_notifier_fdb_info info;
2332 info.offloaded = offloaded;
2333 call_switchdev_notifiers(type, dev, &info.info);
2336 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2337 char *sfn_pl, int rec_index,
2340 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2341 struct mlxsw_sp_bridge_device *bridge_device;
2342 struct mlxsw_sp_bridge_port *bridge_port;
2343 struct mlxsw_sp_port *mlxsw_sp_port;
2344 enum switchdev_notifier_type type;
2348 bool do_notification = true;
2351 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2352 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2353 if (!mlxsw_sp_port) {
2354 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2358 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2359 if (!mlxsw_sp_port_vlan) {
2360 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2364 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2366 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2370 bridge_device = bridge_port->bridge_device;
2371 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2374 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2377 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2381 if (!do_notification)
2383 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2384 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2390 do_notification = false;
2394 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2395 char *sfn_pl, int rec_index,
2398 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2399 struct mlxsw_sp_bridge_device *bridge_device;
2400 struct mlxsw_sp_bridge_port *bridge_port;
2401 struct mlxsw_sp_port *mlxsw_sp_port;
2402 enum switchdev_notifier_type type;
2407 bool do_notification = true;
2410 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2411 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2412 if (!mlxsw_sp_port) {
2413 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2417 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2418 if (!mlxsw_sp_port_vlan) {
2419 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2423 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2425 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2429 bridge_device = bridge_port->bridge_device;
2430 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2431 lag_vid = mlxsw_sp_port_vlan->vid;
2434 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2437 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2441 if (!do_notification)
2443 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2444 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2450 do_notification = false;
2454 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2455 char *sfn_pl, int rec_index)
2457 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2458 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2459 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2462 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2463 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2466 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2467 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2470 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2471 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2477 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2479 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2481 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2482 msecs_to_jiffies(bridge->fdb_notify.interval));
2485 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2487 struct mlxsw_sp_bridge *bridge;
2488 struct mlxsw_sp *mlxsw_sp;
2494 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2498 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2499 mlxsw_sp = bridge->mlxsw_sp;
2502 mlxsw_reg_sfn_pack(sfn_pl);
2503 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2505 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2508 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2509 for (i = 0; i < num_rec; i++)
2510 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2515 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2518 struct mlxsw_sp_switchdev_event_work {
2519 struct work_struct work;
2521 struct switchdev_notifier_fdb_info fdb_info;
2522 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2524 struct net_device *dev;
2525 unsigned long event;
2529 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2530 enum mlxsw_sp_l3proto *proto,
2531 union mlxsw_sp_l3addr *addr)
2533 if (vxlan_addr->sa.sa_family == AF_INET) {
2534 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2535 *proto = MLXSW_SP_L3_PROTO_IPV4;
2537 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2538 *proto = MLXSW_SP_L3_PROTO_IPV6;
2543 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2544 struct mlxsw_sp_switchdev_event_work *
2546 struct mlxsw_sp_fid *fid, __be32 vni)
2548 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2549 struct switchdev_notifier_fdb_info *fdb_info;
2550 struct net_device *dev = switchdev_work->dev;
2551 enum mlxsw_sp_l3proto proto;
2552 union mlxsw_sp_l3addr addr;
2555 fdb_info = &switchdev_work->fdb_info;
2556 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2560 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2563 switch (switchdev_work->event) {
2564 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2565 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2566 vxlan_fdb_info.eth_addr,
2567 mlxsw_sp_fid_index(fid),
2568 proto, &addr, true, false);
2571 vxlan_fdb_info.offloaded = true;
2572 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2573 &vxlan_fdb_info.info);
2574 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2575 vxlan_fdb_info.eth_addr,
2576 fdb_info->vid, dev, true);
2578 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2579 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2580 vxlan_fdb_info.eth_addr,
2581 mlxsw_sp_fid_index(fid),
2582 proto, &addr, false,
2584 vxlan_fdb_info.offloaded = false;
2585 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2586 &vxlan_fdb_info.info);
2592 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2595 struct mlxsw_sp_bridge_device *bridge_device;
2596 struct net_device *dev = switchdev_work->dev;
2597 struct net_device *br_dev;
2598 struct mlxsw_sp *mlxsw_sp;
2599 struct mlxsw_sp_fid *fid;
2603 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2604 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2607 if (!switchdev_work->fdb_info.added_by_user)
2610 if (!netif_running(dev))
2612 br_dev = netdev_master_upper_dev_get(dev);
2615 if (!netif_is_bridge_master(br_dev))
2617 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2620 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2624 fid = bridge_device->ops->fid_lookup(bridge_device,
2625 switchdev_work->fdb_info.vid);
2629 err = mlxsw_sp_fid_vni(fid, &vni);
2633 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2637 mlxsw_sp_fid_put(fid);
2640 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2642 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2643 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2644 struct net_device *dev = switchdev_work->dev;
2645 struct switchdev_notifier_fdb_info *fdb_info;
2646 struct mlxsw_sp_port *mlxsw_sp_port;
2650 if (netif_is_vxlan(dev)) {
2651 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2655 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2659 switch (switchdev_work->event) {
2660 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2661 fdb_info = &switchdev_work->fdb_info;
2662 if (!fdb_info->added_by_user)
2664 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2667 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2669 fdb_info->vid, dev, true);
2671 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2672 fdb_info = &switchdev_work->fdb_info;
2673 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2675 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2676 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2677 /* These events are only used to potentially update an existing
2683 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2687 kfree(switchdev_work->fdb_info.addr);
2688 kfree(switchdev_work);
2693 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
2694 struct mlxsw_sp_switchdev_event_work *
2697 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2698 struct mlxsw_sp_bridge_device *bridge_device;
2699 struct net_device *dev = switchdev_work->dev;
2700 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2701 enum mlxsw_sp_l3proto proto;
2702 union mlxsw_sp_l3addr addr;
2703 struct net_device *br_dev;
2704 struct mlxsw_sp_fid *fid;
2708 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2709 br_dev = netdev_master_upper_dev_get(dev);
2711 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2715 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2719 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2722 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2723 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
2725 mlxsw_sp_fid_put(fid);
2728 vxlan_fdb_info->offloaded = true;
2729 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2730 &vxlan_fdb_info->info);
2731 mlxsw_sp_fid_put(fid);
2735 /* The device has a single FDB table, whereas Linux has two - one
2736 * in the bridge driver and another in the VxLAN driver. We only
2737 * program an entry to the device if the MAC points to the VxLAN
2738 * device in the bridge's FDB table
2740 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2741 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
2742 goto err_br_fdb_find;
2744 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2745 mlxsw_sp_fid_index(fid), proto,
2746 &addr, true, false);
2748 goto err_fdb_tunnel_uc_op;
2749 vxlan_fdb_info->offloaded = true;
2750 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2751 &vxlan_fdb_info->info);
2752 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2753 vxlan_fdb_info->eth_addr, vid, dev, true);
2755 mlxsw_sp_fid_put(fid);
2759 err_fdb_tunnel_uc_op:
2761 mlxsw_sp_fid_put(fid);
2765 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
2766 struct mlxsw_sp_switchdev_event_work *
2769 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2770 struct mlxsw_sp_bridge_device *bridge_device;
2771 struct net_device *dev = switchdev_work->dev;
2772 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2773 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2774 enum mlxsw_sp_l3proto proto;
2775 union mlxsw_sp_l3addr addr;
2776 struct mlxsw_sp_fid *fid;
2779 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2781 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2785 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2789 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2792 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2793 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
2794 mlxsw_sp_fid_put(fid);
2798 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2799 mlxsw_sp_fid_index(fid), proto, &addr,
2801 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2802 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2803 vxlan_fdb_info->eth_addr, vid, dev, false);
2805 mlxsw_sp_fid_put(fid);
2808 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
2810 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2811 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2812 struct net_device *dev = switchdev_work->dev;
2813 struct mlxsw_sp *mlxsw_sp;
2814 struct net_device *br_dev;
2818 if (!netif_running(dev))
2820 br_dev = netdev_master_upper_dev_get(dev);
2823 if (!netif_is_bridge_master(br_dev))
2825 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2829 switch (switchdev_work->event) {
2830 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
2831 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
2833 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
2834 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
2840 kfree(switchdev_work);
2845 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
2847 struct switchdev_notifier_info *info)
2849 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
2850 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2851 struct vxlan_config *cfg = &vxlan->cfg;
2853 vxlan_fdb_info = container_of(info,
2854 struct switchdev_notifier_vxlan_fdb_info,
2857 if (vxlan_fdb_info->remote_port != cfg->dst_port)
2859 if (vxlan_fdb_info->remote_vni != cfg->vni)
2861 if (vxlan_fdb_info->vni != cfg->vni)
2863 if (vxlan_fdb_info->remote_ifindex)
2865 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr))
2867 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip))
2870 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
2875 /* Called under rcu_read_lock() */
2876 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2877 unsigned long event, void *ptr)
2879 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2880 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2881 struct switchdev_notifier_fdb_info *fdb_info;
2882 struct switchdev_notifier_info *info = ptr;
2883 struct net_device *br_dev;
2886 /* Tunnel devices are not our uppers, so check their master instead */
2887 br_dev = netdev_master_upper_dev_get_rcu(dev);
2890 if (!netif_is_bridge_master(br_dev))
2892 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
2895 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2896 if (!switchdev_work)
2899 switchdev_work->dev = dev;
2900 switchdev_work->event = event;
2903 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2904 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2905 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2906 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2907 fdb_info = container_of(info,
2908 struct switchdev_notifier_fdb_info,
2910 INIT_WORK(&switchdev_work->work,
2911 mlxsw_sp_switchdev_bridge_fdb_event_work);
2912 memcpy(&switchdev_work->fdb_info, ptr,
2913 sizeof(switchdev_work->fdb_info));
2914 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2915 if (!switchdev_work->fdb_info.addr)
2916 goto err_addr_alloc;
2917 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2919 /* Take a reference on the device. This can be either
2920 * upper device containig mlxsw_sp_port or just a
2925 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE: /* fall through */
2926 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
2927 INIT_WORK(&switchdev_work->work,
2928 mlxsw_sp_switchdev_vxlan_fdb_event_work);
2929 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
2932 goto err_vxlan_work_prepare;
2936 kfree(switchdev_work);
2940 mlxsw_core_schedule_work(&switchdev_work->work);
2944 err_vxlan_work_prepare:
2946 kfree(switchdev_work);
2950 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2951 .notifier_call = mlxsw_sp_switchdev_event,
2955 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2957 return bridge_port->stp_state;
2960 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2962 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2965 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2967 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2971 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2973 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2977 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2978 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2979 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2983 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2985 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2986 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2990 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2992 struct mlxsw_sp_bridge *bridge;
2994 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2997 mlxsw_sp->bridge = bridge;
2998 bridge->mlxsw_sp = mlxsw_sp;
3000 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3002 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3003 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3005 return mlxsw_sp_fdb_init(mlxsw_sp);
3008 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3010 mlxsw_sp_fdb_fini(mlxsw_sp);
3011 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3012 kfree(mlxsw_sp->bridge);
3015 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
3017 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
3020 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)