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 struct devlink_port *dlp = &dp->devlink_port;
357
358 if (!dp->setup)
359 return;
360
361 devlink_port_type_clear(dlp);
362
363 switch (dp->type) {
364 case DSA_PORT_TYPE_UNUSED:
365 break;
366 case DSA_PORT_TYPE_CPU:
367 dsa_port_disable(dp);
368 dsa_tag_driver_put(dp->tag_ops);
369 dsa_port_link_unregister_of(dp);
370 break;
371 case DSA_PORT_TYPE_DSA:
372 dsa_port_disable(dp);
373 dsa_port_link_unregister_of(dp);
374 break;
375 case DSA_PORT_TYPE_USER:
376 if (dp->slave) {
377 dsa_slave_destroy(dp->slave);
378 dp->slave = NULL;
379 }
380 break;
381 }
382
383 dp->setup = false;
384 }
385
dsa_port_devlink_teardown(struct dsa_port * dp)386 static void dsa_port_devlink_teardown(struct dsa_port *dp)
387 {
388 struct devlink_port *dlp = &dp->devlink_port;
389
390 if (dp->devlink_port_setup)
391 devlink_port_unregister(dlp);
392 dp->devlink_port_setup = false;
393 }
394
dsa_devlink_info_get(struct devlink * dl,struct devlink_info_req * req,struct netlink_ext_ack * extack)395 static int dsa_devlink_info_get(struct devlink *dl,
396 struct devlink_info_req *req,
397 struct netlink_ext_ack *extack)
398 {
399 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
400
401 if (ds->ops->devlink_info_get)
402 return ds->ops->devlink_info_get(ds, req, extack);
403
404 return -EOPNOTSUPP;
405 }
406
407 static const struct devlink_ops dsa_devlink_ops = {
408 .info_get = dsa_devlink_info_get,
409 };
410
dsa_switch_setup(struct dsa_switch * ds)411 static int dsa_switch_setup(struct dsa_switch *ds)
412 {
413 struct dsa_devlink_priv *dl_priv;
414 struct dsa_port *dp;
415 int err;
416
417 if (ds->setup)
418 return 0;
419
420 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
421 * driver and before ops->setup() has run, since the switch drivers and
422 * the slave MDIO bus driver rely on these values for probing PHY
423 * devices or not
424 */
425 ds->phys_mii_mask |= dsa_user_ports(ds);
426
427 /* Add the switch to devlink before calling setup, so that setup can
428 * add dpipe tables
429 */
430 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
431 if (!ds->devlink)
432 return -ENOMEM;
433 dl_priv = devlink_priv(ds->devlink);
434 dl_priv->ds = ds;
435
436 err = devlink_register(ds->devlink, ds->dev);
437 if (err)
438 goto free_devlink;
439
440 /* Setup devlink port instances now, so that the switch
441 * setup() can register regions etc, against the ports
442 */
443 list_for_each_entry(dp, &ds->dst->ports, list) {
444 if (dp->ds == ds) {
445 err = dsa_port_devlink_setup(dp);
446 if (err)
447 goto unregister_devlink_ports;
448 }
449 }
450
451 err = dsa_switch_register_notifier(ds);
452 if (err)
453 goto unregister_devlink_ports;
454
455 err = ds->ops->setup(ds);
456 if (err < 0)
457 goto unregister_notifier;
458
459 devlink_params_publish(ds->devlink);
460
461 if (!ds->slave_mii_bus && ds->ops->phy_read) {
462 ds->slave_mii_bus = mdiobus_alloc();
463 if (!ds->slave_mii_bus) {
464 err = -ENOMEM;
465 goto teardown;
466 }
467
468 dsa_slave_mii_bus_init(ds);
469
470 err = mdiobus_register(ds->slave_mii_bus);
471 if (err < 0)
472 goto free_slave_mii_bus;
473 }
474
475 ds->setup = true;
476
477 return 0;
478
479 free_slave_mii_bus:
480 if (ds->slave_mii_bus && ds->ops->phy_read)
481 mdiobus_free(ds->slave_mii_bus);
482 teardown:
483 if (ds->ops->teardown)
484 ds->ops->teardown(ds);
485 unregister_notifier:
486 dsa_switch_unregister_notifier(ds);
487 unregister_devlink_ports:
488 list_for_each_entry(dp, &ds->dst->ports, list)
489 if (dp->ds == ds)
490 dsa_port_devlink_teardown(dp);
491 devlink_unregister(ds->devlink);
492 free_devlink:
493 devlink_free(ds->devlink);
494 ds->devlink = NULL;
495
496 return err;
497 }
498
dsa_switch_teardown(struct dsa_switch * ds)499 static void dsa_switch_teardown(struct dsa_switch *ds)
500 {
501 struct dsa_port *dp;
502
503 if (!ds->setup)
504 return;
505
506 if (ds->slave_mii_bus && ds->ops->phy_read) {
507 mdiobus_unregister(ds->slave_mii_bus);
508 mdiobus_free(ds->slave_mii_bus);
509 ds->slave_mii_bus = NULL;
510 }
511
512 dsa_switch_unregister_notifier(ds);
513
514 if (ds->ops->teardown)
515 ds->ops->teardown(ds);
516
517 if (ds->devlink) {
518 list_for_each_entry(dp, &ds->dst->ports, list)
519 if (dp->ds == ds)
520 dsa_port_devlink_teardown(dp);
521 devlink_unregister(ds->devlink);
522 devlink_free(ds->devlink);
523 ds->devlink = NULL;
524 }
525
526 ds->setup = false;
527 }
528
dsa_tree_setup_switches(struct dsa_switch_tree * dst)529 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
530 {
531 struct dsa_port *dp;
532 int err;
533
534 list_for_each_entry(dp, &dst->ports, list) {
535 err = dsa_switch_setup(dp->ds);
536 if (err)
537 goto teardown;
538 }
539
540 list_for_each_entry(dp, &dst->ports, list) {
541 err = dsa_port_setup(dp);
542 if (err) {
543 dsa_port_devlink_teardown(dp);
544 dp->type = DSA_PORT_TYPE_UNUSED;
545 err = dsa_port_devlink_setup(dp);
546 if (err)
547 goto teardown;
548 continue;
549 }
550 }
551
552 return 0;
553
554 teardown:
555 list_for_each_entry(dp, &dst->ports, list)
556 dsa_port_teardown(dp);
557
558 list_for_each_entry(dp, &dst->ports, list)
559 dsa_switch_teardown(dp->ds);
560
561 return err;
562 }
563
dsa_tree_teardown_switches(struct dsa_switch_tree * dst)564 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
565 {
566 struct dsa_port *dp;
567
568 list_for_each_entry(dp, &dst->ports, list)
569 dsa_port_teardown(dp);
570
571 list_for_each_entry(dp, &dst->ports, list)
572 dsa_switch_teardown(dp->ds);
573 }
574
dsa_tree_setup_master(struct dsa_switch_tree * dst)575 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
576 {
577 struct dsa_port *dp;
578 int err;
579
580 list_for_each_entry(dp, &dst->ports, list) {
581 if (dsa_port_is_cpu(dp)) {
582 err = dsa_master_setup(dp->master, dp);
583 if (err)
584 return err;
585 }
586 }
587
588 return 0;
589 }
590
dsa_tree_teardown_master(struct dsa_switch_tree * dst)591 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
592 {
593 struct dsa_port *dp;
594
595 list_for_each_entry(dp, &dst->ports, list)
596 if (dsa_port_is_cpu(dp))
597 dsa_master_teardown(dp->master);
598 }
599
dsa_tree_setup(struct dsa_switch_tree * dst)600 static int dsa_tree_setup(struct dsa_switch_tree *dst)
601 {
602 bool complete;
603 int err;
604
605 if (dst->setup) {
606 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
607 dst->index);
608 return -EEXIST;
609 }
610
611 complete = dsa_tree_setup_routing_table(dst);
612 if (!complete)
613 return 0;
614
615 err = dsa_tree_setup_default_cpu(dst);
616 if (err)
617 return err;
618
619 err = dsa_tree_setup_switches(dst);
620 if (err)
621 goto teardown_default_cpu;
622
623 err = dsa_tree_setup_master(dst);
624 if (err)
625 goto teardown_switches;
626
627 dst->setup = true;
628
629 pr_info("DSA: tree %d setup\n", dst->index);
630
631 return 0;
632
633 teardown_switches:
634 dsa_tree_teardown_switches(dst);
635 teardown_default_cpu:
636 dsa_tree_teardown_default_cpu(dst);
637
638 return err;
639 }
640
dsa_tree_teardown(struct dsa_switch_tree * dst)641 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
642 {
643 struct dsa_link *dl, *next;
644
645 if (!dst->setup)
646 return;
647
648 dsa_tree_teardown_master(dst);
649
650 dsa_tree_teardown_switches(dst);
651
652 dsa_tree_teardown_default_cpu(dst);
653
654 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
655 list_del(&dl->list);
656 kfree(dl);
657 }
658
659 pr_info("DSA: tree %d torn down\n", dst->index);
660
661 dst->setup = false;
662 }
663
dsa_port_touch(struct dsa_switch * ds,int index)664 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
665 {
666 struct dsa_switch_tree *dst = ds->dst;
667 struct dsa_port *dp;
668
669 list_for_each_entry(dp, &dst->ports, list)
670 if (dp->ds == ds && dp->index == index)
671 return dp;
672
673 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
674 if (!dp)
675 return NULL;
676
677 dp->ds = ds;
678 dp->index = index;
679
680 INIT_LIST_HEAD(&dp->list);
681 list_add_tail(&dp->list, &dst->ports);
682
683 return dp;
684 }
685
dsa_port_parse_user(struct dsa_port * dp,const char * name)686 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
687 {
688 if (!name)
689 name = "eth%d";
690
691 dp->type = DSA_PORT_TYPE_USER;
692 dp->name = name;
693
694 return 0;
695 }
696
dsa_port_parse_dsa(struct dsa_port * dp)697 static int dsa_port_parse_dsa(struct dsa_port *dp)
698 {
699 dp->type = DSA_PORT_TYPE_DSA;
700
701 return 0;
702 }
703
dsa_get_tag_protocol(struct dsa_port * dp,struct net_device * master)704 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
705 struct net_device *master)
706 {
707 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
708 struct dsa_switch *mds, *ds = dp->ds;
709 unsigned int mdp_upstream;
710 struct dsa_port *mdp;
711
712 /* It is possible to stack DSA switches onto one another when that
713 * happens the switch driver may want to know if its tagging protocol
714 * is going to work in such a configuration.
715 */
716 if (dsa_slave_dev_check(master)) {
717 mdp = dsa_slave_to_port(master);
718 mds = mdp->ds;
719 mdp_upstream = dsa_upstream_port(mds, mdp->index);
720 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
721 DSA_TAG_PROTO_NONE);
722 }
723
724 /* If the master device is not itself a DSA slave in a disjoint DSA
725 * tree, then return immediately.
726 */
727 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
728 }
729
dsa_port_parse_cpu(struct dsa_port * dp,struct net_device * master)730 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
731 {
732 struct dsa_switch *ds = dp->ds;
733 struct dsa_switch_tree *dst = ds->dst;
734 const struct dsa_device_ops *tag_ops;
735 enum dsa_tag_protocol tag_protocol;
736
737 tag_protocol = dsa_get_tag_protocol(dp, master);
738 tag_ops = dsa_tag_driver_get(tag_protocol);
739 if (IS_ERR(tag_ops)) {
740 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
741 return -EPROBE_DEFER;
742 dev_warn(ds->dev, "No tagger for this switch\n");
743 dp->master = NULL;
744 return PTR_ERR(tag_ops);
745 }
746
747 dp->master = master;
748 dp->type = DSA_PORT_TYPE_CPU;
749 dp->filter = tag_ops->filter;
750 dp->rcv = tag_ops->rcv;
751 dp->tag_ops = tag_ops;
752 dp->dst = dst;
753
754 return 0;
755 }
756
dsa_port_parse_of(struct dsa_port * dp,struct device_node * dn)757 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
758 {
759 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
760 const char *name = of_get_property(dn, "label", NULL);
761 bool link = of_property_read_bool(dn, "link");
762
763 dp->dn = dn;
764
765 if (ethernet) {
766 struct net_device *master;
767
768 master = of_find_net_device_by_node(ethernet);
769 if (!master)
770 return -EPROBE_DEFER;
771
772 return dsa_port_parse_cpu(dp, master);
773 }
774
775 if (link)
776 return dsa_port_parse_dsa(dp);
777
778 return dsa_port_parse_user(dp, name);
779 }
780
dsa_switch_parse_ports_of(struct dsa_switch * ds,struct device_node * dn)781 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
782 struct device_node *dn)
783 {
784 struct device_node *ports, *port;
785 struct dsa_port *dp;
786 int err = 0;
787 u32 reg;
788
789 ports = of_get_child_by_name(dn, "ports");
790 if (!ports) {
791 /* The second possibility is "ethernet-ports" */
792 ports = of_get_child_by_name(dn, "ethernet-ports");
793 if (!ports) {
794 dev_err(ds->dev, "no ports child node found\n");
795 return -EINVAL;
796 }
797 }
798
799 for_each_available_child_of_node(ports, port) {
800 err = of_property_read_u32(port, "reg", ®);
801 if (err)
802 goto out_put_node;
803
804 if (reg >= ds->num_ports) {
805 err = -EINVAL;
806 goto out_put_node;
807 }
808
809 dp = dsa_to_port(ds, reg);
810
811 err = dsa_port_parse_of(dp, port);
812 if (err)
813 goto out_put_node;
814 }
815
816 out_put_node:
817 of_node_put(ports);
818 return err;
819 }
820
dsa_switch_parse_member_of(struct dsa_switch * ds,struct device_node * dn)821 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
822 struct device_node *dn)
823 {
824 u32 m[2] = { 0, 0 };
825 int sz;
826
827 /* Don't error out if this optional property isn't found */
828 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
829 if (sz < 0 && sz != -EINVAL)
830 return sz;
831
832 ds->index = m[1];
833
834 ds->dst = dsa_tree_touch(m[0]);
835 if (!ds->dst)
836 return -ENOMEM;
837
838 return 0;
839 }
840
dsa_switch_touch_ports(struct dsa_switch * ds)841 static int dsa_switch_touch_ports(struct dsa_switch *ds)
842 {
843 struct dsa_port *dp;
844 int port;
845
846 for (port = 0; port < ds->num_ports; port++) {
847 dp = dsa_port_touch(ds, port);
848 if (!dp)
849 return -ENOMEM;
850 }
851
852 return 0;
853 }
854
dsa_switch_parse_of(struct dsa_switch * ds,struct device_node * dn)855 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
856 {
857 int err;
858
859 err = dsa_switch_parse_member_of(ds, dn);
860 if (err)
861 return err;
862
863 err = dsa_switch_touch_ports(ds);
864 if (err)
865 return err;
866
867 return dsa_switch_parse_ports_of(ds, dn);
868 }
869
dsa_port_parse(struct dsa_port * dp,const char * name,struct device * dev)870 static int dsa_port_parse(struct dsa_port *dp, const char *name,
871 struct device *dev)
872 {
873 if (!strcmp(name, "cpu")) {
874 struct net_device *master;
875
876 master = dsa_dev_to_net_device(dev);
877 if (!master)
878 return -EPROBE_DEFER;
879
880 dev_put(master);
881
882 return dsa_port_parse_cpu(dp, master);
883 }
884
885 if (!strcmp(name, "dsa"))
886 return dsa_port_parse_dsa(dp);
887
888 return dsa_port_parse_user(dp, name);
889 }
890
dsa_switch_parse_ports(struct dsa_switch * ds,struct dsa_chip_data * cd)891 static int dsa_switch_parse_ports(struct dsa_switch *ds,
892 struct dsa_chip_data *cd)
893 {
894 bool valid_name_found = false;
895 struct dsa_port *dp;
896 struct device *dev;
897 const char *name;
898 unsigned int i;
899 int err;
900
901 for (i = 0; i < DSA_MAX_PORTS; i++) {
902 name = cd->port_names[i];
903 dev = cd->netdev[i];
904 dp = dsa_to_port(ds, i);
905
906 if (!name)
907 continue;
908
909 err = dsa_port_parse(dp, name, dev);
910 if (err)
911 return err;
912
913 valid_name_found = true;
914 }
915
916 if (!valid_name_found && i == DSA_MAX_PORTS)
917 return -EINVAL;
918
919 return 0;
920 }
921
dsa_switch_parse(struct dsa_switch * ds,struct dsa_chip_data * cd)922 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
923 {
924 int err;
925
926 ds->cd = cd;
927
928 /* We don't support interconnected switches nor multiple trees via
929 * platform data, so this is the unique switch of the tree.
930 */
931 ds->index = 0;
932 ds->dst = dsa_tree_touch(0);
933 if (!ds->dst)
934 return -ENOMEM;
935
936 err = dsa_switch_touch_ports(ds);
937 if (err)
938 return err;
939
940 return dsa_switch_parse_ports(ds, cd);
941 }
942
dsa_switch_release_ports(struct dsa_switch * ds)943 static void dsa_switch_release_ports(struct dsa_switch *ds)
944 {
945 struct dsa_switch_tree *dst = ds->dst;
946 struct dsa_port *dp, *next;
947
948 list_for_each_entry_safe(dp, next, &dst->ports, list) {
949 if (dp->ds != ds)
950 continue;
951 list_del(&dp->list);
952 kfree(dp);
953 }
954 }
955
dsa_switch_probe(struct dsa_switch * ds)956 static int dsa_switch_probe(struct dsa_switch *ds)
957 {
958 struct dsa_switch_tree *dst;
959 struct dsa_chip_data *pdata;
960 struct device_node *np;
961 int err;
962
963 if (!ds->dev)
964 return -ENODEV;
965
966 pdata = ds->dev->platform_data;
967 np = ds->dev->of_node;
968
969 if (!ds->num_ports)
970 return -EINVAL;
971
972 if (np) {
973 err = dsa_switch_parse_of(ds, np);
974 if (err)
975 dsa_switch_release_ports(ds);
976 } else if (pdata) {
977 err = dsa_switch_parse(ds, pdata);
978 if (err)
979 dsa_switch_release_ports(ds);
980 } else {
981 err = -ENODEV;
982 }
983
984 if (err)
985 return err;
986
987 dst = ds->dst;
988 dsa_tree_get(dst);
989 err = dsa_tree_setup(dst);
990 if (err) {
991 dsa_switch_release_ports(ds);
992 dsa_tree_put(dst);
993 }
994
995 return err;
996 }
997
dsa_register_switch(struct dsa_switch * ds)998 int dsa_register_switch(struct dsa_switch *ds)
999 {
1000 int err;
1001
1002 mutex_lock(&dsa2_mutex);
1003 err = dsa_switch_probe(ds);
1004 dsa_tree_put(ds->dst);
1005 mutex_unlock(&dsa2_mutex);
1006
1007 return err;
1008 }
1009 EXPORT_SYMBOL_GPL(dsa_register_switch);
1010
dsa_switch_remove(struct dsa_switch * ds)1011 static void dsa_switch_remove(struct dsa_switch *ds)
1012 {
1013 struct dsa_switch_tree *dst = ds->dst;
1014
1015 dsa_tree_teardown(dst);
1016 dsa_switch_release_ports(ds);
1017 dsa_tree_put(dst);
1018 }
1019
dsa_unregister_switch(struct dsa_switch * ds)1020 void dsa_unregister_switch(struct dsa_switch *ds)
1021 {
1022 mutex_lock(&dsa2_mutex);
1023 dsa_switch_remove(ds);
1024 mutex_unlock(&dsa2_mutex);
1025 }
1026 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1027