]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/dsa/dsa2.c
net: dsa: Pass device pointer to dsa_register_switch
[mirror_ubuntu-artful-kernel.git] / net / dsa / dsa2.c
CommitLineData
83c0afae
AL
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)
7a99cd6e
NY
31 if (dst->tree == tree) {
32 kref_get(&dst->refcount);
83c0afae 33 return dst;
7a99cd6e 34 }
83c0afae
AL
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;
83c0afae
AL
60 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{
9f91484f 83 return !!of_parse_phandle(port, "link", 0);
83c0afae
AL
84}
85
86static bool dsa_port_is_cpu(struct device_node *port)
87{
9f91484f 88 return !!of_parse_phandle(port, "ethernet", 0);
83c0afae
AL
89}
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);
9f91484f
VD
252 if (!name)
253 name = "eth%d";
83c0afae
AL
254
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;
6e830d8f 271 ds->enabled_port_mask &= ~(1 << index);
83c0afae
AL
272 }
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
6e830d8f 281 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
9d490b4e 282 * driver and before ops->setup() has run, since the switch drivers and
6e830d8f
FF
283 * 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
9d490b4e 288 err = ds->ops->setup(ds);
83c0afae
AL
289 if (err < 0)
290 return err;
291
092183df
JC
292 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 }
83c0afae 297
9d490b4e 298 if (!ds->slave_mii_bus && ds->ops->phy_read) {
1eb59443
FF
299 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
83c0afae
AL
310 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 }
1eb59443 359
9d490b4e 360 if (ds->slave_mii_bus && ds->ops->phy_read)
1eb59443 361 mdiobus_unregister(ds->slave_mii_bus);
83c0afae
AL
362}
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
9520ed8f
VD
380 if (dst->cpu_switch) {
381 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
faf3a932
FF
382 if (err)
383 return err;
384 }
0c73c523 385
83c0afae
AL
386 /* 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
9520ed8f
VD
421 if (dst->cpu_switch)
422 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
0c73c523 423
83c0afae
AL
424 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{
7b314362 432 enum dsa_tag_protocol tag_protocol;
83c0afae
AL
433 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
b22de490
VD
450 if (!dst->cpu_switch) {
451 dst->cpu_switch = ds;
83c0afae
AL
452 dst->cpu_port = index;
453 }
454
9d490b4e 455 tag_protocol = ds->ops->get_tag_protocol(ds);
7b314362 456 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
83c0afae
AL
457 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;
6e830d8f 531
9d490b4e 532 /* Initialize enabled_port_mask now for ops->setup()
6e830d8f
FF
533 * 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;
83c0afae
AL
538 }
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
55ed0ce0 581static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
83c0afae 582{
55ed0ce0 583 struct device_node *np = dev->of_node;
83c0afae
AL
584 struct device_node *ports = dsa_get_ports(ds, np);
585 struct dsa_switch_tree *dst;
586 u32 tree, index;
d390238c 587 int i, err;
83c0afae
AL
588
589 err = dsa_parse_member(np, &tree, &index);
590 if (err)
591 return err;
592
593 if (IS_ERR(ports))
594 return PTR_ERR(ports);
595
596 err = dsa_parse_ports_dn(ports, ds);
597 if (err)
598 return err;
599
600 dst = dsa_get_dst(tree);
601 if (!dst) {
602 dst = dsa_add_dst(tree);
603 if (!dst)
604 return -ENOMEM;
605 }
606
607 if (dst->ds[index]) {
608 err = -EBUSY;
609 goto out;
610 }
611
612 ds->dst = dst;
613 ds->index = index;
d390238c
VD
614
615 /* Initialize the routing table */
616 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
617 ds->rtable[i] = DSA_RTABLE_NONE;
618
83c0afae
AL
619 dsa_dst_add_ds(dst, ds, index);
620
621 err = dsa_dst_complete(dst);
622 if (err < 0)
623 goto out_del_dst;
624
625 if (err == 1) {
626 /* Not all switches registered yet */
627 err = 0;
628 goto out;
629 }
630
631 if (dst->applied) {
632 pr_info("DSA: Disjoint trees?\n");
633 return -EINVAL;
634 }
635
636 err = dsa_dst_parse(dst);
5e6eb456
VB
637 if (err) {
638 if (err == -EPROBE_DEFER) {
639 dsa_dst_del_ds(dst, ds, ds->index);
640 return err;
641 }
642
83c0afae 643 goto out_del_dst;
5e6eb456 644 }
83c0afae
AL
645
646 err = dsa_dst_apply(dst);
647 if (err) {
648 dsa_dst_unapply(dst);
649 goto out_del_dst;
650 }
651
652 dsa_put_dst(dst);
653 return 0;
654
655out_del_dst:
656 dsa_dst_del_ds(dst, ds, ds->index);
657out:
658 dsa_put_dst(dst);
659
660 return err;
661}
662
55ed0ce0 663int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
83c0afae
AL
664{
665 int err;
666
667 mutex_lock(&dsa2_mutex);
55ed0ce0 668 err = _dsa_register_switch(ds, dev);
83c0afae
AL
669 mutex_unlock(&dsa2_mutex);
670
671 return err;
672}
673EXPORT_SYMBOL_GPL(dsa_register_switch);
674
85c22bad 675static void _dsa_unregister_switch(struct dsa_switch *ds)
83c0afae
AL
676{
677 struct dsa_switch_tree *dst = ds->dst;
678
679 dsa_dst_unapply(dst);
680
681 dsa_dst_del_ds(dst, ds, ds->index);
682}
683
684void dsa_unregister_switch(struct dsa_switch *ds)
685{
686 mutex_lock(&dsa2_mutex);
687 _dsa_unregister_switch(ds);
688 mutex_unlock(&dsa2_mutex);
689}
690EXPORT_SYMBOL_GPL(dsa_unregister_switch);