1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
3 
4 #include <linux/if_bridge.h>
5 #include <linux/if_vlan.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/notifier.h>
9 #include <net/netevent.h>
10 #include <net/switchdev.h>
11 
12 #include "prestera.h"
13 #include "prestera_hw.h"
14 #include "prestera_switchdev.h"
15 
16 #define PRESTERA_VID_ALL (0xffff)
17 
18 #define PRESTERA_DEFAULT_AGEING_TIME_MS 300000
19 #define PRESTERA_MAX_AGEING_TIME_MS 1000000000
20 #define PRESTERA_MIN_AGEING_TIME_MS 32000
21 
22 struct prestera_fdb_event_work {
23 	struct work_struct work;
24 	struct switchdev_notifier_fdb_info fdb_info;
25 	struct net_device *dev;
26 	unsigned long event;
27 };
28 
29 struct prestera_switchdev {
30 	struct prestera_switch *sw;
31 	struct list_head bridge_list;
32 	bool bridge_8021q_exists;
33 	struct notifier_block swdev_nb_blk;
34 	struct notifier_block swdev_nb;
35 };
36 
37 struct prestera_bridge {
38 	struct list_head head;
39 	struct net_device *dev;
40 	struct prestera_switchdev *swdev;
41 	struct list_head port_list;
42 	struct list_head br_mdb_entry_list;
43 	bool mrouter_exist;
44 	bool vlan_enabled;
45 	bool multicast_enabled;
46 	u16 bridge_id;
47 };
48 
49 struct prestera_bridge_port {
50 	struct list_head head;
51 	struct net_device *dev;
52 	struct prestera_bridge *bridge;
53 	struct list_head vlan_list;
54 	struct list_head br_mdb_port_list;
55 	refcount_t ref_count;
56 	unsigned long flags;
57 	bool mrouter;
58 	u8 stp_state;
59 };
60 
61 struct prestera_bridge_vlan {
62 	struct list_head head;
63 	struct list_head port_vlan_list;
64 	u16 vid;
65 };
66 
67 struct prestera_port_vlan {
68 	struct list_head br_vlan_head;
69 	struct list_head port_head;
70 	struct prestera_port *port;
71 	struct prestera_bridge_port *br_port;
72 	u16 vid;
73 };
74 
75 struct prestera_br_mdb_port {
76 	struct prestera_bridge_port *br_port;
77 	struct list_head br_mdb_port_node;
78 };
79 
80 /* Software representation of MDB table. */
81 struct prestera_br_mdb_entry {
82 	struct prestera_bridge *bridge;
83 	struct prestera_mdb_entry *mdb;
84 	struct list_head br_mdb_port_list;
85 	struct list_head br_mdb_entry_node;
86 	bool enabled;
87 };
88 
89 static struct workqueue_struct *swdev_wq;
90 
91 static void prestera_bridge_port_put(struct prestera_bridge_port *br_port);
92 
93 static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
94 				     u8 state);
95 
96 static struct prestera_bridge *
97 prestera_bridge_find(const struct prestera_switch *sw,
98 		     const struct net_device *br_dev)
99 {
100 	struct prestera_bridge *bridge;
101 
102 	list_for_each_entry(bridge, &sw->swdev->bridge_list, head)
103 		if (bridge->dev == br_dev)
104 			return bridge;
105 
106 	return NULL;
107 }
108 
109 static struct prestera_bridge_port *
110 __prestera_bridge_port_find(const struct prestera_bridge *bridge,
111 			    const struct net_device *brport_dev)
112 {
113 	struct prestera_bridge_port *br_port;
114 
115 	list_for_each_entry(br_port, &bridge->port_list, head)
116 		if (br_port->dev == brport_dev)
117 			return br_port;
118 
119 	return NULL;
120 }
121 
122 static struct prestera_bridge_port *
123 prestera_bridge_port_find(struct prestera_switch *sw,
124 			  struct net_device *brport_dev)
125 {
126 	struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
127 	struct prestera_bridge *bridge;
128 
129 	if (!br_dev)
130 		return NULL;
131 
132 	bridge = prestera_bridge_find(sw, br_dev);
133 	if (!bridge)
134 		return NULL;
135 
136 	return __prestera_bridge_port_find(bridge, brport_dev);
137 }
138 
139 static void
140 prestera_br_port_flags_reset(struct prestera_bridge_port *br_port,
141 			     struct prestera_port *port)
142 {
143 	prestera_port_uc_flood_set(port, false);
144 	prestera_port_mc_flood_set(port, false);
145 	prestera_port_learning_set(port, false);
146 }
147 
148 static int prestera_br_port_flags_set(struct prestera_bridge_port *br_port,
149 				      struct prestera_port *port)
150 {
151 	int err;
152 
153 	err = prestera_port_uc_flood_set(port, br_port->flags & BR_FLOOD);
154 	if (err)
155 		goto err_out;
156 
157 	err = prestera_port_mc_flood_set(port, br_port->flags & BR_MCAST_FLOOD);
158 	if (err)
159 		goto err_out;
160 
161 	err = prestera_port_learning_set(port, br_port->flags & BR_LEARNING);
162 	if (err)
163 		goto err_out;
164 
165 	return 0;
166 
167 err_out:
168 	prestera_br_port_flags_reset(br_port, port);
169 	return err;
170 }
171 
172 static struct prestera_bridge_vlan *
173 prestera_bridge_vlan_create(struct prestera_bridge_port *br_port, u16 vid)
174 {
175 	struct prestera_bridge_vlan *br_vlan;
176 
177 	br_vlan = kzalloc(sizeof(*br_vlan), GFP_KERNEL);
178 	if (!br_vlan)
179 		return NULL;
180 
181 	INIT_LIST_HEAD(&br_vlan->port_vlan_list);
182 	br_vlan->vid = vid;
183 	list_add(&br_vlan->head, &br_port->vlan_list);
184 
185 	return br_vlan;
186 }
187 
188 static void prestera_bridge_vlan_destroy(struct prestera_bridge_vlan *br_vlan)
189 {
190 	list_del(&br_vlan->head);
191 	WARN_ON(!list_empty(&br_vlan->port_vlan_list));
192 	kfree(br_vlan);
193 }
194 
195 static struct prestera_bridge_vlan *
196 prestera_bridge_vlan_by_vid(struct prestera_bridge_port *br_port, u16 vid)
197 {
198 	struct prestera_bridge_vlan *br_vlan;
199 
200 	list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
201 		if (br_vlan->vid == vid)
202 			return br_vlan;
203 	}
204 
205 	return NULL;
206 }
207 
208 static int prestera_bridge_vlan_port_count(struct prestera_bridge *bridge,
209 					   u16 vid)
210 {
211 	struct prestera_bridge_port *br_port;
212 	struct prestera_bridge_vlan *br_vlan;
213 	int count = 0;
214 
215 	list_for_each_entry(br_port, &bridge->port_list, head) {
216 		list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
217 			if (br_vlan->vid == vid) {
218 				count += 1;
219 				break;
220 			}
221 		}
222 	}
223 
224 	return count;
225 }
226 
227 static void prestera_bridge_vlan_put(struct prestera_bridge_vlan *br_vlan)
228 {
229 	if (list_empty(&br_vlan->port_vlan_list))
230 		prestera_bridge_vlan_destroy(br_vlan);
231 }
232 
233 static struct prestera_port_vlan *
234 prestera_port_vlan_by_vid(struct prestera_port *port, u16 vid)
235 {
236 	struct prestera_port_vlan *port_vlan;
237 
238 	list_for_each_entry(port_vlan, &port->vlans_list, port_head) {
239 		if (port_vlan->vid == vid)
240 			return port_vlan;
241 	}
242 
243 	return NULL;
244 }
245 
246 static struct prestera_port_vlan *
247 prestera_port_vlan_create(struct prestera_port *port, u16 vid, bool untagged)
248 {
249 	struct prestera_port_vlan *port_vlan;
250 	int err;
251 
252 	port_vlan = prestera_port_vlan_by_vid(port, vid);
253 	if (port_vlan)
254 		return ERR_PTR(-EEXIST);
255 
256 	err = prestera_hw_vlan_port_set(port, vid, true, untagged);
257 	if (err)
258 		return ERR_PTR(err);
259 
260 	port_vlan = kzalloc(sizeof(*port_vlan), GFP_KERNEL);
261 	if (!port_vlan) {
262 		err = -ENOMEM;
263 		goto err_port_vlan_alloc;
264 	}
265 
266 	port_vlan->port = port;
267 	port_vlan->vid = vid;
268 
269 	list_add(&port_vlan->port_head, &port->vlans_list);
270 
271 	return port_vlan;
272 
273 err_port_vlan_alloc:
274 	prestera_hw_vlan_port_set(port, vid, false, false);
275 	return ERR_PTR(err);
276 }
277 
278 static int prestera_fdb_add(struct prestera_port *port,
279 			    const unsigned char *mac, u16 vid, bool dynamic)
280 {
281 	if (prestera_port_is_lag_member(port))
282 		return prestera_hw_lag_fdb_add(port->sw, prestera_port_lag_id(port),
283 					      mac, vid, dynamic);
284 
285 	return prestera_hw_fdb_add(port, mac, vid, dynamic);
286 }
287 
288 static int prestera_fdb_del(struct prestera_port *port,
289 			    const unsigned char *mac, u16 vid)
290 {
291 	if (prestera_port_is_lag_member(port))
292 		return prestera_hw_lag_fdb_del(port->sw, prestera_port_lag_id(port),
293 					      mac, vid);
294 	else
295 		return prestera_hw_fdb_del(port, mac, vid);
296 }
297 
298 static int prestera_fdb_flush_port_vlan(struct prestera_port *port, u16 vid,
299 					u32 mode)
300 {
301 	if (prestera_port_is_lag_member(port))
302 		return prestera_hw_fdb_flush_lag_vlan(port->sw, prestera_port_lag_id(port),
303 						      vid, mode);
304 	else
305 		return prestera_hw_fdb_flush_port_vlan(port, vid, mode);
306 }
307 
308 static int prestera_fdb_flush_port(struct prestera_port *port, u32 mode)
309 {
310 	if (prestera_port_is_lag_member(port))
311 		return prestera_hw_fdb_flush_lag(port->sw, prestera_port_lag_id(port),
312 						 mode);
313 	else
314 		return prestera_hw_fdb_flush_port(port, mode);
315 }
316 
317 static void
318 prestera_mdb_port_del(struct prestera_mdb_entry *mdb,
319 		      struct net_device *orig_dev)
320 {
321 	struct prestera_flood_domain *fl_domain = mdb->flood_domain;
322 	struct prestera_flood_domain_port *flood_domain_port;
323 
324 	flood_domain_port = prestera_flood_domain_port_find(fl_domain,
325 							    orig_dev,
326 							    mdb->vid);
327 	if (flood_domain_port)
328 		prestera_flood_domain_port_destroy(flood_domain_port);
329 }
330 
331 static void
332 prestera_br_mdb_entry_put(struct prestera_br_mdb_entry *br_mdb)
333 {
334 	struct prestera_bridge_port *br_port;
335 
336 	if (list_empty(&br_mdb->br_mdb_port_list)) {
337 		list_for_each_entry(br_port, &br_mdb->bridge->port_list, head)
338 			prestera_mdb_port_del(br_mdb->mdb, br_port->dev);
339 
340 		prestera_mdb_entry_destroy(br_mdb->mdb);
341 		list_del(&br_mdb->br_mdb_entry_node);
342 		kfree(br_mdb);
343 	}
344 }
345 
346 static void
347 prestera_br_mdb_port_del(struct prestera_br_mdb_entry *br_mdb,
348 			 struct prestera_bridge_port *br_port)
349 {
350 	struct prestera_br_mdb_port *br_mdb_port, *tmp;
351 
352 	list_for_each_entry_safe(br_mdb_port, tmp, &br_mdb->br_mdb_port_list,
353 				 br_mdb_port_node) {
354 		if (br_mdb_port->br_port == br_port) {
355 			list_del(&br_mdb_port->br_mdb_port_node);
356 			kfree(br_mdb_port);
357 		}
358 	}
359 }
360 
361 static void
362 prestera_mdb_flush_bridge_port(struct prestera_bridge_port *br_port)
363 {
364 	struct prestera_br_mdb_port *br_mdb_port, *tmp_port;
365 	struct prestera_br_mdb_entry *br_mdb, *br_mdb_tmp;
366 	struct prestera_bridge *br_dev = br_port->bridge;
367 
368 	list_for_each_entry_safe(br_mdb, br_mdb_tmp, &br_dev->br_mdb_entry_list,
369 				 br_mdb_entry_node) {
370 		list_for_each_entry_safe(br_mdb_port, tmp_port,
371 					 &br_mdb->br_mdb_port_list,
372 					 br_mdb_port_node) {
373 			prestera_mdb_port_del(br_mdb->mdb,
374 					      br_mdb_port->br_port->dev);
375 			prestera_br_mdb_port_del(br_mdb,  br_mdb_port->br_port);
376 		}
377 		prestera_br_mdb_entry_put(br_mdb);
378 	}
379 }
380 
381 static void
382 prestera_port_vlan_bridge_leave(struct prestera_port_vlan *port_vlan)
383 {
384 	u32 fdb_flush_mode = PRESTERA_FDB_FLUSH_MODE_DYNAMIC;
385 	struct prestera_port *port = port_vlan->port;
386 	struct prestera_bridge_vlan *br_vlan;
387 	struct prestera_bridge_port *br_port;
388 	bool last_port, last_vlan;
389 	u16 vid = port_vlan->vid;
390 	int port_count;
391 
392 	br_port = port_vlan->br_port;
393 	port_count = prestera_bridge_vlan_port_count(br_port->bridge, vid);
394 	br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
395 
396 	last_vlan = list_is_singular(&br_port->vlan_list);
397 	last_port = port_count == 1;
398 
399 	if (last_vlan)
400 		prestera_fdb_flush_port(port, fdb_flush_mode);
401 	else if (last_port)
402 		prestera_hw_fdb_flush_vlan(port->sw, vid, fdb_flush_mode);
403 	else
404 		prestera_fdb_flush_port_vlan(port, vid, fdb_flush_mode);
405 
406 	prestera_mdb_flush_bridge_port(br_port);
407 
408 	list_del(&port_vlan->br_vlan_head);
409 	prestera_bridge_vlan_put(br_vlan);
410 	prestera_bridge_port_put(br_port);
411 	port_vlan->br_port = NULL;
412 }
413 
414 static void prestera_port_vlan_destroy(struct prestera_port_vlan *port_vlan)
415 {
416 	struct prestera_port *port = port_vlan->port;
417 	u16 vid = port_vlan->vid;
418 
419 	if (port_vlan->br_port)
420 		prestera_port_vlan_bridge_leave(port_vlan);
421 
422 	prestera_hw_vlan_port_set(port, vid, false, false);
423 	list_del(&port_vlan->port_head);
424 	kfree(port_vlan);
425 }
426 
427 static struct prestera_bridge *
428 prestera_bridge_create(struct prestera_switchdev *swdev, struct net_device *dev)
429 {
430 	bool vlan_enabled = br_vlan_enabled(dev);
431 	struct prestera_bridge *bridge;
432 	u16 bridge_id;
433 	int err;
434 
435 	if (vlan_enabled && swdev->bridge_8021q_exists) {
436 		netdev_err(dev, "Only one VLAN-aware bridge is supported\n");
437 		return ERR_PTR(-EINVAL);
438 	}
439 
440 	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
441 	if (!bridge)
442 		return ERR_PTR(-ENOMEM);
443 
444 	if (vlan_enabled) {
445 		swdev->bridge_8021q_exists = true;
446 	} else {
447 		err = prestera_hw_bridge_create(swdev->sw, &bridge_id);
448 		if (err) {
449 			kfree(bridge);
450 			return ERR_PTR(err);
451 		}
452 
453 		bridge->bridge_id = bridge_id;
454 	}
455 
456 	bridge->vlan_enabled = vlan_enabled;
457 	bridge->swdev = swdev;
458 	bridge->dev = dev;
459 	bridge->multicast_enabled = br_multicast_enabled(dev);
460 
461 	INIT_LIST_HEAD(&bridge->port_list);
462 	INIT_LIST_HEAD(&bridge->br_mdb_entry_list);
463 
464 	list_add(&bridge->head, &swdev->bridge_list);
465 
466 	return bridge;
467 }
468 
469 static void prestera_bridge_destroy(struct prestera_bridge *bridge)
470 {
471 	struct prestera_switchdev *swdev = bridge->swdev;
472 
473 	list_del(&bridge->head);
474 
475 	if (bridge->vlan_enabled)
476 		swdev->bridge_8021q_exists = false;
477 	else
478 		prestera_hw_bridge_delete(swdev->sw, bridge->bridge_id);
479 
480 	WARN_ON(!list_empty(&bridge->br_mdb_entry_list));
481 	WARN_ON(!list_empty(&bridge->port_list));
482 	kfree(bridge);
483 }
484 
485 static void prestera_bridge_put(struct prestera_bridge *bridge)
486 {
487 	if (list_empty(&bridge->port_list))
488 		prestera_bridge_destroy(bridge);
489 }
490 
491 static
492 struct prestera_bridge *prestera_bridge_by_dev(struct prestera_switchdev *swdev,
493 					       const struct net_device *dev)
494 {
495 	struct prestera_bridge *bridge;
496 
497 	list_for_each_entry(bridge, &swdev->bridge_list, head)
498 		if (bridge->dev == dev)
499 			return bridge;
500 
501 	return NULL;
502 }
503 
504 static struct prestera_bridge_port *
505 __prestera_bridge_port_by_dev(struct prestera_bridge *bridge,
506 			      struct net_device *dev)
507 {
508 	struct prestera_bridge_port *br_port;
509 
510 	list_for_each_entry(br_port, &bridge->port_list, head) {
511 		if (br_port->dev == dev)
512 			return br_port;
513 	}
514 
515 	return NULL;
516 }
517 
518 static int prestera_match_upper_bridge_dev(struct net_device *dev,
519 					   struct netdev_nested_priv *priv)
520 {
521 	if (netif_is_bridge_master(dev))
522 		priv->data = dev;
523 
524 	return 0;
525 }
526 
527 static struct net_device *prestera_get_upper_bridge_dev(struct net_device *dev)
528 {
529 	struct netdev_nested_priv priv = { };
530 
531 	netdev_walk_all_upper_dev_rcu(dev, prestera_match_upper_bridge_dev,
532 				      &priv);
533 	return priv.data;
534 }
535 
536 static struct prestera_bridge_port *
537 prestera_bridge_port_by_dev(struct prestera_switchdev *swdev,
538 			    struct net_device *dev)
539 {
540 	struct net_device *br_dev = prestera_get_upper_bridge_dev(dev);
541 	struct prestera_bridge *bridge;
542 
543 	if (!br_dev)
544 		return NULL;
545 
546 	bridge = prestera_bridge_by_dev(swdev, br_dev);
547 	if (!bridge)
548 		return NULL;
549 
550 	return __prestera_bridge_port_by_dev(bridge, dev);
551 }
552 
553 static struct prestera_bridge_port *
554 prestera_bridge_port_create(struct prestera_bridge *bridge,
555 			    struct net_device *dev)
556 {
557 	struct prestera_bridge_port *br_port;
558 
559 	br_port = kzalloc(sizeof(*br_port), GFP_KERNEL);
560 	if (!br_port)
561 		return NULL;
562 
563 	br_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
564 				BR_MCAST_FLOOD;
565 	br_port->stp_state = BR_STATE_DISABLED;
566 	refcount_set(&br_port->ref_count, 1);
567 	br_port->bridge = bridge;
568 	br_port->dev = dev;
569 
570 	INIT_LIST_HEAD(&br_port->vlan_list);
571 	list_add(&br_port->head, &bridge->port_list);
572 	INIT_LIST_HEAD(&br_port->br_mdb_port_list);
573 
574 	return br_port;
575 }
576 
577 static void
578 prestera_bridge_port_destroy(struct prestera_bridge_port *br_port)
579 {
580 	list_del(&br_port->head);
581 	WARN_ON(!list_empty(&br_port->vlan_list));
582 	WARN_ON(!list_empty(&br_port->br_mdb_port_list));
583 	kfree(br_port);
584 }
585 
586 static void prestera_bridge_port_get(struct prestera_bridge_port *br_port)
587 {
588 	refcount_inc(&br_port->ref_count);
589 }
590 
591 static void prestera_bridge_port_put(struct prestera_bridge_port *br_port)
592 {
593 	struct prestera_bridge *bridge = br_port->bridge;
594 
595 	if (refcount_dec_and_test(&br_port->ref_count)) {
596 		prestera_bridge_port_destroy(br_port);
597 		prestera_bridge_put(bridge);
598 	}
599 }
600 
601 static struct prestera_bridge_port *
602 prestera_bridge_port_add(struct prestera_bridge *bridge, struct net_device *dev)
603 {
604 	struct prestera_bridge_port *br_port;
605 
606 	br_port = __prestera_bridge_port_by_dev(bridge, dev);
607 	if (br_port) {
608 		prestera_bridge_port_get(br_port);
609 		return br_port;
610 	}
611 
612 	br_port = prestera_bridge_port_create(bridge, dev);
613 	if (!br_port)
614 		return ERR_PTR(-ENOMEM);
615 
616 	return br_port;
617 }
618 
619 static int
620 prestera_bridge_1d_port_join(struct prestera_bridge_port *br_port)
621 {
622 	struct prestera_port *port = netdev_priv(br_port->dev);
623 	struct prestera_bridge *bridge = br_port->bridge;
624 	int err;
625 
626 	err = prestera_hw_bridge_port_add(port, bridge->bridge_id);
627 	if (err)
628 		return err;
629 
630 	err = prestera_br_port_flags_set(br_port, port);
631 	if (err)
632 		goto err_flags2port_set;
633 
634 	return 0;
635 
636 err_flags2port_set:
637 	prestera_hw_bridge_port_delete(port, bridge->bridge_id);
638 
639 	return err;
640 }
641 
642 int prestera_bridge_port_join(struct net_device *br_dev,
643 			      struct prestera_port *port,
644 			      struct netlink_ext_ack *extack)
645 {
646 	struct prestera_switchdev *swdev = port->sw->swdev;
647 	struct prestera_bridge_port *br_port;
648 	struct prestera_bridge *bridge;
649 	int err;
650 
651 	bridge = prestera_bridge_by_dev(swdev, br_dev);
652 	if (!bridge) {
653 		bridge = prestera_bridge_create(swdev, br_dev);
654 		if (IS_ERR(bridge))
655 			return PTR_ERR(bridge);
656 	}
657 
658 	br_port = prestera_bridge_port_add(bridge, port->dev);
659 	if (IS_ERR(br_port)) {
660 		prestera_bridge_put(bridge);
661 		return PTR_ERR(br_port);
662 	}
663 
664 	err = switchdev_bridge_port_offload(br_port->dev, port->dev, NULL,
665 					    NULL, NULL, false, extack);
666 	if (err)
667 		goto err_switchdev_offload;
668 
669 	if (bridge->vlan_enabled)
670 		return 0;
671 
672 	err = prestera_bridge_1d_port_join(br_port);
673 	if (err)
674 		goto err_port_join;
675 
676 	return 0;
677 
678 err_port_join:
679 	switchdev_bridge_port_unoffload(br_port->dev, NULL, NULL, NULL);
680 err_switchdev_offload:
681 	prestera_bridge_port_put(br_port);
682 	return err;
683 }
684 
685 static void prestera_bridge_1q_port_leave(struct prestera_bridge_port *br_port)
686 {
687 	struct prestera_port *port = netdev_priv(br_port->dev);
688 
689 	prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
690 	prestera_port_pvid_set(port, PRESTERA_DEFAULT_VID);
691 }
692 
693 static void prestera_bridge_1d_port_leave(struct prestera_bridge_port *br_port)
694 {
695 	struct prestera_port *port = netdev_priv(br_port->dev);
696 
697 	prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
698 	prestera_hw_bridge_port_delete(port, br_port->bridge->bridge_id);
699 }
700 
701 static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
702 				     u8 state)
703 {
704 	u8 hw_state = state;
705 
706 	switch (state) {
707 	case BR_STATE_DISABLED:
708 		hw_state = PRESTERA_STP_DISABLED;
709 		break;
710 
711 	case BR_STATE_BLOCKING:
712 	case BR_STATE_LISTENING:
713 		hw_state = PRESTERA_STP_BLOCK_LISTEN;
714 		break;
715 
716 	case BR_STATE_LEARNING:
717 		hw_state = PRESTERA_STP_LEARN;
718 		break;
719 
720 	case BR_STATE_FORWARDING:
721 		hw_state = PRESTERA_STP_FORWARD;
722 		break;
723 
724 	default:
725 		return -EINVAL;
726 	}
727 
728 	return prestera_hw_vlan_port_stp_set(port, vid, hw_state);
729 }
730 
731 void prestera_bridge_port_leave(struct net_device *br_dev,
732 				struct prestera_port *port)
733 {
734 	struct prestera_switchdev *swdev = port->sw->swdev;
735 	struct prestera_bridge_port *br_port;
736 	struct prestera_bridge *bridge;
737 
738 	bridge = prestera_bridge_by_dev(swdev, br_dev);
739 	if (!bridge)
740 		return;
741 
742 	br_port = __prestera_bridge_port_by_dev(bridge, port->dev);
743 	if (!br_port)
744 		return;
745 
746 	bridge = br_port->bridge;
747 
748 	if (bridge->vlan_enabled)
749 		prestera_bridge_1q_port_leave(br_port);
750 	else
751 		prestera_bridge_1d_port_leave(br_port);
752 
753 	switchdev_bridge_port_unoffload(br_port->dev, NULL, NULL, NULL);
754 
755 	prestera_mdb_flush_bridge_port(br_port);
756 
757 	prestera_br_port_flags_reset(br_port, port);
758 	prestera_port_vid_stp_set(port, PRESTERA_VID_ALL, BR_STATE_FORWARDING);
759 	prestera_bridge_port_put(br_port);
760 }
761 
762 static int prestera_port_attr_br_flags_set(struct prestera_port *port,
763 					   struct net_device *dev,
764 					   struct switchdev_brport_flags flags)
765 {
766 	struct prestera_bridge_port *br_port;
767 
768 	br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
769 	if (!br_port)
770 		return 0;
771 
772 	br_port->flags &= ~flags.mask;
773 	br_port->flags |= flags.val & flags.mask;
774 	return prestera_br_port_flags_set(br_port, port);
775 }
776 
777 static int prestera_port_attr_br_ageing_set(struct prestera_port *port,
778 					    unsigned long ageing_clock_t)
779 {
780 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
781 	u32 ageing_time_ms = jiffies_to_msecs(ageing_jiffies);
782 	struct prestera_switch *sw = port->sw;
783 
784 	if (ageing_time_ms < PRESTERA_MIN_AGEING_TIME_MS ||
785 	    ageing_time_ms > PRESTERA_MAX_AGEING_TIME_MS)
786 		return -ERANGE;
787 
788 	return prestera_hw_switch_ageing_set(sw, ageing_time_ms);
789 }
790 
791 static int prestera_port_attr_br_vlan_set(struct prestera_port *port,
792 					  struct net_device *dev,
793 					  bool vlan_enabled)
794 {
795 	struct prestera_switch *sw = port->sw;
796 	struct prestera_bridge *bridge;
797 
798 	bridge = prestera_bridge_by_dev(sw->swdev, dev);
799 	if (WARN_ON(!bridge))
800 		return -EINVAL;
801 
802 	if (bridge->vlan_enabled == vlan_enabled)
803 		return 0;
804 
805 	netdev_err(bridge->dev, "VLAN filtering can't be changed for existing bridge\n");
806 
807 	return -EINVAL;
808 }
809 
810 static int prestera_port_bridge_vlan_stp_set(struct prestera_port *port,
811 					     struct prestera_bridge_vlan *br_vlan,
812 					     u8 state)
813 {
814 	struct prestera_port_vlan *port_vlan;
815 
816 	list_for_each_entry(port_vlan, &br_vlan->port_vlan_list, br_vlan_head) {
817 		if (port_vlan->port != port)
818 			continue;
819 
820 		return prestera_port_vid_stp_set(port, br_vlan->vid, state);
821 	}
822 
823 	return 0;
824 }
825 
826 static int prestera_port_attr_stp_state_set(struct prestera_port *port,
827 					    struct net_device *dev,
828 					    u8 state)
829 {
830 	struct prestera_bridge_port *br_port;
831 	struct prestera_bridge_vlan *br_vlan;
832 	int err;
833 	u16 vid;
834 
835 	br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
836 	if (!br_port)
837 		return 0;
838 
839 	if (!br_port->bridge->vlan_enabled) {
840 		vid = br_port->bridge->bridge_id;
841 		err = prestera_port_vid_stp_set(port, vid, state);
842 		if (err)
843 			goto err_port_stp_set;
844 	} else {
845 		list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
846 			err = prestera_port_bridge_vlan_stp_set(port, br_vlan,
847 								state);
848 			if (err)
849 				goto err_port_vlan_stp_set;
850 		}
851 	}
852 
853 	br_port->stp_state = state;
854 
855 	return 0;
856 
857 err_port_vlan_stp_set:
858 	list_for_each_entry_continue_reverse(br_vlan, &br_port->vlan_list, head)
859 		prestera_port_bridge_vlan_stp_set(port, br_vlan, br_port->stp_state);
860 	return err;
861 
862 err_port_stp_set:
863 	prestera_port_vid_stp_set(port, vid, br_port->stp_state);
864 
865 	return err;
866 }
867 
868 static int
869 prestera_br_port_lag_mdb_mc_enable_sync(struct prestera_bridge_port *br_port,
870 					bool enabled)
871 {
872 	struct prestera_port *pr_port;
873 	struct prestera_switch *sw;
874 	u16 lag_id;
875 	int err;
876 
877 	pr_port = prestera_port_dev_lower_find(br_port->dev);
878 	if (!pr_port)
879 		return 0;
880 
881 	sw = pr_port->sw;
882 	err = prestera_lag_id(sw, br_port->dev, &lag_id);
883 	if (err)
884 		return err;
885 
886 	list_for_each_entry(pr_port, &sw->port_list, list) {
887 		if (pr_port->lag->lag_id == lag_id) {
888 			err = prestera_port_mc_flood_set(pr_port, enabled);
889 			if (err)
890 				return err;
891 		}
892 	}
893 
894 	return 0;
895 }
896 
897 static int prestera_br_mdb_mc_enable_sync(struct prestera_bridge *br_dev)
898 {
899 	struct prestera_bridge_port *br_port;
900 	struct prestera_port *port;
901 	bool enabled;
902 	int err;
903 
904 	/* if mrouter exists:
905 	 *  - make sure every mrouter receives unreg mcast traffic;
906 	 * if mrouter doesn't exists:
907 	 *  - make sure every port receives unreg mcast traffic;
908 	 */
909 	list_for_each_entry(br_port, &br_dev->port_list, head) {
910 		if (br_dev->multicast_enabled && br_dev->mrouter_exist)
911 			enabled = br_port->mrouter;
912 		else
913 			enabled = br_port->flags & BR_MCAST_FLOOD;
914 
915 		if (netif_is_lag_master(br_port->dev)) {
916 			err = prestera_br_port_lag_mdb_mc_enable_sync(br_port,
917 								      enabled);
918 			if (err)
919 				return err;
920 			continue;
921 		}
922 
923 		port = prestera_port_dev_lower_find(br_port->dev);
924 		if (!port)
925 			continue;
926 
927 		err = prestera_port_mc_flood_set(port, enabled);
928 		if (err)
929 			return err;
930 	}
931 
932 	return 0;
933 }
934 
935 static bool
936 prestera_br_mdb_port_is_member(struct prestera_br_mdb_entry *br_mdb,
937 			       struct net_device *orig_dev)
938 {
939 	struct prestera_br_mdb_port *tmp_port;
940 
941 	list_for_each_entry(tmp_port, &br_mdb->br_mdb_port_list,
942 			    br_mdb_port_node)
943 		if (tmp_port->br_port->dev == orig_dev)
944 			return true;
945 
946 	return false;
947 }
948 
949 static int
950 prestera_mdb_port_add(struct prestera_mdb_entry *mdb,
951 		      struct net_device *orig_dev,
952 		      const unsigned char addr[ETH_ALEN], u16 vid)
953 {
954 	struct prestera_flood_domain *flood_domain = mdb->flood_domain;
955 	int err;
956 
957 	if (!prestera_flood_domain_port_find(flood_domain,
958 					     orig_dev, vid)) {
959 		err = prestera_flood_domain_port_create(flood_domain, orig_dev,
960 							vid);
961 		if (err)
962 			return err;
963 	}
964 
965 	return 0;
966 }
967 
968 /* Sync bridge mdb (software table) with HW table (if MC is enabled). */
969 static int prestera_br_mdb_sync(struct prestera_bridge *br_dev)
970 {
971 	struct prestera_br_mdb_port *br_mdb_port;
972 	struct prestera_bridge_port *br_port;
973 	struct prestera_br_mdb_entry *br_mdb;
974 	struct prestera_mdb_entry *mdb;
975 	struct prestera_port *pr_port;
976 	int err = 0;
977 
978 	if (!br_dev->multicast_enabled)
979 		return 0;
980 
981 	list_for_each_entry(br_mdb, &br_dev->br_mdb_entry_list,
982 			    br_mdb_entry_node) {
983 		mdb = br_mdb->mdb;
984 		/* Make sure every port that explicitly been added to the mdb
985 		 * joins the specified group.
986 		 */
987 		list_for_each_entry(br_mdb_port, &br_mdb->br_mdb_port_list,
988 				    br_mdb_port_node) {
989 			br_port = br_mdb_port->br_port;
990 			pr_port = prestera_port_dev_lower_find(br_port->dev);
991 
992 			/* Match only mdb and br_mdb ports that belong to the
993 			 * same broadcast domain.
994 			 */
995 			if (br_dev->vlan_enabled &&
996 			    !prestera_port_vlan_by_vid(pr_port,
997 						       mdb->vid))
998 				continue;
999 
1000 			/* If port is not in MDB or there's no Mrouter
1001 			 * clear HW mdb.
1002 			 */
1003 			if (prestera_br_mdb_port_is_member(br_mdb,
1004 							   br_mdb_port->br_port->dev) &&
1005 							   br_dev->mrouter_exist)
1006 				err = prestera_mdb_port_add(mdb, br_port->dev,
1007 							    mdb->addr,
1008 							    mdb->vid);
1009 			else
1010 				prestera_mdb_port_del(mdb, br_port->dev);
1011 
1012 			if (err)
1013 				return err;
1014 		}
1015 
1016 		/* Make sure that every mrouter port joins every MC group int
1017 		 * broadcast domain. If it's not an mrouter - it should leave
1018 		 */
1019 		list_for_each_entry(br_port, &br_dev->port_list, head) {
1020 			pr_port = prestera_port_dev_lower_find(br_port->dev);
1021 
1022 			/* Make sure mrouter woudln't receive traffci from
1023 			 * another broadcast domain (e.g. from a vlan, which
1024 			 * mrouter port is not a member of).
1025 			 */
1026 			if (br_dev->vlan_enabled &&
1027 			    !prestera_port_vlan_by_vid(pr_port,
1028 						       mdb->vid))
1029 				continue;
1030 
1031 			if (br_port->mrouter) {
1032 				err = prestera_mdb_port_add(mdb, br_port->dev,
1033 							    mdb->addr,
1034 							    mdb->vid);
1035 				if (err)
1036 					return err;
1037 			} else if (!br_port->mrouter &&
1038 				   !prestera_br_mdb_port_is_member
1039 				   (br_mdb, br_port->dev)) {
1040 				prestera_mdb_port_del(mdb, br_port->dev);
1041 			}
1042 		}
1043 	}
1044 
1045 	return 0;
1046 }
1047 
1048 static int
1049 prestera_mdb_enable_set(struct prestera_br_mdb_entry *br_mdb, bool enable)
1050 {
1051 	int err;
1052 
1053 	if (enable != br_mdb->enabled) {
1054 		if (enable)
1055 			err = prestera_hw_mdb_create(br_mdb->mdb);
1056 		else
1057 			err = prestera_hw_mdb_destroy(br_mdb->mdb);
1058 
1059 		if (err)
1060 			return err;
1061 
1062 		br_mdb->enabled = enable;
1063 	}
1064 
1065 	return 0;
1066 }
1067 
1068 static int
1069 prestera_br_mdb_enable_set(struct prestera_bridge *br_dev, bool enable)
1070 {
1071 	struct prestera_br_mdb_entry *br_mdb;
1072 	int err;
1073 
1074 	list_for_each_entry(br_mdb, &br_dev->br_mdb_entry_list,
1075 			    br_mdb_entry_node) {
1076 		err = prestera_mdb_enable_set(br_mdb, enable);
1077 		if (err)
1078 			return err;
1079 	}
1080 
1081 	return 0;
1082 }
1083 
1084 static int prestera_port_attr_br_mc_disabled_set(struct prestera_port *port,
1085 						 struct net_device *orig_dev,
1086 						 bool mc_disabled)
1087 {
1088 	struct prestera_switch *sw = port->sw;
1089 	struct prestera_bridge *br_dev;
1090 
1091 	br_dev = prestera_bridge_find(sw, orig_dev);
1092 	if (!br_dev)
1093 		return 0;
1094 
1095 	br_dev->multicast_enabled = !mc_disabled;
1096 
1097 	/* There's no point in enabling mdb back if router is missing. */
1098 	WARN_ON(prestera_br_mdb_enable_set(br_dev, br_dev->multicast_enabled &&
1099 					   br_dev->mrouter_exist));
1100 
1101 	WARN_ON(prestera_br_mdb_sync(br_dev));
1102 
1103 	WARN_ON(prestera_br_mdb_mc_enable_sync(br_dev));
1104 
1105 	return 0;
1106 }
1107 
1108 static bool
1109 prestera_bridge_mdb_mc_mrouter_exists(struct prestera_bridge *br_dev)
1110 {
1111 	struct prestera_bridge_port *br_port;
1112 
1113 	list_for_each_entry(br_port, &br_dev->port_list, head)
1114 		if (br_port->mrouter)
1115 			return true;
1116 
1117 	return false;
1118 }
1119 
1120 static int
1121 prestera_port_attr_mrouter_set(struct prestera_port *port,
1122 			       struct net_device *orig_dev,
1123 			       bool is_port_mrouter)
1124 {
1125 	struct prestera_bridge_port *br_port;
1126 	struct prestera_bridge *br_dev;
1127 
1128 	br_port = prestera_bridge_port_find(port->sw, orig_dev);
1129 	if (!br_port)
1130 		return 0;
1131 
1132 	br_dev = br_port->bridge;
1133 	br_port->mrouter = is_port_mrouter;
1134 
1135 	br_dev->mrouter_exist = prestera_bridge_mdb_mc_mrouter_exists(br_dev);
1136 
1137 	/* Enable MDB processing if both mrouter exists and mc is enabled.
1138 	 * In case if MC enabled, but there is no mrouter, device would flood
1139 	 * all multicast traffic (even if MDB table is not empty) with the use
1140 	 * of bridge's flood capabilities (without the use of flood_domain).
1141 	 */
1142 	WARN_ON(prestera_br_mdb_enable_set(br_dev, br_dev->multicast_enabled &&
1143 					   br_dev->mrouter_exist));
1144 
1145 	WARN_ON(prestera_br_mdb_sync(br_dev));
1146 
1147 	WARN_ON(prestera_br_mdb_mc_enable_sync(br_dev));
1148 
1149 	return 0;
1150 }
1151 
1152 static int prestera_port_obj_attr_set(struct net_device *dev, const void *ctx,
1153 				      const struct switchdev_attr *attr,
1154 				      struct netlink_ext_ack *extack)
1155 {
1156 	struct prestera_port *port = netdev_priv(dev);
1157 	int err = 0;
1158 
1159 	switch (attr->id) {
1160 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1161 		err = prestera_port_attr_stp_state_set(port, attr->orig_dev,
1162 						       attr->u.stp_state);
1163 		break;
1164 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
1165 		if (attr->u.brport_flags.mask &
1166 		    ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
1167 			err = -EINVAL;
1168 		break;
1169 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
1170 		err = prestera_port_attr_br_flags_set(port, attr->orig_dev,
1171 						      attr->u.brport_flags);
1172 		break;
1173 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1174 		err = prestera_port_attr_br_ageing_set(port,
1175 						       attr->u.ageing_time);
1176 		break;
1177 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1178 		err = prestera_port_attr_br_vlan_set(port, attr->orig_dev,
1179 						     attr->u.vlan_filtering);
1180 		break;
1181 	case SWITCHDEV_ATTR_ID_PORT_MROUTER:
1182 		err = prestera_port_attr_mrouter_set(port, attr->orig_dev,
1183 						     attr->u.mrouter);
1184 		break;
1185 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1186 		err = prestera_port_attr_br_mc_disabled_set(port, attr->orig_dev,
1187 							    attr->u.mc_disabled);
1188 		break;
1189 	default:
1190 		err = -EOPNOTSUPP;
1191 	}
1192 
1193 	return err;
1194 }
1195 
1196 static void
1197 prestera_fdb_offload_notify(struct prestera_port *port,
1198 			    struct switchdev_notifier_fdb_info *info)
1199 {
1200 	struct switchdev_notifier_fdb_info send_info = {};
1201 
1202 	send_info.addr = info->addr;
1203 	send_info.vid = info->vid;
1204 	send_info.offloaded = true;
1205 
1206 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, port->dev,
1207 				 &send_info.info, NULL);
1208 }
1209 
1210 static int prestera_port_fdb_set(struct prestera_port *port,
1211 				 struct switchdev_notifier_fdb_info *fdb_info,
1212 				 bool adding)
1213 {
1214 	struct prestera_switch *sw = port->sw;
1215 	struct prestera_bridge_port *br_port;
1216 	struct prestera_bridge *bridge;
1217 	int err;
1218 	u16 vid;
1219 
1220 	br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
1221 	if (!br_port)
1222 		return -EINVAL;
1223 
1224 	bridge = br_port->bridge;
1225 
1226 	if (bridge->vlan_enabled)
1227 		vid = fdb_info->vid;
1228 	else
1229 		vid = bridge->bridge_id;
1230 
1231 	if (adding)
1232 		err = prestera_fdb_add(port, fdb_info->addr, vid, false);
1233 	else
1234 		err = prestera_fdb_del(port, fdb_info->addr, vid);
1235 
1236 	return err;
1237 }
1238 
1239 static void prestera_fdb_event_work(struct work_struct *work)
1240 {
1241 	struct switchdev_notifier_fdb_info *fdb_info;
1242 	struct prestera_fdb_event_work *swdev_work;
1243 	struct prestera_port *port;
1244 	struct net_device *dev;
1245 	int err;
1246 
1247 	swdev_work = container_of(work, struct prestera_fdb_event_work, work);
1248 	dev = swdev_work->dev;
1249 
1250 	rtnl_lock();
1251 
1252 	port = prestera_port_dev_lower_find(dev);
1253 	if (!port)
1254 		goto out_unlock;
1255 
1256 	switch (swdev_work->event) {
1257 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1258 		fdb_info = &swdev_work->fdb_info;
1259 		if (!fdb_info->added_by_user || fdb_info->is_local)
1260 			break;
1261 
1262 		err = prestera_port_fdb_set(port, fdb_info, true);
1263 		if (err)
1264 			break;
1265 
1266 		prestera_fdb_offload_notify(port, fdb_info);
1267 		break;
1268 
1269 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1270 		fdb_info = &swdev_work->fdb_info;
1271 		prestera_port_fdb_set(port, fdb_info, false);
1272 		break;
1273 	}
1274 
1275 out_unlock:
1276 	rtnl_unlock();
1277 
1278 	kfree(swdev_work->fdb_info.addr);
1279 	kfree(swdev_work);
1280 	dev_put(dev);
1281 }
1282 
1283 static int prestera_switchdev_event(struct notifier_block *unused,
1284 				    unsigned long event, void *ptr)
1285 {
1286 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1287 	struct switchdev_notifier_fdb_info *fdb_info;
1288 	struct switchdev_notifier_info *info = ptr;
1289 	struct prestera_fdb_event_work *swdev_work;
1290 	struct net_device *upper;
1291 	int err;
1292 
1293 	if (event == SWITCHDEV_PORT_ATTR_SET) {
1294 		err = switchdev_handle_port_attr_set(dev, ptr,
1295 						     prestera_netdev_check,
1296 						     prestera_port_obj_attr_set);
1297 		return notifier_from_errno(err);
1298 	}
1299 
1300 	if (!prestera_netdev_check(dev))
1301 		return NOTIFY_DONE;
1302 
1303 	upper = netdev_master_upper_dev_get_rcu(dev);
1304 	if (!upper)
1305 		return NOTIFY_DONE;
1306 
1307 	if (!netif_is_bridge_master(upper))
1308 		return NOTIFY_DONE;
1309 
1310 	swdev_work = kzalloc(sizeof(*swdev_work), GFP_ATOMIC);
1311 	if (!swdev_work)
1312 		return NOTIFY_BAD;
1313 
1314 	swdev_work->event = event;
1315 	swdev_work->dev = dev;
1316 
1317 	switch (event) {
1318 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1319 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1320 		fdb_info = container_of(info,
1321 					struct switchdev_notifier_fdb_info,
1322 					info);
1323 
1324 		INIT_WORK(&swdev_work->work, prestera_fdb_event_work);
1325 		memcpy(&swdev_work->fdb_info, ptr,
1326 		       sizeof(swdev_work->fdb_info));
1327 
1328 		swdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1329 		if (!swdev_work->fdb_info.addr)
1330 			goto out_bad;
1331 
1332 		ether_addr_copy((u8 *)swdev_work->fdb_info.addr,
1333 				fdb_info->addr);
1334 		dev_hold(dev);
1335 		break;
1336 
1337 	default:
1338 		kfree(swdev_work);
1339 		return NOTIFY_DONE;
1340 	}
1341 
1342 	queue_work(swdev_wq, &swdev_work->work);
1343 	return NOTIFY_DONE;
1344 
1345 out_bad:
1346 	kfree(swdev_work);
1347 	return NOTIFY_BAD;
1348 }
1349 
1350 static int
1351 prestera_port_vlan_bridge_join(struct prestera_port_vlan *port_vlan,
1352 			       struct prestera_bridge_port *br_port)
1353 {
1354 	struct prestera_port *port = port_vlan->port;
1355 	struct prestera_bridge_vlan *br_vlan;
1356 	u16 vid = port_vlan->vid;
1357 	int err;
1358 
1359 	if (port_vlan->br_port)
1360 		return 0;
1361 
1362 	err = prestera_br_port_flags_set(br_port, port);
1363 	if (err)
1364 		goto err_flags2port_set;
1365 
1366 	err = prestera_port_vid_stp_set(port, vid, br_port->stp_state);
1367 	if (err)
1368 		goto err_port_vid_stp_set;
1369 
1370 	br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
1371 	if (!br_vlan) {
1372 		br_vlan = prestera_bridge_vlan_create(br_port, vid);
1373 		if (!br_vlan) {
1374 			err = -ENOMEM;
1375 			goto err_bridge_vlan_get;
1376 		}
1377 	}
1378 
1379 	list_add(&port_vlan->br_vlan_head, &br_vlan->port_vlan_list);
1380 
1381 	prestera_bridge_port_get(br_port);
1382 	port_vlan->br_port = br_port;
1383 
1384 	return 0;
1385 
1386 err_bridge_vlan_get:
1387 	prestera_port_vid_stp_set(port, vid, BR_STATE_FORWARDING);
1388 err_port_vid_stp_set:
1389 	prestera_br_port_flags_reset(br_port, port);
1390 err_flags2port_set:
1391 	return err;
1392 }
1393 
1394 static int
1395 prestera_bridge_port_vlan_add(struct prestera_port *port,
1396 			      struct prestera_bridge_port *br_port,
1397 			      u16 vid, bool is_untagged, bool is_pvid,
1398 			      struct netlink_ext_ack *extack)
1399 {
1400 	struct prestera_port_vlan *port_vlan;
1401 	u16 old_pvid = port->pvid;
1402 	u16 pvid;
1403 	int err;
1404 
1405 	if (is_pvid)
1406 		pvid = vid;
1407 	else
1408 		pvid = port->pvid == vid ? 0 : port->pvid;
1409 
1410 	port_vlan = prestera_port_vlan_by_vid(port, vid);
1411 	if (port_vlan && port_vlan->br_port != br_port)
1412 		return -EEXIST;
1413 
1414 	if (!port_vlan) {
1415 		port_vlan = prestera_port_vlan_create(port, vid, is_untagged);
1416 		if (IS_ERR(port_vlan))
1417 			return PTR_ERR(port_vlan);
1418 	} else {
1419 		err = prestera_hw_vlan_port_set(port, vid, true, is_untagged);
1420 		if (err)
1421 			goto err_port_vlan_set;
1422 	}
1423 
1424 	err = prestera_port_pvid_set(port, pvid);
1425 	if (err)
1426 		goto err_port_pvid_set;
1427 
1428 	err = prestera_port_vlan_bridge_join(port_vlan, br_port);
1429 	if (err)
1430 		goto err_port_vlan_bridge_join;
1431 
1432 	return 0;
1433 
1434 err_port_vlan_bridge_join:
1435 	prestera_port_pvid_set(port, old_pvid);
1436 err_port_pvid_set:
1437 	prestera_hw_vlan_port_set(port, vid, false, false);
1438 err_port_vlan_set:
1439 	prestera_port_vlan_destroy(port_vlan);
1440 
1441 	return err;
1442 }
1443 
1444 static void
1445 prestera_bridge_port_vlan_del(struct prestera_port *port,
1446 			      struct prestera_bridge_port *br_port, u16 vid)
1447 {
1448 	u16 pvid = port->pvid == vid ? 0 : port->pvid;
1449 	struct prestera_port_vlan *port_vlan;
1450 
1451 	port_vlan = prestera_port_vlan_by_vid(port, vid);
1452 	if (WARN_ON(!port_vlan))
1453 		return;
1454 
1455 	prestera_port_vlan_bridge_leave(port_vlan);
1456 	prestera_port_pvid_set(port, pvid);
1457 	prestera_port_vlan_destroy(port_vlan);
1458 }
1459 
1460 static int prestera_port_vlans_add(struct prestera_port *port,
1461 				   const struct switchdev_obj_port_vlan *vlan,
1462 				   struct netlink_ext_ack *extack)
1463 {
1464 	bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1465 	bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1466 	struct net_device *orig_dev = vlan->obj.orig_dev;
1467 	struct prestera_bridge_port *br_port;
1468 	struct prestera_switch *sw = port->sw;
1469 	struct prestera_bridge *bridge;
1470 
1471 	if (netif_is_bridge_master(orig_dev))
1472 		return 0;
1473 
1474 	br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
1475 	if (WARN_ON(!br_port))
1476 		return -EINVAL;
1477 
1478 	bridge = br_port->bridge;
1479 	if (!bridge->vlan_enabled)
1480 		return 0;
1481 
1482 	return prestera_bridge_port_vlan_add(port, br_port,
1483 					     vlan->vid, flag_untagged,
1484 					     flag_pvid, extack);
1485 }
1486 
1487 static struct prestera_br_mdb_entry *
1488 prestera_br_mdb_entry_create(struct prestera_switch *sw,
1489 			     struct prestera_bridge *br_dev,
1490 			     const unsigned char *addr, u16 vid)
1491 {
1492 	struct prestera_br_mdb_entry *br_mdb_entry;
1493 	struct prestera_mdb_entry *mdb_entry;
1494 
1495 	br_mdb_entry = kzalloc(sizeof(*br_mdb_entry), GFP_KERNEL);
1496 	if (!br_mdb_entry)
1497 		return NULL;
1498 
1499 	mdb_entry = prestera_mdb_entry_create(sw, addr, vid);
1500 	if (!mdb_entry)
1501 		goto err_mdb_alloc;
1502 
1503 	br_mdb_entry->mdb = mdb_entry;
1504 	br_mdb_entry->bridge = br_dev;
1505 	br_mdb_entry->enabled = true;
1506 	INIT_LIST_HEAD(&br_mdb_entry->br_mdb_port_list);
1507 
1508 	list_add(&br_mdb_entry->br_mdb_entry_node, &br_dev->br_mdb_entry_list);
1509 
1510 	return br_mdb_entry;
1511 
1512 err_mdb_alloc:
1513 	kfree(br_mdb_entry);
1514 	return NULL;
1515 }
1516 
1517 static int prestera_br_mdb_port_add(struct prestera_br_mdb_entry *br_mdb,
1518 				    struct prestera_bridge_port *br_port)
1519 {
1520 	struct prestera_br_mdb_port *br_mdb_port;
1521 
1522 	list_for_each_entry(br_mdb_port, &br_mdb->br_mdb_port_list,
1523 			    br_mdb_port_node)
1524 		if (br_mdb_port->br_port == br_port)
1525 			return 0;
1526 
1527 	br_mdb_port = kzalloc(sizeof(*br_mdb_port), GFP_KERNEL);
1528 	if (!br_mdb_port)
1529 		return -ENOMEM;
1530 
1531 	br_mdb_port->br_port = br_port;
1532 	list_add(&br_mdb_port->br_mdb_port_node,
1533 		 &br_mdb->br_mdb_port_list);
1534 
1535 	return 0;
1536 }
1537 
1538 static struct prestera_br_mdb_entry *
1539 prestera_br_mdb_entry_find(struct prestera_bridge *br_dev,
1540 			   const unsigned char *addr, u16 vid)
1541 {
1542 	struct prestera_br_mdb_entry *br_mdb;
1543 
1544 	list_for_each_entry(br_mdb, &br_dev->br_mdb_entry_list,
1545 			    br_mdb_entry_node)
1546 		if (ether_addr_equal(&br_mdb->mdb->addr[0], addr) &&
1547 		    vid == br_mdb->mdb->vid)
1548 			return br_mdb;
1549 
1550 	return NULL;
1551 }
1552 
1553 static struct prestera_br_mdb_entry *
1554 prestera_br_mdb_entry_get(struct prestera_switch *sw,
1555 			  struct prestera_bridge *br_dev,
1556 			  const unsigned char *addr, u16 vid)
1557 {
1558 	struct prestera_br_mdb_entry *br_mdb;
1559 
1560 	br_mdb = prestera_br_mdb_entry_find(br_dev, addr, vid);
1561 	if (br_mdb)
1562 		return br_mdb;
1563 
1564 	return prestera_br_mdb_entry_create(sw, br_dev, addr, vid);
1565 }
1566 
1567 static int
1568 prestera_mdb_port_addr_obj_add(const struct switchdev_obj_port_mdb *mdb)
1569 {
1570 	struct prestera_br_mdb_entry *br_mdb;
1571 	struct prestera_bridge_port *br_port;
1572 	struct prestera_bridge *br_dev;
1573 	struct prestera_switch *sw;
1574 	struct prestera_port *port;
1575 	int err;
1576 
1577 	sw = prestera_switch_get(mdb->obj.orig_dev);
1578 	port = prestera_port_dev_lower_find(mdb->obj.orig_dev);
1579 
1580 	br_port = prestera_bridge_port_find(sw, mdb->obj.orig_dev);
1581 	if (!br_port)
1582 		return 0;
1583 
1584 	br_dev = br_port->bridge;
1585 
1586 	if (mdb->vid && !prestera_port_vlan_by_vid(port, mdb->vid))
1587 		return 0;
1588 
1589 	if (mdb->vid)
1590 		br_mdb = prestera_br_mdb_entry_get(sw, br_dev, &mdb->addr[0],
1591 						   mdb->vid);
1592 	else
1593 		br_mdb = prestera_br_mdb_entry_get(sw, br_dev, &mdb->addr[0],
1594 						   br_dev->bridge_id);
1595 
1596 	if (!br_mdb)
1597 		return -ENOMEM;
1598 
1599 	/* Make sure newly allocated MDB entry gets disabled if either MC is
1600 	 * disabled, or the mrouter does not exist.
1601 	 */
1602 	WARN_ON(prestera_mdb_enable_set(br_mdb, br_dev->multicast_enabled &&
1603 					br_dev->mrouter_exist));
1604 
1605 	err = prestera_br_mdb_port_add(br_mdb, br_port);
1606 	if (err) {
1607 		prestera_br_mdb_entry_put(br_mdb);
1608 		return err;
1609 	}
1610 
1611 	err = prestera_br_mdb_sync(br_dev);
1612 	if (err)
1613 		return err;
1614 
1615 	return 0;
1616 }
1617 
1618 static int prestera_port_obj_add(struct net_device *dev, const void *ctx,
1619 				 const struct switchdev_obj *obj,
1620 				 struct netlink_ext_ack *extack)
1621 {
1622 	struct prestera_port *port = netdev_priv(dev);
1623 	const struct switchdev_obj_port_vlan *vlan;
1624 	const struct switchdev_obj_port_mdb *mdb;
1625 	int err = 0;
1626 
1627 	switch (obj->id) {
1628 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1629 		vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1630 		return prestera_port_vlans_add(port, vlan, extack);
1631 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1632 		mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
1633 		err = prestera_mdb_port_addr_obj_add(mdb);
1634 		break;
1635 	case SWITCHDEV_OBJ_ID_HOST_MDB:
1636 		fallthrough;
1637 	default:
1638 		err = -EOPNOTSUPP;
1639 		break;
1640 	}
1641 
1642 	return err;
1643 }
1644 
1645 static int prestera_port_vlans_del(struct prestera_port *port,
1646 				   const struct switchdev_obj_port_vlan *vlan)
1647 {
1648 	struct net_device *orig_dev = vlan->obj.orig_dev;
1649 	struct prestera_bridge_port *br_port;
1650 	struct prestera_switch *sw = port->sw;
1651 
1652 	if (netif_is_bridge_master(orig_dev))
1653 		return -EOPNOTSUPP;
1654 
1655 	br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
1656 	if (WARN_ON(!br_port))
1657 		return -EINVAL;
1658 
1659 	if (!br_port->bridge->vlan_enabled)
1660 		return 0;
1661 
1662 	prestera_bridge_port_vlan_del(port, br_port, vlan->vid);
1663 
1664 	return 0;
1665 }
1666 
1667 static int
1668 prestera_mdb_port_addr_obj_del(struct prestera_port *port,
1669 			       const struct switchdev_obj_port_mdb *mdb)
1670 {
1671 	struct prestera_br_mdb_entry *br_mdb;
1672 	struct prestera_bridge_port *br_port;
1673 	struct prestera_bridge *br_dev;
1674 	int err;
1675 
1676 	/* Bridge port no longer exists - and so does this MDB entry */
1677 	br_port = prestera_bridge_port_find(port->sw, mdb->obj.orig_dev);
1678 	if (!br_port)
1679 		return 0;
1680 
1681 	/* Removing MDB with non-existing VLAN - not supported; */
1682 	if (mdb->vid && !prestera_port_vlan_by_vid(port, mdb->vid))
1683 		return 0;
1684 
1685 	br_dev = br_port->bridge;
1686 
1687 	if (br_port->bridge->vlan_enabled)
1688 		br_mdb = prestera_br_mdb_entry_find(br_dev, &mdb->addr[0],
1689 						    mdb->vid);
1690 	else
1691 		br_mdb = prestera_br_mdb_entry_find(br_dev, &mdb->addr[0],
1692 						    br_port->bridge->bridge_id);
1693 
1694 	if (!br_mdb)
1695 		return 0;
1696 
1697 	/* Since there might be a situation that this port was the last in the
1698 	 * MDB group, we have to both remove this port from software and HW MDB,
1699 	 * sync MDB table, and then destroy software MDB (if needed).
1700 	 */
1701 	prestera_br_mdb_port_del(br_mdb, br_port);
1702 
1703 	prestera_br_mdb_entry_put(br_mdb);
1704 
1705 	err = prestera_br_mdb_sync(br_dev);
1706 	if (err)
1707 		return err;
1708 
1709 	return 0;
1710 }
1711 
1712 static int prestera_port_obj_del(struct net_device *dev, const void *ctx,
1713 				 const struct switchdev_obj *obj)
1714 {
1715 	struct prestera_port *port = netdev_priv(dev);
1716 	const struct switchdev_obj_port_mdb *mdb;
1717 	int err = 0;
1718 
1719 	switch (obj->id) {
1720 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1721 		return prestera_port_vlans_del(port, SWITCHDEV_OBJ_PORT_VLAN(obj));
1722 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1723 		mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
1724 		err = prestera_mdb_port_addr_obj_del(port, mdb);
1725 		break;
1726 	default:
1727 		err = -EOPNOTSUPP;
1728 		break;
1729 	}
1730 
1731 	return err;
1732 }
1733 
1734 static int prestera_switchdev_blk_event(struct notifier_block *unused,
1735 					unsigned long event, void *ptr)
1736 {
1737 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1738 	int err;
1739 
1740 	switch (event) {
1741 	case SWITCHDEV_PORT_OBJ_ADD:
1742 		err = switchdev_handle_port_obj_add(dev, ptr,
1743 						    prestera_netdev_check,
1744 						    prestera_port_obj_add);
1745 		break;
1746 	case SWITCHDEV_PORT_OBJ_DEL:
1747 		err = switchdev_handle_port_obj_del(dev, ptr,
1748 						    prestera_netdev_check,
1749 						    prestera_port_obj_del);
1750 		break;
1751 	case SWITCHDEV_PORT_ATTR_SET:
1752 		err = switchdev_handle_port_attr_set(dev, ptr,
1753 						     prestera_netdev_check,
1754 						     prestera_port_obj_attr_set);
1755 		break;
1756 	default:
1757 		return NOTIFY_DONE;
1758 	}
1759 
1760 	return notifier_from_errno(err);
1761 }
1762 
1763 static void prestera_fdb_event(struct prestera_switch *sw,
1764 			       struct prestera_event *evt, void *arg)
1765 {
1766 	struct switchdev_notifier_fdb_info info = {};
1767 	struct net_device *dev = NULL;
1768 	struct prestera_port *port;
1769 	struct prestera_lag *lag;
1770 
1771 	switch (evt->fdb_evt.type) {
1772 	case PRESTERA_FDB_ENTRY_TYPE_REG_PORT:
1773 		port = prestera_find_port(sw, evt->fdb_evt.dest.port_id);
1774 		if (port)
1775 			dev = port->dev;
1776 		break;
1777 	case PRESTERA_FDB_ENTRY_TYPE_LAG:
1778 		lag = prestera_lag_by_id(sw, evt->fdb_evt.dest.lag_id);
1779 		if (lag)
1780 			dev = lag->dev;
1781 		break;
1782 	default:
1783 		return;
1784 	}
1785 
1786 	if (!dev)
1787 		return;
1788 
1789 	info.addr = evt->fdb_evt.data.mac;
1790 	info.vid = evt->fdb_evt.vid;
1791 	info.offloaded = true;
1792 
1793 	rtnl_lock();
1794 
1795 	switch (evt->id) {
1796 	case PRESTERA_FDB_EVENT_LEARNED:
1797 		call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
1798 					 dev, &info.info, NULL);
1799 		break;
1800 	case PRESTERA_FDB_EVENT_AGED:
1801 		call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
1802 					 dev, &info.info, NULL);
1803 		break;
1804 	}
1805 
1806 	rtnl_unlock();
1807 }
1808 
1809 static int prestera_fdb_init(struct prestera_switch *sw)
1810 {
1811 	int err;
1812 
1813 	err = prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_FDB,
1814 						 prestera_fdb_event, NULL);
1815 	if (err)
1816 		return err;
1817 
1818 	err = prestera_hw_switch_ageing_set(sw, PRESTERA_DEFAULT_AGEING_TIME_MS);
1819 	if (err)
1820 		goto err_ageing_set;
1821 
1822 	return 0;
1823 
1824 err_ageing_set:
1825 	prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
1826 					     prestera_fdb_event);
1827 	return err;
1828 }
1829 
1830 static void prestera_fdb_fini(struct prestera_switch *sw)
1831 {
1832 	prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
1833 					     prestera_fdb_event);
1834 }
1835 
1836 static int prestera_switchdev_handler_init(struct prestera_switchdev *swdev)
1837 {
1838 	int err;
1839 
1840 	swdev->swdev_nb.notifier_call = prestera_switchdev_event;
1841 	err = register_switchdev_notifier(&swdev->swdev_nb);
1842 	if (err)
1843 		goto err_register_swdev_notifier;
1844 
1845 	swdev->swdev_nb_blk.notifier_call = prestera_switchdev_blk_event;
1846 	err = register_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
1847 	if (err)
1848 		goto err_register_blk_swdev_notifier;
1849 
1850 	return 0;
1851 
1852 err_register_blk_swdev_notifier:
1853 	unregister_switchdev_notifier(&swdev->swdev_nb);
1854 err_register_swdev_notifier:
1855 	destroy_workqueue(swdev_wq);
1856 	return err;
1857 }
1858 
1859 static void prestera_switchdev_handler_fini(struct prestera_switchdev *swdev)
1860 {
1861 	unregister_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
1862 	unregister_switchdev_notifier(&swdev->swdev_nb);
1863 }
1864 
1865 int prestera_switchdev_init(struct prestera_switch *sw)
1866 {
1867 	struct prestera_switchdev *swdev;
1868 	int err;
1869 
1870 	swdev = kzalloc(sizeof(*swdev), GFP_KERNEL);
1871 	if (!swdev)
1872 		return -ENOMEM;
1873 
1874 	sw->swdev = swdev;
1875 	swdev->sw = sw;
1876 
1877 	INIT_LIST_HEAD(&swdev->bridge_list);
1878 
1879 	swdev_wq = alloc_ordered_workqueue("%s_ordered", 0, "prestera_br");
1880 	if (!swdev_wq) {
1881 		err = -ENOMEM;
1882 		goto err_alloc_wq;
1883 	}
1884 
1885 	err = prestera_switchdev_handler_init(swdev);
1886 	if (err)
1887 		goto err_swdev_init;
1888 
1889 	err = prestera_fdb_init(sw);
1890 	if (err)
1891 		goto err_fdb_init;
1892 
1893 	return 0;
1894 
1895 err_fdb_init:
1896 err_swdev_init:
1897 	destroy_workqueue(swdev_wq);
1898 err_alloc_wq:
1899 	kfree(swdev);
1900 
1901 	return err;
1902 }
1903 
1904 void prestera_switchdev_fini(struct prestera_switch *sw)
1905 {
1906 	struct prestera_switchdev *swdev = sw->swdev;
1907 
1908 	prestera_fdb_fini(sw);
1909 	prestera_switchdev_handler_fini(swdev);
1910 	destroy_workqueue(swdev_wq);
1911 	kfree(swdev);
1912 }
1913