aboutsummaryrefslogtreecommitdiffstats
path: root/net/dsa
diff options
context:
space:
mode:
authorAndrew Lunn <andrew@lunn.ch>2016-06-04 15:17:07 -0400
committerDavid S. Miller <davem@davemloft.net>2016-06-04 17:29:55 -0400
commit83c0afaec7b730b16c518aecc8e6246ec91b265e (patch)
tree5d675d5517efb9532cb4154ae982a3d3ecb2798e /net/dsa
parentb516d453239551d7916d5e35bc68823ed5b55f98 (diff)
net: dsa: Add new binding implementation
The existing DSA binding has a number of limitations and problems. The main problem is that it cannot represent a switch as a linux device, hanging off some bus. It is limited to one CPU port. The DSA platform device is artificial, and does not really represent hardware. Implement a new binding which can be embedded into any type of node on a bus to represent one switch device, and its links to other switches. Signed-off-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/dsa')
-rw-r--r--net/dsa/Makefile2
-rw-r--r--net/dsa/dsa.c5
-rw-r--r--net/dsa/dsa2.c654
-rw-r--r--net/dsa/dsa_priv.h2
-rw-r--r--net/dsa/slave.c8
5 files changed, 667 insertions, 4 deletions
diff --git a/net/dsa/Makefile b/net/dsa/Makefile
index da06ed1df620..8af4ded70f1c 100644
--- a/net/dsa/Makefile
+++ b/net/dsa/Makefile
@@ -1,6 +1,6 @@
1# the core 1# the core
2obj-$(CONFIG_NET_DSA) += dsa_core.o 2obj-$(CONFIG_NET_DSA) += dsa_core.o
3dsa_core-y += dsa.o slave.o 3dsa_core-y += dsa.o slave.o dsa2.o
4 4
5# tagging formats 5# tagging formats
6dsa_core-$(CONFIG_NET_DSA_TAG_BRCM) += tag_brcm.o 6dsa_core-$(CONFIG_NET_DSA_TAG_BRCM) += tag_brcm.o
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 6c314f300424..ce3b942dce76 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -294,6 +294,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
294 } 294 }
295 dst->cpu_switch = index; 295 dst->cpu_switch = index;
296 dst->cpu_port = i; 296 dst->cpu_port = i;
297 ds->cpu_port_mask |= 1 << i;
297 } else if (!strcmp(name, "dsa")) { 298 } else if (!strcmp(name, "dsa")) {
298 ds->dsa_port_mask |= 1 << i; 299 ds->dsa_port_mask |= 1 << i;
299 } else { 300 } else {
@@ -492,6 +493,10 @@ static void dsa_switch_destroy(struct dsa_switch *ds)
492 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) 493 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
493 continue; 494 continue;
494 dsa_cpu_dsa_destroy(ds->ports[port].dn); 495 dsa_cpu_dsa_destroy(ds->ports[port].dn);
496
497 /* Clearing a bit which is not set does no harm */
498 ds->cpu_port_mask |= ~(1 << port);
499 ds->dsa_port_mask |= ~(1 << port);
495 } 500 }
496 501
497 if (ds->slave_mii_bus && ds->drv->phy_read) 502 if (ds->slave_mii_bus && ds->drv->phy_read)
diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c
new file mode 100644
index 000000000000..80dfe08db825
--- /dev/null
+++ b/net/dsa/dsa2.c
@@ -0,0 +1,654 @@
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 return dst;
33 return NULL;
34}
35
36static void dsa_free_dst(struct kref *ref)
37{
38 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
39 refcount);
40
41 list_del(&dst->list);
42 kfree(dst);
43}
44
45static void dsa_put_dst(struct dsa_switch_tree *dst)
46{
47 kref_put(&dst->refcount, dsa_free_dst);
48}
49
50static struct dsa_switch_tree *dsa_add_dst(u32 tree)
51{
52 struct dsa_switch_tree *dst;
53
54 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
55 if (!dst)
56 return NULL;
57 dst->tree = tree;
58 dst->cpu_switch = -1;
59 INIT_LIST_HEAD(&dst->list);
60 list_add_tail(&dsa_switch_trees, &dst->list);
61 kref_init(&dst->refcount);
62
63 return dst;
64}
65
66static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
67 struct dsa_switch *ds, u32 index)
68{
69 kref_get(&dst->refcount);
70 dst->ds[index] = ds;
71}
72
73static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
74 struct dsa_switch *ds, u32 index)
75{
76 dst->ds[index] = NULL;
77 kref_put(&dst->refcount, dsa_free_dst);
78}
79
80static bool dsa_port_is_dsa(struct device_node *port)
81{
82 const char *name;
83
84 name = of_get_property(port, "label", NULL);
85 if (!name)
86 return false;
87
88 if (!strcmp(name, "dsa"))
89 return true;
90
91 return false;
92}
93
94static bool dsa_port_is_cpu(struct device_node *port)
95{
96 const char *name;
97
98 name = of_get_property(port, "label", NULL);
99 if (!name)
100 return false;
101
102 if (!strcmp(name, "cpu"))
103 return true;
104
105 return false;
106}
107
108static bool dsa_ds_find_port(struct dsa_switch *ds,
109 struct device_node *port)
110{
111 u32 index;
112
113 for (index = 0; index < DSA_MAX_PORTS; index++)
114 if (ds->ports[index].dn == port)
115 return true;
116 return false;
117}
118
119static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
120 struct device_node *port)
121{
122 struct dsa_switch *ds;
123 u32 index;
124
125 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
126 ds = dst->ds[index];
127 if (!ds)
128 continue;
129
130 if (dsa_ds_find_port(ds, port))
131 return ds;
132 }
133
134 return NULL;
135}
136
137static int dsa_port_complete(struct dsa_switch_tree *dst,
138 struct dsa_switch *src_ds,
139 struct device_node *port,
140 u32 src_port)
141{
142 struct device_node *link;
143 int index;
144 struct dsa_switch *dst_ds;
145
146 for (index = 0;; index++) {
147 link = of_parse_phandle(port, "link", index);
148 if (!link)
149 break;
150
151 dst_ds = dsa_dst_find_port(dst, link);
152 of_node_put(link);
153
154 if (!dst_ds)
155 return 1;
156
157 src_ds->rtable[dst_ds->index] = src_port;
158 }
159
160 return 0;
161}
162
163/* A switch is complete if all the DSA ports phandles point to ports
164 * known in the tree. A return value of 1 means the tree is not
165 * complete. This is not an error condition. A value of 0 is
166 * success.
167 */
168static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
169{
170 struct device_node *port;
171 u32 index;
172 int err;
173
174 for (index = 0; index < DSA_MAX_PORTS; index++) {
175 port = ds->ports[index].dn;
176 if (!port)
177 continue;
178
179 if (!dsa_port_is_dsa(port))
180 continue;
181
182 err = dsa_port_complete(dst, ds, port, index);
183 if (err != 0)
184 return err;
185
186 ds->dsa_port_mask |= BIT(index);
187 }
188
189 return 0;
190}
191
192/* A tree is complete if all the DSA ports phandles point to ports
193 * known in the tree. A return value of 1 means the tree is not
194 * complete. This is not an error condition. A value of 0 is
195 * success.
196 */
197static int dsa_dst_complete(struct dsa_switch_tree *dst)
198{
199 struct dsa_switch *ds;
200 u32 index;
201 int err;
202
203 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
204 ds = dst->ds[index];
205 if (!ds)
206 continue;
207
208 err = dsa_ds_complete(dst, ds);
209 if (err != 0)
210 return err;
211 }
212
213 return 0;
214}
215
216static int dsa_dsa_port_apply(struct device_node *port, u32 index,
217 struct dsa_switch *ds)
218{
219 int err;
220
221 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
222 if (err) {
223 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
224 index, err);
225 return err;
226 }
227
228 return 0;
229}
230
231static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
232 struct dsa_switch *ds)
233{
234 dsa_cpu_dsa_destroy(port);
235}
236
237static int dsa_cpu_port_apply(struct device_node *port, u32 index,
238 struct dsa_switch *ds)
239{
240 int err;
241
242 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
243 if (err) {
244 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
245 index, err);
246 return err;
247 }
248
249 ds->cpu_port_mask |= BIT(index);
250
251 return 0;
252}
253
254static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
255 struct dsa_switch *ds)
256{
257 dsa_cpu_dsa_destroy(port);
258 ds->cpu_port_mask &= ~BIT(index);
259
260}
261
262static int dsa_user_port_apply(struct device_node *port, u32 index,
263 struct dsa_switch *ds)
264{
265 const char *name;
266 int err;
267
268 name = of_get_property(port, "label", NULL);
269
270 err = dsa_slave_create(ds, ds->dev, index, name);
271 if (err) {
272 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
273 index, err);
274 return err;
275 }
276
277 return 0;
278}
279
280static void dsa_user_port_unapply(struct device_node *port, u32 index,
281 struct dsa_switch *ds)
282{
283 if (ds->ports[index].netdev) {
284 dsa_slave_destroy(ds->ports[index].netdev);
285 ds->ports[index].netdev = NULL;
286 }
287}
288
289static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
290{
291 struct device_node *port;
292 u32 index;
293 int err;
294
295 err = ds->drv->setup(ds);
296 if (err < 0)
297 return err;
298
299 err = ds->drv->set_addr(ds, dst->master_netdev->dev_addr);
300 if (err < 0)
301 return err;
302
303 err = ds->drv->set_addr(ds, dst->master_netdev->dev_addr);
304 if (err < 0)
305 return err;
306
307 for (index = 0; index < DSA_MAX_PORTS; index++) {
308 port = ds->ports[index].dn;
309 if (!port)
310 continue;
311
312 if (dsa_port_is_dsa(port)) {
313 err = dsa_dsa_port_apply(port, index, ds);
314 if (err)
315 return err;
316 continue;
317 }
318
319 if (dsa_port_is_cpu(port)) {
320 err = dsa_cpu_port_apply(port, index, ds);
321 if (err)
322 return err;
323 continue;
324 }
325
326 err = dsa_user_port_apply(port, index, ds);
327 if (err)
328 continue;
329 }
330
331 return 0;
332}
333
334static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
335{
336 struct device_node *port;
337 u32 index;
338
339 for (index = 0; index < DSA_MAX_PORTS; index++) {
340 port = ds->ports[index].dn;
341 if (!port)
342 continue;
343
344 if (dsa_port_is_dsa(port)) {
345 dsa_dsa_port_unapply(port, index, ds);
346 continue;
347 }
348
349 if (dsa_port_is_cpu(port)) {
350 dsa_cpu_port_unapply(port, index, ds);
351 continue;
352 }
353
354 dsa_user_port_unapply(port, index, ds);
355 }
356}
357
358static int dsa_dst_apply(struct dsa_switch_tree *dst)
359{
360 struct dsa_switch *ds;
361 u32 index;
362 int err;
363
364 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
365 ds = dst->ds[index];
366 if (!ds)
367 continue;
368
369 err = dsa_ds_apply(dst, ds);
370 if (err)
371 return err;
372 }
373
374 /* If we use a tagging format that doesn't have an ethertype
375 * field, make sure that all packets from this point on get
376 * sent to the tag format's receive function.
377 */
378 wmb();
379 dst->master_netdev->dsa_ptr = (void *)dst;
380 dst->applied = true;
381
382 return 0;
383}
384
385static void dsa_dst_unapply(struct dsa_switch_tree *dst)
386{
387 struct dsa_switch *ds;
388 u32 index;
389
390 if (!dst->applied)
391 return;
392
393 dst->master_netdev->dsa_ptr = NULL;
394
395 /* If we used a tagging format that doesn't have an ethertype
396 * field, make sure that all packets from this point get sent
397 * without the tag and go through the regular receive path.
398 */
399 wmb();
400
401 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
402 ds = dst->ds[index];
403 if (!ds)
404 continue;
405
406 dsa_ds_unapply(dst, ds);
407 }
408
409 pr_info("DSA: tree %d unapplied\n", dst->tree);
410 dst->applied = false;
411}
412
413static int dsa_cpu_parse(struct device_node *port, u32 index,
414 struct dsa_switch_tree *dst,
415 struct dsa_switch *ds)
416{
417 struct net_device *ethernet_dev;
418 struct device_node *ethernet;
419
420 ethernet = of_parse_phandle(port, "ethernet", 0);
421 if (!ethernet)
422 return -EINVAL;
423
424 ethernet_dev = of_find_net_device_by_node(ethernet);
425 if (!ethernet_dev)
426 return -EPROBE_DEFER;
427
428 if (!ds->master_netdev)
429 ds->master_netdev = ethernet_dev;
430
431 if (!dst->master_netdev)
432 dst->master_netdev = ethernet_dev;
433
434 if (dst->cpu_switch == -1) {
435 dst->cpu_switch = ds->index;
436 dst->cpu_port = index;
437 }
438
439 dst->tag_ops = dsa_resolve_tag_protocol(ds->drv->tag_protocol);
440 if (IS_ERR(dst->tag_ops)) {
441 dev_warn(ds->dev, "No tagger for this switch\n");
442 return PTR_ERR(dst->tag_ops);
443 }
444
445 dst->rcv = dst->tag_ops->rcv;
446
447 return 0;
448}
449
450static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
451{
452 struct device_node *port;
453 u32 index;
454 int err;
455
456 for (index = 0; index < DSA_MAX_PORTS; index++) {
457 port = ds->ports[index].dn;
458 if (!port)
459 continue;
460
461 if (dsa_port_is_cpu(port)) {
462 err = dsa_cpu_parse(port, index, dst, ds);
463 if (err)
464 return err;
465 }
466 }
467
468 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
469
470 return 0;
471}
472
473static int dsa_dst_parse(struct dsa_switch_tree *dst)
474{
475 struct dsa_switch *ds;
476 u32 index;
477 int err;
478
479 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
480 ds = dst->ds[index];
481 if (!ds)
482 continue;
483
484 err = dsa_ds_parse(dst, ds);
485 if (err)
486 return err;
487 }
488
489 if (!dst->master_netdev) {
490 pr_warn("Tree has no master device\n");
491 return -EINVAL;
492 }
493
494 pr_info("DSA: tree %d parsed\n", dst->tree);
495
496 return 0;
497}
498
499static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
500{
501 struct device_node *port;
502 int err;
503 u32 reg;
504
505 for_each_available_child_of_node(ports, port) {
506 err = of_property_read_u32(port, "reg", &reg);
507 if (err)
508 return err;
509
510 if (reg >= DSA_MAX_PORTS)
511 return -EINVAL;
512
513 ds->ports[reg].dn = port;
514 }
515
516 return 0;
517}
518
519static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
520{
521 int err;
522
523 *tree = *index = 0;
524
525 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
526 if (err) {
527 /* Does not exist, but it is optional */
528 if (err == -EINVAL)
529 return 0;
530 return err;
531 }
532
533 err = of_property_read_u32_index(np, "dsa,member", 1, index);
534 if (err)
535 return err;
536
537 if (*index >= DSA_MAX_SWITCHES)
538 return -EINVAL;
539
540 return 0;
541}
542
543static struct device_node *dsa_get_ports(struct dsa_switch *ds,
544 struct device_node *np)
545{
546 struct device_node *ports;
547
548 ports = of_get_child_by_name(np, "ports");
549 if (!ports) {
550 dev_err(ds->dev, "no ports child node found\n");
551 return ERR_PTR(-EINVAL);
552 }
553
554 return ports;
555}
556
557static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
558{
559 struct device_node *ports = dsa_get_ports(ds, np);
560 struct dsa_switch_tree *dst;
561 u32 tree, index;
562 int err;
563
564 err = dsa_parse_member(np, &tree, &index);
565 if (err)
566 return err;
567
568 if (IS_ERR(ports))
569 return PTR_ERR(ports);
570
571 err = dsa_parse_ports_dn(ports, ds);
572 if (err)
573 return err;
574
575 dst = dsa_get_dst(tree);
576 if (!dst) {
577 dst = dsa_add_dst(tree);
578 if (!dst)
579 return -ENOMEM;
580 }
581
582 if (dst->ds[index]) {
583 err = -EBUSY;
584 goto out;
585 }
586
587 ds->dst = dst;
588 ds->index = index;
589 dsa_dst_add_ds(dst, ds, index);
590
591 err = dsa_dst_complete(dst);
592 if (err < 0)
593 goto out_del_dst;
594
595 if (err == 1) {
596 /* Not all switches registered yet */
597 err = 0;
598 goto out;
599 }
600
601 if (dst->applied) {
602 pr_info("DSA: Disjoint trees?\n");
603 return -EINVAL;
604 }
605
606 err = dsa_dst_parse(dst);
607 if (err)
608 goto out_del_dst;
609
610 err = dsa_dst_apply(dst);
611 if (err) {
612 dsa_dst_unapply(dst);
613 goto out_del_dst;
614 }
615
616 dsa_put_dst(dst);
617 return 0;
618
619out_del_dst:
620 dsa_dst_del_ds(dst, ds, ds->index);
621out:
622 dsa_put_dst(dst);
623
624 return err;
625}
626
627int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
628{
629 int err;
630
631 mutex_lock(&dsa2_mutex);
632 err = _dsa_register_switch(ds, np);
633 mutex_unlock(&dsa2_mutex);
634
635 return err;
636}
637EXPORT_SYMBOL_GPL(dsa_register_switch);
638
639void _dsa_unregister_switch(struct dsa_switch *ds)
640{
641 struct dsa_switch_tree *dst = ds->dst;
642
643 dsa_dst_unapply(dst);
644
645 dsa_dst_del_ds(dst, ds, ds->index);
646}
647
648void dsa_unregister_switch(struct dsa_switch *ds)
649{
650 mutex_lock(&dsa2_mutex);
651 _dsa_unregister_switch(ds);
652 mutex_unlock(&dsa2_mutex);
653}
654EXPORT_SYMBOL_GPL(dsa_unregister_switch);
diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
index 72f7b8989cfb..b42f1a5f95f3 100644
--- a/net/dsa/dsa_priv.h
+++ b/net/dsa/dsa_priv.h
@@ -59,7 +59,7 @@ const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol);
59extern const struct dsa_device_ops notag_netdev_ops; 59extern const struct dsa_device_ops notag_netdev_ops;
60void dsa_slave_mii_bus_init(struct dsa_switch *ds); 60void dsa_slave_mii_bus_init(struct dsa_switch *ds);
61int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 61int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
62 int port, char *name); 62 int port, const char *name);
63void dsa_slave_destroy(struct net_device *slave_dev); 63void dsa_slave_destroy(struct net_device *slave_dev);
64int dsa_slave_suspend(struct net_device *slave_dev); 64int dsa_slave_suspend(struct net_device *slave_dev);
65int dsa_slave_resume(struct net_device *slave_dev); 65int dsa_slave_resume(struct net_device *slave_dev);
diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index 35e5f0f6688b..15a492261895 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -1099,14 +1099,18 @@ int dsa_slave_resume(struct net_device *slave_dev)
1099} 1099}
1100 1100
1101int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 1101int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
1102 int port, char *name) 1102 int port, const char *name)
1103{ 1103{
1104 struct net_device *master = ds->dst->master_netdev;
1105 struct dsa_switch_tree *dst = ds->dst; 1104 struct dsa_switch_tree *dst = ds->dst;
1105 struct net_device *master;
1106 struct net_device *slave_dev; 1106 struct net_device *slave_dev;
1107 struct dsa_slave_priv *p; 1107 struct dsa_slave_priv *p;
1108 int ret; 1108 int ret;
1109 1109
1110 master = ds->dst->master_netdev;
1111 if (ds->master_netdev)
1112 master = ds->master_netdev;
1113
1110 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 1114 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1111 NET_NAME_UNKNOWN, ether_setup); 1115 NET_NAME_UNKNOWN, ether_setup);
1112 if (slave_dev == NULL) 1116 if (slave_dev == NULL)