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_router.h"
57 struct mlxsw_sp_bridge_ops
;
59 struct mlxsw_sp_bridge
{
60 struct mlxsw_sp
*mlxsw_sp
;
62 struct delayed_work dw
;
63 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
64 unsigned int interval
; /* ms */
66 #define MLXSW_SP_MIN_AGEING_TIME 10
67 #define MLXSW_SP_MAX_AGEING_TIME 1000000
68 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
70 bool vlan_enabled_exists
;
71 struct list_head bridges_list
;
72 DECLARE_BITMAP(mids_bitmap
, MLXSW_SP_MID_MAX
);
73 const struct mlxsw_sp_bridge_ops
*bridge_8021q_ops
;
74 const struct mlxsw_sp_bridge_ops
*bridge_8021d_ops
;
77 struct mlxsw_sp_bridge_device
{
78 struct net_device
*dev
;
79 struct list_head list
;
80 struct list_head ports_list
;
81 struct list_head mids_list
;
85 const struct mlxsw_sp_bridge_ops
*ops
;
88 struct mlxsw_sp_bridge_port
{
89 struct net_device
*dev
;
90 struct mlxsw_sp_bridge_device
*bridge_device
;
91 struct list_head list
;
92 struct list_head vlans_list
;
93 unsigned int ref_count
;
104 struct mlxsw_sp_bridge_vlan
{
105 struct list_head list
;
106 struct list_head port_vlan_list
;
110 struct mlxsw_sp_bridge_ops
{
111 int (*port_join
)(struct mlxsw_sp_bridge_device
*bridge_device
,
112 struct mlxsw_sp_bridge_port
*bridge_port
,
113 struct mlxsw_sp_port
*mlxsw_sp_port
,
114 struct netlink_ext_ack
*extack
);
115 void (*port_leave
)(struct mlxsw_sp_bridge_device
*bridge_device
,
116 struct mlxsw_sp_bridge_port
*bridge_port
,
117 struct mlxsw_sp_port
*mlxsw_sp_port
);
118 struct mlxsw_sp_fid
*
119 (*fid_get
)(struct mlxsw_sp_bridge_device
*bridge_device
,
124 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp
*mlxsw_sp
,
125 struct mlxsw_sp_bridge_port
*bridge_port
,
129 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
130 struct mlxsw_sp_bridge_port
*bridge_port
);
133 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port
*mlxsw_sp_port
,
134 struct mlxsw_sp_bridge_device
138 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port
*mlxsw_sp_port
,
139 struct mlxsw_sp_bridge_port
*bridge_port
,
142 static struct mlxsw_sp_bridge_device
*
143 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge
*bridge
,
144 const struct net_device
*br_dev
)
146 struct mlxsw_sp_bridge_device
*bridge_device
;
148 list_for_each_entry(bridge_device
, &bridge
->bridges_list
, list
)
149 if (bridge_device
->dev
== br_dev
)
150 return bridge_device
;
155 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp
*mlxsw_sp
,
156 const struct net_device
*br_dev
)
158 return !!mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
161 static struct mlxsw_sp_bridge_device
*
162 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge
*bridge
,
163 struct net_device
*br_dev
)
165 struct device
*dev
= bridge
->mlxsw_sp
->bus_info
->dev
;
166 struct mlxsw_sp_bridge_device
*bridge_device
;
167 bool vlan_enabled
= br_vlan_enabled(br_dev
);
169 if (vlan_enabled
&& bridge
->vlan_enabled_exists
) {
170 dev_err(dev
, "Only one VLAN-aware bridge is supported\n");
171 return ERR_PTR(-EINVAL
);
174 bridge_device
= kzalloc(sizeof(*bridge_device
), GFP_KERNEL
);
176 return ERR_PTR(-ENOMEM
);
178 bridge_device
->dev
= br_dev
;
179 bridge_device
->vlan_enabled
= vlan_enabled
;
180 bridge_device
->multicast_enabled
= br_multicast_enabled(br_dev
);
181 bridge_device
->mrouter
= br_multicast_router(br_dev
);
182 INIT_LIST_HEAD(&bridge_device
->ports_list
);
184 bridge
->vlan_enabled_exists
= true;
185 bridge_device
->ops
= bridge
->bridge_8021q_ops
;
187 bridge_device
->ops
= bridge
->bridge_8021d_ops
;
189 INIT_LIST_HEAD(&bridge_device
->mids_list
);
190 list_add(&bridge_device
->list
, &bridge
->bridges_list
);
192 return bridge_device
;
196 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge
*bridge
,
197 struct mlxsw_sp_bridge_device
*bridge_device
)
199 list_del(&bridge_device
->list
);
200 if (bridge_device
->vlan_enabled
)
201 bridge
->vlan_enabled_exists
= false;
202 WARN_ON(!list_empty(&bridge_device
->ports_list
));
203 WARN_ON(!list_empty(&bridge_device
->mids_list
));
204 kfree(bridge_device
);
207 static struct mlxsw_sp_bridge_device
*
208 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge
*bridge
,
209 struct net_device
*br_dev
)
211 struct mlxsw_sp_bridge_device
*bridge_device
;
213 bridge_device
= mlxsw_sp_bridge_device_find(bridge
, br_dev
);
215 return bridge_device
;
217 return mlxsw_sp_bridge_device_create(bridge
, br_dev
);
221 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge
*bridge
,
222 struct mlxsw_sp_bridge_device
*bridge_device
)
224 if (list_empty(&bridge_device
->ports_list
))
225 mlxsw_sp_bridge_device_destroy(bridge
, bridge_device
);
228 static struct mlxsw_sp_bridge_port
*
229 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device
*bridge_device
,
230 const struct net_device
*brport_dev
)
232 struct mlxsw_sp_bridge_port
*bridge_port
;
234 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
235 if (bridge_port
->dev
== brport_dev
)
242 static struct mlxsw_sp_bridge_port
*
243 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge
*bridge
,
244 struct net_device
*brport_dev
)
246 struct net_device
*br_dev
= netdev_master_upper_dev_get(brport_dev
);
247 struct mlxsw_sp_bridge_device
*bridge_device
;
252 bridge_device
= mlxsw_sp_bridge_device_find(bridge
, br_dev
);
256 return __mlxsw_sp_bridge_port_find(bridge_device
, brport_dev
);
259 static struct mlxsw_sp_bridge_port
*
260 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device
*bridge_device
,
261 struct net_device
*brport_dev
)
263 struct mlxsw_sp_bridge_port
*bridge_port
;
264 struct mlxsw_sp_port
*mlxsw_sp_port
;
266 bridge_port
= kzalloc(sizeof(*bridge_port
), GFP_KERNEL
);
270 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(brport_dev
);
271 bridge_port
->lagged
= mlxsw_sp_port
->lagged
;
272 if (bridge_port
->lagged
)
273 bridge_port
->lag_id
= mlxsw_sp_port
->lag_id
;
275 bridge_port
->system_port
= mlxsw_sp_port
->local_port
;
276 bridge_port
->dev
= brport_dev
;
277 bridge_port
->bridge_device
= bridge_device
;
278 bridge_port
->stp_state
= BR_STATE_DISABLED
;
279 bridge_port
->flags
= BR_LEARNING
| BR_FLOOD
| BR_LEARNING_SYNC
|
281 INIT_LIST_HEAD(&bridge_port
->vlans_list
);
282 list_add(&bridge_port
->list
, &bridge_device
->ports_list
);
283 bridge_port
->ref_count
= 1;
289 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port
*bridge_port
)
291 list_del(&bridge_port
->list
);
292 WARN_ON(!list_empty(&bridge_port
->vlans_list
));
297 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port
*
300 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_port
->dev
);
302 /* In case ports were pulled from out of a bridged LAG, then
303 * it's possible the reference count isn't zero, yet the bridge
304 * port should be destroyed, as it's no longer an upper of ours.
306 if (!mlxsw_sp
&& list_empty(&bridge_port
->vlans_list
))
308 else if (bridge_port
->ref_count
== 0)
314 static struct mlxsw_sp_bridge_port
*
315 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge
*bridge
,
316 struct net_device
*brport_dev
)
318 struct net_device
*br_dev
= netdev_master_upper_dev_get(brport_dev
);
319 struct mlxsw_sp_bridge_device
*bridge_device
;
320 struct mlxsw_sp_bridge_port
*bridge_port
;
323 bridge_port
= mlxsw_sp_bridge_port_find(bridge
, brport_dev
);
325 bridge_port
->ref_count
++;
329 bridge_device
= mlxsw_sp_bridge_device_get(bridge
, br_dev
);
330 if (IS_ERR(bridge_device
))
331 return ERR_CAST(bridge_device
);
333 bridge_port
= mlxsw_sp_bridge_port_create(bridge_device
, brport_dev
);
336 goto err_bridge_port_create
;
341 err_bridge_port_create
:
342 mlxsw_sp_bridge_device_put(bridge
, bridge_device
);
346 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge
*bridge
,
347 struct mlxsw_sp_bridge_port
*bridge_port
)
349 struct mlxsw_sp_bridge_device
*bridge_device
;
351 bridge_port
->ref_count
--;
352 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port
))
354 bridge_device
= bridge_port
->bridge_device
;
355 mlxsw_sp_bridge_port_destroy(bridge_port
);
356 mlxsw_sp_bridge_device_put(bridge
, bridge_device
);
359 static struct mlxsw_sp_port_vlan
*
360 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port
*mlxsw_sp_port
,
361 const struct mlxsw_sp_bridge_device
*
365 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
367 list_for_each_entry(mlxsw_sp_port_vlan
, &mlxsw_sp_port
->vlans_list
,
369 if (!mlxsw_sp_port_vlan
->bridge_port
)
371 if (mlxsw_sp_port_vlan
->bridge_port
->bridge_device
!=
374 if (bridge_device
->vlan_enabled
&&
375 mlxsw_sp_port_vlan
->vid
!= vid
)
377 return mlxsw_sp_port_vlan
;
383 static struct mlxsw_sp_port_vlan
*
384 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port
*mlxsw_sp_port
,
387 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
389 list_for_each_entry(mlxsw_sp_port_vlan
, &mlxsw_sp_port
->vlans_list
,
391 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
393 if (fid
&& mlxsw_sp_fid_index(fid
) == fid_index
)
394 return mlxsw_sp_port_vlan
;
400 static struct mlxsw_sp_bridge_vlan
*
401 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port
*bridge_port
,
404 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
406 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
407 if (bridge_vlan
->vid
== vid
)
414 static struct mlxsw_sp_bridge_vlan
*
415 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
417 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
419 bridge_vlan
= kzalloc(sizeof(*bridge_vlan
), GFP_KERNEL
);
423 INIT_LIST_HEAD(&bridge_vlan
->port_vlan_list
);
424 bridge_vlan
->vid
= vid
;
425 list_add(&bridge_vlan
->list
, &bridge_port
->vlans_list
);
431 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan
*bridge_vlan
)
433 list_del(&bridge_vlan
->list
);
434 WARN_ON(!list_empty(&bridge_vlan
->port_vlan_list
));
438 static struct mlxsw_sp_bridge_vlan
*
439 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
441 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
443 bridge_vlan
= mlxsw_sp_bridge_vlan_find(bridge_port
, vid
);
447 return mlxsw_sp_bridge_vlan_create(bridge_port
, vid
);
450 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan
*bridge_vlan
)
452 if (list_empty(&bridge_vlan
->port_vlan_list
))
453 mlxsw_sp_bridge_vlan_destroy(bridge_vlan
);
456 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge
*bridge
,
457 struct net_device
*dev
,
458 unsigned long *brport_flags
)
460 struct mlxsw_sp_bridge_port
*bridge_port
;
462 bridge_port
= mlxsw_sp_bridge_port_find(bridge
, dev
);
463 if (WARN_ON(!bridge_port
))
466 memcpy(brport_flags
, &bridge_port
->flags
, sizeof(*brport_flags
));
469 static int mlxsw_sp_port_attr_get(struct net_device
*dev
,
470 struct switchdev_attr
*attr
)
472 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
473 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
476 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
477 attr
->u
.ppid
.id_len
= sizeof(mlxsw_sp
->base_mac
);
478 memcpy(&attr
->u
.ppid
.id
, &mlxsw_sp
->base_mac
,
479 attr
->u
.ppid
.id_len
);
481 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
482 mlxsw_sp_port_bridge_flags_get(mlxsw_sp
->bridge
, attr
->orig_dev
,
483 &attr
->u
.brport_flags
);
485 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
486 attr
->u
.brport_flags_support
= BR_LEARNING
| BR_FLOOD
|
497 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
498 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
501 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
503 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
505 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
507 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
,
508 bridge_vlan
->vid
, state
);
514 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
515 struct switchdev_trans
*trans
,
516 struct net_device
*orig_dev
,
519 struct mlxsw_sp_bridge_port
*bridge_port
;
520 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
523 if (switchdev_trans_ph_prepare(trans
))
526 /* It's possible we failed to enslave the port, yet this
527 * operation is executed due to it being deferred.
529 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
534 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
535 err
= mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port
,
538 goto err_port_bridge_vlan_stp_set
;
541 bridge_port
->stp_state
= state
;
545 err_port_bridge_vlan_stp_set
:
546 list_for_each_entry_continue_reverse(bridge_vlan
,
547 &bridge_port
->vlans_list
, list
)
548 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port
, bridge_vlan
,
549 bridge_port
->stp_state
);
554 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
555 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
556 enum mlxsw_sp_flood_type packet_type
,
559 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
561 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
563 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
565 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan
->fid
,
567 mlxsw_sp_port
->local_port
,
575 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
576 struct mlxsw_sp_bridge_port
*bridge_port
,
577 enum mlxsw_sp_flood_type packet_type
,
580 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
583 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
584 err
= mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port
,
589 goto err_port_bridge_vlan_flood_set
;
594 err_port_bridge_vlan_flood_set
:
595 list_for_each_entry_continue_reverse(bridge_vlan
,
596 &bridge_port
->vlans_list
, list
)
597 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port
, bridge_vlan
,
598 packet_type
, !member
);
603 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
604 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
607 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
608 u16 vid
= bridge_vlan
->vid
;
610 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
612 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
614 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, set
);
621 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
622 struct mlxsw_sp_bridge_port
*bridge_port
,
625 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
628 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
629 err
= mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port
,
632 goto err_port_bridge_vlan_learning_set
;
637 err_port_bridge_vlan_learning_set
:
638 list_for_each_entry_continue_reverse(bridge_vlan
,
639 &bridge_port
->vlans_list
, list
)
640 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port
,
645 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
646 struct switchdev_trans
*trans
,
647 struct net_device
*orig_dev
,
648 unsigned long brport_flags
)
650 struct mlxsw_sp_bridge_port
*bridge_port
;
653 if (switchdev_trans_ph_prepare(trans
))
656 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
661 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
662 MLXSW_SP_FLOOD_TYPE_UC
,
663 brport_flags
& BR_FLOOD
);
667 err
= mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port
, bridge_port
,
668 brport_flags
& BR_LEARNING
);
672 if (bridge_port
->bridge_device
->multicast_enabled
)
675 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
676 MLXSW_SP_FLOOD_TYPE_MC
,
683 memcpy(&bridge_port
->flags
, &brport_flags
, sizeof(brport_flags
));
687 static int mlxsw_sp_ageing_set(struct mlxsw_sp
*mlxsw_sp
, u32 ageing_time
)
689 char sfdat_pl
[MLXSW_REG_SFDAT_LEN
];
692 mlxsw_reg_sfdat_pack(sfdat_pl
, ageing_time
);
693 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdat
), sfdat_pl
);
696 mlxsw_sp
->bridge
->ageing_time
= ageing_time
;
700 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
701 struct switchdev_trans
*trans
,
702 unsigned long ageing_clock_t
)
704 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
705 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock_t
);
706 u32 ageing_time
= jiffies_to_msecs(ageing_jiffies
) / 1000;
708 if (switchdev_trans_ph_prepare(trans
)) {
709 if (ageing_time
< MLXSW_SP_MIN_AGEING_TIME
||
710 ageing_time
> MLXSW_SP_MAX_AGEING_TIME
)
716 return mlxsw_sp_ageing_set(mlxsw_sp
, ageing_time
);
719 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
720 struct switchdev_trans
*trans
,
721 struct net_device
*orig_dev
,
724 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
725 struct mlxsw_sp_bridge_device
*bridge_device
;
727 if (!switchdev_trans_ph_prepare(trans
))
730 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
731 if (WARN_ON(!bridge_device
))
734 if (bridge_device
->vlan_enabled
== vlan_enabled
)
737 netdev_err(bridge_device
->dev
, "VLAN filtering can't be changed for existing bridge\n");
741 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
742 struct switchdev_trans
*trans
,
743 struct net_device
*orig_dev
,
744 bool is_port_mrouter
)
746 struct mlxsw_sp_bridge_port
*bridge_port
;
749 if (switchdev_trans_ph_prepare(trans
))
752 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
757 if (!bridge_port
->bridge_device
->multicast_enabled
)
760 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
761 MLXSW_SP_FLOOD_TYPE_MC
,
766 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port
, bridge_port
,
769 bridge_port
->mrouter
= is_port_mrouter
;
773 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port
*bridge_port
)
775 const struct mlxsw_sp_bridge_device
*bridge_device
;
777 bridge_device
= bridge_port
->bridge_device
;
778 return bridge_device
->multicast_enabled
? bridge_port
->mrouter
:
779 bridge_port
->flags
& BR_MCAST_FLOOD
;
782 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
783 struct switchdev_trans
*trans
,
784 struct net_device
*orig_dev
,
787 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
788 struct mlxsw_sp_bridge_device
*bridge_device
;
789 struct mlxsw_sp_bridge_port
*bridge_port
;
792 if (switchdev_trans_ph_prepare(trans
))
795 /* It's possible we failed to enslave the port, yet this
796 * operation is executed due to it being deferred.
798 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
802 if (bridge_device
->multicast_enabled
!= !mc_disabled
) {
803 bridge_device
->multicast_enabled
= !mc_disabled
;
804 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port
,
808 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
809 enum mlxsw_sp_flood_type packet_type
= MLXSW_SP_FLOOD_TYPE_MC
;
810 bool member
= mlxsw_sp_mc_flood(bridge_port
);
812 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
,
814 packet_type
, member
);
819 bridge_device
->multicast_enabled
= !mc_disabled
;
824 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp
*mlxsw_sp
,
825 u16 mid_idx
, bool add
)
830 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
834 mlxsw_reg_smid_pack(smid_pl
, mid_idx
,
835 mlxsw_sp_router_port(mlxsw_sp
), add
);
836 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
842 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp
*mlxsw_sp
,
843 struct mlxsw_sp_bridge_device
*bridge_device
,
846 struct mlxsw_sp_mid
*mid
;
848 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
)
849 mlxsw_sp_smid_router_port_set(mlxsw_sp
, mid
->mid
, add
);
853 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
854 struct switchdev_trans
*trans
,
855 struct net_device
*orig_dev
,
858 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
859 struct mlxsw_sp_bridge_device
*bridge_device
;
861 if (switchdev_trans_ph_prepare(trans
))
864 /* It's possible we failed to enslave the port, yet this
865 * operation is executed due to it being deferred.
867 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
871 if (bridge_device
->mrouter
!= is_mrouter
)
872 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp
, bridge_device
,
874 bridge_device
->mrouter
= is_mrouter
;
878 static int mlxsw_sp_port_attr_set(struct net_device
*dev
,
879 const struct switchdev_attr
*attr
,
880 struct switchdev_trans
*trans
)
882 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
886 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
887 err
= mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port
, trans
,
891 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
892 err
= mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port
, trans
,
894 attr
->u
.brport_flags
);
896 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
897 err
= mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port
, trans
,
898 attr
->u
.ageing_time
);
900 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
901 err
= mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port
, trans
,
903 attr
->u
.vlan_filtering
);
905 case SWITCHDEV_ATTR_ID_PORT_MROUTER
:
906 err
= mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port
, trans
,
910 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
:
911 err
= mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port
, trans
,
913 attr
->u
.mc_disabled
);
915 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER
:
916 err
= mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port
, trans
,
929 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
,
930 struct mlxsw_sp_bridge_port
*bridge_port
)
932 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
933 struct mlxsw_sp_bridge_device
*bridge_device
;
934 u8 local_port
= mlxsw_sp_port
->local_port
;
935 u16 vid
= mlxsw_sp_port_vlan
->vid
;
936 struct mlxsw_sp_fid
*fid
;
939 bridge_device
= bridge_port
->bridge_device
;
940 fid
= bridge_device
->ops
->fid_get(bridge_device
, vid
);
944 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
,
945 bridge_port
->flags
& BR_FLOOD
);
947 goto err_fid_uc_flood_set
;
949 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
,
950 mlxsw_sp_mc_flood(bridge_port
));
952 goto err_fid_mc_flood_set
;
954 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
,
957 goto err_fid_bc_flood_set
;
959 err
= mlxsw_sp_fid_port_vid_map(fid
, mlxsw_sp_port
, vid
);
961 goto err_fid_port_vid_map
;
963 mlxsw_sp_port_vlan
->fid
= fid
;
967 err_fid_port_vid_map
:
968 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
, false);
969 err_fid_bc_flood_set
:
970 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
, false);
971 err_fid_mc_flood_set
:
972 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
, false);
973 err_fid_uc_flood_set
:
974 mlxsw_sp_fid_put(fid
);
979 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
)
981 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
982 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
983 u8 local_port
= mlxsw_sp_port
->local_port
;
984 u16 vid
= mlxsw_sp_port_vlan
->vid
;
986 mlxsw_sp_port_vlan
->fid
= NULL
;
987 mlxsw_sp_fid_port_vid_unmap(fid
, mlxsw_sp_port
, vid
);
988 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
, false);
989 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
, false);
990 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
, false);
991 mlxsw_sp_fid_put(fid
);
995 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port
*mlxsw_sp_port
,
996 u16 vid
, bool is_pvid
)
1000 else if (mlxsw_sp_port
->pvid
== vid
)
1001 return 0; /* Dis-allow untagged packets */
1003 return mlxsw_sp_port
->pvid
;
1007 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
,
1008 struct mlxsw_sp_bridge_port
*bridge_port
)
1010 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
1011 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
1012 u16 vid
= mlxsw_sp_port_vlan
->vid
;
1015 /* No need to continue if only VLAN flags were changed */
1016 if (mlxsw_sp_port_vlan
->bridge_port
)
1019 err
= mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan
, bridge_port
);
1023 err
= mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
,
1024 bridge_port
->flags
& BR_LEARNING
);
1026 goto err_port_vid_learning_set
;
1028 err
= mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
,
1029 bridge_port
->stp_state
);
1031 goto err_port_vid_stp_set
;
1033 bridge_vlan
= mlxsw_sp_bridge_vlan_get(bridge_port
, vid
);
1036 goto err_bridge_vlan_get
;
1039 list_add(&mlxsw_sp_port_vlan
->bridge_vlan_node
,
1040 &bridge_vlan
->port_vlan_list
);
1042 mlxsw_sp_bridge_port_get(mlxsw_sp_port
->mlxsw_sp
->bridge
,
1044 mlxsw_sp_port_vlan
->bridge_port
= bridge_port
;
1048 err_bridge_vlan_get
:
1049 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
, BR_STATE_DISABLED
);
1050 err_port_vid_stp_set
:
1051 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, false);
1052 err_port_vid_learning_set
:
1053 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan
);
1058 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
)
1060 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
1061 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
1062 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
1063 struct mlxsw_sp_bridge_port
*bridge_port
;
1064 u16 vid
= mlxsw_sp_port_vlan
->vid
;
1065 bool last_port
, last_vlan
;
1067 if (WARN_ON(mlxsw_sp_fid_type(fid
) != MLXSW_SP_FID_TYPE_8021Q
&&
1068 mlxsw_sp_fid_type(fid
) != MLXSW_SP_FID_TYPE_8021D
))
1071 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
1072 last_vlan
= list_is_singular(&bridge_port
->vlans_list
);
1073 bridge_vlan
= mlxsw_sp_bridge_vlan_find(bridge_port
, vid
);
1074 last_port
= list_is_singular(&bridge_vlan
->port_vlan_list
);
1076 list_del(&mlxsw_sp_port_vlan
->bridge_vlan_node
);
1077 mlxsw_sp_bridge_vlan_put(bridge_vlan
);
1078 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
, BR_STATE_DISABLED
);
1079 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, false);
1081 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port
->mlxsw_sp
,
1083 mlxsw_sp_fid_index(fid
));
1085 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port
, bridge_port
);
1087 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan
);
1089 mlxsw_sp_bridge_port_put(mlxsw_sp_port
->mlxsw_sp
->bridge
, bridge_port
);
1090 mlxsw_sp_port_vlan
->bridge_port
= NULL
;
1094 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1095 struct mlxsw_sp_bridge_port
*bridge_port
,
1096 u16 vid
, bool is_untagged
, bool is_pvid
)
1098 u16 pvid
= mlxsw_sp_port_pvid_determine(mlxsw_sp_port
, vid
, is_pvid
);
1099 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1100 u16 old_pvid
= mlxsw_sp_port
->pvid
;
1103 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_get(mlxsw_sp_port
, vid
);
1104 if (IS_ERR(mlxsw_sp_port_vlan
))
1105 return PTR_ERR(mlxsw_sp_port_vlan
);
1107 err
= mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, true,
1110 goto err_port_vlan_set
;
1112 err
= mlxsw_sp_port_pvid_set(mlxsw_sp_port
, pvid
);
1114 goto err_port_pvid_set
;
1116 err
= mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan
, bridge_port
);
1118 goto err_port_vlan_bridge_join
;
1122 err_port_vlan_bridge_join
:
1123 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, old_pvid
);
1125 mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, false, false);
1127 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan
);
1131 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1132 const struct switchdev_obj_port_vlan
*vlan
,
1133 struct switchdev_trans
*trans
)
1135 bool flag_untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
1136 bool flag_pvid
= vlan
->flags
& BRIDGE_VLAN_INFO_PVID
;
1137 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1138 struct net_device
*orig_dev
= vlan
->obj
.orig_dev
;
1139 struct mlxsw_sp_bridge_port
*bridge_port
;
1142 if (switchdev_trans_ph_prepare(trans
))
1145 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1146 if (WARN_ON(!bridge_port
))
1149 if (!bridge_port
->bridge_device
->vlan_enabled
)
1152 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
1155 err
= mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port
, bridge_port
,
1165 static enum mlxsw_reg_sfdf_flush_type
mlxsw_sp_fdb_flush_type(bool lagged
)
1167 return lagged
? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID
:
1168 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID
;
1172 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp
*mlxsw_sp
,
1173 struct mlxsw_sp_bridge_port
*bridge_port
,
1176 bool lagged
= bridge_port
->lagged
;
1177 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
1180 system_port
= lagged
? bridge_port
->lag_id
: bridge_port
->system_port
;
1181 mlxsw_reg_sfdf_pack(sfdf_pl
, mlxsw_sp_fdb_flush_type(lagged
));
1182 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid_index
);
1183 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl
, system_port
);
1185 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
1188 static enum mlxsw_reg_sfd_rec_policy
mlxsw_sp_sfd_rec_policy(bool dynamic
)
1190 return dynamic
? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS
:
1191 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY
;
1194 static enum mlxsw_reg_sfd_op
mlxsw_sp_sfd_op(bool adding
)
1196 return adding
? MLXSW_REG_SFD_OP_WRITE_EDIT
:
1197 MLXSW_REG_SFD_OP_WRITE_REMOVE
;
1200 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
1201 const char *mac
, u16 fid
, bool adding
,
1202 enum mlxsw_reg_sfd_rec_action action
,
1209 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1213 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1214 mlxsw_reg_sfd_uc_pack(sfd_pl
, 0, mlxsw_sp_sfd_rec_policy(dynamic
),
1215 mac
, fid
, action
, local_port
);
1216 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1217 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1221 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1229 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
1230 const char *mac
, u16 fid
, bool adding
,
1233 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, local_port
, mac
, fid
, adding
,
1234 MLXSW_REG_SFD_REC_ACTION_NOP
, dynamic
);
1237 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp
*mlxsw_sp
, const char *mac
, u16 fid
,
1240 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, 0, mac
, fid
, adding
,
1241 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER
,
1245 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
,
1246 const char *mac
, u16 fid
, u16 lag_vid
,
1247 bool adding
, bool dynamic
)
1253 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1257 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1258 mlxsw_reg_sfd_uc_lag_pack(sfd_pl
, 0, mlxsw_sp_sfd_rec_policy(dynamic
),
1259 mac
, fid
, MLXSW_REG_SFD_REC_ACTION_NOP
,
1261 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1262 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1266 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1275 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1276 struct switchdev_notifier_fdb_info
*fdb_info
, bool adding
)
1278 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1279 struct net_device
*orig_dev
= fdb_info
->info
.dev
;
1280 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1281 struct mlxsw_sp_bridge_device
*bridge_device
;
1282 struct mlxsw_sp_bridge_port
*bridge_port
;
1285 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1289 bridge_device
= bridge_port
->bridge_device
;
1290 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1293 if (!mlxsw_sp_port_vlan
)
1296 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1297 vid
= mlxsw_sp_port_vlan
->vid
;
1299 if (!bridge_port
->lagged
)
1300 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp
,
1301 bridge_port
->system_port
,
1302 fdb_info
->addr
, fid_index
,
1305 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp
,
1306 bridge_port
->lag_id
,
1307 fdb_info
->addr
, fid_index
,
1308 vid
, adding
, false);
1311 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp
*mlxsw_sp
, const char *addr
,
1312 u16 fid
, u16 mid_idx
, bool adding
)
1318 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1322 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1323 mlxsw_reg_sfd_mc_pack(sfd_pl
, 0, addr
, fid
,
1324 MLXSW_REG_SFD_REC_ACTION_NOP
, mid_idx
);
1325 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1326 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1330 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1338 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp
*mlxsw_sp
, u16 mid_idx
,
1340 bool set_router_port
)
1345 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
1349 mlxsw_reg_smid_pack(smid_pl
, mid_idx
, 0, false);
1350 for (i
= 1; i
< mlxsw_core_max_ports(mlxsw_sp
->core
); i
++) {
1351 if (mlxsw_sp
->ports
[i
])
1352 mlxsw_reg_smid_port_mask_set(smid_pl
, i
, 1);
1355 mlxsw_reg_smid_port_mask_set(smid_pl
,
1356 mlxsw_sp_router_port(mlxsw_sp
), 1);
1358 for_each_set_bit(i
, ports_bitmap
, mlxsw_core_max_ports(mlxsw_sp
->core
))
1359 mlxsw_reg_smid_port_set(smid_pl
, i
, 1);
1361 mlxsw_reg_smid_port_set(smid_pl
, mlxsw_sp_router_port(mlxsw_sp
),
1364 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
1369 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1370 u16 mid_idx
, bool add
)
1372 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1376 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
1380 mlxsw_reg_smid_pack(smid_pl
, mid_idx
, mlxsw_sp_port
->local_port
, add
);
1381 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
1387 mlxsw_sp_mid
*__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device
*bridge_device
,
1388 const unsigned char *addr
,
1391 struct mlxsw_sp_mid
*mid
;
1393 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1394 if (ether_addr_equal(mid
->addr
, addr
) && mid
->fid
== fid
)
1401 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp
*mlxsw_sp
,
1402 struct mlxsw_sp_bridge_port
*bridge_port
,
1403 unsigned long *ports_bitmap
)
1405 struct mlxsw_sp_port
*mlxsw_sp_port
;
1406 u64 max_lag_members
, i
;
1409 if (!bridge_port
->lagged
) {
1410 set_bit(bridge_port
->system_port
, ports_bitmap
);
1412 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1414 lag_id
= bridge_port
->lag_id
;
1415 for (i
= 0; i
< max_lag_members
; i
++) {
1416 mlxsw_sp_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
,
1419 set_bit(mlxsw_sp_port
->local_port
,
1426 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap
,
1427 struct mlxsw_sp_bridge_device
*bridge_device
,
1428 struct mlxsw_sp
*mlxsw_sp
)
1430 struct mlxsw_sp_bridge_port
*bridge_port
;
1432 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
1433 if (bridge_port
->mrouter
) {
1434 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp
,
1442 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp
*mlxsw_sp
,
1443 struct mlxsw_sp_mid
*mid
,
1444 struct mlxsw_sp_bridge_device
*bridge_device
)
1452 mid_idx
= find_first_zero_bit(mlxsw_sp
->bridge
->mids_bitmap
,
1454 if (mid_idx
== MLXSW_SP_MID_MAX
)
1457 num_of_ports
= mlxsw_core_max_ports(mlxsw_sp
->core
);
1458 alloc_size
= sizeof(long) * BITS_TO_LONGS(num_of_ports
);
1459 flood_bitmap
= kzalloc(alloc_size
, GFP_KERNEL
);
1463 bitmap_copy(flood_bitmap
, mid
->ports_in_mid
, num_of_ports
);
1464 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap
, bridge_device
, mlxsw_sp
);
1467 err
= mlxsw_sp_port_smid_full_entry(mlxsw_sp
, mid_idx
, flood_bitmap
,
1468 bridge_device
->mrouter
);
1469 kfree(flood_bitmap
);
1473 err
= mlxsw_sp_port_mdb_op(mlxsw_sp
, mid
->addr
, mid
->fid
, mid_idx
,
1478 set_bit(mid_idx
, mlxsw_sp
->bridge
->mids_bitmap
);
1483 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp
*mlxsw_sp
,
1484 struct mlxsw_sp_mid
*mid
)
1489 clear_bit(mid
->mid
, mlxsw_sp
->bridge
->mids_bitmap
);
1491 return mlxsw_sp_port_mdb_op(mlxsw_sp
, mid
->addr
, mid
->fid
, mid
->mid
,
1496 mlxsw_sp_mid
*__mlxsw_sp_mc_alloc(struct mlxsw_sp
*mlxsw_sp
,
1497 struct mlxsw_sp_bridge_device
*bridge_device
,
1498 const unsigned char *addr
,
1501 struct mlxsw_sp_mid
*mid
;
1504 mid
= kzalloc(sizeof(*mid
), GFP_KERNEL
);
1508 alloc_size
= sizeof(unsigned long) *
1509 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp
->core
));
1511 mid
->ports_in_mid
= kzalloc(alloc_size
, GFP_KERNEL
);
1512 if (!mid
->ports_in_mid
)
1513 goto err_ports_in_mid_alloc
;
1515 ether_addr_copy(mid
->addr
, addr
);
1519 if (!bridge_device
->multicast_enabled
)
1522 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp
, mid
, bridge_device
))
1523 goto err_write_mdb_entry
;
1526 list_add_tail(&mid
->list
, &bridge_device
->mids_list
);
1529 err_write_mdb_entry
:
1530 kfree(mid
->ports_in_mid
);
1531 err_ports_in_mid_alloc
:
1536 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port
*mlxsw_sp_port
,
1537 struct mlxsw_sp_mid
*mid
)
1539 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1542 clear_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
);
1543 if (bitmap_empty(mid
->ports_in_mid
,
1544 mlxsw_core_max_ports(mlxsw_sp
->core
))) {
1545 err
= mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp
, mid
);
1546 list_del(&mid
->list
);
1547 kfree(mid
->ports_in_mid
);
1553 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1554 const struct switchdev_obj_port_mdb
*mdb
,
1555 struct switchdev_trans
*trans
)
1557 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1558 struct net_device
*orig_dev
= mdb
->obj
.orig_dev
;
1559 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1560 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1561 struct mlxsw_sp_bridge_device
*bridge_device
;
1562 struct mlxsw_sp_bridge_port
*bridge_port
;
1563 struct mlxsw_sp_mid
*mid
;
1567 if (switchdev_trans_ph_prepare(trans
))
1570 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1574 bridge_device
= bridge_port
->bridge_device
;
1575 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1578 if (!mlxsw_sp_port_vlan
)
1581 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1583 mid
= __mlxsw_sp_mc_get(bridge_device
, mdb
->addr
, fid_index
);
1585 mid
= __mlxsw_sp_mc_alloc(mlxsw_sp
, bridge_device
, mdb
->addr
,
1588 netdev_err(dev
, "Unable to allocate MC group\n");
1592 set_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
);
1594 if (!bridge_device
->multicast_enabled
)
1597 if (bridge_port
->mrouter
)
1600 err
= mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, true);
1602 netdev_err(dev
, "Unable to set SMID\n");
1609 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port
, mid
);
1614 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port
*mlxsw_sp_port
,
1615 struct mlxsw_sp_bridge_device
1618 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1619 struct mlxsw_sp_mid
*mid
;
1622 mc_enabled
= bridge_device
->multicast_enabled
;
1624 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1626 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp
, mid
,
1629 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp
, mid
);
1634 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port
*mlxsw_sp_port
,
1635 struct mlxsw_sp_bridge_port
*bridge_port
,
1638 struct mlxsw_sp_bridge_device
*bridge_device
;
1639 struct mlxsw_sp_mid
*mid
;
1641 bridge_device
= bridge_port
->bridge_device
;
1643 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1644 if (!test_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
))
1645 mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, add
);
1649 static int mlxsw_sp_port_obj_add(struct net_device
*dev
,
1650 const struct switchdev_obj
*obj
,
1651 struct switchdev_trans
*trans
)
1653 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1657 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1658 err
= mlxsw_sp_port_vlans_add(mlxsw_sp_port
,
1659 SWITCHDEV_OBJ_PORT_VLAN(obj
),
1662 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1663 err
= mlxsw_sp_port_mdb_add(mlxsw_sp_port
,
1664 SWITCHDEV_OBJ_PORT_MDB(obj
),
1676 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1677 struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
1679 u16 pvid
= mlxsw_sp_port
->pvid
== vid
? 0 : vid
;
1680 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1682 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
1683 if (WARN_ON(!mlxsw_sp_port_vlan
))
1686 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan
);
1687 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, pvid
);
1688 mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, false, false);
1689 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan
);
1692 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1693 const struct switchdev_obj_port_vlan
*vlan
)
1695 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1696 struct net_device
*orig_dev
= vlan
->obj
.orig_dev
;
1697 struct mlxsw_sp_bridge_port
*bridge_port
;
1700 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1701 if (WARN_ON(!bridge_port
))
1704 if (!bridge_port
->bridge_device
->vlan_enabled
)
1707 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++)
1708 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port
, bridge_port
, vid
);
1714 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1715 struct mlxsw_sp_bridge_port
*bridge_port
,
1716 struct mlxsw_sp_mid
*mid
)
1718 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1721 if (bridge_port
->bridge_device
->multicast_enabled
) {
1722 if (bridge_port
->bridge_device
->multicast_enabled
) {
1723 err
= mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
,
1726 netdev_err(dev
, "Unable to remove port from SMID\n");
1730 err
= mlxsw_sp_port_remove_from_mid(mlxsw_sp_port
, mid
);
1732 netdev_err(dev
, "Unable to remove MC SFD\n");
1737 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1738 const struct switchdev_obj_port_mdb
*mdb
)
1740 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1741 struct net_device
*orig_dev
= mdb
->obj
.orig_dev
;
1742 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1743 struct mlxsw_sp_bridge_device
*bridge_device
;
1744 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1745 struct mlxsw_sp_bridge_port
*bridge_port
;
1746 struct mlxsw_sp_mid
*mid
;
1749 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1753 bridge_device
= bridge_port
->bridge_device
;
1754 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1757 if (!mlxsw_sp_port_vlan
)
1760 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1762 mid
= __mlxsw_sp_mc_get(bridge_device
, mdb
->addr
, fid_index
);
1764 netdev_err(dev
, "Unable to remove port from MC DB\n");
1768 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port
, bridge_port
, mid
);
1772 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
1773 struct mlxsw_sp_bridge_port
*bridge_port
)
1775 struct mlxsw_sp_bridge_device
*bridge_device
;
1776 struct mlxsw_sp_mid
*mid
, *tmp
;
1778 bridge_device
= bridge_port
->bridge_device
;
1780 list_for_each_entry_safe(mid
, tmp
, &bridge_device
->mids_list
, list
) {
1781 if (test_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
)) {
1782 __mlxsw_sp_port_mdb_del(mlxsw_sp_port
, bridge_port
,
1784 } else if (bridge_device
->multicast_enabled
&&
1785 bridge_port
->mrouter
) {
1786 mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, false);
1791 static int mlxsw_sp_port_obj_del(struct net_device
*dev
,
1792 const struct switchdev_obj
*obj
)
1794 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1798 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1799 err
= mlxsw_sp_port_vlans_del(mlxsw_sp_port
,
1800 SWITCHDEV_OBJ_PORT_VLAN(obj
));
1802 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1803 err
= mlxsw_sp_port_mdb_del(mlxsw_sp_port
,
1804 SWITCHDEV_OBJ_PORT_MDB(obj
));
1814 static struct mlxsw_sp_port
*mlxsw_sp_lag_rep_port(struct mlxsw_sp
*mlxsw_sp
,
1817 struct mlxsw_sp_port
*mlxsw_sp_port
;
1818 u64 max_lag_members
;
1821 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1823 for (i
= 0; i
< max_lag_members
; i
++) {
1824 mlxsw_sp_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
);
1826 return mlxsw_sp_port
;
1831 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops
= {
1832 .switchdev_port_attr_get
= mlxsw_sp_port_attr_get
,
1833 .switchdev_port_attr_set
= mlxsw_sp_port_attr_set
,
1834 .switchdev_port_obj_add
= mlxsw_sp_port_obj_add
,
1835 .switchdev_port_obj_del
= mlxsw_sp_port_obj_del
,
1839 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device
*bridge_device
,
1840 struct mlxsw_sp_bridge_port
*bridge_port
,
1841 struct mlxsw_sp_port
*mlxsw_sp_port
,
1842 struct netlink_ext_ack
*extack
)
1844 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1846 if (is_vlan_dev(bridge_port
->dev
)) {
1847 NL_SET_ERR_MSG(extack
, "spectrum: Can not enslave a VLAN device to a VLAN-aware bridge");
1851 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, 1);
1852 if (WARN_ON(!mlxsw_sp_port_vlan
))
1855 /* Let VLAN-aware bridge take care of its own VLANs */
1856 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan
);
1862 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device
*bridge_device
,
1863 struct mlxsw_sp_bridge_port
*bridge_port
,
1864 struct mlxsw_sp_port
*mlxsw_sp_port
)
1866 mlxsw_sp_port_vlan_get(mlxsw_sp_port
, 1);
1867 /* Make sure untagged frames are allowed to ingress */
1868 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, 1);
1871 static struct mlxsw_sp_fid
*
1872 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device
*bridge_device
,
1875 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
1877 return mlxsw_sp_fid_8021q_get(mlxsw_sp
, vid
);
1880 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops
= {
1881 .port_join
= mlxsw_sp_bridge_8021q_port_join
,
1882 .port_leave
= mlxsw_sp_bridge_8021q_port_leave
,
1883 .fid_get
= mlxsw_sp_bridge_8021q_fid_get
,
1887 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port
*mlxsw_sp_port
,
1888 const struct net_device
*br_dev
)
1890 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1892 list_for_each_entry(mlxsw_sp_port_vlan
, &mlxsw_sp_port
->vlans_list
,
1894 if (mlxsw_sp_port_vlan
->bridge_port
&&
1895 mlxsw_sp_port_vlan
->bridge_port
->bridge_device
->dev
==
1904 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device
*bridge_device
,
1905 struct mlxsw_sp_bridge_port
*bridge_port
,
1906 struct mlxsw_sp_port
*mlxsw_sp_port
,
1907 struct netlink_ext_ack
*extack
)
1909 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1912 if (!is_vlan_dev(bridge_port
->dev
)) {
1913 NL_SET_ERR_MSG(extack
, "spectrum: Only VLAN devices can be enslaved to a VLAN-unaware bridge");
1916 vid
= vlan_dev_vlan_id(bridge_port
->dev
);
1918 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
1919 if (WARN_ON(!mlxsw_sp_port_vlan
))
1922 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port
, bridge_device
->dev
)) {
1923 NL_SET_ERR_MSG(extack
, "spectrum: Can not bridge VLAN uppers of the same port");
1927 /* Port is no longer usable as a router interface */
1928 if (mlxsw_sp_port_vlan
->fid
)
1929 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan
);
1931 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan
, bridge_port
);
1935 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device
*bridge_device
,
1936 struct mlxsw_sp_bridge_port
*bridge_port
,
1937 struct mlxsw_sp_port
*mlxsw_sp_port
)
1939 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1940 u16 vid
= vlan_dev_vlan_id(bridge_port
->dev
);
1942 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
1943 if (WARN_ON(!mlxsw_sp_port_vlan
))
1946 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan
);
1949 static struct mlxsw_sp_fid
*
1950 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device
*bridge_device
,
1953 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
1955 return mlxsw_sp_fid_8021d_get(mlxsw_sp
, bridge_device
->dev
->ifindex
);
1958 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops
= {
1959 .port_join
= mlxsw_sp_bridge_8021d_port_join
,
1960 .port_leave
= mlxsw_sp_bridge_8021d_port_leave
,
1961 .fid_get
= mlxsw_sp_bridge_8021d_fid_get
,
1964 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
1965 struct net_device
*brport_dev
,
1966 struct net_device
*br_dev
,
1967 struct netlink_ext_ack
*extack
)
1969 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1970 struct mlxsw_sp_bridge_device
*bridge_device
;
1971 struct mlxsw_sp_bridge_port
*bridge_port
;
1974 bridge_port
= mlxsw_sp_bridge_port_get(mlxsw_sp
->bridge
, brport_dev
);
1975 if (IS_ERR(bridge_port
))
1976 return PTR_ERR(bridge_port
);
1977 bridge_device
= bridge_port
->bridge_device
;
1979 err
= bridge_device
->ops
->port_join(bridge_device
, bridge_port
,
1980 mlxsw_sp_port
, extack
);
1987 mlxsw_sp_bridge_port_put(mlxsw_sp
->bridge
, bridge_port
);
1991 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_port
,
1992 struct net_device
*brport_dev
,
1993 struct net_device
*br_dev
)
1995 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1996 struct mlxsw_sp_bridge_device
*bridge_device
;
1997 struct mlxsw_sp_bridge_port
*bridge_port
;
1999 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2002 bridge_port
= __mlxsw_sp_bridge_port_find(bridge_device
, brport_dev
);
2006 bridge_device
->ops
->port_leave(bridge_device
, bridge_port
,
2008 mlxsw_sp_bridge_port_put(mlxsw_sp
->bridge
, bridge_port
);
2012 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type
,
2013 const char *mac
, u16 vid
,
2014 struct net_device
*dev
)
2016 struct switchdev_notifier_fdb_info info
;
2020 call_switchdev_notifiers(type
, dev
, &info
.info
);
2023 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp
*mlxsw_sp
,
2024 char *sfn_pl
, int rec_index
,
2027 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2028 struct mlxsw_sp_bridge_device
*bridge_device
;
2029 struct mlxsw_sp_bridge_port
*bridge_port
;
2030 struct mlxsw_sp_port
*mlxsw_sp_port
;
2031 enum switchdev_notifier_type type
;
2035 bool do_notification
= true;
2038 mlxsw_reg_sfn_mac_unpack(sfn_pl
, rec_index
, mac
, &fid
, &local_port
);
2039 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2040 if (!mlxsw_sp_port
) {
2041 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Incorrect local port in FDB notification\n");
2045 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port
, fid
);
2046 if (!mlxsw_sp_port_vlan
) {
2047 netdev_err(mlxsw_sp_port
->dev
, "Failed to find a matching {Port, VID} following FDB notification\n");
2051 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
2053 netdev_err(mlxsw_sp_port
->dev
, "{Port, VID} not associated with a bridge\n");
2057 bridge_device
= bridge_port
->bridge_device
;
2058 vid
= bridge_device
->vlan_enabled
? mlxsw_sp_port_vlan
->vid
: 0;
2061 err
= mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, local_port
, mac
, fid
,
2064 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to set FDB entry\n");
2068 if (!do_notification
)
2070 type
= adding
? SWITCHDEV_FDB_ADD_TO_BRIDGE
: SWITCHDEV_FDB_DEL_TO_BRIDGE
;
2071 mlxsw_sp_fdb_call_notifiers(type
, mac
, vid
, bridge_port
->dev
);
2077 do_notification
= false;
2081 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp
*mlxsw_sp
,
2082 char *sfn_pl
, int rec_index
,
2085 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2086 struct mlxsw_sp_bridge_device
*bridge_device
;
2087 struct mlxsw_sp_bridge_port
*bridge_port
;
2088 struct mlxsw_sp_port
*mlxsw_sp_port
;
2089 enum switchdev_notifier_type type
;
2094 bool do_notification
= true;
2097 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl
, rec_index
, mac
, &fid
, &lag_id
);
2098 mlxsw_sp_port
= mlxsw_sp_lag_rep_port(mlxsw_sp
, lag_id
);
2099 if (!mlxsw_sp_port
) {
2100 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Cannot find port representor for LAG\n");
2104 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port
, fid
);
2105 if (!mlxsw_sp_port_vlan
) {
2106 netdev_err(mlxsw_sp_port
->dev
, "Failed to find a matching {Port, VID} following FDB notification\n");
2110 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
2112 netdev_err(mlxsw_sp_port
->dev
, "{Port, VID} not associated with a bridge\n");
2116 bridge_device
= bridge_port
->bridge_device
;
2117 vid
= bridge_device
->vlan_enabled
? mlxsw_sp_port_vlan
->vid
: 0;
2118 lag_vid
= mlxsw_sp_port_vlan
->vid
;
2121 err
= mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp
, lag_id
, mac
, fid
, lag_vid
,
2124 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to set FDB entry\n");
2128 if (!do_notification
)
2130 type
= adding
? SWITCHDEV_FDB_ADD_TO_BRIDGE
: SWITCHDEV_FDB_DEL_TO_BRIDGE
;
2131 mlxsw_sp_fdb_call_notifiers(type
, mac
, vid
, bridge_port
->dev
);
2137 do_notification
= false;
2141 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp
*mlxsw_sp
,
2142 char *sfn_pl
, int rec_index
)
2144 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl
, rec_index
)) {
2145 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC
:
2146 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp
, sfn_pl
,
2149 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC
:
2150 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp
, sfn_pl
,
2153 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG
:
2154 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp
, sfn_pl
,
2157 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG
:
2158 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp
, sfn_pl
,
2164 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp
*mlxsw_sp
)
2166 struct mlxsw_sp_bridge
*bridge
= mlxsw_sp
->bridge
;
2168 mlxsw_core_schedule_dw(&bridge
->fdb_notify
.dw
,
2169 msecs_to_jiffies(bridge
->fdb_notify
.interval
));
2172 static void mlxsw_sp_fdb_notify_work(struct work_struct
*work
)
2174 struct mlxsw_sp_bridge
*bridge
;
2175 struct mlxsw_sp
*mlxsw_sp
;
2181 sfn_pl
= kmalloc(MLXSW_REG_SFN_LEN
, GFP_KERNEL
);
2185 bridge
= container_of(work
, struct mlxsw_sp_bridge
, fdb_notify
.dw
.work
);
2186 mlxsw_sp
= bridge
->mlxsw_sp
;
2189 mlxsw_reg_sfn_pack(sfn_pl
);
2190 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(sfn
), sfn_pl
);
2192 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to get FDB notifications\n");
2195 num_rec
= mlxsw_reg_sfn_num_rec_get(sfn_pl
);
2196 for (i
= 0; i
< num_rec
; i
++)
2197 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp
, sfn_pl
, i
);
2202 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp
);
2205 struct mlxsw_sp_switchdev_event_work
{
2206 struct work_struct work
;
2207 struct switchdev_notifier_fdb_info fdb_info
;
2208 struct net_device
*dev
;
2209 unsigned long event
;
2212 static void mlxsw_sp_switchdev_event_work(struct work_struct
*work
)
2214 struct mlxsw_sp_switchdev_event_work
*switchdev_work
=
2215 container_of(work
, struct mlxsw_sp_switchdev_event_work
, work
);
2216 struct net_device
*dev
= switchdev_work
->dev
;
2217 struct switchdev_notifier_fdb_info
*fdb_info
;
2218 struct mlxsw_sp_port
*mlxsw_sp_port
;
2222 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(dev
);
2226 switch (switchdev_work
->event
) {
2227 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
2228 fdb_info
= &switchdev_work
->fdb_info
;
2229 err
= mlxsw_sp_port_fdb_set(mlxsw_sp_port
, fdb_info
, true);
2232 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED
,
2234 fdb_info
->vid
, dev
);
2236 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2237 fdb_info
= &switchdev_work
->fdb_info
;
2238 mlxsw_sp_port_fdb_set(mlxsw_sp_port
, fdb_info
, false);
2244 kfree(switchdev_work
->fdb_info
.addr
);
2245 kfree(switchdev_work
);
2249 /* Called under rcu_read_lock() */
2250 static int mlxsw_sp_switchdev_event(struct notifier_block
*unused
,
2251 unsigned long event
, void *ptr
)
2253 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
2254 struct mlxsw_sp_switchdev_event_work
*switchdev_work
;
2255 struct switchdev_notifier_fdb_info
*fdb_info
= ptr
;
2257 if (!mlxsw_sp_port_dev_lower_find_rcu(dev
))
2260 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
2261 if (!switchdev_work
)
2264 INIT_WORK(&switchdev_work
->work
, mlxsw_sp_switchdev_event_work
);
2265 switchdev_work
->dev
= dev
;
2266 switchdev_work
->event
= event
;
2269 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
2270 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2271 memcpy(&switchdev_work
->fdb_info
, ptr
,
2272 sizeof(switchdev_work
->fdb_info
));
2273 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
2274 if (!switchdev_work
->fdb_info
.addr
)
2275 goto err_addr_alloc
;
2276 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
2278 /* Take a reference on the device. This can be either
2279 * upper device containig mlxsw_sp_port or just a
2285 kfree(switchdev_work
);
2289 mlxsw_core_schedule_work(&switchdev_work
->work
);
2294 kfree(switchdev_work
);
2298 static struct notifier_block mlxsw_sp_switchdev_notifier
= {
2299 .notifier_call
= mlxsw_sp_switchdev_event
,
2302 static int mlxsw_sp_fdb_init(struct mlxsw_sp
*mlxsw_sp
)
2304 struct mlxsw_sp_bridge
*bridge
= mlxsw_sp
->bridge
;
2307 err
= mlxsw_sp_ageing_set(mlxsw_sp
, MLXSW_SP_DEFAULT_AGEING_TIME
);
2309 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to set default ageing time\n");
2313 err
= register_switchdev_notifier(&mlxsw_sp_switchdev_notifier
);
2315 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to register switchdev notifier\n");
2319 INIT_DELAYED_WORK(&bridge
->fdb_notify
.dw
, mlxsw_sp_fdb_notify_work
);
2320 bridge
->fdb_notify
.interval
= MLXSW_SP_DEFAULT_LEARNING_INTERVAL
;
2321 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp
);
2325 static void mlxsw_sp_fdb_fini(struct mlxsw_sp
*mlxsw_sp
)
2327 cancel_delayed_work_sync(&mlxsw_sp
->bridge
->fdb_notify
.dw
);
2328 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier
);
2332 int mlxsw_sp_switchdev_init(struct mlxsw_sp
*mlxsw_sp
)
2334 struct mlxsw_sp_bridge
*bridge
;
2336 bridge
= kzalloc(sizeof(*mlxsw_sp
->bridge
), GFP_KERNEL
);
2339 mlxsw_sp
->bridge
= bridge
;
2340 bridge
->mlxsw_sp
= mlxsw_sp
;
2342 INIT_LIST_HEAD(&mlxsw_sp
->bridge
->bridges_list
);
2344 bridge
->bridge_8021q_ops
= &mlxsw_sp_bridge_8021q_ops
;
2345 bridge
->bridge_8021d_ops
= &mlxsw_sp_bridge_8021d_ops
;
2347 return mlxsw_sp_fdb_init(mlxsw_sp
);
2350 void mlxsw_sp_switchdev_fini(struct mlxsw_sp
*mlxsw_sp
)
2352 mlxsw_sp_fdb_fini(mlxsw_sp
);
2353 WARN_ON(!list_empty(&mlxsw_sp
->bridge
->bridges_list
));
2354 kfree(mlxsw_sp
->bridge
);
2357 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
2359 mlxsw_sp_port
->dev
->switchdev_ops
= &mlxsw_sp_port_switchdev_ops
;
2362 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port
*mlxsw_sp_port
)