blob: 866222a8f9bf56933acebd8b2f0c44d22091fbae [file] [log] [blame]
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001/*
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)
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030031 if (dst->tree == tree) {
32 kref_get(&dst->refcount);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020033 return dst;
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030034 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +020035 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;
Andrew Lunn83c0afa2016-06-04 21:17:07 +020060 INIT_LIST_HEAD(&dst->list);
61 list_add_tail(&dsa_switch_trees, &dst->list);
62 kref_init(&dst->refcount);
63
64 return dst;
65}
66
67static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
68 struct dsa_switch *ds, u32 index)
69{
70 kref_get(&dst->refcount);
71 dst->ds[index] = ds;
72}
73
74static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
75 struct dsa_switch *ds, u32 index)
76{
77 dst->ds[index] = NULL;
78 kref_put(&dst->refcount, dsa_free_dst);
79}
80
81static bool dsa_port_is_dsa(struct device_node *port)
82{
Vivien Didelot9f914842017-01-09 18:13:51 -050083 return !!of_parse_phandle(port, "link", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020084}
85
86static bool dsa_port_is_cpu(struct device_node *port)
87{
Vivien Didelot9f914842017-01-09 18:13:51 -050088 return !!of_parse_phandle(port, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020089}
90
91static bool dsa_ds_find_port(struct dsa_switch *ds,
92 struct device_node *port)
93{
94 u32 index;
95
96 for (index = 0; index < DSA_MAX_PORTS; index++)
97 if (ds->ports[index].dn == port)
98 return true;
99 return false;
100}
101
102static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
103 struct device_node *port)
104{
105 struct dsa_switch *ds;
106 u32 index;
107
108 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
109 ds = dst->ds[index];
110 if (!ds)
111 continue;
112
113 if (dsa_ds_find_port(ds, port))
114 return ds;
115 }
116
117 return NULL;
118}
119
120static int dsa_port_complete(struct dsa_switch_tree *dst,
121 struct dsa_switch *src_ds,
122 struct device_node *port,
123 u32 src_port)
124{
125 struct device_node *link;
126 int index;
127 struct dsa_switch *dst_ds;
128
129 for (index = 0;; index++) {
130 link = of_parse_phandle(port, "link", index);
131 if (!link)
132 break;
133
134 dst_ds = dsa_dst_find_port(dst, link);
135 of_node_put(link);
136
137 if (!dst_ds)
138 return 1;
139
140 src_ds->rtable[dst_ds->index] = src_port;
141 }
142
143 return 0;
144}
145
146/* A switch is complete if all the DSA ports phandles point to ports
147 * known in the tree. A return value of 1 means the tree is not
148 * complete. This is not an error condition. A value of 0 is
149 * success.
150 */
151static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
152{
153 struct device_node *port;
154 u32 index;
155 int err;
156
157 for (index = 0; index < DSA_MAX_PORTS; index++) {
158 port = ds->ports[index].dn;
159 if (!port)
160 continue;
161
162 if (!dsa_port_is_dsa(port))
163 continue;
164
165 err = dsa_port_complete(dst, ds, port, index);
166 if (err != 0)
167 return err;
168
169 ds->dsa_port_mask |= BIT(index);
170 }
171
172 return 0;
173}
174
175/* A tree is complete if all the DSA ports phandles point to ports
176 * known in the tree. A return value of 1 means the tree is not
177 * complete. This is not an error condition. A value of 0 is
178 * success.
179 */
180static int dsa_dst_complete(struct dsa_switch_tree *dst)
181{
182 struct dsa_switch *ds;
183 u32 index;
184 int err;
185
186 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
187 ds = dst->ds[index];
188 if (!ds)
189 continue;
190
191 err = dsa_ds_complete(dst, ds);
192 if (err != 0)
193 return err;
194 }
195
196 return 0;
197}
198
199static int dsa_dsa_port_apply(struct device_node *port, u32 index,
200 struct dsa_switch *ds)
201{
202 int err;
203
204 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
205 if (err) {
206 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
207 index, err);
208 return err;
209 }
210
211 return 0;
212}
213
214static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
215 struct dsa_switch *ds)
216{
217 dsa_cpu_dsa_destroy(port);
218}
219
220static int dsa_cpu_port_apply(struct device_node *port, u32 index,
221 struct dsa_switch *ds)
222{
223 int err;
224
225 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
226 if (err) {
227 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
228 index, err);
229 return err;
230 }
231
232 ds->cpu_port_mask |= BIT(index);
233
234 return 0;
235}
236
237static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
238 struct dsa_switch *ds)
239{
240 dsa_cpu_dsa_destroy(port);
241 ds->cpu_port_mask &= ~BIT(index);
242
243}
244
245static int dsa_user_port_apply(struct device_node *port, u32 index,
246 struct dsa_switch *ds)
247{
248 const char *name;
249 int err;
250
251 name = of_get_property(port, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500252 if (!name)
253 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200254
255 err = dsa_slave_create(ds, ds->dev, index, name);
256 if (err) {
257 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
258 index, err);
259 return err;
260 }
261
262 return 0;
263}
264
265static void dsa_user_port_unapply(struct device_node *port, u32 index,
266 struct dsa_switch *ds)
267{
268 if (ds->ports[index].netdev) {
269 dsa_slave_destroy(ds->ports[index].netdev);
270 ds->ports[index].netdev = NULL;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700271 ds->enabled_port_mask &= ~(1 << index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200272 }
273}
274
275static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
276{
277 struct device_node *port;
278 u32 index;
279 int err;
280
Florian Fainelli6e830d82016-06-07 16:32:39 -0700281 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400282 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700283 * the slave MDIO bus driver rely on these values for probing PHY
284 * devices or not
285 */
286 ds->phys_mii_mask = ds->enabled_port_mask;
287
Vivien Didelot9d490b42016-08-23 12:38:56 -0400288 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200289 if (err < 0)
290 return err;
291
John Crispin092183d2016-09-19 15:28:01 +0200292 if (ds->ops->set_addr) {
293 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
294 if (err < 0)
295 return err;
296 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200297
Vivien Didelot9d490b42016-08-23 12:38:56 -0400298 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700299 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
300 if (!ds->slave_mii_bus)
301 return -ENOMEM;
302
303 dsa_slave_mii_bus_init(ds);
304
305 err = mdiobus_register(ds->slave_mii_bus);
306 if (err < 0)
307 return err;
308 }
309
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200310 for (index = 0; index < DSA_MAX_PORTS; index++) {
311 port = ds->ports[index].dn;
312 if (!port)
313 continue;
314
315 if (dsa_port_is_dsa(port)) {
316 err = dsa_dsa_port_apply(port, index, ds);
317 if (err)
318 return err;
319 continue;
320 }
321
322 if (dsa_port_is_cpu(port)) {
323 err = dsa_cpu_port_apply(port, index, ds);
324 if (err)
325 return err;
326 continue;
327 }
328
329 err = dsa_user_port_apply(port, index, ds);
330 if (err)
331 continue;
332 }
333
334 return 0;
335}
336
337static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
338{
339 struct device_node *port;
340 u32 index;
341
342 for (index = 0; index < DSA_MAX_PORTS; index++) {
343 port = ds->ports[index].dn;
344 if (!port)
345 continue;
346
347 if (dsa_port_is_dsa(port)) {
348 dsa_dsa_port_unapply(port, index, ds);
349 continue;
350 }
351
352 if (dsa_port_is_cpu(port)) {
353 dsa_cpu_port_unapply(port, index, ds);
354 continue;
355 }
356
357 dsa_user_port_unapply(port, index, ds);
358 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700359
Vivien Didelot9d490b42016-08-23 12:38:56 -0400360 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700361 mdiobus_unregister(ds->slave_mii_bus);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200362}
363
364static int dsa_dst_apply(struct dsa_switch_tree *dst)
365{
366 struct dsa_switch *ds;
367 u32 index;
368 int err;
369
370 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
371 ds = dst->ds[index];
372 if (!ds)
373 continue;
374
375 err = dsa_ds_apply(dst, ds);
376 if (err)
377 return err;
378 }
379
Vivien Didelot9520ed82017-01-17 20:41:39 -0500380 if (dst->cpu_switch) {
381 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800382 if (err)
383 return err;
384 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700385
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200386 /* If we use a tagging format that doesn't have an ethertype
387 * field, make sure that all packets from this point on get
388 * sent to the tag format's receive function.
389 */
390 wmb();
391 dst->master_netdev->dsa_ptr = (void *)dst;
392 dst->applied = true;
393
394 return 0;
395}
396
397static void dsa_dst_unapply(struct dsa_switch_tree *dst)
398{
399 struct dsa_switch *ds;
400 u32 index;
401
402 if (!dst->applied)
403 return;
404
405 dst->master_netdev->dsa_ptr = NULL;
406
407 /* If we used a tagging format that doesn't have an ethertype
408 * field, make sure that all packets from this point get sent
409 * without the tag and go through the regular receive path.
410 */
411 wmb();
412
413 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
414 ds = dst->ds[index];
415 if (!ds)
416 continue;
417
418 dsa_ds_unapply(dst, ds);
419 }
420
Vivien Didelot9520ed82017-01-17 20:41:39 -0500421 if (dst->cpu_switch)
422 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700423
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200424 pr_info("DSA: tree %d unapplied\n", dst->tree);
425 dst->applied = false;
426}
427
428static int dsa_cpu_parse(struct device_node *port, u32 index,
429 struct dsa_switch_tree *dst,
430 struct dsa_switch *ds)
431{
Andrew Lunn7b314362016-08-22 16:01:01 +0200432 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200433 struct net_device *ethernet_dev;
434 struct device_node *ethernet;
435
436 ethernet = of_parse_phandle(port, "ethernet", 0);
437 if (!ethernet)
438 return -EINVAL;
439
440 ethernet_dev = of_find_net_device_by_node(ethernet);
441 if (!ethernet_dev)
442 return -EPROBE_DEFER;
443
444 if (!ds->master_netdev)
445 ds->master_netdev = ethernet_dev;
446
447 if (!dst->master_netdev)
448 dst->master_netdev = ethernet_dev;
449
Vivien Didelotb22de492017-01-17 20:41:38 -0500450 if (!dst->cpu_switch) {
451 dst->cpu_switch = ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200452 dst->cpu_port = index;
453 }
454
Vivien Didelot9d490b42016-08-23 12:38:56 -0400455 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200456 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200457 if (IS_ERR(dst->tag_ops)) {
458 dev_warn(ds->dev, "No tagger for this switch\n");
459 return PTR_ERR(dst->tag_ops);
460 }
461
462 dst->rcv = dst->tag_ops->rcv;
463
464 return 0;
465}
466
467static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
468{
469 struct device_node *port;
470 u32 index;
471 int err;
472
473 for (index = 0; index < DSA_MAX_PORTS; index++) {
474 port = ds->ports[index].dn;
475 if (!port)
476 continue;
477
478 if (dsa_port_is_cpu(port)) {
479 err = dsa_cpu_parse(port, index, dst, ds);
480 if (err)
481 return err;
482 }
483 }
484
485 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
486
487 return 0;
488}
489
490static int dsa_dst_parse(struct dsa_switch_tree *dst)
491{
492 struct dsa_switch *ds;
493 u32 index;
494 int err;
495
496 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
497 ds = dst->ds[index];
498 if (!ds)
499 continue;
500
501 err = dsa_ds_parse(dst, ds);
502 if (err)
503 return err;
504 }
505
506 if (!dst->master_netdev) {
507 pr_warn("Tree has no master device\n");
508 return -EINVAL;
509 }
510
511 pr_info("DSA: tree %d parsed\n", dst->tree);
512
513 return 0;
514}
515
516static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
517{
518 struct device_node *port;
519 int err;
520 u32 reg;
521
522 for_each_available_child_of_node(ports, port) {
523 err = of_property_read_u32(port, "reg", &reg);
524 if (err)
525 return err;
526
527 if (reg >= DSA_MAX_PORTS)
528 return -EINVAL;
529
530 ds->ports[reg].dn = port;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700531
Vivien Didelot9d490b42016-08-23 12:38:56 -0400532 /* Initialize enabled_port_mask now for ops->setup()
Florian Fainelli6e830d82016-06-07 16:32:39 -0700533 * to have access to a correct value, just like what
534 * net/dsa/dsa.c::dsa_switch_setup_one does.
535 */
536 if (!dsa_port_is_cpu(port))
537 ds->enabled_port_mask |= 1 << reg;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200538 }
539
540 return 0;
541}
542
543static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
544{
545 int err;
546
547 *tree = *index = 0;
548
549 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
550 if (err) {
551 /* Does not exist, but it is optional */
552 if (err == -EINVAL)
553 return 0;
554 return err;
555 }
556
557 err = of_property_read_u32_index(np, "dsa,member", 1, index);
558 if (err)
559 return err;
560
561 if (*index >= DSA_MAX_SWITCHES)
562 return -EINVAL;
563
564 return 0;
565}
566
567static struct device_node *dsa_get_ports(struct dsa_switch *ds,
568 struct device_node *np)
569{
570 struct device_node *ports;
571
572 ports = of_get_child_by_name(np, "ports");
573 if (!ports) {
574 dev_err(ds->dev, "no ports child node found\n");
575 return ERR_PTR(-EINVAL);
576 }
577
578 return ports;
579}
580
581static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
582{
583 struct device_node *ports = dsa_get_ports(ds, np);
584 struct dsa_switch_tree *dst;
585 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400586 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200587
588 err = dsa_parse_member(np, &tree, &index);
589 if (err)
590 return err;
591
592 if (IS_ERR(ports))
593 return PTR_ERR(ports);
594
595 err = dsa_parse_ports_dn(ports, ds);
596 if (err)
597 return err;
598
599 dst = dsa_get_dst(tree);
600 if (!dst) {
601 dst = dsa_add_dst(tree);
602 if (!dst)
603 return -ENOMEM;
604 }
605
606 if (dst->ds[index]) {
607 err = -EBUSY;
608 goto out;
609 }
610
611 ds->dst = dst;
612 ds->index = index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400613
614 /* Initialize the routing table */
615 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
616 ds->rtable[i] = DSA_RTABLE_NONE;
617
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200618 dsa_dst_add_ds(dst, ds, index);
619
620 err = dsa_dst_complete(dst);
621 if (err < 0)
622 goto out_del_dst;
623
624 if (err == 1) {
625 /* Not all switches registered yet */
626 err = 0;
627 goto out;
628 }
629
630 if (dst->applied) {
631 pr_info("DSA: Disjoint trees?\n");
632 return -EINVAL;
633 }
634
635 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100636 if (err) {
637 if (err == -EPROBE_DEFER) {
638 dsa_dst_del_ds(dst, ds, ds->index);
639 return err;
640 }
641
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200642 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100643 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200644
645 err = dsa_dst_apply(dst);
646 if (err) {
647 dsa_dst_unapply(dst);
648 goto out_del_dst;
649 }
650
651 dsa_put_dst(dst);
652 return 0;
653
654out_del_dst:
655 dsa_dst_del_ds(dst, ds, ds->index);
656out:
657 dsa_put_dst(dst);
658
659 return err;
660}
661
662int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
663{
664 int err;
665
666 mutex_lock(&dsa2_mutex);
667 err = _dsa_register_switch(ds, np);
668 mutex_unlock(&dsa2_mutex);
669
670 return err;
671}
672EXPORT_SYMBOL_GPL(dsa_register_switch);
673
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000674static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200675{
676 struct dsa_switch_tree *dst = ds->dst;
677
678 dsa_dst_unapply(dst);
679
680 dsa_dst_del_ds(dst, ds, ds->index);
681}
682
683void dsa_unregister_switch(struct dsa_switch *ds)
684{
685 mutex_lock(&dsa2_mutex);
686 _dsa_unregister_switch(ds);
687 mutex_unlock(&dsa2_mutex);
688}
689EXPORT_SYMBOL_GPL(dsa_unregister_switch);