xref: /linux/net/dsa/dsa.c (revision 8f7aa3d3c7323f4ca2768a9e74ebbe359c4f8f88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DSA topology and switch handling
4  *
5  * Copyright (c) 2008-2009 Marvell Semiconductor
6  * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
7  * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/if_hsr.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/slab.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/of.h>
19 #include <linux/of_net.h>
20 #include <net/dsa_stubs.h>
21 #include <net/sch_generic.h>
22 
23 #include "conduit.h"
24 #include "devlink.h"
25 #include "dsa.h"
26 #include "netlink.h"
27 #include "port.h"
28 #include "switch.h"
29 #include "tag.h"
30 #include "user.h"
31 
32 #define DSA_MAX_NUM_OFFLOADING_BRIDGES		BITS_PER_LONG
33 
34 static DEFINE_MUTEX(dsa2_mutex);
35 LIST_HEAD(dsa_tree_list);
36 
37 static struct workqueue_struct *dsa_owq;
38 
39 /* Track the bridges with forwarding offload enabled */
40 static unsigned long dsa_fwd_offloading_bridges;
41 
42 bool dsa_schedule_work(struct work_struct *work)
43 {
44 	return queue_work(dsa_owq, work);
45 }
46 
47 void dsa_flush_workqueue(void)
48 {
49 	flush_workqueue(dsa_owq);
50 }
51 EXPORT_SYMBOL_GPL(dsa_flush_workqueue);
52 
53 /**
54  * dsa_lag_map() - Map LAG structure to a linear LAG array
55  * @dst: Tree in which to record the mapping.
56  * @lag: LAG structure that is to be mapped to the tree's array.
57  *
58  * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
59  * two spaces. The size of the mapping space is determined by the
60  * driver by setting ds->num_lag_ids. It is perfectly legal to leave
61  * it unset if it is not needed, in which case these functions become
62  * no-ops.
63  */
64 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
65 {
66 	unsigned int id;
67 
68 	for (id = 1; id <= dst->lags_len; id++) {
69 		if (!dsa_lag_by_id(dst, id)) {
70 			dst->lags[id - 1] = lag;
71 			lag->id = id;
72 			return;
73 		}
74 	}
75 
76 	/* No IDs left, which is OK. Some drivers do not need it. The
77 	 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
78 	 * returns an error for this device when joining the LAG. The
79 	 * driver can then return -EOPNOTSUPP back to DSA, which will
80 	 * fall back to a software LAG.
81 	 */
82 }
83 
84 /**
85  * dsa_lag_unmap() - Remove a LAG ID mapping
86  * @dst: Tree in which the mapping is recorded.
87  * @lag: LAG structure that was mapped.
88  *
89  * As there may be multiple users of the mapping, it is only removed
90  * if there are no other references to it.
91  */
92 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
93 {
94 	unsigned int id;
95 
96 	dsa_lags_foreach_id(id, dst) {
97 		if (dsa_lag_by_id(dst, id) == lag) {
98 			dst->lags[id - 1] = NULL;
99 			lag->id = 0;
100 			break;
101 		}
102 	}
103 }
104 
105 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
106 				  const struct net_device *lag_dev)
107 {
108 	struct dsa_port *dp;
109 
110 	list_for_each_entry(dp, &dst->ports, list)
111 		if (dsa_port_lag_dev_get(dp) == lag_dev)
112 			return dp->lag;
113 
114 	return NULL;
115 }
116 
117 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
118 					const struct net_device *br)
119 {
120 	struct dsa_port *dp;
121 
122 	list_for_each_entry(dp, &dst->ports, list)
123 		if (dsa_port_bridge_dev_get(dp) == br)
124 			return dp->bridge;
125 
126 	return NULL;
127 }
128 
129 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
130 {
131 	struct dsa_switch_tree *dst;
132 
133 	list_for_each_entry(dst, &dsa_tree_list, list) {
134 		struct dsa_bridge *bridge;
135 
136 		bridge = dsa_tree_bridge_find(dst, bridge_dev);
137 		if (bridge)
138 			return bridge->num;
139 	}
140 
141 	return 0;
142 }
143 
144 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
145 {
146 	unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
147 
148 	/* Switches without FDB isolation support don't get unique
149 	 * bridge numbering
150 	 */
151 	if (!max)
152 		return 0;
153 
154 	if (!bridge_num) {
155 		/* First port that requests FDB isolation or TX forwarding
156 		 * offload for this bridge
157 		 */
158 		bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
159 						DSA_MAX_NUM_OFFLOADING_BRIDGES,
160 						1);
161 		if (bridge_num >= max)
162 			return 0;
163 
164 		set_bit(bridge_num, &dsa_fwd_offloading_bridges);
165 	}
166 
167 	return bridge_num;
168 }
169 
170 void dsa_bridge_num_put(const struct net_device *bridge_dev,
171 			unsigned int bridge_num)
172 {
173 	/* Since we refcount bridges, we know that when we call this function
174 	 * it is no longer in use, so we can just go ahead and remove it from
175 	 * the bit mask.
176 	 */
177 	clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
178 }
179 
180 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
181 {
182 	struct dsa_switch_tree *dst;
183 	struct dsa_port *dp;
184 
185 	list_for_each_entry(dst, &dsa_tree_list, list) {
186 		if (dst->index != tree_index)
187 			continue;
188 
189 		list_for_each_entry(dp, &dst->ports, list) {
190 			if (dp->ds->index != sw_index)
191 				continue;
192 
193 			return dp->ds;
194 		}
195 	}
196 
197 	return NULL;
198 }
199 EXPORT_SYMBOL_GPL(dsa_switch_find);
200 
201 static struct dsa_switch_tree *dsa_tree_find(int index)
202 {
203 	struct dsa_switch_tree *dst;
204 
205 	list_for_each_entry(dst, &dsa_tree_list, list)
206 		if (dst->index == index)
207 			return dst;
208 
209 	return NULL;
210 }
211 
212 static struct dsa_switch_tree *dsa_tree_alloc(int index)
213 {
214 	struct dsa_switch_tree *dst;
215 
216 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
217 	if (!dst)
218 		return NULL;
219 
220 	dst->index = index;
221 
222 	INIT_LIST_HEAD(&dst->rtable);
223 
224 	INIT_LIST_HEAD(&dst->ports);
225 
226 	INIT_LIST_HEAD(&dst->list);
227 	list_add_tail(&dst->list, &dsa_tree_list);
228 
229 	kref_init(&dst->refcount);
230 
231 	return dst;
232 }
233 
234 static void dsa_tree_free(struct dsa_switch_tree *dst)
235 {
236 	if (dst->tag_ops)
237 		dsa_tag_driver_put(dst->tag_ops);
238 	list_del(&dst->list);
239 	kfree(dst);
240 }
241 
242 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
243 {
244 	if (dst)
245 		kref_get(&dst->refcount);
246 
247 	return dst;
248 }
249 
250 static struct dsa_switch_tree *dsa_tree_touch(int index)
251 {
252 	struct dsa_switch_tree *dst;
253 
254 	dst = dsa_tree_find(index);
255 	if (dst)
256 		return dsa_tree_get(dst);
257 	else
258 		return dsa_tree_alloc(index);
259 }
260 
261 static void dsa_tree_release(struct kref *ref)
262 {
263 	struct dsa_switch_tree *dst;
264 
265 	dst = container_of(ref, struct dsa_switch_tree, refcount);
266 
267 	dsa_tree_free(dst);
268 }
269 
270 static void dsa_tree_put(struct dsa_switch_tree *dst)
271 {
272 	if (dst)
273 		kref_put(&dst->refcount, dsa_tree_release);
274 }
275 
276 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
277 						   struct device_node *dn)
278 {
279 	struct dsa_port *dp;
280 
281 	list_for_each_entry(dp, &dst->ports, list)
282 		if (dp->dn == dn)
283 			return dp;
284 
285 	return NULL;
286 }
287 
288 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
289 				       struct dsa_port *link_dp)
290 {
291 	struct dsa_switch *ds = dp->ds;
292 	struct dsa_switch_tree *dst;
293 	struct dsa_link *dl;
294 
295 	dst = ds->dst;
296 
297 	list_for_each_entry(dl, &dst->rtable, list)
298 		if (dl->dp == dp && dl->link_dp == link_dp)
299 			return dl;
300 
301 	dl = kzalloc(sizeof(*dl), GFP_KERNEL);
302 	if (!dl)
303 		return NULL;
304 
305 	dl->dp = dp;
306 	dl->link_dp = link_dp;
307 
308 	INIT_LIST_HEAD(&dl->list);
309 	list_add_tail(&dl->list, &dst->rtable);
310 
311 	return dl;
312 }
313 
314 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
315 {
316 	struct dsa_switch *ds = dp->ds;
317 	struct dsa_switch_tree *dst = ds->dst;
318 	struct device_node *dn = dp->dn;
319 	struct of_phandle_iterator it;
320 	struct dsa_port *link_dp;
321 	struct dsa_link *dl;
322 	int err;
323 
324 	of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
325 		link_dp = dsa_tree_find_port_by_node(dst, it.node);
326 		if (!link_dp) {
327 			of_node_put(it.node);
328 			return false;
329 		}
330 
331 		dl = dsa_link_touch(dp, link_dp);
332 		if (!dl) {
333 			of_node_put(it.node);
334 			return false;
335 		}
336 	}
337 
338 	return true;
339 }
340 
341 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
342 {
343 	bool complete = true;
344 	struct dsa_port *dp;
345 
346 	list_for_each_entry(dp, &dst->ports, list) {
347 		if (dsa_port_is_dsa(dp)) {
348 			complete = dsa_port_setup_routing_table(dp);
349 			if (!complete)
350 				break;
351 		}
352 	}
353 
354 	return complete;
355 }
356 
357 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
358 {
359 	struct dsa_port *dp;
360 
361 	list_for_each_entry(dp, &dst->ports, list)
362 		if (dsa_port_is_cpu(dp))
363 			return dp;
364 
365 	return NULL;
366 }
367 
368 struct net_device *dsa_tree_find_first_conduit(struct dsa_switch_tree *dst)
369 {
370 	struct device_node *ethernet;
371 	struct net_device *conduit;
372 	struct dsa_port *cpu_dp;
373 
374 	cpu_dp = dsa_tree_find_first_cpu(dst);
375 	ethernet = of_parse_phandle(cpu_dp->dn, "ethernet", 0);
376 	conduit = of_find_net_device_by_node(ethernet);
377 	of_node_put(ethernet);
378 
379 	return conduit;
380 }
381 
382 /* Assign the default CPU port (the first one in the tree) to all ports of the
383  * fabric which don't already have one as part of their own switch.
384  */
385 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
386 {
387 	struct dsa_port *cpu_dp, *dp;
388 
389 	cpu_dp = dsa_tree_find_first_cpu(dst);
390 	if (!cpu_dp) {
391 		pr_err("DSA: tree %d has no CPU port\n", dst->index);
392 		return -EINVAL;
393 	}
394 
395 	list_for_each_entry(dp, &dst->ports, list) {
396 		if (dp->cpu_dp)
397 			continue;
398 
399 		if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
400 			dp->cpu_dp = cpu_dp;
401 	}
402 
403 	return 0;
404 }
405 
406 static struct dsa_port *
407 dsa_switch_preferred_default_local_cpu_port(struct dsa_switch *ds)
408 {
409 	struct dsa_port *cpu_dp;
410 
411 	if (!ds->ops->preferred_default_local_cpu_port)
412 		return NULL;
413 
414 	cpu_dp = ds->ops->preferred_default_local_cpu_port(ds);
415 	if (!cpu_dp)
416 		return NULL;
417 
418 	if (WARN_ON(!dsa_port_is_cpu(cpu_dp) || cpu_dp->ds != ds))
419 		return NULL;
420 
421 	return cpu_dp;
422 }
423 
424 /* Perform initial assignment of CPU ports to user ports and DSA links in the
425  * fabric, giving preference to CPU ports local to each switch. Default to
426  * using the first CPU port in the switch tree if the port does not have a CPU
427  * port local to this switch.
428  */
429 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
430 {
431 	struct dsa_port *preferred_cpu_dp, *cpu_dp, *dp;
432 
433 	list_for_each_entry(cpu_dp, &dst->ports, list) {
434 		if (!dsa_port_is_cpu(cpu_dp))
435 			continue;
436 
437 		preferred_cpu_dp = dsa_switch_preferred_default_local_cpu_port(cpu_dp->ds);
438 		if (preferred_cpu_dp && preferred_cpu_dp != cpu_dp)
439 			continue;
440 
441 		/* Prefer a local CPU port */
442 		dsa_switch_for_each_port(dp, cpu_dp->ds) {
443 			/* Prefer the first local CPU port found */
444 			if (dp->cpu_dp)
445 				continue;
446 
447 			if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
448 				dp->cpu_dp = cpu_dp;
449 		}
450 	}
451 
452 	return dsa_tree_setup_default_cpu(dst);
453 }
454 
455 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
456 {
457 	struct dsa_port *dp;
458 
459 	list_for_each_entry(dp, &dst->ports, list)
460 		if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
461 			dp->cpu_dp = NULL;
462 }
463 
464 static int dsa_port_setup(struct dsa_port *dp)
465 {
466 	bool dsa_port_link_registered = false;
467 	struct dsa_switch *ds = dp->ds;
468 	bool dsa_port_enabled = false;
469 	int err = 0;
470 
471 	if (dp->setup)
472 		return 0;
473 
474 	err = dsa_port_devlink_setup(dp);
475 	if (err)
476 		return err;
477 
478 	switch (dp->type) {
479 	case DSA_PORT_TYPE_UNUSED:
480 		dsa_port_disable(dp);
481 		break;
482 	case DSA_PORT_TYPE_CPU:
483 		if (dp->dn) {
484 			err = dsa_shared_port_link_register_of(dp);
485 			if (err)
486 				break;
487 			dsa_port_link_registered = true;
488 		} else {
489 			dev_warn(ds->dev,
490 				 "skipping link registration for CPU port %d\n",
491 				 dp->index);
492 		}
493 
494 		err = dsa_port_enable(dp, NULL);
495 		if (err)
496 			break;
497 		dsa_port_enabled = true;
498 
499 		break;
500 	case DSA_PORT_TYPE_DSA:
501 		if (dp->dn) {
502 			err = dsa_shared_port_link_register_of(dp);
503 			if (err)
504 				break;
505 			dsa_port_link_registered = true;
506 		} else {
507 			dev_warn(ds->dev,
508 				 "skipping link registration for DSA port %d\n",
509 				 dp->index);
510 		}
511 
512 		err = dsa_port_enable(dp, NULL);
513 		if (err)
514 			break;
515 		dsa_port_enabled = true;
516 
517 		break;
518 	case DSA_PORT_TYPE_USER:
519 		of_get_mac_address(dp->dn, dp->mac);
520 		err = dsa_user_create(dp);
521 		break;
522 	}
523 
524 	if (err && dsa_port_enabled)
525 		dsa_port_disable(dp);
526 	if (err && dsa_port_link_registered)
527 		dsa_shared_port_link_unregister_of(dp);
528 	if (err) {
529 		dsa_port_devlink_teardown(dp);
530 		return err;
531 	}
532 
533 	dp->setup = true;
534 
535 	return 0;
536 }
537 
538 static void dsa_port_teardown(struct dsa_port *dp)
539 {
540 	if (!dp->setup)
541 		return;
542 
543 	switch (dp->type) {
544 	case DSA_PORT_TYPE_UNUSED:
545 		break;
546 	case DSA_PORT_TYPE_CPU:
547 		dsa_port_disable(dp);
548 		if (dp->dn)
549 			dsa_shared_port_link_unregister_of(dp);
550 		break;
551 	case DSA_PORT_TYPE_DSA:
552 		dsa_port_disable(dp);
553 		if (dp->dn)
554 			dsa_shared_port_link_unregister_of(dp);
555 		break;
556 	case DSA_PORT_TYPE_USER:
557 		if (dp->user) {
558 			dsa_user_destroy(dp->user);
559 			dp->user = NULL;
560 		}
561 		break;
562 	}
563 
564 	dsa_port_devlink_teardown(dp);
565 
566 	dp->setup = false;
567 }
568 
569 static int dsa_port_setup_as_unused(struct dsa_port *dp)
570 {
571 	dp->type = DSA_PORT_TYPE_UNUSED;
572 	return dsa_port_setup(dp);
573 }
574 
575 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
576 {
577 	const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
578 	struct dsa_switch_tree *dst = ds->dst;
579 	int err;
580 
581 	if (tag_ops->proto == dst->default_proto)
582 		goto connect;
583 
584 	rtnl_lock();
585 	err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
586 	rtnl_unlock();
587 	if (err) {
588 		dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
589 			tag_ops->name, ERR_PTR(err));
590 		return err;
591 	}
592 
593 connect:
594 	if (tag_ops->connect) {
595 		err = tag_ops->connect(ds);
596 		if (err)
597 			return err;
598 	}
599 
600 	if (ds->ops->connect_tag_protocol) {
601 		err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
602 		if (err) {
603 			dev_err(ds->dev,
604 				"Unable to connect to tag protocol \"%s\": %pe\n",
605 				tag_ops->name, ERR_PTR(err));
606 			goto disconnect;
607 		}
608 	}
609 
610 	return 0;
611 
612 disconnect:
613 	if (tag_ops->disconnect)
614 		tag_ops->disconnect(ds);
615 
616 	return err;
617 }
618 
619 static void dsa_switch_teardown_tag_protocol(struct dsa_switch *ds)
620 {
621 	const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
622 
623 	if (tag_ops->disconnect)
624 		tag_ops->disconnect(ds);
625 }
626 
627 static int dsa_switch_setup(struct dsa_switch *ds)
628 {
629 	int err;
630 
631 	if (ds->setup)
632 		return 0;
633 
634 	/* Initialize ds->phys_mii_mask before registering the user MDIO bus
635 	 * driver and before ops->setup() has run, since the switch drivers and
636 	 * the user MDIO bus driver rely on these values for probing PHY
637 	 * devices or not
638 	 */
639 	ds->phys_mii_mask |= dsa_user_ports(ds);
640 
641 	err = dsa_switch_devlink_alloc(ds);
642 	if (err)
643 		return err;
644 
645 	err = dsa_switch_register_notifier(ds);
646 	if (err)
647 		goto devlink_free;
648 
649 	ds->configure_vlan_while_not_filtering = true;
650 
651 	err = ds->ops->setup(ds);
652 	if (err < 0)
653 		goto unregister_notifier;
654 
655 	err = dsa_switch_setup_tag_protocol(ds);
656 	if (err)
657 		goto teardown;
658 
659 	if (!ds->user_mii_bus && ds->ops->phy_read) {
660 		ds->user_mii_bus = mdiobus_alloc();
661 		if (!ds->user_mii_bus) {
662 			err = -ENOMEM;
663 			goto teardown;
664 		}
665 
666 		dsa_user_mii_bus_init(ds);
667 
668 		err = mdiobus_register(ds->user_mii_bus);
669 		if (err < 0)
670 			goto free_user_mii_bus;
671 	}
672 
673 	dsa_switch_devlink_register(ds);
674 
675 	ds->setup = true;
676 	return 0;
677 
678 free_user_mii_bus:
679 	if (ds->user_mii_bus && ds->ops->phy_read)
680 		mdiobus_free(ds->user_mii_bus);
681 teardown:
682 	if (ds->ops->teardown)
683 		ds->ops->teardown(ds);
684 unregister_notifier:
685 	dsa_switch_unregister_notifier(ds);
686 devlink_free:
687 	dsa_switch_devlink_free(ds);
688 	return err;
689 }
690 
691 static void dsa_switch_teardown(struct dsa_switch *ds)
692 {
693 	if (!ds->setup)
694 		return;
695 
696 	dsa_switch_devlink_unregister(ds);
697 
698 	if (ds->user_mii_bus && ds->ops->phy_read) {
699 		mdiobus_unregister(ds->user_mii_bus);
700 		mdiobus_free(ds->user_mii_bus);
701 		ds->user_mii_bus = NULL;
702 	}
703 
704 	dsa_switch_teardown_tag_protocol(ds);
705 
706 	if (ds->ops->teardown)
707 		ds->ops->teardown(ds);
708 
709 	dsa_switch_unregister_notifier(ds);
710 
711 	dsa_switch_devlink_free(ds);
712 
713 	ds->setup = false;
714 }
715 
716 /* First tear down the non-shared, then the shared ports. This ensures that
717  * all work items scheduled by our switchdev handlers for user ports have
718  * completed before we destroy the refcounting kept on the shared ports.
719  */
720 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
721 {
722 	struct dsa_port *dp;
723 
724 	list_for_each_entry(dp, &dst->ports, list)
725 		if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
726 			dsa_port_teardown(dp);
727 
728 	dsa_flush_workqueue();
729 
730 	list_for_each_entry(dp, &dst->ports, list)
731 		if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
732 			dsa_port_teardown(dp);
733 }
734 
735 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
736 {
737 	struct dsa_port *dp;
738 
739 	list_for_each_entry(dp, &dst->ports, list)
740 		dsa_switch_teardown(dp->ds);
741 }
742 
743 /* Bring shared ports up first, then non-shared ports */
744 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
745 {
746 	struct dsa_port *dp;
747 	int err = 0;
748 
749 	list_for_each_entry(dp, &dst->ports, list) {
750 		if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
751 			err = dsa_port_setup(dp);
752 			if (err)
753 				goto teardown;
754 		}
755 	}
756 
757 	list_for_each_entry(dp, &dst->ports, list) {
758 		if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
759 			err = dsa_port_setup(dp);
760 			if (err) {
761 				err = dsa_port_setup_as_unused(dp);
762 				if (err)
763 					goto teardown;
764 			}
765 		}
766 	}
767 
768 	return 0;
769 
770 teardown:
771 	dsa_tree_teardown_ports(dst);
772 
773 	return err;
774 }
775 
776 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
777 {
778 	struct dsa_port *dp;
779 	int err = 0;
780 
781 	list_for_each_entry(dp, &dst->ports, list) {
782 		err = dsa_switch_setup(dp->ds);
783 		if (err) {
784 			dsa_tree_teardown_switches(dst);
785 			break;
786 		}
787 	}
788 
789 	return err;
790 }
791 
792 static int dsa_tree_setup_conduit(struct dsa_switch_tree *dst)
793 {
794 	struct dsa_port *cpu_dp;
795 	int err = 0;
796 
797 	rtnl_lock();
798 
799 	dsa_tree_for_each_cpu_port(cpu_dp, dst) {
800 		struct net_device *conduit = cpu_dp->conduit;
801 		bool admin_up = (conduit->flags & IFF_UP) &&
802 				!qdisc_tx_is_noop(conduit);
803 
804 		err = dsa_conduit_setup(conduit, cpu_dp);
805 		if (err)
806 			break;
807 
808 		/* Replay conduit state event */
809 		dsa_tree_conduit_admin_state_change(dst, conduit, admin_up);
810 		dsa_tree_conduit_oper_state_change(dst, conduit,
811 						   netif_oper_up(conduit));
812 	}
813 
814 	rtnl_unlock();
815 
816 	return err;
817 }
818 
819 static void dsa_tree_teardown_conduit(struct dsa_switch_tree *dst)
820 {
821 	struct dsa_port *cpu_dp;
822 
823 	rtnl_lock();
824 
825 	dsa_tree_for_each_cpu_port(cpu_dp, dst) {
826 		struct net_device *conduit = cpu_dp->conduit;
827 
828 		/* Synthesizing an "admin down" state is sufficient for
829 		 * the switches to get a notification if the conduit is
830 		 * currently up and running.
831 		 */
832 		dsa_tree_conduit_admin_state_change(dst, conduit, false);
833 
834 		dsa_conduit_teardown(conduit);
835 	}
836 
837 	rtnl_unlock();
838 }
839 
840 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
841 {
842 	unsigned int len = 0;
843 	struct dsa_port *dp;
844 
845 	list_for_each_entry(dp, &dst->ports, list) {
846 		if (dp->ds->num_lag_ids > len)
847 			len = dp->ds->num_lag_ids;
848 	}
849 
850 	if (!len)
851 		return 0;
852 
853 	dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
854 	if (!dst->lags)
855 		return -ENOMEM;
856 
857 	dst->lags_len = len;
858 	return 0;
859 }
860 
861 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
862 {
863 	kfree(dst->lags);
864 }
865 
866 static void dsa_tree_teardown_routing_table(struct dsa_switch_tree *dst)
867 {
868 	struct dsa_link *dl, *next;
869 
870 	list_for_each_entry_safe(dl, next, &dst->rtable, list) {
871 		list_del(&dl->list);
872 		kfree(dl);
873 	}
874 }
875 
876 static int dsa_tree_setup(struct dsa_switch_tree *dst)
877 {
878 	bool complete;
879 	int err;
880 
881 	if (dst->setup) {
882 		pr_err("DSA: tree %d already setup! Disjoint trees?\n",
883 		       dst->index);
884 		return -EEXIST;
885 	}
886 
887 	complete = dsa_tree_setup_routing_table(dst);
888 	if (!complete)
889 		return 0;
890 
891 	err = dsa_tree_setup_cpu_ports(dst);
892 	if (err)
893 		goto teardown_rtable;
894 
895 	err = dsa_tree_setup_switches(dst);
896 	if (err)
897 		goto teardown_cpu_ports;
898 
899 	err = dsa_tree_setup_ports(dst);
900 	if (err)
901 		goto teardown_switches;
902 
903 	err = dsa_tree_setup_conduit(dst);
904 	if (err)
905 		goto teardown_ports;
906 
907 	err = dsa_tree_setup_lags(dst);
908 	if (err)
909 		goto teardown_conduit;
910 
911 	dst->setup = true;
912 
913 	pr_info("DSA: tree %d setup\n", dst->index);
914 
915 	return 0;
916 
917 teardown_conduit:
918 	dsa_tree_teardown_conduit(dst);
919 teardown_ports:
920 	dsa_tree_teardown_ports(dst);
921 teardown_switches:
922 	dsa_tree_teardown_switches(dst);
923 teardown_cpu_ports:
924 	dsa_tree_teardown_cpu_ports(dst);
925 teardown_rtable:
926 	dsa_tree_teardown_routing_table(dst);
927 
928 	return err;
929 }
930 
931 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
932 {
933 	if (!dst->setup)
934 		return;
935 
936 	dsa_tree_teardown_lags(dst);
937 
938 	dsa_tree_teardown_conduit(dst);
939 
940 	dsa_tree_teardown_ports(dst);
941 
942 	dsa_tree_teardown_switches(dst);
943 
944 	dsa_tree_teardown_cpu_ports(dst);
945 
946 	dsa_tree_teardown_routing_table(dst);
947 
948 	pr_info("DSA: tree %d torn down\n", dst->index);
949 
950 	dst->setup = false;
951 }
952 
953 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
954 				   const struct dsa_device_ops *tag_ops)
955 {
956 	const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
957 	struct dsa_notifier_tag_proto_info info;
958 	int err;
959 
960 	dst->tag_ops = tag_ops;
961 
962 	/* Notify the switches from this tree about the connection
963 	 * to the new tagger
964 	 */
965 	info.tag_ops = tag_ops;
966 	err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
967 	if (err && err != -EOPNOTSUPP)
968 		goto out_disconnect;
969 
970 	/* Notify the old tagger about the disconnection from this tree */
971 	info.tag_ops = old_tag_ops;
972 	dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
973 
974 	return 0;
975 
976 out_disconnect:
977 	info.tag_ops = tag_ops;
978 	dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
979 	dst->tag_ops = old_tag_ops;
980 
981 	return err;
982 }
983 
984 /* Since the dsa/tagging sysfs device attribute is per conduit, the assumption
985  * is that all DSA switches within a tree share the same tagger, otherwise
986  * they would have formed disjoint trees (different "dsa,member" values).
987  */
988 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
989 			      const struct dsa_device_ops *tag_ops,
990 			      const struct dsa_device_ops *old_tag_ops)
991 {
992 	struct dsa_notifier_tag_proto_info info;
993 	struct dsa_port *dp;
994 	int err = -EBUSY;
995 
996 	if (!rtnl_trylock())
997 		return restart_syscall();
998 
999 	/* At the moment we don't allow changing the tag protocol under
1000 	 * traffic. The rtnl_mutex also happens to serialize concurrent
1001 	 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1002 	 * restriction, there needs to be another mutex which serializes this.
1003 	 */
1004 	dsa_tree_for_each_user_port(dp, dst) {
1005 		if (dsa_port_to_conduit(dp)->flags & IFF_UP)
1006 			goto out_unlock;
1007 
1008 		if (dp->user->flags & IFF_UP)
1009 			goto out_unlock;
1010 	}
1011 
1012 	/* Notify the tag protocol change */
1013 	info.tag_ops = tag_ops;
1014 	err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1015 	if (err)
1016 		goto out_unwind_tagger;
1017 
1018 	err = dsa_tree_bind_tag_proto(dst, tag_ops);
1019 	if (err)
1020 		goto out_unwind_tagger;
1021 
1022 	rtnl_unlock();
1023 
1024 	return 0;
1025 
1026 out_unwind_tagger:
1027 	info.tag_ops = old_tag_ops;
1028 	dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1029 out_unlock:
1030 	rtnl_unlock();
1031 	return err;
1032 }
1033 
1034 static void dsa_tree_conduit_state_change(struct dsa_switch_tree *dst,
1035 					  struct net_device *conduit)
1036 {
1037 	struct dsa_notifier_conduit_state_info info;
1038 	struct dsa_port *cpu_dp = conduit->dsa_ptr;
1039 
1040 	info.conduit = conduit;
1041 	info.operational = dsa_port_conduit_is_operational(cpu_dp);
1042 
1043 	dsa_tree_notify(dst, DSA_NOTIFIER_CONDUIT_STATE_CHANGE, &info);
1044 }
1045 
1046 void dsa_tree_conduit_admin_state_change(struct dsa_switch_tree *dst,
1047 					 struct net_device *conduit,
1048 					 bool up)
1049 {
1050 	struct dsa_port *cpu_dp = conduit->dsa_ptr;
1051 	bool notify = false;
1052 
1053 	/* Don't keep track of admin state on LAG DSA conduits,
1054 	 * but rather just of physical DSA conduits
1055 	 */
1056 	if (netif_is_lag_master(conduit))
1057 		return;
1058 
1059 	if ((dsa_port_conduit_is_operational(cpu_dp)) !=
1060 	    (up && cpu_dp->conduit_oper_up))
1061 		notify = true;
1062 
1063 	cpu_dp->conduit_admin_up = up;
1064 
1065 	if (notify)
1066 		dsa_tree_conduit_state_change(dst, conduit);
1067 }
1068 
1069 void dsa_tree_conduit_oper_state_change(struct dsa_switch_tree *dst,
1070 					struct net_device *conduit,
1071 					bool up)
1072 {
1073 	struct dsa_port *cpu_dp = conduit->dsa_ptr;
1074 	bool notify = false;
1075 
1076 	/* Don't keep track of oper state on LAG DSA conduits,
1077 	 * but rather just of physical DSA conduits
1078 	 */
1079 	if (netif_is_lag_master(conduit))
1080 		return;
1081 
1082 	if ((dsa_port_conduit_is_operational(cpu_dp)) !=
1083 	    (cpu_dp->conduit_admin_up && up))
1084 		notify = true;
1085 
1086 	cpu_dp->conduit_oper_up = up;
1087 
1088 	if (notify)
1089 		dsa_tree_conduit_state_change(dst, conduit);
1090 }
1091 
1092 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1093 {
1094 	struct dsa_switch_tree *dst = ds->dst;
1095 	struct dsa_port *dp;
1096 
1097 	dsa_switch_for_each_port(dp, ds)
1098 		if (dp->index == index)
1099 			return dp;
1100 
1101 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1102 	if (!dp)
1103 		return NULL;
1104 
1105 	dp->ds = ds;
1106 	dp->index = index;
1107 
1108 	mutex_init(&dp->addr_lists_lock);
1109 	mutex_init(&dp->vlans_lock);
1110 	INIT_LIST_HEAD(&dp->fdbs);
1111 	INIT_LIST_HEAD(&dp->mdbs);
1112 	INIT_LIST_HEAD(&dp->vlans); /* also initializes &dp->user_vlans */
1113 	INIT_LIST_HEAD(&dp->list);
1114 	list_add_tail(&dp->list, &dst->ports);
1115 
1116 	return dp;
1117 }
1118 
1119 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1120 {
1121 	dp->type = DSA_PORT_TYPE_USER;
1122 	dp->name = name;
1123 
1124 	return 0;
1125 }
1126 
1127 static int dsa_port_parse_dsa(struct dsa_port *dp)
1128 {
1129 	dp->type = DSA_PORT_TYPE_DSA;
1130 
1131 	return 0;
1132 }
1133 
1134 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1135 						  struct net_device *conduit)
1136 {
1137 	enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1138 	struct dsa_switch *mds, *ds = dp->ds;
1139 	unsigned int mdp_upstream;
1140 	struct dsa_port *mdp;
1141 
1142 	/* It is possible to stack DSA switches onto one another when that
1143 	 * happens the switch driver may want to know if its tagging protocol
1144 	 * is going to work in such a configuration.
1145 	 */
1146 	if (dsa_user_dev_check(conduit)) {
1147 		mdp = dsa_user_to_port(conduit);
1148 		mds = mdp->ds;
1149 		mdp_upstream = dsa_upstream_port(mds, mdp->index);
1150 		tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1151 							  DSA_TAG_PROTO_NONE);
1152 	}
1153 
1154 	/* If the conduit device is not itself a DSA user in a disjoint DSA
1155 	 * tree, then return immediately.
1156 	 */
1157 	return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1158 }
1159 
1160 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *conduit,
1161 			      const char *user_protocol)
1162 {
1163 	const struct dsa_device_ops *tag_ops = NULL;
1164 	struct dsa_switch *ds = dp->ds;
1165 	struct dsa_switch_tree *dst = ds->dst;
1166 	enum dsa_tag_protocol default_proto;
1167 
1168 	/* Find out which protocol the switch would prefer. */
1169 	default_proto = dsa_get_tag_protocol(dp, conduit);
1170 	if (dst->default_proto) {
1171 		if (dst->default_proto != default_proto) {
1172 			dev_err(ds->dev,
1173 				"A DSA switch tree can have only one tagging protocol\n");
1174 			return -EINVAL;
1175 		}
1176 	} else {
1177 		dst->default_proto = default_proto;
1178 	}
1179 
1180 	/* See if the user wants to override that preference. */
1181 	if (user_protocol) {
1182 		if (!ds->ops->change_tag_protocol) {
1183 			dev_err(ds->dev, "Tag protocol cannot be modified\n");
1184 			return -EINVAL;
1185 		}
1186 
1187 		tag_ops = dsa_tag_driver_get_by_name(user_protocol);
1188 		if (IS_ERR(tag_ops)) {
1189 			dev_warn(ds->dev,
1190 				 "Failed to find a tagging driver for protocol %s, using default\n",
1191 				 user_protocol);
1192 			tag_ops = NULL;
1193 		}
1194 	}
1195 
1196 	if (!tag_ops)
1197 		tag_ops = dsa_tag_driver_get_by_id(default_proto);
1198 
1199 	if (IS_ERR(tag_ops)) {
1200 		if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1201 			return -EPROBE_DEFER;
1202 
1203 		dev_warn(ds->dev, "No tagger for this switch\n");
1204 		return PTR_ERR(tag_ops);
1205 	}
1206 
1207 	if (dst->tag_ops) {
1208 		if (dst->tag_ops != tag_ops) {
1209 			dev_err(ds->dev,
1210 				"A DSA switch tree can have only one tagging protocol\n");
1211 
1212 			dsa_tag_driver_put(tag_ops);
1213 			return -EINVAL;
1214 		}
1215 
1216 		/* In the case of multiple CPU ports per switch, the tagging
1217 		 * protocol is still reference-counted only per switch tree.
1218 		 */
1219 		dsa_tag_driver_put(tag_ops);
1220 	} else {
1221 		dst->tag_ops = tag_ops;
1222 	}
1223 
1224 	dp->conduit = conduit;
1225 	dp->type = DSA_PORT_TYPE_CPU;
1226 	dsa_port_set_tag_protocol(dp, dst->tag_ops);
1227 	dp->dst = dst;
1228 
1229 	/* At this point, the tree may be configured to use a different
1230 	 * tagger than the one chosen by the switch driver during
1231 	 * .setup, in the case when a user selects a custom protocol
1232 	 * through the DT.
1233 	 *
1234 	 * This is resolved by syncing the driver with the tree in
1235 	 * dsa_switch_setup_tag_protocol once .setup has run and the
1236 	 * driver is ready to accept calls to .change_tag_protocol. If
1237 	 * the driver does not support the custom protocol at that
1238 	 * point, the tree is wholly rejected, thereby ensuring that the
1239 	 * tree and driver are always in agreement on the protocol to
1240 	 * use.
1241 	 */
1242 	return 0;
1243 }
1244 
1245 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1246 {
1247 	struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1248 	const char *name = of_get_property(dn, "label", NULL);
1249 	bool link = of_property_read_bool(dn, "link");
1250 
1251 	dp->dn = dn;
1252 
1253 	if (ethernet) {
1254 		struct net_device *conduit;
1255 		const char *user_protocol;
1256 
1257 		conduit = of_find_net_device_by_node(ethernet);
1258 		of_node_put(ethernet);
1259 		if (!conduit)
1260 			return -EPROBE_DEFER;
1261 
1262 		user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1263 		return dsa_port_parse_cpu(dp, conduit, user_protocol);
1264 	}
1265 
1266 	if (link)
1267 		return dsa_port_parse_dsa(dp);
1268 
1269 	return dsa_port_parse_user(dp, name);
1270 }
1271 
1272 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1273 				     struct device_node *dn)
1274 {
1275 	struct device_node *ports, *port;
1276 	struct dsa_port *dp;
1277 	int err = 0;
1278 	u32 reg;
1279 
1280 	ports = of_get_child_by_name(dn, "ports");
1281 	if (!ports) {
1282 		/* The second possibility is "ethernet-ports" */
1283 		ports = of_get_child_by_name(dn, "ethernet-ports");
1284 		if (!ports) {
1285 			dev_err(ds->dev, "no ports child node found\n");
1286 			return -EINVAL;
1287 		}
1288 	}
1289 
1290 	for_each_available_child_of_node(ports, port) {
1291 		err = of_property_read_u32(port, "reg", &reg);
1292 		if (err) {
1293 			of_node_put(port);
1294 			goto out_put_node;
1295 		}
1296 
1297 		if (reg >= ds->num_ports) {
1298 			dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1299 				port, reg, ds->num_ports);
1300 			of_node_put(port);
1301 			err = -EINVAL;
1302 			goto out_put_node;
1303 		}
1304 
1305 		dp = dsa_to_port(ds, reg);
1306 
1307 		err = dsa_port_parse_of(dp, port);
1308 		if (err) {
1309 			of_node_put(port);
1310 			goto out_put_node;
1311 		}
1312 	}
1313 
1314 out_put_node:
1315 	of_node_put(ports);
1316 	return err;
1317 }
1318 
1319 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1320 				      struct device_node *dn)
1321 {
1322 	u32 m[2] = { 0, 0 };
1323 	int sz;
1324 
1325 	/* Don't error out if this optional property isn't found */
1326 	sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1327 	if (sz < 0 && sz != -EINVAL)
1328 		return sz;
1329 
1330 	ds->index = m[1];
1331 
1332 	ds->dst = dsa_tree_touch(m[0]);
1333 	if (!ds->dst)
1334 		return -ENOMEM;
1335 
1336 	if (dsa_switch_find(ds->dst->index, ds->index)) {
1337 		dev_err(ds->dev,
1338 			"A DSA switch with index %d already exists in tree %d\n",
1339 			ds->index, ds->dst->index);
1340 		return -EEXIST;
1341 	}
1342 
1343 	if (ds->dst->last_switch < ds->index)
1344 		ds->dst->last_switch = ds->index;
1345 
1346 	return 0;
1347 }
1348 
1349 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1350 {
1351 	struct dsa_port *dp;
1352 	int port;
1353 
1354 	for (port = 0; port < ds->num_ports; port++) {
1355 		dp = dsa_port_touch(ds, port);
1356 		if (!dp)
1357 			return -ENOMEM;
1358 	}
1359 
1360 	return 0;
1361 }
1362 
1363 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1364 {
1365 	int err;
1366 
1367 	err = dsa_switch_parse_member_of(ds, dn);
1368 	if (err)
1369 		return err;
1370 
1371 	err = dsa_switch_touch_ports(ds);
1372 	if (err)
1373 		return err;
1374 
1375 	return dsa_switch_parse_ports_of(ds, dn);
1376 }
1377 
1378 static int dev_is_class(struct device *dev, const void *class)
1379 {
1380 	if (dev->class != NULL && !strcmp(dev->class->name, class))
1381 		return 1;
1382 
1383 	return 0;
1384 }
1385 
1386 static struct device *dev_find_class(struct device *parent, char *class)
1387 {
1388 	if (dev_is_class(parent, class)) {
1389 		get_device(parent);
1390 		return parent;
1391 	}
1392 
1393 	return device_find_child(parent, class, dev_is_class);
1394 }
1395 
1396 static struct net_device *dsa_dev_to_net_device(struct device *dev)
1397 {
1398 	struct device *d;
1399 
1400 	d = dev_find_class(dev, "net");
1401 	if (d != NULL) {
1402 		struct net_device *nd;
1403 
1404 		nd = to_net_dev(d);
1405 		dev_hold(nd);
1406 		put_device(d);
1407 
1408 		return nd;
1409 	}
1410 
1411 	return NULL;
1412 }
1413 
1414 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1415 			  struct device *dev)
1416 {
1417 	if (!strcmp(name, "cpu")) {
1418 		struct net_device *conduit;
1419 
1420 		conduit = dsa_dev_to_net_device(dev);
1421 		if (!conduit)
1422 			return -EPROBE_DEFER;
1423 
1424 		dev_put(conduit);
1425 
1426 		return dsa_port_parse_cpu(dp, conduit, NULL);
1427 	}
1428 
1429 	if (!strcmp(name, "dsa"))
1430 		return dsa_port_parse_dsa(dp);
1431 
1432 	return dsa_port_parse_user(dp, name);
1433 }
1434 
1435 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1436 				  struct dsa_chip_data *cd)
1437 {
1438 	bool valid_name_found = false;
1439 	struct dsa_port *dp;
1440 	struct device *dev;
1441 	const char *name;
1442 	unsigned int i;
1443 	int err;
1444 
1445 	for (i = 0; i < DSA_MAX_PORTS; i++) {
1446 		name = cd->port_names[i];
1447 		dev = cd->netdev[i];
1448 		dp = dsa_to_port(ds, i);
1449 
1450 		if (!name)
1451 			continue;
1452 
1453 		err = dsa_port_parse(dp, name, dev);
1454 		if (err)
1455 			return err;
1456 
1457 		valid_name_found = true;
1458 	}
1459 
1460 	if (!valid_name_found && i == DSA_MAX_PORTS)
1461 		return -EINVAL;
1462 
1463 	return 0;
1464 }
1465 
1466 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1467 {
1468 	int err;
1469 
1470 	ds->cd = cd;
1471 
1472 	/* We don't support interconnected switches nor multiple trees via
1473 	 * platform data, so this is the unique switch of the tree.
1474 	 */
1475 	ds->index = 0;
1476 	ds->dst = dsa_tree_touch(0);
1477 	if (!ds->dst)
1478 		return -ENOMEM;
1479 
1480 	err = dsa_switch_touch_ports(ds);
1481 	if (err)
1482 		return err;
1483 
1484 	return dsa_switch_parse_ports(ds, cd);
1485 }
1486 
1487 static void dsa_switch_release_ports(struct dsa_switch *ds)
1488 {
1489 	struct dsa_mac_addr *a, *tmp;
1490 	struct dsa_port *dp, *next;
1491 	struct dsa_vlan *v, *n;
1492 
1493 	dsa_switch_for_each_port_safe(dp, next, ds) {
1494 		/* These are either entries that upper layers lost track of
1495 		 * (probably due to bugs), or installed through interfaces
1496 		 * where one does not necessarily have to remove them, like
1497 		 * ndo_dflt_fdb_add().
1498 		 */
1499 		list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
1500 			dev_info(ds->dev,
1501 				 "Cleaning up unicast address %pM vid %u from port %d\n",
1502 				 a->addr, a->vid, dp->index);
1503 			list_del(&a->list);
1504 			kfree(a);
1505 		}
1506 
1507 		list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
1508 			dev_info(ds->dev,
1509 				 "Cleaning up multicast address %pM vid %u from port %d\n",
1510 				 a->addr, a->vid, dp->index);
1511 			list_del(&a->list);
1512 			kfree(a);
1513 		}
1514 
1515 		/* These are entries that upper layers have lost track of,
1516 		 * probably due to bugs, but also due to dsa_port_do_vlan_del()
1517 		 * having failed and the VLAN entry still lingering on.
1518 		 */
1519 		list_for_each_entry_safe(v, n, &dp->vlans, list) {
1520 			dev_info(ds->dev,
1521 				 "Cleaning up vid %u from port %d\n",
1522 				 v->vid, dp->index);
1523 			list_del(&v->list);
1524 			kfree(v);
1525 		}
1526 
1527 		list_del(&dp->list);
1528 		kfree(dp);
1529 	}
1530 }
1531 
1532 static int dsa_switch_probe(struct dsa_switch *ds)
1533 {
1534 	struct dsa_switch_tree *dst;
1535 	struct dsa_chip_data *pdata;
1536 	struct device_node *np;
1537 	int err;
1538 
1539 	if (!ds->dev)
1540 		return -ENODEV;
1541 
1542 	pdata = ds->dev->platform_data;
1543 	np = ds->dev->of_node;
1544 
1545 	if (!ds->num_ports)
1546 		return -EINVAL;
1547 
1548 	if (np) {
1549 		err = dsa_switch_parse_of(ds, np);
1550 		if (err)
1551 			dsa_switch_release_ports(ds);
1552 	} else if (pdata) {
1553 		err = dsa_switch_parse(ds, pdata);
1554 		if (err)
1555 			dsa_switch_release_ports(ds);
1556 	} else {
1557 		err = -ENODEV;
1558 	}
1559 
1560 	if (err)
1561 		return err;
1562 
1563 	dst = ds->dst;
1564 	dsa_tree_get(dst);
1565 	err = dsa_tree_setup(dst);
1566 	if (err) {
1567 		dsa_switch_release_ports(ds);
1568 		dsa_tree_put(dst);
1569 	}
1570 
1571 	return err;
1572 }
1573 
1574 int dsa_register_switch(struct dsa_switch *ds)
1575 {
1576 	int err;
1577 
1578 	mutex_lock(&dsa2_mutex);
1579 	err = dsa_switch_probe(ds);
1580 	dsa_tree_put(ds->dst);
1581 	mutex_unlock(&dsa2_mutex);
1582 
1583 	return err;
1584 }
1585 EXPORT_SYMBOL_GPL(dsa_register_switch);
1586 
1587 static void dsa_switch_remove(struct dsa_switch *ds)
1588 {
1589 	struct dsa_switch_tree *dst = ds->dst;
1590 
1591 	dsa_tree_teardown(dst);
1592 	dsa_switch_release_ports(ds);
1593 	dsa_tree_put(dst);
1594 }
1595 
1596 void dsa_unregister_switch(struct dsa_switch *ds)
1597 {
1598 	mutex_lock(&dsa2_mutex);
1599 	dsa_switch_remove(ds);
1600 	mutex_unlock(&dsa2_mutex);
1601 }
1602 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1603 
1604 /* If the DSA conduit chooses to unregister its net_device on .shutdown, DSA is
1605  * blocking that operation from completion, due to the dev_hold taken inside
1606  * netdev_upper_dev_link. Unlink the DSA user interfaces from being uppers of
1607  * the DSA conduit, so that the system can reboot successfully.
1608  */
1609 void dsa_switch_shutdown(struct dsa_switch *ds)
1610 {
1611 	struct net_device *conduit, *user_dev;
1612 	LIST_HEAD(close_list);
1613 	struct dsa_port *dp;
1614 
1615 	mutex_lock(&dsa2_mutex);
1616 
1617 	if (!ds->setup)
1618 		goto out;
1619 
1620 	rtnl_lock();
1621 
1622 	dsa_switch_for_each_cpu_port(dp, ds)
1623 		list_add(&dp->conduit->close_list, &close_list);
1624 
1625 	netif_close_many(&close_list, true);
1626 
1627 	dsa_switch_for_each_user_port(dp, ds) {
1628 		conduit = dsa_port_to_conduit(dp);
1629 		user_dev = dp->user;
1630 
1631 		netif_device_detach(user_dev);
1632 		netdev_upper_dev_unlink(conduit, user_dev);
1633 	}
1634 
1635 	/* Disconnect from further netdevice notifiers on the conduit,
1636 	 * since netdev_uses_dsa() will now return false.
1637 	 */
1638 	dsa_switch_for_each_cpu_port(dp, ds)
1639 		dp->conduit->dsa_ptr = NULL;
1640 
1641 	rtnl_unlock();
1642 out:
1643 	mutex_unlock(&dsa2_mutex);
1644 }
1645 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
1646 
1647 #ifdef CONFIG_PM_SLEEP
1648 static bool dsa_port_is_initialized(const struct dsa_port *dp)
1649 {
1650 	return dp->type == DSA_PORT_TYPE_USER && dp->user;
1651 }
1652 
1653 int dsa_switch_suspend(struct dsa_switch *ds)
1654 {
1655 	struct dsa_port *dp;
1656 	int ret = 0;
1657 
1658 	/* Suspend user network devices */
1659 	dsa_switch_for_each_port(dp, ds) {
1660 		if (!dsa_port_is_initialized(dp))
1661 			continue;
1662 
1663 		ret = dsa_user_suspend(dp->user);
1664 		if (ret)
1665 			return ret;
1666 	}
1667 
1668 	if (ds->ops->suspend)
1669 		ret = ds->ops->suspend(ds);
1670 
1671 	return ret;
1672 }
1673 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
1674 
1675 int dsa_switch_resume(struct dsa_switch *ds)
1676 {
1677 	struct dsa_port *dp;
1678 	int ret = 0;
1679 
1680 	if (ds->ops->resume)
1681 		ret = ds->ops->resume(ds);
1682 
1683 	if (ret)
1684 		return ret;
1685 
1686 	/* Resume user network devices */
1687 	dsa_switch_for_each_port(dp, ds) {
1688 		if (!dsa_port_is_initialized(dp))
1689 			continue;
1690 
1691 		ret = dsa_user_resume(dp->user);
1692 		if (ret)
1693 			return ret;
1694 	}
1695 
1696 	return 0;
1697 }
1698 EXPORT_SYMBOL_GPL(dsa_switch_resume);
1699 #endif
1700 
1701 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
1702 {
1703 	if (!netdev || !dsa_user_dev_check(netdev))
1704 		return ERR_PTR(-ENODEV);
1705 
1706 	return dsa_user_to_port(netdev);
1707 }
1708 EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
1709 
1710 bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
1711 {
1712 	if (a->type != b->type)
1713 		return false;
1714 
1715 	switch (a->type) {
1716 	case DSA_DB_PORT:
1717 		return a->dp == b->dp;
1718 	case DSA_DB_LAG:
1719 		return a->lag.dev == b->lag.dev;
1720 	case DSA_DB_BRIDGE:
1721 		return a->bridge.num == b->bridge.num;
1722 	default:
1723 		WARN_ON(1);
1724 		return false;
1725 	}
1726 }
1727 
1728 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1729 				 const unsigned char *addr, u16 vid,
1730 				 struct dsa_db db)
1731 {
1732 	struct dsa_port *dp = dsa_to_port(ds, port);
1733 	struct dsa_mac_addr *a;
1734 
1735 	lockdep_assert_held(&dp->addr_lists_lock);
1736 
1737 	list_for_each_entry(a, &dp->fdbs, list) {
1738 		if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
1739 			continue;
1740 
1741 		if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1742 			return true;
1743 	}
1744 
1745 	return false;
1746 }
1747 EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
1748 
1749 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1750 				 const struct switchdev_obj_port_mdb *mdb,
1751 				 struct dsa_db db)
1752 {
1753 	struct dsa_port *dp = dsa_to_port(ds, port);
1754 	struct dsa_mac_addr *a;
1755 
1756 	lockdep_assert_held(&dp->addr_lists_lock);
1757 
1758 	list_for_each_entry(a, &dp->mdbs, list) {
1759 		if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
1760 			continue;
1761 
1762 		if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1763 			return true;
1764 	}
1765 
1766 	return false;
1767 }
1768 EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
1769 
1770 /* Helpers for switches without specific HSR offloads, but which can implement
1771  * NETIF_F_HW_HSR_DUP because their tagger uses dsa_xmit_port_mask()
1772  */
1773 int dsa_port_simple_hsr_validate(struct dsa_switch *ds, int port,
1774 				 struct net_device *hsr,
1775 				 struct netlink_ext_ack *extack)
1776 {
1777 	enum hsr_port_type type;
1778 	int err;
1779 
1780 	err = hsr_get_port_type(hsr, dsa_to_port(ds, port)->user, &type);
1781 	if (err)
1782 		return err;
1783 
1784 	if (type != HSR_PT_SLAVE_A && type != HSR_PT_SLAVE_B) {
1785 		NL_SET_ERR_MSG_MOD(extack,
1786 				   "Only HSR slave ports can be offloaded");
1787 		return -EOPNOTSUPP;
1788 	}
1789 
1790 	return 0;
1791 }
1792 EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_validate);
1793 
1794 int dsa_port_simple_hsr_join(struct dsa_switch *ds, int port,
1795 			     struct net_device *hsr,
1796 			     struct netlink_ext_ack *extack)
1797 {
1798 	struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1799 	int err;
1800 
1801 	err = dsa_port_simple_hsr_validate(ds, port, hsr, extack);
1802 	if (err)
1803 		return err;
1804 
1805 	dsa_hsr_foreach_port(other_dp, ds, hsr) {
1806 		if (other_dp != dp) {
1807 			dp->user->features |= NETIF_F_HW_HSR_DUP;
1808 			other_dp->user->features |= NETIF_F_HW_HSR_DUP;
1809 			break;
1810 		}
1811 	}
1812 
1813 	return 0;
1814 }
1815 EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_join);
1816 
1817 int dsa_port_simple_hsr_leave(struct dsa_switch *ds, int port,
1818 			      struct net_device *hsr)
1819 {
1820 	struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
1821 
1822 	dsa_hsr_foreach_port(other_dp, ds, hsr) {
1823 		if (other_dp != dp) {
1824 			dp->user->features &= ~NETIF_F_HW_HSR_DUP;
1825 			other_dp->user->features &= ~NETIF_F_HW_HSR_DUP;
1826 			break;
1827 		}
1828 	}
1829 
1830 	return 0;
1831 }
1832 EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_leave);
1833 
1834 static const struct dsa_stubs __dsa_stubs = {
1835 	.conduit_hwtstamp_validate = __dsa_conduit_hwtstamp_validate,
1836 };
1837 
1838 static void dsa_register_stubs(void)
1839 {
1840 	dsa_stubs = &__dsa_stubs;
1841 }
1842 
1843 static void dsa_unregister_stubs(void)
1844 {
1845 	dsa_stubs = NULL;
1846 }
1847 
1848 static int __init dsa_init_module(void)
1849 {
1850 	int rc;
1851 
1852 	dsa_owq = alloc_ordered_workqueue("dsa_ordered",
1853 					  WQ_MEM_RECLAIM);
1854 	if (!dsa_owq)
1855 		return -ENOMEM;
1856 
1857 	rc = dsa_user_register_notifier();
1858 	if (rc)
1859 		goto register_notifier_fail;
1860 
1861 	dev_add_pack(&dsa_pack_type);
1862 
1863 	rc = rtnl_link_register(&dsa_link_ops);
1864 	if (rc)
1865 		goto netlink_register_fail;
1866 
1867 	dsa_register_stubs();
1868 
1869 	return 0;
1870 
1871 netlink_register_fail:
1872 	dsa_user_unregister_notifier();
1873 	dev_remove_pack(&dsa_pack_type);
1874 register_notifier_fail:
1875 	destroy_workqueue(dsa_owq);
1876 
1877 	return rc;
1878 }
1879 module_init(dsa_init_module);
1880 
1881 static void __exit dsa_cleanup_module(void)
1882 {
1883 	dsa_unregister_stubs();
1884 
1885 	rtnl_link_unregister(&dsa_link_ops);
1886 
1887 	dsa_user_unregister_notifier();
1888 	dev_remove_pack(&dsa_pack_type);
1889 	destroy_workqueue(dsa_owq);
1890 }
1891 module_exit(dsa_cleanup_module);
1892 
1893 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1894 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1895 MODULE_LICENSE("GPL");
1896 MODULE_ALIAS("platform:dsa");
1897 MODULE_IMPORT_NS("NETDEV_INTERNAL");
1898