2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/slab.h>
42 #include <linux/device.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_vlan.h>
45 #include <linux/if_bridge.h>
46 #include <linux/workqueue.h>
47 #include <linux/jiffies.h>
48 #include <linux/rtnetlink.h>
49 #include <linux/netlink.h>
50 #include <net/switchdev.h>
52 #include "spectrum_span.h"
53 #include "spectrum_router.h"
54 #include "spectrum_switchdev.h"
59 struct mlxsw_sp_bridge_ops;
61 struct mlxsw_sp_bridge {
62 struct mlxsw_sp *mlxsw_sp;
64 struct delayed_work dw;
65 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
66 unsigned int interval; /* ms */
68 #define MLXSW_SP_MIN_AGEING_TIME 10
69 #define MLXSW_SP_MAX_AGEING_TIME 1000000
70 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
72 bool vlan_enabled_exists;
73 struct list_head bridges_list;
74 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
75 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
76 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
79 struct mlxsw_sp_bridge_device {
80 struct net_device *dev;
81 struct list_head list;
82 struct list_head ports_list;
83 struct list_head mids_list;
87 const struct mlxsw_sp_bridge_ops *ops;
90 struct mlxsw_sp_bridge_port {
91 struct net_device *dev;
92 struct mlxsw_sp_bridge_device *bridge_device;
93 struct list_head list;
94 struct list_head vlans_list;
95 unsigned int ref_count;
106 struct mlxsw_sp_bridge_vlan {
107 struct list_head list;
108 struct list_head port_vlan_list;
112 struct mlxsw_sp_bridge_ops {
113 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
114 struct mlxsw_sp_bridge_port *bridge_port,
115 struct mlxsw_sp_port *mlxsw_sp_port,
116 struct netlink_ext_ack *extack);
117 void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
118 struct mlxsw_sp_bridge_port *bridge_port,
119 struct mlxsw_sp_port *mlxsw_sp_port);
120 struct mlxsw_sp_fid *
121 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
126 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
127 struct mlxsw_sp_bridge_port *bridge_port,
131 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
132 struct mlxsw_sp_bridge_port *bridge_port);
135 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
136 struct mlxsw_sp_bridge_device
140 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
141 struct mlxsw_sp_bridge_port *bridge_port,
144 static struct mlxsw_sp_bridge_device *
145 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
146 const struct net_device *br_dev)
148 struct mlxsw_sp_bridge_device *bridge_device;
150 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
151 if (bridge_device->dev == br_dev)
152 return bridge_device;
157 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
158 const struct net_device *br_dev)
160 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
163 static struct mlxsw_sp_bridge_device *
164 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
165 struct net_device *br_dev)
167 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
168 struct mlxsw_sp_bridge_device *bridge_device;
169 bool vlan_enabled = br_vlan_enabled(br_dev);
171 if (vlan_enabled && bridge->vlan_enabled_exists) {
172 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
173 return ERR_PTR(-EINVAL);
176 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
178 return ERR_PTR(-ENOMEM);
180 bridge_device->dev = br_dev;
181 bridge_device->vlan_enabled = vlan_enabled;
182 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
183 bridge_device->mrouter = br_multicast_router(br_dev);
184 INIT_LIST_HEAD(&bridge_device->ports_list);
186 bridge->vlan_enabled_exists = true;
187 bridge_device->ops = bridge->bridge_8021q_ops;
189 bridge_device->ops = bridge->bridge_8021d_ops;
191 INIT_LIST_HEAD(&bridge_device->mids_list);
192 list_add(&bridge_device->list, &bridge->bridges_list);
194 return bridge_device;
198 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
199 struct mlxsw_sp_bridge_device *bridge_device)
201 list_del(&bridge_device->list);
202 if (bridge_device->vlan_enabled)
203 bridge->vlan_enabled_exists = false;
204 WARN_ON(!list_empty(&bridge_device->ports_list));
205 WARN_ON(!list_empty(&bridge_device->mids_list));
206 kfree(bridge_device);
209 static struct mlxsw_sp_bridge_device *
210 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
211 struct net_device *br_dev)
213 struct mlxsw_sp_bridge_device *bridge_device;
215 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
217 return bridge_device;
219 return mlxsw_sp_bridge_device_create(bridge, br_dev);
223 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
224 struct mlxsw_sp_bridge_device *bridge_device)
226 if (list_empty(&bridge_device->ports_list))
227 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
230 static struct mlxsw_sp_bridge_port *
231 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
232 const struct net_device *brport_dev)
234 struct mlxsw_sp_bridge_port *bridge_port;
236 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
237 if (bridge_port->dev == brport_dev)
244 struct mlxsw_sp_bridge_port *
245 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
246 struct net_device *brport_dev)
248 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
249 struct mlxsw_sp_bridge_device *bridge_device;
254 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
258 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
261 static struct mlxsw_sp_bridge_port *
262 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
263 struct net_device *brport_dev)
265 struct mlxsw_sp_bridge_port *bridge_port;
266 struct mlxsw_sp_port *mlxsw_sp_port;
268 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
272 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
273 bridge_port->lagged = mlxsw_sp_port->lagged;
274 if (bridge_port->lagged)
275 bridge_port->lag_id = mlxsw_sp_port->lag_id;
277 bridge_port->system_port = mlxsw_sp_port->local_port;
278 bridge_port->dev = brport_dev;
279 bridge_port->bridge_device = bridge_device;
280 bridge_port->stp_state = BR_STATE_DISABLED;
281 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
283 INIT_LIST_HEAD(&bridge_port->vlans_list);
284 list_add(&bridge_port->list, &bridge_device->ports_list);
285 bridge_port->ref_count = 1;
291 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
293 list_del(&bridge_port->list);
294 WARN_ON(!list_empty(&bridge_port->vlans_list));
299 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
302 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
304 /* In case ports were pulled from out of a bridged LAG, then
305 * it's possible the reference count isn't zero, yet the bridge
306 * port should be destroyed, as it's no longer an upper of ours.
308 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
310 else if (bridge_port->ref_count == 0)
316 static struct mlxsw_sp_bridge_port *
317 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
318 struct net_device *brport_dev)
320 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
321 struct mlxsw_sp_bridge_device *bridge_device;
322 struct mlxsw_sp_bridge_port *bridge_port;
325 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
327 bridge_port->ref_count++;
331 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
332 if (IS_ERR(bridge_device))
333 return ERR_CAST(bridge_device);
335 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
338 goto err_bridge_port_create;
343 err_bridge_port_create:
344 mlxsw_sp_bridge_device_put(bridge, bridge_device);
348 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
349 struct mlxsw_sp_bridge_port *bridge_port)
351 struct mlxsw_sp_bridge_device *bridge_device;
353 bridge_port->ref_count--;
354 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
356 bridge_device = bridge_port->bridge_device;
357 mlxsw_sp_bridge_port_destroy(bridge_port);
358 mlxsw_sp_bridge_device_put(bridge, bridge_device);
361 static struct mlxsw_sp_port_vlan *
362 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
363 const struct mlxsw_sp_bridge_device *
367 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
369 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
371 if (!mlxsw_sp_port_vlan->bridge_port)
373 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
376 if (bridge_device->vlan_enabled &&
377 mlxsw_sp_port_vlan->vid != vid)
379 return mlxsw_sp_port_vlan;
385 static struct mlxsw_sp_port_vlan*
386 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
389 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
391 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
393 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
395 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
396 return mlxsw_sp_port_vlan;
402 static struct mlxsw_sp_bridge_vlan *
403 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
406 struct mlxsw_sp_bridge_vlan *bridge_vlan;
408 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
409 if (bridge_vlan->vid == vid)
416 static struct mlxsw_sp_bridge_vlan *
417 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
419 struct mlxsw_sp_bridge_vlan *bridge_vlan;
421 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
425 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
426 bridge_vlan->vid = vid;
427 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
433 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
435 list_del(&bridge_vlan->list);
436 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
440 static struct mlxsw_sp_bridge_vlan *
441 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
443 struct mlxsw_sp_bridge_vlan *bridge_vlan;
445 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
449 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
452 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
454 if (list_empty(&bridge_vlan->port_vlan_list))
455 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
458 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
459 struct net_device *dev,
460 unsigned long *brport_flags)
462 struct mlxsw_sp_bridge_port *bridge_port;
464 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
465 if (WARN_ON(!bridge_port))
468 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
471 static int mlxsw_sp_port_attr_get(struct net_device *dev,
472 struct switchdev_attr *attr)
474 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
475 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
478 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
479 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
480 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
481 attr->u.ppid.id_len);
483 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
484 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
485 &attr->u.brport_flags);
487 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
488 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
499 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
500 struct mlxsw_sp_bridge_vlan *bridge_vlan,
503 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
505 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
507 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
509 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
510 bridge_vlan->vid, state);
516 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
517 struct switchdev_trans *trans,
518 struct net_device *orig_dev,
521 struct mlxsw_sp_bridge_port *bridge_port;
522 struct mlxsw_sp_bridge_vlan *bridge_vlan;
525 if (switchdev_trans_ph_prepare(trans))
528 /* It's possible we failed to enslave the port, yet this
529 * operation is executed due to it being deferred.
531 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
536 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
537 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
540 goto err_port_bridge_vlan_stp_set;
543 bridge_port->stp_state = state;
547 err_port_bridge_vlan_stp_set:
548 list_for_each_entry_continue_reverse(bridge_vlan,
549 &bridge_port->vlans_list, list)
550 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
551 bridge_port->stp_state);
556 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
557 struct mlxsw_sp_bridge_vlan *bridge_vlan,
558 enum mlxsw_sp_flood_type packet_type,
561 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
563 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
565 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
567 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
569 mlxsw_sp_port->local_port,
577 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
578 struct mlxsw_sp_bridge_port *bridge_port,
579 enum mlxsw_sp_flood_type packet_type,
582 struct mlxsw_sp_bridge_vlan *bridge_vlan;
585 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
586 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
591 goto err_port_bridge_vlan_flood_set;
596 err_port_bridge_vlan_flood_set:
597 list_for_each_entry_continue_reverse(bridge_vlan,
598 &bridge_port->vlans_list, list)
599 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
600 packet_type, !member);
605 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
606 struct mlxsw_sp_bridge_vlan *bridge_vlan,
609 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
610 u16 vid = bridge_vlan->vid;
612 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
614 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
616 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
623 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
624 struct mlxsw_sp_bridge_port *bridge_port,
627 struct mlxsw_sp_bridge_vlan *bridge_vlan;
630 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
631 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
634 goto err_port_bridge_vlan_learning_set;
639 err_port_bridge_vlan_learning_set:
640 list_for_each_entry_continue_reverse(bridge_vlan,
641 &bridge_port->vlans_list, list)
642 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
647 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
648 struct switchdev_trans *trans,
649 struct net_device *orig_dev,
650 unsigned long brport_flags)
652 struct mlxsw_sp_bridge_port *bridge_port;
655 if (switchdev_trans_ph_prepare(trans))
658 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
663 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
664 MLXSW_SP_FLOOD_TYPE_UC,
665 brport_flags & BR_FLOOD);
669 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
670 brport_flags & BR_LEARNING);
674 if (bridge_port->bridge_device->multicast_enabled)
677 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
678 MLXSW_SP_FLOOD_TYPE_MC,
685 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
689 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
691 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
694 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
695 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
698 mlxsw_sp->bridge->ageing_time = ageing_time;
702 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
703 struct switchdev_trans *trans,
704 unsigned long ageing_clock_t)
706 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
707 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
708 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
710 if (switchdev_trans_ph_prepare(trans)) {
711 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
712 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
718 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
721 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
722 struct switchdev_trans *trans,
723 struct net_device *orig_dev,
726 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
727 struct mlxsw_sp_bridge_device *bridge_device;
729 if (!switchdev_trans_ph_prepare(trans))
732 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
733 if (WARN_ON(!bridge_device))
736 if (bridge_device->vlan_enabled == vlan_enabled)
739 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
743 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
744 struct switchdev_trans *trans,
745 struct net_device *orig_dev,
746 bool is_port_mrouter)
748 struct mlxsw_sp_bridge_port *bridge_port;
751 if (switchdev_trans_ph_prepare(trans))
754 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
759 if (!bridge_port->bridge_device->multicast_enabled)
762 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
763 MLXSW_SP_FLOOD_TYPE_MC,
768 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
771 bridge_port->mrouter = is_port_mrouter;
775 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
777 const struct mlxsw_sp_bridge_device *bridge_device;
779 bridge_device = bridge_port->bridge_device;
780 return bridge_device->multicast_enabled ? bridge_port->mrouter :
781 bridge_port->flags & BR_MCAST_FLOOD;
784 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
785 struct switchdev_trans *trans,
786 struct net_device *orig_dev,
789 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
790 struct mlxsw_sp_bridge_device *bridge_device;
791 struct mlxsw_sp_bridge_port *bridge_port;
794 if (switchdev_trans_ph_prepare(trans))
797 /* It's possible we failed to enslave the port, yet this
798 * operation is executed due to it being deferred.
800 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
804 if (bridge_device->multicast_enabled != !mc_disabled) {
805 bridge_device->multicast_enabled = !mc_disabled;
806 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
810 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
811 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
812 bool member = mlxsw_sp_mc_flood(bridge_port);
814 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
816 packet_type, member);
821 bridge_device->multicast_enabled = !mc_disabled;
826 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
827 u16 mid_idx, bool add)
832 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
836 mlxsw_reg_smid_pack(smid_pl, mid_idx,
837 mlxsw_sp_router_port(mlxsw_sp), add);
838 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
844 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
845 struct mlxsw_sp_bridge_device *bridge_device,
848 struct mlxsw_sp_mid *mid;
850 list_for_each_entry(mid, &bridge_device->mids_list, list)
851 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
855 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
856 struct switchdev_trans *trans,
857 struct net_device *orig_dev,
860 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
861 struct mlxsw_sp_bridge_device *bridge_device;
863 if (switchdev_trans_ph_prepare(trans))
866 /* It's possible we failed to enslave the port, yet this
867 * operation is executed due to it being deferred.
869 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
873 if (bridge_device->mrouter != is_mrouter)
874 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
876 bridge_device->mrouter = is_mrouter;
880 static int mlxsw_sp_port_attr_set(struct net_device *dev,
881 const struct switchdev_attr *attr,
882 struct switchdev_trans *trans)
884 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
888 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
889 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
893 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
894 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
896 attr->u.brport_flags);
898 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
899 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
900 attr->u.ageing_time);
902 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
903 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
905 attr->u.vlan_filtering);
907 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
908 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
912 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
913 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
915 attr->u.mc_disabled);
917 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
918 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
927 if (switchdev_trans_ph_commit(trans))
928 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
934 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
935 struct mlxsw_sp_bridge_port *bridge_port)
937 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
938 struct mlxsw_sp_bridge_device *bridge_device;
939 u8 local_port = mlxsw_sp_port->local_port;
940 u16 vid = mlxsw_sp_port_vlan->vid;
941 struct mlxsw_sp_fid *fid;
944 bridge_device = bridge_port->bridge_device;
945 fid = bridge_device->ops->fid_get(bridge_device, vid);
949 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
950 bridge_port->flags & BR_FLOOD);
952 goto err_fid_uc_flood_set;
954 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
955 mlxsw_sp_mc_flood(bridge_port));
957 goto err_fid_mc_flood_set;
959 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
962 goto err_fid_bc_flood_set;
964 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
966 goto err_fid_port_vid_map;
968 mlxsw_sp_port_vlan->fid = fid;
972 err_fid_port_vid_map:
973 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
974 err_fid_bc_flood_set:
975 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
976 err_fid_mc_flood_set:
977 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
978 err_fid_uc_flood_set:
979 mlxsw_sp_fid_put(fid);
984 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
986 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
987 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
988 u8 local_port = mlxsw_sp_port->local_port;
989 u16 vid = mlxsw_sp_port_vlan->vid;
991 mlxsw_sp_port_vlan->fid = NULL;
992 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
993 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
994 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
995 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
996 mlxsw_sp_fid_put(fid);
1000 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1001 u16 vid, bool is_pvid)
1005 else if (mlxsw_sp_port->pvid == vid)
1006 return 0; /* Dis-allow untagged packets */
1008 return mlxsw_sp_port->pvid;
1012 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1013 struct mlxsw_sp_bridge_port *bridge_port)
1015 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1016 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1017 u16 vid = mlxsw_sp_port_vlan->vid;
1020 /* No need to continue if only VLAN flags were changed */
1021 if (mlxsw_sp_port_vlan->bridge_port)
1024 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1028 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1029 bridge_port->flags & BR_LEARNING);
1031 goto err_port_vid_learning_set;
1033 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1034 bridge_port->stp_state);
1036 goto err_port_vid_stp_set;
1038 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1041 goto err_bridge_vlan_get;
1044 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1045 &bridge_vlan->port_vlan_list);
1047 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1049 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1053 err_bridge_vlan_get:
1054 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1055 err_port_vid_stp_set:
1056 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1057 err_port_vid_learning_set:
1058 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1063 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1065 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1066 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1067 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1068 struct mlxsw_sp_bridge_port *bridge_port;
1069 u16 vid = mlxsw_sp_port_vlan->vid;
1070 bool last_port, last_vlan;
1072 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1073 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1076 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1077 last_vlan = list_is_singular(&bridge_port->vlans_list);
1078 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1079 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1081 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1082 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1083 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1084 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1086 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1088 mlxsw_sp_fid_index(fid));
1090 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1092 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1094 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1095 mlxsw_sp_port_vlan->bridge_port = NULL;
1099 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1100 struct mlxsw_sp_bridge_port *bridge_port,
1101 u16 vid, bool is_untagged, bool is_pvid)
1103 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1104 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1105 u16 old_pvid = mlxsw_sp_port->pvid;
1108 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1109 if (IS_ERR(mlxsw_sp_port_vlan))
1110 return PTR_ERR(mlxsw_sp_port_vlan);
1112 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1115 goto err_port_vlan_set;
1117 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1119 goto err_port_pvid_set;
1121 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1123 goto err_port_vlan_bridge_join;
1127 err_port_vlan_bridge_join:
1128 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1130 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1132 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1136 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1137 const struct switchdev_obj_port_vlan *vlan,
1138 struct switchdev_trans *trans)
1140 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1141 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1142 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1143 struct net_device *orig_dev = vlan->obj.orig_dev;
1144 struct mlxsw_sp_bridge_port *bridge_port;
1147 if (switchdev_trans_ph_prepare(trans))
1150 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1151 if (WARN_ON(!bridge_port))
1154 if (!bridge_port->bridge_device->vlan_enabled)
1157 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1160 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1170 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1172 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1173 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1177 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1178 struct mlxsw_sp_bridge_port *bridge_port,
1181 bool lagged = bridge_port->lagged;
1182 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1185 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1186 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1187 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1188 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1190 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1193 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1195 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1196 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1199 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1201 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1202 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1205 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1206 const char *mac, u16 fid, bool adding,
1207 enum mlxsw_reg_sfd_rec_action action,
1214 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1218 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1219 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1220 mac, fid, action, local_port);
1221 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1222 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1226 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1234 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1235 const char *mac, u16 fid, bool adding,
1238 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1239 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1242 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1245 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1246 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1250 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1251 const char *mac, u16 fid, u16 lag_vid,
1252 bool adding, bool dynamic)
1258 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1262 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1263 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1264 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1266 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1267 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1271 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1280 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1281 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1283 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1284 struct net_device *orig_dev = fdb_info->info.dev;
1285 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1286 struct mlxsw_sp_bridge_device *bridge_device;
1287 struct mlxsw_sp_bridge_port *bridge_port;
1290 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1294 bridge_device = bridge_port->bridge_device;
1295 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1298 if (!mlxsw_sp_port_vlan)
1301 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1302 vid = mlxsw_sp_port_vlan->vid;
1304 if (!bridge_port->lagged)
1305 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1306 bridge_port->system_port,
1307 fdb_info->addr, fid_index,
1310 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1311 bridge_port->lag_id,
1312 fdb_info->addr, fid_index,
1313 vid, adding, false);
1316 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1317 u16 fid, u16 mid_idx, bool adding)
1323 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1327 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1328 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1329 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1330 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1331 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1335 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1343 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1345 bool set_router_port)
1350 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1354 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1355 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1356 if (mlxsw_sp->ports[i])
1357 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1360 mlxsw_reg_smid_port_mask_set(smid_pl,
1361 mlxsw_sp_router_port(mlxsw_sp), 1);
1363 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1364 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1366 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1369 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1374 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1375 u16 mid_idx, bool add)
1377 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1381 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1385 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1386 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1392 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1393 const unsigned char *addr,
1396 struct mlxsw_sp_mid *mid;
1398 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1399 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1406 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1407 struct mlxsw_sp_bridge_port *bridge_port,
1408 unsigned long *ports_bitmap)
1410 struct mlxsw_sp_port *mlxsw_sp_port;
1411 u64 max_lag_members, i;
1414 if (!bridge_port->lagged) {
1415 set_bit(bridge_port->system_port, ports_bitmap);
1417 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1419 lag_id = bridge_port->lag_id;
1420 for (i = 0; i < max_lag_members; i++) {
1421 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1424 set_bit(mlxsw_sp_port->local_port,
1431 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1432 struct mlxsw_sp_bridge_device *bridge_device,
1433 struct mlxsw_sp *mlxsw_sp)
1435 struct mlxsw_sp_bridge_port *bridge_port;
1437 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1438 if (bridge_port->mrouter) {
1439 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1447 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1448 struct mlxsw_sp_mid *mid,
1449 struct mlxsw_sp_bridge_device *bridge_device)
1457 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1459 if (mid_idx == MLXSW_SP_MID_MAX)
1462 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1463 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1464 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1468 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1469 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1472 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1473 bridge_device->mrouter);
1474 kfree(flood_bitmap);
1478 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1483 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1488 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1489 struct mlxsw_sp_mid *mid)
1494 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1496 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1501 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1502 struct mlxsw_sp_bridge_device *bridge_device,
1503 const unsigned char *addr,
1506 struct mlxsw_sp_mid *mid;
1509 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1513 alloc_size = sizeof(unsigned long) *
1514 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1516 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1517 if (!mid->ports_in_mid)
1518 goto err_ports_in_mid_alloc;
1520 ether_addr_copy(mid->addr, addr);
1524 if (!bridge_device->multicast_enabled)
1527 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1528 goto err_write_mdb_entry;
1531 list_add_tail(&mid->list, &bridge_device->mids_list);
1534 err_write_mdb_entry:
1535 kfree(mid->ports_in_mid);
1536 err_ports_in_mid_alloc:
1541 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1542 struct mlxsw_sp_mid *mid)
1544 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1547 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1548 if (bitmap_empty(mid->ports_in_mid,
1549 mlxsw_core_max_ports(mlxsw_sp->core))) {
1550 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1551 list_del(&mid->list);
1552 kfree(mid->ports_in_mid);
1558 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1559 const struct switchdev_obj_port_mdb *mdb,
1560 struct switchdev_trans *trans)
1562 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1563 struct net_device *orig_dev = mdb->obj.orig_dev;
1564 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1565 struct net_device *dev = mlxsw_sp_port->dev;
1566 struct mlxsw_sp_bridge_device *bridge_device;
1567 struct mlxsw_sp_bridge_port *bridge_port;
1568 struct mlxsw_sp_mid *mid;
1572 if (switchdev_trans_ph_prepare(trans))
1575 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1579 bridge_device = bridge_port->bridge_device;
1580 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1583 if (!mlxsw_sp_port_vlan)
1586 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1588 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1590 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1593 netdev_err(dev, "Unable to allocate MC group\n");
1597 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1599 if (!bridge_device->multicast_enabled)
1602 if (bridge_port->mrouter)
1605 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1607 netdev_err(dev, "Unable to set SMID\n");
1614 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1619 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1620 struct mlxsw_sp_bridge_device
1623 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1624 struct mlxsw_sp_mid *mid;
1627 mc_enabled = bridge_device->multicast_enabled;
1629 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1631 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1634 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1639 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1640 struct mlxsw_sp_bridge_port *bridge_port,
1643 struct mlxsw_sp_bridge_device *bridge_device;
1644 struct mlxsw_sp_mid *mid;
1646 bridge_device = bridge_port->bridge_device;
1648 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1649 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1650 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1654 struct mlxsw_sp_span_respin_work {
1655 struct work_struct work;
1656 struct mlxsw_sp *mlxsw_sp;
1659 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1661 struct mlxsw_sp_span_respin_work *respin_work =
1662 container_of(work, struct mlxsw_sp_span_respin_work, work);
1665 mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1670 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1672 struct mlxsw_sp_span_respin_work *respin_work;
1674 respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1678 INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1679 respin_work->mlxsw_sp = mlxsw_sp;
1681 mlxsw_core_schedule_work(&respin_work->work);
1684 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1685 const struct switchdev_obj *obj,
1686 struct switchdev_trans *trans)
1688 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1689 const struct switchdev_obj_port_vlan *vlan;
1693 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1694 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1695 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1697 if (switchdev_trans_ph_commit(trans)) {
1698 /* The event is emitted before the changes are actually
1699 * applied to the bridge. Therefore schedule the respin
1700 * call for later, so that the respin logic sees the
1701 * updated bridge state.
1703 mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1706 case SWITCHDEV_OBJ_ID_PORT_MDB:
1707 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1708 SWITCHDEV_OBJ_PORT_MDB(obj),
1720 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1721 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1723 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1724 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1726 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1727 if (WARN_ON(!mlxsw_sp_port_vlan))
1730 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1731 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1732 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1733 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1736 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1737 const struct switchdev_obj_port_vlan *vlan)
1739 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1740 struct net_device *orig_dev = vlan->obj.orig_dev;
1741 struct mlxsw_sp_bridge_port *bridge_port;
1744 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1745 if (WARN_ON(!bridge_port))
1748 if (!bridge_port->bridge_device->vlan_enabled)
1751 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1752 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1758 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1759 struct mlxsw_sp_bridge_port *bridge_port,
1760 struct mlxsw_sp_mid *mid)
1762 struct net_device *dev = mlxsw_sp_port->dev;
1765 if (bridge_port->bridge_device->multicast_enabled) {
1766 if (bridge_port->bridge_device->multicast_enabled) {
1767 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid,
1770 netdev_err(dev, "Unable to remove port from SMID\n");
1774 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1776 netdev_err(dev, "Unable to remove MC SFD\n");
1781 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1782 const struct switchdev_obj_port_mdb *mdb)
1784 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1785 struct net_device *orig_dev = mdb->obj.orig_dev;
1786 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1787 struct mlxsw_sp_bridge_device *bridge_device;
1788 struct net_device *dev = mlxsw_sp_port->dev;
1789 struct mlxsw_sp_bridge_port *bridge_port;
1790 struct mlxsw_sp_mid *mid;
1793 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1797 bridge_device = bridge_port->bridge_device;
1798 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1801 if (!mlxsw_sp_port_vlan)
1804 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1806 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1808 netdev_err(dev, "Unable to remove port from MC DB\n");
1812 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1816 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1817 struct mlxsw_sp_bridge_port *bridge_port)
1819 struct mlxsw_sp_bridge_device *bridge_device;
1820 struct mlxsw_sp_mid *mid, *tmp;
1822 bridge_device = bridge_port->bridge_device;
1824 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1825 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1826 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1828 } else if (bridge_device->multicast_enabled &&
1829 bridge_port->mrouter) {
1830 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1835 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1836 const struct switchdev_obj *obj)
1838 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1842 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1843 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1844 SWITCHDEV_OBJ_PORT_VLAN(obj));
1846 case SWITCHDEV_OBJ_ID_PORT_MDB:
1847 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1848 SWITCHDEV_OBJ_PORT_MDB(obj));
1855 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1860 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1863 struct mlxsw_sp_port *mlxsw_sp_port;
1864 u64 max_lag_members;
1867 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1869 for (i = 0; i < max_lag_members; i++) {
1870 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1872 return mlxsw_sp_port;
1877 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1878 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1879 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1880 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1881 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1885 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1886 struct mlxsw_sp_bridge_port *bridge_port,
1887 struct mlxsw_sp_port *mlxsw_sp_port,
1888 struct netlink_ext_ack *extack)
1890 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1892 if (is_vlan_dev(bridge_port->dev)) {
1893 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1897 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1898 if (WARN_ON(!mlxsw_sp_port_vlan))
1901 /* Let VLAN-aware bridge take care of its own VLANs */
1902 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1908 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1909 struct mlxsw_sp_bridge_port *bridge_port,
1910 struct mlxsw_sp_port *mlxsw_sp_port)
1912 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1913 /* Make sure untagged frames are allowed to ingress */
1914 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1917 static struct mlxsw_sp_fid *
1918 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1921 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1923 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1926 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1927 .port_join = mlxsw_sp_bridge_8021q_port_join,
1928 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
1929 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
1933 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1934 const struct net_device *br_dev)
1936 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1938 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1940 if (mlxsw_sp_port_vlan->bridge_port &&
1941 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1950 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1951 struct mlxsw_sp_bridge_port *bridge_port,
1952 struct mlxsw_sp_port *mlxsw_sp_port,
1953 struct netlink_ext_ack *extack)
1955 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1956 struct net_device *dev = bridge_port->dev;
1959 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1960 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1961 if (WARN_ON(!mlxsw_sp_port_vlan))
1964 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1965 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
1969 /* Port is no longer usable as a router interface */
1970 if (mlxsw_sp_port_vlan->fid)
1971 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1973 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1977 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1978 struct mlxsw_sp_bridge_port *bridge_port,
1979 struct mlxsw_sp_port *mlxsw_sp_port)
1981 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1982 struct net_device *dev = bridge_port->dev;
1985 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1986 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1987 if (WARN_ON(!mlxsw_sp_port_vlan))
1990 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1993 static struct mlxsw_sp_fid *
1994 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1997 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1999 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2002 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2003 .port_join = mlxsw_sp_bridge_8021d_port_join,
2004 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2005 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2008 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2009 struct net_device *brport_dev,
2010 struct net_device *br_dev,
2011 struct netlink_ext_ack *extack)
2013 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2014 struct mlxsw_sp_bridge_device *bridge_device;
2015 struct mlxsw_sp_bridge_port *bridge_port;
2018 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2019 if (IS_ERR(bridge_port))
2020 return PTR_ERR(bridge_port);
2021 bridge_device = bridge_port->bridge_device;
2023 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2024 mlxsw_sp_port, extack);
2031 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2035 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2036 struct net_device *brport_dev,
2037 struct net_device *br_dev)
2039 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2040 struct mlxsw_sp_bridge_device *bridge_device;
2041 struct mlxsw_sp_bridge_port *bridge_port;
2043 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2046 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2050 bridge_device->ops->port_leave(bridge_device, bridge_port,
2052 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2056 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2057 const char *mac, u16 vid,
2058 struct net_device *dev)
2060 struct switchdev_notifier_fdb_info info;
2064 call_switchdev_notifiers(type, dev, &info.info);
2067 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2068 char *sfn_pl, int rec_index,
2071 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2072 struct mlxsw_sp_bridge_device *bridge_device;
2073 struct mlxsw_sp_bridge_port *bridge_port;
2074 struct mlxsw_sp_port *mlxsw_sp_port;
2075 enum switchdev_notifier_type type;
2079 bool do_notification = true;
2082 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2083 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2084 if (!mlxsw_sp_port) {
2085 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2089 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2090 if (!mlxsw_sp_port_vlan) {
2091 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2095 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2097 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2101 bridge_device = bridge_port->bridge_device;
2102 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2105 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2108 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2112 if (!do_notification)
2114 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2115 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2121 do_notification = false;
2125 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2126 char *sfn_pl, int rec_index,
2129 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2130 struct mlxsw_sp_bridge_device *bridge_device;
2131 struct mlxsw_sp_bridge_port *bridge_port;
2132 struct mlxsw_sp_port *mlxsw_sp_port;
2133 enum switchdev_notifier_type type;
2138 bool do_notification = true;
2141 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2142 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2143 if (!mlxsw_sp_port) {
2144 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2148 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2149 if (!mlxsw_sp_port_vlan) {
2150 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2154 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2156 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2160 bridge_device = bridge_port->bridge_device;
2161 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2162 lag_vid = mlxsw_sp_port_vlan->vid;
2165 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2168 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2172 if (!do_notification)
2174 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2175 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2181 do_notification = false;
2185 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2186 char *sfn_pl, int rec_index)
2188 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2189 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2190 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2193 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2194 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2197 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2198 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2201 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2202 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2208 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2210 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2212 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2213 msecs_to_jiffies(bridge->fdb_notify.interval));
2216 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2218 struct mlxsw_sp_bridge *bridge;
2219 struct mlxsw_sp *mlxsw_sp;
2225 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2229 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2230 mlxsw_sp = bridge->mlxsw_sp;
2233 mlxsw_reg_sfn_pack(sfn_pl);
2234 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2236 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2239 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2240 for (i = 0; i < num_rec; i++)
2241 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2246 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2249 struct mlxsw_sp_switchdev_event_work {
2250 struct work_struct work;
2251 struct switchdev_notifier_fdb_info fdb_info;
2252 struct net_device *dev;
2253 unsigned long event;
2256 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2258 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2259 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2260 struct net_device *dev = switchdev_work->dev;
2261 struct switchdev_notifier_fdb_info *fdb_info;
2262 struct mlxsw_sp_port *mlxsw_sp_port;
2266 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2270 switch (switchdev_work->event) {
2271 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2272 fdb_info = &switchdev_work->fdb_info;
2273 if (!fdb_info->added_by_user)
2275 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2278 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2280 fdb_info->vid, dev);
2282 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2283 fdb_info = &switchdev_work->fdb_info;
2284 if (!fdb_info->added_by_user)
2286 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2288 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2289 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2290 /* These events are only used to potentially update an existing
2296 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2300 kfree(switchdev_work->fdb_info.addr);
2301 kfree(switchdev_work);
2305 /* Called under rcu_read_lock() */
2306 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2307 unsigned long event, void *ptr)
2309 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2310 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2311 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2313 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2316 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2317 if (!switchdev_work)
2320 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2321 switchdev_work->dev = dev;
2322 switchdev_work->event = event;
2325 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2326 case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2327 case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2328 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2329 memcpy(&switchdev_work->fdb_info, ptr,
2330 sizeof(switchdev_work->fdb_info));
2331 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2332 if (!switchdev_work->fdb_info.addr)
2333 goto err_addr_alloc;
2334 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2336 /* Take a reference on the device. This can be either
2337 * upper device containig mlxsw_sp_port or just a
2343 kfree(switchdev_work);
2347 mlxsw_core_schedule_work(&switchdev_work->work);
2352 kfree(switchdev_work);
2356 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2357 .notifier_call = mlxsw_sp_switchdev_event,
2361 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2363 return bridge_port->stp_state;
2366 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2368 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2371 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2373 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2377 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2379 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2383 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2384 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2385 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2389 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2391 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2392 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2396 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2398 struct mlxsw_sp_bridge *bridge;
2400 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2403 mlxsw_sp->bridge = bridge;
2404 bridge->mlxsw_sp = mlxsw_sp;
2406 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2408 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2409 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2411 return mlxsw_sp_fdb_init(mlxsw_sp);
2414 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2416 mlxsw_sp_fdb_fini(mlxsw_sp);
2417 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2418 kfree(mlxsw_sp->bridge);
2421 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2423 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2426 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)