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>
19 #include "spectrum_span.h"
20 #include "spectrum_router.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);
88 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
93 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
94 struct mlxsw_sp_bridge_port *bridge_port,
98 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
99 struct mlxsw_sp_bridge_port *bridge_port);
102 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
103 struct mlxsw_sp_bridge_device
107 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
108 struct mlxsw_sp_bridge_port *bridge_port,
111 static struct mlxsw_sp_bridge_device *
112 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
113 const struct net_device *br_dev)
115 struct mlxsw_sp_bridge_device *bridge_device;
117 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
118 if (bridge_device->dev == br_dev)
119 return bridge_device;
124 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
125 const struct net_device *br_dev)
127 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
130 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
133 struct mlxsw_sp *mlxsw_sp = data;
135 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
139 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
140 struct net_device *dev)
142 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
143 netdev_walk_all_upper_dev_rcu(dev,
144 mlxsw_sp_bridge_device_upper_rif_destroy,
148 static struct mlxsw_sp_bridge_device *
149 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
150 struct net_device *br_dev)
152 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
153 struct mlxsw_sp_bridge_device *bridge_device;
154 bool vlan_enabled = br_vlan_enabled(br_dev);
156 if (vlan_enabled && bridge->vlan_enabled_exists) {
157 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
158 return ERR_PTR(-EINVAL);
161 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
163 return ERR_PTR(-ENOMEM);
165 bridge_device->dev = br_dev;
166 bridge_device->vlan_enabled = vlan_enabled;
167 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
168 bridge_device->mrouter = br_multicast_router(br_dev);
169 INIT_LIST_HEAD(&bridge_device->ports_list);
171 bridge->vlan_enabled_exists = true;
172 bridge_device->ops = bridge->bridge_8021q_ops;
174 bridge_device->ops = bridge->bridge_8021d_ops;
176 INIT_LIST_HEAD(&bridge_device->mids_list);
177 list_add(&bridge_device->list, &bridge->bridges_list);
179 return bridge_device;
183 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
184 struct mlxsw_sp_bridge_device *bridge_device)
186 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
188 list_del(&bridge_device->list);
189 if (bridge_device->vlan_enabled)
190 bridge->vlan_enabled_exists = false;
191 WARN_ON(!list_empty(&bridge_device->ports_list));
192 WARN_ON(!list_empty(&bridge_device->mids_list));
193 kfree(bridge_device);
196 static struct mlxsw_sp_bridge_device *
197 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
198 struct net_device *br_dev)
200 struct mlxsw_sp_bridge_device *bridge_device;
202 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
204 return bridge_device;
206 return mlxsw_sp_bridge_device_create(bridge, br_dev);
210 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
211 struct mlxsw_sp_bridge_device *bridge_device)
213 if (list_empty(&bridge_device->ports_list))
214 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
217 static struct mlxsw_sp_bridge_port *
218 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
219 const struct net_device *brport_dev)
221 struct mlxsw_sp_bridge_port *bridge_port;
223 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
224 if (bridge_port->dev == brport_dev)
231 struct mlxsw_sp_bridge_port *
232 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
233 struct net_device *brport_dev)
235 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
236 struct mlxsw_sp_bridge_device *bridge_device;
241 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
245 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
248 static struct mlxsw_sp_bridge_port *
249 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
250 struct net_device *brport_dev)
252 struct mlxsw_sp_bridge_port *bridge_port;
253 struct mlxsw_sp_port *mlxsw_sp_port;
255 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
259 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
260 bridge_port->lagged = mlxsw_sp_port->lagged;
261 if (bridge_port->lagged)
262 bridge_port->lag_id = mlxsw_sp_port->lag_id;
264 bridge_port->system_port = mlxsw_sp_port->local_port;
265 bridge_port->dev = brport_dev;
266 bridge_port->bridge_device = bridge_device;
267 bridge_port->stp_state = BR_STATE_DISABLED;
268 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
270 INIT_LIST_HEAD(&bridge_port->vlans_list);
271 list_add(&bridge_port->list, &bridge_device->ports_list);
272 bridge_port->ref_count = 1;
278 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
280 list_del(&bridge_port->list);
281 WARN_ON(!list_empty(&bridge_port->vlans_list));
285 static struct mlxsw_sp_bridge_port *
286 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
287 struct net_device *brport_dev)
289 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
290 struct mlxsw_sp_bridge_device *bridge_device;
291 struct mlxsw_sp_bridge_port *bridge_port;
294 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
296 bridge_port->ref_count++;
300 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
301 if (IS_ERR(bridge_device))
302 return ERR_CAST(bridge_device);
304 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
307 goto err_bridge_port_create;
312 err_bridge_port_create:
313 mlxsw_sp_bridge_device_put(bridge, bridge_device);
317 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
318 struct mlxsw_sp_bridge_port *bridge_port)
320 struct mlxsw_sp_bridge_device *bridge_device;
322 if (--bridge_port->ref_count != 0)
324 bridge_device = bridge_port->bridge_device;
325 mlxsw_sp_bridge_port_destroy(bridge_port);
326 mlxsw_sp_bridge_device_put(bridge, bridge_device);
329 static struct mlxsw_sp_port_vlan *
330 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
331 const struct mlxsw_sp_bridge_device *
335 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
337 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
339 if (!mlxsw_sp_port_vlan->bridge_port)
341 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
344 if (bridge_device->vlan_enabled &&
345 mlxsw_sp_port_vlan->vid != vid)
347 return mlxsw_sp_port_vlan;
353 static struct mlxsw_sp_port_vlan*
354 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
357 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
359 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
361 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
363 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
364 return mlxsw_sp_port_vlan;
370 static struct mlxsw_sp_bridge_vlan *
371 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
374 struct mlxsw_sp_bridge_vlan *bridge_vlan;
376 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
377 if (bridge_vlan->vid == vid)
384 static struct mlxsw_sp_bridge_vlan *
385 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
387 struct mlxsw_sp_bridge_vlan *bridge_vlan;
389 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
393 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
394 bridge_vlan->vid = vid;
395 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
401 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
403 list_del(&bridge_vlan->list);
404 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
408 static struct mlxsw_sp_bridge_vlan *
409 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
411 struct mlxsw_sp_bridge_vlan *bridge_vlan;
413 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
417 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
420 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
422 if (list_empty(&bridge_vlan->port_vlan_list))
423 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
426 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
427 struct net_device *dev,
428 unsigned long *brport_flags)
430 struct mlxsw_sp_bridge_port *bridge_port;
432 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
433 if (WARN_ON(!bridge_port))
436 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
439 static int mlxsw_sp_port_attr_get(struct net_device *dev,
440 struct switchdev_attr *attr)
442 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
443 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
446 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
447 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
448 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
449 attr->u.ppid.id_len);
451 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
452 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
453 &attr->u.brport_flags);
455 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
456 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
467 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
468 struct mlxsw_sp_bridge_vlan *bridge_vlan,
471 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
473 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
475 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
477 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
478 bridge_vlan->vid, state);
484 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
485 struct switchdev_trans *trans,
486 struct net_device *orig_dev,
489 struct mlxsw_sp_bridge_port *bridge_port;
490 struct mlxsw_sp_bridge_vlan *bridge_vlan;
493 if (switchdev_trans_ph_prepare(trans))
496 /* It's possible we failed to enslave the port, yet this
497 * operation is executed due to it being deferred.
499 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
504 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
505 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
508 goto err_port_bridge_vlan_stp_set;
511 bridge_port->stp_state = state;
515 err_port_bridge_vlan_stp_set:
516 list_for_each_entry_continue_reverse(bridge_vlan,
517 &bridge_port->vlans_list, list)
518 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
519 bridge_port->stp_state);
524 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
525 struct mlxsw_sp_bridge_vlan *bridge_vlan,
526 enum mlxsw_sp_flood_type packet_type,
529 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
531 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
533 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
535 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
537 mlxsw_sp_port->local_port,
545 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
546 struct mlxsw_sp_bridge_port *bridge_port,
547 enum mlxsw_sp_flood_type packet_type,
550 struct mlxsw_sp_bridge_vlan *bridge_vlan;
553 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
554 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
559 goto err_port_bridge_vlan_flood_set;
564 err_port_bridge_vlan_flood_set:
565 list_for_each_entry_continue_reverse(bridge_vlan,
566 &bridge_port->vlans_list, list)
567 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
568 packet_type, !member);
573 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
574 struct mlxsw_sp_bridge_vlan *bridge_vlan,
577 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
578 u16 vid = bridge_vlan->vid;
580 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
582 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
584 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
591 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
592 struct mlxsw_sp_bridge_port *bridge_port,
595 struct mlxsw_sp_bridge_vlan *bridge_vlan;
598 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
599 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
602 goto err_port_bridge_vlan_learning_set;
607 err_port_bridge_vlan_learning_set:
608 list_for_each_entry_continue_reverse(bridge_vlan,
609 &bridge_port->vlans_list, list)
610 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
615 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
616 struct switchdev_trans *trans,
617 struct net_device *orig_dev,
618 unsigned long brport_flags)
620 struct mlxsw_sp_bridge_port *bridge_port;
623 if (switchdev_trans_ph_prepare(trans))
626 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
631 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
632 MLXSW_SP_FLOOD_TYPE_UC,
633 brport_flags & BR_FLOOD);
637 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
638 brport_flags & BR_LEARNING);
642 if (bridge_port->bridge_device->multicast_enabled)
645 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
646 MLXSW_SP_FLOOD_TYPE_MC,
653 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
657 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
659 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
662 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
663 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
666 mlxsw_sp->bridge->ageing_time = ageing_time;
670 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
671 struct switchdev_trans *trans,
672 unsigned long ageing_clock_t)
674 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
675 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
676 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
678 if (switchdev_trans_ph_prepare(trans)) {
679 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
680 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
686 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
689 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
690 struct switchdev_trans *trans,
691 struct net_device *orig_dev,
694 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
695 struct mlxsw_sp_bridge_device *bridge_device;
697 if (!switchdev_trans_ph_prepare(trans))
700 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
701 if (WARN_ON(!bridge_device))
704 if (bridge_device->vlan_enabled == vlan_enabled)
707 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
711 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
712 struct switchdev_trans *trans,
713 struct net_device *orig_dev,
714 bool is_port_mrouter)
716 struct mlxsw_sp_bridge_port *bridge_port;
719 if (switchdev_trans_ph_prepare(trans))
722 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
727 if (!bridge_port->bridge_device->multicast_enabled)
730 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
731 MLXSW_SP_FLOOD_TYPE_MC,
736 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
739 bridge_port->mrouter = is_port_mrouter;
743 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
745 const struct mlxsw_sp_bridge_device *bridge_device;
747 bridge_device = bridge_port->bridge_device;
748 return bridge_device->multicast_enabled ? bridge_port->mrouter :
749 bridge_port->flags & BR_MCAST_FLOOD;
752 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
753 struct switchdev_trans *trans,
754 struct net_device *orig_dev,
757 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
758 struct mlxsw_sp_bridge_device *bridge_device;
759 struct mlxsw_sp_bridge_port *bridge_port;
762 if (switchdev_trans_ph_prepare(trans))
765 /* It's possible we failed to enslave the port, yet this
766 * operation is executed due to it being deferred.
768 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
772 if (bridge_device->multicast_enabled != !mc_disabled) {
773 bridge_device->multicast_enabled = !mc_disabled;
774 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
778 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
779 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
780 bool member = mlxsw_sp_mc_flood(bridge_port);
782 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
784 packet_type, member);
789 bridge_device->multicast_enabled = !mc_disabled;
794 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
795 u16 mid_idx, bool add)
800 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
804 mlxsw_reg_smid_pack(smid_pl, mid_idx,
805 mlxsw_sp_router_port(mlxsw_sp), add);
806 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
812 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
813 struct mlxsw_sp_bridge_device *bridge_device,
816 struct mlxsw_sp_mid *mid;
818 list_for_each_entry(mid, &bridge_device->mids_list, list)
819 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
823 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
824 struct switchdev_trans *trans,
825 struct net_device *orig_dev,
828 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
829 struct mlxsw_sp_bridge_device *bridge_device;
831 if (switchdev_trans_ph_prepare(trans))
834 /* It's possible we failed to enslave the port, yet this
835 * operation is executed due to it being deferred.
837 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
841 if (bridge_device->mrouter != is_mrouter)
842 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
844 bridge_device->mrouter = is_mrouter;
848 static int mlxsw_sp_port_attr_set(struct net_device *dev,
849 const struct switchdev_attr *attr,
850 struct switchdev_trans *trans)
852 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
856 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
857 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
861 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
862 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
864 attr->u.brport_flags);
866 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
867 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
868 attr->u.ageing_time);
870 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
871 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
873 attr->u.vlan_filtering);
875 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
876 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
880 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
881 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
883 attr->u.mc_disabled);
885 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
886 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
895 if (switchdev_trans_ph_commit(trans))
896 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
902 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
903 struct mlxsw_sp_bridge_port *bridge_port)
905 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
906 struct mlxsw_sp_bridge_device *bridge_device;
907 u8 local_port = mlxsw_sp_port->local_port;
908 u16 vid = mlxsw_sp_port_vlan->vid;
909 struct mlxsw_sp_fid *fid;
912 bridge_device = bridge_port->bridge_device;
913 fid = bridge_device->ops->fid_get(bridge_device, vid);
917 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
918 bridge_port->flags & BR_FLOOD);
920 goto err_fid_uc_flood_set;
922 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
923 mlxsw_sp_mc_flood(bridge_port));
925 goto err_fid_mc_flood_set;
927 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
930 goto err_fid_bc_flood_set;
932 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
934 goto err_fid_port_vid_map;
936 mlxsw_sp_port_vlan->fid = fid;
940 err_fid_port_vid_map:
941 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
942 err_fid_bc_flood_set:
943 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
944 err_fid_mc_flood_set:
945 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
946 err_fid_uc_flood_set:
947 mlxsw_sp_fid_put(fid);
952 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
954 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
955 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
956 u8 local_port = mlxsw_sp_port->local_port;
957 u16 vid = mlxsw_sp_port_vlan->vid;
959 mlxsw_sp_port_vlan->fid = NULL;
960 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
961 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
962 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
963 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
964 mlxsw_sp_fid_put(fid);
968 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
969 u16 vid, bool is_pvid)
973 else if (mlxsw_sp_port->pvid == vid)
974 return 0; /* Dis-allow untagged packets */
976 return mlxsw_sp_port->pvid;
980 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
981 struct mlxsw_sp_bridge_port *bridge_port)
983 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
984 struct mlxsw_sp_bridge_vlan *bridge_vlan;
985 u16 vid = mlxsw_sp_port_vlan->vid;
988 /* No need to continue if only VLAN flags were changed */
989 if (mlxsw_sp_port_vlan->bridge_port) {
990 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
994 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
998 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
999 bridge_port->flags & BR_LEARNING);
1001 goto err_port_vid_learning_set;
1003 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1004 bridge_port->stp_state);
1006 goto err_port_vid_stp_set;
1008 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1011 goto err_bridge_vlan_get;
1014 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1015 &bridge_vlan->port_vlan_list);
1017 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1019 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1023 err_bridge_vlan_get:
1024 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1025 err_port_vid_stp_set:
1026 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1027 err_port_vid_learning_set:
1028 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1033 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1035 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1036 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1037 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1038 struct mlxsw_sp_bridge_port *bridge_port;
1039 u16 vid = mlxsw_sp_port_vlan->vid;
1040 bool last_port, last_vlan;
1042 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1043 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1046 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1047 last_vlan = list_is_singular(&bridge_port->vlans_list);
1048 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1049 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1051 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1052 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1053 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1054 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1056 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1058 mlxsw_sp_fid_index(fid));
1060 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1062 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1064 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1065 mlxsw_sp_port_vlan->bridge_port = NULL;
1069 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1070 struct mlxsw_sp_bridge_port *bridge_port,
1071 u16 vid, bool is_untagged, bool is_pvid)
1073 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1074 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1075 u16 old_pvid = mlxsw_sp_port->pvid;
1078 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1079 if (IS_ERR(mlxsw_sp_port_vlan))
1080 return PTR_ERR(mlxsw_sp_port_vlan);
1082 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1085 goto err_port_vlan_set;
1087 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1089 goto err_port_pvid_set;
1091 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1093 goto err_port_vlan_bridge_join;
1097 err_port_vlan_bridge_join:
1098 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1100 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1102 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1107 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1108 const struct net_device *br_dev,
1109 const struct switchdev_obj_port_vlan *vlan)
1111 struct mlxsw_sp_rif *rif;
1112 struct mlxsw_sp_fid *fid;
1116 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev);
1119 fid = mlxsw_sp_rif_fid(rif);
1120 pvid = mlxsw_sp_fid_8021q_vid(fid);
1122 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1123 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1125 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1130 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1139 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1140 const struct switchdev_obj_port_vlan *vlan,
1141 struct switchdev_trans *trans)
1143 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1144 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1145 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1146 struct net_device *orig_dev = vlan->obj.orig_dev;
1147 struct mlxsw_sp_bridge_port *bridge_port;
1150 if (netif_is_bridge_master(orig_dev)) {
1153 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1154 br_vlan_enabled(orig_dev) &&
1155 switchdev_trans_ph_prepare(trans))
1156 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1163 if (switchdev_trans_ph_prepare(trans))
1166 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1167 if (WARN_ON(!bridge_port))
1170 if (!bridge_port->bridge_device->vlan_enabled)
1173 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1176 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1186 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1188 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1189 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1193 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1194 struct mlxsw_sp_bridge_port *bridge_port,
1197 bool lagged = bridge_port->lagged;
1198 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1201 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1202 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1203 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1204 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1206 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1209 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1211 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1212 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1215 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1217 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1218 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1221 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1222 const char *mac, u16 fid, bool adding,
1223 enum mlxsw_reg_sfd_rec_action action,
1224 enum mlxsw_reg_sfd_rec_policy policy)
1230 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1234 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1235 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1236 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1237 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1241 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1249 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1250 const char *mac, u16 fid, bool adding,
1253 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1254 MLXSW_REG_SFD_REC_ACTION_NOP,
1255 mlxsw_sp_sfd_rec_policy(dynamic));
1258 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1261 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1262 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1263 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1266 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1267 const char *mac, u16 fid, u16 lag_vid,
1268 bool adding, bool dynamic)
1274 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1278 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1279 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1280 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
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))
1296 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1297 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1299 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1300 struct net_device *orig_dev = fdb_info->info.dev;
1301 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1302 struct mlxsw_sp_bridge_device *bridge_device;
1303 struct mlxsw_sp_bridge_port *bridge_port;
1306 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1310 bridge_device = bridge_port->bridge_device;
1311 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1314 if (!mlxsw_sp_port_vlan)
1317 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1318 vid = mlxsw_sp_port_vlan->vid;
1320 if (!bridge_port->lagged)
1321 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1322 bridge_port->system_port,
1323 fdb_info->addr, fid_index,
1326 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1327 bridge_port->lag_id,
1328 fdb_info->addr, fid_index,
1329 vid, adding, false);
1332 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1333 u16 fid, u16 mid_idx, bool adding)
1339 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1343 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1344 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1345 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1346 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1347 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1351 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1359 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1361 bool set_router_port)
1366 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1370 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1371 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1372 if (mlxsw_sp->ports[i])
1373 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1376 mlxsw_reg_smid_port_mask_set(smid_pl,
1377 mlxsw_sp_router_port(mlxsw_sp), 1);
1379 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1380 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1382 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1385 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1390 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1391 u16 mid_idx, bool add)
1393 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1397 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1401 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1402 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1408 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1409 const unsigned char *addr,
1412 struct mlxsw_sp_mid *mid;
1414 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1415 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1422 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1423 struct mlxsw_sp_bridge_port *bridge_port,
1424 unsigned long *ports_bitmap)
1426 struct mlxsw_sp_port *mlxsw_sp_port;
1427 u64 max_lag_members, i;
1430 if (!bridge_port->lagged) {
1431 set_bit(bridge_port->system_port, ports_bitmap);
1433 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1435 lag_id = bridge_port->lag_id;
1436 for (i = 0; i < max_lag_members; i++) {
1437 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1440 set_bit(mlxsw_sp_port->local_port,
1447 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1448 struct mlxsw_sp_bridge_device *bridge_device,
1449 struct mlxsw_sp *mlxsw_sp)
1451 struct mlxsw_sp_bridge_port *bridge_port;
1453 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1454 if (bridge_port->mrouter) {
1455 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1463 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1464 struct mlxsw_sp_mid *mid,
1465 struct mlxsw_sp_bridge_device *bridge_device)
1473 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1475 if (mid_idx == MLXSW_SP_MID_MAX)
1478 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1479 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1480 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1484 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1485 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1488 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1489 bridge_device->mrouter);
1490 kfree(flood_bitmap);
1494 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1499 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1504 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1505 struct mlxsw_sp_mid *mid)
1510 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1512 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1517 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1518 struct mlxsw_sp_bridge_device *bridge_device,
1519 const unsigned char *addr,
1522 struct mlxsw_sp_mid *mid;
1525 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1529 alloc_size = sizeof(unsigned long) *
1530 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1532 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1533 if (!mid->ports_in_mid)
1534 goto err_ports_in_mid_alloc;
1536 ether_addr_copy(mid->addr, addr);
1540 if (!bridge_device->multicast_enabled)
1543 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1544 goto err_write_mdb_entry;
1547 list_add_tail(&mid->list, &bridge_device->mids_list);
1550 err_write_mdb_entry:
1551 kfree(mid->ports_in_mid);
1552 err_ports_in_mid_alloc:
1557 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1558 struct mlxsw_sp_mid *mid)
1560 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1563 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1564 if (bitmap_empty(mid->ports_in_mid,
1565 mlxsw_core_max_ports(mlxsw_sp->core))) {
1566 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1567 list_del(&mid->list);
1568 kfree(mid->ports_in_mid);
1574 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1575 const struct switchdev_obj_port_mdb *mdb,
1576 struct switchdev_trans *trans)
1578 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1579 struct net_device *orig_dev = mdb->obj.orig_dev;
1580 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1581 struct net_device *dev = mlxsw_sp_port->dev;
1582 struct mlxsw_sp_bridge_device *bridge_device;
1583 struct mlxsw_sp_bridge_port *bridge_port;
1584 struct mlxsw_sp_mid *mid;
1588 if (switchdev_trans_ph_commit(trans))
1591 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1595 bridge_device = bridge_port->bridge_device;
1596 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1599 if (!mlxsw_sp_port_vlan)
1602 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1604 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1606 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1609 netdev_err(dev, "Unable to allocate MC group\n");
1613 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1615 if (!bridge_device->multicast_enabled)
1618 if (bridge_port->mrouter)
1621 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1623 netdev_err(dev, "Unable to set SMID\n");
1630 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1635 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1636 struct mlxsw_sp_bridge_device
1639 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1640 struct mlxsw_sp_mid *mid;
1643 mc_enabled = bridge_device->multicast_enabled;
1645 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1647 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1650 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1655 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1656 struct mlxsw_sp_bridge_port *bridge_port,
1659 struct mlxsw_sp_bridge_device *bridge_device;
1660 struct mlxsw_sp_mid *mid;
1662 bridge_device = bridge_port->bridge_device;
1664 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1665 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1666 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1670 struct mlxsw_sp_span_respin_work {
1671 struct work_struct work;
1672 struct mlxsw_sp *mlxsw_sp;
1675 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1677 struct mlxsw_sp_span_respin_work *respin_work =
1678 container_of(work, struct mlxsw_sp_span_respin_work, work);
1681 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1686 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1688 struct mlxsw_sp_span_respin_work *respin_work;
1690 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1694 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1695 respin_work->mlxsw_sp = mlxsw_sp;
1697 mlxsw_core_schedule_work(&respin_work->work);
1700 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1701 const struct switchdev_obj *obj,
1702 struct switchdev_trans *trans)
1704 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1705 const struct switchdev_obj_port_vlan *vlan;
1709 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1710 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1711 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1713 if (switchdev_trans_ph_prepare(trans)) {
1714 /* The event is emitted before the changes are actually
1715 * applied to the bridge. Therefore schedule the respin
1716 * call for later, so that the respin logic sees the
1717 * updated bridge state.
1719 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1722 case SWITCHDEV_OBJ_ID_PORT_MDB:
1723 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1724 SWITCHDEV_OBJ_PORT_MDB(obj),
1736 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1737 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1739 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1740 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1742 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1743 if (WARN_ON(!mlxsw_sp_port_vlan))
1746 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1747 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1748 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1749 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1752 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1753 const struct switchdev_obj_port_vlan *vlan)
1755 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1756 struct net_device *orig_dev = vlan->obj.orig_dev;
1757 struct mlxsw_sp_bridge_port *bridge_port;
1760 if (netif_is_bridge_master(orig_dev))
1763 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1764 if (WARN_ON(!bridge_port))
1767 if (!bridge_port->bridge_device->vlan_enabled)
1770 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1771 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1777 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1778 struct mlxsw_sp_bridge_port *bridge_port,
1779 struct mlxsw_sp_mid *mid)
1781 struct net_device *dev = mlxsw_sp_port->dev;
1784 if (bridge_port->bridge_device->multicast_enabled &&
1785 !bridge_port->mrouter) {
1786 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1788 netdev_err(dev, "Unable to remove port from SMID\n");
1791 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1793 netdev_err(dev, "Unable to remove MC SFD\n");
1798 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1799 const struct switchdev_obj_port_mdb *mdb)
1801 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1802 struct net_device *orig_dev = mdb->obj.orig_dev;
1803 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1804 struct mlxsw_sp_bridge_device *bridge_device;
1805 struct net_device *dev = mlxsw_sp_port->dev;
1806 struct mlxsw_sp_bridge_port *bridge_port;
1807 struct mlxsw_sp_mid *mid;
1810 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1814 bridge_device = bridge_port->bridge_device;
1815 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1818 if (!mlxsw_sp_port_vlan)
1821 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1823 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1825 netdev_err(dev, "Unable to remove port from MC DB\n");
1829 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1833 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1834 struct mlxsw_sp_bridge_port *bridge_port)
1836 struct mlxsw_sp_bridge_device *bridge_device;
1837 struct mlxsw_sp_mid *mid, *tmp;
1839 bridge_device = bridge_port->bridge_device;
1841 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1842 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1843 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1845 } else if (bridge_device->multicast_enabled &&
1846 bridge_port->mrouter) {
1847 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1852 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1853 const struct switchdev_obj *obj)
1855 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1859 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1860 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1861 SWITCHDEV_OBJ_PORT_VLAN(obj));
1863 case SWITCHDEV_OBJ_ID_PORT_MDB:
1864 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1865 SWITCHDEV_OBJ_PORT_MDB(obj));
1872 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1877 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1880 struct mlxsw_sp_port *mlxsw_sp_port;
1881 u64 max_lag_members;
1884 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1886 for (i = 0; i < max_lag_members; i++) {
1887 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1889 return mlxsw_sp_port;
1894 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1895 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1896 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1897 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1898 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1902 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1903 struct mlxsw_sp_bridge_port *bridge_port,
1904 struct mlxsw_sp_port *mlxsw_sp_port,
1905 struct netlink_ext_ack *extack)
1907 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1909 if (is_vlan_dev(bridge_port->dev)) {
1910 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1914 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1915 if (WARN_ON(!mlxsw_sp_port_vlan))
1918 /* Let VLAN-aware bridge take care of its own VLANs */
1919 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1925 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1926 struct mlxsw_sp_bridge_port *bridge_port,
1927 struct mlxsw_sp_port *mlxsw_sp_port)
1929 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1930 /* Make sure untagged frames are allowed to ingress */
1931 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1934 static struct mlxsw_sp_fid *
1935 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1938 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1940 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1943 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1944 .port_join = mlxsw_sp_bridge_8021q_port_join,
1945 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
1946 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
1950 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1951 const struct net_device *br_dev)
1953 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1955 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1957 if (mlxsw_sp_port_vlan->bridge_port &&
1958 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1967 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1968 struct mlxsw_sp_bridge_port *bridge_port,
1969 struct mlxsw_sp_port *mlxsw_sp_port,
1970 struct netlink_ext_ack *extack)
1972 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1973 struct net_device *dev = bridge_port->dev;
1976 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1977 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1978 if (WARN_ON(!mlxsw_sp_port_vlan))
1981 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1982 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
1986 /* Port is no longer usable as a router interface */
1987 if (mlxsw_sp_port_vlan->fid)
1988 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1990 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1994 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1995 struct mlxsw_sp_bridge_port *bridge_port,
1996 struct mlxsw_sp_port *mlxsw_sp_port)
1998 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1999 struct net_device *dev = bridge_port->dev;
2002 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2003 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2004 if (!mlxsw_sp_port_vlan)
2007 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2010 static struct mlxsw_sp_fid *
2011 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2014 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2016 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2019 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2020 .port_join = mlxsw_sp_bridge_8021d_port_join,
2021 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2022 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2025 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2026 struct net_device *brport_dev,
2027 struct net_device *br_dev,
2028 struct netlink_ext_ack *extack)
2030 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2031 struct mlxsw_sp_bridge_device *bridge_device;
2032 struct mlxsw_sp_bridge_port *bridge_port;
2035 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2036 if (IS_ERR(bridge_port))
2037 return PTR_ERR(bridge_port);
2038 bridge_device = bridge_port->bridge_device;
2040 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2041 mlxsw_sp_port, extack);
2048 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2052 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2053 struct net_device *brport_dev,
2054 struct net_device *br_dev)
2056 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2057 struct mlxsw_sp_bridge_device *bridge_device;
2058 struct mlxsw_sp_bridge_port *bridge_port;
2060 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2063 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2067 bridge_device->ops->port_leave(bridge_device, bridge_port,
2069 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2073 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2074 const char *mac, u16 vid,
2075 struct net_device *dev)
2077 struct switchdev_notifier_fdb_info info;
2081 call_switchdev_notifiers(type, dev, &info.info);
2084 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2085 char *sfn_pl, int rec_index,
2088 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2089 struct mlxsw_sp_bridge_device *bridge_device;
2090 struct mlxsw_sp_bridge_port *bridge_port;
2091 struct mlxsw_sp_port *mlxsw_sp_port;
2092 enum switchdev_notifier_type type;
2096 bool do_notification = true;
2099 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2100 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2101 if (!mlxsw_sp_port) {
2102 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2106 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2107 if (!mlxsw_sp_port_vlan) {
2108 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2112 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2114 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2118 bridge_device = bridge_port->bridge_device;
2119 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2122 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2125 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2129 if (!do_notification)
2131 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2132 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2138 do_notification = false;
2142 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2143 char *sfn_pl, int rec_index,
2146 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2147 struct mlxsw_sp_bridge_device *bridge_device;
2148 struct mlxsw_sp_bridge_port *bridge_port;
2149 struct mlxsw_sp_port *mlxsw_sp_port;
2150 enum switchdev_notifier_type type;
2155 bool do_notification = true;
2158 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2159 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2160 if (!mlxsw_sp_port) {
2161 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2165 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2166 if (!mlxsw_sp_port_vlan) {
2167 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2171 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2173 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2177 bridge_device = bridge_port->bridge_device;
2178 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2179 lag_vid = mlxsw_sp_port_vlan->vid;
2182 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2185 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2189 if (!do_notification)
2191 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2192 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2198 do_notification = false;
2202 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2203 char *sfn_pl, int rec_index)
2205 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2206 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2207 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2210 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2211 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2214 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2215 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2218 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2219 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2225 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2227 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2229 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2230 msecs_to_jiffies(bridge->fdb_notify.interval));
2233 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2235 struct mlxsw_sp_bridge *bridge;
2236 struct mlxsw_sp *mlxsw_sp;
2242 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2246 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2247 mlxsw_sp = bridge->mlxsw_sp;
2250 mlxsw_reg_sfn_pack(sfn_pl);
2251 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2253 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2256 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2257 for (i = 0; i < num_rec; i++)
2258 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2263 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2266 struct mlxsw_sp_switchdev_event_work {
2267 struct work_struct work;
2268 struct switchdev_notifier_fdb_info fdb_info;
2269 struct net_device *dev;
2270 unsigned long event;
2273 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2275 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2276 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2277 struct net_device *dev = switchdev_work->dev;
2278 struct switchdev_notifier_fdb_info *fdb_info;
2279 struct mlxsw_sp_port *mlxsw_sp_port;
2283 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2287 switch (switchdev_work->event) {
2288 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2289 fdb_info = &switchdev_work->fdb_info;
2290 if (!fdb_info->added_by_user)
2292 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2295 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2297 fdb_info->vid, dev);
2299 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2300 fdb_info = &switchdev_work->fdb_info;
2301 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2303 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2304 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2305 /* These events are only used to potentially update an existing
2311 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2315 kfree(switchdev_work->fdb_info.addr);
2316 kfree(switchdev_work);
2320 /* Called under rcu_read_lock() */
2321 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2322 unsigned long event, void *ptr)
2324 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2325 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2326 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2327 struct net_device *br_dev;
2329 /* Tunnel devices are not our uppers, so check their master instead */
2330 br_dev = netdev_master_upper_dev_get_rcu(dev);
2333 if (!netif_is_bridge_master(br_dev))
2335 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
2338 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2339 if (!switchdev_work)
2342 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2343 switchdev_work->dev = dev;
2344 switchdev_work->event = event;
2347 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2348 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2349 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2350 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2351 memcpy(&switchdev_work->fdb_info, ptr,
2352 sizeof(switchdev_work->fdb_info));
2353 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2354 if (!switchdev_work->fdb_info.addr)
2355 goto err_addr_alloc;
2356 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2358 /* Take a reference on the device. This can be either
2359 * upper device containig mlxsw_sp_port or just a
2365 kfree(switchdev_work);
2369 mlxsw_core_schedule_work(&switchdev_work->work);
2374 kfree(switchdev_work);
2378 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2379 .notifier_call = mlxsw_sp_switchdev_event,
2383 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2385 return bridge_port->stp_state;
2388 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2390 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2393 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2395 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2399 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2401 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2405 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2406 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2407 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2411 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2413 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2414 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2418 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2420 struct mlxsw_sp_bridge *bridge;
2422 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2425 mlxsw_sp->bridge = bridge;
2426 bridge->mlxsw_sp = mlxsw_sp;
2428 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2430 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2431 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2433 return mlxsw_sp_fdb_init(mlxsw_sp);
2436 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2438 mlxsw_sp_fdb_fini(mlxsw_sp);
2439 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2440 kfree(mlxsw_sp->bridge);
2443 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2445 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2448 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)