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 mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
301 /* In case ports were pulled from out of a bridged LAG, then
302 * it's possible the reference count isn't zero, yet the bridge
303 * port should be destroyed, as it's no longer an upper of ours.
305 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
307 else if (bridge_port->ref_count == 0)
313 static struct mlxsw_sp_bridge_port *
314 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
315 struct net_device *brport_dev)
317 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
318 struct mlxsw_sp_bridge_device *bridge_device;
319 struct mlxsw_sp_bridge_port *bridge_port;
322 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
324 bridge_port->ref_count++;
328 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
329 if (IS_ERR(bridge_device))
330 return ERR_CAST(bridge_device);
332 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
335 goto err_bridge_port_create;
340 err_bridge_port_create:
341 mlxsw_sp_bridge_device_put(bridge, bridge_device);
345 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
346 struct mlxsw_sp_bridge_port *bridge_port)
348 struct mlxsw_sp_bridge_device *bridge_device;
350 bridge_port->ref_count--;
351 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
353 bridge_device = bridge_port->bridge_device;
354 mlxsw_sp_bridge_port_destroy(bridge_port);
355 mlxsw_sp_bridge_device_put(bridge, bridge_device);
358 static struct mlxsw_sp_port_vlan *
359 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
360 const struct mlxsw_sp_bridge_device *
364 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
366 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
368 if (!mlxsw_sp_port_vlan->bridge_port)
370 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
373 if (bridge_device->vlan_enabled &&
374 mlxsw_sp_port_vlan->vid != vid)
376 return mlxsw_sp_port_vlan;
382 static struct mlxsw_sp_port_vlan*
383 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
386 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
388 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
390 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
392 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
393 return mlxsw_sp_port_vlan;
399 static struct mlxsw_sp_bridge_vlan *
400 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
403 struct mlxsw_sp_bridge_vlan *bridge_vlan;
405 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
406 if (bridge_vlan->vid == vid)
413 static struct mlxsw_sp_bridge_vlan *
414 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
416 struct mlxsw_sp_bridge_vlan *bridge_vlan;
418 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
422 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
423 bridge_vlan->vid = vid;
424 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
430 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
432 list_del(&bridge_vlan->list);
433 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
437 static struct mlxsw_sp_bridge_vlan *
438 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
440 struct mlxsw_sp_bridge_vlan *bridge_vlan;
442 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
446 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
449 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
451 if (list_empty(&bridge_vlan->port_vlan_list))
452 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
455 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
456 struct net_device *dev,
457 unsigned long *brport_flags)
459 struct mlxsw_sp_bridge_port *bridge_port;
461 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
462 if (WARN_ON(!bridge_port))
465 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
468 static int mlxsw_sp_port_attr_get(struct net_device *dev,
469 struct switchdev_attr *attr)
471 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
472 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
475 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
476 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
477 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
478 attr->u.ppid.id_len);
480 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
481 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
482 &attr->u.brport_flags);
484 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
485 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
496 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
497 struct mlxsw_sp_bridge_vlan *bridge_vlan,
500 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
502 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
504 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
506 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
507 bridge_vlan->vid, state);
513 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
514 struct switchdev_trans *trans,
515 struct net_device *orig_dev,
518 struct mlxsw_sp_bridge_port *bridge_port;
519 struct mlxsw_sp_bridge_vlan *bridge_vlan;
522 if (switchdev_trans_ph_prepare(trans))
525 /* It's possible we failed to enslave the port, yet this
526 * operation is executed due to it being deferred.
528 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
533 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
534 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
537 goto err_port_bridge_vlan_stp_set;
540 bridge_port->stp_state = state;
544 err_port_bridge_vlan_stp_set:
545 list_for_each_entry_continue_reverse(bridge_vlan,
546 &bridge_port->vlans_list, list)
547 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
548 bridge_port->stp_state);
553 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
554 struct mlxsw_sp_bridge_vlan *bridge_vlan,
555 enum mlxsw_sp_flood_type packet_type,
558 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
560 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
562 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
564 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
566 mlxsw_sp_port->local_port,
574 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
575 struct mlxsw_sp_bridge_port *bridge_port,
576 enum mlxsw_sp_flood_type packet_type,
579 struct mlxsw_sp_bridge_vlan *bridge_vlan;
582 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
583 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
588 goto err_port_bridge_vlan_flood_set;
593 err_port_bridge_vlan_flood_set:
594 list_for_each_entry_continue_reverse(bridge_vlan,
595 &bridge_port->vlans_list, list)
596 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
597 packet_type, !member);
602 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
603 struct mlxsw_sp_bridge_vlan *bridge_vlan,
606 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
607 u16 vid = bridge_vlan->vid;
609 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
611 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
613 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
620 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
621 struct mlxsw_sp_bridge_port *bridge_port,
624 struct mlxsw_sp_bridge_vlan *bridge_vlan;
627 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
628 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
631 goto err_port_bridge_vlan_learning_set;
636 err_port_bridge_vlan_learning_set:
637 list_for_each_entry_continue_reverse(bridge_vlan,
638 &bridge_port->vlans_list, list)
639 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
644 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
645 struct switchdev_trans *trans,
646 struct net_device *orig_dev,
647 unsigned long brport_flags)
649 struct mlxsw_sp_bridge_port *bridge_port;
652 if (switchdev_trans_ph_prepare(trans))
655 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
660 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
661 MLXSW_SP_FLOOD_TYPE_UC,
662 brport_flags & BR_FLOOD);
666 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
667 brport_flags & BR_LEARNING);
671 if (bridge_port->bridge_device->multicast_enabled)
674 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
675 MLXSW_SP_FLOOD_TYPE_MC,
682 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
686 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
688 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
691 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
692 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
695 mlxsw_sp->bridge->ageing_time = ageing_time;
699 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
700 struct switchdev_trans *trans,
701 unsigned long ageing_clock_t)
703 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
704 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
705 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
707 if (switchdev_trans_ph_prepare(trans)) {
708 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
709 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
715 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
718 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
719 struct switchdev_trans *trans,
720 struct net_device *orig_dev,
723 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
724 struct mlxsw_sp_bridge_device *bridge_device;
726 if (!switchdev_trans_ph_prepare(trans))
729 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
730 if (WARN_ON(!bridge_device))
733 if (bridge_device->vlan_enabled == vlan_enabled)
736 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
740 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
741 struct switchdev_trans *trans,
742 struct net_device *orig_dev,
743 bool is_port_mrouter)
745 struct mlxsw_sp_bridge_port *bridge_port;
748 if (switchdev_trans_ph_prepare(trans))
751 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
756 if (!bridge_port->bridge_device->multicast_enabled)
759 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
760 MLXSW_SP_FLOOD_TYPE_MC,
765 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
768 bridge_port->mrouter = is_port_mrouter;
772 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
774 const struct mlxsw_sp_bridge_device *bridge_device;
776 bridge_device = bridge_port->bridge_device;
777 return bridge_device->multicast_enabled ? bridge_port->mrouter :
778 bridge_port->flags & BR_MCAST_FLOOD;
781 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
782 struct switchdev_trans *trans,
783 struct net_device *orig_dev,
786 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
787 struct mlxsw_sp_bridge_device *bridge_device;
788 struct mlxsw_sp_bridge_port *bridge_port;
791 if (switchdev_trans_ph_prepare(trans))
794 /* It's possible we failed to enslave the port, yet this
795 * operation is executed due to it being deferred.
797 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
801 if (bridge_device->multicast_enabled != !mc_disabled) {
802 bridge_device->multicast_enabled = !mc_disabled;
803 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
807 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
808 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
809 bool member = mlxsw_sp_mc_flood(bridge_port);
811 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
813 packet_type, member);
818 bridge_device->multicast_enabled = !mc_disabled;
823 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
824 u16 mid_idx, bool add)
829 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
833 mlxsw_reg_smid_pack(smid_pl, mid_idx,
834 mlxsw_sp_router_port(mlxsw_sp), add);
835 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
841 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
842 struct mlxsw_sp_bridge_device *bridge_device,
845 struct mlxsw_sp_mid *mid;
847 list_for_each_entry(mid, &bridge_device->mids_list, list)
848 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
852 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
853 struct switchdev_trans *trans,
854 struct net_device *orig_dev,
857 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
858 struct mlxsw_sp_bridge_device *bridge_device;
860 if (switchdev_trans_ph_prepare(trans))
863 /* It's possible we failed to enslave the port, yet this
864 * operation is executed due to it being deferred.
866 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
870 if (bridge_device->mrouter != is_mrouter)
871 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
873 bridge_device->mrouter = is_mrouter;
877 static int mlxsw_sp_port_attr_set(struct net_device *dev,
878 const struct switchdev_attr *attr,
879 struct switchdev_trans *trans)
881 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
885 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
886 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
890 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
891 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
893 attr->u.brport_flags);
895 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
896 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
897 attr->u.ageing_time);
899 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
900 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
902 attr->u.vlan_filtering);
904 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
905 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
909 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
910 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
912 attr->u.mc_disabled);
914 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
915 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
924 if (switchdev_trans_ph_commit(trans))
925 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
931 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
932 struct mlxsw_sp_bridge_port *bridge_port)
934 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
935 struct mlxsw_sp_bridge_device *bridge_device;
936 u8 local_port = mlxsw_sp_port->local_port;
937 u16 vid = mlxsw_sp_port_vlan->vid;
938 struct mlxsw_sp_fid *fid;
941 bridge_device = bridge_port->bridge_device;
942 fid = bridge_device->ops->fid_get(bridge_device, vid);
946 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
947 bridge_port->flags & BR_FLOOD);
949 goto err_fid_uc_flood_set;
951 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
952 mlxsw_sp_mc_flood(bridge_port));
954 goto err_fid_mc_flood_set;
956 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
959 goto err_fid_bc_flood_set;
961 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
963 goto err_fid_port_vid_map;
965 mlxsw_sp_port_vlan->fid = fid;
969 err_fid_port_vid_map:
970 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
971 err_fid_bc_flood_set:
972 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
973 err_fid_mc_flood_set:
974 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
975 err_fid_uc_flood_set:
976 mlxsw_sp_fid_put(fid);
981 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
983 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
984 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
985 u8 local_port = mlxsw_sp_port->local_port;
986 u16 vid = mlxsw_sp_port_vlan->vid;
988 mlxsw_sp_port_vlan->fid = NULL;
989 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
990 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
991 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
992 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
993 mlxsw_sp_fid_put(fid);
997 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
998 u16 vid, bool is_pvid)
1002 else if (mlxsw_sp_port->pvid == vid)
1003 return 0; /* Dis-allow untagged packets */
1005 return mlxsw_sp_port->pvid;
1009 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1010 struct mlxsw_sp_bridge_port *bridge_port)
1012 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1013 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1014 u16 vid = mlxsw_sp_port_vlan->vid;
1017 /* No need to continue if only VLAN flags were changed */
1018 if (mlxsw_sp_port_vlan->bridge_port) {
1019 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1023 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1027 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1028 bridge_port->flags & BR_LEARNING);
1030 goto err_port_vid_learning_set;
1032 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1033 bridge_port->stp_state);
1035 goto err_port_vid_stp_set;
1037 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1040 goto err_bridge_vlan_get;
1043 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1044 &bridge_vlan->port_vlan_list);
1046 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1048 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1052 err_bridge_vlan_get:
1053 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1054 err_port_vid_stp_set:
1055 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1056 err_port_vid_learning_set:
1057 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1062 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1064 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1065 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1066 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1067 struct mlxsw_sp_bridge_port *bridge_port;
1068 u16 vid = mlxsw_sp_port_vlan->vid;
1069 bool last_port, last_vlan;
1071 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1072 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1075 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1076 last_vlan = list_is_singular(&bridge_port->vlans_list);
1077 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1078 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1080 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1081 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1082 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1083 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1085 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1087 mlxsw_sp_fid_index(fid));
1089 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1091 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1093 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1094 mlxsw_sp_port_vlan->bridge_port = NULL;
1098 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1099 struct mlxsw_sp_bridge_port *bridge_port,
1100 u16 vid, bool is_untagged, bool is_pvid)
1102 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1103 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1104 u16 old_pvid = mlxsw_sp_port->pvid;
1107 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1108 if (IS_ERR(mlxsw_sp_port_vlan))
1109 return PTR_ERR(mlxsw_sp_port_vlan);
1111 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1114 goto err_port_vlan_set;
1116 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1118 goto err_port_pvid_set;
1120 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1122 goto err_port_vlan_bridge_join;
1126 err_port_vlan_bridge_join:
1127 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1129 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1131 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1136 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1137 const struct net_device *br_dev,
1138 const struct switchdev_obj_port_vlan *vlan)
1140 struct mlxsw_sp_rif *rif;
1141 struct mlxsw_sp_fid *fid;
1145 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev);
1148 fid = mlxsw_sp_rif_fid(rif);
1149 pvid = mlxsw_sp_fid_8021q_vid(fid);
1151 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1152 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1154 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1159 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1168 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1169 const struct switchdev_obj_port_vlan *vlan,
1170 struct switchdev_trans *trans)
1172 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1173 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1174 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1175 struct net_device *orig_dev = vlan->obj.orig_dev;
1176 struct mlxsw_sp_bridge_port *bridge_port;
1179 if (netif_is_bridge_master(orig_dev)) {
1182 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1183 br_vlan_enabled(orig_dev) &&
1184 switchdev_trans_ph_prepare(trans))
1185 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1192 if (switchdev_trans_ph_prepare(trans))
1195 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1196 if (WARN_ON(!bridge_port))
1199 if (!bridge_port->bridge_device->vlan_enabled)
1202 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1205 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1215 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1217 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1218 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1222 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1223 struct mlxsw_sp_bridge_port *bridge_port,
1226 bool lagged = bridge_port->lagged;
1227 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1230 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1231 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1232 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1233 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1235 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1238 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1240 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1241 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1244 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1246 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1247 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1250 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1251 const char *mac, u16 fid,
1252 enum mlxsw_sp_l3proto proto,
1253 const union mlxsw_sp_l3addr *addr,
1254 bool adding, bool dynamic)
1256 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1263 case MLXSW_SP_L3_PROTO_IPV4:
1264 uip = be32_to_cpu(addr->addr4);
1265 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1267 case MLXSW_SP_L3_PROTO_IPV6: /* fall through */
1273 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1277 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1278 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1279 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1280 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1282 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1283 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1287 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1295 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1296 const char *mac, u16 fid, bool adding,
1297 enum mlxsw_reg_sfd_rec_action action,
1304 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1308 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1309 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1310 mac, fid, action, local_port);
1311 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1312 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1316 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1324 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1325 const char *mac, u16 fid, bool adding,
1328 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1329 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1332 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1335 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1336 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1340 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1341 const char *mac, u16 fid, u16 lag_vid,
1342 bool adding, bool dynamic)
1348 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1352 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1353 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1354 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1356 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1357 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1361 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1370 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1371 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1373 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1374 struct net_device *orig_dev = fdb_info->info.dev;
1375 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1376 struct mlxsw_sp_bridge_device *bridge_device;
1377 struct mlxsw_sp_bridge_port *bridge_port;
1380 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1384 bridge_device = bridge_port->bridge_device;
1385 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1388 if (!mlxsw_sp_port_vlan)
1391 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1392 vid = mlxsw_sp_port_vlan->vid;
1394 if (!bridge_port->lagged)
1395 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1396 bridge_port->system_port,
1397 fdb_info->addr, fid_index,
1400 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1401 bridge_port->lag_id,
1402 fdb_info->addr, fid_index,
1403 vid, adding, false);
1406 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1407 u16 fid, u16 mid_idx, bool adding)
1413 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1417 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1418 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1419 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1420 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1421 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1425 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1433 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1435 bool set_router_port)
1440 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1444 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1445 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1446 if (mlxsw_sp->ports[i])
1447 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1450 mlxsw_reg_smid_port_mask_set(smid_pl,
1451 mlxsw_sp_router_port(mlxsw_sp), 1);
1453 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1454 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1456 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1459 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1464 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1465 u16 mid_idx, bool add)
1467 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1471 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1475 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1476 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1482 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1483 const unsigned char *addr,
1486 struct mlxsw_sp_mid *mid;
1488 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1489 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1496 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1497 struct mlxsw_sp_bridge_port *bridge_port,
1498 unsigned long *ports_bitmap)
1500 struct mlxsw_sp_port *mlxsw_sp_port;
1501 u64 max_lag_members, i;
1504 if (!bridge_port->lagged) {
1505 set_bit(bridge_port->system_port, ports_bitmap);
1507 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1509 lag_id = bridge_port->lag_id;
1510 for (i = 0; i < max_lag_members; i++) {
1511 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1514 set_bit(mlxsw_sp_port->local_port,
1521 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1522 struct mlxsw_sp_bridge_device *bridge_device,
1523 struct mlxsw_sp *mlxsw_sp)
1525 struct mlxsw_sp_bridge_port *bridge_port;
1527 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1528 if (bridge_port->mrouter) {
1529 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1537 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1538 struct mlxsw_sp_mid *mid,
1539 struct mlxsw_sp_bridge_device *bridge_device)
1547 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1549 if (mid_idx == MLXSW_SP_MID_MAX)
1552 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1553 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1554 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1558 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1559 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1562 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1563 bridge_device->mrouter);
1564 kfree(flood_bitmap);
1568 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1573 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1578 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1579 struct mlxsw_sp_mid *mid)
1584 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1586 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1591 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1592 struct mlxsw_sp_bridge_device *bridge_device,
1593 const unsigned char *addr,
1596 struct mlxsw_sp_mid *mid;
1599 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1603 alloc_size = sizeof(unsigned long) *
1604 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1606 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1607 if (!mid->ports_in_mid)
1608 goto err_ports_in_mid_alloc;
1610 ether_addr_copy(mid->addr, addr);
1614 if (!bridge_device->multicast_enabled)
1617 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1618 goto err_write_mdb_entry;
1621 list_add_tail(&mid->list, &bridge_device->mids_list);
1624 err_write_mdb_entry:
1625 kfree(mid->ports_in_mid);
1626 err_ports_in_mid_alloc:
1631 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1632 struct mlxsw_sp_mid *mid)
1634 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1637 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1638 if (bitmap_empty(mid->ports_in_mid,
1639 mlxsw_core_max_ports(mlxsw_sp->core))) {
1640 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1641 list_del(&mid->list);
1642 kfree(mid->ports_in_mid);
1648 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1649 const struct switchdev_obj_port_mdb *mdb,
1650 struct switchdev_trans *trans)
1652 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1653 struct net_device *orig_dev = mdb->obj.orig_dev;
1654 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1655 struct net_device *dev = mlxsw_sp_port->dev;
1656 struct mlxsw_sp_bridge_device *bridge_device;
1657 struct mlxsw_sp_bridge_port *bridge_port;
1658 struct mlxsw_sp_mid *mid;
1662 if (switchdev_trans_ph_prepare(trans))
1665 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1669 bridge_device = bridge_port->bridge_device;
1670 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1673 if (!mlxsw_sp_port_vlan)
1676 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1678 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1680 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1683 netdev_err(dev, "Unable to allocate MC group\n");
1687 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1689 if (!bridge_device->multicast_enabled)
1692 if (bridge_port->mrouter)
1695 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1697 netdev_err(dev, "Unable to set SMID\n");
1704 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1709 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1710 struct mlxsw_sp_bridge_device
1713 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1714 struct mlxsw_sp_mid *mid;
1717 mc_enabled = bridge_device->multicast_enabled;
1719 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1721 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1724 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1729 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1730 struct mlxsw_sp_bridge_port *bridge_port,
1733 struct mlxsw_sp_bridge_device *bridge_device;
1734 struct mlxsw_sp_mid *mid;
1736 bridge_device = bridge_port->bridge_device;
1738 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1739 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1740 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1744 struct mlxsw_sp_span_respin_work {
1745 struct work_struct work;
1746 struct mlxsw_sp *mlxsw_sp;
1749 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1751 struct mlxsw_sp_span_respin_work *respin_work =
1752 container_of(work, struct mlxsw_sp_span_respin_work, work);
1755 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1760 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1762 struct mlxsw_sp_span_respin_work *respin_work;
1764 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1768 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1769 respin_work->mlxsw_sp = mlxsw_sp;
1771 mlxsw_core_schedule_work(&respin_work->work);
1774 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1775 const struct switchdev_obj *obj,
1776 struct switchdev_trans *trans)
1778 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1779 const struct switchdev_obj_port_vlan *vlan;
1783 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1784 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1785 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1787 if (switchdev_trans_ph_prepare(trans)) {
1788 /* The event is emitted before the changes are actually
1789 * applied to the bridge. Therefore schedule the respin
1790 * call for later, so that the respin logic sees the
1791 * updated bridge state.
1793 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1796 case SWITCHDEV_OBJ_ID_PORT_MDB:
1797 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1798 SWITCHDEV_OBJ_PORT_MDB(obj),
1810 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1811 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1813 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1814 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1816 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1817 if (WARN_ON(!mlxsw_sp_port_vlan))
1820 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1821 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1822 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1823 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1826 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1827 const struct switchdev_obj_port_vlan *vlan)
1829 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1830 struct net_device *orig_dev = vlan->obj.orig_dev;
1831 struct mlxsw_sp_bridge_port *bridge_port;
1834 if (netif_is_bridge_master(orig_dev))
1837 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1838 if (WARN_ON(!bridge_port))
1841 if (!bridge_port->bridge_device->vlan_enabled)
1844 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1845 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1851 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1852 struct mlxsw_sp_bridge_port *bridge_port,
1853 struct mlxsw_sp_mid *mid)
1855 struct net_device *dev = mlxsw_sp_port->dev;
1858 if (bridge_port->bridge_device->multicast_enabled &&
1859 !bridge_port->mrouter) {
1860 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1862 netdev_err(dev, "Unable to remove port from SMID\n");
1865 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1867 netdev_err(dev, "Unable to remove MC SFD\n");
1872 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1873 const struct switchdev_obj_port_mdb *mdb)
1875 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1876 struct net_device *orig_dev = mdb->obj.orig_dev;
1877 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1878 struct mlxsw_sp_bridge_device *bridge_device;
1879 struct net_device *dev = mlxsw_sp_port->dev;
1880 struct mlxsw_sp_bridge_port *bridge_port;
1881 struct mlxsw_sp_mid *mid;
1884 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1888 bridge_device = bridge_port->bridge_device;
1889 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1892 if (!mlxsw_sp_port_vlan)
1895 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1897 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1899 netdev_err(dev, "Unable to remove port from MC DB\n");
1903 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1907 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1908 struct mlxsw_sp_bridge_port *bridge_port)
1910 struct mlxsw_sp_bridge_device *bridge_device;
1911 struct mlxsw_sp_mid *mid, *tmp;
1913 bridge_device = bridge_port->bridge_device;
1915 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1916 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1917 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1919 } else if (bridge_device->multicast_enabled &&
1920 bridge_port->mrouter) {
1921 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1926 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1927 const struct switchdev_obj *obj)
1929 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1933 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1934 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1935 SWITCHDEV_OBJ_PORT_VLAN(obj));
1937 case SWITCHDEV_OBJ_ID_PORT_MDB:
1938 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1939 SWITCHDEV_OBJ_PORT_MDB(obj));
1946 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1951 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1954 struct mlxsw_sp_port *mlxsw_sp_port;
1955 u64 max_lag_members;
1958 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1960 for (i = 0; i < max_lag_members; i++) {
1961 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1963 return mlxsw_sp_port;
1968 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1969 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1970 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1971 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1972 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1976 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1977 struct mlxsw_sp_bridge_port *bridge_port,
1978 struct mlxsw_sp_port *mlxsw_sp_port,
1979 struct netlink_ext_ack *extack)
1981 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1983 if (is_vlan_dev(bridge_port->dev)) {
1984 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1988 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1989 if (WARN_ON(!mlxsw_sp_port_vlan))
1992 /* Let VLAN-aware bridge take care of its own VLANs */
1993 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1999 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2000 struct mlxsw_sp_bridge_port *bridge_port,
2001 struct mlxsw_sp_port *mlxsw_sp_port)
2003 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
2004 /* Make sure untagged frames are allowed to ingress */
2005 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
2009 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2010 const struct net_device *vxlan_dev,
2011 struct netlink_ext_ack *extack)
2018 mlxsw_sp_bridge_8021q_vxlan_leave(struct mlxsw_sp_bridge_device *bridge_device,
2019 const struct net_device *vxlan_dev)
2023 static struct mlxsw_sp_fid *
2024 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2027 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2029 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2032 static struct mlxsw_sp_fid *
2033 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2041 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2042 const struct mlxsw_sp_fid *fid)
2044 return mlxsw_sp_fid_8021q_vid(fid);
2047 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2048 .port_join = mlxsw_sp_bridge_8021q_port_join,
2049 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2050 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2051 .vxlan_leave = mlxsw_sp_bridge_8021q_vxlan_leave,
2052 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2053 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2054 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2058 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2059 const struct net_device *br_dev)
2061 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2063 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2065 if (mlxsw_sp_port_vlan->bridge_port &&
2066 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2075 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2076 struct mlxsw_sp_bridge_port *bridge_port,
2077 struct mlxsw_sp_port *mlxsw_sp_port,
2078 struct netlink_ext_ack *extack)
2080 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2081 struct net_device *dev = bridge_port->dev;
2084 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2085 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2086 if (WARN_ON(!mlxsw_sp_port_vlan))
2089 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2090 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2094 /* Port is no longer usable as a router interface */
2095 if (mlxsw_sp_port_vlan->fid)
2096 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2098 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
2102 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2103 struct mlxsw_sp_bridge_port *bridge_port,
2104 struct mlxsw_sp_port *mlxsw_sp_port)
2106 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2107 struct net_device *dev = bridge_port->dev;
2110 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2111 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2112 if (WARN_ON(!mlxsw_sp_port_vlan))
2115 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2119 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2120 const struct net_device *vxlan_dev,
2121 struct netlink_ext_ack *extack)
2123 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2124 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2125 struct mlxsw_sp_nve_params params = {
2126 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2127 .vni = vxlan->cfg.vni,
2130 struct mlxsw_sp_fid *fid;
2133 fid = mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2137 if (mlxsw_sp_fid_vni_is_set(fid))
2140 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2142 goto err_nve_fid_enable;
2144 /* The tunnel port does not hold a reference on the FID. Only
2145 * local ports and the router port
2147 mlxsw_sp_fid_put(fid);
2152 mlxsw_sp_fid_put(fid);
2157 mlxsw_sp_bridge_8021d_vxlan_leave(struct mlxsw_sp_bridge_device *bridge_device,
2158 const struct net_device *vxlan_dev)
2160 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2161 struct mlxsw_sp_fid *fid;
2163 fid = mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2167 /* If the VxLAN device is down, then the FID does not have a VNI */
2168 if (!mlxsw_sp_fid_vni_is_set(fid))
2171 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2173 mlxsw_sp_fid_put(fid);
2176 static struct mlxsw_sp_fid *
2177 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2180 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2181 struct net_device *vxlan_dev;
2182 struct mlxsw_sp_fid *fid;
2185 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2189 if (mlxsw_sp_fid_vni_is_set(fid))
2192 vxlan_dev = mlxsw_sp_bridge_vxlan_dev_find(bridge_device->dev);
2196 if (!netif_running(vxlan_dev))
2199 err = mlxsw_sp_bridge_8021d_vxlan_join(bridge_device, vxlan_dev, NULL);
2201 goto err_vxlan_join;
2206 mlxsw_sp_fid_put(fid);
2207 return ERR_PTR(err);
2210 static struct mlxsw_sp_fid *
2211 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2214 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2216 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2220 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2224 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2225 const struct mlxsw_sp_fid *fid)
2230 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2231 .port_join = mlxsw_sp_bridge_8021d_port_join,
2232 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2233 .vxlan_join = mlxsw_sp_bridge_8021d_vxlan_join,
2234 .vxlan_leave = mlxsw_sp_bridge_8021d_vxlan_leave,
2235 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2236 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2237 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2240 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2241 struct net_device *brport_dev,
2242 struct net_device *br_dev,
2243 struct netlink_ext_ack *extack)
2245 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2246 struct mlxsw_sp_bridge_device *bridge_device;
2247 struct mlxsw_sp_bridge_port *bridge_port;
2250 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2251 if (IS_ERR(bridge_port))
2252 return PTR_ERR(bridge_port);
2253 bridge_device = bridge_port->bridge_device;
2255 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2256 mlxsw_sp_port, extack);
2263 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2267 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2268 struct net_device *brport_dev,
2269 struct net_device *br_dev)
2271 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2272 struct mlxsw_sp_bridge_device *bridge_device;
2273 struct mlxsw_sp_bridge_port *bridge_port;
2275 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2278 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2282 bridge_device->ops->port_leave(bridge_device, bridge_port,
2284 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2287 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2288 const struct net_device *br_dev,
2289 const struct net_device *vxlan_dev,
2290 struct netlink_ext_ack *extack)
2292 struct mlxsw_sp_bridge_device *bridge_device;
2294 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2295 if (WARN_ON(!bridge_device))
2298 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, extack);
2301 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2302 const struct net_device *br_dev,
2303 const struct net_device *vxlan_dev)
2305 struct mlxsw_sp_bridge_device *bridge_device;
2307 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2308 if (WARN_ON(!bridge_device))
2311 bridge_device->ops->vxlan_leave(bridge_device, vxlan_dev);
2315 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2316 const char *mac, u16 vid,
2317 struct net_device *dev, bool offloaded)
2319 struct switchdev_notifier_fdb_info info;
2323 info.offloaded = offloaded;
2324 call_switchdev_notifiers(type, dev, &info.info);
2327 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2328 char *sfn_pl, int rec_index,
2331 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2332 struct mlxsw_sp_bridge_device *bridge_device;
2333 struct mlxsw_sp_bridge_port *bridge_port;
2334 struct mlxsw_sp_port *mlxsw_sp_port;
2335 enum switchdev_notifier_type type;
2339 bool do_notification = true;
2342 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2343 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2344 if (!mlxsw_sp_port) {
2345 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2349 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2350 if (!mlxsw_sp_port_vlan) {
2351 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2355 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2357 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2361 bridge_device = bridge_port->bridge_device;
2362 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2365 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2368 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2372 if (!do_notification)
2374 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2375 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2381 do_notification = false;
2385 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2386 char *sfn_pl, int rec_index,
2389 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2390 struct mlxsw_sp_bridge_device *bridge_device;
2391 struct mlxsw_sp_bridge_port *bridge_port;
2392 struct mlxsw_sp_port *mlxsw_sp_port;
2393 enum switchdev_notifier_type type;
2398 bool do_notification = true;
2401 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2402 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2403 if (!mlxsw_sp_port) {
2404 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2408 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2409 if (!mlxsw_sp_port_vlan) {
2410 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2414 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2416 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2420 bridge_device = bridge_port->bridge_device;
2421 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2422 lag_vid = mlxsw_sp_port_vlan->vid;
2425 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2428 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2432 if (!do_notification)
2434 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2435 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2441 do_notification = false;
2445 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2446 char *sfn_pl, int rec_index)
2448 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2449 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2450 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2453 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2454 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2457 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2458 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2461 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2462 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2468 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2470 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2472 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2473 msecs_to_jiffies(bridge->fdb_notify.interval));
2476 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2478 struct mlxsw_sp_bridge *bridge;
2479 struct mlxsw_sp *mlxsw_sp;
2485 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2489 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2490 mlxsw_sp = bridge->mlxsw_sp;
2493 mlxsw_reg_sfn_pack(sfn_pl);
2494 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2496 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2499 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2500 for (i = 0; i < num_rec; i++)
2501 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2506 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2509 struct mlxsw_sp_switchdev_event_work {
2510 struct work_struct work;
2512 struct switchdev_notifier_fdb_info fdb_info;
2513 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2515 struct net_device *dev;
2516 unsigned long event;
2520 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2521 enum mlxsw_sp_l3proto *proto,
2522 union mlxsw_sp_l3addr *addr)
2524 if (vxlan_addr->sa.sa_family == AF_INET) {
2525 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2526 *proto = MLXSW_SP_L3_PROTO_IPV4;
2528 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2529 *proto = MLXSW_SP_L3_PROTO_IPV6;
2534 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2535 struct mlxsw_sp_switchdev_event_work *
2537 struct mlxsw_sp_fid *fid, __be32 vni)
2539 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2540 struct switchdev_notifier_fdb_info *fdb_info;
2541 struct net_device *dev = switchdev_work->dev;
2542 enum mlxsw_sp_l3proto proto;
2543 union mlxsw_sp_l3addr addr;
2546 fdb_info = &switchdev_work->fdb_info;
2547 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2551 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2554 switch (switchdev_work->event) {
2555 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2556 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2557 vxlan_fdb_info.eth_addr,
2558 mlxsw_sp_fid_index(fid),
2559 proto, &addr, true, false);
2562 vxlan_fdb_info.offloaded = true;
2563 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2564 &vxlan_fdb_info.info);
2565 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2566 vxlan_fdb_info.eth_addr,
2567 fdb_info->vid, dev, true);
2569 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2570 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2571 vxlan_fdb_info.eth_addr,
2572 mlxsw_sp_fid_index(fid),
2573 proto, &addr, false,
2575 vxlan_fdb_info.offloaded = false;
2576 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2577 &vxlan_fdb_info.info);
2583 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2586 struct mlxsw_sp_bridge_device *bridge_device;
2587 struct net_device *dev = switchdev_work->dev;
2588 struct net_device *br_dev;
2589 struct mlxsw_sp *mlxsw_sp;
2590 struct mlxsw_sp_fid *fid;
2594 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2595 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2598 if (!switchdev_work->fdb_info.added_by_user)
2601 if (!netif_running(dev))
2603 br_dev = netdev_master_upper_dev_get(dev);
2606 if (!netif_is_bridge_master(br_dev))
2608 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2611 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2615 fid = bridge_device->ops->fid_lookup(bridge_device,
2616 switchdev_work->fdb_info.vid);
2620 err = mlxsw_sp_fid_vni(fid, &vni);
2624 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2628 mlxsw_sp_fid_put(fid);
2631 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2633 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2634 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2635 struct net_device *dev = switchdev_work->dev;
2636 struct switchdev_notifier_fdb_info *fdb_info;
2637 struct mlxsw_sp_port *mlxsw_sp_port;
2641 if (netif_is_vxlan(dev)) {
2642 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2646 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2650 switch (switchdev_work->event) {
2651 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2652 fdb_info = &switchdev_work->fdb_info;
2653 if (!fdb_info->added_by_user)
2655 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2658 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2660 fdb_info->vid, dev, true);
2662 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2663 fdb_info = &switchdev_work->fdb_info;
2664 if (!fdb_info->added_by_user)
2666 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2668 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2669 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2670 /* These events are only used to potentially update an existing
2676 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2680 kfree(switchdev_work->fdb_info.addr);
2681 kfree(switchdev_work);
2686 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
2687 struct mlxsw_sp_switchdev_event_work *
2690 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2691 struct mlxsw_sp_bridge_device *bridge_device;
2692 struct net_device *dev = switchdev_work->dev;
2693 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2694 enum mlxsw_sp_l3proto proto;
2695 union mlxsw_sp_l3addr addr;
2696 struct net_device *br_dev;
2697 struct mlxsw_sp_fid *fid;
2701 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2702 br_dev = netdev_master_upper_dev_get(dev);
2704 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2708 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2712 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2715 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2716 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
2718 mlxsw_sp_fid_put(fid);
2721 vxlan_fdb_info->offloaded = true;
2722 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2723 &vxlan_fdb_info->info);
2724 mlxsw_sp_fid_put(fid);
2728 /* The device has a single FDB table, whereas Linux has two - one
2729 * in the bridge driver and another in the VxLAN driver. We only
2730 * program an entry to the device if the MAC points to the VxLAN
2731 * device in the bridge's FDB table
2733 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2734 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
2735 goto err_br_fdb_find;
2737 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2738 mlxsw_sp_fid_index(fid), proto,
2739 &addr, true, false);
2741 goto err_fdb_tunnel_uc_op;
2742 vxlan_fdb_info->offloaded = true;
2743 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2744 &vxlan_fdb_info->info);
2745 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2746 vxlan_fdb_info->eth_addr, vid, dev, true);
2748 mlxsw_sp_fid_put(fid);
2752 err_fdb_tunnel_uc_op:
2754 mlxsw_sp_fid_put(fid);
2758 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
2759 struct mlxsw_sp_switchdev_event_work *
2762 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2763 struct mlxsw_sp_bridge_device *bridge_device;
2764 struct net_device *dev = switchdev_work->dev;
2765 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2766 u8 all_zeros_mac[ETH_ALEN] = { 0 };
2767 enum mlxsw_sp_l3proto proto;
2768 union mlxsw_sp_l3addr addr;
2769 struct mlxsw_sp_fid *fid;
2772 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
2774 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2778 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
2782 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
2785 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
2786 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
2787 mlxsw_sp_fid_put(fid);
2791 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
2792 mlxsw_sp_fid_index(fid), proto, &addr,
2794 vid = bridge_device->ops->fid_vid(bridge_device, fid);
2795 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2796 vxlan_fdb_info->eth_addr, vid, dev, false);
2798 mlxsw_sp_fid_put(fid);
2801 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
2803 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2804 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2805 struct net_device *dev = switchdev_work->dev;
2806 struct mlxsw_sp *mlxsw_sp;
2807 struct net_device *br_dev;
2811 if (!netif_running(dev))
2813 br_dev = netdev_master_upper_dev_get(dev);
2816 if (!netif_is_bridge_master(br_dev))
2818 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2822 switch (switchdev_work->event) {
2823 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
2824 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
2826 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
2827 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
2833 kfree(switchdev_work);
2838 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
2840 struct switchdev_notifier_info *info)
2842 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
2843 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
2844 struct vxlan_config *cfg = &vxlan->cfg;
2846 vxlan_fdb_info = container_of(info,
2847 struct switchdev_notifier_vxlan_fdb_info,
2850 if (vxlan_fdb_info->remote_port != cfg->dst_port)
2852 if (vxlan_fdb_info->remote_vni != cfg->vni)
2854 if (vxlan_fdb_info->vni != cfg->vni)
2856 if (vxlan_fdb_info->remote_ifindex)
2858 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr))
2860 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip))
2863 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
2868 /* Called under rcu_read_lock() */
2869 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2870 unsigned long event, void *ptr)
2872 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2873 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2874 struct switchdev_notifier_fdb_info *fdb_info;
2875 struct switchdev_notifier_info *info = ptr;
2876 struct net_device *br_dev;
2879 /* Tunnel devices are not our uppers, so check their master instead */
2880 br_dev = netdev_master_upper_dev_get_rcu(dev);
2883 if (!netif_is_bridge_master(br_dev))
2885 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
2888 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2889 if (!switchdev_work)
2892 switchdev_work->dev = dev;
2893 switchdev_work->event = event;
2896 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2897 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2898 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2899 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2900 fdb_info = container_of(info,
2901 struct switchdev_notifier_fdb_info,
2903 INIT_WORK(&switchdev_work->work,
2904 mlxsw_sp_switchdev_bridge_fdb_event_work);
2905 memcpy(&switchdev_work->fdb_info, ptr,
2906 sizeof(switchdev_work->fdb_info));
2907 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2908 if (!switchdev_work->fdb_info.addr)
2909 goto err_addr_alloc;
2910 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2912 /* Take a reference on the device. This can be either
2913 * upper device containig mlxsw_sp_port or just a
2918 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE: /* fall through */
2919 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
2920 INIT_WORK(&switchdev_work->work,
2921 mlxsw_sp_switchdev_vxlan_fdb_event_work);
2922 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
2925 goto err_vxlan_work_prepare;
2929 kfree(switchdev_work);
2933 mlxsw_core_schedule_work(&switchdev_work->work);
2937 err_vxlan_work_prepare:
2939 kfree(switchdev_work);
2943 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2944 .notifier_call = mlxsw_sp_switchdev_event,
2948 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2950 return bridge_port->stp_state;
2953 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2955 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2958 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2960 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2964 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2966 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2970 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2971 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2972 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2976 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2978 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2979 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2983 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2985 struct mlxsw_sp_bridge *bridge;
2987 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2990 mlxsw_sp->bridge = bridge;
2991 bridge->mlxsw_sp = mlxsw_sp;
2993 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2995 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2996 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2998 return mlxsw_sp_fdb_init(mlxsw_sp);
3001 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3003 mlxsw_sp_fdb_fini(mlxsw_sp);
3004 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3005 kfree(mlxsw_sp->bridge);
3008 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
3010 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
3013 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)