Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[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         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
68 static 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
75 static 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
82 static bool dsa_port_is_dsa(struct device_node *port)
83 {
84         return !!of_parse_phandle(port, "link", 0);
85 }
86
87 static bool dsa_port_is_cpu(struct device_node *port)
88 {
89         return !!of_parse_phandle(port, "ethernet", 0);
90 }
91
92 static bool dsa_ds_find_port(struct dsa_switch *ds,
93                              struct device_node *port)
94 {
95         u32 index;
96
97         for (index = 0; index < DSA_MAX_PORTS; index++)
98                 if (ds->ports[index].dn == port)
99                         return true;
100         return false;
101 }
102
103 static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
104                                             struct device_node *port)
105 {
106         struct dsa_switch *ds;
107         u32 index;
108
109         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
110                 ds = dst->ds[index];
111                 if (!ds)
112                         continue;
113
114                 if (dsa_ds_find_port(ds, port))
115                         return ds;
116         }
117
118         return NULL;
119 }
120
121 static int dsa_port_complete(struct dsa_switch_tree *dst,
122                              struct dsa_switch *src_ds,
123                              struct device_node *port,
124                              u32 src_port)
125 {
126         struct device_node *link;
127         int index;
128         struct dsa_switch *dst_ds;
129
130         for (index = 0;; index++) {
131                 link = of_parse_phandle(port, "link", index);
132                 if (!link)
133                         break;
134
135                 dst_ds = dsa_dst_find_port(dst, link);
136                 of_node_put(link);
137
138                 if (!dst_ds)
139                         return 1;
140
141                 src_ds->rtable[dst_ds->index] = src_port;
142         }
143
144         return 0;
145 }
146
147 /* A switch is complete if all the DSA ports phandles point to ports
148  * known in the tree. A return value of 1 means the tree is not
149  * complete. This is not an error condition. A value of 0 is
150  * success.
151  */
152 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
153 {
154         struct device_node *port;
155         u32 index;
156         int err;
157
158         for (index = 0; index < DSA_MAX_PORTS; index++) {
159                 port = ds->ports[index].dn;
160                 if (!port)
161                         continue;
162
163                 if (!dsa_port_is_dsa(port))
164                         continue;
165
166                 err = dsa_port_complete(dst, ds, port, index);
167                 if (err != 0)
168                         return err;
169
170                 ds->dsa_port_mask |= BIT(index);
171         }
172
173         return 0;
174 }
175
176 /* A tree is complete if all the DSA ports phandles point to ports
177  * known in the tree. A return value of 1 means the tree is not
178  * complete. This is not an error condition. A value of 0 is
179  * success.
180  */
181 static int dsa_dst_complete(struct dsa_switch_tree *dst)
182 {
183         struct dsa_switch *ds;
184         u32 index;
185         int err;
186
187         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
188                 ds = dst->ds[index];
189                 if (!ds)
190                         continue;
191
192                 err = dsa_ds_complete(dst, ds);
193                 if (err != 0)
194                         return err;
195         }
196
197         return 0;
198 }
199
200 static int dsa_dsa_port_apply(struct device_node *port, u32 index,
201                               struct dsa_switch *ds)
202 {
203         int err;
204
205         err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
206         if (err) {
207                 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
208                          index, err);
209                 return err;
210         }
211
212         return 0;
213 }
214
215 static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
216                                  struct dsa_switch *ds)
217 {
218         dsa_cpu_dsa_destroy(port);
219 }
220
221 static int dsa_cpu_port_apply(struct device_node *port, u32 index,
222                               struct dsa_switch *ds)
223 {
224         int err;
225
226         err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
227         if (err) {
228                 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
229                          index, err);
230                 return err;
231         }
232
233         ds->cpu_port_mask |= BIT(index);
234
235         return 0;
236 }
237
238 static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
239                                  struct dsa_switch *ds)
240 {
241         dsa_cpu_dsa_destroy(port);
242         ds->cpu_port_mask &= ~BIT(index);
243
244 }
245
246 static int dsa_user_port_apply(struct device_node *port, u32 index,
247                                struct dsa_switch *ds)
248 {
249         const char *name;
250         int err;
251
252         name = of_get_property(port, "label", NULL);
253         if (!name)
254                 name = "eth%d";
255
256         err = dsa_slave_create(ds, ds->dev, index, name);
257         if (err) {
258                 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
259                          index, err);
260                 return err;
261         }
262
263         return 0;
264 }
265
266 static void dsa_user_port_unapply(struct device_node *port, u32 index,
267                                   struct dsa_switch *ds)
268 {
269         if (ds->ports[index].netdev) {
270                 dsa_slave_destroy(ds->ports[index].netdev);
271                 ds->ports[index].netdev = NULL;
272                 ds->enabled_port_mask &= ~(1 << index);
273         }
274 }
275
276 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
277 {
278         struct device_node *port;
279         u32 index;
280         int err;
281
282         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
283          * driver and before ops->setup() has run, since the switch drivers and
284          * the slave MDIO bus driver rely on these values for probing PHY
285          * devices or not
286          */
287         ds->phys_mii_mask = ds->enabled_port_mask;
288
289         err = ds->ops->setup(ds);
290         if (err < 0)
291                 return err;
292
293         if (ds->ops->set_addr) {
294                 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
295                 if (err < 0)
296                         return err;
297         }
298
299         if (!ds->slave_mii_bus && ds->ops->phy_read) {
300                 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
301                 if (!ds->slave_mii_bus)
302                         return -ENOMEM;
303
304                 dsa_slave_mii_bus_init(ds);
305
306                 err = mdiobus_register(ds->slave_mii_bus);
307                 if (err < 0)
308                         return err;
309         }
310
311         for (index = 0; index < DSA_MAX_PORTS; index++) {
312                 port = ds->ports[index].dn;
313                 if (!port)
314                         continue;
315
316                 if (dsa_port_is_dsa(port)) {
317                         err = dsa_dsa_port_apply(port, index, ds);
318                         if (err)
319                                 return err;
320                         continue;
321                 }
322
323                 if (dsa_port_is_cpu(port)) {
324                         err = dsa_cpu_port_apply(port, index, ds);
325                         if (err)
326                                 return err;
327                         continue;
328                 }
329
330                 err = dsa_user_port_apply(port, index, ds);
331                 if (err)
332                         continue;
333         }
334
335         return 0;
336 }
337
338 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
339 {
340         struct device_node *port;
341         u32 index;
342
343         for (index = 0; index < DSA_MAX_PORTS; index++) {
344                 port = ds->ports[index].dn;
345                 if (!port)
346                         continue;
347
348                 if (dsa_port_is_dsa(port)) {
349                         dsa_dsa_port_unapply(port, index, ds);
350                         continue;
351                 }
352
353                 if (dsa_port_is_cpu(port)) {
354                         dsa_cpu_port_unapply(port, index, ds);
355                         continue;
356                 }
357
358                 dsa_user_port_unapply(port, index, ds);
359         }
360
361         if (ds->slave_mii_bus && ds->ops->phy_read)
362                 mdiobus_unregister(ds->slave_mii_bus);
363 }
364
365 static int dsa_dst_apply(struct dsa_switch_tree *dst)
366 {
367         struct dsa_switch *ds;
368         u32 index;
369         int err;
370
371         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
372                 ds = dst->ds[index];
373                 if (!ds)
374                         continue;
375
376                 err = dsa_ds_apply(dst, ds);
377                 if (err)
378                         return err;
379         }
380
381         if (dst->ds[0]) {
382                 err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
383                 if (err)
384                         return err;
385         }
386
387         /* If we use a tagging format that doesn't have an ethertype
388          * field, make sure that all packets from this point on get
389          * sent to the tag format's receive function.
390          */
391         wmb();
392         dst->master_netdev->dsa_ptr = (void *)dst;
393         dst->applied = true;
394
395         return 0;
396 }
397
398 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
399 {
400         struct dsa_switch *ds;
401         u32 index;
402
403         if (!dst->applied)
404                 return;
405
406         dst->master_netdev->dsa_ptr = NULL;
407
408         /* If we used a tagging format that doesn't have an ethertype
409          * field, make sure that all packets from this point get sent
410          * without the tag and go through the regular receive path.
411          */
412         wmb();
413
414         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
415                 ds = dst->ds[index];
416                 if (!ds)
417                         continue;
418
419                 dsa_ds_unapply(dst, ds);
420         }
421
422         if (dst->ds[0])
423                 dsa_cpu_port_ethtool_restore(dst->ds[0]);
424
425         pr_info("DSA: tree %d unapplied\n", dst->tree);
426         dst->applied = false;
427 }
428
429 static int dsa_cpu_parse(struct device_node *port, u32 index,
430                          struct dsa_switch_tree *dst,
431                          struct dsa_switch *ds)
432 {
433         enum dsa_tag_protocol tag_protocol;
434         struct net_device *ethernet_dev;
435         struct device_node *ethernet;
436
437         ethernet = of_parse_phandle(port, "ethernet", 0);
438         if (!ethernet)
439                 return -EINVAL;
440
441         ethernet_dev = of_find_net_device_by_node(ethernet);
442         if (!ethernet_dev)
443                 return -EPROBE_DEFER;
444
445         if (!ds->master_netdev)
446                 ds->master_netdev = ethernet_dev;
447
448         if (!dst->master_netdev)
449                 dst->master_netdev = ethernet_dev;
450
451         if (dst->cpu_switch == -1) {
452                 dst->cpu_switch = ds->index;
453                 dst->cpu_port = index;
454         }
455
456         tag_protocol = ds->ops->get_tag_protocol(ds);
457         dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
458         if (IS_ERR(dst->tag_ops)) {
459                 dev_warn(ds->dev, "No tagger for this switch\n");
460                 return PTR_ERR(dst->tag_ops);
461         }
462
463         dst->rcv = dst->tag_ops->rcv;
464
465         return 0;
466 }
467
468 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
469 {
470         struct device_node *port;
471         u32 index;
472         int err;
473
474         for (index = 0; index < DSA_MAX_PORTS; index++) {
475                 port = ds->ports[index].dn;
476                 if (!port)
477                         continue;
478
479                 if (dsa_port_is_cpu(port)) {
480                         err = dsa_cpu_parse(port, index, dst, ds);
481                         if (err)
482                                 return err;
483                 }
484         }
485
486         pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
487
488         return 0;
489 }
490
491 static int dsa_dst_parse(struct dsa_switch_tree *dst)
492 {
493         struct dsa_switch *ds;
494         u32 index;
495         int err;
496
497         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
498                 ds = dst->ds[index];
499                 if (!ds)
500                         continue;
501
502                 err = dsa_ds_parse(dst, ds);
503                 if (err)
504                         return err;
505         }
506
507         if (!dst->master_netdev) {
508                 pr_warn("Tree has no master device\n");
509                 return -EINVAL;
510         }
511
512         pr_info("DSA: tree %d parsed\n", dst->tree);
513
514         return 0;
515 }
516
517 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
518 {
519         struct device_node *port;
520         int err;
521         u32 reg;
522
523         for_each_available_child_of_node(ports, port) {
524                 err = of_property_read_u32(port, "reg", &reg);
525                 if (err)
526                         return err;
527
528                 if (reg >= DSA_MAX_PORTS)
529                         return -EINVAL;
530
531                 ds->ports[reg].dn = port;
532
533                 /* Initialize enabled_port_mask now for ops->setup()
534                  * to have access to a correct value, just like what
535                  * net/dsa/dsa.c::dsa_switch_setup_one does.
536                  */
537                 if (!dsa_port_is_cpu(port))
538                         ds->enabled_port_mask |= 1 << reg;
539         }
540
541         return 0;
542 }
543
544 static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
545 {
546         int err;
547
548         *tree = *index = 0;
549
550         err = of_property_read_u32_index(np, "dsa,member", 0, tree);
551         if (err) {
552                 /* Does not exist, but it is optional */
553                 if (err == -EINVAL)
554                         return 0;
555                 return err;
556         }
557
558         err = of_property_read_u32_index(np, "dsa,member", 1, index);
559         if (err)
560                 return err;
561
562         if (*index >= DSA_MAX_SWITCHES)
563                 return -EINVAL;
564
565         return 0;
566 }
567
568 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
569                                          struct device_node *np)
570 {
571         struct device_node *ports;
572
573         ports = of_get_child_by_name(np, "ports");
574         if (!ports) {
575                 dev_err(ds->dev, "no ports child node found\n");
576                 return ERR_PTR(-EINVAL);
577         }
578
579         return ports;
580 }
581
582 static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
583 {
584         struct device_node *ports = dsa_get_ports(ds, np);
585         struct dsa_switch_tree *dst;
586         u32 tree, index;
587         int i, err;
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;
614
615         /* Initialize the routing table */
616         for (i = 0; i < DSA_MAX_SWITCHES; ++i)
617                 ds->rtable[i] = DSA_RTABLE_NONE;
618
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);
637         if (err) {
638                 if (err == -EPROBE_DEFER) {
639                         dsa_dst_del_ds(dst, ds, ds->index);
640                         return err;
641                 }
642
643                 goto out_del_dst;
644         }
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
655 out_del_dst:
656         dsa_dst_del_ds(dst, ds, ds->index);
657 out:
658         dsa_put_dst(dst);
659
660         return err;
661 }
662
663 int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
664 {
665         int err;
666
667         mutex_lock(&dsa2_mutex);
668         err = _dsa_register_switch(ds, np);
669         mutex_unlock(&dsa2_mutex);
670
671         return err;
672 }
673 EXPORT_SYMBOL_GPL(dsa_register_switch);
674
675 static void _dsa_unregister_switch(struct dsa_switch *ds)
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
684 void dsa_unregister_switch(struct dsa_switch *ds)
685 {
686         mutex_lock(&dsa2_mutex);
687         _dsa_unregister_switch(ds);
688         mutex_unlock(&dsa2_mutex);
689 }
690 EXPORT_SYMBOL_GPL(dsa_unregister_switch);