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 	bool vlan_enabled;
43 	u16 bridge_id;
44 };
45 
46 struct prestera_bridge_port {
47 	struct list_head head;
48 	struct net_device *dev;
49 	struct prestera_bridge *bridge;
50 	struct list_head vlan_list;
51 	refcount_t ref_count;
52 	unsigned long flags;
53 	u8 stp_state;
54 };
55 
56 struct prestera_bridge_vlan {
57 	struct list_head head;
58 	struct list_head port_vlan_list;
59 	u16 vid;
60 };
61 
62 struct prestera_port_vlan {
63 	struct list_head br_vlan_head;
64 	struct list_head port_head;
65 	struct prestera_port *port;
66 	struct prestera_bridge_port *br_port;
67 	u16 vid;
68 };
69 
70 static struct workqueue_struct *swdev_wq;
71 
72 static void prestera_bridge_port_put(struct prestera_bridge_port *br_port);
73 
74 static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
75 				     u8 state);
76 
77 static struct prestera_bridge_vlan *
78 prestera_bridge_vlan_create(struct prestera_bridge_port *br_port, u16 vid)
79 {
80 	struct prestera_bridge_vlan *br_vlan;
81 
82 	br_vlan = kzalloc(sizeof(*br_vlan), GFP_KERNEL);
83 	if (!br_vlan)
84 		return NULL;
85 
86 	INIT_LIST_HEAD(&br_vlan->port_vlan_list);
87 	br_vlan->vid = vid;
88 	list_add(&br_vlan->head, &br_port->vlan_list);
89 
90 	return br_vlan;
91 }
92 
93 static void prestera_bridge_vlan_destroy(struct prestera_bridge_vlan *br_vlan)
94 {
95 	list_del(&br_vlan->head);
96 	WARN_ON(!list_empty(&br_vlan->port_vlan_list));
97 	kfree(br_vlan);
98 }
99 
100 static struct prestera_bridge_vlan *
101 prestera_bridge_vlan_by_vid(struct prestera_bridge_port *br_port, u16 vid)
102 {
103 	struct prestera_bridge_vlan *br_vlan;
104 
105 	list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
106 		if (br_vlan->vid == vid)
107 			return br_vlan;
108 	}
109 
110 	return NULL;
111 }
112 
113 static int prestera_bridge_vlan_port_count(struct prestera_bridge *bridge,
114 					   u16 vid)
115 {
116 	struct prestera_bridge_port *br_port;
117 	struct prestera_bridge_vlan *br_vlan;
118 	int count = 0;
119 
120 	list_for_each_entry(br_port, &bridge->port_list, head) {
121 		list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
122 			if (br_vlan->vid == vid) {
123 				count += 1;
124 				break;
125 			}
126 		}
127 	}
128 
129 	return count;
130 }
131 
132 static void prestera_bridge_vlan_put(struct prestera_bridge_vlan *br_vlan)
133 {
134 	if (list_empty(&br_vlan->port_vlan_list))
135 		prestera_bridge_vlan_destroy(br_vlan);
136 }
137 
138 static struct prestera_port_vlan *
139 prestera_port_vlan_by_vid(struct prestera_port *port, u16 vid)
140 {
141 	struct prestera_port_vlan *port_vlan;
142 
143 	list_for_each_entry(port_vlan, &port->vlans_list, port_head) {
144 		if (port_vlan->vid == vid)
145 			return port_vlan;
146 	}
147 
148 	return NULL;
149 }
150 
151 static struct prestera_port_vlan *
152 prestera_port_vlan_create(struct prestera_port *port, u16 vid, bool untagged)
153 {
154 	struct prestera_port_vlan *port_vlan;
155 	int err;
156 
157 	port_vlan = prestera_port_vlan_by_vid(port, vid);
158 	if (port_vlan)
159 		return ERR_PTR(-EEXIST);
160 
161 	err = prestera_hw_vlan_port_set(port, vid, true, untagged);
162 	if (err)
163 		return ERR_PTR(err);
164 
165 	port_vlan = kzalloc(sizeof(*port_vlan), GFP_KERNEL);
166 	if (!port_vlan) {
167 		err = -ENOMEM;
168 		goto err_port_vlan_alloc;
169 	}
170 
171 	port_vlan->port = port;
172 	port_vlan->vid = vid;
173 
174 	list_add(&port_vlan->port_head, &port->vlans_list);
175 
176 	return port_vlan;
177 
178 err_port_vlan_alloc:
179 	prestera_hw_vlan_port_set(port, vid, false, false);
180 	return ERR_PTR(err);
181 }
182 
183 static void
184 prestera_port_vlan_bridge_leave(struct prestera_port_vlan *port_vlan)
185 {
186 	u32 fdb_flush_mode = PRESTERA_FDB_FLUSH_MODE_DYNAMIC;
187 	struct prestera_port *port = port_vlan->port;
188 	struct prestera_bridge_vlan *br_vlan;
189 	struct prestera_bridge_port *br_port;
190 	bool last_port, last_vlan;
191 	u16 vid = port_vlan->vid;
192 	int port_count;
193 
194 	br_port = port_vlan->br_port;
195 	port_count = prestera_bridge_vlan_port_count(br_port->bridge, vid);
196 	br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
197 
198 	last_vlan = list_is_singular(&br_port->vlan_list);
199 	last_port = port_count == 1;
200 
201 	if (last_vlan)
202 		prestera_hw_fdb_flush_port(port, fdb_flush_mode);
203 	else if (last_port)
204 		prestera_hw_fdb_flush_vlan(port->sw, vid, fdb_flush_mode);
205 	else
206 		prestera_hw_fdb_flush_port_vlan(port, vid, fdb_flush_mode);
207 
208 	list_del(&port_vlan->br_vlan_head);
209 	prestera_bridge_vlan_put(br_vlan);
210 	prestera_bridge_port_put(br_port);
211 	port_vlan->br_port = NULL;
212 }
213 
214 static void prestera_port_vlan_destroy(struct prestera_port_vlan *port_vlan)
215 {
216 	struct prestera_port *port = port_vlan->port;
217 	u16 vid = port_vlan->vid;
218 
219 	if (port_vlan->br_port)
220 		prestera_port_vlan_bridge_leave(port_vlan);
221 
222 	prestera_hw_vlan_port_set(port, vid, false, false);
223 	list_del(&port_vlan->port_head);
224 	kfree(port_vlan);
225 }
226 
227 static struct prestera_bridge *
228 prestera_bridge_create(struct prestera_switchdev *swdev, struct net_device *dev)
229 {
230 	bool vlan_enabled = br_vlan_enabled(dev);
231 	struct prestera_bridge *bridge;
232 	u16 bridge_id;
233 	int err;
234 
235 	if (vlan_enabled && swdev->bridge_8021q_exists) {
236 		netdev_err(dev, "Only one VLAN-aware bridge is supported\n");
237 		return ERR_PTR(-EINVAL);
238 	}
239 
240 	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
241 	if (!bridge)
242 		return ERR_PTR(-ENOMEM);
243 
244 	if (vlan_enabled) {
245 		swdev->bridge_8021q_exists = true;
246 	} else {
247 		err = prestera_hw_bridge_create(swdev->sw, &bridge_id);
248 		if (err) {
249 			kfree(bridge);
250 			return ERR_PTR(err);
251 		}
252 
253 		bridge->bridge_id = bridge_id;
254 	}
255 
256 	bridge->vlan_enabled = vlan_enabled;
257 	bridge->swdev = swdev;
258 	bridge->dev = dev;
259 
260 	INIT_LIST_HEAD(&bridge->port_list);
261 
262 	list_add(&bridge->head, &swdev->bridge_list);
263 
264 	return bridge;
265 }
266 
267 static void prestera_bridge_destroy(struct prestera_bridge *bridge)
268 {
269 	struct prestera_switchdev *swdev = bridge->swdev;
270 
271 	list_del(&bridge->head);
272 
273 	if (bridge->vlan_enabled)
274 		swdev->bridge_8021q_exists = false;
275 	else
276 		prestera_hw_bridge_delete(swdev->sw, bridge->bridge_id);
277 
278 	WARN_ON(!list_empty(&bridge->port_list));
279 	kfree(bridge);
280 }
281 
282 static void prestera_bridge_put(struct prestera_bridge *bridge)
283 {
284 	if (list_empty(&bridge->port_list))
285 		prestera_bridge_destroy(bridge);
286 }
287 
288 static
289 struct prestera_bridge *prestera_bridge_by_dev(struct prestera_switchdev *swdev,
290 					       const struct net_device *dev)
291 {
292 	struct prestera_bridge *bridge;
293 
294 	list_for_each_entry(bridge, &swdev->bridge_list, head)
295 		if (bridge->dev == dev)
296 			return bridge;
297 
298 	return NULL;
299 }
300 
301 static struct prestera_bridge_port *
302 __prestera_bridge_port_by_dev(struct prestera_bridge *bridge,
303 			      struct net_device *dev)
304 {
305 	struct prestera_bridge_port *br_port;
306 
307 	list_for_each_entry(br_port, &bridge->port_list, head) {
308 		if (br_port->dev == dev)
309 			return br_port;
310 	}
311 
312 	return NULL;
313 }
314 
315 static struct prestera_bridge_port *
316 prestera_bridge_port_by_dev(struct prestera_switchdev *swdev,
317 			    struct net_device *dev)
318 {
319 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
320 	struct prestera_bridge *bridge;
321 
322 	if (!br_dev)
323 		return NULL;
324 
325 	bridge = prestera_bridge_by_dev(swdev, br_dev);
326 	if (!bridge)
327 		return NULL;
328 
329 	return __prestera_bridge_port_by_dev(bridge, dev);
330 }
331 
332 static struct prestera_bridge_port *
333 prestera_bridge_port_create(struct prestera_bridge *bridge,
334 			    struct net_device *dev)
335 {
336 	struct prestera_bridge_port *br_port;
337 
338 	br_port = kzalloc(sizeof(*br_port), GFP_KERNEL);
339 	if (!br_port)
340 		return NULL;
341 
342 	br_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
343 				BR_MCAST_FLOOD;
344 	br_port->stp_state = BR_STATE_DISABLED;
345 	refcount_set(&br_port->ref_count, 1);
346 	br_port->bridge = bridge;
347 	br_port->dev = dev;
348 
349 	INIT_LIST_HEAD(&br_port->vlan_list);
350 	list_add(&br_port->head, &bridge->port_list);
351 
352 	return br_port;
353 }
354 
355 static void
356 prestera_bridge_port_destroy(struct prestera_bridge_port *br_port)
357 {
358 	list_del(&br_port->head);
359 	WARN_ON(!list_empty(&br_port->vlan_list));
360 	kfree(br_port);
361 }
362 
363 static void prestera_bridge_port_get(struct prestera_bridge_port *br_port)
364 {
365 	refcount_inc(&br_port->ref_count);
366 }
367 
368 static void prestera_bridge_port_put(struct prestera_bridge_port *br_port)
369 {
370 	struct prestera_bridge *bridge = br_port->bridge;
371 
372 	if (refcount_dec_and_test(&br_port->ref_count)) {
373 		prestera_bridge_port_destroy(br_port);
374 		prestera_bridge_put(bridge);
375 	}
376 }
377 
378 static struct prestera_bridge_port *
379 prestera_bridge_port_add(struct prestera_bridge *bridge, struct net_device *dev)
380 {
381 	struct prestera_bridge_port *br_port;
382 
383 	br_port = __prestera_bridge_port_by_dev(bridge, dev);
384 	if (br_port) {
385 		prestera_bridge_port_get(br_port);
386 		return br_port;
387 	}
388 
389 	br_port = prestera_bridge_port_create(bridge, dev);
390 	if (!br_port)
391 		return ERR_PTR(-ENOMEM);
392 
393 	return br_port;
394 }
395 
396 static int
397 prestera_bridge_1d_port_join(struct prestera_bridge_port *br_port)
398 {
399 	struct prestera_port *port = netdev_priv(br_port->dev);
400 	struct prestera_bridge *bridge = br_port->bridge;
401 	int err;
402 
403 	err = prestera_hw_bridge_port_add(port, bridge->bridge_id);
404 	if (err)
405 		return err;
406 
407 	err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
408 	if (err)
409 		goto err_port_flood_set;
410 
411 	err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
412 	if (err)
413 		goto err_port_learning_set;
414 
415 	return 0;
416 
417 err_port_learning_set:
418 	prestera_hw_port_flood_set(port, false);
419 err_port_flood_set:
420 	prestera_hw_bridge_port_delete(port, bridge->bridge_id);
421 
422 	return err;
423 }
424 
425 static int prestera_port_bridge_join(struct prestera_port *port,
426 				     struct net_device *upper)
427 {
428 	struct prestera_switchdev *swdev = port->sw->swdev;
429 	struct prestera_bridge_port *br_port;
430 	struct prestera_bridge *bridge;
431 	int err;
432 
433 	bridge = prestera_bridge_by_dev(swdev, upper);
434 	if (!bridge) {
435 		bridge = prestera_bridge_create(swdev, upper);
436 		if (IS_ERR(bridge))
437 			return PTR_ERR(bridge);
438 	}
439 
440 	br_port = prestera_bridge_port_add(bridge, port->dev);
441 	if (IS_ERR(br_port)) {
442 		err = PTR_ERR(br_port);
443 		goto err_brport_create;
444 	}
445 
446 	if (bridge->vlan_enabled)
447 		return 0;
448 
449 	err = prestera_bridge_1d_port_join(br_port);
450 	if (err)
451 		goto err_port_join;
452 
453 	return 0;
454 
455 err_port_join:
456 	prestera_bridge_port_put(br_port);
457 err_brport_create:
458 	prestera_bridge_put(bridge);
459 	return err;
460 }
461 
462 static void prestera_bridge_1q_port_leave(struct prestera_bridge_port *br_port)
463 {
464 	struct prestera_port *port = netdev_priv(br_port->dev);
465 
466 	prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
467 	prestera_port_pvid_set(port, PRESTERA_DEFAULT_VID);
468 }
469 
470 static void prestera_bridge_1d_port_leave(struct prestera_bridge_port *br_port)
471 {
472 	struct prestera_port *port = netdev_priv(br_port->dev);
473 
474 	prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
475 	prestera_hw_bridge_port_delete(port, br_port->bridge->bridge_id);
476 }
477 
478 static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
479 				     u8 state)
480 {
481 	u8 hw_state = state;
482 
483 	switch (state) {
484 	case BR_STATE_DISABLED:
485 		hw_state = PRESTERA_STP_DISABLED;
486 		break;
487 
488 	case BR_STATE_BLOCKING:
489 	case BR_STATE_LISTENING:
490 		hw_state = PRESTERA_STP_BLOCK_LISTEN;
491 		break;
492 
493 	case BR_STATE_LEARNING:
494 		hw_state = PRESTERA_STP_LEARN;
495 		break;
496 
497 	case BR_STATE_FORWARDING:
498 		hw_state = PRESTERA_STP_FORWARD;
499 		break;
500 
501 	default:
502 		return -EINVAL;
503 	}
504 
505 	return prestera_hw_vlan_port_stp_set(port, vid, hw_state);
506 }
507 
508 static void prestera_port_bridge_leave(struct prestera_port *port,
509 				       struct net_device *upper)
510 {
511 	struct prestera_switchdev *swdev = port->sw->swdev;
512 	struct prestera_bridge_port *br_port;
513 	struct prestera_bridge *bridge;
514 
515 	bridge = prestera_bridge_by_dev(swdev, upper);
516 	if (!bridge)
517 		return;
518 
519 	br_port = __prestera_bridge_port_by_dev(bridge, port->dev);
520 	if (!br_port)
521 		return;
522 
523 	bridge = br_port->bridge;
524 
525 	if (bridge->vlan_enabled)
526 		prestera_bridge_1q_port_leave(br_port);
527 	else
528 		prestera_bridge_1d_port_leave(br_port);
529 
530 	prestera_hw_port_learning_set(port, false);
531 	prestera_hw_port_flood_set(port, false);
532 	prestera_port_vid_stp_set(port, PRESTERA_VID_ALL, BR_STATE_FORWARDING);
533 	prestera_bridge_port_put(br_port);
534 }
535 
536 int prestera_bridge_port_event(struct net_device *dev, unsigned long event,
537 			       void *ptr)
538 {
539 	struct netdev_notifier_changeupper_info *info = ptr;
540 	struct netlink_ext_ack *extack;
541 	struct prestera_port *port;
542 	struct net_device *upper;
543 	int err;
544 
545 	extack = netdev_notifier_info_to_extack(&info->info);
546 	port = netdev_priv(dev);
547 	upper = info->upper_dev;
548 
549 	switch (event) {
550 	case NETDEV_PRECHANGEUPPER:
551 		if (!netif_is_bridge_master(upper)) {
552 			NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
553 			return -EINVAL;
554 		}
555 
556 		if (!info->linking)
557 			break;
558 
559 		if (netdev_has_any_upper_dev(upper)) {
560 			NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved");
561 			return -EINVAL;
562 		}
563 		break;
564 
565 	case NETDEV_CHANGEUPPER:
566 		if (!netif_is_bridge_master(upper))
567 			break;
568 
569 		if (info->linking) {
570 			err = prestera_port_bridge_join(port, upper);
571 			if (err)
572 				return err;
573 		} else {
574 			prestera_port_bridge_leave(port, upper);
575 		}
576 		break;
577 	}
578 
579 	return 0;
580 }
581 
582 static int prestera_port_attr_br_flags_set(struct prestera_port *port,
583 					   struct net_device *dev,
584 					   struct switchdev_brport_flags flags)
585 {
586 	struct prestera_bridge_port *br_port;
587 	int err;
588 
589 	br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
590 	if (!br_port)
591 		return 0;
592 
593 	if (flags.mask & BR_FLOOD) {
594 		err = prestera_hw_port_flood_set(port, flags.val & BR_FLOOD);
595 		if (err)
596 			return err;
597 	}
598 
599 	if (flags.mask & BR_LEARNING) {
600 		err = prestera_hw_port_learning_set(port,
601 						    flags.val & BR_LEARNING);
602 		if (err)
603 			return err;
604 	}
605 
606 	memcpy(&br_port->flags, &flags.val, sizeof(flags.val));
607 
608 	return 0;
609 }
610 
611 static int prestera_port_attr_br_ageing_set(struct prestera_port *port,
612 					    unsigned long ageing_clock_t)
613 {
614 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
615 	u32 ageing_time_ms = jiffies_to_msecs(ageing_jiffies);
616 	struct prestera_switch *sw = port->sw;
617 
618 	if (ageing_time_ms < PRESTERA_MIN_AGEING_TIME_MS ||
619 	    ageing_time_ms > PRESTERA_MAX_AGEING_TIME_MS)
620 		return -ERANGE;
621 
622 	return prestera_hw_switch_ageing_set(sw, ageing_time_ms);
623 }
624 
625 static int prestera_port_attr_br_vlan_set(struct prestera_port *port,
626 					  struct net_device *dev,
627 					  bool vlan_enabled)
628 {
629 	struct prestera_switch *sw = port->sw;
630 	struct prestera_bridge *bridge;
631 
632 	bridge = prestera_bridge_by_dev(sw->swdev, dev);
633 	if (WARN_ON(!bridge))
634 		return -EINVAL;
635 
636 	if (bridge->vlan_enabled == vlan_enabled)
637 		return 0;
638 
639 	netdev_err(bridge->dev, "VLAN filtering can't be changed for existing bridge\n");
640 
641 	return -EINVAL;
642 }
643 
644 static int prestera_port_bridge_vlan_stp_set(struct prestera_port *port,
645 					     struct prestera_bridge_vlan *br_vlan,
646 					     u8 state)
647 {
648 	struct prestera_port_vlan *port_vlan;
649 
650 	list_for_each_entry(port_vlan, &br_vlan->port_vlan_list, br_vlan_head) {
651 		if (port_vlan->port != port)
652 			continue;
653 
654 		return prestera_port_vid_stp_set(port, br_vlan->vid, state);
655 	}
656 
657 	return 0;
658 }
659 
660 static int prestera_port_attr_stp_state_set(struct prestera_port *port,
661 					    struct net_device *dev,
662 					    u8 state)
663 {
664 	struct prestera_bridge_port *br_port;
665 	struct prestera_bridge_vlan *br_vlan;
666 	int err;
667 	u16 vid;
668 
669 	br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
670 	if (!br_port)
671 		return 0;
672 
673 	if (!br_port->bridge->vlan_enabled) {
674 		vid = br_port->bridge->bridge_id;
675 		err = prestera_port_vid_stp_set(port, vid, state);
676 		if (err)
677 			goto err_port_stp_set;
678 	} else {
679 		list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
680 			err = prestera_port_bridge_vlan_stp_set(port, br_vlan,
681 								state);
682 			if (err)
683 				goto err_port_vlan_stp_set;
684 		}
685 	}
686 
687 	br_port->stp_state = state;
688 
689 	return 0;
690 
691 err_port_vlan_stp_set:
692 	list_for_each_entry_continue_reverse(br_vlan, &br_port->vlan_list, head)
693 		prestera_port_bridge_vlan_stp_set(port, br_vlan, br_port->stp_state);
694 	return err;
695 
696 err_port_stp_set:
697 	prestera_port_vid_stp_set(port, vid, br_port->stp_state);
698 
699 	return err;
700 }
701 
702 static int prestera_port_obj_attr_set(struct net_device *dev,
703 				      const struct switchdev_attr *attr,
704 				      struct netlink_ext_ack *extack)
705 {
706 	struct prestera_port *port = netdev_priv(dev);
707 	int err = 0;
708 
709 	switch (attr->id) {
710 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
711 		err = prestera_port_attr_stp_state_set(port, attr->orig_dev,
712 						       attr->u.stp_state);
713 		break;
714 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
715 		if (attr->u.brport_flags.mask &
716 		    ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
717 			err = -EINVAL;
718 		break;
719 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
720 		err = prestera_port_attr_br_flags_set(port, attr->orig_dev,
721 						      attr->u.brport_flags);
722 		break;
723 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
724 		err = prestera_port_attr_br_ageing_set(port,
725 						       attr->u.ageing_time);
726 		break;
727 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
728 		err = prestera_port_attr_br_vlan_set(port, attr->orig_dev,
729 						     attr->u.vlan_filtering);
730 		break;
731 	default:
732 		err = -EOPNOTSUPP;
733 	}
734 
735 	return err;
736 }
737 
738 static void
739 prestera_fdb_offload_notify(struct prestera_port *port,
740 			    struct switchdev_notifier_fdb_info *info)
741 {
742 	struct switchdev_notifier_fdb_info send_info;
743 
744 	send_info.addr = info->addr;
745 	send_info.vid = info->vid;
746 	send_info.offloaded = true;
747 
748 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, port->dev,
749 				 &send_info.info, NULL);
750 }
751 
752 static int prestera_port_fdb_set(struct prestera_port *port,
753 				 struct switchdev_notifier_fdb_info *fdb_info,
754 				 bool adding)
755 {
756 	struct prestera_switch *sw = port->sw;
757 	struct prestera_bridge_port *br_port;
758 	struct prestera_bridge *bridge;
759 	int err;
760 	u16 vid;
761 
762 	br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
763 	if (!br_port)
764 		return -EINVAL;
765 
766 	bridge = br_port->bridge;
767 
768 	if (bridge->vlan_enabled)
769 		vid = fdb_info->vid;
770 	else
771 		vid = bridge->bridge_id;
772 
773 	if (adding)
774 		err = prestera_hw_fdb_add(port, fdb_info->addr, vid, false);
775 	else
776 		err = prestera_hw_fdb_del(port, fdb_info->addr, vid);
777 
778 	return err;
779 }
780 
781 static void prestera_fdb_event_work(struct work_struct *work)
782 {
783 	struct switchdev_notifier_fdb_info *fdb_info;
784 	struct prestera_fdb_event_work *swdev_work;
785 	struct prestera_port *port;
786 	struct net_device *dev;
787 	int err;
788 
789 	swdev_work = container_of(work, struct prestera_fdb_event_work, work);
790 	dev = swdev_work->dev;
791 
792 	rtnl_lock();
793 
794 	port = prestera_port_dev_lower_find(dev);
795 	if (!port)
796 		goto out_unlock;
797 
798 	switch (swdev_work->event) {
799 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
800 		fdb_info = &swdev_work->fdb_info;
801 		if (!fdb_info->added_by_user || fdb_info->is_local)
802 			break;
803 
804 		err = prestera_port_fdb_set(port, fdb_info, true);
805 		if (err)
806 			break;
807 
808 		prestera_fdb_offload_notify(port, fdb_info);
809 		break;
810 
811 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
812 		fdb_info = &swdev_work->fdb_info;
813 		prestera_port_fdb_set(port, fdb_info, false);
814 		break;
815 	}
816 
817 out_unlock:
818 	rtnl_unlock();
819 
820 	kfree(swdev_work->fdb_info.addr);
821 	kfree(swdev_work);
822 	dev_put(dev);
823 }
824 
825 static int prestera_switchdev_event(struct notifier_block *unused,
826 				    unsigned long event, void *ptr)
827 {
828 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
829 	struct switchdev_notifier_fdb_info *fdb_info;
830 	struct switchdev_notifier_info *info = ptr;
831 	struct prestera_fdb_event_work *swdev_work;
832 	struct net_device *upper;
833 	int err;
834 
835 	if (event == SWITCHDEV_PORT_ATTR_SET) {
836 		err = switchdev_handle_port_attr_set(dev, ptr,
837 						     prestera_netdev_check,
838 						     prestera_port_obj_attr_set);
839 		return notifier_from_errno(err);
840 	}
841 
842 	if (!prestera_netdev_check(dev))
843 		return NOTIFY_DONE;
844 
845 	upper = netdev_master_upper_dev_get_rcu(dev);
846 	if (!upper)
847 		return NOTIFY_DONE;
848 
849 	if (!netif_is_bridge_master(upper))
850 		return NOTIFY_DONE;
851 
852 	swdev_work = kzalloc(sizeof(*swdev_work), GFP_ATOMIC);
853 	if (!swdev_work)
854 		return NOTIFY_BAD;
855 
856 	swdev_work->event = event;
857 	swdev_work->dev = dev;
858 
859 	switch (event) {
860 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
861 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
862 		fdb_info = container_of(info,
863 					struct switchdev_notifier_fdb_info,
864 					info);
865 
866 		INIT_WORK(&swdev_work->work, prestera_fdb_event_work);
867 		memcpy(&swdev_work->fdb_info, ptr,
868 		       sizeof(swdev_work->fdb_info));
869 
870 		swdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
871 		if (!swdev_work->fdb_info.addr)
872 			goto out_bad;
873 
874 		ether_addr_copy((u8 *)swdev_work->fdb_info.addr,
875 				fdb_info->addr);
876 		dev_hold(dev);
877 		break;
878 
879 	default:
880 		kfree(swdev_work);
881 		return NOTIFY_DONE;
882 	}
883 
884 	queue_work(swdev_wq, &swdev_work->work);
885 	return NOTIFY_DONE;
886 
887 out_bad:
888 	kfree(swdev_work);
889 	return NOTIFY_BAD;
890 }
891 
892 static int
893 prestera_port_vlan_bridge_join(struct prestera_port_vlan *port_vlan,
894 			       struct prestera_bridge_port *br_port)
895 {
896 	struct prestera_port *port = port_vlan->port;
897 	struct prestera_bridge_vlan *br_vlan;
898 	u16 vid = port_vlan->vid;
899 	int err;
900 
901 	if (port_vlan->br_port)
902 		return 0;
903 
904 	err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
905 	if (err)
906 		return err;
907 
908 	err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
909 	if (err)
910 		goto err_port_learning_set;
911 
912 	err = prestera_port_vid_stp_set(port, vid, br_port->stp_state);
913 	if (err)
914 		goto err_port_vid_stp_set;
915 
916 	br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
917 	if (!br_vlan) {
918 		br_vlan = prestera_bridge_vlan_create(br_port, vid);
919 		if (!br_vlan) {
920 			err = -ENOMEM;
921 			goto err_bridge_vlan_get;
922 		}
923 	}
924 
925 	list_add(&port_vlan->br_vlan_head, &br_vlan->port_vlan_list);
926 
927 	prestera_bridge_port_get(br_port);
928 	port_vlan->br_port = br_port;
929 
930 	return 0;
931 
932 err_bridge_vlan_get:
933 	prestera_port_vid_stp_set(port, vid, BR_STATE_FORWARDING);
934 err_port_vid_stp_set:
935 	prestera_hw_port_learning_set(port, false);
936 err_port_learning_set:
937 	return err;
938 }
939 
940 static int
941 prestera_bridge_port_vlan_add(struct prestera_port *port,
942 			      struct prestera_bridge_port *br_port,
943 			      u16 vid, bool is_untagged, bool is_pvid,
944 			      struct netlink_ext_ack *extack)
945 {
946 	struct prestera_port_vlan *port_vlan;
947 	u16 old_pvid = port->pvid;
948 	u16 pvid;
949 	int err;
950 
951 	if (is_pvid)
952 		pvid = vid;
953 	else
954 		pvid = port->pvid == vid ? 0 : port->pvid;
955 
956 	port_vlan = prestera_port_vlan_by_vid(port, vid);
957 	if (port_vlan && port_vlan->br_port != br_port)
958 		return -EEXIST;
959 
960 	if (!port_vlan) {
961 		port_vlan = prestera_port_vlan_create(port, vid, is_untagged);
962 		if (IS_ERR(port_vlan))
963 			return PTR_ERR(port_vlan);
964 	} else {
965 		err = prestera_hw_vlan_port_set(port, vid, true, is_untagged);
966 		if (err)
967 			goto err_port_vlan_set;
968 	}
969 
970 	err = prestera_port_pvid_set(port, pvid);
971 	if (err)
972 		goto err_port_pvid_set;
973 
974 	err = prestera_port_vlan_bridge_join(port_vlan, br_port);
975 	if (err)
976 		goto err_port_vlan_bridge_join;
977 
978 	return 0;
979 
980 err_port_vlan_bridge_join:
981 	prestera_port_pvid_set(port, old_pvid);
982 err_port_pvid_set:
983 	prestera_hw_vlan_port_set(port, vid, false, false);
984 err_port_vlan_set:
985 	prestera_port_vlan_destroy(port_vlan);
986 
987 	return err;
988 }
989 
990 static void
991 prestera_bridge_port_vlan_del(struct prestera_port *port,
992 			      struct prestera_bridge_port *br_port, u16 vid)
993 {
994 	u16 pvid = port->pvid == vid ? 0 : port->pvid;
995 	struct prestera_port_vlan *port_vlan;
996 
997 	port_vlan = prestera_port_vlan_by_vid(port, vid);
998 	if (WARN_ON(!port_vlan))
999 		return;
1000 
1001 	prestera_port_vlan_bridge_leave(port_vlan);
1002 	prestera_port_pvid_set(port, pvid);
1003 	prestera_port_vlan_destroy(port_vlan);
1004 }
1005 
1006 static int prestera_port_vlans_add(struct prestera_port *port,
1007 				   const struct switchdev_obj_port_vlan *vlan,
1008 				   struct netlink_ext_ack *extack)
1009 {
1010 	bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1011 	bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1012 	struct net_device *dev = vlan->obj.orig_dev;
1013 	struct prestera_bridge_port *br_port;
1014 	struct prestera_switch *sw = port->sw;
1015 	struct prestera_bridge *bridge;
1016 
1017 	if (netif_is_bridge_master(dev))
1018 		return 0;
1019 
1020 	br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
1021 	if (WARN_ON(!br_port))
1022 		return -EINVAL;
1023 
1024 	bridge = br_port->bridge;
1025 	if (!bridge->vlan_enabled)
1026 		return 0;
1027 
1028 	return prestera_bridge_port_vlan_add(port, br_port,
1029 					     vlan->vid, flag_untagged,
1030 					     flag_pvid, extack);
1031 }
1032 
1033 static int prestera_port_obj_add(struct net_device *dev,
1034 				 const struct switchdev_obj *obj,
1035 				 struct netlink_ext_ack *extack)
1036 {
1037 	struct prestera_port *port = netdev_priv(dev);
1038 	const struct switchdev_obj_port_vlan *vlan;
1039 
1040 	switch (obj->id) {
1041 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1042 		vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1043 		return prestera_port_vlans_add(port, vlan, extack);
1044 	default:
1045 		return -EOPNOTSUPP;
1046 	}
1047 }
1048 
1049 static int prestera_port_vlans_del(struct prestera_port *port,
1050 				   const struct switchdev_obj_port_vlan *vlan)
1051 {
1052 	struct net_device *dev = vlan->obj.orig_dev;
1053 	struct prestera_bridge_port *br_port;
1054 	struct prestera_switch *sw = port->sw;
1055 
1056 	if (netif_is_bridge_master(dev))
1057 		return -EOPNOTSUPP;
1058 
1059 	br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
1060 	if (WARN_ON(!br_port))
1061 		return -EINVAL;
1062 
1063 	if (!br_port->bridge->vlan_enabled)
1064 		return 0;
1065 
1066 	prestera_bridge_port_vlan_del(port, br_port, vlan->vid);
1067 
1068 	return 0;
1069 }
1070 
1071 static int prestera_port_obj_del(struct net_device *dev,
1072 				 const struct switchdev_obj *obj)
1073 {
1074 	struct prestera_port *port = netdev_priv(dev);
1075 
1076 	switch (obj->id) {
1077 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1078 		return prestera_port_vlans_del(port, SWITCHDEV_OBJ_PORT_VLAN(obj));
1079 	default:
1080 		return -EOPNOTSUPP;
1081 	}
1082 }
1083 
1084 static int prestera_switchdev_blk_event(struct notifier_block *unused,
1085 					unsigned long event, void *ptr)
1086 {
1087 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1088 	int err;
1089 
1090 	switch (event) {
1091 	case SWITCHDEV_PORT_OBJ_ADD:
1092 		err = switchdev_handle_port_obj_add(dev, ptr,
1093 						    prestera_netdev_check,
1094 						    prestera_port_obj_add);
1095 		break;
1096 	case SWITCHDEV_PORT_OBJ_DEL:
1097 		err = switchdev_handle_port_obj_del(dev, ptr,
1098 						    prestera_netdev_check,
1099 						    prestera_port_obj_del);
1100 		break;
1101 	case SWITCHDEV_PORT_ATTR_SET:
1102 		err = switchdev_handle_port_attr_set(dev, ptr,
1103 						     prestera_netdev_check,
1104 						     prestera_port_obj_attr_set);
1105 		break;
1106 	default:
1107 		err = -EOPNOTSUPP;
1108 	}
1109 
1110 	return notifier_from_errno(err);
1111 }
1112 
1113 static void prestera_fdb_event(struct prestera_switch *sw,
1114 			       struct prestera_event *evt, void *arg)
1115 {
1116 	struct switchdev_notifier_fdb_info info;
1117 	struct prestera_port *port;
1118 
1119 	port = prestera_find_port(sw, evt->fdb_evt.port_id);
1120 	if (!port)
1121 		return;
1122 
1123 	info.addr = evt->fdb_evt.data.mac;
1124 	info.vid = evt->fdb_evt.vid;
1125 	info.offloaded = true;
1126 
1127 	rtnl_lock();
1128 
1129 	switch (evt->id) {
1130 	case PRESTERA_FDB_EVENT_LEARNED:
1131 		call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
1132 					 port->dev, &info.info, NULL);
1133 		break;
1134 	case PRESTERA_FDB_EVENT_AGED:
1135 		call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
1136 					 port->dev, &info.info, NULL);
1137 		break;
1138 	}
1139 
1140 	rtnl_unlock();
1141 }
1142 
1143 static int prestera_fdb_init(struct prestera_switch *sw)
1144 {
1145 	int err;
1146 
1147 	err = prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_FDB,
1148 						 prestera_fdb_event, NULL);
1149 	if (err)
1150 		return err;
1151 
1152 	err = prestera_hw_switch_ageing_set(sw, PRESTERA_DEFAULT_AGEING_TIME_MS);
1153 	if (err)
1154 		goto err_ageing_set;
1155 
1156 	return 0;
1157 
1158 err_ageing_set:
1159 	prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
1160 					     prestera_fdb_event);
1161 	return err;
1162 }
1163 
1164 static void prestera_fdb_fini(struct prestera_switch *sw)
1165 {
1166 	prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
1167 					     prestera_fdb_event);
1168 }
1169 
1170 static int prestera_switchdev_handler_init(struct prestera_switchdev *swdev)
1171 {
1172 	int err;
1173 
1174 	swdev->swdev_nb.notifier_call = prestera_switchdev_event;
1175 	err = register_switchdev_notifier(&swdev->swdev_nb);
1176 	if (err)
1177 		goto err_register_swdev_notifier;
1178 
1179 	swdev->swdev_nb_blk.notifier_call = prestera_switchdev_blk_event;
1180 	err = register_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
1181 	if (err)
1182 		goto err_register_blk_swdev_notifier;
1183 
1184 	return 0;
1185 
1186 err_register_blk_swdev_notifier:
1187 	unregister_switchdev_notifier(&swdev->swdev_nb);
1188 err_register_swdev_notifier:
1189 	destroy_workqueue(swdev_wq);
1190 	return err;
1191 }
1192 
1193 static void prestera_switchdev_handler_fini(struct prestera_switchdev *swdev)
1194 {
1195 	unregister_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
1196 	unregister_switchdev_notifier(&swdev->swdev_nb);
1197 }
1198 
1199 int prestera_switchdev_init(struct prestera_switch *sw)
1200 {
1201 	struct prestera_switchdev *swdev;
1202 	int err;
1203 
1204 	swdev = kzalloc(sizeof(*swdev), GFP_KERNEL);
1205 	if (!swdev)
1206 		return -ENOMEM;
1207 
1208 	sw->swdev = swdev;
1209 	swdev->sw = sw;
1210 
1211 	INIT_LIST_HEAD(&swdev->bridge_list);
1212 
1213 	swdev_wq = alloc_ordered_workqueue("%s_ordered", 0, "prestera_br");
1214 	if (!swdev_wq) {
1215 		err = -ENOMEM;
1216 		goto err_alloc_wq;
1217 	}
1218 
1219 	err = prestera_switchdev_handler_init(swdev);
1220 	if (err)
1221 		goto err_swdev_init;
1222 
1223 	err = prestera_fdb_init(sw);
1224 	if (err)
1225 		goto err_fdb_init;
1226 
1227 	return 0;
1228 
1229 err_fdb_init:
1230 err_swdev_init:
1231 	destroy_workqueue(swdev_wq);
1232 err_alloc_wq:
1233 	kfree(swdev);
1234 
1235 	return err;
1236 }
1237 
1238 void prestera_switchdev_fini(struct prestera_switch *sw)
1239 {
1240 	struct prestera_switchdev *swdev = sw->swdev;
1241 
1242 	prestera_fdb_fini(sw);
1243 	prestera_switchdev_handler_fini(swdev);
1244 	destroy_workqueue(swdev_wq);
1245 	kfree(swdev);
1246 }
1247