1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6  * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/of.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
18 
19 #include "dsa_priv.h"
20 
21 static DEFINE_MUTEX(dsa2_mutex);
22 LIST_HEAD(dsa_tree_list);
23 
dsa_switch_find(int tree_index,int sw_index)24 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
25 {
26 	struct dsa_switch_tree *dst;
27 	struct dsa_port *dp;
28 
29 	list_for_each_entry(dst, &dsa_tree_list, list) {
30 		if (dst->index != tree_index)
31 			continue;
32 
33 		list_for_each_entry(dp, &dst->ports, list) {
34 			if (dp->ds->index != sw_index)
35 				continue;
36 
37 			return dp->ds;
38 		}
39 	}
40 
41 	return NULL;
42 }
43 EXPORT_SYMBOL_GPL(dsa_switch_find);
44 
dsa_tree_find(int index)45 static struct dsa_switch_tree *dsa_tree_find(int index)
46 {
47 	struct dsa_switch_tree *dst;
48 
49 	list_for_each_entry(dst, &dsa_tree_list, list)
50 		if (dst->index == index)
51 			return dst;
52 
53 	return NULL;
54 }
55 
dsa_tree_alloc(int index)56 static struct dsa_switch_tree *dsa_tree_alloc(int index)
57 {
58 	struct dsa_switch_tree *dst;
59 
60 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61 	if (!dst)
62 		return NULL;
63 
64 	dst->index = index;
65 
66 	INIT_LIST_HEAD(&dst->rtable);
67 
68 	INIT_LIST_HEAD(&dst->ports);
69 
70 	INIT_LIST_HEAD(&dst->list);
71 	list_add_tail(&dst->list, &dsa_tree_list);
72 
73 	kref_init(&dst->refcount);
74 
75 	return dst;
76 }
77 
dsa_tree_free(struct dsa_switch_tree * dst)78 static void dsa_tree_free(struct dsa_switch_tree *dst)
79 {
80 	list_del(&dst->list);
81 	kfree(dst);
82 }
83 
dsa_tree_get(struct dsa_switch_tree * dst)84 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
85 {
86 	if (dst)
87 		kref_get(&dst->refcount);
88 
89 	return dst;
90 }
91 
dsa_tree_touch(int index)92 static struct dsa_switch_tree *dsa_tree_touch(int index)
93 {
94 	struct dsa_switch_tree *dst;
95 
96 	dst = dsa_tree_find(index);
97 	if (dst)
98 		return dsa_tree_get(dst);
99 	else
100 		return dsa_tree_alloc(index);
101 }
102 
dsa_tree_release(struct kref * ref)103 static void dsa_tree_release(struct kref *ref)
104 {
105 	struct dsa_switch_tree *dst;
106 
107 	dst = container_of(ref, struct dsa_switch_tree, refcount);
108 
109 	dsa_tree_free(dst);
110 }
111 
dsa_tree_put(struct dsa_switch_tree * dst)112 static void dsa_tree_put(struct dsa_switch_tree *dst)
113 {
114 	if (dst)
115 		kref_put(&dst->refcount, dsa_tree_release);
116 }
117 
dsa_port_is_dsa(struct dsa_port * port)118 static bool dsa_port_is_dsa(struct dsa_port *port)
119 {
120 	return port->type == DSA_PORT_TYPE_DSA;
121 }
122 
dsa_port_is_cpu(struct dsa_port * port)123 static bool dsa_port_is_cpu(struct dsa_port *port)
124 {
125 	return port->type == DSA_PORT_TYPE_CPU;
126 }
127 
dsa_port_is_user(struct dsa_port * dp)128 static bool dsa_port_is_user(struct dsa_port *dp)
129 {
130 	return dp->type == DSA_PORT_TYPE_USER;
131 }
132 
dsa_tree_find_port_by_node(struct dsa_switch_tree * dst,struct device_node * dn)133 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
134 						   struct device_node *dn)
135 {
136 	struct dsa_port *dp;
137 
138 	list_for_each_entry(dp, &dst->ports, list)
139 		if (dp->dn == dn)
140 			return dp;
141 
142 	return NULL;
143 }
144 
dsa_link_touch(struct dsa_port * dp,struct dsa_port * link_dp)145 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
146 				       struct dsa_port *link_dp)
147 {
148 	struct dsa_switch *ds = dp->ds;
149 	struct dsa_switch_tree *dst;
150 	struct dsa_link *dl;
151 
152 	dst = ds->dst;
153 
154 	list_for_each_entry(dl, &dst->rtable, list)
155 		if (dl->dp == dp && dl->link_dp == link_dp)
156 			return dl;
157 
158 	dl = kzalloc(sizeof(*dl), GFP_KERNEL);
159 	if (!dl)
160 		return NULL;
161 
162 	dl->dp = dp;
163 	dl->link_dp = link_dp;
164 
165 	INIT_LIST_HEAD(&dl->list);
166 	list_add_tail(&dl->list, &dst->rtable);
167 
168 	return dl;
169 }
170 
dsa_port_setup_routing_table(struct dsa_port * dp)171 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
172 {
173 	struct dsa_switch *ds = dp->ds;
174 	struct dsa_switch_tree *dst = ds->dst;
175 	struct device_node *dn = dp->dn;
176 	struct of_phandle_iterator it;
177 	struct dsa_port *link_dp;
178 	struct dsa_link *dl;
179 	int err;
180 
181 	of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
182 		link_dp = dsa_tree_find_port_by_node(dst, it.node);
183 		if (!link_dp) {
184 			of_node_put(it.node);
185 			return false;
186 		}
187 
188 		dl = dsa_link_touch(dp, link_dp);
189 		if (!dl) {
190 			of_node_put(it.node);
191 			return false;
192 		}
193 	}
194 
195 	return true;
196 }
197 
dsa_tree_setup_routing_table(struct dsa_switch_tree * dst)198 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
199 {
200 	bool complete = true;
201 	struct dsa_port *dp;
202 
203 	list_for_each_entry(dp, &dst->ports, list) {
204 		if (dsa_port_is_dsa(dp)) {
205 			complete = dsa_port_setup_routing_table(dp);
206 			if (!complete)
207 				break;
208 		}
209 	}
210 
211 	return complete;
212 }
213 
dsa_tree_find_first_cpu(struct dsa_switch_tree * dst)214 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
215 {
216 	struct dsa_port *dp;
217 
218 	list_for_each_entry(dp, &dst->ports, list)
219 		if (dsa_port_is_cpu(dp))
220 			return dp;
221 
222 	return NULL;
223 }
224 
dsa_tree_setup_default_cpu(struct dsa_switch_tree * dst)225 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
226 {
227 	struct dsa_port *cpu_dp, *dp;
228 
229 	cpu_dp = dsa_tree_find_first_cpu(dst);
230 	if (!cpu_dp) {
231 		pr_err("DSA: tree %d has no CPU port\n", dst->index);
232 		return -EINVAL;
233 	}
234 
235 	/* Assign the default CPU port to all ports of the fabric */
236 	list_for_each_entry(dp, &dst->ports, list)
237 		if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
238 			dp->cpu_dp = cpu_dp;
239 
240 	return 0;
241 }
242 
dsa_tree_teardown_default_cpu(struct dsa_switch_tree * dst)243 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
244 {
245 	struct dsa_port *dp;
246 
247 	list_for_each_entry(dp, &dst->ports, list)
248 		if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
249 			dp->cpu_dp = NULL;
250 }
251 
dsa_port_setup(struct dsa_port * dp)252 static int dsa_port_setup(struct dsa_port *dp)
253 {
254 	struct devlink_port *dlp = &dp->devlink_port;
255 	bool dsa_port_link_registered = false;
256 	bool dsa_port_enabled = false;
257 	int err = 0;
258 
259 	if (dp->setup)
260 		return 0;
261 
262 	switch (dp->type) {
263 	case DSA_PORT_TYPE_UNUSED:
264 		dsa_port_disable(dp);
265 		break;
266 	case DSA_PORT_TYPE_CPU:
267 		err = dsa_port_link_register_of(dp);
268 		if (err)
269 			break;
270 		dsa_port_link_registered = true;
271 
272 		err = dsa_port_enable(dp, NULL);
273 		if (err)
274 			break;
275 		dsa_port_enabled = true;
276 
277 		break;
278 	case DSA_PORT_TYPE_DSA:
279 		err = dsa_port_link_register_of(dp);
280 		if (err)
281 			break;
282 		dsa_port_link_registered = true;
283 
284 		err = dsa_port_enable(dp, NULL);
285 		if (err)
286 			break;
287 		dsa_port_enabled = true;
288 
289 		break;
290 	case DSA_PORT_TYPE_USER:
291 		dp->mac = of_get_mac_address(dp->dn);
292 		err = dsa_slave_create(dp);
293 		if (err)
294 			break;
295 
296 		devlink_port_type_eth_set(dlp, dp->slave);
297 		break;
298 	}
299 
300 	if (err && dsa_port_enabled)
301 		dsa_port_disable(dp);
302 	if (err && dsa_port_link_registered)
303 		dsa_port_link_unregister_of(dp);
304 	if (err)
305 		return err;
306 
307 	dp->setup = true;
308 
309 	return 0;
310 }
311 
dsa_port_devlink_setup(struct dsa_port * dp)312 static int dsa_port_devlink_setup(struct dsa_port *dp)
313 {
314 	struct devlink_port *dlp = &dp->devlink_port;
315 	struct dsa_switch_tree *dst = dp->ds->dst;
316 	struct devlink_port_attrs attrs = {};
317 	struct devlink *dl = dp->ds->devlink;
318 	const unsigned char *id;
319 	unsigned char len;
320 	int err;
321 
322 	id = (const unsigned char *)&dst->index;
323 	len = sizeof(dst->index);
324 
325 	attrs.phys.port_number = dp->index;
326 	memcpy(attrs.switch_id.id, id, len);
327 	attrs.switch_id.id_len = len;
328 	memset(dlp, 0, sizeof(*dlp));
329 
330 	switch (dp->type) {
331 	case DSA_PORT_TYPE_UNUSED:
332 		attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
333 		break;
334 	case DSA_PORT_TYPE_CPU:
335 		attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
336 		break;
337 	case DSA_PORT_TYPE_DSA:
338 		attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
339 		break;
340 	case DSA_PORT_TYPE_USER:
341 		attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
342 		break;
343 	}
344 
345 	devlink_port_attrs_set(dlp, &attrs);
346 	err = devlink_port_register(dl, dlp, dp->index);
347 
348 	if (!err)
349 		dp->devlink_port_setup = true;
350 
351 	return err;
352 }
353 
dsa_port_teardown(struct dsa_port * dp)354 static void dsa_port_teardown(struct dsa_port *dp)
355 {
356 	if (!dp->setup)
357 		return;
358 
359 	switch (dp->type) {
360 	case DSA_PORT_TYPE_UNUSED:
361 		break;
362 	case DSA_PORT_TYPE_CPU:
363 		dsa_port_disable(dp);
364 		dsa_tag_driver_put(dp->tag_ops);
365 		dsa_port_link_unregister_of(dp);
366 		break;
367 	case DSA_PORT_TYPE_DSA:
368 		dsa_port_disable(dp);
369 		dsa_port_link_unregister_of(dp);
370 		break;
371 	case DSA_PORT_TYPE_USER:
372 		if (dp->slave) {
373 			dsa_slave_destroy(dp->slave);
374 			dp->slave = NULL;
375 		}
376 		break;
377 	}
378 
379 	dp->setup = false;
380 }
381 
dsa_port_devlink_teardown(struct dsa_port * dp)382 static void dsa_port_devlink_teardown(struct dsa_port *dp)
383 {
384 	struct devlink_port *dlp = &dp->devlink_port;
385 
386 	if (dp->devlink_port_setup)
387 		devlink_port_unregister(dlp);
388 	dp->devlink_port_setup = false;
389 }
390 
dsa_devlink_info_get(struct devlink * dl,struct devlink_info_req * req,struct netlink_ext_ack * extack)391 static int dsa_devlink_info_get(struct devlink *dl,
392 				struct devlink_info_req *req,
393 				struct netlink_ext_ack *extack)
394 {
395 	struct dsa_switch *ds = dsa_devlink_to_ds(dl);
396 
397 	if (ds->ops->devlink_info_get)
398 		return ds->ops->devlink_info_get(ds, req, extack);
399 
400 	return -EOPNOTSUPP;
401 }
402 
403 static const struct devlink_ops dsa_devlink_ops = {
404 	.info_get = dsa_devlink_info_get,
405 };
406 
dsa_switch_setup(struct dsa_switch * ds)407 static int dsa_switch_setup(struct dsa_switch *ds)
408 {
409 	struct dsa_devlink_priv *dl_priv;
410 	struct dsa_port *dp;
411 	int err;
412 
413 	if (ds->setup)
414 		return 0;
415 
416 	/* Initialize ds->phys_mii_mask before registering the slave MDIO bus
417 	 * driver and before ops->setup() has run, since the switch drivers and
418 	 * the slave MDIO bus driver rely on these values for probing PHY
419 	 * devices or not
420 	 */
421 	ds->phys_mii_mask |= dsa_user_ports(ds);
422 
423 	/* Add the switch to devlink before calling setup, so that setup can
424 	 * add dpipe tables
425 	 */
426 	ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
427 	if (!ds->devlink)
428 		return -ENOMEM;
429 	dl_priv = devlink_priv(ds->devlink);
430 	dl_priv->ds = ds;
431 
432 	err = devlink_register(ds->devlink, ds->dev);
433 	if (err)
434 		goto free_devlink;
435 
436 	/* Setup devlink port instances now, so that the switch
437 	 * setup() can register regions etc, against the ports
438 	 */
439 	list_for_each_entry(dp, &ds->dst->ports, list) {
440 		if (dp->ds == ds) {
441 			err = dsa_port_devlink_setup(dp);
442 			if (err)
443 				goto unregister_devlink_ports;
444 		}
445 	}
446 
447 	err = dsa_switch_register_notifier(ds);
448 	if (err)
449 		goto unregister_devlink_ports;
450 
451 	err = ds->ops->setup(ds);
452 	if (err < 0)
453 		goto unregister_notifier;
454 
455 	devlink_params_publish(ds->devlink);
456 
457 	if (!ds->slave_mii_bus && ds->ops->phy_read) {
458 		ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
459 		if (!ds->slave_mii_bus) {
460 			err = -ENOMEM;
461 			goto unregister_notifier;
462 		}
463 
464 		dsa_slave_mii_bus_init(ds);
465 
466 		err = mdiobus_register(ds->slave_mii_bus);
467 		if (err < 0)
468 			goto unregister_notifier;
469 	}
470 
471 	ds->setup = true;
472 
473 	return 0;
474 
475 unregister_notifier:
476 	dsa_switch_unregister_notifier(ds);
477 unregister_devlink_ports:
478 	list_for_each_entry(dp, &ds->dst->ports, list)
479 		if (dp->ds == ds)
480 			dsa_port_devlink_teardown(dp);
481 	devlink_unregister(ds->devlink);
482 free_devlink:
483 	devlink_free(ds->devlink);
484 	ds->devlink = NULL;
485 
486 	return err;
487 }
488 
dsa_switch_teardown(struct dsa_switch * ds)489 static void dsa_switch_teardown(struct dsa_switch *ds)
490 {
491 	struct dsa_port *dp;
492 
493 	if (!ds->setup)
494 		return;
495 
496 	if (ds->slave_mii_bus && ds->ops->phy_read)
497 		mdiobus_unregister(ds->slave_mii_bus);
498 
499 	dsa_switch_unregister_notifier(ds);
500 
501 	if (ds->ops->teardown)
502 		ds->ops->teardown(ds);
503 
504 	if (ds->devlink) {
505 		list_for_each_entry(dp, &ds->dst->ports, list)
506 			if (dp->ds == ds)
507 				dsa_port_devlink_teardown(dp);
508 		devlink_unregister(ds->devlink);
509 		devlink_free(ds->devlink);
510 		ds->devlink = NULL;
511 	}
512 
513 	ds->setup = false;
514 }
515 
dsa_tree_setup_switches(struct dsa_switch_tree * dst)516 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
517 {
518 	struct dsa_port *dp;
519 	int err;
520 
521 	list_for_each_entry(dp, &dst->ports, list) {
522 		err = dsa_switch_setup(dp->ds);
523 		if (err)
524 			goto teardown;
525 	}
526 
527 	list_for_each_entry(dp, &dst->ports, list) {
528 		err = dsa_port_setup(dp);
529 		if (err)
530 			continue;
531 	}
532 
533 	return 0;
534 
535 teardown:
536 	list_for_each_entry(dp, &dst->ports, list)
537 		dsa_port_teardown(dp);
538 
539 	list_for_each_entry(dp, &dst->ports, list)
540 		dsa_switch_teardown(dp->ds);
541 
542 	return err;
543 }
544 
dsa_tree_teardown_switches(struct dsa_switch_tree * dst)545 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
546 {
547 	struct dsa_port *dp;
548 
549 	list_for_each_entry(dp, &dst->ports, list)
550 		dsa_port_teardown(dp);
551 
552 	list_for_each_entry(dp, &dst->ports, list)
553 		dsa_switch_teardown(dp->ds);
554 }
555 
dsa_tree_setup_master(struct dsa_switch_tree * dst)556 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
557 {
558 	struct dsa_port *dp;
559 	int err;
560 
561 	list_for_each_entry(dp, &dst->ports, list) {
562 		if (dsa_port_is_cpu(dp)) {
563 			err = dsa_master_setup(dp->master, dp);
564 			if (err)
565 				return err;
566 		}
567 	}
568 
569 	return 0;
570 }
571 
dsa_tree_teardown_master(struct dsa_switch_tree * dst)572 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
573 {
574 	struct dsa_port *dp;
575 
576 	list_for_each_entry(dp, &dst->ports, list)
577 		if (dsa_port_is_cpu(dp))
578 			dsa_master_teardown(dp->master);
579 }
580 
dsa_tree_setup(struct dsa_switch_tree * dst)581 static int dsa_tree_setup(struct dsa_switch_tree *dst)
582 {
583 	bool complete;
584 	int err;
585 
586 	if (dst->setup) {
587 		pr_err("DSA: tree %d already setup! Disjoint trees?\n",
588 		       dst->index);
589 		return -EEXIST;
590 	}
591 
592 	complete = dsa_tree_setup_routing_table(dst);
593 	if (!complete)
594 		return 0;
595 
596 	err = dsa_tree_setup_default_cpu(dst);
597 	if (err)
598 		return err;
599 
600 	err = dsa_tree_setup_switches(dst);
601 	if (err)
602 		goto teardown_default_cpu;
603 
604 	err = dsa_tree_setup_master(dst);
605 	if (err)
606 		goto teardown_switches;
607 
608 	dst->setup = true;
609 
610 	pr_info("DSA: tree %d setup\n", dst->index);
611 
612 	return 0;
613 
614 teardown_switches:
615 	dsa_tree_teardown_switches(dst);
616 teardown_default_cpu:
617 	dsa_tree_teardown_default_cpu(dst);
618 
619 	return err;
620 }
621 
dsa_tree_teardown(struct dsa_switch_tree * dst)622 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
623 {
624 	struct dsa_link *dl, *next;
625 
626 	if (!dst->setup)
627 		return;
628 
629 	dsa_tree_teardown_master(dst);
630 
631 	dsa_tree_teardown_switches(dst);
632 
633 	dsa_tree_teardown_default_cpu(dst);
634 
635 	list_for_each_entry_safe(dl, next, &dst->rtable, list) {
636 		list_del(&dl->list);
637 		kfree(dl);
638 	}
639 
640 	pr_info("DSA: tree %d torn down\n", dst->index);
641 
642 	dst->setup = false;
643 }
644 
dsa_port_touch(struct dsa_switch * ds,int index)645 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
646 {
647 	struct dsa_switch_tree *dst = ds->dst;
648 	struct dsa_port *dp;
649 
650 	list_for_each_entry(dp, &dst->ports, list)
651 		if (dp->ds == ds && dp->index == index)
652 			return dp;
653 
654 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
655 	if (!dp)
656 		return NULL;
657 
658 	dp->ds = ds;
659 	dp->index = index;
660 
661 	INIT_LIST_HEAD(&dp->list);
662 	list_add_tail(&dp->list, &dst->ports);
663 
664 	return dp;
665 }
666 
dsa_port_parse_user(struct dsa_port * dp,const char * name)667 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
668 {
669 	if (!name)
670 		name = "eth%d";
671 
672 	dp->type = DSA_PORT_TYPE_USER;
673 	dp->name = name;
674 
675 	return 0;
676 }
677 
dsa_port_parse_dsa(struct dsa_port * dp)678 static int dsa_port_parse_dsa(struct dsa_port *dp)
679 {
680 	dp->type = DSA_PORT_TYPE_DSA;
681 
682 	return 0;
683 }
684 
dsa_get_tag_protocol(struct dsa_port * dp,struct net_device * master)685 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
686 						  struct net_device *master)
687 {
688 	enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
689 	struct dsa_switch *mds, *ds = dp->ds;
690 	unsigned int mdp_upstream;
691 	struct dsa_port *mdp;
692 
693 	/* It is possible to stack DSA switches onto one another when that
694 	 * happens the switch driver may want to know if its tagging protocol
695 	 * is going to work in such a configuration.
696 	 */
697 	if (dsa_slave_dev_check(master)) {
698 		mdp = dsa_slave_to_port(master);
699 		mds = mdp->ds;
700 		mdp_upstream = dsa_upstream_port(mds, mdp->index);
701 		tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
702 							  DSA_TAG_PROTO_NONE);
703 	}
704 
705 	/* If the master device is not itself a DSA slave in a disjoint DSA
706 	 * tree, then return immediately.
707 	 */
708 	return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
709 }
710 
dsa_port_parse_cpu(struct dsa_port * dp,struct net_device * master)711 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
712 {
713 	struct dsa_switch *ds = dp->ds;
714 	struct dsa_switch_tree *dst = ds->dst;
715 	const struct dsa_device_ops *tag_ops;
716 	enum dsa_tag_protocol tag_protocol;
717 
718 	tag_protocol = dsa_get_tag_protocol(dp, master);
719 	tag_ops = dsa_tag_driver_get(tag_protocol);
720 	if (IS_ERR(tag_ops)) {
721 		if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
722 			return -EPROBE_DEFER;
723 		dev_warn(ds->dev, "No tagger for this switch\n");
724 		dp->master = NULL;
725 		return PTR_ERR(tag_ops);
726 	}
727 
728 	dp->master = master;
729 	dp->type = DSA_PORT_TYPE_CPU;
730 	dp->filter = tag_ops->filter;
731 	dp->rcv = tag_ops->rcv;
732 	dp->tag_ops = tag_ops;
733 	dp->dst = dst;
734 
735 	return 0;
736 }
737 
dsa_port_parse_of(struct dsa_port * dp,struct device_node * dn)738 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
739 {
740 	struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
741 	const char *name = of_get_property(dn, "label", NULL);
742 	bool link = of_property_read_bool(dn, "link");
743 
744 	dp->dn = dn;
745 
746 	if (ethernet) {
747 		struct net_device *master;
748 
749 		master = of_find_net_device_by_node(ethernet);
750 		if (!master)
751 			return -EPROBE_DEFER;
752 
753 		return dsa_port_parse_cpu(dp, master);
754 	}
755 
756 	if (link)
757 		return dsa_port_parse_dsa(dp);
758 
759 	return dsa_port_parse_user(dp, name);
760 }
761 
dsa_switch_parse_ports_of(struct dsa_switch * ds,struct device_node * dn)762 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
763 				     struct device_node *dn)
764 {
765 	struct device_node *ports, *port;
766 	struct dsa_port *dp;
767 	int err = 0;
768 	u32 reg;
769 
770 	ports = of_get_child_by_name(dn, "ports");
771 	if (!ports) {
772 		/* The second possibility is "ethernet-ports" */
773 		ports = of_get_child_by_name(dn, "ethernet-ports");
774 		if (!ports) {
775 			dev_err(ds->dev, "no ports child node found\n");
776 			return -EINVAL;
777 		}
778 	}
779 
780 	for_each_available_child_of_node(ports, port) {
781 		err = of_property_read_u32(port, "reg", &reg);
782 		if (err)
783 			goto out_put_node;
784 
785 		if (reg >= ds->num_ports) {
786 			err = -EINVAL;
787 			goto out_put_node;
788 		}
789 
790 		dp = dsa_to_port(ds, reg);
791 
792 		err = dsa_port_parse_of(dp, port);
793 		if (err)
794 			goto out_put_node;
795 	}
796 
797 out_put_node:
798 	of_node_put(ports);
799 	return err;
800 }
801 
dsa_switch_parse_member_of(struct dsa_switch * ds,struct device_node * dn)802 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
803 				      struct device_node *dn)
804 {
805 	u32 m[2] = { 0, 0 };
806 	int sz;
807 
808 	/* Don't error out if this optional property isn't found */
809 	sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
810 	if (sz < 0 && sz != -EINVAL)
811 		return sz;
812 
813 	ds->index = m[1];
814 
815 	ds->dst = dsa_tree_touch(m[0]);
816 	if (!ds->dst)
817 		return -ENOMEM;
818 
819 	return 0;
820 }
821 
dsa_switch_touch_ports(struct dsa_switch * ds)822 static int dsa_switch_touch_ports(struct dsa_switch *ds)
823 {
824 	struct dsa_port *dp;
825 	int port;
826 
827 	for (port = 0; port < ds->num_ports; port++) {
828 		dp = dsa_port_touch(ds, port);
829 		if (!dp)
830 			return -ENOMEM;
831 	}
832 
833 	return 0;
834 }
835 
dsa_switch_parse_of(struct dsa_switch * ds,struct device_node * dn)836 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
837 {
838 	int err;
839 
840 	err = dsa_switch_parse_member_of(ds, dn);
841 	if (err)
842 		return err;
843 
844 	err = dsa_switch_touch_ports(ds);
845 	if (err)
846 		return err;
847 
848 	return dsa_switch_parse_ports_of(ds, dn);
849 }
850 
dsa_port_parse(struct dsa_port * dp,const char * name,struct device * dev)851 static int dsa_port_parse(struct dsa_port *dp, const char *name,
852 			  struct device *dev)
853 {
854 	if (!strcmp(name, "cpu")) {
855 		struct net_device *master;
856 
857 		master = dsa_dev_to_net_device(dev);
858 		if (!master)
859 			return -EPROBE_DEFER;
860 
861 		dev_put(master);
862 
863 		return dsa_port_parse_cpu(dp, master);
864 	}
865 
866 	if (!strcmp(name, "dsa"))
867 		return dsa_port_parse_dsa(dp);
868 
869 	return dsa_port_parse_user(dp, name);
870 }
871 
dsa_switch_parse_ports(struct dsa_switch * ds,struct dsa_chip_data * cd)872 static int dsa_switch_parse_ports(struct dsa_switch *ds,
873 				  struct dsa_chip_data *cd)
874 {
875 	bool valid_name_found = false;
876 	struct dsa_port *dp;
877 	struct device *dev;
878 	const char *name;
879 	unsigned int i;
880 	int err;
881 
882 	for (i = 0; i < DSA_MAX_PORTS; i++) {
883 		name = cd->port_names[i];
884 		dev = cd->netdev[i];
885 		dp = dsa_to_port(ds, i);
886 
887 		if (!name)
888 			continue;
889 
890 		err = dsa_port_parse(dp, name, dev);
891 		if (err)
892 			return err;
893 
894 		valid_name_found = true;
895 	}
896 
897 	if (!valid_name_found && i == DSA_MAX_PORTS)
898 		return -EINVAL;
899 
900 	return 0;
901 }
902 
dsa_switch_parse(struct dsa_switch * ds,struct dsa_chip_data * cd)903 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
904 {
905 	int err;
906 
907 	ds->cd = cd;
908 
909 	/* We don't support interconnected switches nor multiple trees via
910 	 * platform data, so this is the unique switch of the tree.
911 	 */
912 	ds->index = 0;
913 	ds->dst = dsa_tree_touch(0);
914 	if (!ds->dst)
915 		return -ENOMEM;
916 
917 	err = dsa_switch_touch_ports(ds);
918 	if (err)
919 		return err;
920 
921 	return dsa_switch_parse_ports(ds, cd);
922 }
923 
dsa_switch_release_ports(struct dsa_switch * ds)924 static void dsa_switch_release_ports(struct dsa_switch *ds)
925 {
926 	struct dsa_switch_tree *dst = ds->dst;
927 	struct dsa_port *dp, *next;
928 
929 	list_for_each_entry_safe(dp, next, &dst->ports, list) {
930 		if (dp->ds != ds)
931 			continue;
932 		list_del(&dp->list);
933 		kfree(dp);
934 	}
935 }
936 
dsa_switch_probe(struct dsa_switch * ds)937 static int dsa_switch_probe(struct dsa_switch *ds)
938 {
939 	struct dsa_switch_tree *dst;
940 	struct dsa_chip_data *pdata;
941 	struct device_node *np;
942 	int err;
943 
944 	if (!ds->dev)
945 		return -ENODEV;
946 
947 	pdata = ds->dev->platform_data;
948 	np = ds->dev->of_node;
949 
950 	if (!ds->num_ports)
951 		return -EINVAL;
952 
953 	if (np) {
954 		err = dsa_switch_parse_of(ds, np);
955 		if (err)
956 			dsa_switch_release_ports(ds);
957 	} else if (pdata) {
958 		err = dsa_switch_parse(ds, pdata);
959 		if (err)
960 			dsa_switch_release_ports(ds);
961 	} else {
962 		err = -ENODEV;
963 	}
964 
965 	if (err)
966 		return err;
967 
968 	dst = ds->dst;
969 	dsa_tree_get(dst);
970 	err = dsa_tree_setup(dst);
971 	if (err) {
972 		dsa_switch_release_ports(ds);
973 		dsa_tree_put(dst);
974 	}
975 
976 	return err;
977 }
978 
dsa_register_switch(struct dsa_switch * ds)979 int dsa_register_switch(struct dsa_switch *ds)
980 {
981 	int err;
982 
983 	mutex_lock(&dsa2_mutex);
984 	err = dsa_switch_probe(ds);
985 	dsa_tree_put(ds->dst);
986 	mutex_unlock(&dsa2_mutex);
987 
988 	return err;
989 }
990 EXPORT_SYMBOL_GPL(dsa_register_switch);
991 
dsa_switch_remove(struct dsa_switch * ds)992 static void dsa_switch_remove(struct dsa_switch *ds)
993 {
994 	struct dsa_switch_tree *dst = ds->dst;
995 
996 	dsa_tree_teardown(dst);
997 	dsa_switch_release_ports(ds);
998 	dsa_tree_put(dst);
999 }
1000 
dsa_unregister_switch(struct dsa_switch * ds)1001 void dsa_unregister_switch(struct dsa_switch *ds)
1002 {
1003 	mutex_lock(&dsa2_mutex);
1004 	dsa_switch_remove(ds);
1005 	mutex_unlock(&dsa2_mutex);
1006 }
1007 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1008