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 <net/switchdev.h>
55 struct mlxsw_sp_bridge_ops;
57 struct mlxsw_sp_bridge {
58 struct mlxsw_sp *mlxsw_sp;
60 struct delayed_work dw;
61 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
62 unsigned int interval; /* ms */
64 #define MLXSW_SP_MIN_AGEING_TIME 10
65 #define MLXSW_SP_MAX_AGEING_TIME 1000000
66 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
68 bool vlan_enabled_exists;
69 struct list_head bridges_list;
70 struct list_head mids_list;
71 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
72 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
73 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
76 struct mlxsw_sp_bridge_device {
77 struct net_device *dev;
78 struct list_head list;
79 struct list_head ports_list;
82 const struct mlxsw_sp_bridge_ops *ops;
85 struct mlxsw_sp_bridge_port {
86 struct net_device *dev;
87 struct mlxsw_sp_bridge_device *bridge_device;
88 struct list_head list;
89 struct list_head vlans_list;
90 unsigned int ref_count;
101 struct mlxsw_sp_bridge_vlan {
102 struct list_head list;
103 struct list_head port_vlan_list;
105 u8 egress_untagged:1,
109 struct mlxsw_sp_bridge_ops {
110 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
111 struct mlxsw_sp_bridge_port *bridge_port,
112 struct mlxsw_sp_port *mlxsw_sp_port);
113 void (*port_leave)(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 mlxsw_sp_fid *
117 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
122 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
123 struct mlxsw_sp_bridge_port *bridge_port,
126 static struct mlxsw_sp_bridge_device *
127 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
128 const struct net_device *br_dev)
130 struct mlxsw_sp_bridge_device *bridge_device;
132 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
133 if (bridge_device->dev == br_dev)
134 return bridge_device;
139 static struct mlxsw_sp_bridge_device *
140 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
141 struct net_device *br_dev)
143 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
144 struct mlxsw_sp_bridge_device *bridge_device;
145 bool vlan_enabled = br_vlan_enabled(br_dev);
147 if (vlan_enabled && bridge->vlan_enabled_exists) {
148 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
149 return ERR_PTR(-EINVAL);
152 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
154 return ERR_PTR(-ENOMEM);
156 bridge_device->dev = br_dev;
157 bridge_device->vlan_enabled = vlan_enabled;
158 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
159 INIT_LIST_HEAD(&bridge_device->ports_list);
161 bridge->vlan_enabled_exists = true;
162 bridge_device->ops = bridge->bridge_8021q_ops;
164 bridge_device->ops = bridge->bridge_8021d_ops;
166 list_add(&bridge_device->list, &bridge->bridges_list);
168 return bridge_device;
172 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
173 struct mlxsw_sp_bridge_device *bridge_device)
175 list_del(&bridge_device->list);
176 if (bridge_device->vlan_enabled)
177 bridge->vlan_enabled_exists = false;
178 WARN_ON(!list_empty(&bridge_device->ports_list));
179 kfree(bridge_device);
182 static struct mlxsw_sp_bridge_device *
183 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
184 struct net_device *br_dev)
186 struct mlxsw_sp_bridge_device *bridge_device;
188 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
190 return bridge_device;
192 return mlxsw_sp_bridge_device_create(bridge, br_dev);
196 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
197 struct mlxsw_sp_bridge_device *bridge_device)
199 if (list_empty(&bridge_device->ports_list))
200 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
203 static struct mlxsw_sp_bridge_port *
204 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
205 const struct net_device *brport_dev)
207 struct mlxsw_sp_bridge_port *bridge_port;
209 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
210 if (bridge_port->dev == brport_dev)
217 static struct mlxsw_sp_bridge_port *
218 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
219 struct net_device *brport_dev)
221 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
222 struct mlxsw_sp_bridge_device *bridge_device;
227 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
231 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
234 static struct mlxsw_sp_bridge_port *
235 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
236 struct net_device *brport_dev)
238 struct mlxsw_sp_bridge_port *bridge_port;
239 struct mlxsw_sp_port *mlxsw_sp_port;
241 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
245 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
246 bridge_port->lagged = mlxsw_sp_port->lagged;
247 if (bridge_port->lagged)
248 bridge_port->lag_id = mlxsw_sp_port->lag_id;
250 bridge_port->system_port = mlxsw_sp_port->local_port;
251 bridge_port->dev = brport_dev;
252 bridge_port->bridge_device = bridge_device;
253 bridge_port->stp_state = BR_STATE_DISABLED;
254 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC;
255 INIT_LIST_HEAD(&bridge_port->vlans_list);
256 list_add(&bridge_port->list, &bridge_device->ports_list);
257 bridge_port->ref_count = 1;
263 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
265 list_del(&bridge_port->list);
266 WARN_ON(!list_empty(&bridge_port->vlans_list));
271 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
274 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
276 /* In case ports were pulled from out of a bridged LAG, then
277 * it's possible the reference count isn't zero, yet the bridge
278 * port should be destroyed, as it's no longer an upper of ours.
280 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
282 else if (bridge_port->ref_count == 0)
288 static struct mlxsw_sp_bridge_port *
289 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
290 struct net_device *brport_dev)
292 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
293 struct mlxsw_sp_bridge_device *bridge_device;
294 struct mlxsw_sp_bridge_port *bridge_port;
297 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
299 bridge_port->ref_count++;
303 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
304 if (IS_ERR(bridge_device))
305 return ERR_CAST(bridge_device);
307 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
310 goto err_bridge_port_create;
315 err_bridge_port_create:
316 mlxsw_sp_bridge_device_put(bridge, bridge_device);
320 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
321 struct mlxsw_sp_bridge_port *bridge_port)
323 struct mlxsw_sp_bridge_device *bridge_device;
325 bridge_port->ref_count--;
326 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
328 bridge_device = bridge_port->bridge_device;
329 mlxsw_sp_bridge_port_destroy(bridge_port);
330 mlxsw_sp_bridge_device_put(bridge, bridge_device);
333 static struct mlxsw_sp_port_vlan *
334 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
335 const struct mlxsw_sp_bridge_device *
339 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
341 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
343 if (!mlxsw_sp_port_vlan->bridge_port)
345 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
348 if (bridge_device->vlan_enabled &&
349 mlxsw_sp_port_vlan->vid != vid)
351 return mlxsw_sp_port_vlan;
357 static struct mlxsw_sp_port_vlan*
358 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
361 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
363 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
365 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
367 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
368 return mlxsw_sp_port_vlan;
374 static struct mlxsw_sp_bridge_vlan *
375 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
378 struct mlxsw_sp_bridge_vlan *bridge_vlan;
380 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
381 if (bridge_vlan->vid == vid)
388 static struct mlxsw_sp_bridge_vlan *
389 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
391 struct mlxsw_sp_bridge_vlan *bridge_vlan;
393 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
397 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
398 bridge_vlan->vid = vid;
399 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
405 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
407 list_del(&bridge_vlan->list);
408 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
412 static struct mlxsw_sp_bridge_vlan *
413 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
415 struct mlxsw_sp_bridge_vlan *bridge_vlan;
417 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
421 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
424 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
426 if (list_empty(&bridge_vlan->port_vlan_list))
427 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
430 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
431 struct net_device *dev,
432 unsigned long *brport_flags)
434 struct mlxsw_sp_bridge_port *bridge_port;
436 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
437 if (WARN_ON(!bridge_port))
440 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
443 static int mlxsw_sp_port_attr_get(struct net_device *dev,
444 struct switchdev_attr *attr)
446 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
447 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
450 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
451 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
452 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
453 attr->u.ppid.id_len);
455 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
456 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
457 &attr->u.brport_flags);
467 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
468 struct mlxsw_sp_bridge_vlan *bridge_vlan,
471 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
473 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
475 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
477 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
478 bridge_vlan->vid, state);
484 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
485 struct switchdev_trans *trans,
486 struct net_device *orig_dev,
489 struct mlxsw_sp_bridge_port *bridge_port;
490 struct mlxsw_sp_bridge_vlan *bridge_vlan;
493 if (switchdev_trans_ph_prepare(trans))
496 /* It's possible we failed to enslave the port, yet this
497 * operation is executed due to it being deferred.
499 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
504 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
505 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
508 goto err_port_bridge_vlan_stp_set;
511 bridge_port->stp_state = state;
515 err_port_bridge_vlan_stp_set:
516 list_for_each_entry_continue_reverse(bridge_vlan,
517 &bridge_port->vlans_list, list)
518 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
519 bridge_port->stp_state);
524 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
525 struct mlxsw_sp_bridge_vlan *bridge_vlan,
526 enum mlxsw_sp_flood_type packet_type,
529 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
531 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
533 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
535 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
537 mlxsw_sp_port->local_port,
545 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
546 struct mlxsw_sp_bridge_port *bridge_port,
547 enum mlxsw_sp_flood_type packet_type,
550 struct mlxsw_sp_bridge_vlan *bridge_vlan;
553 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
554 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
559 goto err_port_bridge_vlan_flood_set;
564 err_port_bridge_vlan_flood_set:
565 list_for_each_entry_continue_reverse(bridge_vlan,
566 &bridge_port->vlans_list, list)
567 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
568 packet_type, !member);
573 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
574 struct mlxsw_sp_bridge_vlan *bridge_vlan,
577 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
578 u16 vid = bridge_vlan->vid;
580 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
582 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
584 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
591 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
592 struct mlxsw_sp_bridge_port *bridge_port,
595 struct mlxsw_sp_bridge_vlan *bridge_vlan;
598 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
599 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
602 goto err_port_bridge_vlan_learning_set;
607 err_port_bridge_vlan_learning_set:
608 list_for_each_entry_continue_reverse(bridge_vlan,
609 &bridge_port->vlans_list, list)
610 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
615 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
616 struct switchdev_trans *trans,
617 struct net_device *orig_dev,
618 unsigned long brport_flags)
620 struct mlxsw_sp_bridge_port *bridge_port;
623 if (switchdev_trans_ph_prepare(trans))
626 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
628 if (WARN_ON(!bridge_port))
631 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
632 MLXSW_SP_FLOOD_TYPE_UC,
633 brport_flags & BR_FLOOD);
637 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
638 brport_flags & BR_LEARNING);
642 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
647 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
649 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
652 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
653 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
656 mlxsw_sp->bridge->ageing_time = ageing_time;
660 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
661 struct switchdev_trans *trans,
662 unsigned long ageing_clock_t)
664 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
665 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
666 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
668 if (switchdev_trans_ph_prepare(trans)) {
669 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
670 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
676 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
679 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
680 struct switchdev_trans *trans,
681 struct net_device *orig_dev,
684 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
685 struct mlxsw_sp_bridge_device *bridge_device;
687 if (!switchdev_trans_ph_prepare(trans))
690 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
691 if (WARN_ON(!bridge_device))
694 if (bridge_device->vlan_enabled == vlan_enabled)
697 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
701 static int mlxsw_sp_port_attr_mc_router_set(struct mlxsw_sp_port *mlxsw_sp_port,
702 struct switchdev_trans *trans,
703 struct net_device *orig_dev,
704 bool is_port_mc_router)
706 struct mlxsw_sp_bridge_port *bridge_port;
708 if (switchdev_trans_ph_prepare(trans))
711 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
713 if (WARN_ON(!bridge_port))
716 if (!bridge_port->bridge_device->multicast_enabled)
719 return mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
720 MLXSW_SP_FLOOD_TYPE_MC,
724 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
725 struct switchdev_trans *trans,
726 struct net_device *orig_dev,
729 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
730 struct mlxsw_sp_bridge_device *bridge_device;
731 struct mlxsw_sp_bridge_port *bridge_port;
734 if (switchdev_trans_ph_prepare(trans))
737 /* It's possible we failed to enslave the port, yet this
738 * operation is executed due to it being deferred.
740 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
744 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
745 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
746 bool member = mc_disabled ? true : bridge_port->mrouter;
748 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
750 packet_type, member);
755 bridge_device->multicast_enabled = !mc_disabled;
760 static int mlxsw_sp_port_attr_set(struct net_device *dev,
761 const struct switchdev_attr *attr,
762 struct switchdev_trans *trans)
764 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
768 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
769 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
773 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
774 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
776 attr->u.brport_flags);
778 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
779 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
780 attr->u.ageing_time);
782 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
783 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
785 attr->u.vlan_filtering);
787 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
788 err = mlxsw_sp_port_attr_mc_router_set(mlxsw_sp_port, trans,
792 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
793 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
795 attr->u.mc_disabled);
805 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
807 const struct mlxsw_sp_bridge_device *bridge_device;
809 bridge_device = bridge_port->bridge_device;
810 return !bridge_device->multicast_enabled ? true : bridge_port->mrouter;
814 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
815 struct mlxsw_sp_bridge_port *bridge_port)
817 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
818 struct mlxsw_sp_bridge_device *bridge_device;
819 u8 local_port = mlxsw_sp_port->local_port;
820 u16 vid = mlxsw_sp_port_vlan->vid;
821 struct mlxsw_sp_fid *fid;
824 bridge_device = bridge_port->bridge_device;
825 fid = bridge_device->ops->fid_get(bridge_device, vid);
829 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
830 bridge_port->flags & BR_FLOOD);
832 goto err_fid_uc_flood_set;
834 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
835 mlxsw_sp_mc_flood(bridge_port));
837 goto err_fid_mc_flood_set;
839 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
842 goto err_fid_bc_flood_set;
844 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
846 goto err_fid_port_vid_map;
848 mlxsw_sp_port_vlan->fid = fid;
852 err_fid_port_vid_map:
853 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
854 err_fid_bc_flood_set:
855 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
856 err_fid_mc_flood_set:
857 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
858 err_fid_uc_flood_set:
859 mlxsw_sp_fid_put(fid);
864 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
866 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
867 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
868 u8 local_port = mlxsw_sp_port->local_port;
869 u16 vid = mlxsw_sp_port_vlan->vid;
871 mlxsw_sp_port_vlan->fid = NULL;
872 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
873 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
874 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
875 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
876 mlxsw_sp_fid_put(fid);
880 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
881 u16 vid, bool is_pvid)
885 else if (mlxsw_sp_port->pvid == vid)
886 return 0; /* Dis-allow untagged packets */
888 return mlxsw_sp_port->pvid;
892 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
893 struct mlxsw_sp_bridge_port *bridge_port)
895 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
896 struct mlxsw_sp_bridge_vlan *bridge_vlan;
897 u16 vid = mlxsw_sp_port_vlan->vid;
900 /* No need to continue if only VLAN flags were changed */
901 if (mlxsw_sp_port_vlan->bridge_port)
904 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
908 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
909 bridge_port->flags & BR_LEARNING);
911 goto err_port_vid_learning_set;
913 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
914 bridge_port->stp_state);
916 goto err_port_vid_stp_set;
918 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
921 goto err_bridge_vlan_get;
924 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
925 &bridge_vlan->port_vlan_list);
927 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
929 mlxsw_sp_port_vlan->bridge_port = bridge_port;
934 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
935 err_port_vid_stp_set:
936 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
937 err_port_vid_learning_set:
938 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
943 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
945 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
946 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
947 struct mlxsw_sp_bridge_vlan *bridge_vlan;
948 struct mlxsw_sp_bridge_port *bridge_port;
949 u16 vid = mlxsw_sp_port_vlan->vid;
952 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
953 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
956 bridge_port = mlxsw_sp_port_vlan->bridge_port;
957 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
958 last = list_is_singular(&bridge_vlan->port_vlan_list);
960 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
961 mlxsw_sp_bridge_vlan_put(bridge_vlan);
962 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
963 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
965 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
967 mlxsw_sp_fid_index(fid));
968 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
970 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
971 mlxsw_sp_port_vlan->bridge_port = NULL;
975 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
976 struct mlxsw_sp_bridge_port *bridge_port,
977 u16 vid, bool is_untagged, bool is_pvid)
979 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
980 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
981 struct mlxsw_sp_bridge_vlan *bridge_vlan;
982 u16 old_pvid = mlxsw_sp_port->pvid;
985 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
986 if (IS_ERR(mlxsw_sp_port_vlan))
987 return PTR_ERR(mlxsw_sp_port_vlan);
989 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
992 goto err_port_vlan_set;
994 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
996 goto err_port_pvid_set;
998 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1000 goto err_port_vlan_bridge_join;
1002 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1003 bridge_vlan->egress_untagged = is_untagged;
1004 bridge_vlan->pvid = is_pvid;
1008 err_port_vlan_bridge_join:
1009 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1011 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1013 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1017 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1018 const struct switchdev_obj_port_vlan *vlan,
1019 struct switchdev_trans *trans)
1021 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1022 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1023 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1024 struct net_device *orig_dev = vlan->obj.orig_dev;
1025 struct mlxsw_sp_bridge_port *bridge_port;
1028 if (switchdev_trans_ph_prepare(trans))
1031 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1032 if (WARN_ON(!bridge_port))
1035 if (!bridge_port->bridge_device->vlan_enabled)
1038 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1041 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1051 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1053 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1054 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1058 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1059 struct mlxsw_sp_bridge_port *bridge_port,
1062 bool lagged = bridge_port->lagged;
1063 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1066 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1067 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1068 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1069 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1071 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1074 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1076 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1077 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1080 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1082 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1083 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1086 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1087 const char *mac, u16 fid, bool adding,
1088 enum mlxsw_reg_sfd_rec_action action,
1094 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1098 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1099 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1100 mac, fid, action, local_port);
1101 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1107 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1108 const char *mac, u16 fid, bool adding,
1111 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1112 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1115 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1118 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1119 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1123 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1124 const char *mac, u16 fid, u16 lag_vid,
1125 bool adding, bool dynamic)
1130 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1134 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1135 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1136 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1138 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1145 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
1146 const struct switchdev_obj_port_fdb *fdb,
1147 struct switchdev_trans *trans)
1149 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1150 struct net_device *orig_dev = fdb->obj.orig_dev;
1151 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1152 struct mlxsw_sp_bridge_device *bridge_device;
1153 struct mlxsw_sp_bridge_port *bridge_port;
1156 if (switchdev_trans_ph_prepare(trans))
1159 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1160 if (WARN_ON(!bridge_port))
1163 bridge_device = bridge_port->bridge_device;
1164 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1167 if (!mlxsw_sp_port_vlan)
1170 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1171 vid = mlxsw_sp_port_vlan->vid;
1173 if (!mlxsw_sp_port->lagged)
1174 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1175 mlxsw_sp_port->local_port,
1176 fdb->addr, fid_index, true,
1179 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1180 mlxsw_sp_port->lag_id,
1181 fdb->addr, fid_index, vid,
1185 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1186 u16 fid, u16 mid, bool adding)
1191 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1195 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1196 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1197 MLXSW_REG_SFD_REC_ACTION_NOP, mid);
1198 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1203 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
1204 bool add, bool clear_all_ports)
1206 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1210 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1214 mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
1215 if (clear_all_ports) {
1216 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
1217 if (mlxsw_sp->ports[i])
1218 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1220 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1225 static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
1226 const unsigned char *addr,
1229 struct mlxsw_sp_mid *mid;
1231 list_for_each_entry(mid, &mlxsw_sp->bridge->mids_list, list) {
1232 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1238 static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1239 const unsigned char *addr,
1242 struct mlxsw_sp_mid *mid;
1245 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1247 if (mid_idx == MLXSW_SP_MID_MAX)
1250 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1254 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1255 ether_addr_copy(mid->addr, addr);
1259 list_add_tail(&mid->list, &mlxsw_sp->bridge->mids_list);
1264 static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
1265 struct mlxsw_sp_mid *mid)
1267 if (--mid->ref_count == 0) {
1268 list_del(&mid->list);
1269 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1276 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1277 const struct switchdev_obj_port_mdb *mdb,
1278 struct switchdev_trans *trans)
1280 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1281 struct net_device *orig_dev = mdb->obj.orig_dev;
1282 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1283 struct net_device *dev = mlxsw_sp_port->dev;
1284 struct mlxsw_sp_bridge_device *bridge_device;
1285 struct mlxsw_sp_bridge_port *bridge_port;
1286 struct mlxsw_sp_mid *mid;
1290 if (switchdev_trans_ph_prepare(trans))
1293 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1294 if (WARN_ON(!bridge_port))
1297 bridge_device = bridge_port->bridge_device;
1298 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1301 if (WARN_ON(!mlxsw_sp_port_vlan))
1304 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1306 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, fid_index);
1308 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, fid_index);
1310 netdev_err(dev, "Unable to allocate MC group\n");
1316 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
1317 mid->ref_count == 1);
1319 netdev_err(dev, "Unable to set SMID\n");
1323 if (mid->ref_count == 1) {
1324 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index,
1327 netdev_err(dev, "Unable to set MC SFD\n");
1335 __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
1339 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1340 const struct switchdev_obj *obj,
1341 struct switchdev_trans *trans)
1343 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1347 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1348 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1349 SWITCHDEV_OBJ_PORT_VLAN(obj),
1352 case SWITCHDEV_OBJ_ID_PORT_FDB:
1353 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
1354 SWITCHDEV_OBJ_PORT_FDB(obj),
1357 case SWITCHDEV_OBJ_ID_PORT_MDB:
1358 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1359 SWITCHDEV_OBJ_PORT_MDB(obj),
1371 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1372 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1374 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1375 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1377 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1378 if (WARN_ON(!mlxsw_sp_port_vlan))
1381 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1382 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1383 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1384 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1387 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1388 const struct switchdev_obj_port_vlan *vlan)
1390 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1391 struct net_device *orig_dev = vlan->obj.orig_dev;
1392 struct mlxsw_sp_bridge_port *bridge_port;
1395 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1396 if (WARN_ON(!bridge_port))
1399 if (!bridge_port->bridge_device->vlan_enabled)
1402 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1403 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1409 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1410 const struct switchdev_obj_port_fdb *fdb)
1412 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1413 struct net_device *orig_dev = fdb->obj.orig_dev;
1414 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1415 struct mlxsw_sp_bridge_device *bridge_device;
1416 struct mlxsw_sp_bridge_port *bridge_port;
1419 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1420 if (WARN_ON(!bridge_port))
1423 bridge_device = bridge_port->bridge_device;
1424 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1427 if (!mlxsw_sp_port_vlan)
1430 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1431 vid = mlxsw_sp_port_vlan->vid;
1433 if (!mlxsw_sp_port->lagged)
1434 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1435 mlxsw_sp_port->local_port,
1436 fdb->addr, fid_index, false,
1439 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1440 mlxsw_sp_port->lag_id,
1441 fdb->addr, fid_index, vid,
1445 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1446 const struct switchdev_obj_port_mdb *mdb)
1448 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1449 struct net_device *orig_dev = mdb->obj.orig_dev;
1450 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1451 struct mlxsw_sp_bridge_device *bridge_device;
1452 struct net_device *dev = mlxsw_sp_port->dev;
1453 struct mlxsw_sp_bridge_port *bridge_port;
1454 struct mlxsw_sp_mid *mid;
1459 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1460 if (WARN_ON(!bridge_port))
1463 bridge_device = bridge_port->bridge_device;
1464 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1467 if (WARN_ON(!mlxsw_sp_port_vlan))
1470 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1472 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, fid_index);
1474 netdev_err(dev, "Unable to remove port from MC DB\n");
1478 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1480 netdev_err(dev, "Unable to remove port from SMID\n");
1483 if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1484 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index,
1487 netdev_err(dev, "Unable to remove MC SFD\n");
1493 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1494 const struct switchdev_obj *obj)
1496 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1500 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1501 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1502 SWITCHDEV_OBJ_PORT_VLAN(obj));
1504 case SWITCHDEV_OBJ_ID_PORT_FDB:
1505 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1506 SWITCHDEV_OBJ_PORT_FDB(obj));
1508 case SWITCHDEV_OBJ_ID_PORT_MDB:
1509 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1510 SWITCHDEV_OBJ_PORT_MDB(obj));
1520 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1523 struct mlxsw_sp_port *mlxsw_sp_port;
1524 u64 max_lag_members;
1527 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1529 for (i = 0; i < max_lag_members; i++) {
1530 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1532 return mlxsw_sp_port;
1537 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1538 struct switchdev_obj_port_fdb *fdb,
1539 switchdev_obj_dump_cb_t *cb)
1541 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1542 struct net_device *orig_dev = fdb->obj.orig_dev;
1543 struct mlxsw_sp_bridge_port *bridge_port;
1544 u16 lag_id, fid_index;
1551 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1555 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1559 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1561 struct mlxsw_sp_port *tmp;
1565 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
1566 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1570 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1572 /* Even in case of error, we have to run the dump to the end
1573 * so the session in firmware is finished.
1578 for (i = 0; i < num_rec; i++) {
1579 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1580 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
1581 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac,
1584 if (bridge_port->lagged)
1586 if (bridge_port->system_port != local_port)
1588 if (bridge_port->bridge_device->vlan_enabled)
1589 fdb->vid = fid_index;
1592 ether_addr_copy(fdb->addr, mac);
1593 fdb->ndm_state = NUD_REACHABLE;
1594 err = cb(&fdb->obj);
1598 case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1599 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
1602 if (!bridge_port->lagged)
1604 if (bridge_port->lag_id != lag_id)
1606 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1607 if (tmp->local_port !=
1608 mlxsw_sp_port->local_port)
1610 if (bridge_port->bridge_device->vlan_enabled)
1611 fdb->vid = fid_index;
1614 ether_addr_copy(fdb->addr, mac);
1615 fdb->ndm_state = NUD_REACHABLE;
1616 err = cb(&fdb->obj);
1622 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1626 return stored_err ? stored_err : err;
1629 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1630 struct switchdev_obj_port_vlan *vlan,
1631 switchdev_obj_dump_cb_t *cb)
1633 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1634 struct net_device *orig_dev = vlan->obj.orig_dev;
1635 struct mlxsw_sp_bridge_port *bridge_port;
1636 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1639 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1640 if (WARN_ON(!bridge_port))
1643 if (!bridge_port->bridge_device->vlan_enabled)
1646 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
1648 if (bridge_vlan->pvid)
1649 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1650 if (bridge_vlan->egress_untagged)
1651 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1652 vlan->vid_begin = bridge_vlan->vid;
1653 vlan->vid_end = bridge_vlan->vid;
1654 err = cb(&vlan->obj);
1662 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1663 struct switchdev_obj *obj,
1664 switchdev_obj_dump_cb_t *cb)
1666 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1670 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1671 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1672 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1674 case SWITCHDEV_OBJ_ID_PORT_FDB:
1675 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
1676 SWITCHDEV_OBJ_PORT_FDB(obj), cb);
1686 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1687 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1688 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1689 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1690 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1691 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
1695 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1696 struct mlxsw_sp_bridge_port *bridge_port,
1697 struct mlxsw_sp_port *mlxsw_sp_port)
1699 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1701 if (is_vlan_dev(bridge_port->dev))
1704 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1705 if (WARN_ON(!mlxsw_sp_port_vlan))
1708 /* Let VLAN-aware bridge take care of its own VLANs */
1709 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1715 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1716 struct mlxsw_sp_bridge_port *bridge_port,
1717 struct mlxsw_sp_port *mlxsw_sp_port)
1719 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1720 /* Make sure untagged frames are allowed to ingress */
1721 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1724 static struct mlxsw_sp_fid *
1725 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1728 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1730 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1733 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1734 .port_join = mlxsw_sp_bridge_8021q_port_join,
1735 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
1736 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
1740 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1741 const struct net_device *br_dev)
1743 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1745 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1747 if (mlxsw_sp_port_vlan->bridge_port &&
1748 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1757 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1758 struct mlxsw_sp_bridge_port *bridge_port,
1759 struct mlxsw_sp_port *mlxsw_sp_port)
1761 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1764 if (!is_vlan_dev(bridge_port->dev))
1766 vid = vlan_dev_vlan_id(bridge_port->dev);
1768 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1769 if (WARN_ON(!mlxsw_sp_port_vlan))
1772 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1773 netdev_err(mlxsw_sp_port->dev, "Can't bridge VLAN uppers of the same port\n");
1777 /* Port is no longer usable as a router interface */
1778 if (mlxsw_sp_port_vlan->fid)
1779 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1781 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1785 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1786 struct mlxsw_sp_bridge_port *bridge_port,
1787 struct mlxsw_sp_port *mlxsw_sp_port)
1789 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1790 u16 vid = vlan_dev_vlan_id(bridge_port->dev);
1792 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1793 if (WARN_ON(!mlxsw_sp_port_vlan))
1796 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1799 static struct mlxsw_sp_fid *
1800 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1803 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1805 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
1808 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
1809 .port_join = mlxsw_sp_bridge_8021d_port_join,
1810 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
1811 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
1814 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
1815 struct net_device *brport_dev,
1816 struct net_device *br_dev)
1818 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1819 struct mlxsw_sp_bridge_device *bridge_device;
1820 struct mlxsw_sp_bridge_port *bridge_port;
1823 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
1824 if (IS_ERR(bridge_port))
1825 return PTR_ERR(bridge_port);
1826 bridge_device = bridge_port->bridge_device;
1828 err = bridge_device->ops->port_join(bridge_device, bridge_port,
1836 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1840 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
1841 struct net_device *brport_dev,
1842 struct net_device *br_dev)
1844 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1845 struct mlxsw_sp_bridge_device *bridge_device;
1846 struct mlxsw_sp_bridge_port *bridge_port;
1848 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
1851 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
1855 bridge_device->ops->port_leave(bridge_device, bridge_port,
1857 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1860 static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1862 struct net_device *dev)
1864 struct switchdev_notifier_fdb_info info;
1865 unsigned long notifier_type;
1867 if (learning_sync) {
1870 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1871 call_switchdev_notifiers(notifier_type, dev, &info.info);
1875 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1876 char *sfn_pl, int rec_index,
1879 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1880 struct mlxsw_sp_bridge_device *bridge_device;
1881 struct mlxsw_sp_bridge_port *bridge_port;
1882 struct mlxsw_sp_port *mlxsw_sp_port;
1886 bool do_notification = true;
1889 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
1890 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1891 if (!mlxsw_sp_port) {
1892 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
1896 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
1897 if (!mlxsw_sp_port_vlan) {
1898 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
1902 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1904 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
1908 bridge_device = bridge_port->bridge_device;
1909 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
1912 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
1915 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
1919 if (!do_notification)
1921 mlxsw_sp_fdb_call_notifiers(bridge_port->flags & BR_LEARNING_SYNC,
1922 adding, mac, vid, bridge_port->dev);
1927 do_notification = false;
1931 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1932 char *sfn_pl, int rec_index,
1935 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1936 struct mlxsw_sp_bridge_device *bridge_device;
1937 struct mlxsw_sp_bridge_port *bridge_port;
1938 struct mlxsw_sp_port *mlxsw_sp_port;
1943 bool do_notification = true;
1946 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
1947 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1948 if (!mlxsw_sp_port) {
1949 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
1953 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
1954 if (!mlxsw_sp_port_vlan) {
1955 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
1959 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1961 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
1965 bridge_device = bridge_port->bridge_device;
1966 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
1967 lag_vid = mlxsw_sp_port_vlan->vid;
1970 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
1973 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
1977 if (!do_notification)
1979 mlxsw_sp_fdb_call_notifiers(bridge_port->flags & BR_LEARNING_SYNC,
1980 adding, mac, vid, bridge_port->dev);
1985 do_notification = false;
1989 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1990 char *sfn_pl, int rec_index)
1992 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1993 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1994 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1997 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1998 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2001 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2002 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2005 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2006 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2012 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2014 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2016 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2017 msecs_to_jiffies(bridge->fdb_notify.interval));
2020 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2022 struct mlxsw_sp_bridge *bridge;
2023 struct mlxsw_sp *mlxsw_sp;
2029 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2033 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2034 mlxsw_sp = bridge->mlxsw_sp;
2037 mlxsw_reg_sfn_pack(sfn_pl);
2038 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2040 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2043 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2044 for (i = 0; i < num_rec; i++)
2045 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2050 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2053 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2055 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2058 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2060 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2063 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2064 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2065 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2069 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2071 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2074 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2076 struct mlxsw_sp_bridge *bridge;
2078 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2081 mlxsw_sp->bridge = bridge;
2082 bridge->mlxsw_sp = mlxsw_sp;
2084 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2085 INIT_LIST_HEAD(&mlxsw_sp->bridge->mids_list);
2087 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2088 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2090 return mlxsw_sp_fdb_init(mlxsw_sp);
2093 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2095 mlxsw_sp_fdb_fini(mlxsw_sp);
2096 WARN_ON(!list_empty(&mlxsw_sp->bridge->mids_list));
2097 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2098 kfree(mlxsw_sp->bridge);
2101 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2103 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2106 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)