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));
286 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
289 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
291 /* In case ports were pulled from out of a bridged LAG, then
292 * it's possible the reference count isn't zero, yet the bridge
293 * port should be destroyed, as it's no longer an upper of ours.
295 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
297 else if (bridge_port->ref_count == 0)
303 static struct mlxsw_sp_bridge_port *
304 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
305 struct net_device *brport_dev)
307 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
308 struct mlxsw_sp_bridge_device *bridge_device;
309 struct mlxsw_sp_bridge_port *bridge_port;
312 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
314 bridge_port->ref_count++;
318 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
319 if (IS_ERR(bridge_device))
320 return ERR_CAST(bridge_device);
322 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
325 goto err_bridge_port_create;
330 err_bridge_port_create:
331 mlxsw_sp_bridge_device_put(bridge, bridge_device);
335 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
336 struct mlxsw_sp_bridge_port *bridge_port)
338 struct mlxsw_sp_bridge_device *bridge_device;
340 bridge_port->ref_count--;
341 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
343 bridge_device = bridge_port->bridge_device;
344 mlxsw_sp_bridge_port_destroy(bridge_port);
345 mlxsw_sp_bridge_device_put(bridge, bridge_device);
348 static struct mlxsw_sp_port_vlan *
349 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
350 const struct mlxsw_sp_bridge_device *
354 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
356 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
358 if (!mlxsw_sp_port_vlan->bridge_port)
360 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
363 if (bridge_device->vlan_enabled &&
364 mlxsw_sp_port_vlan->vid != vid)
366 return mlxsw_sp_port_vlan;
372 static struct mlxsw_sp_port_vlan*
373 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
376 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
378 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
380 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
382 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
383 return mlxsw_sp_port_vlan;
389 static struct mlxsw_sp_bridge_vlan *
390 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
393 struct mlxsw_sp_bridge_vlan *bridge_vlan;
395 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
396 if (bridge_vlan->vid == vid)
403 static struct mlxsw_sp_bridge_vlan *
404 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
406 struct mlxsw_sp_bridge_vlan *bridge_vlan;
408 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
412 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
413 bridge_vlan->vid = vid;
414 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
420 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
422 list_del(&bridge_vlan->list);
423 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
427 static struct mlxsw_sp_bridge_vlan *
428 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
430 struct mlxsw_sp_bridge_vlan *bridge_vlan;
432 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
436 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
439 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
441 if (list_empty(&bridge_vlan->port_vlan_list))
442 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
445 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
446 struct net_device *dev,
447 unsigned long *brport_flags)
449 struct mlxsw_sp_bridge_port *bridge_port;
451 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
452 if (WARN_ON(!bridge_port))
455 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
458 static int mlxsw_sp_port_attr_get(struct net_device *dev,
459 struct switchdev_attr *attr)
461 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
462 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
465 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
466 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
467 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
468 attr->u.ppid.id_len);
470 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
471 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
472 &attr->u.brport_flags);
474 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
475 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
486 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
487 struct mlxsw_sp_bridge_vlan *bridge_vlan,
490 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
492 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
494 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
496 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
497 bridge_vlan->vid, state);
503 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
504 struct switchdev_trans *trans,
505 struct net_device *orig_dev,
508 struct mlxsw_sp_bridge_port *bridge_port;
509 struct mlxsw_sp_bridge_vlan *bridge_vlan;
512 if (switchdev_trans_ph_prepare(trans))
515 /* It's possible we failed to enslave the port, yet this
516 * operation is executed due to it being deferred.
518 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
523 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
524 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
527 goto err_port_bridge_vlan_stp_set;
530 bridge_port->stp_state = state;
534 err_port_bridge_vlan_stp_set:
535 list_for_each_entry_continue_reverse(bridge_vlan,
536 &bridge_port->vlans_list, list)
537 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
538 bridge_port->stp_state);
543 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
544 struct mlxsw_sp_bridge_vlan *bridge_vlan,
545 enum mlxsw_sp_flood_type packet_type,
548 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
550 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
552 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
554 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
556 mlxsw_sp_port->local_port,
564 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
565 struct mlxsw_sp_bridge_port *bridge_port,
566 enum mlxsw_sp_flood_type packet_type,
569 struct mlxsw_sp_bridge_vlan *bridge_vlan;
572 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
573 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
578 goto err_port_bridge_vlan_flood_set;
583 err_port_bridge_vlan_flood_set:
584 list_for_each_entry_continue_reverse(bridge_vlan,
585 &bridge_port->vlans_list, list)
586 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
587 packet_type, !member);
592 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
593 struct mlxsw_sp_bridge_vlan *bridge_vlan,
596 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
597 u16 vid = bridge_vlan->vid;
599 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
601 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
603 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
610 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
611 struct mlxsw_sp_bridge_port *bridge_port,
614 struct mlxsw_sp_bridge_vlan *bridge_vlan;
617 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
618 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
621 goto err_port_bridge_vlan_learning_set;
626 err_port_bridge_vlan_learning_set:
627 list_for_each_entry_continue_reverse(bridge_vlan,
628 &bridge_port->vlans_list, list)
629 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
634 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
635 struct switchdev_trans *trans,
636 struct net_device *orig_dev,
637 unsigned long brport_flags)
639 struct mlxsw_sp_bridge_port *bridge_port;
642 if (switchdev_trans_ph_prepare(trans))
645 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
650 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
651 MLXSW_SP_FLOOD_TYPE_UC,
652 brport_flags & BR_FLOOD);
656 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
657 brport_flags & BR_LEARNING);
661 if (bridge_port->bridge_device->multicast_enabled)
664 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
665 MLXSW_SP_FLOOD_TYPE_MC,
672 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
676 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
678 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
681 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
682 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
685 mlxsw_sp->bridge->ageing_time = ageing_time;
689 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
690 struct switchdev_trans *trans,
691 unsigned long ageing_clock_t)
693 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
694 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
695 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
697 if (switchdev_trans_ph_prepare(trans)) {
698 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
699 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
705 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
708 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
709 struct switchdev_trans *trans,
710 struct net_device *orig_dev,
713 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
714 struct mlxsw_sp_bridge_device *bridge_device;
716 if (!switchdev_trans_ph_prepare(trans))
719 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
720 if (WARN_ON(!bridge_device))
723 if (bridge_device->vlan_enabled == vlan_enabled)
726 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
730 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
731 struct switchdev_trans *trans,
732 struct net_device *orig_dev,
733 bool is_port_mrouter)
735 struct mlxsw_sp_bridge_port *bridge_port;
738 if (switchdev_trans_ph_prepare(trans))
741 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
746 if (!bridge_port->bridge_device->multicast_enabled)
749 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
750 MLXSW_SP_FLOOD_TYPE_MC,
755 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
758 bridge_port->mrouter = is_port_mrouter;
762 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
764 const struct mlxsw_sp_bridge_device *bridge_device;
766 bridge_device = bridge_port->bridge_device;
767 return bridge_device->multicast_enabled ? bridge_port->mrouter :
768 bridge_port->flags & BR_MCAST_FLOOD;
771 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
772 struct switchdev_trans *trans,
773 struct net_device *orig_dev,
776 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
777 struct mlxsw_sp_bridge_device *bridge_device;
778 struct mlxsw_sp_bridge_port *bridge_port;
781 if (switchdev_trans_ph_prepare(trans))
784 /* It's possible we failed to enslave the port, yet this
785 * operation is executed due to it being deferred.
787 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
791 if (bridge_device->multicast_enabled != !mc_disabled) {
792 bridge_device->multicast_enabled = !mc_disabled;
793 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
797 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
798 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
799 bool member = mlxsw_sp_mc_flood(bridge_port);
801 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
803 packet_type, member);
808 bridge_device->multicast_enabled = !mc_disabled;
813 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
814 u16 mid_idx, bool add)
819 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
823 mlxsw_reg_smid_pack(smid_pl, mid_idx,
824 mlxsw_sp_router_port(mlxsw_sp), add);
825 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
831 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
832 struct mlxsw_sp_bridge_device *bridge_device,
835 struct mlxsw_sp_mid *mid;
837 list_for_each_entry(mid, &bridge_device->mids_list, list)
838 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
842 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
843 struct switchdev_trans *trans,
844 struct net_device *orig_dev,
847 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
848 struct mlxsw_sp_bridge_device *bridge_device;
850 if (switchdev_trans_ph_prepare(trans))
853 /* It's possible we failed to enslave the port, yet this
854 * operation is executed due to it being deferred.
856 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
860 if (bridge_device->mrouter != is_mrouter)
861 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
863 bridge_device->mrouter = is_mrouter;
867 static int mlxsw_sp_port_attr_set(struct net_device *dev,
868 const struct switchdev_attr *attr,
869 struct switchdev_trans *trans)
871 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
875 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
876 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
880 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
881 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
883 attr->u.brport_flags);
885 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
886 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
887 attr->u.ageing_time);
889 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
890 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
892 attr->u.vlan_filtering);
894 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
895 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
899 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
900 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
902 attr->u.mc_disabled);
904 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
905 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
914 if (switchdev_trans_ph_commit(trans))
915 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
921 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
922 struct mlxsw_sp_bridge_port *bridge_port)
924 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
925 struct mlxsw_sp_bridge_device *bridge_device;
926 u8 local_port = mlxsw_sp_port->local_port;
927 u16 vid = mlxsw_sp_port_vlan->vid;
928 struct mlxsw_sp_fid *fid;
931 bridge_device = bridge_port->bridge_device;
932 fid = bridge_device->ops->fid_get(bridge_device, vid);
936 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
937 bridge_port->flags & BR_FLOOD);
939 goto err_fid_uc_flood_set;
941 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
942 mlxsw_sp_mc_flood(bridge_port));
944 goto err_fid_mc_flood_set;
946 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
949 goto err_fid_bc_flood_set;
951 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
953 goto err_fid_port_vid_map;
955 mlxsw_sp_port_vlan->fid = fid;
959 err_fid_port_vid_map:
960 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
961 err_fid_bc_flood_set:
962 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
963 err_fid_mc_flood_set:
964 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
965 err_fid_uc_flood_set:
966 mlxsw_sp_fid_put(fid);
971 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
973 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
974 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
975 u8 local_port = mlxsw_sp_port->local_port;
976 u16 vid = mlxsw_sp_port_vlan->vid;
978 mlxsw_sp_port_vlan->fid = NULL;
979 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
980 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
981 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
982 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
983 mlxsw_sp_fid_put(fid);
987 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
988 u16 vid, bool is_pvid)
992 else if (mlxsw_sp_port->pvid == vid)
993 return 0; /* Dis-allow untagged packets */
995 return mlxsw_sp_port->pvid;
999 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1000 struct mlxsw_sp_bridge_port *bridge_port)
1002 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1003 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1004 u16 vid = mlxsw_sp_port_vlan->vid;
1007 /* No need to continue if only VLAN flags were changed */
1008 if (mlxsw_sp_port_vlan->bridge_port) {
1009 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1013 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1017 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1018 bridge_port->flags & BR_LEARNING);
1020 goto err_port_vid_learning_set;
1022 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1023 bridge_port->stp_state);
1025 goto err_port_vid_stp_set;
1027 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1030 goto err_bridge_vlan_get;
1033 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1034 &bridge_vlan->port_vlan_list);
1036 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1038 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1042 err_bridge_vlan_get:
1043 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1044 err_port_vid_stp_set:
1045 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1046 err_port_vid_learning_set:
1047 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1052 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1054 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1055 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1056 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1057 struct mlxsw_sp_bridge_port *bridge_port;
1058 u16 vid = mlxsw_sp_port_vlan->vid;
1059 bool last_port, last_vlan;
1061 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1062 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1065 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1066 last_vlan = list_is_singular(&bridge_port->vlans_list);
1067 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1068 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1070 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1071 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1072 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1073 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1075 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1077 mlxsw_sp_fid_index(fid));
1079 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1081 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1083 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1084 mlxsw_sp_port_vlan->bridge_port = NULL;
1088 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1089 struct mlxsw_sp_bridge_port *bridge_port,
1090 u16 vid, bool is_untagged, bool is_pvid)
1092 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1093 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1094 u16 old_pvid = mlxsw_sp_port->pvid;
1097 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1098 if (IS_ERR(mlxsw_sp_port_vlan))
1099 return PTR_ERR(mlxsw_sp_port_vlan);
1101 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1104 goto err_port_vlan_set;
1106 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1108 goto err_port_pvid_set;
1110 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1112 goto err_port_vlan_bridge_join;
1116 err_port_vlan_bridge_join:
1117 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1119 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1121 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1126 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1127 const struct net_device *br_dev,
1128 const struct switchdev_obj_port_vlan *vlan)
1130 struct mlxsw_sp_rif *rif;
1131 struct mlxsw_sp_fid *fid;
1135 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, br_dev);
1138 fid = mlxsw_sp_rif_fid(rif);
1139 pvid = mlxsw_sp_fid_8021q_vid(fid);
1141 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1142 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1144 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1149 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1158 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1159 const struct switchdev_obj_port_vlan *vlan,
1160 struct switchdev_trans *trans)
1162 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1163 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1164 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1165 struct net_device *orig_dev = vlan->obj.orig_dev;
1166 struct mlxsw_sp_bridge_port *bridge_port;
1169 if (netif_is_bridge_master(orig_dev)) {
1172 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1173 br_vlan_enabled(orig_dev) &&
1174 switchdev_trans_ph_prepare(trans))
1175 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1182 if (switchdev_trans_ph_prepare(trans))
1185 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1186 if (WARN_ON(!bridge_port))
1189 if (!bridge_port->bridge_device->vlan_enabled)
1192 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1195 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1205 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1207 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1208 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1212 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1213 struct mlxsw_sp_bridge_port *bridge_port,
1216 bool lagged = bridge_port->lagged;
1217 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1220 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1221 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1222 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1223 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1225 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1228 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1230 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1231 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1234 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1236 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1237 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1240 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1241 const char *mac, u16 fid, bool adding,
1242 enum mlxsw_reg_sfd_rec_action action,
1249 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1253 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1254 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1255 mac, fid, action, local_port);
1256 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1257 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1261 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1269 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1270 const char *mac, u16 fid, bool adding,
1273 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1274 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1277 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1280 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1281 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1285 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1286 const char *mac, u16 fid, u16 lag_vid,
1287 bool adding, bool dynamic)
1293 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1297 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1298 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1299 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1301 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1302 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1306 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1315 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1316 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1318 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1319 struct net_device *orig_dev = fdb_info->info.dev;
1320 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1321 struct mlxsw_sp_bridge_device *bridge_device;
1322 struct mlxsw_sp_bridge_port *bridge_port;
1325 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1329 bridge_device = bridge_port->bridge_device;
1330 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1333 if (!mlxsw_sp_port_vlan)
1336 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1337 vid = mlxsw_sp_port_vlan->vid;
1339 if (!bridge_port->lagged)
1340 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1341 bridge_port->system_port,
1342 fdb_info->addr, fid_index,
1345 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1346 bridge_port->lag_id,
1347 fdb_info->addr, fid_index,
1348 vid, adding, false);
1351 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1352 u16 fid, u16 mid_idx, bool adding)
1358 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1362 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1363 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1364 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1365 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1366 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1370 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1378 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1380 bool set_router_port)
1385 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1389 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1390 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1391 if (mlxsw_sp->ports[i])
1392 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1395 mlxsw_reg_smid_port_mask_set(smid_pl,
1396 mlxsw_sp_router_port(mlxsw_sp), 1);
1398 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1399 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1401 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1404 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1409 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1410 u16 mid_idx, bool add)
1412 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1416 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1420 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1421 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1427 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1428 const unsigned char *addr,
1431 struct mlxsw_sp_mid *mid;
1433 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1434 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1441 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1442 struct mlxsw_sp_bridge_port *bridge_port,
1443 unsigned long *ports_bitmap)
1445 struct mlxsw_sp_port *mlxsw_sp_port;
1446 u64 max_lag_members, i;
1449 if (!bridge_port->lagged) {
1450 set_bit(bridge_port->system_port, ports_bitmap);
1452 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1454 lag_id = bridge_port->lag_id;
1455 for (i = 0; i < max_lag_members; i++) {
1456 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1459 set_bit(mlxsw_sp_port->local_port,
1466 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1467 struct mlxsw_sp_bridge_device *bridge_device,
1468 struct mlxsw_sp *mlxsw_sp)
1470 struct mlxsw_sp_bridge_port *bridge_port;
1472 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1473 if (bridge_port->mrouter) {
1474 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1482 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1483 struct mlxsw_sp_mid *mid,
1484 struct mlxsw_sp_bridge_device *bridge_device)
1492 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1494 if (mid_idx == MLXSW_SP_MID_MAX)
1497 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1498 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1499 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1503 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1504 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1507 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1508 bridge_device->mrouter);
1509 kfree(flood_bitmap);
1513 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1518 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1523 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1524 struct mlxsw_sp_mid *mid)
1529 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1531 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1536 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1537 struct mlxsw_sp_bridge_device *bridge_device,
1538 const unsigned char *addr,
1541 struct mlxsw_sp_mid *mid;
1544 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1548 alloc_size = sizeof(unsigned long) *
1549 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1551 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1552 if (!mid->ports_in_mid)
1553 goto err_ports_in_mid_alloc;
1555 ether_addr_copy(mid->addr, addr);
1559 if (!bridge_device->multicast_enabled)
1562 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1563 goto err_write_mdb_entry;
1566 list_add_tail(&mid->list, &bridge_device->mids_list);
1569 err_write_mdb_entry:
1570 kfree(mid->ports_in_mid);
1571 err_ports_in_mid_alloc:
1576 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1577 struct mlxsw_sp_mid *mid)
1579 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1582 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1583 if (bitmap_empty(mid->ports_in_mid,
1584 mlxsw_core_max_ports(mlxsw_sp->core))) {
1585 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1586 list_del(&mid->list);
1587 kfree(mid->ports_in_mid);
1593 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1594 const struct switchdev_obj_port_mdb *mdb,
1595 struct switchdev_trans *trans)
1597 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1598 struct net_device *orig_dev = mdb->obj.orig_dev;
1599 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1600 struct net_device *dev = mlxsw_sp_port->dev;
1601 struct mlxsw_sp_bridge_device *bridge_device;
1602 struct mlxsw_sp_bridge_port *bridge_port;
1603 struct mlxsw_sp_mid *mid;
1607 if (switchdev_trans_ph_prepare(trans))
1610 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1614 bridge_device = bridge_port->bridge_device;
1615 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1618 if (!mlxsw_sp_port_vlan)
1621 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1623 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1625 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1628 netdev_err(dev, "Unable to allocate MC group\n");
1632 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1634 if (!bridge_device->multicast_enabled)
1637 if (bridge_port->mrouter)
1640 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1642 netdev_err(dev, "Unable to set SMID\n");
1649 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1654 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1655 struct mlxsw_sp_bridge_device
1658 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1659 struct mlxsw_sp_mid *mid;
1662 mc_enabled = bridge_device->multicast_enabled;
1664 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1666 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1669 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1674 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1675 struct mlxsw_sp_bridge_port *bridge_port,
1678 struct mlxsw_sp_bridge_device *bridge_device;
1679 struct mlxsw_sp_mid *mid;
1681 bridge_device = bridge_port->bridge_device;
1683 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1684 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1685 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1689 struct mlxsw_sp_span_respin_work {
1690 struct work_struct work;
1691 struct mlxsw_sp *mlxsw_sp;
1694 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1696 struct mlxsw_sp_span_respin_work *respin_work =
1697 container_of(work, struct mlxsw_sp_span_respin_work, work);
1700 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1705 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1707 struct mlxsw_sp_span_respin_work *respin_work;
1709 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1713 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1714 respin_work->mlxsw_sp = mlxsw_sp;
1716 mlxsw_core_schedule_work(&respin_work->work);
1719 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1720 const struct switchdev_obj *obj,
1721 struct switchdev_trans *trans)
1723 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1724 const struct switchdev_obj_port_vlan *vlan;
1728 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1729 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1730 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1732 if (switchdev_trans_ph_prepare(trans)) {
1733 /* The event is emitted before the changes are actually
1734 * applied to the bridge. Therefore schedule the respin
1735 * call for later, so that the respin logic sees the
1736 * updated bridge state.
1738 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1741 case SWITCHDEV_OBJ_ID_PORT_MDB:
1742 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1743 SWITCHDEV_OBJ_PORT_MDB(obj),
1755 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1756 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1758 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1759 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1761 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1762 if (WARN_ON(!mlxsw_sp_port_vlan))
1765 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1766 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1767 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1768 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1771 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1772 const struct switchdev_obj_port_vlan *vlan)
1774 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1775 struct net_device *orig_dev = vlan->obj.orig_dev;
1776 struct mlxsw_sp_bridge_port *bridge_port;
1779 if (netif_is_bridge_master(orig_dev))
1782 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1783 if (WARN_ON(!bridge_port))
1786 if (!bridge_port->bridge_device->vlan_enabled)
1789 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1790 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1796 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1797 struct mlxsw_sp_bridge_port *bridge_port,
1798 struct mlxsw_sp_mid *mid)
1800 struct net_device *dev = mlxsw_sp_port->dev;
1803 if (bridge_port->bridge_device->multicast_enabled &&
1804 !bridge_port->mrouter) {
1805 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1807 netdev_err(dev, "Unable to remove port from SMID\n");
1810 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1812 netdev_err(dev, "Unable to remove MC SFD\n");
1817 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1818 const struct switchdev_obj_port_mdb *mdb)
1820 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1821 struct net_device *orig_dev = mdb->obj.orig_dev;
1822 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1823 struct mlxsw_sp_bridge_device *bridge_device;
1824 struct net_device *dev = mlxsw_sp_port->dev;
1825 struct mlxsw_sp_bridge_port *bridge_port;
1826 struct mlxsw_sp_mid *mid;
1829 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1833 bridge_device = bridge_port->bridge_device;
1834 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1837 if (!mlxsw_sp_port_vlan)
1840 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1842 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1844 netdev_err(dev, "Unable to remove port from MC DB\n");
1848 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1852 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1853 struct mlxsw_sp_bridge_port *bridge_port)
1855 struct mlxsw_sp_bridge_device *bridge_device;
1856 struct mlxsw_sp_mid *mid, *tmp;
1858 bridge_device = bridge_port->bridge_device;
1860 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1861 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1862 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1864 } else if (bridge_device->multicast_enabled &&
1865 bridge_port->mrouter) {
1866 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1871 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1872 const struct switchdev_obj *obj)
1874 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1878 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1879 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1880 SWITCHDEV_OBJ_PORT_VLAN(obj));
1882 case SWITCHDEV_OBJ_ID_PORT_MDB:
1883 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1884 SWITCHDEV_OBJ_PORT_MDB(obj));
1891 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1896 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1899 struct mlxsw_sp_port *mlxsw_sp_port;
1900 u64 max_lag_members;
1903 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1905 for (i = 0; i < max_lag_members; i++) {
1906 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1908 return mlxsw_sp_port;
1913 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1914 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1915 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1916 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1917 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1921 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1922 struct mlxsw_sp_bridge_port *bridge_port,
1923 struct mlxsw_sp_port *mlxsw_sp_port,
1924 struct netlink_ext_ack *extack)
1926 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1928 if (is_vlan_dev(bridge_port->dev)) {
1929 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1933 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1934 if (WARN_ON(!mlxsw_sp_port_vlan))
1937 /* Let VLAN-aware bridge take care of its own VLANs */
1938 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1944 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1945 struct mlxsw_sp_bridge_port *bridge_port,
1946 struct mlxsw_sp_port *mlxsw_sp_port)
1948 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1949 /* Make sure untagged frames are allowed to ingress */
1950 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1953 static struct mlxsw_sp_fid *
1954 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1957 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1959 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1962 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1963 .port_join = mlxsw_sp_bridge_8021q_port_join,
1964 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
1965 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
1969 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1970 const struct net_device *br_dev)
1972 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1974 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1976 if (mlxsw_sp_port_vlan->bridge_port &&
1977 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1986 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1987 struct mlxsw_sp_bridge_port *bridge_port,
1988 struct mlxsw_sp_port *mlxsw_sp_port,
1989 struct netlink_ext_ack *extack)
1991 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1992 struct net_device *dev = bridge_port->dev;
1995 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1996 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1997 if (WARN_ON(!mlxsw_sp_port_vlan))
2000 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2001 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2005 /* Port is no longer usable as a router interface */
2006 if (mlxsw_sp_port_vlan->fid)
2007 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2009 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
2013 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2014 struct mlxsw_sp_bridge_port *bridge_port,
2015 struct mlxsw_sp_port *mlxsw_sp_port)
2017 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2018 struct net_device *dev = bridge_port->dev;
2021 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
2022 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2023 if (WARN_ON(!mlxsw_sp_port_vlan))
2026 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2029 static struct mlxsw_sp_fid *
2030 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2033 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2035 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2038 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2039 .port_join = mlxsw_sp_bridge_8021d_port_join,
2040 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2041 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2044 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2045 struct net_device *brport_dev,
2046 struct net_device *br_dev,
2047 struct netlink_ext_ack *extack)
2049 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2050 struct mlxsw_sp_bridge_device *bridge_device;
2051 struct mlxsw_sp_bridge_port *bridge_port;
2054 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2055 if (IS_ERR(bridge_port))
2056 return PTR_ERR(bridge_port);
2057 bridge_device = bridge_port->bridge_device;
2059 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2060 mlxsw_sp_port, extack);
2067 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2071 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2072 struct net_device *brport_dev,
2073 struct net_device *br_dev)
2075 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2076 struct mlxsw_sp_bridge_device *bridge_device;
2077 struct mlxsw_sp_bridge_port *bridge_port;
2079 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2082 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2086 bridge_device->ops->port_leave(bridge_device, bridge_port,
2088 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2092 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2093 const char *mac, u16 vid,
2094 struct net_device *dev)
2096 struct switchdev_notifier_fdb_info info;
2100 call_switchdev_notifiers(type, dev, &info.info);
2103 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2104 char *sfn_pl, int rec_index,
2107 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2108 struct mlxsw_sp_bridge_device *bridge_device;
2109 struct mlxsw_sp_bridge_port *bridge_port;
2110 struct mlxsw_sp_port *mlxsw_sp_port;
2111 enum switchdev_notifier_type type;
2115 bool do_notification = true;
2118 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2119 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2120 if (!mlxsw_sp_port) {
2121 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2125 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2126 if (!mlxsw_sp_port_vlan) {
2127 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2131 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2133 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2137 bridge_device = bridge_port->bridge_device;
2138 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2141 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2144 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2148 if (!do_notification)
2150 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2151 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2157 do_notification = false;
2161 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2162 char *sfn_pl, int rec_index,
2165 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2166 struct mlxsw_sp_bridge_device *bridge_device;
2167 struct mlxsw_sp_bridge_port *bridge_port;
2168 struct mlxsw_sp_port *mlxsw_sp_port;
2169 enum switchdev_notifier_type type;
2174 bool do_notification = true;
2177 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2178 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2179 if (!mlxsw_sp_port) {
2180 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2184 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2185 if (!mlxsw_sp_port_vlan) {
2186 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2190 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2192 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2196 bridge_device = bridge_port->bridge_device;
2197 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2198 lag_vid = mlxsw_sp_port_vlan->vid;
2201 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2204 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2208 if (!do_notification)
2210 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2211 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2217 do_notification = false;
2221 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2222 char *sfn_pl, int rec_index)
2224 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2225 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2226 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2229 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2230 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2233 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2234 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2237 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2238 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2244 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2246 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2248 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2249 msecs_to_jiffies(bridge->fdb_notify.interval));
2252 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2254 struct mlxsw_sp_bridge *bridge;
2255 struct mlxsw_sp *mlxsw_sp;
2261 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2265 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2266 mlxsw_sp = bridge->mlxsw_sp;
2269 mlxsw_reg_sfn_pack(sfn_pl);
2270 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2272 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2275 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2276 for (i = 0; i < num_rec; i++)
2277 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2282 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2285 struct mlxsw_sp_switchdev_event_work {
2286 struct work_struct work;
2287 struct switchdev_notifier_fdb_info fdb_info;
2288 struct net_device *dev;
2289 unsigned long event;
2292 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2294 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2295 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2296 struct net_device *dev = switchdev_work->dev;
2297 struct switchdev_notifier_fdb_info *fdb_info;
2298 struct mlxsw_sp_port *mlxsw_sp_port;
2302 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2306 switch (switchdev_work->event) {
2307 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2308 fdb_info = &switchdev_work->fdb_info;
2309 if (!fdb_info->added_by_user)
2311 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2314 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2316 fdb_info->vid, dev);
2318 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2319 fdb_info = &switchdev_work->fdb_info;
2320 if (!fdb_info->added_by_user)
2322 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2324 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2325 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2326 /* These events are only used to potentially update an existing
2332 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2336 kfree(switchdev_work->fdb_info.addr);
2337 kfree(switchdev_work);
2341 /* Called under rcu_read_lock() */
2342 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2343 unsigned long event, void *ptr)
2345 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2346 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2347 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2349 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2352 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2353 if (!switchdev_work)
2356 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2357 switchdev_work->dev = dev;
2358 switchdev_work->event = event;
2361 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2362 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2363 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2364 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2365 memcpy(&switchdev_work->fdb_info, ptr,
2366 sizeof(switchdev_work->fdb_info));
2367 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2368 if (!switchdev_work->fdb_info.addr)
2369 goto err_addr_alloc;
2370 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2372 /* Take a reference on the device. This can be either
2373 * upper device containig mlxsw_sp_port or just a
2379 kfree(switchdev_work);
2383 mlxsw_core_schedule_work(&switchdev_work->work);
2388 kfree(switchdev_work);
2392 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2393 .notifier_call = mlxsw_sp_switchdev_event,
2397 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2399 return bridge_port->stp_state;
2402 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2404 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2407 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2409 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2413 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2415 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2419 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2420 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2421 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2425 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2427 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2428 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2432 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2434 struct mlxsw_sp_bridge *bridge;
2436 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2439 mlxsw_sp->bridge = bridge;
2440 bridge->mlxsw_sp = mlxsw_sp;
2442 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2444 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2445 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2447 return mlxsw_sp_fdb_init(mlxsw_sp);
2450 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2452 mlxsw_sp_fdb_fini(mlxsw_sp);
2453 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2454 kfree(mlxsw_sp->bridge);
2457 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2459 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2462 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)