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