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