Merge tag 'batadv-next-for-davem-20170126' of git://git.open-mesh.org/linux-merge
[linux-2.6-microblaze.git] / net / dsa / dsa2.c
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
23 static LIST_HEAD(dsa_switch_trees);
24 static DEFINE_MUTEX(dsa2_mutex);
25
26 static 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
38 static 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
47 static void dsa_put_dst(struct dsa_switch_tree *dst)
48 {
49         kref_put(&dst->refcount, dsa_free_dst);
50 }
51
52 static 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         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
67 static 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
74 static 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
81 static bool dsa_port_is_dsa(struct device_node *port)
82 {
83         return !!of_parse_phandle(port, "link", 0);
84 }
85
86 static bool dsa_port_is_cpu(struct device_node *port)
87 {
88         return !!of_parse_phandle(port, "ethernet", 0);
89 }
90
91 static 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
102 static 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
120 static 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  */
151 static 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  */
180 static 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
199 static 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
214 static 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
220 static 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
237 static 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
245 static 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);
252         if (!name)
253                 name = "eth%d";
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
265 static 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;
271                 ds->enabled_port_mask &= ~(1 << index);
272         }
273 }
274
275 static 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
281         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
282          * driver and before ops->setup() has run, since the switch drivers and
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
288         err = ds->ops->setup(ds);
289         if (err < 0)
290                 return err;
291
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         }
297
298         if (!ds->slave_mii_bus && ds->ops->phy_read) {
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
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
337 static 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         }
359
360         if (ds->slave_mii_bus && ds->ops->phy_read)
361                 mdiobus_unregister(ds->slave_mii_bus);
362 }
363
364 static 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
380         if (dst->cpu_switch) {
381                 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
382                 if (err)
383                         return err;
384         }
385
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
397 static 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
421         if (dst->cpu_switch)
422                 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
423
424         pr_info("DSA: tree %d unapplied\n", dst->tree);
425         dst->applied = false;
426 }
427
428 static int dsa_cpu_parse(struct device_node *port, u32 index,
429                          struct dsa_switch_tree *dst,
430                          struct dsa_switch *ds)
431 {
432         enum dsa_tag_protocol tag_protocol;
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
450         if (!dst->cpu_switch) {
451                 dst->cpu_switch = ds;
452                 dst->cpu_port = index;
453         }
454
455         tag_protocol = ds->ops->get_tag_protocol(ds);
456         dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
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
467 static 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
490 static 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
516 static 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;
531
532                 /* Initialize enabled_port_mask now for ops->setup()
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;
538         }
539
540         return 0;
541 }
542
543 static 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
567 static 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
581 static 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;
586         int i, err;
587
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;
613
614         /* Initialize the routing table */
615         for (i = 0; i < DSA_MAX_SWITCHES; ++i)
616                 ds->rtable[i] = DSA_RTABLE_NONE;
617
618         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);
636         if (err) {
637                 if (err == -EPROBE_DEFER) {
638                         dsa_dst_del_ds(dst, ds, ds->index);
639                         return err;
640                 }
641
642                 goto out_del_dst;
643         }
644
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
654 out_del_dst:
655         dsa_dst_del_ds(dst, ds, ds->index);
656 out:
657         dsa_put_dst(dst);
658
659         return err;
660 }
661
662 int 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 }
672 EXPORT_SYMBOL_GPL(dsa_register_switch);
673
674 static void _dsa_unregister_switch(struct dsa_switch *ds)
675 {
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
683 void dsa_unregister_switch(struct dsa_switch *ds)
684 {
685         mutex_lock(&dsa2_mutex);
686         _dsa_unregister_switch(ds);
687         mutex_unlock(&dsa2_mutex);
688 }
689 EXPORT_SYMBOL_GPL(dsa_unregister_switch);