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