1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/device.h>
10 #include <linux/skbuff.h>
11 #include <linux/if_vlan.h>
12 #include <linux/if_bridge.h>
13 #include <linux/workqueue.h>
14 #include <linux/jiffies.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <net/switchdev.h>
18 #include <net/vxlan.h>
20 #include "spectrum_span.h"
21 #include "spectrum_switchdev.h"
26 struct mlxsw_sp_bridge_ops
;
28 struct mlxsw_sp_bridge
{
29 struct mlxsw_sp
*mlxsw_sp
;
31 struct delayed_work dw
;
32 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
33 unsigned int interval
; /* ms */
35 #define MLXSW_SP_MIN_AGEING_TIME 10
36 #define MLXSW_SP_MAX_AGEING_TIME 1000000
37 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
39 bool vlan_enabled_exists
;
40 struct list_head bridges_list
;
41 DECLARE_BITMAP(mids_bitmap
, MLXSW_SP_MID_MAX
);
42 const struct mlxsw_sp_bridge_ops
*bridge_8021q_ops
;
43 const struct mlxsw_sp_bridge_ops
*bridge_8021d_ops
;
46 struct mlxsw_sp_bridge_device
{
47 struct net_device
*dev
;
48 struct list_head list
;
49 struct list_head ports_list
;
50 struct list_head mids_list
;
54 const struct mlxsw_sp_bridge_ops
*ops
;
57 struct mlxsw_sp_bridge_port
{
58 struct net_device
*dev
;
59 struct mlxsw_sp_bridge_device
*bridge_device
;
60 struct list_head list
;
61 struct list_head vlans_list
;
62 unsigned int ref_count
;
73 struct mlxsw_sp_bridge_vlan
{
74 struct list_head list
;
75 struct list_head port_vlan_list
;
79 struct mlxsw_sp_bridge_ops
{
80 int (*port_join
)(struct mlxsw_sp_bridge_device
*bridge_device
,
81 struct mlxsw_sp_bridge_port
*bridge_port
,
82 struct mlxsw_sp_port
*mlxsw_sp_port
,
83 struct netlink_ext_ack
*extack
);
84 void (*port_leave
)(struct mlxsw_sp_bridge_device
*bridge_device
,
85 struct mlxsw_sp_bridge_port
*bridge_port
,
86 struct mlxsw_sp_port
*mlxsw_sp_port
);
87 int (*vxlan_join
)(struct mlxsw_sp_bridge_device
*bridge_device
,
88 const struct net_device
*vxlan_dev
, u16 vid
,
89 struct netlink_ext_ack
*extack
);
91 (*fid_get
)(struct mlxsw_sp_bridge_device
*bridge_device
,
92 u16 vid
, struct netlink_ext_ack
*extack
);
94 (*fid_lookup
)(struct mlxsw_sp_bridge_device
*bridge_device
,
96 u16 (*fid_vid
)(struct mlxsw_sp_bridge_device
*bridge_device
,
97 const struct mlxsw_sp_fid
*fid
);
101 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp
*mlxsw_sp
,
102 struct mlxsw_sp_bridge_port
*bridge_port
,
106 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
107 struct mlxsw_sp_bridge_port
*bridge_port
);
110 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port
*mlxsw_sp_port
,
111 struct mlxsw_sp_bridge_device
115 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port
*mlxsw_sp_port
,
116 struct mlxsw_sp_bridge_port
*bridge_port
,
119 static struct mlxsw_sp_bridge_device
*
120 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge
*bridge
,
121 const struct net_device
*br_dev
)
123 struct mlxsw_sp_bridge_device
*bridge_device
;
125 list_for_each_entry(bridge_device
, &bridge
->bridges_list
, list
)
126 if (bridge_device
->dev
== br_dev
)
127 return bridge_device
;
132 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp
*mlxsw_sp
,
133 const struct net_device
*br_dev
)
135 return !!mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
138 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device
*dev
,
141 struct mlxsw_sp
*mlxsw_sp
= data
;
143 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp
, dev
);
147 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp
*mlxsw_sp
,
148 struct net_device
*dev
)
150 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp
, dev
);
151 netdev_walk_all_upper_dev_rcu(dev
,
152 mlxsw_sp_bridge_device_upper_rif_destroy
,
156 static struct mlxsw_sp_bridge_device
*
157 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge
*bridge
,
158 struct net_device
*br_dev
)
160 struct device
*dev
= bridge
->mlxsw_sp
->bus_info
->dev
;
161 struct mlxsw_sp_bridge_device
*bridge_device
;
162 bool vlan_enabled
= br_vlan_enabled(br_dev
);
164 if (vlan_enabled
&& bridge
->vlan_enabled_exists
) {
165 dev_err(dev
, "Only one VLAN-aware bridge is supported\n");
166 return ERR_PTR(-EINVAL
);
169 bridge_device
= kzalloc(sizeof(*bridge_device
), GFP_KERNEL
);
171 return ERR_PTR(-ENOMEM
);
173 bridge_device
->dev
= br_dev
;
174 bridge_device
->vlan_enabled
= vlan_enabled
;
175 bridge_device
->multicast_enabled
= br_multicast_enabled(br_dev
);
176 bridge_device
->mrouter
= br_multicast_router(br_dev
);
177 INIT_LIST_HEAD(&bridge_device
->ports_list
);
179 bridge
->vlan_enabled_exists
= true;
180 bridge_device
->ops
= bridge
->bridge_8021q_ops
;
182 bridge_device
->ops
= bridge
->bridge_8021d_ops
;
184 INIT_LIST_HEAD(&bridge_device
->mids_list
);
185 list_add(&bridge_device
->list
, &bridge
->bridges_list
);
187 return bridge_device
;
191 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge
*bridge
,
192 struct mlxsw_sp_bridge_device
*bridge_device
)
194 mlxsw_sp_bridge_device_rifs_destroy(bridge
->mlxsw_sp
,
196 list_del(&bridge_device
->list
);
197 if (bridge_device
->vlan_enabled
)
198 bridge
->vlan_enabled_exists
= false;
199 WARN_ON(!list_empty(&bridge_device
->ports_list
));
200 WARN_ON(!list_empty(&bridge_device
->mids_list
));
201 kfree(bridge_device
);
204 static struct mlxsw_sp_bridge_device
*
205 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge
*bridge
,
206 struct net_device
*br_dev
)
208 struct mlxsw_sp_bridge_device
*bridge_device
;
210 bridge_device
= mlxsw_sp_bridge_device_find(bridge
, br_dev
);
212 return bridge_device
;
214 return mlxsw_sp_bridge_device_create(bridge
, br_dev
);
218 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge
*bridge
,
219 struct mlxsw_sp_bridge_device
*bridge_device
)
221 if (list_empty(&bridge_device
->ports_list
))
222 mlxsw_sp_bridge_device_destroy(bridge
, bridge_device
);
225 static struct mlxsw_sp_bridge_port
*
226 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device
*bridge_device
,
227 const struct net_device
*brport_dev
)
229 struct mlxsw_sp_bridge_port
*bridge_port
;
231 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
232 if (bridge_port
->dev
== brport_dev
)
239 struct mlxsw_sp_bridge_port
*
240 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge
*bridge
,
241 struct net_device
*brport_dev
)
243 struct net_device
*br_dev
= netdev_master_upper_dev_get(brport_dev
);
244 struct mlxsw_sp_bridge_device
*bridge_device
;
249 bridge_device
= mlxsw_sp_bridge_device_find(bridge
, br_dev
);
253 return __mlxsw_sp_bridge_port_find(bridge_device
, brport_dev
);
256 static struct mlxsw_sp_bridge_port
*
257 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device
*bridge_device
,
258 struct net_device
*brport_dev
)
260 struct mlxsw_sp_bridge_port
*bridge_port
;
261 struct mlxsw_sp_port
*mlxsw_sp_port
;
263 bridge_port
= kzalloc(sizeof(*bridge_port
), GFP_KERNEL
);
267 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(brport_dev
);
268 bridge_port
->lagged
= mlxsw_sp_port
->lagged
;
269 if (bridge_port
->lagged
)
270 bridge_port
->lag_id
= mlxsw_sp_port
->lag_id
;
272 bridge_port
->system_port
= mlxsw_sp_port
->local_port
;
273 bridge_port
->dev
= brport_dev
;
274 bridge_port
->bridge_device
= bridge_device
;
275 bridge_port
->stp_state
= BR_STATE_DISABLED
;
276 bridge_port
->flags
= BR_LEARNING
| BR_FLOOD
| BR_LEARNING_SYNC
|
278 INIT_LIST_HEAD(&bridge_port
->vlans_list
);
279 list_add(&bridge_port
->list
, &bridge_device
->ports_list
);
280 bridge_port
->ref_count
= 1;
286 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port
*bridge_port
)
288 list_del(&bridge_port
->list
);
289 WARN_ON(!list_empty(&bridge_port
->vlans_list
));
293 static struct mlxsw_sp_bridge_port
*
294 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge
*bridge
,
295 struct net_device
*brport_dev
)
297 struct net_device
*br_dev
= netdev_master_upper_dev_get(brport_dev
);
298 struct mlxsw_sp_bridge_device
*bridge_device
;
299 struct mlxsw_sp_bridge_port
*bridge_port
;
302 bridge_port
= mlxsw_sp_bridge_port_find(bridge
, brport_dev
);
304 bridge_port
->ref_count
++;
308 bridge_device
= mlxsw_sp_bridge_device_get(bridge
, br_dev
);
309 if (IS_ERR(bridge_device
))
310 return ERR_CAST(bridge_device
);
312 bridge_port
= mlxsw_sp_bridge_port_create(bridge_device
, brport_dev
);
315 goto err_bridge_port_create
;
320 err_bridge_port_create
:
321 mlxsw_sp_bridge_device_put(bridge
, bridge_device
);
325 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge
*bridge
,
326 struct mlxsw_sp_bridge_port
*bridge_port
)
328 struct mlxsw_sp_bridge_device
*bridge_device
;
330 if (--bridge_port
->ref_count
!= 0)
332 bridge_device
= bridge_port
->bridge_device
;
333 mlxsw_sp_bridge_port_destroy(bridge_port
);
334 mlxsw_sp_bridge_device_put(bridge
, bridge_device
);
337 static struct mlxsw_sp_port_vlan
*
338 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port
*mlxsw_sp_port
,
339 const struct mlxsw_sp_bridge_device
*
343 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
345 list_for_each_entry(mlxsw_sp_port_vlan
, &mlxsw_sp_port
->vlans_list
,
347 if (!mlxsw_sp_port_vlan
->bridge_port
)
349 if (mlxsw_sp_port_vlan
->bridge_port
->bridge_device
!=
352 if (bridge_device
->vlan_enabled
&&
353 mlxsw_sp_port_vlan
->vid
!= vid
)
355 return mlxsw_sp_port_vlan
;
361 static struct mlxsw_sp_port_vlan
*
362 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port
*mlxsw_sp_port
,
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 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
371 if (fid
&& mlxsw_sp_fid_index(fid
) == fid_index
)
372 return mlxsw_sp_port_vlan
;
378 static struct mlxsw_sp_bridge_vlan
*
379 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port
*bridge_port
,
382 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
384 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
385 if (bridge_vlan
->vid
== vid
)
392 static struct mlxsw_sp_bridge_vlan
*
393 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
395 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
397 bridge_vlan
= kzalloc(sizeof(*bridge_vlan
), GFP_KERNEL
);
401 INIT_LIST_HEAD(&bridge_vlan
->port_vlan_list
);
402 bridge_vlan
->vid
= vid
;
403 list_add(&bridge_vlan
->list
, &bridge_port
->vlans_list
);
409 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan
*bridge_vlan
)
411 list_del(&bridge_vlan
->list
);
412 WARN_ON(!list_empty(&bridge_vlan
->port_vlan_list
));
416 static struct mlxsw_sp_bridge_vlan
*
417 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
419 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
421 bridge_vlan
= mlxsw_sp_bridge_vlan_find(bridge_port
, vid
);
425 return mlxsw_sp_bridge_vlan_create(bridge_port
, vid
);
428 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan
*bridge_vlan
)
430 if (list_empty(&bridge_vlan
->port_vlan_list
))
431 mlxsw_sp_bridge_vlan_destroy(bridge_vlan
);
434 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge
*bridge
,
435 struct net_device
*dev
,
436 unsigned long *brport_flags
)
438 struct mlxsw_sp_bridge_port
*bridge_port
;
440 bridge_port
= mlxsw_sp_bridge_port_find(bridge
, dev
);
441 if (WARN_ON(!bridge_port
))
444 memcpy(brport_flags
, &bridge_port
->flags
, sizeof(*brport_flags
));
447 static int mlxsw_sp_port_attr_get(struct net_device
*dev
,
448 struct switchdev_attr
*attr
)
450 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
451 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
454 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
455 attr
->u
.ppid
.id_len
= sizeof(mlxsw_sp
->base_mac
);
456 memcpy(&attr
->u
.ppid
.id
, &mlxsw_sp
->base_mac
,
457 attr
->u
.ppid
.id_len
);
459 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
460 mlxsw_sp_port_bridge_flags_get(mlxsw_sp
->bridge
, attr
->orig_dev
,
461 &attr
->u
.brport_flags
);
463 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
464 attr
->u
.brport_flags_support
= BR_LEARNING
| BR_FLOOD
|
475 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
476 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
479 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
481 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
483 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
485 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
,
486 bridge_vlan
->vid
, state
);
492 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
493 struct switchdev_trans
*trans
,
494 struct net_device
*orig_dev
,
497 struct mlxsw_sp_bridge_port
*bridge_port
;
498 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
501 if (switchdev_trans_ph_prepare(trans
))
504 /* It's possible we failed to enslave the port, yet this
505 * operation is executed due to it being deferred.
507 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
512 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
513 err
= mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port
,
516 goto err_port_bridge_vlan_stp_set
;
519 bridge_port
->stp_state
= state
;
523 err_port_bridge_vlan_stp_set
:
524 list_for_each_entry_continue_reverse(bridge_vlan
,
525 &bridge_port
->vlans_list
, list
)
526 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port
, bridge_vlan
,
527 bridge_port
->stp_state
);
532 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
533 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
534 enum mlxsw_sp_flood_type packet_type
,
537 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
539 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
541 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
543 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan
->fid
,
545 mlxsw_sp_port
->local_port
,
553 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
554 struct mlxsw_sp_bridge_port
*bridge_port
,
555 enum mlxsw_sp_flood_type packet_type
,
558 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
561 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
562 err
= mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port
,
567 goto err_port_bridge_vlan_flood_set
;
572 err_port_bridge_vlan_flood_set
:
573 list_for_each_entry_continue_reverse(bridge_vlan
,
574 &bridge_port
->vlans_list
, list
)
575 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port
, bridge_vlan
,
576 packet_type
, !member
);
581 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
582 struct mlxsw_sp_bridge_vlan
*bridge_vlan
,
585 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
586 u16 vid
= bridge_vlan
->vid
;
588 list_for_each_entry(mlxsw_sp_port_vlan
, &bridge_vlan
->port_vlan_list
,
590 if (mlxsw_sp_port_vlan
->mlxsw_sp_port
!= mlxsw_sp_port
)
592 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, set
);
599 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
600 struct mlxsw_sp_bridge_port
*bridge_port
,
603 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
606 list_for_each_entry(bridge_vlan
, &bridge_port
->vlans_list
, list
) {
607 err
= mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port
,
610 goto err_port_bridge_vlan_learning_set
;
615 err_port_bridge_vlan_learning_set
:
616 list_for_each_entry_continue_reverse(bridge_vlan
,
617 &bridge_port
->vlans_list
, list
)
618 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port
,
623 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
624 struct switchdev_trans
*trans
,
625 struct net_device
*orig_dev
,
626 unsigned long brport_flags
)
628 struct mlxsw_sp_bridge_port
*bridge_port
;
631 if (switchdev_trans_ph_prepare(trans
))
634 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
639 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
640 MLXSW_SP_FLOOD_TYPE_UC
,
641 brport_flags
& BR_FLOOD
);
645 err
= mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port
, bridge_port
,
646 brport_flags
& BR_LEARNING
);
650 if (bridge_port
->bridge_device
->multicast_enabled
)
653 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
654 MLXSW_SP_FLOOD_TYPE_MC
,
661 memcpy(&bridge_port
->flags
, &brport_flags
, sizeof(brport_flags
));
665 static int mlxsw_sp_ageing_set(struct mlxsw_sp
*mlxsw_sp
, u32 ageing_time
)
667 char sfdat_pl
[MLXSW_REG_SFDAT_LEN
];
670 mlxsw_reg_sfdat_pack(sfdat_pl
, ageing_time
);
671 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdat
), sfdat_pl
);
674 mlxsw_sp
->bridge
->ageing_time
= ageing_time
;
678 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
679 struct switchdev_trans
*trans
,
680 unsigned long ageing_clock_t
)
682 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
683 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock_t
);
684 u32 ageing_time
= jiffies_to_msecs(ageing_jiffies
) / 1000;
686 if (switchdev_trans_ph_prepare(trans
)) {
687 if (ageing_time
< MLXSW_SP_MIN_AGEING_TIME
||
688 ageing_time
> MLXSW_SP_MAX_AGEING_TIME
)
694 return mlxsw_sp_ageing_set(mlxsw_sp
, ageing_time
);
697 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
698 struct switchdev_trans
*trans
,
699 struct net_device
*orig_dev
,
702 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
703 struct mlxsw_sp_bridge_device
*bridge_device
;
705 if (!switchdev_trans_ph_prepare(trans
))
708 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
709 if (WARN_ON(!bridge_device
))
712 if (bridge_device
->vlan_enabled
== vlan_enabled
)
715 netdev_err(bridge_device
->dev
, "VLAN filtering can't be changed for existing bridge\n");
719 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
720 struct switchdev_trans
*trans
,
721 struct net_device
*orig_dev
,
722 bool is_port_mrouter
)
724 struct mlxsw_sp_bridge_port
*bridge_port
;
727 if (switchdev_trans_ph_prepare(trans
))
730 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp_port
->mlxsw_sp
->bridge
,
735 if (!bridge_port
->bridge_device
->multicast_enabled
)
738 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
, bridge_port
,
739 MLXSW_SP_FLOOD_TYPE_MC
,
744 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port
, bridge_port
,
747 bridge_port
->mrouter
= is_port_mrouter
;
751 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port
*bridge_port
)
753 const struct mlxsw_sp_bridge_device
*bridge_device
;
755 bridge_device
= bridge_port
->bridge_device
;
756 return bridge_device
->multicast_enabled
? bridge_port
->mrouter
:
757 bridge_port
->flags
& BR_MCAST_FLOOD
;
760 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
761 struct switchdev_trans
*trans
,
762 struct net_device
*orig_dev
,
765 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
766 struct mlxsw_sp_bridge_device
*bridge_device
;
767 struct mlxsw_sp_bridge_port
*bridge_port
;
770 if (switchdev_trans_ph_prepare(trans
))
773 /* It's possible we failed to enslave the port, yet this
774 * operation is executed due to it being deferred.
776 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
780 if (bridge_device
->multicast_enabled
!= !mc_disabled
) {
781 bridge_device
->multicast_enabled
= !mc_disabled
;
782 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port
,
786 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
787 enum mlxsw_sp_flood_type packet_type
= MLXSW_SP_FLOOD_TYPE_MC
;
788 bool member
= mlxsw_sp_mc_flood(bridge_port
);
790 err
= mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port
,
792 packet_type
, member
);
797 bridge_device
->multicast_enabled
= !mc_disabled
;
802 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp
*mlxsw_sp
,
803 u16 mid_idx
, bool add
)
808 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
812 mlxsw_reg_smid_pack(smid_pl
, mid_idx
,
813 mlxsw_sp_router_port(mlxsw_sp
), add
);
814 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
820 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp
*mlxsw_sp
,
821 struct mlxsw_sp_bridge_device
*bridge_device
,
824 struct mlxsw_sp_mid
*mid
;
826 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
)
827 mlxsw_sp_smid_router_port_set(mlxsw_sp
, mid
->mid
, add
);
831 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
832 struct switchdev_trans
*trans
,
833 struct net_device
*orig_dev
,
836 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
837 struct mlxsw_sp_bridge_device
*bridge_device
;
839 if (switchdev_trans_ph_prepare(trans
))
842 /* It's possible we failed to enslave the port, yet this
843 * operation is executed due to it being deferred.
845 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, orig_dev
);
849 if (bridge_device
->mrouter
!= is_mrouter
)
850 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp
, bridge_device
,
852 bridge_device
->mrouter
= is_mrouter
;
856 static int mlxsw_sp_port_attr_set(struct net_device
*dev
,
857 const struct switchdev_attr
*attr
,
858 struct switchdev_trans
*trans
)
860 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
864 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
865 err
= mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port
, trans
,
869 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
870 err
= mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port
, trans
,
872 attr
->u
.brport_flags
);
874 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
875 err
= mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port
, trans
,
876 attr
->u
.ageing_time
);
878 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
879 err
= mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port
, trans
,
881 attr
->u
.vlan_filtering
);
883 case SWITCHDEV_ATTR_ID_PORT_MROUTER
:
884 err
= mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port
, trans
,
888 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
:
889 err
= mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port
, trans
,
891 attr
->u
.mc_disabled
);
893 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER
:
894 err
= mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port
, trans
,
903 if (switchdev_trans_ph_commit(trans
))
904 mlxsw_sp_span_respin(mlxsw_sp_port
->mlxsw_sp
);
910 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
,
911 struct mlxsw_sp_bridge_port
*bridge_port
,
912 struct netlink_ext_ack
*extack
)
914 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
915 struct mlxsw_sp_bridge_device
*bridge_device
;
916 u8 local_port
= mlxsw_sp_port
->local_port
;
917 u16 vid
= mlxsw_sp_port_vlan
->vid
;
918 struct mlxsw_sp_fid
*fid
;
921 bridge_device
= bridge_port
->bridge_device
;
922 fid
= bridge_device
->ops
->fid_get(bridge_device
, vid
, extack
);
926 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
,
927 bridge_port
->flags
& BR_FLOOD
);
929 goto err_fid_uc_flood_set
;
931 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
,
932 mlxsw_sp_mc_flood(bridge_port
));
934 goto err_fid_mc_flood_set
;
936 err
= mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
,
939 goto err_fid_bc_flood_set
;
941 err
= mlxsw_sp_fid_port_vid_map(fid
, mlxsw_sp_port
, vid
);
943 goto err_fid_port_vid_map
;
945 mlxsw_sp_port_vlan
->fid
= fid
;
949 err_fid_port_vid_map
:
950 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
, false);
951 err_fid_bc_flood_set
:
952 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
, false);
953 err_fid_mc_flood_set
:
954 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
, false);
955 err_fid_uc_flood_set
:
956 mlxsw_sp_fid_put(fid
);
961 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
)
963 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
964 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
965 u8 local_port
= mlxsw_sp_port
->local_port
;
966 u16 vid
= mlxsw_sp_port_vlan
->vid
;
968 mlxsw_sp_port_vlan
->fid
= NULL
;
969 mlxsw_sp_fid_port_vid_unmap(fid
, mlxsw_sp_port
, vid
);
970 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_BC
, local_port
, false);
971 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_MC
, local_port
, false);
972 mlxsw_sp_fid_flood_set(fid
, MLXSW_SP_FLOOD_TYPE_UC
, local_port
, false);
973 mlxsw_sp_fid_put(fid
);
977 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port
*mlxsw_sp_port
,
978 u16 vid
, bool is_pvid
)
982 else if (mlxsw_sp_port
->pvid
== vid
)
983 return 0; /* Dis-allow untagged packets */
985 return mlxsw_sp_port
->pvid
;
989 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
,
990 struct mlxsw_sp_bridge_port
*bridge_port
,
991 struct netlink_ext_ack
*extack
)
993 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
994 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
995 u16 vid
= mlxsw_sp_port_vlan
->vid
;
998 /* No need to continue if only VLAN flags were changed */
999 if (mlxsw_sp_port_vlan
->bridge_port
)
1002 err
= mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan
, bridge_port
,
1007 err
= mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
,
1008 bridge_port
->flags
& BR_LEARNING
);
1010 goto err_port_vid_learning_set
;
1012 err
= mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
,
1013 bridge_port
->stp_state
);
1015 goto err_port_vid_stp_set
;
1017 bridge_vlan
= mlxsw_sp_bridge_vlan_get(bridge_port
, vid
);
1020 goto err_bridge_vlan_get
;
1023 list_add(&mlxsw_sp_port_vlan
->bridge_vlan_node
,
1024 &bridge_vlan
->port_vlan_list
);
1026 mlxsw_sp_bridge_port_get(mlxsw_sp_port
->mlxsw_sp
->bridge
,
1028 mlxsw_sp_port_vlan
->bridge_port
= bridge_port
;
1032 err_bridge_vlan_get
:
1033 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
, BR_STATE_DISABLED
);
1034 err_port_vid_stp_set
:
1035 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, false);
1036 err_port_vid_learning_set
:
1037 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan
);
1042 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
)
1044 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp_port_vlan
->mlxsw_sp_port
;
1045 struct mlxsw_sp_fid
*fid
= mlxsw_sp_port_vlan
->fid
;
1046 struct mlxsw_sp_bridge_vlan
*bridge_vlan
;
1047 struct mlxsw_sp_bridge_port
*bridge_port
;
1048 u16 vid
= mlxsw_sp_port_vlan
->vid
;
1049 bool last_port
, last_vlan
;
1051 if (WARN_ON(mlxsw_sp_fid_type(fid
) != MLXSW_SP_FID_TYPE_8021Q
&&
1052 mlxsw_sp_fid_type(fid
) != MLXSW_SP_FID_TYPE_8021D
))
1055 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
1056 last_vlan
= list_is_singular(&bridge_port
->vlans_list
);
1057 bridge_vlan
= mlxsw_sp_bridge_vlan_find(bridge_port
, vid
);
1058 last_port
= list_is_singular(&bridge_vlan
->port_vlan_list
);
1060 list_del(&mlxsw_sp_port_vlan
->bridge_vlan_node
);
1061 mlxsw_sp_bridge_vlan_put(bridge_vlan
);
1062 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port
, vid
, BR_STATE_DISABLED
);
1063 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, false);
1065 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port
->mlxsw_sp
,
1067 mlxsw_sp_fid_index(fid
));
1069 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port
, bridge_port
);
1071 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan
);
1073 mlxsw_sp_bridge_port_put(mlxsw_sp_port
->mlxsw_sp
->bridge
, bridge_port
);
1074 mlxsw_sp_port_vlan
->bridge_port
= NULL
;
1078 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1079 struct mlxsw_sp_bridge_port
*bridge_port
,
1080 u16 vid
, bool is_untagged
, bool is_pvid
,
1081 struct netlink_ext_ack
*extack
)
1083 u16 pvid
= mlxsw_sp_port_pvid_determine(mlxsw_sp_port
, vid
, is_pvid
);
1084 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1085 u16 old_pvid
= mlxsw_sp_port
->pvid
;
1088 /* The only valid scenario in which a port-vlan already exists, is if
1089 * the VLAN flags were changed and the port-vlan is associated with the
1090 * correct bridge port
1092 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
1093 if (mlxsw_sp_port_vlan
&&
1094 mlxsw_sp_port_vlan
->bridge_port
!= bridge_port
)
1097 if (!mlxsw_sp_port_vlan
) {
1098 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_create(mlxsw_sp_port
,
1100 if (IS_ERR(mlxsw_sp_port_vlan
))
1101 return PTR_ERR(mlxsw_sp_port_vlan
);
1104 err
= mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, true,
1107 goto err_port_vlan_set
;
1109 err
= mlxsw_sp_port_pvid_set(mlxsw_sp_port
, pvid
);
1111 goto err_port_pvid_set
;
1113 err
= mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan
, bridge_port
,
1116 goto err_port_vlan_bridge_join
;
1120 err_port_vlan_bridge_join
:
1121 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, old_pvid
);
1123 mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, false, false);
1125 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan
);
1130 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp
*mlxsw_sp
,
1131 const struct net_device
*br_dev
,
1132 const struct switchdev_obj_port_vlan
*vlan
)
1134 struct mlxsw_sp_rif
*rif
;
1135 struct mlxsw_sp_fid
*fid
;
1139 rif
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, br_dev
);
1142 fid
= mlxsw_sp_rif_fid(rif
);
1143 pvid
= mlxsw_sp_fid_8021q_vid(fid
);
1145 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; ++vid
) {
1146 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
) {
1148 netdev_err(br_dev
, "Can't change PVID, it's used by router interface\n");
1153 netdev_err(br_dev
, "Can't remove PVID, it's used by router interface\n");
1162 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1163 const struct switchdev_obj_port_vlan
*vlan
,
1164 struct switchdev_trans
*trans
,
1165 struct netlink_ext_ack
*extack
)
1167 bool flag_untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
1168 bool flag_pvid
= vlan
->flags
& BRIDGE_VLAN_INFO_PVID
;
1169 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1170 struct net_device
*orig_dev
= vlan
->obj
.orig_dev
;
1171 struct mlxsw_sp_bridge_port
*bridge_port
;
1174 if (netif_is_bridge_master(orig_dev
)) {
1177 if ((vlan
->flags
& BRIDGE_VLAN_INFO_BRENTRY
) &&
1178 br_vlan_enabled(orig_dev
) &&
1179 switchdev_trans_ph_prepare(trans
))
1180 err
= mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp
,
1187 if (switchdev_trans_ph_commit(trans
))
1190 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1191 if (WARN_ON(!bridge_port
))
1194 if (!bridge_port
->bridge_device
->vlan_enabled
)
1197 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
1200 err
= mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port
, bridge_port
,
1210 static enum mlxsw_reg_sfdf_flush_type
mlxsw_sp_fdb_flush_type(bool lagged
)
1212 return lagged
? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID
:
1213 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID
;
1217 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp
*mlxsw_sp
,
1218 struct mlxsw_sp_bridge_port
*bridge_port
,
1221 bool lagged
= bridge_port
->lagged
;
1222 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
1225 system_port
= lagged
? bridge_port
->lag_id
: bridge_port
->system_port
;
1226 mlxsw_reg_sfdf_pack(sfdf_pl
, mlxsw_sp_fdb_flush_type(lagged
));
1227 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid_index
);
1228 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl
, system_port
);
1230 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
1233 static enum mlxsw_reg_sfd_rec_policy
mlxsw_sp_sfd_rec_policy(bool dynamic
)
1235 return dynamic
? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS
:
1236 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY
;
1239 static enum mlxsw_reg_sfd_op
mlxsw_sp_sfd_op(bool adding
)
1241 return adding
? MLXSW_REG_SFD_OP_WRITE_EDIT
:
1242 MLXSW_REG_SFD_OP_WRITE_REMOVE
;
1245 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp
*mlxsw_sp
,
1246 const char *mac
, u16 fid
,
1247 enum mlxsw_sp_l3proto proto
,
1248 const union mlxsw_sp_l3addr
*addr
,
1249 bool adding
, bool dynamic
)
1251 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto
;
1258 case MLXSW_SP_L3_PROTO_IPV4
:
1259 uip
= be32_to_cpu(addr
->addr4
);
1260 sfd_proto
= MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4
;
1262 case MLXSW_SP_L3_PROTO_IPV6
: /* fall through */
1268 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1272 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1273 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl
, 0,
1274 mlxsw_sp_sfd_rec_policy(dynamic
), mac
, fid
,
1275 MLXSW_REG_SFD_REC_ACTION_NOP
, uip
,
1277 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1278 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1282 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1290 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
1291 const char *mac
, u16 fid
, bool adding
,
1292 enum mlxsw_reg_sfd_rec_action action
,
1299 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1303 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1304 mlxsw_reg_sfd_uc_pack(sfd_pl
, 0, mlxsw_sp_sfd_rec_policy(dynamic
),
1305 mac
, fid
, action
, local_port
);
1306 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1307 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1311 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1319 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
1320 const char *mac
, u16 fid
, bool adding
,
1323 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, local_port
, mac
, fid
, adding
,
1324 MLXSW_REG_SFD_REC_ACTION_NOP
, dynamic
);
1327 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp
*mlxsw_sp
, const char *mac
, u16 fid
,
1330 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, 0, mac
, fid
, adding
,
1331 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER
,
1335 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
,
1336 const char *mac
, u16 fid
, u16 lag_vid
,
1337 bool adding
, bool dynamic
)
1343 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1347 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1348 mlxsw_reg_sfd_uc_lag_pack(sfd_pl
, 0, mlxsw_sp_sfd_rec_policy(dynamic
),
1349 mac
, fid
, MLXSW_REG_SFD_REC_ACTION_NOP
,
1351 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1352 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1356 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1365 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1366 struct switchdev_notifier_fdb_info
*fdb_info
, bool adding
)
1368 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1369 struct net_device
*orig_dev
= fdb_info
->info
.dev
;
1370 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1371 struct mlxsw_sp_bridge_device
*bridge_device
;
1372 struct mlxsw_sp_bridge_port
*bridge_port
;
1375 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1379 bridge_device
= bridge_port
->bridge_device
;
1380 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1383 if (!mlxsw_sp_port_vlan
)
1386 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1387 vid
= mlxsw_sp_port_vlan
->vid
;
1389 if (!bridge_port
->lagged
)
1390 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp
,
1391 bridge_port
->system_port
,
1392 fdb_info
->addr
, fid_index
,
1395 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp
,
1396 bridge_port
->lag_id
,
1397 fdb_info
->addr
, fid_index
,
1398 vid
, adding
, false);
1401 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp
*mlxsw_sp
, const char *addr
,
1402 u16 fid
, u16 mid_idx
, bool adding
)
1408 sfd_pl
= kmalloc(MLXSW_REG_SFD_LEN
, GFP_KERNEL
);
1412 mlxsw_reg_sfd_pack(sfd_pl
, mlxsw_sp_sfd_op(adding
), 0);
1413 mlxsw_reg_sfd_mc_pack(sfd_pl
, 0, addr
, fid
,
1414 MLXSW_REG_SFD_REC_ACTION_NOP
, mid_idx
);
1415 num_rec
= mlxsw_reg_sfd_num_rec_get(sfd_pl
);
1416 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfd
), sfd_pl
);
1420 if (num_rec
!= mlxsw_reg_sfd_num_rec_get(sfd_pl
))
1428 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp
*mlxsw_sp
, u16 mid_idx
,
1430 bool set_router_port
)
1435 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
1439 mlxsw_reg_smid_pack(smid_pl
, mid_idx
, 0, false);
1440 for (i
= 1; i
< mlxsw_core_max_ports(mlxsw_sp
->core
); i
++) {
1441 if (mlxsw_sp
->ports
[i
])
1442 mlxsw_reg_smid_port_mask_set(smid_pl
, i
, 1);
1445 mlxsw_reg_smid_port_mask_set(smid_pl
,
1446 mlxsw_sp_router_port(mlxsw_sp
), 1);
1448 for_each_set_bit(i
, ports_bitmap
, mlxsw_core_max_ports(mlxsw_sp
->core
))
1449 mlxsw_reg_smid_port_set(smid_pl
, i
, 1);
1451 mlxsw_reg_smid_port_set(smid_pl
, mlxsw_sp_router_port(mlxsw_sp
),
1454 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
1459 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1460 u16 mid_idx
, bool add
)
1462 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1466 smid_pl
= kmalloc(MLXSW_REG_SMID_LEN
, GFP_KERNEL
);
1470 mlxsw_reg_smid_pack(smid_pl
, mid_idx
, mlxsw_sp_port
->local_port
, add
);
1471 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(smid
), smid_pl
);
1477 mlxsw_sp_mid
*__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device
*bridge_device
,
1478 const unsigned char *addr
,
1481 struct mlxsw_sp_mid
*mid
;
1483 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1484 if (ether_addr_equal(mid
->addr
, addr
) && mid
->fid
== fid
)
1491 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp
*mlxsw_sp
,
1492 struct mlxsw_sp_bridge_port
*bridge_port
,
1493 unsigned long *ports_bitmap
)
1495 struct mlxsw_sp_port
*mlxsw_sp_port
;
1496 u64 max_lag_members
, i
;
1499 if (!bridge_port
->lagged
) {
1500 set_bit(bridge_port
->system_port
, ports_bitmap
);
1502 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1504 lag_id
= bridge_port
->lag_id
;
1505 for (i
= 0; i
< max_lag_members
; i
++) {
1506 mlxsw_sp_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
,
1509 set_bit(mlxsw_sp_port
->local_port
,
1516 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap
,
1517 struct mlxsw_sp_bridge_device
*bridge_device
,
1518 struct mlxsw_sp
*mlxsw_sp
)
1520 struct mlxsw_sp_bridge_port
*bridge_port
;
1522 list_for_each_entry(bridge_port
, &bridge_device
->ports_list
, list
) {
1523 if (bridge_port
->mrouter
) {
1524 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp
,
1532 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp
*mlxsw_sp
,
1533 struct mlxsw_sp_mid
*mid
,
1534 struct mlxsw_sp_bridge_device
*bridge_device
)
1542 mid_idx
= find_first_zero_bit(mlxsw_sp
->bridge
->mids_bitmap
,
1544 if (mid_idx
== MLXSW_SP_MID_MAX
)
1547 num_of_ports
= mlxsw_core_max_ports(mlxsw_sp
->core
);
1548 alloc_size
= sizeof(long) * BITS_TO_LONGS(num_of_ports
);
1549 flood_bitmap
= kzalloc(alloc_size
, GFP_KERNEL
);
1553 bitmap_copy(flood_bitmap
, mid
->ports_in_mid
, num_of_ports
);
1554 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap
, bridge_device
, mlxsw_sp
);
1557 err
= mlxsw_sp_port_smid_full_entry(mlxsw_sp
, mid_idx
, flood_bitmap
,
1558 bridge_device
->mrouter
);
1559 kfree(flood_bitmap
);
1563 err
= mlxsw_sp_port_mdb_op(mlxsw_sp
, mid
->addr
, mid
->fid
, mid_idx
,
1568 set_bit(mid_idx
, mlxsw_sp
->bridge
->mids_bitmap
);
1573 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp
*mlxsw_sp
,
1574 struct mlxsw_sp_mid
*mid
)
1579 clear_bit(mid
->mid
, mlxsw_sp
->bridge
->mids_bitmap
);
1581 return mlxsw_sp_port_mdb_op(mlxsw_sp
, mid
->addr
, mid
->fid
, mid
->mid
,
1586 mlxsw_sp_mid
*__mlxsw_sp_mc_alloc(struct mlxsw_sp
*mlxsw_sp
,
1587 struct mlxsw_sp_bridge_device
*bridge_device
,
1588 const unsigned char *addr
,
1591 struct mlxsw_sp_mid
*mid
;
1594 mid
= kzalloc(sizeof(*mid
), GFP_KERNEL
);
1598 alloc_size
= sizeof(unsigned long) *
1599 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp
->core
));
1601 mid
->ports_in_mid
= kzalloc(alloc_size
, GFP_KERNEL
);
1602 if (!mid
->ports_in_mid
)
1603 goto err_ports_in_mid_alloc
;
1605 ether_addr_copy(mid
->addr
, addr
);
1609 if (!bridge_device
->multicast_enabled
)
1612 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp
, mid
, bridge_device
))
1613 goto err_write_mdb_entry
;
1616 list_add_tail(&mid
->list
, &bridge_device
->mids_list
);
1619 err_write_mdb_entry
:
1620 kfree(mid
->ports_in_mid
);
1621 err_ports_in_mid_alloc
:
1626 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port
*mlxsw_sp_port
,
1627 struct mlxsw_sp_mid
*mid
)
1629 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1632 clear_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
);
1633 if (bitmap_empty(mid
->ports_in_mid
,
1634 mlxsw_core_max_ports(mlxsw_sp
->core
))) {
1635 err
= mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp
, mid
);
1636 list_del(&mid
->list
);
1637 kfree(mid
->ports_in_mid
);
1643 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1644 const struct switchdev_obj_port_mdb
*mdb
,
1645 struct switchdev_trans
*trans
)
1647 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1648 struct net_device
*orig_dev
= mdb
->obj
.orig_dev
;
1649 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1650 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1651 struct mlxsw_sp_bridge_device
*bridge_device
;
1652 struct mlxsw_sp_bridge_port
*bridge_port
;
1653 struct mlxsw_sp_mid
*mid
;
1657 if (switchdev_trans_ph_prepare(trans
))
1660 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1664 bridge_device
= bridge_port
->bridge_device
;
1665 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1668 if (!mlxsw_sp_port_vlan
)
1671 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1673 mid
= __mlxsw_sp_mc_get(bridge_device
, mdb
->addr
, fid_index
);
1675 mid
= __mlxsw_sp_mc_alloc(mlxsw_sp
, bridge_device
, mdb
->addr
,
1678 netdev_err(dev
, "Unable to allocate MC group\n");
1682 set_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
);
1684 if (!bridge_device
->multicast_enabled
)
1687 if (bridge_port
->mrouter
)
1690 err
= mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, true);
1692 netdev_err(dev
, "Unable to set SMID\n");
1699 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port
, mid
);
1704 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port
*mlxsw_sp_port
,
1705 struct mlxsw_sp_bridge_device
1708 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1709 struct mlxsw_sp_mid
*mid
;
1712 mc_enabled
= bridge_device
->multicast_enabled
;
1714 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1716 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp
, mid
,
1719 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp
, mid
);
1724 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port
*mlxsw_sp_port
,
1725 struct mlxsw_sp_bridge_port
*bridge_port
,
1728 struct mlxsw_sp_bridge_device
*bridge_device
;
1729 struct mlxsw_sp_mid
*mid
;
1731 bridge_device
= bridge_port
->bridge_device
;
1733 list_for_each_entry(mid
, &bridge_device
->mids_list
, list
) {
1734 if (!test_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
))
1735 mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, add
);
1739 struct mlxsw_sp_span_respin_work
{
1740 struct work_struct work
;
1741 struct mlxsw_sp
*mlxsw_sp
;
1744 static void mlxsw_sp_span_respin_work(struct work_struct
*work
)
1746 struct mlxsw_sp_span_respin_work
*respin_work
=
1747 container_of(work
, struct mlxsw_sp_span_respin_work
, work
);
1750 mlxsw_sp_span_respin(respin_work
->mlxsw_sp
);
1755 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp
*mlxsw_sp
)
1757 struct mlxsw_sp_span_respin_work
*respin_work
;
1759 respin_work
= kzalloc(sizeof(*respin_work
), GFP_ATOMIC
);
1763 INIT_WORK(&respin_work
->work
, mlxsw_sp_span_respin_work
);
1764 respin_work
->mlxsw_sp
= mlxsw_sp
;
1766 mlxsw_core_schedule_work(&respin_work
->work
);
1769 static int mlxsw_sp_port_obj_add(struct net_device
*dev
,
1770 const struct switchdev_obj
*obj
,
1771 struct switchdev_trans
*trans
,
1772 struct netlink_ext_ack
*extack
)
1774 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1775 const struct switchdev_obj_port_vlan
*vlan
;
1779 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1780 vlan
= SWITCHDEV_OBJ_PORT_VLAN(obj
);
1781 err
= mlxsw_sp_port_vlans_add(mlxsw_sp_port
, vlan
, trans
,
1784 if (switchdev_trans_ph_prepare(trans
)) {
1785 /* The event is emitted before the changes are actually
1786 * applied to the bridge. Therefore schedule the respin
1787 * call for later, so that the respin logic sees the
1788 * updated bridge state.
1790 mlxsw_sp_span_respin_schedule(mlxsw_sp_port
->mlxsw_sp
);
1793 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1794 err
= mlxsw_sp_port_mdb_add(mlxsw_sp_port
,
1795 SWITCHDEV_OBJ_PORT_MDB(obj
),
1807 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1808 struct mlxsw_sp_bridge_port
*bridge_port
, u16 vid
)
1810 u16 pvid
= mlxsw_sp_port
->pvid
== vid
? 0 : mlxsw_sp_port
->pvid
;
1811 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1813 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
1814 if (WARN_ON(!mlxsw_sp_port_vlan
))
1817 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan
);
1818 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, pvid
);
1819 mlxsw_sp_port_vlan_set(mlxsw_sp_port
, vid
, vid
, false, false);
1820 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan
);
1823 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1824 const struct switchdev_obj_port_vlan
*vlan
)
1826 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1827 struct net_device
*orig_dev
= vlan
->obj
.orig_dev
;
1828 struct mlxsw_sp_bridge_port
*bridge_port
;
1831 if (netif_is_bridge_master(orig_dev
))
1834 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1835 if (WARN_ON(!bridge_port
))
1838 if (!bridge_port
->bridge_device
->vlan_enabled
)
1841 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++)
1842 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port
, bridge_port
, vid
);
1848 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1849 struct mlxsw_sp_bridge_port
*bridge_port
,
1850 struct mlxsw_sp_mid
*mid
)
1852 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1855 if (bridge_port
->bridge_device
->multicast_enabled
&&
1856 !bridge_port
->mrouter
) {
1857 err
= mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, false);
1859 netdev_err(dev
, "Unable to remove port from SMID\n");
1862 err
= mlxsw_sp_port_remove_from_mid(mlxsw_sp_port
, mid
);
1864 netdev_err(dev
, "Unable to remove MC SFD\n");
1869 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1870 const struct switchdev_obj_port_mdb
*mdb
)
1872 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1873 struct net_device
*orig_dev
= mdb
->obj
.orig_dev
;
1874 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
1875 struct mlxsw_sp_bridge_device
*bridge_device
;
1876 struct net_device
*dev
= mlxsw_sp_port
->dev
;
1877 struct mlxsw_sp_bridge_port
*bridge_port
;
1878 struct mlxsw_sp_mid
*mid
;
1881 bridge_port
= mlxsw_sp_bridge_port_find(mlxsw_sp
->bridge
, orig_dev
);
1885 bridge_device
= bridge_port
->bridge_device
;
1886 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port
,
1889 if (!mlxsw_sp_port_vlan
)
1892 fid_index
= mlxsw_sp_fid_index(mlxsw_sp_port_vlan
->fid
);
1894 mid
= __mlxsw_sp_mc_get(bridge_device
, mdb
->addr
, fid_index
);
1896 netdev_err(dev
, "Unable to remove port from MC DB\n");
1900 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port
, bridge_port
, mid
);
1904 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
1905 struct mlxsw_sp_bridge_port
*bridge_port
)
1907 struct mlxsw_sp_bridge_device
*bridge_device
;
1908 struct mlxsw_sp_mid
*mid
, *tmp
;
1910 bridge_device
= bridge_port
->bridge_device
;
1912 list_for_each_entry_safe(mid
, tmp
, &bridge_device
->mids_list
, list
) {
1913 if (test_bit(mlxsw_sp_port
->local_port
, mid
->ports_in_mid
)) {
1914 __mlxsw_sp_port_mdb_del(mlxsw_sp_port
, bridge_port
,
1916 } else if (bridge_device
->multicast_enabled
&&
1917 bridge_port
->mrouter
) {
1918 mlxsw_sp_port_smid_set(mlxsw_sp_port
, mid
->mid
, false);
1923 static int mlxsw_sp_port_obj_del(struct net_device
*dev
,
1924 const struct switchdev_obj
*obj
)
1926 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1930 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1931 err
= mlxsw_sp_port_vlans_del(mlxsw_sp_port
,
1932 SWITCHDEV_OBJ_PORT_VLAN(obj
));
1934 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1935 err
= mlxsw_sp_port_mdb_del(mlxsw_sp_port
,
1936 SWITCHDEV_OBJ_PORT_MDB(obj
));
1943 mlxsw_sp_span_respin_schedule(mlxsw_sp_port
->mlxsw_sp
);
1948 static struct mlxsw_sp_port
*mlxsw_sp_lag_rep_port(struct mlxsw_sp
*mlxsw_sp
,
1951 struct mlxsw_sp_port
*mlxsw_sp_port
;
1952 u64 max_lag_members
;
1955 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1957 for (i
= 0; i
< max_lag_members
; i
++) {
1958 mlxsw_sp_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
);
1960 return mlxsw_sp_port
;
1965 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops
= {
1966 .switchdev_port_attr_get
= mlxsw_sp_port_attr_get
,
1967 .switchdev_port_attr_set
= mlxsw_sp_port_attr_set
,
1971 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device
*bridge_device
,
1972 struct mlxsw_sp_bridge_port
*bridge_port
,
1973 struct mlxsw_sp_port
*mlxsw_sp_port
,
1974 struct netlink_ext_ack
*extack
)
1976 if (is_vlan_dev(bridge_port
->dev
)) {
1977 NL_SET_ERR_MSG_MOD(extack
, "Can not enslave a VLAN device to a VLAN-aware bridge");
1981 /* Port is no longer usable as a router interface */
1982 if (mlxsw_sp_port
->default_vlan
->fid
)
1983 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port
->default_vlan
);
1989 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device
*bridge_device
,
1990 struct mlxsw_sp_bridge_port
*bridge_port
,
1991 struct mlxsw_sp_port
*mlxsw_sp_port
)
1993 /* Make sure untagged frames are allowed to ingress */
1994 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, MLXSW_SP_DEFAULT_VID
);
1998 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device
*bridge_device
,
1999 const struct net_device
*vxlan_dev
, u16 vid
,
2000 struct netlink_ext_ack
*extack
)
2002 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2003 struct vxlan_dev
*vxlan
= netdev_priv(vxlan_dev
);
2004 struct mlxsw_sp_nve_params params
= {
2005 .type
= MLXSW_SP_NVE_TYPE_VXLAN
,
2006 .vni
= vxlan
->cfg
.vni
,
2009 struct mlxsw_sp_fid
*fid
;
2012 /* If the VLAN is 0, we need to find the VLAN that is configured as
2013 * PVID and egress untagged on the bridge port of the VxLAN device.
2014 * It is possible no such VLAN exists
2017 err
= mlxsw_sp_vxlan_mapped_vid(vxlan_dev
, &vid
);
2022 /* If no other port is member in the VLAN, then the FID does not exist.
2023 * NVE will be enabled on the FID once a port joins the VLAN
2025 fid
= mlxsw_sp_fid_8021q_lookup(mlxsw_sp
, vid
);
2029 if (mlxsw_sp_fid_vni_is_set(fid
)) {
2031 goto err_vni_exists
;
2034 err
= mlxsw_sp_nve_fid_enable(mlxsw_sp
, fid
, ¶ms
, extack
);
2036 goto err_nve_fid_enable
;
2038 /* The tunnel port does not hold a reference on the FID. Only
2039 * local ports and the router port
2041 mlxsw_sp_fid_put(fid
);
2047 mlxsw_sp_fid_put(fid
);
2051 static struct net_device
*
2052 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device
*br_dev
, u16 vid
)
2054 struct net_device
*dev
;
2055 struct list_head
*iter
;
2057 netdev_for_each_lower_dev(br_dev
, dev
, iter
) {
2061 if (!netif_is_vxlan(dev
))
2064 err
= mlxsw_sp_vxlan_mapped_vid(dev
, &pvid
);
2065 if (err
|| pvid
!= vid
)
2074 static struct mlxsw_sp_fid
*
2075 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device
*bridge_device
,
2076 u16 vid
, struct netlink_ext_ack
*extack
)
2078 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2079 struct net_device
*vxlan_dev
;
2080 struct mlxsw_sp_fid
*fid
;
2083 fid
= mlxsw_sp_fid_8021q_get(mlxsw_sp
, vid
);
2087 if (mlxsw_sp_fid_vni_is_set(fid
))
2090 /* Find the VxLAN device that has the specified VLAN configured as
2091 * PVID and egress untagged. There can be at most one such device
2093 vxlan_dev
= mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device
->dev
,
2098 if (!netif_running(vxlan_dev
))
2101 err
= mlxsw_sp_bridge_8021q_vxlan_join(bridge_device
, vxlan_dev
, vid
,
2104 goto err_vxlan_join
;
2109 mlxsw_sp_fid_put(fid
);
2110 return ERR_PTR(err
);
2113 static struct mlxsw_sp_fid
*
2114 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device
*bridge_device
,
2117 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2119 return mlxsw_sp_fid_8021q_lookup(mlxsw_sp
, vid
);
2123 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device
*bridge_device
,
2124 const struct mlxsw_sp_fid
*fid
)
2126 return mlxsw_sp_fid_8021q_vid(fid
);
2129 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops
= {
2130 .port_join
= mlxsw_sp_bridge_8021q_port_join
,
2131 .port_leave
= mlxsw_sp_bridge_8021q_port_leave
,
2132 .vxlan_join
= mlxsw_sp_bridge_8021q_vxlan_join
,
2133 .fid_get
= mlxsw_sp_bridge_8021q_fid_get
,
2134 .fid_lookup
= mlxsw_sp_bridge_8021q_fid_lookup
,
2135 .fid_vid
= mlxsw_sp_bridge_8021q_fid_vid
,
2139 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port
*mlxsw_sp_port
,
2140 const struct net_device
*br_dev
)
2142 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2144 list_for_each_entry(mlxsw_sp_port_vlan
, &mlxsw_sp_port
->vlans_list
,
2146 if (mlxsw_sp_port_vlan
->bridge_port
&&
2147 mlxsw_sp_port_vlan
->bridge_port
->bridge_device
->dev
==
2156 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device
*bridge_device
,
2157 struct mlxsw_sp_bridge_port
*bridge_port
,
2158 struct mlxsw_sp_port
*mlxsw_sp_port
,
2159 struct netlink_ext_ack
*extack
)
2161 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2162 struct net_device
*dev
= bridge_port
->dev
;
2165 vid
= is_vlan_dev(dev
) ? vlan_dev_vlan_id(dev
) : MLXSW_SP_DEFAULT_VID
;
2166 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
2167 if (WARN_ON(!mlxsw_sp_port_vlan
))
2170 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port
, bridge_device
->dev
)) {
2171 NL_SET_ERR_MSG_MOD(extack
, "Can not bridge VLAN uppers of the same port");
2175 /* Port is no longer usable as a router interface */
2176 if (mlxsw_sp_port_vlan
->fid
)
2177 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan
);
2179 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan
, bridge_port
,
2184 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device
*bridge_device
,
2185 struct mlxsw_sp_bridge_port
*bridge_port
,
2186 struct mlxsw_sp_port
*mlxsw_sp_port
)
2188 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2189 struct net_device
*dev
= bridge_port
->dev
;
2192 vid
= is_vlan_dev(dev
) ? vlan_dev_vlan_id(dev
) : MLXSW_SP_DEFAULT_VID
;
2193 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port
, vid
);
2194 if (!mlxsw_sp_port_vlan
|| !mlxsw_sp_port_vlan
->bridge_port
)
2197 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan
);
2201 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device
*bridge_device
,
2202 const struct net_device
*vxlan_dev
, u16 vid
,
2203 struct netlink_ext_ack
*extack
)
2205 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2206 struct vxlan_dev
*vxlan
= netdev_priv(vxlan_dev
);
2207 struct mlxsw_sp_nve_params params
= {
2208 .type
= MLXSW_SP_NVE_TYPE_VXLAN
,
2209 .vni
= vxlan
->cfg
.vni
,
2212 struct mlxsw_sp_fid
*fid
;
2215 fid
= mlxsw_sp_fid_8021d_lookup(mlxsw_sp
, bridge_device
->dev
->ifindex
);
2219 if (mlxsw_sp_fid_vni_is_set(fid
)) {
2221 goto err_vni_exists
;
2224 err
= mlxsw_sp_nve_fid_enable(mlxsw_sp
, fid
, ¶ms
, extack
);
2226 goto err_nve_fid_enable
;
2228 /* The tunnel port does not hold a reference on the FID. Only
2229 * local ports and the router port
2231 mlxsw_sp_fid_put(fid
);
2237 mlxsw_sp_fid_put(fid
);
2241 static struct mlxsw_sp_fid
*
2242 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device
*bridge_device
,
2243 u16 vid
, struct netlink_ext_ack
*extack
)
2245 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2246 struct net_device
*vxlan_dev
;
2247 struct mlxsw_sp_fid
*fid
;
2250 fid
= mlxsw_sp_fid_8021d_get(mlxsw_sp
, bridge_device
->dev
->ifindex
);
2254 if (mlxsw_sp_fid_vni_is_set(fid
))
2257 vxlan_dev
= mlxsw_sp_bridge_vxlan_dev_find(bridge_device
->dev
);
2261 if (!netif_running(vxlan_dev
))
2264 err
= mlxsw_sp_bridge_8021d_vxlan_join(bridge_device
, vxlan_dev
, 0,
2267 goto err_vxlan_join
;
2272 mlxsw_sp_fid_put(fid
);
2273 return ERR_PTR(err
);
2276 static struct mlxsw_sp_fid
*
2277 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device
*bridge_device
,
2280 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(bridge_device
->dev
);
2282 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2286 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp
, bridge_device
->dev
->ifindex
);
2290 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device
*bridge_device
,
2291 const struct mlxsw_sp_fid
*fid
)
2296 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops
= {
2297 .port_join
= mlxsw_sp_bridge_8021d_port_join
,
2298 .port_leave
= mlxsw_sp_bridge_8021d_port_leave
,
2299 .vxlan_join
= mlxsw_sp_bridge_8021d_vxlan_join
,
2300 .fid_get
= mlxsw_sp_bridge_8021d_fid_get
,
2301 .fid_lookup
= mlxsw_sp_bridge_8021d_fid_lookup
,
2302 .fid_vid
= mlxsw_sp_bridge_8021d_fid_vid
,
2305 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
2306 struct net_device
*brport_dev
,
2307 struct net_device
*br_dev
,
2308 struct netlink_ext_ack
*extack
)
2310 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2311 struct mlxsw_sp_bridge_device
*bridge_device
;
2312 struct mlxsw_sp_bridge_port
*bridge_port
;
2315 bridge_port
= mlxsw_sp_bridge_port_get(mlxsw_sp
->bridge
, brport_dev
);
2316 if (IS_ERR(bridge_port
))
2317 return PTR_ERR(bridge_port
);
2318 bridge_device
= bridge_port
->bridge_device
;
2320 err
= bridge_device
->ops
->port_join(bridge_device
, bridge_port
,
2321 mlxsw_sp_port
, extack
);
2328 mlxsw_sp_bridge_port_put(mlxsw_sp
->bridge
, bridge_port
);
2332 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_port
,
2333 struct net_device
*brport_dev
,
2334 struct net_device
*br_dev
)
2336 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2337 struct mlxsw_sp_bridge_device
*bridge_device
;
2338 struct mlxsw_sp_bridge_port
*bridge_port
;
2340 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2343 bridge_port
= __mlxsw_sp_bridge_port_find(bridge_device
, brport_dev
);
2347 bridge_device
->ops
->port_leave(bridge_device
, bridge_port
,
2349 mlxsw_sp_bridge_port_put(mlxsw_sp
->bridge
, bridge_port
);
2352 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp
*mlxsw_sp
,
2353 const struct net_device
*br_dev
,
2354 const struct net_device
*vxlan_dev
, u16 vid
,
2355 struct netlink_ext_ack
*extack
)
2357 struct mlxsw_sp_bridge_device
*bridge_device
;
2359 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2360 if (WARN_ON(!bridge_device
))
2363 return bridge_device
->ops
->vxlan_join(bridge_device
, vxlan_dev
, vid
,
2367 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp
*mlxsw_sp
,
2368 const struct net_device
*vxlan_dev
)
2370 struct vxlan_dev
*vxlan
= netdev_priv(vxlan_dev
);
2371 struct mlxsw_sp_fid
*fid
;
2373 /* If the VxLAN device is down, then the FID does not have a VNI */
2374 fid
= mlxsw_sp_fid_lookup_by_vni(mlxsw_sp
, vxlan
->cfg
.vni
);
2378 mlxsw_sp_nve_fid_disable(mlxsw_sp
, fid
);
2379 mlxsw_sp_fid_put(fid
);
2382 struct mlxsw_sp_fid
*mlxsw_sp_bridge_fid_get(struct mlxsw_sp
*mlxsw_sp
,
2383 const struct net_device
*br_dev
,
2385 struct netlink_ext_ack
*extack
)
2387 struct mlxsw_sp_bridge_device
*bridge_device
;
2389 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2390 if (WARN_ON(!bridge_device
))
2391 return ERR_PTR(-EINVAL
);
2393 return bridge_device
->ops
->fid_get(bridge_device
, vid
, extack
);
2397 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr
*vxlan_addr
,
2398 enum mlxsw_sp_l3proto
*proto
,
2399 union mlxsw_sp_l3addr
*addr
)
2401 if (vxlan_addr
->sa
.sa_family
== AF_INET
) {
2402 addr
->addr4
= vxlan_addr
->sin
.sin_addr
.s_addr
;
2403 *proto
= MLXSW_SP_L3_PROTO_IPV4
;
2405 addr
->addr6
= vxlan_addr
->sin6
.sin6_addr
;
2406 *proto
= MLXSW_SP_L3_PROTO_IPV6
;
2411 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto
,
2412 const union mlxsw_sp_l3addr
*addr
,
2413 union vxlan_addr
*vxlan_addr
)
2416 case MLXSW_SP_L3_PROTO_IPV4
:
2417 vxlan_addr
->sa
.sa_family
= AF_INET
;
2418 vxlan_addr
->sin
.sin_addr
.s_addr
= addr
->addr4
;
2420 case MLXSW_SP_L3_PROTO_IPV6
:
2421 vxlan_addr
->sa
.sa_family
= AF_INET6
;
2422 vxlan_addr
->sin6
.sin6_addr
= addr
->addr6
;
2427 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device
*dev
,
2429 enum mlxsw_sp_l3proto proto
,
2430 union mlxsw_sp_l3addr
*addr
,
2431 __be32 vni
, bool adding
)
2433 struct switchdev_notifier_vxlan_fdb_info info
;
2434 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2435 enum switchdev_notifier_type type
;
2437 type
= adding
? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE
:
2438 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE
;
2439 mlxsw_sp_switchdev_addr_vxlan_convert(proto
, addr
, &info
.remote_ip
);
2440 info
.remote_port
= vxlan
->cfg
.dst_port
;
2441 info
.remote_vni
= vni
;
2442 info
.remote_ifindex
= 0;
2443 ether_addr_copy(info
.eth_addr
, mac
);
2445 info
.offloaded
= adding
;
2446 call_switchdev_notifiers(type
, dev
, &info
.info
, NULL
);
2449 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device
*dev
,
2451 enum mlxsw_sp_l3proto proto
,
2452 union mlxsw_sp_l3addr
*addr
,
2456 if (netif_is_vxlan(dev
))
2457 mlxsw_sp_fdb_vxlan_call_notifiers(dev
, mac
, proto
, addr
, vni
,
2462 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type
,
2463 const char *mac
, u16 vid
,
2464 struct net_device
*dev
, bool offloaded
)
2466 struct switchdev_notifier_fdb_info info
;
2470 info
.offloaded
= offloaded
;
2471 call_switchdev_notifiers(type
, dev
, &info
.info
, NULL
);
2474 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp
*mlxsw_sp
,
2475 char *sfn_pl
, int rec_index
,
2478 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2479 struct mlxsw_sp_bridge_device
*bridge_device
;
2480 struct mlxsw_sp_bridge_port
*bridge_port
;
2481 struct mlxsw_sp_port
*mlxsw_sp_port
;
2482 enum switchdev_notifier_type type
;
2486 bool do_notification
= true;
2489 mlxsw_reg_sfn_mac_unpack(sfn_pl
, rec_index
, mac
, &fid
, &local_port
);
2490 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2491 if (!mlxsw_sp_port
) {
2492 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Incorrect local port in FDB notification\n");
2496 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port
, fid
);
2497 if (!mlxsw_sp_port_vlan
) {
2498 netdev_err(mlxsw_sp_port
->dev
, "Failed to find a matching {Port, VID} following FDB notification\n");
2502 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
2504 netdev_err(mlxsw_sp_port
->dev
, "{Port, VID} not associated with a bridge\n");
2508 bridge_device
= bridge_port
->bridge_device
;
2509 vid
= bridge_device
->vlan_enabled
? mlxsw_sp_port_vlan
->vid
: 0;
2512 err
= mlxsw_sp_port_fdb_uc_op(mlxsw_sp
, local_port
, mac
, fid
,
2515 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to set FDB entry\n");
2519 if (!do_notification
)
2521 type
= adding
? SWITCHDEV_FDB_ADD_TO_BRIDGE
: SWITCHDEV_FDB_DEL_TO_BRIDGE
;
2522 mlxsw_sp_fdb_call_notifiers(type
, mac
, vid
, bridge_port
->dev
, adding
);
2528 do_notification
= false;
2532 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp
*mlxsw_sp
,
2533 char *sfn_pl
, int rec_index
,
2536 struct mlxsw_sp_port_vlan
*mlxsw_sp_port_vlan
;
2537 struct mlxsw_sp_bridge_device
*bridge_device
;
2538 struct mlxsw_sp_bridge_port
*bridge_port
;
2539 struct mlxsw_sp_port
*mlxsw_sp_port
;
2540 enum switchdev_notifier_type type
;
2545 bool do_notification
= true;
2548 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl
, rec_index
, mac
, &fid
, &lag_id
);
2549 mlxsw_sp_port
= mlxsw_sp_lag_rep_port(mlxsw_sp
, lag_id
);
2550 if (!mlxsw_sp_port
) {
2551 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Cannot find port representor for LAG\n");
2555 mlxsw_sp_port_vlan
= mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port
, fid
);
2556 if (!mlxsw_sp_port_vlan
) {
2557 netdev_err(mlxsw_sp_port
->dev
, "Failed to find a matching {Port, VID} following FDB notification\n");
2561 bridge_port
= mlxsw_sp_port_vlan
->bridge_port
;
2563 netdev_err(mlxsw_sp_port
->dev
, "{Port, VID} not associated with a bridge\n");
2567 bridge_device
= bridge_port
->bridge_device
;
2568 vid
= bridge_device
->vlan_enabled
? mlxsw_sp_port_vlan
->vid
: 0;
2569 lag_vid
= mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan
->fid
) ?
2570 mlxsw_sp_port_vlan
->vid
: 0;
2573 err
= mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp
, lag_id
, mac
, fid
, lag_vid
,
2576 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to set FDB entry\n");
2580 if (!do_notification
)
2582 type
= adding
? SWITCHDEV_FDB_ADD_TO_BRIDGE
: SWITCHDEV_FDB_DEL_TO_BRIDGE
;
2583 mlxsw_sp_fdb_call_notifiers(type
, mac
, vid
, bridge_port
->dev
, adding
);
2589 do_notification
= false;
2594 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp
*mlxsw_sp
,
2595 const struct mlxsw_sp_fid
*fid
,
2597 struct net_device
**nve_dev
,
2598 u16
*p_vid
, __be32
*p_vni
)
2600 struct mlxsw_sp_bridge_device
*bridge_device
;
2601 struct net_device
*br_dev
, *dev
;
2605 err
= mlxsw_sp_fid_nve_ifindex(fid
, &nve_ifindex
);
2609 err
= mlxsw_sp_fid_vni(fid
, p_vni
);
2613 dev
= __dev_get_by_index(&init_net
, nve_ifindex
);
2618 if (!netif_running(dev
))
2621 if (adding
&& !br_port_flag_is_set(dev
, BR_LEARNING
))
2624 if (adding
&& netif_is_vxlan(dev
)) {
2625 struct vxlan_dev
*vxlan
= netdev_priv(dev
);
2627 if (!(vxlan
->cfg
.flags
& VXLAN_F_LEARN
))
2631 br_dev
= netdev_master_upper_dev_get(dev
);
2635 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2639 *p_vid
= bridge_device
->ops
->fid_vid(bridge_device
, fid
);
2644 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp
*mlxsw_sp
,
2649 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto
;
2650 enum switchdev_notifier_type type
;
2651 struct net_device
*nve_dev
;
2652 union mlxsw_sp_l3addr addr
;
2653 struct mlxsw_sp_fid
*fid
;
2660 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl
, rec_index
, mac
, &fid_index
,
2663 fid
= mlxsw_sp_fid_lookup_by_index(mlxsw_sp
, fid_index
);
2665 goto err_fid_lookup
;
2667 err
= mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp
, uip
,
2668 (enum mlxsw_sp_l3proto
) sfn_proto
,
2671 goto err_ip_resolve
;
2673 err
= __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp
, fid
, adding
,
2674 &nve_dev
, &vid
, &vni
);
2676 goto err_fdb_process
;
2678 err
= mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
, mac
, fid_index
,
2679 (enum mlxsw_sp_l3proto
) sfn_proto
,
2680 &addr
, adding
, true);
2684 mlxsw_sp_fdb_nve_call_notifiers(nve_dev
, mac
,
2685 (enum mlxsw_sp_l3proto
) sfn_proto
,
2686 &addr
, vni
, adding
);
2688 type
= adding
? SWITCHDEV_FDB_ADD_TO_BRIDGE
:
2689 SWITCHDEV_FDB_DEL_TO_BRIDGE
;
2690 mlxsw_sp_fdb_call_notifiers(type
, mac
, vid
, nve_dev
, adding
);
2692 mlxsw_sp_fid_put(fid
);
2699 mlxsw_sp_fid_put(fid
);
2701 /* Remove an FDB entry in case we cannot process it. Otherwise the
2702 * device will keep sending the same notification over and over again.
2704 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
, mac
, fid_index
,
2705 (enum mlxsw_sp_l3proto
) sfn_proto
, &addr
,
2709 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp
*mlxsw_sp
,
2710 char *sfn_pl
, int rec_index
)
2712 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl
, rec_index
)) {
2713 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC
:
2714 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp
, sfn_pl
,
2717 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC
:
2718 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp
, sfn_pl
,
2721 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG
:
2722 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp
, sfn_pl
,
2725 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG
:
2726 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp
, sfn_pl
,
2729 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL
:
2730 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp
, sfn_pl
,
2733 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL
:
2734 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp
, sfn_pl
,
2740 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp
*mlxsw_sp
)
2742 struct mlxsw_sp_bridge
*bridge
= mlxsw_sp
->bridge
;
2744 mlxsw_core_schedule_dw(&bridge
->fdb_notify
.dw
,
2745 msecs_to_jiffies(bridge
->fdb_notify
.interval
));
2748 static void mlxsw_sp_fdb_notify_work(struct work_struct
*work
)
2750 struct mlxsw_sp_bridge
*bridge
;
2751 struct mlxsw_sp
*mlxsw_sp
;
2757 sfn_pl
= kmalloc(MLXSW_REG_SFN_LEN
, GFP_KERNEL
);
2761 bridge
= container_of(work
, struct mlxsw_sp_bridge
, fdb_notify
.dw
.work
);
2762 mlxsw_sp
= bridge
->mlxsw_sp
;
2765 mlxsw_reg_sfn_pack(sfn_pl
);
2766 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(sfn
), sfn_pl
);
2768 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to get FDB notifications\n");
2771 num_rec
= mlxsw_reg_sfn_num_rec_get(sfn_pl
);
2772 for (i
= 0; i
< num_rec
; i
++)
2773 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp
, sfn_pl
, i
);
2778 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp
);
2781 struct mlxsw_sp_switchdev_event_work
{
2782 struct work_struct work
;
2784 struct switchdev_notifier_fdb_info fdb_info
;
2785 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info
;
2787 struct net_device
*dev
;
2788 unsigned long event
;
2792 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp
*mlxsw_sp
,
2793 struct mlxsw_sp_switchdev_event_work
*
2795 struct mlxsw_sp_fid
*fid
, __be32 vni
)
2797 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info
;
2798 struct switchdev_notifier_fdb_info
*fdb_info
;
2799 struct net_device
*dev
= switchdev_work
->dev
;
2800 enum mlxsw_sp_l3proto proto
;
2801 union mlxsw_sp_l3addr addr
;
2804 fdb_info
= &switchdev_work
->fdb_info
;
2805 err
= vxlan_fdb_find_uc(dev
, fdb_info
->addr
, vni
, &vxlan_fdb_info
);
2809 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info
.remote_ip
,
2812 switch (switchdev_work
->event
) {
2813 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
2814 err
= mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
,
2815 vxlan_fdb_info
.eth_addr
,
2816 mlxsw_sp_fid_index(fid
),
2817 proto
, &addr
, true, false);
2820 vxlan_fdb_info
.offloaded
= true;
2821 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED
, dev
,
2822 &vxlan_fdb_info
.info
, NULL
);
2823 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED
,
2824 vxlan_fdb_info
.eth_addr
,
2825 fdb_info
->vid
, dev
, true);
2827 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2828 err
= mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
,
2829 vxlan_fdb_info
.eth_addr
,
2830 mlxsw_sp_fid_index(fid
),
2831 proto
, &addr
, false,
2833 vxlan_fdb_info
.offloaded
= false;
2834 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED
, dev
,
2835 &vxlan_fdb_info
.info
, NULL
);
2841 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work
*
2844 struct mlxsw_sp_bridge_device
*bridge_device
;
2845 struct net_device
*dev
= switchdev_work
->dev
;
2846 struct net_device
*br_dev
;
2847 struct mlxsw_sp
*mlxsw_sp
;
2848 struct mlxsw_sp_fid
*fid
;
2852 if (switchdev_work
->event
!= SWITCHDEV_FDB_ADD_TO_DEVICE
&&
2853 switchdev_work
->event
!= SWITCHDEV_FDB_DEL_TO_DEVICE
)
2856 if (switchdev_work
->event
== SWITCHDEV_FDB_ADD_TO_DEVICE
&&
2857 !switchdev_work
->fdb_info
.added_by_user
)
2860 if (!netif_running(dev
))
2862 br_dev
= netdev_master_upper_dev_get(dev
);
2865 if (!netif_is_bridge_master(br_dev
))
2867 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
2870 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2874 fid
= bridge_device
->ops
->fid_lookup(bridge_device
,
2875 switchdev_work
->fdb_info
.vid
);
2879 err
= mlxsw_sp_fid_vni(fid
, &vni
);
2883 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp
, switchdev_work
, fid
,
2887 mlxsw_sp_fid_put(fid
);
2890 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct
*work
)
2892 struct mlxsw_sp_switchdev_event_work
*switchdev_work
=
2893 container_of(work
, struct mlxsw_sp_switchdev_event_work
, work
);
2894 struct net_device
*dev
= switchdev_work
->dev
;
2895 struct switchdev_notifier_fdb_info
*fdb_info
;
2896 struct mlxsw_sp_port
*mlxsw_sp_port
;
2900 if (netif_is_vxlan(dev
)) {
2901 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work
);
2905 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(dev
);
2909 switch (switchdev_work
->event
) {
2910 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
2911 fdb_info
= &switchdev_work
->fdb_info
;
2912 if (!fdb_info
->added_by_user
)
2914 err
= mlxsw_sp_port_fdb_set(mlxsw_sp_port
, fdb_info
, true);
2917 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED
,
2919 fdb_info
->vid
, dev
, true);
2921 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2922 fdb_info
= &switchdev_work
->fdb_info
;
2923 mlxsw_sp_port_fdb_set(mlxsw_sp_port
, fdb_info
, false);
2925 case SWITCHDEV_FDB_ADD_TO_BRIDGE
: /* fall through */
2926 case SWITCHDEV_FDB_DEL_TO_BRIDGE
:
2927 /* These events are only used to potentially update an existing
2933 mlxsw_sp_span_respin(mlxsw_sp_port
->mlxsw_sp
);
2937 kfree(switchdev_work
->fdb_info
.addr
);
2938 kfree(switchdev_work
);
2943 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp
*mlxsw_sp
,
2944 struct mlxsw_sp_switchdev_event_work
*
2947 struct switchdev_notifier_vxlan_fdb_info
*vxlan_fdb_info
;
2948 struct mlxsw_sp_bridge_device
*bridge_device
;
2949 struct net_device
*dev
= switchdev_work
->dev
;
2950 u8 all_zeros_mac
[ETH_ALEN
] = { 0 };
2951 enum mlxsw_sp_l3proto proto
;
2952 union mlxsw_sp_l3addr addr
;
2953 struct net_device
*br_dev
;
2954 struct mlxsw_sp_fid
*fid
;
2958 vxlan_fdb_info
= &switchdev_work
->vxlan_fdb_info
;
2959 br_dev
= netdev_master_upper_dev_get(dev
);
2961 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
2965 fid
= mlxsw_sp_fid_lookup_by_vni(mlxsw_sp
, vxlan_fdb_info
->vni
);
2969 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info
->remote_ip
,
2972 if (ether_addr_equal(vxlan_fdb_info
->eth_addr
, all_zeros_mac
)) {
2973 err
= mlxsw_sp_nve_flood_ip_add(mlxsw_sp
, fid
, proto
, &addr
);
2975 mlxsw_sp_fid_put(fid
);
2978 vxlan_fdb_info
->offloaded
= true;
2979 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED
, dev
,
2980 &vxlan_fdb_info
->info
, NULL
);
2981 mlxsw_sp_fid_put(fid
);
2985 /* The device has a single FDB table, whereas Linux has two - one
2986 * in the bridge driver and another in the VxLAN driver. We only
2987 * program an entry to the device if the MAC points to the VxLAN
2988 * device in the bridge's FDB table
2990 vid
= bridge_device
->ops
->fid_vid(bridge_device
, fid
);
2991 if (br_fdb_find_port(br_dev
, vxlan_fdb_info
->eth_addr
, vid
) != dev
)
2992 goto err_br_fdb_find
;
2994 err
= mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
, vxlan_fdb_info
->eth_addr
,
2995 mlxsw_sp_fid_index(fid
), proto
,
2996 &addr
, true, false);
2998 goto err_fdb_tunnel_uc_op
;
2999 vxlan_fdb_info
->offloaded
= true;
3000 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED
, dev
,
3001 &vxlan_fdb_info
->info
, NULL
);
3002 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED
,
3003 vxlan_fdb_info
->eth_addr
, vid
, dev
, true);
3005 mlxsw_sp_fid_put(fid
);
3009 err_fdb_tunnel_uc_op
:
3011 mlxsw_sp_fid_put(fid
);
3015 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp
*mlxsw_sp
,
3016 struct mlxsw_sp_switchdev_event_work
*
3019 struct switchdev_notifier_vxlan_fdb_info
*vxlan_fdb_info
;
3020 struct mlxsw_sp_bridge_device
*bridge_device
;
3021 struct net_device
*dev
= switchdev_work
->dev
;
3022 struct net_device
*br_dev
= netdev_master_upper_dev_get(dev
);
3023 u8 all_zeros_mac
[ETH_ALEN
] = { 0 };
3024 enum mlxsw_sp_l3proto proto
;
3025 union mlxsw_sp_l3addr addr
;
3026 struct mlxsw_sp_fid
*fid
;
3029 vxlan_fdb_info
= &switchdev_work
->vxlan_fdb_info
;
3031 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
3035 fid
= mlxsw_sp_fid_lookup_by_vni(mlxsw_sp
, vxlan_fdb_info
->vni
);
3039 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info
->remote_ip
,
3042 if (ether_addr_equal(vxlan_fdb_info
->eth_addr
, all_zeros_mac
)) {
3043 mlxsw_sp_nve_flood_ip_del(mlxsw_sp
, fid
, proto
, &addr
);
3044 mlxsw_sp_fid_put(fid
);
3048 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp
, vxlan_fdb_info
->eth_addr
,
3049 mlxsw_sp_fid_index(fid
), proto
, &addr
,
3051 vid
= bridge_device
->ops
->fid_vid(bridge_device
, fid
);
3052 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED
,
3053 vxlan_fdb_info
->eth_addr
, vid
, dev
, false);
3055 mlxsw_sp_fid_put(fid
);
3058 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct
*work
)
3060 struct mlxsw_sp_switchdev_event_work
*switchdev_work
=
3061 container_of(work
, struct mlxsw_sp_switchdev_event_work
, work
);
3062 struct net_device
*dev
= switchdev_work
->dev
;
3063 struct mlxsw_sp
*mlxsw_sp
;
3064 struct net_device
*br_dev
;
3068 if (!netif_running(dev
))
3070 br_dev
= netdev_master_upper_dev_get(dev
);
3073 if (!netif_is_bridge_master(br_dev
))
3075 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
3079 switch (switchdev_work
->event
) {
3080 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
:
3081 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp
, switchdev_work
);
3083 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE
:
3084 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp
, switchdev_work
);
3090 kfree(switchdev_work
);
3095 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work
*
3097 struct switchdev_notifier_info
*info
)
3099 struct vxlan_dev
*vxlan
= netdev_priv(switchdev_work
->dev
);
3100 struct switchdev_notifier_vxlan_fdb_info
*vxlan_fdb_info
;
3101 struct vxlan_config
*cfg
= &vxlan
->cfg
;
3103 vxlan_fdb_info
= container_of(info
,
3104 struct switchdev_notifier_vxlan_fdb_info
,
3107 if (vxlan_fdb_info
->remote_port
!= cfg
->dst_port
)
3109 if (vxlan_fdb_info
->remote_vni
!= cfg
->vni
)
3111 if (vxlan_fdb_info
->vni
!= cfg
->vni
)
3113 if (vxlan_fdb_info
->remote_ifindex
)
3115 if (is_multicast_ether_addr(vxlan_fdb_info
->eth_addr
))
3117 if (vxlan_addr_multicast(&vxlan_fdb_info
->remote_ip
))
3120 switchdev_work
->vxlan_fdb_info
= *vxlan_fdb_info
;
3125 /* Called under rcu_read_lock() */
3126 static int mlxsw_sp_switchdev_event(struct notifier_block
*unused
,
3127 unsigned long event
, void *ptr
)
3129 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
3130 struct mlxsw_sp_switchdev_event_work
*switchdev_work
;
3131 struct switchdev_notifier_fdb_info
*fdb_info
;
3132 struct switchdev_notifier_info
*info
= ptr
;
3133 struct net_device
*br_dev
;
3136 /* Tunnel devices are not our uppers, so check their master instead */
3137 br_dev
= netdev_master_upper_dev_get_rcu(dev
);
3140 if (!netif_is_bridge_master(br_dev
))
3142 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev
))
3145 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
3146 if (!switchdev_work
)
3149 switchdev_work
->dev
= dev
;
3150 switchdev_work
->event
= event
;
3153 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
3154 case SWITCHDEV_FDB_DEL_TO_DEVICE
: /* fall through */
3155 case SWITCHDEV_FDB_ADD_TO_BRIDGE
: /* fall through */
3156 case SWITCHDEV_FDB_DEL_TO_BRIDGE
:
3157 fdb_info
= container_of(info
,
3158 struct switchdev_notifier_fdb_info
,
3160 INIT_WORK(&switchdev_work
->work
,
3161 mlxsw_sp_switchdev_bridge_fdb_event_work
);
3162 memcpy(&switchdev_work
->fdb_info
, ptr
,
3163 sizeof(switchdev_work
->fdb_info
));
3164 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
3165 if (!switchdev_work
->fdb_info
.addr
)
3166 goto err_addr_alloc
;
3167 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
3169 /* Take a reference on the device. This can be either
3170 * upper device containig mlxsw_sp_port or just a
3175 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
: /* fall through */
3176 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE
:
3177 INIT_WORK(&switchdev_work
->work
,
3178 mlxsw_sp_switchdev_vxlan_fdb_event_work
);
3179 err
= mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work
,
3182 goto err_vxlan_work_prepare
;
3186 kfree(switchdev_work
);
3190 mlxsw_core_schedule_work(&switchdev_work
->work
);
3194 err_vxlan_work_prepare
:
3196 kfree(switchdev_work
);
3200 struct notifier_block mlxsw_sp_switchdev_notifier
= {
3201 .notifier_call
= mlxsw_sp_switchdev_event
,
3205 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp
*mlxsw_sp
,
3206 struct mlxsw_sp_bridge_device
*bridge_device
,
3207 const struct net_device
*vxlan_dev
, u16 vid
,
3208 bool flag_untagged
, bool flag_pvid
,
3209 struct netlink_ext_ack
*extack
)
3211 struct vxlan_dev
*vxlan
= netdev_priv(vxlan_dev
);
3212 __be32 vni
= vxlan
->cfg
.vni
;
3213 struct mlxsw_sp_fid
*fid
;
3217 /* We cannot have the same VLAN as PVID and egress untagged on multiple
3218 * VxLAN devices. Note that we get this notification before the VLAN is
3219 * actually added to the bridge's database, so it is not possible for
3220 * the lookup function to return 'vxlan_dev'
3222 if (flag_untagged
&& flag_pvid
&&
3223 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device
->dev
, vid
))
3226 if (!netif_running(vxlan_dev
))
3229 /* First case: FID is not associated with this VNI, but the new VLAN
3230 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3233 fid
= mlxsw_sp_fid_lookup_by_vni(mlxsw_sp
, vni
);
3235 if (!flag_untagged
|| !flag_pvid
)
3237 return mlxsw_sp_bridge_8021q_vxlan_join(bridge_device
,
3238 vxlan_dev
, vid
, extack
);
3241 /* Second case: FID is associated with the VNI and the VLAN associated
3242 * with the FID is the same as the notified VLAN. This means the flags
3243 * (PVID / egress untagged) were toggled and that NVE should be
3244 * disabled on the FID
3246 old_vid
= mlxsw_sp_fid_8021q_vid(fid
);
3247 if (vid
== old_vid
) {
3248 if (WARN_ON(flag_untagged
&& flag_pvid
)) {
3249 mlxsw_sp_fid_put(fid
);
3252 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp
, vxlan_dev
);
3253 mlxsw_sp_fid_put(fid
);
3257 /* Third case: A new VLAN was configured on the VxLAN device, but this
3258 * VLAN is not PVID, so there is nothing to do.
3261 mlxsw_sp_fid_put(fid
);
3265 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3266 * mapped to the VNI should be unmapped
3268 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp
, vxlan_dev
);
3269 mlxsw_sp_fid_put(fid
);
3271 /* Fifth case: The new VLAN is also egress untagged, which means the
3272 * VLAN needs to be mapped to the VNI
3277 err
= mlxsw_sp_bridge_8021q_vxlan_join(bridge_device
, vxlan_dev
, vid
,
3280 goto err_vxlan_join
;
3285 mlxsw_sp_bridge_8021q_vxlan_join(bridge_device
, vxlan_dev
, old_vid
,
3291 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp
*mlxsw_sp
,
3292 struct mlxsw_sp_bridge_device
*bridge_device
,
3293 const struct net_device
*vxlan_dev
, u16 vid
)
3295 struct vxlan_dev
*vxlan
= netdev_priv(vxlan_dev
);
3296 __be32 vni
= vxlan
->cfg
.vni
;
3297 struct mlxsw_sp_fid
*fid
;
3299 if (!netif_running(vxlan_dev
))
3302 fid
= mlxsw_sp_fid_lookup_by_vni(mlxsw_sp
, vni
);
3306 /* A different VLAN than the one mapped to the VNI is deleted */
3307 if (mlxsw_sp_fid_8021q_vid(fid
) != vid
)
3310 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp
, vxlan_dev
);
3313 mlxsw_sp_fid_put(fid
);
3317 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device
*vxlan_dev
,
3318 struct switchdev_notifier_port_obj_info
*
3321 struct switchdev_obj_port_vlan
*vlan
=
3322 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info
->obj
);
3323 bool flag_untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
3324 bool flag_pvid
= vlan
->flags
& BRIDGE_VLAN_INFO_PVID
;
3325 struct switchdev_trans
*trans
= port_obj_info
->trans
;
3326 struct mlxsw_sp_bridge_device
*bridge_device
;
3327 struct netlink_ext_ack
*extack
;
3328 struct mlxsw_sp
*mlxsw_sp
;
3329 struct net_device
*br_dev
;
3332 extack
= switchdev_notifier_info_to_extack(&port_obj_info
->info
);
3333 br_dev
= netdev_master_upper_dev_get(vxlan_dev
);
3337 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
3341 port_obj_info
->handled
= true;
3343 if (switchdev_trans_ph_commit(trans
))
3346 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
3350 if (!bridge_device
->vlan_enabled
)
3353 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
3356 err
= mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp
, bridge_device
,
3368 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device
*vxlan_dev
,
3369 struct switchdev_notifier_port_obj_info
*
3372 struct switchdev_obj_port_vlan
*vlan
=
3373 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info
->obj
);
3374 struct mlxsw_sp_bridge_device
*bridge_device
;
3375 struct mlxsw_sp
*mlxsw_sp
;
3376 struct net_device
*br_dev
;
3379 br_dev
= netdev_master_upper_dev_get(vxlan_dev
);
3383 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
3387 port_obj_info
->handled
= true;
3389 bridge_device
= mlxsw_sp_bridge_device_find(mlxsw_sp
->bridge
, br_dev
);
3393 if (!bridge_device
->vlan_enabled
)
3396 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++)
3397 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp
, bridge_device
,
3402 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device
*vxlan_dev
,
3403 struct switchdev_notifier_port_obj_info
*
3408 switch (port_obj_info
->obj
->id
) {
3409 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
3410 err
= mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev
,
3421 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device
*vxlan_dev
,
3422 struct switchdev_notifier_port_obj_info
*
3425 switch (port_obj_info
->obj
->id
) {
3426 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
3427 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev
, port_obj_info
);
3434 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block
*unused
,
3435 unsigned long event
, void *ptr
)
3437 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
3441 case SWITCHDEV_PORT_OBJ_ADD
:
3442 if (netif_is_vxlan(dev
))
3443 err
= mlxsw_sp_switchdev_handle_vxlan_obj_add(dev
, ptr
);
3445 err
= switchdev_handle_port_obj_add(dev
, ptr
,
3446 mlxsw_sp_port_dev_check
,
3447 mlxsw_sp_port_obj_add
);
3448 return notifier_from_errno(err
);
3449 case SWITCHDEV_PORT_OBJ_DEL
:
3450 if (netif_is_vxlan(dev
))
3451 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev
, ptr
);
3453 err
= switchdev_handle_port_obj_del(dev
, ptr
,
3454 mlxsw_sp_port_dev_check
,
3455 mlxsw_sp_port_obj_del
);
3456 return notifier_from_errno(err
);
3462 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier
= {
3463 .notifier_call
= mlxsw_sp_switchdev_blocking_event
,
3467 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port
*bridge_port
)
3469 return bridge_port
->stp_state
;
3472 static int mlxsw_sp_fdb_init(struct mlxsw_sp
*mlxsw_sp
)
3474 struct mlxsw_sp_bridge
*bridge
= mlxsw_sp
->bridge
;
3475 struct notifier_block
*nb
;
3478 err
= mlxsw_sp_ageing_set(mlxsw_sp
, MLXSW_SP_DEFAULT_AGEING_TIME
);
3480 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to set default ageing time\n");
3484 err
= register_switchdev_notifier(&mlxsw_sp_switchdev_notifier
);
3486 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to register switchdev notifier\n");
3490 nb
= &mlxsw_sp_switchdev_blocking_notifier
;
3491 err
= register_switchdev_blocking_notifier(nb
);
3493 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to register switchdev blocking notifier\n");
3494 goto err_register_switchdev_blocking_notifier
;
3497 INIT_DELAYED_WORK(&bridge
->fdb_notify
.dw
, mlxsw_sp_fdb_notify_work
);
3498 bridge
->fdb_notify
.interval
= MLXSW_SP_DEFAULT_LEARNING_INTERVAL
;
3499 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp
);
3502 err_register_switchdev_blocking_notifier
:
3503 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier
);
3507 static void mlxsw_sp_fdb_fini(struct mlxsw_sp
*mlxsw_sp
)
3509 struct notifier_block
*nb
;
3511 cancel_delayed_work_sync(&mlxsw_sp
->bridge
->fdb_notify
.dw
);
3513 nb
= &mlxsw_sp_switchdev_blocking_notifier
;
3514 unregister_switchdev_blocking_notifier(nb
);
3516 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier
);
3519 int mlxsw_sp_switchdev_init(struct mlxsw_sp
*mlxsw_sp
)
3521 struct mlxsw_sp_bridge
*bridge
;
3523 bridge
= kzalloc(sizeof(*mlxsw_sp
->bridge
), GFP_KERNEL
);
3526 mlxsw_sp
->bridge
= bridge
;
3527 bridge
->mlxsw_sp
= mlxsw_sp
;
3529 INIT_LIST_HEAD(&mlxsw_sp
->bridge
->bridges_list
);
3531 bridge
->bridge_8021q_ops
= &mlxsw_sp_bridge_8021q_ops
;
3532 bridge
->bridge_8021d_ops
= &mlxsw_sp_bridge_8021d_ops
;
3534 return mlxsw_sp_fdb_init(mlxsw_sp
);
3537 void mlxsw_sp_switchdev_fini(struct mlxsw_sp
*mlxsw_sp
)
3539 mlxsw_sp_fdb_fini(mlxsw_sp
);
3540 WARN_ON(!list_empty(&mlxsw_sp
->bridge
->bridges_list
));
3541 kfree(mlxsw_sp
->bridge
);
3544 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
3546 mlxsw_sp_port
->dev
->switchdev_ops
= &mlxsw_sp_port_switchdev_ops
;
3549 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port
*mlxsw_sp_port
)