1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3 
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>
19 
20 #include "spectrum_span.h"
21 #include "spectrum_switchdev.h"
22 #include "spectrum.h"
23 #include "core.h"
24 #include "reg.h"
25 
26 struct mlxsw_sp_bridge_ops;
27 
28 struct mlxsw_sp_bridge {
29 	struct mlxsw_sp *mlxsw_sp;
30 	struct {
31 		struct delayed_work dw;
32 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
33 		unsigned int interval; /* ms */
34 	} fdb_notify;
35 #define MLXSW_SP_MIN_AGEING_TIME 10
36 #define MLXSW_SP_MAX_AGEING_TIME 1000000
37 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
38 	u32 ageing_time;
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;
44 	const struct mlxsw_sp_bridge_ops *bridge_8021ad_ops;
45 };
46 
47 struct mlxsw_sp_bridge_device {
48 	struct net_device *dev;
49 	struct list_head list;
50 	struct list_head ports_list;
51 	struct list_head mids_list;
52 	u8 vlan_enabled:1,
53 	   multicast_enabled:1,
54 	   mrouter:1;
55 	const struct mlxsw_sp_bridge_ops *ops;
56 };
57 
58 struct mlxsw_sp_bridge_port {
59 	struct net_device *dev;
60 	struct mlxsw_sp_bridge_device *bridge_device;
61 	struct list_head list;
62 	struct list_head vlans_list;
63 	unsigned int ref_count;
64 	u8 stp_state;
65 	unsigned long flags;
66 	bool mrouter;
67 	bool lagged;
68 	union {
69 		u16 lag_id;
70 		u16 system_port;
71 	};
72 };
73 
74 struct mlxsw_sp_bridge_vlan {
75 	struct list_head list;
76 	struct list_head port_vlan_list;
77 	u16 vid;
78 };
79 
80 struct mlxsw_sp_bridge_ops {
81 	int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
82 			 struct mlxsw_sp_bridge_port *bridge_port,
83 			 struct mlxsw_sp_port *mlxsw_sp_port,
84 			 struct netlink_ext_ack *extack);
85 	void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
86 			   struct mlxsw_sp_bridge_port *bridge_port,
87 			   struct mlxsw_sp_port *mlxsw_sp_port);
88 	int (*vxlan_join)(struct mlxsw_sp_bridge_device *bridge_device,
89 			  const struct net_device *vxlan_dev, u16 vid,
90 			  struct netlink_ext_ack *extack);
91 	struct mlxsw_sp_fid *
92 		(*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
93 			   u16 vid, struct netlink_ext_ack *extack);
94 	struct mlxsw_sp_fid *
95 		(*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
96 			      u16 vid);
97 	u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
98 		       const struct mlxsw_sp_fid *fid);
99 };
100 
101 struct mlxsw_sp_switchdev_ops {
102 	void (*init)(struct mlxsw_sp *mlxsw_sp);
103 };
104 
105 static int
106 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
107 			       struct mlxsw_sp_bridge_port *bridge_port,
108 			       u16 fid_index);
109 
110 static void
111 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
112 			       struct mlxsw_sp_bridge_port *bridge_port);
113 
114 static void
115 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
116 				   struct mlxsw_sp_bridge_device
117 				   *bridge_device);
118 
119 static void
120 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
121 				 struct mlxsw_sp_bridge_port *bridge_port,
122 				 bool add);
123 
124 static struct mlxsw_sp_bridge_device *
125 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
126 			    const struct net_device *br_dev)
127 {
128 	struct mlxsw_sp_bridge_device *bridge_device;
129 
130 	list_for_each_entry(bridge_device, &bridge->bridges_list, list)
131 		if (bridge_device->dev == br_dev)
132 			return bridge_device;
133 
134 	return NULL;
135 }
136 
137 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
138 					 const struct net_device *br_dev)
139 {
140 	return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
141 }
142 
143 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
144 						    struct netdev_nested_priv *priv)
145 {
146 	struct mlxsw_sp *mlxsw_sp = priv->data;
147 
148 	mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
149 	return 0;
150 }
151 
152 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
153 						struct net_device *dev)
154 {
155 	struct netdev_nested_priv priv = {
156 		.data = (void *)mlxsw_sp,
157 	};
158 
159 	mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
160 	netdev_walk_all_upper_dev_rcu(dev,
161 				      mlxsw_sp_bridge_device_upper_rif_destroy,
162 				      &priv);
163 }
164 
165 static int mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge *bridge,
166 					     struct net_device *br_dev,
167 					     struct netlink_ext_ack *extack)
168 {
169 	struct net_device *dev, *stop_dev;
170 	struct list_head *iter;
171 	int err;
172 
173 	netdev_for_each_lower_dev(br_dev, dev, iter) {
174 		if (netif_is_vxlan(dev) && netif_running(dev)) {
175 			err = mlxsw_sp_bridge_vxlan_join(bridge->mlxsw_sp,
176 							 br_dev, dev, 0,
177 							 extack);
178 			if (err) {
179 				stop_dev = dev;
180 				goto err_vxlan_join;
181 			}
182 		}
183 	}
184 
185 	return 0;
186 
187 err_vxlan_join:
188 	netdev_for_each_lower_dev(br_dev, dev, iter) {
189 		if (netif_is_vxlan(dev) && netif_running(dev)) {
190 			if (stop_dev == dev)
191 				break;
192 			mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
193 		}
194 	}
195 	return err;
196 }
197 
198 static void mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge *bridge,
199 					      struct net_device *br_dev)
200 {
201 	struct net_device *dev;
202 	struct list_head *iter;
203 
204 	netdev_for_each_lower_dev(br_dev, dev, iter) {
205 		if (netif_is_vxlan(dev) && netif_running(dev))
206 			mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
207 	}
208 }
209 
210 static struct mlxsw_sp_bridge_device *
211 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
212 			      struct net_device *br_dev,
213 			      struct netlink_ext_ack *extack)
214 {
215 	struct device *dev = bridge->mlxsw_sp->bus_info->dev;
216 	struct mlxsw_sp_bridge_device *bridge_device;
217 	bool vlan_enabled = br_vlan_enabled(br_dev);
218 	int err;
219 
220 	if (vlan_enabled && bridge->vlan_enabled_exists) {
221 		dev_err(dev, "Only one VLAN-aware bridge is supported\n");
222 		NL_SET_ERR_MSG_MOD(extack, "Only one VLAN-aware bridge is supported");
223 		return ERR_PTR(-EINVAL);
224 	}
225 
226 	bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
227 	if (!bridge_device)
228 		return ERR_PTR(-ENOMEM);
229 
230 	bridge_device->dev = br_dev;
231 	bridge_device->vlan_enabled = vlan_enabled;
232 	bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
233 	bridge_device->mrouter = br_multicast_router(br_dev);
234 	INIT_LIST_HEAD(&bridge_device->ports_list);
235 	if (vlan_enabled) {
236 		u16 proto;
237 
238 		bridge->vlan_enabled_exists = true;
239 		br_vlan_get_proto(br_dev, &proto);
240 		if (proto == ETH_P_8021AD)
241 			bridge_device->ops = bridge->bridge_8021ad_ops;
242 		else
243 			bridge_device->ops = bridge->bridge_8021q_ops;
244 	} else {
245 		bridge_device->ops = bridge->bridge_8021d_ops;
246 	}
247 	INIT_LIST_HEAD(&bridge_device->mids_list);
248 	list_add(&bridge_device->list, &bridge->bridges_list);
249 
250 	/* It is possible we already have VXLAN devices enslaved to the bridge.
251 	 * In which case, we need to replay their configuration as if they were
252 	 * just now enslaved to the bridge.
253 	 */
254 	err = mlxsw_sp_bridge_device_vxlan_init(bridge, br_dev, extack);
255 	if (err)
256 		goto err_vxlan_init;
257 
258 	return bridge_device;
259 
260 err_vxlan_init:
261 	list_del(&bridge_device->list);
262 	if (bridge_device->vlan_enabled)
263 		bridge->vlan_enabled_exists = false;
264 	kfree(bridge_device);
265 	return ERR_PTR(err);
266 }
267 
268 static void
269 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
270 			       struct mlxsw_sp_bridge_device *bridge_device)
271 {
272 	mlxsw_sp_bridge_device_vxlan_fini(bridge, bridge_device->dev);
273 	mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
274 					    bridge_device->dev);
275 	list_del(&bridge_device->list);
276 	if (bridge_device->vlan_enabled)
277 		bridge->vlan_enabled_exists = false;
278 	WARN_ON(!list_empty(&bridge_device->ports_list));
279 	WARN_ON(!list_empty(&bridge_device->mids_list));
280 	kfree(bridge_device);
281 }
282 
283 static struct mlxsw_sp_bridge_device *
284 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
285 			   struct net_device *br_dev,
286 			   struct netlink_ext_ack *extack)
287 {
288 	struct mlxsw_sp_bridge_device *bridge_device;
289 
290 	bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
291 	if (bridge_device)
292 		return bridge_device;
293 
294 	return mlxsw_sp_bridge_device_create(bridge, br_dev, extack);
295 }
296 
297 static void
298 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
299 			   struct mlxsw_sp_bridge_device *bridge_device)
300 {
301 	if (list_empty(&bridge_device->ports_list))
302 		mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
303 }
304 
305 static struct mlxsw_sp_bridge_port *
306 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
307 			    const struct net_device *brport_dev)
308 {
309 	struct mlxsw_sp_bridge_port *bridge_port;
310 
311 	list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
312 		if (bridge_port->dev == brport_dev)
313 			return bridge_port;
314 	}
315 
316 	return NULL;
317 }
318 
319 struct mlxsw_sp_bridge_port *
320 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
321 			  struct net_device *brport_dev)
322 {
323 	struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
324 	struct mlxsw_sp_bridge_device *bridge_device;
325 
326 	if (!br_dev)
327 		return NULL;
328 
329 	bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
330 	if (!bridge_device)
331 		return NULL;
332 
333 	return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
334 }
335 
336 static struct mlxsw_sp_bridge_port *
337 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
338 			    struct net_device *brport_dev,
339 			    struct netlink_ext_ack *extack)
340 {
341 	struct mlxsw_sp_bridge_port *bridge_port;
342 	struct mlxsw_sp_port *mlxsw_sp_port;
343 	int err;
344 
345 	bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
346 	if (!bridge_port)
347 		return ERR_PTR(-ENOMEM);
348 
349 	mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
350 	bridge_port->lagged = mlxsw_sp_port->lagged;
351 	if (bridge_port->lagged)
352 		bridge_port->lag_id = mlxsw_sp_port->lag_id;
353 	else
354 		bridge_port->system_port = mlxsw_sp_port->local_port;
355 	bridge_port->dev = brport_dev;
356 	bridge_port->bridge_device = bridge_device;
357 	bridge_port->stp_state = BR_STATE_DISABLED;
358 	bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
359 			     BR_MCAST_FLOOD;
360 	INIT_LIST_HEAD(&bridge_port->vlans_list);
361 	list_add(&bridge_port->list, &bridge_device->ports_list);
362 	bridge_port->ref_count = 1;
363 
364 	err = switchdev_bridge_port_offload(brport_dev, mlxsw_sp_port->dev,
365 					    NULL, NULL, NULL, false, extack);
366 	if (err)
367 		goto err_switchdev_offload;
368 
369 	return bridge_port;
370 
371 err_switchdev_offload:
372 	list_del(&bridge_port->list);
373 	kfree(bridge_port);
374 	return ERR_PTR(err);
375 }
376 
377 static void
378 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
379 {
380 	switchdev_bridge_port_unoffload(bridge_port->dev, NULL, NULL, NULL);
381 	list_del(&bridge_port->list);
382 	WARN_ON(!list_empty(&bridge_port->vlans_list));
383 	kfree(bridge_port);
384 }
385 
386 static struct mlxsw_sp_bridge_port *
387 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
388 			 struct net_device *brport_dev,
389 			 struct netlink_ext_ack *extack)
390 {
391 	struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
392 	struct mlxsw_sp_bridge_device *bridge_device;
393 	struct mlxsw_sp_bridge_port *bridge_port;
394 	int err;
395 
396 	bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
397 	if (bridge_port) {
398 		bridge_port->ref_count++;
399 		return bridge_port;
400 	}
401 
402 	bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev, extack);
403 	if (IS_ERR(bridge_device))
404 		return ERR_CAST(bridge_device);
405 
406 	bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev,
407 						  extack);
408 	if (IS_ERR(bridge_port)) {
409 		err = PTR_ERR(bridge_port);
410 		goto err_bridge_port_create;
411 	}
412 
413 	return bridge_port;
414 
415 err_bridge_port_create:
416 	mlxsw_sp_bridge_device_put(bridge, bridge_device);
417 	return ERR_PTR(err);
418 }
419 
420 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
421 				     struct mlxsw_sp_bridge_port *bridge_port)
422 {
423 	struct mlxsw_sp_bridge_device *bridge_device;
424 
425 	if (--bridge_port->ref_count != 0)
426 		return;
427 	bridge_device = bridge_port->bridge_device;
428 	mlxsw_sp_bridge_port_destroy(bridge_port);
429 	mlxsw_sp_bridge_device_put(bridge, bridge_device);
430 }
431 
432 static struct mlxsw_sp_port_vlan *
433 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
434 				  const struct mlxsw_sp_bridge_device *
435 				  bridge_device,
436 				  u16 vid)
437 {
438 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
439 
440 	list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
441 			    list) {
442 		if (!mlxsw_sp_port_vlan->bridge_port)
443 			continue;
444 		if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
445 		    bridge_device)
446 			continue;
447 		if (bridge_device->vlan_enabled &&
448 		    mlxsw_sp_port_vlan->vid != vid)
449 			continue;
450 		return mlxsw_sp_port_vlan;
451 	}
452 
453 	return NULL;
454 }
455 
456 static struct mlxsw_sp_port_vlan*
457 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
458 			       u16 fid_index)
459 {
460 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
461 
462 	list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
463 			    list) {
464 		struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
465 
466 		if (fid && mlxsw_sp_fid_index(fid) == fid_index)
467 			return mlxsw_sp_port_vlan;
468 	}
469 
470 	return NULL;
471 }
472 
473 static struct mlxsw_sp_bridge_vlan *
474 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
475 			  u16 vid)
476 {
477 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
478 
479 	list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
480 		if (bridge_vlan->vid == vid)
481 			return bridge_vlan;
482 	}
483 
484 	return NULL;
485 }
486 
487 static struct mlxsw_sp_bridge_vlan *
488 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
489 {
490 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
491 
492 	bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
493 	if (!bridge_vlan)
494 		return NULL;
495 
496 	INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
497 	bridge_vlan->vid = vid;
498 	list_add(&bridge_vlan->list, &bridge_port->vlans_list);
499 
500 	return bridge_vlan;
501 }
502 
503 static void
504 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
505 {
506 	list_del(&bridge_vlan->list);
507 	WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
508 	kfree(bridge_vlan);
509 }
510 
511 static struct mlxsw_sp_bridge_vlan *
512 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
513 {
514 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
515 
516 	bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
517 	if (bridge_vlan)
518 		return bridge_vlan;
519 
520 	return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
521 }
522 
523 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
524 {
525 	if (list_empty(&bridge_vlan->port_vlan_list))
526 		mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
527 }
528 
529 static int
530 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
531 				  struct mlxsw_sp_bridge_vlan *bridge_vlan,
532 				  u8 state)
533 {
534 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
535 
536 	list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
537 			    bridge_vlan_node) {
538 		if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
539 			continue;
540 		return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
541 						 bridge_vlan->vid, state);
542 	}
543 
544 	return 0;
545 }
546 
547 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
548 					    struct net_device *orig_dev,
549 					    u8 state)
550 {
551 	struct mlxsw_sp_bridge_port *bridge_port;
552 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
553 	int err;
554 
555 	/* It's possible we failed to enslave the port, yet this
556 	 * operation is executed due to it being deferred.
557 	 */
558 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
559 						orig_dev);
560 	if (!bridge_port)
561 		return 0;
562 
563 	list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
564 		err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
565 							bridge_vlan, state);
566 		if (err)
567 			goto err_port_bridge_vlan_stp_set;
568 	}
569 
570 	bridge_port->stp_state = state;
571 
572 	return 0;
573 
574 err_port_bridge_vlan_stp_set:
575 	list_for_each_entry_continue_reverse(bridge_vlan,
576 					     &bridge_port->vlans_list, list)
577 		mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
578 						  bridge_port->stp_state);
579 	return err;
580 }
581 
582 static int
583 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
584 				    struct mlxsw_sp_bridge_vlan *bridge_vlan,
585 				    enum mlxsw_sp_flood_type packet_type,
586 				    bool member)
587 {
588 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
589 
590 	list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
591 			    bridge_vlan_node) {
592 		if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
593 			continue;
594 		return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
595 					      packet_type,
596 					      mlxsw_sp_port->local_port,
597 					      member);
598 	}
599 
600 	return 0;
601 }
602 
603 static int
604 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
605 				     struct mlxsw_sp_bridge_port *bridge_port,
606 				     enum mlxsw_sp_flood_type packet_type,
607 				     bool member)
608 {
609 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
610 	int err;
611 
612 	list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
613 		err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
614 							  bridge_vlan,
615 							  packet_type,
616 							  member);
617 		if (err)
618 			goto err_port_bridge_vlan_flood_set;
619 	}
620 
621 	return 0;
622 
623 err_port_bridge_vlan_flood_set:
624 	list_for_each_entry_continue_reverse(bridge_vlan,
625 					     &bridge_port->vlans_list, list)
626 		mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
627 						    packet_type, !member);
628 	return err;
629 }
630 
631 static int
632 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
633 				       struct mlxsw_sp_bridge_vlan *bridge_vlan,
634 				       bool set)
635 {
636 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
637 	u16 vid = bridge_vlan->vid;
638 
639 	list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
640 			    bridge_vlan_node) {
641 		if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
642 			continue;
643 		return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
644 	}
645 
646 	return 0;
647 }
648 
649 static int
650 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
651 				  struct mlxsw_sp_bridge_port *bridge_port,
652 				  bool set)
653 {
654 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
655 	int err;
656 
657 	list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
658 		err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
659 							     bridge_vlan, set);
660 		if (err)
661 			goto err_port_bridge_vlan_learning_set;
662 	}
663 
664 	return 0;
665 
666 err_port_bridge_vlan_learning_set:
667 	list_for_each_entry_continue_reverse(bridge_vlan,
668 					     &bridge_port->vlans_list, list)
669 		mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
670 						       bridge_vlan, !set);
671 	return err;
672 }
673 
674 static int
675 mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
676 				    struct switchdev_brport_flags flags)
677 {
678 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
679 		return -EINVAL;
680 
681 	return 0;
682 }
683 
684 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
685 					   struct net_device *orig_dev,
686 					   struct switchdev_brport_flags flags)
687 {
688 	struct mlxsw_sp_bridge_port *bridge_port;
689 	int err;
690 
691 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
692 						orig_dev);
693 	if (!bridge_port)
694 		return 0;
695 
696 	if (flags.mask & BR_FLOOD) {
697 		err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
698 							   bridge_port,
699 							   MLXSW_SP_FLOOD_TYPE_UC,
700 							   flags.val & BR_FLOOD);
701 		if (err)
702 			return err;
703 	}
704 
705 	if (flags.mask & BR_LEARNING) {
706 		err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port,
707 							bridge_port,
708 							flags.val & BR_LEARNING);
709 		if (err)
710 			return err;
711 	}
712 
713 	if (bridge_port->bridge_device->multicast_enabled)
714 		goto out;
715 
716 	if (flags.mask & BR_MCAST_FLOOD) {
717 		err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
718 							   bridge_port,
719 							   MLXSW_SP_FLOOD_TYPE_MC,
720 							   flags.val & BR_MCAST_FLOOD);
721 		if (err)
722 			return err;
723 	}
724 
725 out:
726 	memcpy(&bridge_port->flags, &flags.val, sizeof(flags.val));
727 	return 0;
728 }
729 
730 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
731 {
732 	char sfdat_pl[MLXSW_REG_SFDAT_LEN];
733 	int err;
734 
735 	mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
736 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
737 	if (err)
738 		return err;
739 	mlxsw_sp->bridge->ageing_time = ageing_time;
740 	return 0;
741 }
742 
743 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
744 					    unsigned long ageing_clock_t)
745 {
746 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
747 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
748 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
749 
750 	if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
751 	    ageing_time > MLXSW_SP_MAX_AGEING_TIME)
752 		return -ERANGE;
753 
754 	return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
755 }
756 
757 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
758 					  struct net_device *orig_dev,
759 					  bool vlan_enabled)
760 {
761 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
762 	struct mlxsw_sp_bridge_device *bridge_device;
763 
764 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
765 	if (WARN_ON(!bridge_device))
766 		return -EINVAL;
767 
768 	if (bridge_device->vlan_enabled == vlan_enabled)
769 		return 0;
770 
771 	netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
772 	return -EINVAL;
773 }
774 
775 static int mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port *mlxsw_sp_port,
776 						struct net_device *orig_dev,
777 						u16 vlan_proto)
778 {
779 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
780 	struct mlxsw_sp_bridge_device *bridge_device;
781 
782 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
783 	if (WARN_ON(!bridge_device))
784 		return -EINVAL;
785 
786 	netdev_err(bridge_device->dev, "VLAN protocol can't be changed on existing bridge\n");
787 	return -EINVAL;
788 }
789 
790 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
791 					  struct net_device *orig_dev,
792 					  bool is_port_mrouter)
793 {
794 	struct mlxsw_sp_bridge_port *bridge_port;
795 	int err;
796 
797 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
798 						orig_dev);
799 	if (!bridge_port)
800 		return 0;
801 
802 	if (!bridge_port->bridge_device->multicast_enabled)
803 		goto out;
804 
805 	err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
806 						   MLXSW_SP_FLOOD_TYPE_MC,
807 						   is_port_mrouter);
808 	if (err)
809 		return err;
810 
811 	mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
812 					 is_port_mrouter);
813 out:
814 	bridge_port->mrouter = is_port_mrouter;
815 	return 0;
816 }
817 
818 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
819 {
820 	const struct mlxsw_sp_bridge_device *bridge_device;
821 
822 	bridge_device = bridge_port->bridge_device;
823 	return bridge_device->multicast_enabled ? bridge_port->mrouter :
824 					bridge_port->flags & BR_MCAST_FLOOD;
825 }
826 
827 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
828 					 struct net_device *orig_dev,
829 					 bool mc_disabled)
830 {
831 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
832 	struct mlxsw_sp_bridge_device *bridge_device;
833 	struct mlxsw_sp_bridge_port *bridge_port;
834 	int err;
835 
836 	/* It's possible we failed to enslave the port, yet this
837 	 * operation is executed due to it being deferred.
838 	 */
839 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
840 	if (!bridge_device)
841 		return 0;
842 
843 	if (bridge_device->multicast_enabled != !mc_disabled) {
844 		bridge_device->multicast_enabled = !mc_disabled;
845 		mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
846 						   bridge_device);
847 	}
848 
849 	list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
850 		enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
851 		bool member = mlxsw_sp_mc_flood(bridge_port);
852 
853 		err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
854 							   bridge_port,
855 							   packet_type, member);
856 		if (err)
857 			return err;
858 	}
859 
860 	bridge_device->multicast_enabled = !mc_disabled;
861 
862 	return 0;
863 }
864 
865 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
866 					 u16 mid_idx, bool add)
867 {
868 	char *smid2_pl;
869 	int err;
870 
871 	smid2_pl = kmalloc(MLXSW_REG_SMID2_LEN, GFP_KERNEL);
872 	if (!smid2_pl)
873 		return -ENOMEM;
874 
875 	mlxsw_reg_smid2_pack(smid2_pl, mid_idx,
876 			     mlxsw_sp_router_port(mlxsw_sp), add);
877 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid2), smid2_pl);
878 	kfree(smid2_pl);
879 	return err;
880 }
881 
882 static void
883 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
884 				   struct mlxsw_sp_bridge_device *bridge_device,
885 				   bool add)
886 {
887 	struct mlxsw_sp_mid *mid;
888 
889 	list_for_each_entry(mid, &bridge_device->mids_list, list)
890 		mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
891 }
892 
893 static int
894 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
895 				  struct net_device *orig_dev,
896 				  bool is_mrouter)
897 {
898 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
899 	struct mlxsw_sp_bridge_device *bridge_device;
900 
901 	/* It's possible we failed to enslave the port, yet this
902 	 * operation is executed due to it being deferred.
903 	 */
904 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
905 	if (!bridge_device)
906 		return 0;
907 
908 	if (bridge_device->mrouter != is_mrouter)
909 		mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
910 						   is_mrouter);
911 	bridge_device->mrouter = is_mrouter;
912 	return 0;
913 }
914 
915 static int mlxsw_sp_port_attr_set(struct net_device *dev, const void *ctx,
916 				  const struct switchdev_attr *attr,
917 				  struct netlink_ext_ack *extack)
918 {
919 	struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
920 	int err;
921 
922 	switch (attr->id) {
923 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
924 		err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port,
925 						       attr->orig_dev,
926 						       attr->u.stp_state);
927 		break;
928 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
929 		err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
930 							  attr->u.brport_flags);
931 		break;
932 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
933 		err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port,
934 						      attr->orig_dev,
935 						      attr->u.brport_flags);
936 		break;
937 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
938 		err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port,
939 						       attr->u.ageing_time);
940 		break;
941 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
942 		err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port,
943 						     attr->orig_dev,
944 						     attr->u.vlan_filtering);
945 		break;
946 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL:
947 		err = mlxsw_sp_port_attr_br_vlan_proto_set(mlxsw_sp_port,
948 							   attr->orig_dev,
949 							   attr->u.vlan_protocol);
950 		break;
951 	case SWITCHDEV_ATTR_ID_PORT_MROUTER:
952 		err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port,
953 						     attr->orig_dev,
954 						     attr->u.mrouter);
955 		break;
956 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
957 		err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port,
958 						    attr->orig_dev,
959 						    attr->u.mc_disabled);
960 		break;
961 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
962 		err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port,
963 							attr->orig_dev,
964 							attr->u.mrouter);
965 		break;
966 	default:
967 		err = -EOPNOTSUPP;
968 		break;
969 	}
970 
971 	mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
972 
973 	return err;
974 }
975 
976 static int
977 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
978 			    struct mlxsw_sp_bridge_port *bridge_port,
979 			    struct netlink_ext_ack *extack)
980 {
981 	struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
982 	struct mlxsw_sp_bridge_device *bridge_device;
983 	u16 local_port = mlxsw_sp_port->local_port;
984 	u16 vid = mlxsw_sp_port_vlan->vid;
985 	struct mlxsw_sp_fid *fid;
986 	int err;
987 
988 	bridge_device = bridge_port->bridge_device;
989 	fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
990 	if (IS_ERR(fid))
991 		return PTR_ERR(fid);
992 
993 	err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
994 				     bridge_port->flags & BR_FLOOD);
995 	if (err)
996 		goto err_fid_uc_flood_set;
997 
998 	err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
999 				     mlxsw_sp_mc_flood(bridge_port));
1000 	if (err)
1001 		goto err_fid_mc_flood_set;
1002 
1003 	err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
1004 				     true);
1005 	if (err)
1006 		goto err_fid_bc_flood_set;
1007 
1008 	err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
1009 	if (err)
1010 		goto err_fid_port_vid_map;
1011 
1012 	mlxsw_sp_port_vlan->fid = fid;
1013 
1014 	return 0;
1015 
1016 err_fid_port_vid_map:
1017 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1018 err_fid_bc_flood_set:
1019 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1020 err_fid_mc_flood_set:
1021 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1022 err_fid_uc_flood_set:
1023 	mlxsw_sp_fid_put(fid);
1024 	return err;
1025 }
1026 
1027 static void
1028 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1029 {
1030 	struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1031 	struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1032 	u16 local_port = mlxsw_sp_port->local_port;
1033 	u16 vid = mlxsw_sp_port_vlan->vid;
1034 
1035 	mlxsw_sp_port_vlan->fid = NULL;
1036 	mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
1037 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1038 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1039 	mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1040 	mlxsw_sp_fid_put(fid);
1041 }
1042 
1043 static u16
1044 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1045 			     u16 vid, bool is_pvid)
1046 {
1047 	if (is_pvid)
1048 		return vid;
1049 	else if (mlxsw_sp_port->pvid == vid)
1050 		return 0;	/* Dis-allow untagged packets */
1051 	else
1052 		return mlxsw_sp_port->pvid;
1053 }
1054 
1055 static int
1056 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1057 			       struct mlxsw_sp_bridge_port *bridge_port,
1058 			       struct netlink_ext_ack *extack)
1059 {
1060 	struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1061 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
1062 	u16 vid = mlxsw_sp_port_vlan->vid;
1063 	int err;
1064 
1065 	/* No need to continue if only VLAN flags were changed */
1066 	if (mlxsw_sp_port_vlan->bridge_port)
1067 		return 0;
1068 
1069 	err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
1070 					  extack);
1071 	if (err)
1072 		return err;
1073 
1074 	err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1075 					     bridge_port->flags & BR_LEARNING);
1076 	if (err)
1077 		goto err_port_vid_learning_set;
1078 
1079 	err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1080 					bridge_port->stp_state);
1081 	if (err)
1082 		goto err_port_vid_stp_set;
1083 
1084 	bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1085 	if (!bridge_vlan) {
1086 		err = -ENOMEM;
1087 		goto err_bridge_vlan_get;
1088 	}
1089 
1090 	list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1091 		 &bridge_vlan->port_vlan_list);
1092 
1093 	mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1094 				 bridge_port->dev, extack);
1095 	mlxsw_sp_port_vlan->bridge_port = bridge_port;
1096 
1097 	return 0;
1098 
1099 err_bridge_vlan_get:
1100 	mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1101 err_port_vid_stp_set:
1102 	mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1103 err_port_vid_learning_set:
1104 	mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1105 	return err;
1106 }
1107 
1108 void
1109 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1110 {
1111 	struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1112 	struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1113 	struct mlxsw_sp_bridge_vlan *bridge_vlan;
1114 	struct mlxsw_sp_bridge_port *bridge_port;
1115 	u16 vid = mlxsw_sp_port_vlan->vid;
1116 	bool last_port, last_vlan;
1117 
1118 	if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1119 		    mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1120 		return;
1121 
1122 	bridge_port = mlxsw_sp_port_vlan->bridge_port;
1123 	last_vlan = list_is_singular(&bridge_port->vlans_list);
1124 	bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1125 	last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1126 
1127 	list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1128 	mlxsw_sp_bridge_vlan_put(bridge_vlan);
1129 	mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1130 	mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1131 	if (last_port)
1132 		mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1133 					       bridge_port,
1134 					       mlxsw_sp_fid_index(fid));
1135 	if (last_vlan)
1136 		mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1137 
1138 	mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1139 
1140 	mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1141 	mlxsw_sp_port_vlan->bridge_port = NULL;
1142 }
1143 
1144 static int
1145 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1146 			      struct mlxsw_sp_bridge_port *bridge_port,
1147 			      u16 vid, bool is_untagged, bool is_pvid,
1148 			      struct netlink_ext_ack *extack)
1149 {
1150 	u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1151 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1152 	u16 old_pvid = mlxsw_sp_port->pvid;
1153 	u16 proto;
1154 	int err;
1155 
1156 	/* The only valid scenario in which a port-vlan already exists, is if
1157 	 * the VLAN flags were changed and the port-vlan is associated with the
1158 	 * correct bridge port
1159 	 */
1160 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1161 	if (mlxsw_sp_port_vlan &&
1162 	    mlxsw_sp_port_vlan->bridge_port != bridge_port)
1163 		return -EEXIST;
1164 
1165 	if (!mlxsw_sp_port_vlan) {
1166 		mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1167 							       vid);
1168 		if (IS_ERR(mlxsw_sp_port_vlan))
1169 			return PTR_ERR(mlxsw_sp_port_vlan);
1170 	}
1171 
1172 	err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1173 				     is_untagged);
1174 	if (err)
1175 		goto err_port_vlan_set;
1176 
1177 	br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1178 	err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1179 	if (err)
1180 		goto err_port_pvid_set;
1181 
1182 	err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1183 					     extack);
1184 	if (err)
1185 		goto err_port_vlan_bridge_join;
1186 
1187 	return 0;
1188 
1189 err_port_vlan_bridge_join:
1190 	mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto);
1191 err_port_pvid_set:
1192 	mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1193 err_port_vlan_set:
1194 	mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1195 	return err;
1196 }
1197 
1198 static int
1199 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1200 				const struct net_device *br_dev,
1201 				const struct switchdev_obj_port_vlan *vlan)
1202 {
1203 	u16 pvid;
1204 
1205 	pvid = mlxsw_sp_rif_vid(mlxsw_sp, br_dev);
1206 	if (!pvid)
1207 		return 0;
1208 
1209 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1210 		if (vlan->vid != pvid) {
1211 			netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1212 			return -EBUSY;
1213 		}
1214 	} else {
1215 		if (vlan->vid == pvid) {
1216 			netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1217 			return -EBUSY;
1218 		}
1219 	}
1220 
1221 	return 0;
1222 }
1223 
1224 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1225 				   const struct switchdev_obj_port_vlan *vlan,
1226 				   struct netlink_ext_ack *extack)
1227 {
1228 	bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1229 	bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1230 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1231 	struct net_device *orig_dev = vlan->obj.orig_dev;
1232 	struct mlxsw_sp_bridge_port *bridge_port;
1233 
1234 	if (netif_is_bridge_master(orig_dev)) {
1235 		int err = 0;
1236 
1237 		if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1238 		    br_vlan_enabled(orig_dev))
1239 			err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1240 							      orig_dev, vlan);
1241 		if (!err)
1242 			err = -EOPNOTSUPP;
1243 		return err;
1244 	}
1245 
1246 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1247 	if (WARN_ON(!bridge_port))
1248 		return -EINVAL;
1249 
1250 	if (!bridge_port->bridge_device->vlan_enabled)
1251 		return 0;
1252 
1253 	return mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1254 					     vlan->vid, flag_untagged,
1255 					     flag_pvid, extack);
1256 }
1257 
1258 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1259 {
1260 	return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1261 			MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1262 }
1263 
1264 static int
1265 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1266 			       struct mlxsw_sp_bridge_port *bridge_port,
1267 			       u16 fid_index)
1268 {
1269 	bool lagged = bridge_port->lagged;
1270 	char sfdf_pl[MLXSW_REG_SFDF_LEN];
1271 	u16 system_port;
1272 
1273 	system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1274 	mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1275 	mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1276 	mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1277 
1278 	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1279 }
1280 
1281 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1282 {
1283 	return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1284 			 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1285 }
1286 
1287 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1288 {
1289 	return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1290 			MLXSW_REG_SFD_OP_WRITE_REMOVE;
1291 }
1292 
1293 static int
1294 mlxsw_sp_port_fdb_tun_uc_op4(struct mlxsw_sp *mlxsw_sp, bool dynamic,
1295 			     const char *mac, u16 fid, __be32 addr, bool adding)
1296 {
1297 	char *sfd_pl;
1298 	u8 num_rec;
1299 	u32 uip;
1300 	int err;
1301 
1302 	sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1303 	if (!sfd_pl)
1304 		return -ENOMEM;
1305 
1306 	uip = be32_to_cpu(addr);
1307 	mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1308 	mlxsw_reg_sfd_uc_tunnel_pack4(sfd_pl, 0,
1309 				      mlxsw_sp_sfd_rec_policy(dynamic), mac,
1310 				      fid, MLXSW_REG_SFD_REC_ACTION_NOP, uip);
1311 	num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1312 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1313 	if (err)
1314 		goto out;
1315 
1316 	if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1317 		err = -EBUSY;
1318 
1319 out:
1320 	kfree(sfd_pl);
1321 	return err;
1322 }
1323 
1324 static int mlxsw_sp_port_fdb_tun_uc_op6_sfd_write(struct mlxsw_sp *mlxsw_sp,
1325 						  const char *mac, u16 fid,
1326 						  u32 kvdl_index, bool adding)
1327 {
1328 	char *sfd_pl;
1329 	u8 num_rec;
1330 	int err;
1331 
1332 	sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1333 	if (!sfd_pl)
1334 		return -ENOMEM;
1335 
1336 	mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1337 	mlxsw_reg_sfd_uc_tunnel_pack6(sfd_pl, 0, mac, fid,
1338 				      MLXSW_REG_SFD_REC_ACTION_NOP, kvdl_index);
1339 	num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1340 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1341 	if (err)
1342 		goto out;
1343 
1344 	if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1345 		err = -EBUSY;
1346 
1347 out:
1348 	kfree(sfd_pl);
1349 	return err;
1350 }
1351 
1352 static int mlxsw_sp_port_fdb_tun_uc_op6_add(struct mlxsw_sp *mlxsw_sp,
1353 					    const char *mac, u16 fid,
1354 					    const struct in6_addr *addr)
1355 {
1356 	u32 kvdl_index;
1357 	int err;
1358 
1359 	err = mlxsw_sp_nve_ipv6_addr_kvdl_set(mlxsw_sp, addr, &kvdl_index);
1360 	if (err)
1361 		return err;
1362 
1363 	err = mlxsw_sp_port_fdb_tun_uc_op6_sfd_write(mlxsw_sp, mac, fid,
1364 						     kvdl_index, true);
1365 	if (err)
1366 		goto err_sfd_write;
1367 
1368 	err = mlxsw_sp_nve_ipv6_addr_map_replace(mlxsw_sp, mac, fid, addr);
1369 	if (err)
1370 		/* Replace can fail only for creating new mapping, so removing
1371 		 * the FDB entry in the error path is OK.
1372 		 */
1373 		goto err_addr_replace;
1374 
1375 	return 0;
1376 
1377 err_addr_replace:
1378 	mlxsw_sp_port_fdb_tun_uc_op6_sfd_write(mlxsw_sp, mac, fid, kvdl_index,
1379 					       false);
1380 err_sfd_write:
1381 	mlxsw_sp_nve_ipv6_addr_kvdl_unset(mlxsw_sp, addr);
1382 	return err;
1383 }
1384 
1385 static void mlxsw_sp_port_fdb_tun_uc_op6_del(struct mlxsw_sp *mlxsw_sp,
1386 					     const char *mac, u16 fid,
1387 					     const struct in6_addr *addr)
1388 {
1389 	mlxsw_sp_nve_ipv6_addr_map_del(mlxsw_sp, mac, fid);
1390 	mlxsw_sp_port_fdb_tun_uc_op6_sfd_write(mlxsw_sp, mac, fid, 0, false);
1391 	mlxsw_sp_nve_ipv6_addr_kvdl_unset(mlxsw_sp, addr);
1392 }
1393 
1394 static int
1395 mlxsw_sp_port_fdb_tun_uc_op6(struct mlxsw_sp *mlxsw_sp, const char *mac,
1396 			     u16 fid, const struct in6_addr *addr, bool adding)
1397 {
1398 	if (adding)
1399 		return mlxsw_sp_port_fdb_tun_uc_op6_add(mlxsw_sp, mac, fid,
1400 							addr);
1401 
1402 	mlxsw_sp_port_fdb_tun_uc_op6_del(mlxsw_sp, mac, fid, addr);
1403 	return 0;
1404 }
1405 
1406 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1407 					  const char *mac, u16 fid,
1408 					  enum mlxsw_sp_l3proto proto,
1409 					  const union mlxsw_sp_l3addr *addr,
1410 					  bool adding, bool dynamic)
1411 {
1412 	switch (proto) {
1413 	case MLXSW_SP_L3_PROTO_IPV4:
1414 		return mlxsw_sp_port_fdb_tun_uc_op4(mlxsw_sp, dynamic, mac, fid,
1415 						    addr->addr4, adding);
1416 	case MLXSW_SP_L3_PROTO_IPV6:
1417 		return mlxsw_sp_port_fdb_tun_uc_op6(mlxsw_sp, mac, fid,
1418 						    &addr->addr6, adding);
1419 	default:
1420 		WARN_ON(1);
1421 		return -EOPNOTSUPP;
1422 	}
1423 }
1424 
1425 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u16 local_port,
1426 				     const char *mac, u16 fid, bool adding,
1427 				     enum mlxsw_reg_sfd_rec_action action,
1428 				     enum mlxsw_reg_sfd_rec_policy policy)
1429 {
1430 	char *sfd_pl;
1431 	u8 num_rec;
1432 	int err;
1433 
1434 	sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1435 	if (!sfd_pl)
1436 		return -ENOMEM;
1437 
1438 	mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1439 	mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1440 	num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1441 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1442 	if (err)
1443 		goto out;
1444 
1445 	if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1446 		err = -EBUSY;
1447 
1448 out:
1449 	kfree(sfd_pl);
1450 	return err;
1451 }
1452 
1453 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u16 local_port,
1454 				   const char *mac, u16 fid, bool adding,
1455 				   bool dynamic)
1456 {
1457 	return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1458 					 MLXSW_REG_SFD_REC_ACTION_NOP,
1459 					 mlxsw_sp_sfd_rec_policy(dynamic));
1460 }
1461 
1462 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1463 			bool adding)
1464 {
1465 	return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1466 					 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1467 					 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1468 }
1469 
1470 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1471 				       const char *mac, u16 fid, u16 lag_vid,
1472 				       bool adding, bool dynamic)
1473 {
1474 	char *sfd_pl;
1475 	u8 num_rec;
1476 	int err;
1477 
1478 	sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1479 	if (!sfd_pl)
1480 		return -ENOMEM;
1481 
1482 	mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1483 	mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1484 				  mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1485 				  lag_vid, lag_id);
1486 	num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1487 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1488 	if (err)
1489 		goto out;
1490 
1491 	if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1492 		err = -EBUSY;
1493 
1494 out:
1495 	kfree(sfd_pl);
1496 	return err;
1497 }
1498 
1499 static int
1500 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1501 		      struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1502 {
1503 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1504 	struct net_device *orig_dev = fdb_info->info.dev;
1505 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1506 	struct mlxsw_sp_bridge_device *bridge_device;
1507 	struct mlxsw_sp_bridge_port *bridge_port;
1508 	u16 fid_index, vid;
1509 
1510 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1511 	if (!bridge_port)
1512 		return -EINVAL;
1513 
1514 	bridge_device = bridge_port->bridge_device;
1515 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1516 							       bridge_device,
1517 							       fdb_info->vid);
1518 	if (!mlxsw_sp_port_vlan)
1519 		return 0;
1520 
1521 	fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1522 	vid = mlxsw_sp_port_vlan->vid;
1523 
1524 	if (!bridge_port->lagged)
1525 		return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1526 					       bridge_port->system_port,
1527 					       fdb_info->addr, fid_index,
1528 					       adding, false);
1529 	else
1530 		return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1531 						   bridge_port->lag_id,
1532 						   fdb_info->addr, fid_index,
1533 						   vid, adding, false);
1534 }
1535 
1536 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1537 				u16 fid, u16 mid_idx, bool adding)
1538 {
1539 	char *sfd_pl;
1540 	u8 num_rec;
1541 	int err;
1542 
1543 	sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1544 	if (!sfd_pl)
1545 		return -ENOMEM;
1546 
1547 	mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1548 	mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1549 			      MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1550 	num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1551 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1552 	if (err)
1553 		goto out;
1554 
1555 	if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1556 		err = -EBUSY;
1557 
1558 out:
1559 	kfree(sfd_pl);
1560 	return err;
1561 }
1562 
1563 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1564 					 long *ports_bitmap,
1565 					 bool set_router_port)
1566 {
1567 	char *smid2_pl;
1568 	int err, i;
1569 
1570 	smid2_pl = kmalloc(MLXSW_REG_SMID2_LEN, GFP_KERNEL);
1571 	if (!smid2_pl)
1572 		return -ENOMEM;
1573 
1574 	mlxsw_reg_smid2_pack(smid2_pl, mid_idx, 0, false);
1575 	for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1576 		if (mlxsw_sp->ports[i])
1577 			mlxsw_reg_smid2_port_mask_set(smid2_pl, i, 1);
1578 	}
1579 
1580 	mlxsw_reg_smid2_port_mask_set(smid2_pl,
1581 				      mlxsw_sp_router_port(mlxsw_sp), 1);
1582 
1583 	for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1584 		mlxsw_reg_smid2_port_set(smid2_pl, i, 1);
1585 
1586 	mlxsw_reg_smid2_port_set(smid2_pl, mlxsw_sp_router_port(mlxsw_sp),
1587 				 set_router_port);
1588 
1589 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid2), smid2_pl);
1590 	kfree(smid2_pl);
1591 	return err;
1592 }
1593 
1594 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1595 				  u16 mid_idx, bool add)
1596 {
1597 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1598 	char *smid2_pl;
1599 	int err;
1600 
1601 	smid2_pl = kmalloc(MLXSW_REG_SMID2_LEN, GFP_KERNEL);
1602 	if (!smid2_pl)
1603 		return -ENOMEM;
1604 
1605 	mlxsw_reg_smid2_pack(smid2_pl, mid_idx, mlxsw_sp_port->local_port, add);
1606 	err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid2), smid2_pl);
1607 	kfree(smid2_pl);
1608 	return err;
1609 }
1610 
1611 static struct
1612 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1613 				const unsigned char *addr,
1614 				u16 fid)
1615 {
1616 	struct mlxsw_sp_mid *mid;
1617 
1618 	list_for_each_entry(mid, &bridge_device->mids_list, list) {
1619 		if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1620 			return mid;
1621 	}
1622 	return NULL;
1623 }
1624 
1625 static void
1626 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1627 				      struct mlxsw_sp_bridge_port *bridge_port,
1628 				      unsigned long *ports_bitmap)
1629 {
1630 	struct mlxsw_sp_port *mlxsw_sp_port;
1631 	u64 max_lag_members, i;
1632 	int lag_id;
1633 
1634 	if (!bridge_port->lagged) {
1635 		set_bit(bridge_port->system_port, ports_bitmap);
1636 	} else {
1637 		max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1638 						     MAX_LAG_MEMBERS);
1639 		lag_id = bridge_port->lag_id;
1640 		for (i = 0; i < max_lag_members; i++) {
1641 			mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1642 								 lag_id, i);
1643 			if (mlxsw_sp_port)
1644 				set_bit(mlxsw_sp_port->local_port,
1645 					ports_bitmap);
1646 		}
1647 	}
1648 }
1649 
1650 static void
1651 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1652 				struct mlxsw_sp_bridge_device *bridge_device,
1653 				struct mlxsw_sp *mlxsw_sp)
1654 {
1655 	struct mlxsw_sp_bridge_port *bridge_port;
1656 
1657 	list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1658 		if (bridge_port->mrouter) {
1659 			mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1660 							      bridge_port,
1661 							      flood_bitmap);
1662 		}
1663 	}
1664 }
1665 
1666 static bool
1667 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1668 			    struct mlxsw_sp_mid *mid,
1669 			    struct mlxsw_sp_bridge_device *bridge_device)
1670 {
1671 	long *flood_bitmap;
1672 	int num_of_ports;
1673 	u16 mid_idx;
1674 	int err;
1675 
1676 	mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1677 				      MLXSW_SP_MID_MAX);
1678 	if (mid_idx == MLXSW_SP_MID_MAX)
1679 		return false;
1680 
1681 	num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1682 	flood_bitmap = bitmap_alloc(num_of_ports, GFP_KERNEL);
1683 	if (!flood_bitmap)
1684 		return false;
1685 
1686 	bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1687 	mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1688 
1689 	mid->mid = mid_idx;
1690 	err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1691 					    bridge_device->mrouter);
1692 	bitmap_free(flood_bitmap);
1693 	if (err)
1694 		return false;
1695 
1696 	err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1697 				   true);
1698 	if (err)
1699 		return false;
1700 
1701 	set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1702 	mid->in_hw = true;
1703 	return true;
1704 }
1705 
1706 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1707 					struct mlxsw_sp_mid *mid)
1708 {
1709 	if (!mid->in_hw)
1710 		return 0;
1711 
1712 	clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1713 	mid->in_hw = false;
1714 	return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1715 				    false);
1716 }
1717 
1718 static struct
1719 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1720 				  struct mlxsw_sp_bridge_device *bridge_device,
1721 				  const unsigned char *addr,
1722 				  u16 fid)
1723 {
1724 	struct mlxsw_sp_mid *mid;
1725 
1726 	mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1727 	if (!mid)
1728 		return NULL;
1729 
1730 	mid->ports_in_mid = bitmap_zalloc(mlxsw_core_max_ports(mlxsw_sp->core),
1731 					  GFP_KERNEL);
1732 	if (!mid->ports_in_mid)
1733 		goto err_ports_in_mid_alloc;
1734 
1735 	ether_addr_copy(mid->addr, addr);
1736 	mid->fid = fid;
1737 	mid->in_hw = false;
1738 
1739 	if (!bridge_device->multicast_enabled)
1740 		goto out;
1741 
1742 	if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1743 		goto err_write_mdb_entry;
1744 
1745 out:
1746 	list_add_tail(&mid->list, &bridge_device->mids_list);
1747 	return mid;
1748 
1749 err_write_mdb_entry:
1750 	bitmap_free(mid->ports_in_mid);
1751 err_ports_in_mid_alloc:
1752 	kfree(mid);
1753 	return NULL;
1754 }
1755 
1756 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1757 					 struct mlxsw_sp_mid *mid)
1758 {
1759 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1760 	int err = 0;
1761 
1762 	clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1763 	if (bitmap_empty(mid->ports_in_mid,
1764 			 mlxsw_core_max_ports(mlxsw_sp->core))) {
1765 		err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1766 		list_del(&mid->list);
1767 		bitmap_free(mid->ports_in_mid);
1768 		kfree(mid);
1769 	}
1770 	return err;
1771 }
1772 
1773 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1774 				 const struct switchdev_obj_port_mdb *mdb)
1775 {
1776 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1777 	struct net_device *orig_dev = mdb->obj.orig_dev;
1778 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1779 	struct net_device *dev = mlxsw_sp_port->dev;
1780 	struct mlxsw_sp_bridge_device *bridge_device;
1781 	struct mlxsw_sp_bridge_port *bridge_port;
1782 	struct mlxsw_sp_mid *mid;
1783 	u16 fid_index;
1784 	int err = 0;
1785 
1786 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1787 	if (!bridge_port)
1788 		return 0;
1789 
1790 	bridge_device = bridge_port->bridge_device;
1791 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1792 							       bridge_device,
1793 							       mdb->vid);
1794 	if (!mlxsw_sp_port_vlan)
1795 		return 0;
1796 
1797 	fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1798 
1799 	mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1800 	if (!mid) {
1801 		mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1802 					  fid_index);
1803 		if (!mid) {
1804 			netdev_err(dev, "Unable to allocate MC group\n");
1805 			return -ENOMEM;
1806 		}
1807 	}
1808 	set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1809 
1810 	if (!bridge_device->multicast_enabled)
1811 		return 0;
1812 
1813 	if (bridge_port->mrouter)
1814 		return 0;
1815 
1816 	err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1817 	if (err) {
1818 		netdev_err(dev, "Unable to set SMID\n");
1819 		goto err_out;
1820 	}
1821 
1822 	return 0;
1823 
1824 err_out:
1825 	mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1826 	return err;
1827 }
1828 
1829 static void
1830 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1831 				   struct mlxsw_sp_bridge_device
1832 				   *bridge_device)
1833 {
1834 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1835 	struct mlxsw_sp_mid *mid;
1836 	bool mc_enabled;
1837 
1838 	mc_enabled = bridge_device->multicast_enabled;
1839 
1840 	list_for_each_entry(mid, &bridge_device->mids_list, list) {
1841 		if (mc_enabled)
1842 			mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1843 						    bridge_device);
1844 		else
1845 			mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1846 	}
1847 }
1848 
1849 static void
1850 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1851 				 struct mlxsw_sp_bridge_port *bridge_port,
1852 				 bool add)
1853 {
1854 	struct mlxsw_sp_bridge_device *bridge_device;
1855 	struct mlxsw_sp_mid *mid;
1856 
1857 	bridge_device = bridge_port->bridge_device;
1858 
1859 	list_for_each_entry(mid, &bridge_device->mids_list, list) {
1860 		if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1861 			mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1862 	}
1863 }
1864 
1865 static int mlxsw_sp_port_obj_add(struct net_device *dev, const void *ctx,
1866 				 const struct switchdev_obj *obj,
1867 				 struct netlink_ext_ack *extack)
1868 {
1869 	struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1870 	const struct switchdev_obj_port_vlan *vlan;
1871 	int err = 0;
1872 
1873 	switch (obj->id) {
1874 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1875 		vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1876 
1877 		err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, extack);
1878 
1879 		/* The event is emitted before the changes are actually
1880 		 * applied to the bridge. Therefore schedule the respin
1881 		 * call for later, so that the respin logic sees the
1882 		 * updated bridge state.
1883 		 */
1884 		mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1885 		break;
1886 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1887 		err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1888 					    SWITCHDEV_OBJ_PORT_MDB(obj));
1889 		break;
1890 	default:
1891 		err = -EOPNOTSUPP;
1892 		break;
1893 	}
1894 
1895 	return err;
1896 }
1897 
1898 static void
1899 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1900 			      struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1901 {
1902 	u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1903 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1904 	u16 proto;
1905 
1906 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1907 	if (WARN_ON(!mlxsw_sp_port_vlan))
1908 		return;
1909 
1910 	mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1911 	br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1912 	mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1913 	mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1914 	mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1915 }
1916 
1917 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1918 				   const struct switchdev_obj_port_vlan *vlan)
1919 {
1920 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1921 	struct net_device *orig_dev = vlan->obj.orig_dev;
1922 	struct mlxsw_sp_bridge_port *bridge_port;
1923 
1924 	if (netif_is_bridge_master(orig_dev))
1925 		return -EOPNOTSUPP;
1926 
1927 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1928 	if (WARN_ON(!bridge_port))
1929 		return -EINVAL;
1930 
1931 	if (!bridge_port->bridge_device->vlan_enabled)
1932 		return 0;
1933 
1934 	mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vlan->vid);
1935 
1936 	return 0;
1937 }
1938 
1939 static int
1940 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1941 			struct mlxsw_sp_bridge_port *bridge_port,
1942 			struct mlxsw_sp_mid *mid)
1943 {
1944 	struct net_device *dev = mlxsw_sp_port->dev;
1945 	int err;
1946 
1947 	if (bridge_port->bridge_device->multicast_enabled &&
1948 	    !bridge_port->mrouter) {
1949 		err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1950 		if (err)
1951 			netdev_err(dev, "Unable to remove port from SMID\n");
1952 	}
1953 
1954 	err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1955 	if (err)
1956 		netdev_err(dev, "Unable to remove MC SFD\n");
1957 
1958 	return err;
1959 }
1960 
1961 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1962 				 const struct switchdev_obj_port_mdb *mdb)
1963 {
1964 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1965 	struct net_device *orig_dev = mdb->obj.orig_dev;
1966 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1967 	struct mlxsw_sp_bridge_device *bridge_device;
1968 	struct net_device *dev = mlxsw_sp_port->dev;
1969 	struct mlxsw_sp_bridge_port *bridge_port;
1970 	struct mlxsw_sp_mid *mid;
1971 	u16 fid_index;
1972 
1973 	bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1974 	if (!bridge_port)
1975 		return 0;
1976 
1977 	bridge_device = bridge_port->bridge_device;
1978 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1979 							       bridge_device,
1980 							       mdb->vid);
1981 	if (!mlxsw_sp_port_vlan)
1982 		return 0;
1983 
1984 	fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1985 
1986 	mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1987 	if (!mid) {
1988 		netdev_err(dev, "Unable to remove port from MC DB\n");
1989 		return -EINVAL;
1990 	}
1991 
1992 	return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1993 }
1994 
1995 static void
1996 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1997 			       struct mlxsw_sp_bridge_port *bridge_port)
1998 {
1999 	struct mlxsw_sp_bridge_device *bridge_device;
2000 	struct mlxsw_sp_mid *mid, *tmp;
2001 
2002 	bridge_device = bridge_port->bridge_device;
2003 
2004 	list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
2005 		if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
2006 			__mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
2007 						mid);
2008 		} else if (bridge_device->multicast_enabled &&
2009 			   bridge_port->mrouter) {
2010 			mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
2011 		}
2012 	}
2013 }
2014 
2015 static int mlxsw_sp_port_obj_del(struct net_device *dev, const void *ctx,
2016 				 const struct switchdev_obj *obj)
2017 {
2018 	struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2019 	int err = 0;
2020 
2021 	switch (obj->id) {
2022 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
2023 		err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
2024 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
2025 		break;
2026 	case SWITCHDEV_OBJ_ID_PORT_MDB:
2027 		err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
2028 					    SWITCHDEV_OBJ_PORT_MDB(obj));
2029 		break;
2030 	default:
2031 		err = -EOPNOTSUPP;
2032 		break;
2033 	}
2034 
2035 	mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2036 
2037 	return err;
2038 }
2039 
2040 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
2041 						   u16 lag_id)
2042 {
2043 	struct mlxsw_sp_port *mlxsw_sp_port;
2044 	u64 max_lag_members;
2045 	int i;
2046 
2047 	max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
2048 					     MAX_LAG_MEMBERS);
2049 	for (i = 0; i < max_lag_members; i++) {
2050 		mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
2051 		if (mlxsw_sp_port)
2052 			return mlxsw_sp_port;
2053 	}
2054 	return NULL;
2055 }
2056 
2057 static int
2058 mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port *bridge_port,
2059 				     struct mlxsw_sp_port *mlxsw_sp_port,
2060 				     struct netlink_ext_ack *extack)
2061 {
2062 	if (is_vlan_dev(bridge_port->dev)) {
2063 		NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
2064 		return -EINVAL;
2065 	}
2066 
2067 	/* Port is no longer usable as a router interface */
2068 	if (mlxsw_sp_port->default_vlan->fid)
2069 		mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
2070 
2071 	return 0;
2072 }
2073 
2074 static int
2075 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2076 				struct mlxsw_sp_bridge_port *bridge_port,
2077 				struct mlxsw_sp_port *mlxsw_sp_port,
2078 				struct netlink_ext_ack *extack)
2079 {
2080 	return mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2081 						    extack);
2082 }
2083 
2084 static void
2085 mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port *mlxsw_sp_port)
2086 {
2087 	/* Make sure untagged frames are allowed to ingress */
2088 	mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
2089 			       ETH_P_8021Q);
2090 }
2091 
2092 static void
2093 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2094 				 struct mlxsw_sp_bridge_port *bridge_port,
2095 				 struct mlxsw_sp_port *mlxsw_sp_port)
2096 {
2097 	mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2098 }
2099 
2100 static int
2101 mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2102 				      const struct net_device *vxlan_dev,
2103 				      u16 vid, u16 ethertype,
2104 				      struct netlink_ext_ack *extack)
2105 {
2106 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2107 	struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2108 	struct mlxsw_sp_nve_params params = {
2109 		.type = MLXSW_SP_NVE_TYPE_VXLAN,
2110 		.vni = vxlan->cfg.vni,
2111 		.dev = vxlan_dev,
2112 		.ethertype = ethertype,
2113 	};
2114 	struct mlxsw_sp_fid *fid;
2115 	int err;
2116 
2117 	/* If the VLAN is 0, we need to find the VLAN that is configured as
2118 	 * PVID and egress untagged on the bridge port of the VxLAN device.
2119 	 * It is possible no such VLAN exists
2120 	 */
2121 	if (!vid) {
2122 		err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
2123 		if (err || !vid)
2124 			return err;
2125 	}
2126 
2127 	fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2128 	if (IS_ERR(fid)) {
2129 		NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1Q FID");
2130 		return PTR_ERR(fid);
2131 	}
2132 
2133 	if (mlxsw_sp_fid_vni_is_set(fid)) {
2134 		NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2135 		err = -EINVAL;
2136 		goto err_vni_exists;
2137 	}
2138 
2139 	err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, &params, extack);
2140 	if (err)
2141 		goto err_nve_fid_enable;
2142 
2143 	return 0;
2144 
2145 err_nve_fid_enable:
2146 err_vni_exists:
2147 	mlxsw_sp_fid_put(fid);
2148 	return err;
2149 }
2150 
2151 static int
2152 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2153 				 const struct net_device *vxlan_dev, u16 vid,
2154 				 struct netlink_ext_ack *extack)
2155 {
2156 	return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2157 						     vid, ETH_P_8021Q, extack);
2158 }
2159 
2160 static struct net_device *
2161 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2162 {
2163 	struct net_device *dev;
2164 	struct list_head *iter;
2165 
2166 	netdev_for_each_lower_dev(br_dev, dev, iter) {
2167 		u16 pvid;
2168 		int err;
2169 
2170 		if (!netif_is_vxlan(dev))
2171 			continue;
2172 
2173 		err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2174 		if (err || pvid != vid)
2175 			continue;
2176 
2177 		return dev;
2178 	}
2179 
2180 	return NULL;
2181 }
2182 
2183 static struct mlxsw_sp_fid *
2184 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2185 			      u16 vid, struct netlink_ext_ack *extack)
2186 {
2187 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2188 
2189 	return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2190 }
2191 
2192 static struct mlxsw_sp_fid *
2193 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2194 				 u16 vid)
2195 {
2196 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2197 
2198 	return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2199 }
2200 
2201 static u16
2202 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2203 			      const struct mlxsw_sp_fid *fid)
2204 {
2205 	return mlxsw_sp_fid_8021q_vid(fid);
2206 }
2207 
2208 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2209 	.port_join	= mlxsw_sp_bridge_8021q_port_join,
2210 	.port_leave	= mlxsw_sp_bridge_8021q_port_leave,
2211 	.vxlan_join	= mlxsw_sp_bridge_8021q_vxlan_join,
2212 	.fid_get	= mlxsw_sp_bridge_8021q_fid_get,
2213 	.fid_lookup	= mlxsw_sp_bridge_8021q_fid_lookup,
2214 	.fid_vid	= mlxsw_sp_bridge_8021q_fid_vid,
2215 };
2216 
2217 static bool
2218 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2219 			   const struct net_device *br_dev)
2220 {
2221 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2222 
2223 	list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2224 			    list) {
2225 		if (mlxsw_sp_port_vlan->bridge_port &&
2226 		    mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2227 		    br_dev)
2228 			return true;
2229 	}
2230 
2231 	return false;
2232 }
2233 
2234 static int
2235 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2236 				struct mlxsw_sp_bridge_port *bridge_port,
2237 				struct mlxsw_sp_port *mlxsw_sp_port,
2238 				struct netlink_ext_ack *extack)
2239 {
2240 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2241 	struct net_device *dev = bridge_port->dev;
2242 	u16 vid;
2243 
2244 	vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2245 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2246 	if (WARN_ON(!mlxsw_sp_port_vlan))
2247 		return -EINVAL;
2248 
2249 	if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2250 		NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2251 		return -EINVAL;
2252 	}
2253 
2254 	/* Port is no longer usable as a router interface */
2255 	if (mlxsw_sp_port_vlan->fid)
2256 		mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2257 
2258 	return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2259 					      extack);
2260 }
2261 
2262 static void
2263 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2264 				 struct mlxsw_sp_bridge_port *bridge_port,
2265 				 struct mlxsw_sp_port *mlxsw_sp_port)
2266 {
2267 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2268 	struct net_device *dev = bridge_port->dev;
2269 	u16 vid;
2270 
2271 	vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2272 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2273 	if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2274 		return;
2275 
2276 	mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2277 }
2278 
2279 static int
2280 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2281 				 const struct net_device *vxlan_dev, u16 vid,
2282 				 struct netlink_ext_ack *extack)
2283 {
2284 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2285 	struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2286 	struct mlxsw_sp_nve_params params = {
2287 		.type = MLXSW_SP_NVE_TYPE_VXLAN,
2288 		.vni = vxlan->cfg.vni,
2289 		.dev = vxlan_dev,
2290 		.ethertype = ETH_P_8021Q,
2291 	};
2292 	struct mlxsw_sp_fid *fid;
2293 	int err;
2294 
2295 	fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2296 	if (IS_ERR(fid)) {
2297 		NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1D FID");
2298 		return -EINVAL;
2299 	}
2300 
2301 	if (mlxsw_sp_fid_vni_is_set(fid)) {
2302 		NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2303 		err = -EINVAL;
2304 		goto err_vni_exists;
2305 	}
2306 
2307 	err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, &params, extack);
2308 	if (err)
2309 		goto err_nve_fid_enable;
2310 
2311 	return 0;
2312 
2313 err_nve_fid_enable:
2314 err_vni_exists:
2315 	mlxsw_sp_fid_put(fid);
2316 	return err;
2317 }
2318 
2319 static struct mlxsw_sp_fid *
2320 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2321 			      u16 vid, struct netlink_ext_ack *extack)
2322 {
2323 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2324 
2325 	return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2326 }
2327 
2328 static struct mlxsw_sp_fid *
2329 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2330 				 u16 vid)
2331 {
2332 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2333 
2334 	/* The only valid VLAN for a VLAN-unaware bridge is 0 */
2335 	if (vid)
2336 		return NULL;
2337 
2338 	return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2339 }
2340 
2341 static u16
2342 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2343 			      const struct mlxsw_sp_fid *fid)
2344 {
2345 	return 0;
2346 }
2347 
2348 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2349 	.port_join	= mlxsw_sp_bridge_8021d_port_join,
2350 	.port_leave	= mlxsw_sp_bridge_8021d_port_leave,
2351 	.vxlan_join	= mlxsw_sp_bridge_8021d_vxlan_join,
2352 	.fid_get	= mlxsw_sp_bridge_8021d_fid_get,
2353 	.fid_lookup	= mlxsw_sp_bridge_8021d_fid_lookup,
2354 	.fid_vid	= mlxsw_sp_bridge_8021d_fid_vid,
2355 };
2356 
2357 static int
2358 mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2359 				 struct mlxsw_sp_bridge_port *bridge_port,
2360 				 struct mlxsw_sp_port *mlxsw_sp_port,
2361 				 struct netlink_ext_ack *extack)
2362 {
2363 	int err;
2364 
2365 	err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, false);
2366 	if (err)
2367 		return err;
2368 
2369 	err = mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2370 						   extack);
2371 	if (err)
2372 		goto err_bridge_vlan_aware_port_join;
2373 
2374 	return 0;
2375 
2376 err_bridge_vlan_aware_port_join:
2377 	mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2378 	return err;
2379 }
2380 
2381 static void
2382 mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2383 				  struct mlxsw_sp_bridge_port *bridge_port,
2384 				  struct mlxsw_sp_port *mlxsw_sp_port)
2385 {
2386 	mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2387 	mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2388 }
2389 
2390 static int
2391 mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2392 				  const struct net_device *vxlan_dev, u16 vid,
2393 				  struct netlink_ext_ack *extack)
2394 {
2395 	return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2396 						     vid, ETH_P_8021AD, extack);
2397 }
2398 
2399 static const struct mlxsw_sp_bridge_ops mlxsw_sp1_bridge_8021ad_ops = {
2400 	.port_join	= mlxsw_sp_bridge_8021ad_port_join,
2401 	.port_leave	= mlxsw_sp_bridge_8021ad_port_leave,
2402 	.vxlan_join	= mlxsw_sp_bridge_8021ad_vxlan_join,
2403 	.fid_get	= mlxsw_sp_bridge_8021q_fid_get,
2404 	.fid_lookup	= mlxsw_sp_bridge_8021q_fid_lookup,
2405 	.fid_vid	= mlxsw_sp_bridge_8021q_fid_vid,
2406 };
2407 
2408 static int
2409 mlxsw_sp2_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2410 				  struct mlxsw_sp_bridge_port *bridge_port,
2411 				  struct mlxsw_sp_port *mlxsw_sp_port,
2412 				  struct netlink_ext_ack *extack)
2413 {
2414 	int err;
2415 
2416 	/* The EtherType of decapsulated packets is determined at the egress
2417 	 * port to allow 802.1d and 802.1ad bridges with VXLAN devices to
2418 	 * co-exist.
2419 	 */
2420 	err = mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021AD);
2421 	if (err)
2422 		return err;
2423 
2424 	err = mlxsw_sp_bridge_8021ad_port_join(bridge_device, bridge_port,
2425 					       mlxsw_sp_port, extack);
2426 	if (err)
2427 		goto err_bridge_8021ad_port_join;
2428 
2429 	return 0;
2430 
2431 err_bridge_8021ad_port_join:
2432 	mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2433 	return err;
2434 }
2435 
2436 static void
2437 mlxsw_sp2_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2438 				   struct mlxsw_sp_bridge_port *bridge_port,
2439 				   struct mlxsw_sp_port *mlxsw_sp_port)
2440 {
2441 	mlxsw_sp_bridge_8021ad_port_leave(bridge_device, bridge_port,
2442 					  mlxsw_sp_port);
2443 	mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2444 }
2445 
2446 static const struct mlxsw_sp_bridge_ops mlxsw_sp2_bridge_8021ad_ops = {
2447 	.port_join	= mlxsw_sp2_bridge_8021ad_port_join,
2448 	.port_leave	= mlxsw_sp2_bridge_8021ad_port_leave,
2449 	.vxlan_join	= mlxsw_sp_bridge_8021ad_vxlan_join,
2450 	.fid_get	= mlxsw_sp_bridge_8021q_fid_get,
2451 	.fid_lookup	= mlxsw_sp_bridge_8021q_fid_lookup,
2452 	.fid_vid	= mlxsw_sp_bridge_8021q_fid_vid,
2453 };
2454 
2455 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2456 			      struct net_device *brport_dev,
2457 			      struct net_device *br_dev,
2458 			      struct netlink_ext_ack *extack)
2459 {
2460 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2461 	struct mlxsw_sp_bridge_device *bridge_device;
2462 	struct mlxsw_sp_bridge_port *bridge_port;
2463 	int err;
2464 
2465 	bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev,
2466 					       extack);
2467 	if (IS_ERR(bridge_port))
2468 		return PTR_ERR(bridge_port);
2469 	bridge_device = bridge_port->bridge_device;
2470 
2471 	err = bridge_device->ops->port_join(bridge_device, bridge_port,
2472 					    mlxsw_sp_port, extack);
2473 	if (err)
2474 		goto err_port_join;
2475 
2476 	return 0;
2477 
2478 err_port_join:
2479 	mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2480 	return err;
2481 }
2482 
2483 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2484 				struct net_device *brport_dev,
2485 				struct net_device *br_dev)
2486 {
2487 	struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2488 	struct mlxsw_sp_bridge_device *bridge_device;
2489 	struct mlxsw_sp_bridge_port *bridge_port;
2490 
2491 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2492 	if (!bridge_device)
2493 		return;
2494 	bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2495 	if (!bridge_port)
2496 		return;
2497 
2498 	bridge_device->ops->port_leave(bridge_device, bridge_port,
2499 				       mlxsw_sp_port);
2500 	mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2501 }
2502 
2503 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2504 			       const struct net_device *br_dev,
2505 			       const struct net_device *vxlan_dev, u16 vid,
2506 			       struct netlink_ext_ack *extack)
2507 {
2508 	struct mlxsw_sp_bridge_device *bridge_device;
2509 
2510 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2511 	if (WARN_ON(!bridge_device))
2512 		return -EINVAL;
2513 
2514 	return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2515 					      extack);
2516 }
2517 
2518 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2519 				 const struct net_device *vxlan_dev)
2520 {
2521 	struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2522 	struct mlxsw_sp_fid *fid;
2523 
2524 	/* If the VxLAN device is down, then the FID does not have a VNI */
2525 	fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2526 	if (!fid)
2527 		return;
2528 
2529 	mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2530 	/* Drop both the reference we just took during lookup and the reference
2531 	 * the VXLAN device took.
2532 	 */
2533 	mlxsw_sp_fid_put(fid);
2534 	mlxsw_sp_fid_put(fid);
2535 }
2536 
2537 static void
2538 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2539 				      enum mlxsw_sp_l3proto *proto,
2540 				      union mlxsw_sp_l3addr *addr)
2541 {
2542 	if (vxlan_addr->sa.sa_family == AF_INET) {
2543 		addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2544 		*proto = MLXSW_SP_L3_PROTO_IPV4;
2545 	} else {
2546 		addr->addr6 = vxlan_addr->sin6.sin6_addr;
2547 		*proto = MLXSW_SP_L3_PROTO_IPV6;
2548 	}
2549 }
2550 
2551 static void
2552 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2553 				      const union mlxsw_sp_l3addr *addr,
2554 				      union vxlan_addr *vxlan_addr)
2555 {
2556 	switch (proto) {
2557 	case MLXSW_SP_L3_PROTO_IPV4:
2558 		vxlan_addr->sa.sa_family = AF_INET;
2559 		vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2560 		break;
2561 	case MLXSW_SP_L3_PROTO_IPV6:
2562 		vxlan_addr->sa.sa_family = AF_INET6;
2563 		vxlan_addr->sin6.sin6_addr = addr->addr6;
2564 		break;
2565 	}
2566 }
2567 
2568 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2569 					      const char *mac,
2570 					      enum mlxsw_sp_l3proto proto,
2571 					      union mlxsw_sp_l3addr *addr,
2572 					      __be32 vni, bool adding)
2573 {
2574 	struct switchdev_notifier_vxlan_fdb_info info;
2575 	struct vxlan_dev *vxlan = netdev_priv(dev);
2576 	enum switchdev_notifier_type type;
2577 
2578 	type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2579 			SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2580 	mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2581 	info.remote_port = vxlan->cfg.dst_port;
2582 	info.remote_vni = vni;
2583 	info.remote_ifindex = 0;
2584 	ether_addr_copy(info.eth_addr, mac);
2585 	info.vni = vni;
2586 	info.offloaded = adding;
2587 	call_switchdev_notifiers(type, dev, &info.info, NULL);
2588 }
2589 
2590 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2591 					    const char *mac,
2592 					    enum mlxsw_sp_l3proto proto,
2593 					    union mlxsw_sp_l3addr *addr,
2594 					    __be32 vni,
2595 					    bool adding)
2596 {
2597 	if (netif_is_vxlan(dev))
2598 		mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2599 						  adding);
2600 }
2601 
2602 static void
2603 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2604 			    const char *mac, u16 vid,
2605 			    struct net_device *dev, bool offloaded)
2606 {
2607 	struct switchdev_notifier_fdb_info info = {};
2608 
2609 	info.addr = mac;
2610 	info.vid = vid;
2611 	info.offloaded = offloaded;
2612 	call_switchdev_notifiers(type, dev, &info.info, NULL);
2613 }
2614 
2615 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2616 					    char *sfn_pl, int rec_index,
2617 					    bool adding)
2618 {
2619 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2620 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2621 	struct mlxsw_sp_bridge_device *bridge_device;
2622 	struct mlxsw_sp_bridge_port *bridge_port;
2623 	struct mlxsw_sp_port *mlxsw_sp_port;
2624 	enum switchdev_notifier_type type;
2625 	char mac[ETH_ALEN];
2626 	u16 local_port;
2627 	u16 vid, fid;
2628 	bool do_notification = true;
2629 	int err;
2630 
2631 	mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2632 
2633 	if (WARN_ON_ONCE(local_port >= max_ports))
2634 		return;
2635 	mlxsw_sp_port = mlxsw_sp->ports[local_port];
2636 	if (!mlxsw_sp_port) {
2637 		dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2638 		goto just_remove;
2639 	}
2640 
2641 	if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2642 		goto just_remove;
2643 
2644 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2645 	if (!mlxsw_sp_port_vlan) {
2646 		netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2647 		goto just_remove;
2648 	}
2649 
2650 	bridge_port = mlxsw_sp_port_vlan->bridge_port;
2651 	if (!bridge_port) {
2652 		netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2653 		goto just_remove;
2654 	}
2655 
2656 	bridge_device = bridge_port->bridge_device;
2657 	vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2658 
2659 do_fdb_op:
2660 	err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2661 				      adding, true);
2662 	if (err) {
2663 		dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2664 		return;
2665 	}
2666 
2667 	if (!do_notification)
2668 		return;
2669 	type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2670 	mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2671 
2672 	return;
2673 
2674 just_remove:
2675 	adding = false;
2676 	do_notification = false;
2677 	goto do_fdb_op;
2678 }
2679 
2680 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2681 						char *sfn_pl, int rec_index,
2682 						bool adding)
2683 {
2684 	struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2685 	struct mlxsw_sp_bridge_device *bridge_device;
2686 	struct mlxsw_sp_bridge_port *bridge_port;
2687 	struct mlxsw_sp_port *mlxsw_sp_port;
2688 	enum switchdev_notifier_type type;
2689 	char mac[ETH_ALEN];
2690 	u16 lag_vid = 0;
2691 	u16 lag_id;
2692 	u16 vid, fid;
2693 	bool do_notification = true;
2694 	int err;
2695 
2696 	mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2697 	mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2698 	if (!mlxsw_sp_port) {
2699 		dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2700 		goto just_remove;
2701 	}
2702 
2703 	if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2704 		goto just_remove;
2705 
2706 	mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2707 	if (!mlxsw_sp_port_vlan) {
2708 		netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2709 		goto just_remove;
2710 	}
2711 
2712 	bridge_port = mlxsw_sp_port_vlan->bridge_port;
2713 	if (!bridge_port) {
2714 		netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2715 		goto just_remove;
2716 	}
2717 
2718 	bridge_device = bridge_port->bridge_device;
2719 	vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2720 	lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2721 		  mlxsw_sp_port_vlan->vid : 0;
2722 
2723 do_fdb_op:
2724 	err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2725 					  adding, true);
2726 	if (err) {
2727 		dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2728 		return;
2729 	}
2730 
2731 	if (!do_notification)
2732 		return;
2733 	type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2734 	mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2735 
2736 	return;
2737 
2738 just_remove:
2739 	adding = false;
2740 	do_notification = false;
2741 	goto do_fdb_op;
2742 }
2743 
2744 static int
2745 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2746 					    const struct mlxsw_sp_fid *fid,
2747 					    bool adding,
2748 					    struct net_device **nve_dev,
2749 					    u16 *p_vid, __be32 *p_vni)
2750 {
2751 	struct mlxsw_sp_bridge_device *bridge_device;
2752 	struct net_device *br_dev, *dev;
2753 	int nve_ifindex;
2754 	int err;
2755 
2756 	err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2757 	if (err)
2758 		return err;
2759 
2760 	err = mlxsw_sp_fid_vni(fid, p_vni);
2761 	if (err)
2762 		return err;
2763 
2764 	dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
2765 	if (!dev)
2766 		return -EINVAL;
2767 	*nve_dev = dev;
2768 
2769 	if (!netif_running(dev))
2770 		return -EINVAL;
2771 
2772 	if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2773 		return -EINVAL;
2774 
2775 	if (adding && netif_is_vxlan(dev)) {
2776 		struct vxlan_dev *vxlan = netdev_priv(dev);
2777 
2778 		if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2779 			return -EINVAL;
2780 	}
2781 
2782 	br_dev = netdev_master_upper_dev_get(dev);
2783 	if (!br_dev)
2784 		return -EINVAL;
2785 
2786 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2787 	if (!bridge_device)
2788 		return -EINVAL;
2789 
2790 	*p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2791 
2792 	return 0;
2793 }
2794 
2795 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2796 						      char *sfn_pl,
2797 						      int rec_index,
2798 						      bool adding)
2799 {
2800 	enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2801 	enum switchdev_notifier_type type;
2802 	struct net_device *nve_dev;
2803 	union mlxsw_sp_l3addr addr;
2804 	struct mlxsw_sp_fid *fid;
2805 	char mac[ETH_ALEN];
2806 	u16 fid_index, vid;
2807 	__be32 vni;
2808 	u32 uip;
2809 	int err;
2810 
2811 	mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2812 				       &uip, &sfn_proto);
2813 
2814 	fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2815 	if (!fid)
2816 		goto err_fid_lookup;
2817 
2818 	err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2819 					      (enum mlxsw_sp_l3proto) sfn_proto,
2820 					      &addr);
2821 	if (err)
2822 		goto err_ip_resolve;
2823 
2824 	err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2825 							  &nve_dev, &vid, &vni);
2826 	if (err)
2827 		goto err_fdb_process;
2828 
2829 	err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2830 					     (enum mlxsw_sp_l3proto) sfn_proto,
2831 					     &addr, adding, true);
2832 	if (err)
2833 		goto err_fdb_op;
2834 
2835 	mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2836 					(enum mlxsw_sp_l3proto) sfn_proto,
2837 					&addr, vni, adding);
2838 
2839 	type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2840 			SWITCHDEV_FDB_DEL_TO_BRIDGE;
2841 	mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2842 
2843 	mlxsw_sp_fid_put(fid);
2844 
2845 	return;
2846 
2847 err_fdb_op:
2848 err_fdb_process:
2849 err_ip_resolve:
2850 	mlxsw_sp_fid_put(fid);
2851 err_fid_lookup:
2852 	/* Remove an FDB entry in case we cannot process it. Otherwise the
2853 	 * device will keep sending the same notification over and over again.
2854 	 */
2855 	mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2856 				       (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2857 				       false, true);
2858 }
2859 
2860 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2861 					    char *sfn_pl, int rec_index)
2862 {
2863 	switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2864 	case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2865 		mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2866 						rec_index, true);
2867 		break;
2868 	case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2869 		mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2870 						rec_index, false);
2871 		break;
2872 	case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2873 		mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2874 						    rec_index, true);
2875 		break;
2876 	case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2877 		mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2878 						    rec_index, false);
2879 		break;
2880 	case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2881 		mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2882 							  rec_index, true);
2883 		break;
2884 	case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2885 		mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2886 							  rec_index, false);
2887 		break;
2888 	}
2889 }
2890 
2891 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp,
2892 					      bool no_delay)
2893 {
2894 	struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2895 	unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval;
2896 
2897 	mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2898 			       msecs_to_jiffies(interval));
2899 }
2900 
2901 #define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10
2902 
2903 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2904 {
2905 	struct mlxsw_sp_bridge *bridge;
2906 	struct mlxsw_sp *mlxsw_sp;
2907 	char *sfn_pl;
2908 	int queries;
2909 	u8 num_rec;
2910 	int i;
2911 	int err;
2912 
2913 	sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2914 	if (!sfn_pl)
2915 		return;
2916 
2917 	bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2918 	mlxsw_sp = bridge->mlxsw_sp;
2919 
2920 	rtnl_lock();
2921 	queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION;
2922 	while (queries > 0) {
2923 		mlxsw_reg_sfn_pack(sfn_pl);
2924 		err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2925 		if (err) {
2926 			dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2927 			goto out;
2928 		}
2929 		num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2930 		for (i = 0; i < num_rec; i++)
2931 			mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2932 		if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT)
2933 			goto out;
2934 		queries--;
2935 	}
2936 
2937 out:
2938 	rtnl_unlock();
2939 	kfree(sfn_pl);
2940 	mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries);
2941 }
2942 
2943 struct mlxsw_sp_switchdev_event_work {
2944 	struct work_struct work;
2945 	union {
2946 		struct switchdev_notifier_fdb_info fdb_info;
2947 		struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2948 	};
2949 	struct net_device *dev;
2950 	unsigned long event;
2951 };
2952 
2953 static void
2954 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2955 					  struct mlxsw_sp_switchdev_event_work *
2956 					  switchdev_work,
2957 					  struct mlxsw_sp_fid *fid, __be32 vni)
2958 {
2959 	struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2960 	struct switchdev_notifier_fdb_info *fdb_info;
2961 	struct net_device *dev = switchdev_work->dev;
2962 	enum mlxsw_sp_l3proto proto;
2963 	union mlxsw_sp_l3addr addr;
2964 	int err;
2965 
2966 	fdb_info = &switchdev_work->fdb_info;
2967 	err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2968 	if (err)
2969 		return;
2970 
2971 	mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2972 					      &proto, &addr);
2973 
2974 	switch (switchdev_work->event) {
2975 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2976 		err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2977 						     vxlan_fdb_info.eth_addr,
2978 						     mlxsw_sp_fid_index(fid),
2979 						     proto, &addr, true, false);
2980 		if (err)
2981 			return;
2982 		vxlan_fdb_info.offloaded = true;
2983 		call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2984 					 &vxlan_fdb_info.info, NULL);
2985 		mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2986 					    vxlan_fdb_info.eth_addr,
2987 					    fdb_info->vid, dev, true);
2988 		break;
2989 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2990 		err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2991 						     vxlan_fdb_info.eth_addr,
2992 						     mlxsw_sp_fid_index(fid),
2993 						     proto, &addr, false,
2994 						     false);
2995 		vxlan_fdb_info.offloaded = false;
2996 		call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2997 					 &vxlan_fdb_info.info, NULL);
2998 		break;
2999 	}
3000 }
3001 
3002 static void
3003 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
3004 					switchdev_work)
3005 {
3006 	struct mlxsw_sp_bridge_device *bridge_device;
3007 	struct net_device *dev = switchdev_work->dev;
3008 	struct net_device *br_dev;
3009 	struct mlxsw_sp *mlxsw_sp;
3010 	struct mlxsw_sp_fid *fid;
3011 	__be32 vni;
3012 	int err;
3013 
3014 	if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
3015 	    switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
3016 		return;
3017 
3018 	if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
3019 	    (!switchdev_work->fdb_info.added_by_user ||
3020 	     switchdev_work->fdb_info.is_local))
3021 		return;
3022 
3023 	if (!netif_running(dev))
3024 		return;
3025 	br_dev = netdev_master_upper_dev_get(dev);
3026 	if (!br_dev)
3027 		return;
3028 	if (!netif_is_bridge_master(br_dev))
3029 		return;
3030 	mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3031 	if (!mlxsw_sp)
3032 		return;
3033 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3034 	if (!bridge_device)
3035 		return;
3036 
3037 	fid = bridge_device->ops->fid_lookup(bridge_device,
3038 					     switchdev_work->fdb_info.vid);
3039 	if (!fid)
3040 		return;
3041 
3042 	err = mlxsw_sp_fid_vni(fid, &vni);
3043 	if (err)
3044 		goto out;
3045 
3046 	mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
3047 						  vni);
3048 
3049 out:
3050 	mlxsw_sp_fid_put(fid);
3051 }
3052 
3053 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
3054 {
3055 	struct mlxsw_sp_switchdev_event_work *switchdev_work =
3056 		container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3057 	struct net_device *dev = switchdev_work->dev;
3058 	struct switchdev_notifier_fdb_info *fdb_info;
3059 	struct mlxsw_sp_port *mlxsw_sp_port;
3060 	int err;
3061 
3062 	rtnl_lock();
3063 	if (netif_is_vxlan(dev)) {
3064 		mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
3065 		goto out;
3066 	}
3067 
3068 	mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
3069 	if (!mlxsw_sp_port)
3070 		goto out;
3071 
3072 	switch (switchdev_work->event) {
3073 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3074 		fdb_info = &switchdev_work->fdb_info;
3075 		if (!fdb_info->added_by_user || fdb_info->is_local)
3076 			break;
3077 		err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
3078 		if (err)
3079 			break;
3080 		mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3081 					    fdb_info->addr,
3082 					    fdb_info->vid, dev, true);
3083 		break;
3084 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3085 		fdb_info = &switchdev_work->fdb_info;
3086 		mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
3087 		break;
3088 	case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3089 	case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3090 		/* These events are only used to potentially update an existing
3091 		 * SPAN mirror.
3092 		 */
3093 		break;
3094 	}
3095 
3096 	mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
3097 
3098 out:
3099 	rtnl_unlock();
3100 	kfree(switchdev_work->fdb_info.addr);
3101 	kfree(switchdev_work);
3102 	dev_put(dev);
3103 }
3104 
3105 static void
3106 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
3107 				 struct mlxsw_sp_switchdev_event_work *
3108 				 switchdev_work)
3109 {
3110 	struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3111 	struct mlxsw_sp_bridge_device *bridge_device;
3112 	struct net_device *dev = switchdev_work->dev;
3113 	u8 all_zeros_mac[ETH_ALEN] = { 0 };
3114 	enum mlxsw_sp_l3proto proto;
3115 	union mlxsw_sp_l3addr addr;
3116 	struct net_device *br_dev;
3117 	struct mlxsw_sp_fid *fid;
3118 	u16 vid;
3119 	int err;
3120 
3121 	vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3122 	br_dev = netdev_master_upper_dev_get(dev);
3123 
3124 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3125 	if (!bridge_device)
3126 		return;
3127 
3128 	fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3129 	if (!fid)
3130 		return;
3131 
3132 	mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3133 					      &proto, &addr);
3134 
3135 	if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3136 		err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
3137 		if (err) {
3138 			mlxsw_sp_fid_put(fid);
3139 			return;
3140 		}
3141 		vxlan_fdb_info->offloaded = true;
3142 		call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3143 					 &vxlan_fdb_info->info, NULL);
3144 		mlxsw_sp_fid_put(fid);
3145 		return;
3146 	}
3147 
3148 	/* The device has a single FDB table, whereas Linux has two - one
3149 	 * in the bridge driver and another in the VxLAN driver. We only
3150 	 * program an entry to the device if the MAC points to the VxLAN
3151 	 * device in the bridge's FDB table
3152 	 */
3153 	vid = bridge_device->ops->fid_vid(bridge_device, fid);
3154 	if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
3155 		goto err_br_fdb_find;
3156 
3157 	err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3158 					     mlxsw_sp_fid_index(fid), proto,
3159 					     &addr, true, false);
3160 	if (err)
3161 		goto err_fdb_tunnel_uc_op;
3162 	vxlan_fdb_info->offloaded = true;
3163 	call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3164 				 &vxlan_fdb_info->info, NULL);
3165 	mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3166 				    vxlan_fdb_info->eth_addr, vid, dev, true);
3167 
3168 	mlxsw_sp_fid_put(fid);
3169 
3170 	return;
3171 
3172 err_fdb_tunnel_uc_op:
3173 err_br_fdb_find:
3174 	mlxsw_sp_fid_put(fid);
3175 }
3176 
3177 static void
3178 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
3179 				 struct mlxsw_sp_switchdev_event_work *
3180 				 switchdev_work)
3181 {
3182 	struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3183 	struct mlxsw_sp_bridge_device *bridge_device;
3184 	struct net_device *dev = switchdev_work->dev;
3185 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3186 	u8 all_zeros_mac[ETH_ALEN] = { 0 };
3187 	enum mlxsw_sp_l3proto proto;
3188 	union mlxsw_sp_l3addr addr;
3189 	struct mlxsw_sp_fid *fid;
3190 	u16 vid;
3191 
3192 	vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3193 
3194 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3195 	if (!bridge_device)
3196 		return;
3197 
3198 	fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3199 	if (!fid)
3200 		return;
3201 
3202 	mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3203 					      &proto, &addr);
3204 
3205 	if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3206 		mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3207 		mlxsw_sp_fid_put(fid);
3208 		return;
3209 	}
3210 
3211 	mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3212 				       mlxsw_sp_fid_index(fid), proto, &addr,
3213 				       false, false);
3214 	vid = bridge_device->ops->fid_vid(bridge_device, fid);
3215 	mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3216 				    vxlan_fdb_info->eth_addr, vid, dev, false);
3217 
3218 	mlxsw_sp_fid_put(fid);
3219 }
3220 
3221 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3222 {
3223 	struct mlxsw_sp_switchdev_event_work *switchdev_work =
3224 		container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3225 	struct net_device *dev = switchdev_work->dev;
3226 	struct mlxsw_sp *mlxsw_sp;
3227 	struct net_device *br_dev;
3228 
3229 	rtnl_lock();
3230 
3231 	if (!netif_running(dev))
3232 		goto out;
3233 	br_dev = netdev_master_upper_dev_get(dev);
3234 	if (!br_dev)
3235 		goto out;
3236 	if (!netif_is_bridge_master(br_dev))
3237 		goto out;
3238 	mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3239 	if (!mlxsw_sp)
3240 		goto out;
3241 
3242 	switch (switchdev_work->event) {
3243 	case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3244 		mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3245 		break;
3246 	case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3247 		mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3248 		break;
3249 	}
3250 
3251 out:
3252 	rtnl_unlock();
3253 	kfree(switchdev_work);
3254 	dev_put(dev);
3255 }
3256 
3257 static int
3258 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3259 				      switchdev_work,
3260 				      struct switchdev_notifier_info *info)
3261 {
3262 	struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3263 	struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3264 	struct vxlan_config *cfg = &vxlan->cfg;
3265 	struct netlink_ext_ack *extack;
3266 
3267 	extack = switchdev_notifier_info_to_extack(info);
3268 	vxlan_fdb_info = container_of(info,
3269 				      struct switchdev_notifier_vxlan_fdb_info,
3270 				      info);
3271 
3272 	if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3273 		NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3274 		return -EOPNOTSUPP;
3275 	}
3276 	if (vxlan_fdb_info->remote_vni != cfg->vni ||
3277 	    vxlan_fdb_info->vni != cfg->vni) {
3278 		NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3279 		return -EOPNOTSUPP;
3280 	}
3281 	if (vxlan_fdb_info->remote_ifindex) {
3282 		NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3283 		return -EOPNOTSUPP;
3284 	}
3285 	if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3286 		NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3287 		return -EOPNOTSUPP;
3288 	}
3289 	if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3290 		NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3291 		return -EOPNOTSUPP;
3292 	}
3293 
3294 	switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3295 
3296 	return 0;
3297 }
3298 
3299 /* Called under rcu_read_lock() */
3300 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3301 				    unsigned long event, void *ptr)
3302 {
3303 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3304 	struct mlxsw_sp_switchdev_event_work *switchdev_work;
3305 	struct switchdev_notifier_fdb_info *fdb_info;
3306 	struct switchdev_notifier_info *info = ptr;
3307 	struct net_device *br_dev;
3308 	int err;
3309 
3310 	if (event == SWITCHDEV_PORT_ATTR_SET) {
3311 		err = switchdev_handle_port_attr_set(dev, ptr,
3312 						     mlxsw_sp_port_dev_check,
3313 						     mlxsw_sp_port_attr_set);
3314 		return notifier_from_errno(err);
3315 	}
3316 
3317 	/* Tunnel devices are not our uppers, so check their master instead */
3318 	br_dev = netdev_master_upper_dev_get_rcu(dev);
3319 	if (!br_dev)
3320 		return NOTIFY_DONE;
3321 	if (!netif_is_bridge_master(br_dev))
3322 		return NOTIFY_DONE;
3323 	if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3324 		return NOTIFY_DONE;
3325 
3326 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3327 	if (!switchdev_work)
3328 		return NOTIFY_BAD;
3329 
3330 	switchdev_work->dev = dev;
3331 	switchdev_work->event = event;
3332 
3333 	switch (event) {
3334 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3335 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3336 	case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3337 	case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3338 		fdb_info = container_of(info,
3339 					struct switchdev_notifier_fdb_info,
3340 					info);
3341 		INIT_WORK(&switchdev_work->work,
3342 			  mlxsw_sp_switchdev_bridge_fdb_event_work);
3343 		memcpy(&switchdev_work->fdb_info, ptr,
3344 		       sizeof(switchdev_work->fdb_info));
3345 		switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3346 		if (!switchdev_work->fdb_info.addr)
3347 			goto err_addr_alloc;
3348 		ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3349 				fdb_info->addr);
3350 		/* Take a reference on the device. This can be either
3351 		 * upper device containig mlxsw_sp_port or just a
3352 		 * mlxsw_sp_port
3353 		 */
3354 		dev_hold(dev);
3355 		break;
3356 	case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3357 	case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3358 		INIT_WORK(&switchdev_work->work,
3359 			  mlxsw_sp_switchdev_vxlan_fdb_event_work);
3360 		err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3361 							    info);
3362 		if (err)
3363 			goto err_vxlan_work_prepare;
3364 		dev_hold(dev);
3365 		break;
3366 	default:
3367 		kfree(switchdev_work);
3368 		return NOTIFY_DONE;
3369 	}
3370 
3371 	mlxsw_core_schedule_work(&switchdev_work->work);
3372 
3373 	return NOTIFY_DONE;
3374 
3375 err_vxlan_work_prepare:
3376 err_addr_alloc:
3377 	kfree(switchdev_work);
3378 	return NOTIFY_BAD;
3379 }
3380 
3381 struct notifier_block mlxsw_sp_switchdev_notifier = {
3382 	.notifier_call = mlxsw_sp_switchdev_event,
3383 };
3384 
3385 static int
3386 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3387 				  struct mlxsw_sp_bridge_device *bridge_device,
3388 				  const struct net_device *vxlan_dev, u16 vid,
3389 				  bool flag_untagged, bool flag_pvid,
3390 				  struct netlink_ext_ack *extack)
3391 {
3392 	struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3393 	__be32 vni = vxlan->cfg.vni;
3394 	struct mlxsw_sp_fid *fid;
3395 	u16 old_vid;
3396 	int err;
3397 
3398 	/* We cannot have the same VLAN as PVID and egress untagged on multiple
3399 	 * VxLAN devices. Note that we get this notification before the VLAN is
3400 	 * actually added to the bridge's database, so it is not possible for
3401 	 * the lookup function to return 'vxlan_dev'
3402 	 */
3403 	if (flag_untagged && flag_pvid &&
3404 	    mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3405 		NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3406 		return -EINVAL;
3407 	}
3408 
3409 	if (!netif_running(vxlan_dev))
3410 		return 0;
3411 
3412 	/* First case: FID is not associated with this VNI, but the new VLAN
3413 	 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3414 	 * it exists
3415 	 */
3416 	fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3417 	if (!fid) {
3418 		if (!flag_untagged || !flag_pvid)
3419 			return 0;
3420 		return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev,
3421 						      vid, extack);
3422 	}
3423 
3424 	/* Second case: FID is associated with the VNI and the VLAN associated
3425 	 * with the FID is the same as the notified VLAN. This means the flags
3426 	 * (PVID / egress untagged) were toggled and that NVE should be
3427 	 * disabled on the FID
3428 	 */
3429 	old_vid = mlxsw_sp_fid_8021q_vid(fid);
3430 	if (vid == old_vid) {
3431 		if (WARN_ON(flag_untagged && flag_pvid)) {
3432 			mlxsw_sp_fid_put(fid);
3433 			return -EINVAL;
3434 		}
3435 		mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3436 		mlxsw_sp_fid_put(fid);
3437 		return 0;
3438 	}
3439 
3440 	/* Third case: A new VLAN was configured on the VxLAN device, but this
3441 	 * VLAN is not PVID, so there is nothing to do.
3442 	 */
3443 	if (!flag_pvid) {
3444 		mlxsw_sp_fid_put(fid);
3445 		return 0;
3446 	}
3447 
3448 	/* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3449 	 * mapped to the VNI should be unmapped
3450 	 */
3451 	mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3452 	mlxsw_sp_fid_put(fid);
3453 
3454 	/* Fifth case: The new VLAN is also egress untagged, which means the
3455 	 * VLAN needs to be mapped to the VNI
3456 	 */
3457 	if (!flag_untagged)
3458 		return 0;
3459 
3460 	err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack);
3461 	if (err)
3462 		goto err_vxlan_join;
3463 
3464 	return 0;
3465 
3466 err_vxlan_join:
3467 	bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL);
3468 	return err;
3469 }
3470 
3471 static void
3472 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3473 				  struct mlxsw_sp_bridge_device *bridge_device,
3474 				  const struct net_device *vxlan_dev, u16 vid)
3475 {
3476 	struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3477 	__be32 vni = vxlan->cfg.vni;
3478 	struct mlxsw_sp_fid *fid;
3479 
3480 	if (!netif_running(vxlan_dev))
3481 		return;
3482 
3483 	fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3484 	if (!fid)
3485 		return;
3486 
3487 	/* A different VLAN than the one mapped to the VNI is deleted */
3488 	if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3489 		goto out;
3490 
3491 	mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3492 
3493 out:
3494 	mlxsw_sp_fid_put(fid);
3495 }
3496 
3497 static int
3498 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3499 				   struct switchdev_notifier_port_obj_info *
3500 				   port_obj_info)
3501 {
3502 	struct switchdev_obj_port_vlan *vlan =
3503 		SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3504 	bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3505 	bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3506 	struct mlxsw_sp_bridge_device *bridge_device;
3507 	struct netlink_ext_ack *extack;
3508 	struct mlxsw_sp *mlxsw_sp;
3509 	struct net_device *br_dev;
3510 
3511 	extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3512 	br_dev = netdev_master_upper_dev_get(vxlan_dev);
3513 	if (!br_dev)
3514 		return 0;
3515 
3516 	mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3517 	if (!mlxsw_sp)
3518 		return 0;
3519 
3520 	port_obj_info->handled = true;
3521 
3522 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3523 	if (!bridge_device)
3524 		return -EINVAL;
3525 
3526 	if (!bridge_device->vlan_enabled)
3527 		return 0;
3528 
3529 	return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3530 						 vxlan_dev, vlan->vid,
3531 						 flag_untagged,
3532 						 flag_pvid, extack);
3533 }
3534 
3535 static void
3536 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3537 				   struct switchdev_notifier_port_obj_info *
3538 				   port_obj_info)
3539 {
3540 	struct switchdev_obj_port_vlan *vlan =
3541 		SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3542 	struct mlxsw_sp_bridge_device *bridge_device;
3543 	struct mlxsw_sp *mlxsw_sp;
3544 	struct net_device *br_dev;
3545 
3546 	br_dev = netdev_master_upper_dev_get(vxlan_dev);
3547 	if (!br_dev)
3548 		return;
3549 
3550 	mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3551 	if (!mlxsw_sp)
3552 		return;
3553 
3554 	port_obj_info->handled = true;
3555 
3556 	bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3557 	if (!bridge_device)
3558 		return;
3559 
3560 	if (!bridge_device->vlan_enabled)
3561 		return;
3562 
3563 	mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev,
3564 					  vlan->vid);
3565 }
3566 
3567 static int
3568 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3569 					struct switchdev_notifier_port_obj_info *
3570 					port_obj_info)
3571 {
3572 	int err = 0;
3573 
3574 	switch (port_obj_info->obj->id) {
3575 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
3576 		err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3577 							 port_obj_info);
3578 		break;
3579 	default:
3580 		break;
3581 	}
3582 
3583 	return err;
3584 }
3585 
3586 static void
3587 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3588 					struct switchdev_notifier_port_obj_info *
3589 					port_obj_info)
3590 {
3591 	switch (port_obj_info->obj->id) {
3592 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
3593 		mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3594 		break;
3595 	default:
3596 		break;
3597 	}
3598 }
3599 
3600 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3601 					     unsigned long event, void *ptr)
3602 {
3603 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3604 	int err = 0;
3605 
3606 	switch (event) {
3607 	case SWITCHDEV_PORT_OBJ_ADD:
3608 		if (netif_is_vxlan(dev))
3609 			err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3610 		else
3611 			err = switchdev_handle_port_obj_add(dev, ptr,
3612 							mlxsw_sp_port_dev_check,
3613 							mlxsw_sp_port_obj_add);
3614 		return notifier_from_errno(err);
3615 	case SWITCHDEV_PORT_OBJ_DEL:
3616 		if (netif_is_vxlan(dev))
3617 			mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3618 		else
3619 			err = switchdev_handle_port_obj_del(dev, ptr,
3620 							mlxsw_sp_port_dev_check,
3621 							mlxsw_sp_port_obj_del);
3622 		return notifier_from_errno(err);
3623 	case SWITCHDEV_PORT_ATTR_SET:
3624 		err = switchdev_handle_port_attr_set(dev, ptr,
3625 						     mlxsw_sp_port_dev_check,
3626 						     mlxsw_sp_port_attr_set);
3627 		return notifier_from_errno(err);
3628 	}
3629 
3630 	return NOTIFY_DONE;
3631 }
3632 
3633 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3634 	.notifier_call = mlxsw_sp_switchdev_blocking_event,
3635 };
3636 
3637 u8
3638 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3639 {
3640 	return bridge_port->stp_state;
3641 }
3642 
3643 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3644 {
3645 	struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3646 	struct notifier_block *nb;
3647 	int err;
3648 
3649 	err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3650 	if (err) {
3651 		dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3652 		return err;
3653 	}
3654 
3655 	err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3656 	if (err) {
3657 		dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3658 		return err;
3659 	}
3660 
3661 	nb = &mlxsw_sp_switchdev_blocking_notifier;
3662 	err = register_switchdev_blocking_notifier(nb);
3663 	if (err) {
3664 		dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3665 		goto err_register_switchdev_blocking_notifier;
3666 	}
3667 
3668 	INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3669 	bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3670 	mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false);
3671 	return 0;
3672 
3673 err_register_switchdev_blocking_notifier:
3674 	unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3675 	return err;
3676 }
3677 
3678 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3679 {
3680 	struct notifier_block *nb;
3681 
3682 	cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3683 
3684 	nb = &mlxsw_sp_switchdev_blocking_notifier;
3685 	unregister_switchdev_blocking_notifier(nb);
3686 
3687 	unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3688 }
3689 
3690 static void mlxsw_sp1_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3691 {
3692 	mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp1_bridge_8021ad_ops;
3693 }
3694 
3695 const struct mlxsw_sp_switchdev_ops mlxsw_sp1_switchdev_ops = {
3696 	.init	= mlxsw_sp1_switchdev_init,
3697 };
3698 
3699 static void mlxsw_sp2_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3700 {
3701 	mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp2_bridge_8021ad_ops;
3702 }
3703 
3704 const struct mlxsw_sp_switchdev_ops mlxsw_sp2_switchdev_ops = {
3705 	.init	= mlxsw_sp2_switchdev_init,
3706 };
3707 
3708 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3709 {
3710 	struct mlxsw_sp_bridge *bridge;
3711 
3712 	bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3713 	if (!bridge)
3714 		return -ENOMEM;
3715 	mlxsw_sp->bridge = bridge;
3716 	bridge->mlxsw_sp = mlxsw_sp;
3717 
3718 	INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3719 
3720 	bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3721 	bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3722 
3723 	mlxsw_sp->switchdev_ops->init(mlxsw_sp);
3724 
3725 	return mlxsw_sp_fdb_init(mlxsw_sp);
3726 }
3727 
3728 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3729 {
3730 	mlxsw_sp_fdb_fini(mlxsw_sp);
3731 	WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3732 	kfree(mlxsw_sp->bridge);
3733 }
3734 
3735