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