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