net: dsa: Pass a port to get_tag_protocol()
[platform/kernel/linux-rpi.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/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/of.h>
20 #include <linux/of_net.h>
21
22 #include "dsa_priv.h"
23
24 static LIST_HEAD(dsa_tree_list);
25 static DEFINE_MUTEX(dsa2_mutex);
26
27 static const struct devlink_ops dsa_devlink_ops = {
28 };
29
30 static struct dsa_switch_tree *dsa_tree_find(int index)
31 {
32         struct dsa_switch_tree *dst;
33
34         list_for_each_entry(dst, &dsa_tree_list, list)
35                 if (dst->index == index)
36                         return dst;
37
38         return NULL;
39 }
40
41 static struct dsa_switch_tree *dsa_tree_alloc(int index)
42 {
43         struct dsa_switch_tree *dst;
44
45         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
46         if (!dst)
47                 return NULL;
48
49         dst->index = index;
50
51         INIT_LIST_HEAD(&dst->list);
52         list_add_tail(&dsa_tree_list, &dst->list);
53
54         /* Initialize the reference counter to the number of switches, not 1 */
55         kref_init(&dst->refcount);
56         refcount_set(&dst->refcount.refcount, 0);
57
58         return dst;
59 }
60
61 static void dsa_tree_free(struct dsa_switch_tree *dst)
62 {
63         list_del(&dst->list);
64         kfree(dst);
65 }
66
67 static struct dsa_switch_tree *dsa_tree_touch(int index)
68 {
69         struct dsa_switch_tree *dst;
70
71         dst = dsa_tree_find(index);
72         if (!dst)
73                 dst = dsa_tree_alloc(index);
74
75         return dst;
76 }
77
78 static void dsa_tree_get(struct dsa_switch_tree *dst)
79 {
80         kref_get(&dst->refcount);
81 }
82
83 static void dsa_tree_release(struct kref *ref)
84 {
85         struct dsa_switch_tree *dst;
86
87         dst = container_of(ref, struct dsa_switch_tree, refcount);
88
89         dsa_tree_free(dst);
90 }
91
92 static void dsa_tree_put(struct dsa_switch_tree *dst)
93 {
94         kref_put(&dst->refcount, dsa_tree_release);
95 }
96
97 static bool dsa_port_is_dsa(struct dsa_port *port)
98 {
99         return port->type == DSA_PORT_TYPE_DSA;
100 }
101
102 static bool dsa_port_is_cpu(struct dsa_port *port)
103 {
104         return port->type == DSA_PORT_TYPE_CPU;
105 }
106
107 static bool dsa_port_is_user(struct dsa_port *dp)
108 {
109         return dp->type == DSA_PORT_TYPE_USER;
110 }
111
112 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
113                                                    struct device_node *dn)
114 {
115         struct dsa_switch *ds;
116         struct dsa_port *dp;
117         int device, port;
118
119         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
120                 ds = dst->ds[device];
121                 if (!ds)
122                         continue;
123
124                 for (port = 0; port < ds->num_ports; port++) {
125                         dp = &ds->ports[port];
126
127                         if (dp->dn == dn)
128                                 return dp;
129                 }
130         }
131
132         return NULL;
133 }
134
135 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
136 {
137         struct dsa_switch *ds = dp->ds;
138         struct dsa_switch_tree *dst = ds->dst;
139         struct device_node *dn = dp->dn;
140         struct of_phandle_iterator it;
141         struct dsa_port *link_dp;
142         int err;
143
144         of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
145                 link_dp = dsa_tree_find_port_by_node(dst, it.node);
146                 if (!link_dp) {
147                         of_node_put(it.node);
148                         return false;
149                 }
150
151                 ds->rtable[link_dp->ds->index] = dp->index;
152         }
153
154         return true;
155 }
156
157 static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
158 {
159         bool complete = true;
160         struct dsa_port *dp;
161         int i;
162
163         for (i = 0; i < DSA_MAX_SWITCHES; i++)
164                 ds->rtable[i] = DSA_RTABLE_NONE;
165
166         for (i = 0; i < ds->num_ports; i++) {
167                 dp = &ds->ports[i];
168
169                 if (dsa_port_is_dsa(dp)) {
170                         complete = dsa_port_setup_routing_table(dp);
171                         if (!complete)
172                                 break;
173                 }
174         }
175
176         return complete;
177 }
178
179 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
180 {
181         struct dsa_switch *ds;
182         bool complete = true;
183         int device;
184
185         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
186                 ds = dst->ds[device];
187                 if (!ds)
188                         continue;
189
190                 complete = dsa_switch_setup_routing_table(ds);
191                 if (!complete)
192                         break;
193         }
194
195         return complete;
196 }
197
198 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199 {
200         struct dsa_switch *ds;
201         struct dsa_port *dp;
202         int device, port;
203
204         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
205                 ds = dst->ds[device];
206                 if (!ds)
207                         continue;
208
209                 for (port = 0; port < ds->num_ports; port++) {
210                         dp = &ds->ports[port];
211
212                         if (dsa_port_is_cpu(dp))
213                                 return dp;
214                 }
215         }
216
217         return NULL;
218 }
219
220 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
221 {
222         struct dsa_switch *ds;
223         struct dsa_port *dp;
224         int device, port;
225
226         /* DSA currently only supports a single CPU port */
227         dst->cpu_dp = dsa_tree_find_first_cpu(dst);
228         if (!dst->cpu_dp) {
229                 pr_warn("Tree has no master device\n");
230                 return -EINVAL;
231         }
232
233         /* Assign the default CPU port to all ports of the fabric */
234         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
235                 ds = dst->ds[device];
236                 if (!ds)
237                         continue;
238
239                 for (port = 0; port < ds->num_ports; port++) {
240                         dp = &ds->ports[port];
241
242                         if (dsa_port_is_user(dp))
243                                 dp->cpu_dp = dst->cpu_dp;
244                 }
245         }
246
247         return 0;
248 }
249
250 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
251 {
252         /* DSA currently only supports a single CPU port */
253         dst->cpu_dp = NULL;
254 }
255
256 static int dsa_port_setup(struct dsa_port *dp)
257 {
258         struct dsa_switch *ds = dp->ds;
259         int err;
260
261         memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
262
263         err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index);
264         if (err)
265                 return err;
266
267         switch (dp->type) {
268         case DSA_PORT_TYPE_UNUSED:
269                 break;
270         case DSA_PORT_TYPE_CPU:
271         case DSA_PORT_TYPE_DSA:
272                 err = dsa_port_fixed_link_register_of(dp);
273                 if (err) {
274                         dev_err(ds->dev, "failed to register fixed link for port %d.%d\n",
275                                 ds->index, dp->index);
276                         return err;
277                 }
278
279                 break;
280         case DSA_PORT_TYPE_USER:
281                 err = dsa_slave_create(dp);
282                 if (err)
283                         dev_err(ds->dev, "failed to create slave for port %d.%d\n",
284                                 ds->index, dp->index);
285                 else
286                         devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
287                 break;
288         }
289
290         return 0;
291 }
292
293 static void dsa_port_teardown(struct dsa_port *dp)
294 {
295         devlink_port_unregister(&dp->devlink_port);
296
297         switch (dp->type) {
298         case DSA_PORT_TYPE_UNUSED:
299                 break;
300         case DSA_PORT_TYPE_CPU:
301         case DSA_PORT_TYPE_DSA:
302                 dsa_port_fixed_link_unregister_of(dp);
303                 break;
304         case DSA_PORT_TYPE_USER:
305                 if (dp->slave) {
306                         dsa_slave_destroy(dp->slave);
307                         dp->slave = NULL;
308                 }
309                 break;
310         }
311 }
312
313 static int dsa_switch_setup(struct dsa_switch *ds)
314 {
315         int err;
316
317         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
318          * driver and before ops->setup() has run, since the switch drivers and
319          * the slave MDIO bus driver rely on these values for probing PHY
320          * devices or not
321          */
322         ds->phys_mii_mask |= dsa_user_ports(ds);
323
324         /* Add the switch to devlink before calling setup, so that setup can
325          * add dpipe tables
326          */
327         ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
328         if (!ds->devlink)
329                 return -ENOMEM;
330
331         err = devlink_register(ds->devlink, ds->dev);
332         if (err)
333                 return err;
334
335         err = ds->ops->setup(ds);
336         if (err < 0)
337                 return err;
338
339         err = dsa_switch_register_notifier(ds);
340         if (err)
341                 return err;
342
343         if (!ds->slave_mii_bus && ds->ops->phy_read) {
344                 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
345                 if (!ds->slave_mii_bus)
346                         return -ENOMEM;
347
348                 dsa_slave_mii_bus_init(ds);
349
350                 err = mdiobus_register(ds->slave_mii_bus);
351                 if (err < 0)
352                         return err;
353         }
354
355         return 0;
356 }
357
358 static void dsa_switch_teardown(struct dsa_switch *ds)
359 {
360         if (ds->slave_mii_bus && ds->ops->phy_read)
361                 mdiobus_unregister(ds->slave_mii_bus);
362
363         dsa_switch_unregister_notifier(ds);
364
365         if (ds->devlink) {
366                 devlink_unregister(ds->devlink);
367                 devlink_free(ds->devlink);
368                 ds->devlink = NULL;
369         }
370
371 }
372
373 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
374 {
375         struct dsa_switch *ds;
376         struct dsa_port *dp;
377         int device, port;
378         int err;
379
380         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
381                 ds = dst->ds[device];
382                 if (!ds)
383                         continue;
384
385                 err = dsa_switch_setup(ds);
386                 if (err)
387                         return err;
388
389                 for (port = 0; port < ds->num_ports; port++) {
390                         dp = &ds->ports[port];
391
392                         err = dsa_port_setup(dp);
393                         if (err)
394                                 return err;
395                 }
396         }
397
398         return 0;
399 }
400
401 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
402 {
403         struct dsa_switch *ds;
404         struct dsa_port *dp;
405         int device, port;
406
407         for (device = 0; device < DSA_MAX_SWITCHES; device++) {
408                 ds = dst->ds[device];
409                 if (!ds)
410                         continue;
411
412                 for (port = 0; port < ds->num_ports; port++) {
413                         dp = &ds->ports[port];
414
415                         dsa_port_teardown(dp);
416                 }
417
418                 dsa_switch_teardown(ds);
419         }
420 }
421
422 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
423 {
424         struct dsa_port *cpu_dp = dst->cpu_dp;
425         struct net_device *master = cpu_dp->master;
426
427         /* DSA currently supports a single pair of CPU port and master device */
428         return dsa_master_setup(master, cpu_dp);
429 }
430
431 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
432 {
433         struct dsa_port *cpu_dp = dst->cpu_dp;
434         struct net_device *master = cpu_dp->master;
435
436         return dsa_master_teardown(master);
437 }
438
439 static int dsa_tree_setup(struct dsa_switch_tree *dst)
440 {
441         bool complete;
442         int err;
443
444         if (dst->setup) {
445                 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
446                        dst->index);
447                 return -EEXIST;
448         }
449
450         complete = dsa_tree_setup_routing_table(dst);
451         if (!complete)
452                 return 0;
453
454         err = dsa_tree_setup_default_cpu(dst);
455         if (err)
456                 return err;
457
458         err = dsa_tree_setup_switches(dst);
459         if (err)
460                 return err;
461
462         err = dsa_tree_setup_master(dst);
463         if (err)
464                 return err;
465
466         dst->setup = true;
467
468         pr_info("DSA: tree %d setup\n", dst->index);
469
470         return 0;
471 }
472
473 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
474 {
475         if (!dst->setup)
476                 return;
477
478         dsa_tree_teardown_master(dst);
479
480         dsa_tree_teardown_switches(dst);
481
482         dsa_tree_teardown_default_cpu(dst);
483
484         pr_info("DSA: tree %d torn down\n", dst->index);
485
486         dst->setup = false;
487 }
488
489 static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
490                                    unsigned int index)
491 {
492         dsa_tree_teardown(dst);
493
494         dst->ds[index] = NULL;
495         dsa_tree_put(dst);
496 }
497
498 static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
499                                struct dsa_switch *ds)
500 {
501         unsigned int index = ds->index;
502         int err;
503
504         if (dst->ds[index])
505                 return -EBUSY;
506
507         dsa_tree_get(dst);
508         dst->ds[index] = ds;
509
510         err = dsa_tree_setup(dst);
511         if (err)
512                 dsa_tree_remove_switch(dst, index);
513
514         return err;
515 }
516
517 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
518 {
519         if (!name)
520                 name = "eth%d";
521
522         dp->type = DSA_PORT_TYPE_USER;
523         dp->name = name;
524
525         return 0;
526 }
527
528 static int dsa_port_parse_dsa(struct dsa_port *dp)
529 {
530         dp->type = DSA_PORT_TYPE_DSA;
531
532         return 0;
533 }
534
535 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
536 {
537         struct dsa_switch *ds = dp->ds;
538         struct dsa_switch_tree *dst = ds->dst;
539         const struct dsa_device_ops *tag_ops;
540         enum dsa_tag_protocol tag_protocol;
541
542         tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
543         tag_ops = dsa_resolve_tag_protocol(tag_protocol);
544         if (IS_ERR(tag_ops)) {
545                 dev_warn(ds->dev, "No tagger for this switch\n");
546                 return PTR_ERR(tag_ops);
547         }
548
549         dp->type = DSA_PORT_TYPE_CPU;
550         dp->rcv = tag_ops->rcv;
551         dp->tag_ops = tag_ops;
552         dp->master = master;
553         dp->dst = dst;
554
555         return 0;
556 }
557
558 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
559 {
560         struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
561         const char *name = of_get_property(dn, "label", NULL);
562         bool link = of_property_read_bool(dn, "link");
563
564         dp->dn = dn;
565
566         if (ethernet) {
567                 struct net_device *master;
568
569                 master = of_find_net_device_by_node(ethernet);
570                 if (!master)
571                         return -EPROBE_DEFER;
572
573                 return dsa_port_parse_cpu(dp, master);
574         }
575
576         if (link)
577                 return dsa_port_parse_dsa(dp);
578
579         return dsa_port_parse_user(dp, name);
580 }
581
582 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
583                                      struct device_node *dn)
584 {
585         struct device_node *ports, *port;
586         struct dsa_port *dp;
587         u32 reg;
588         int err;
589
590         ports = of_get_child_by_name(dn, "ports");
591         if (!ports) {
592                 dev_err(ds->dev, "no ports child node found\n");
593                 return -EINVAL;
594         }
595
596         for_each_available_child_of_node(ports, port) {
597                 err = of_property_read_u32(port, "reg", &reg);
598                 if (err)
599                         return err;
600
601                 if (reg >= ds->num_ports)
602                         return -EINVAL;
603
604                 dp = &ds->ports[reg];
605
606                 err = dsa_port_parse_of(dp, port);
607                 if (err)
608                         return err;
609         }
610
611         return 0;
612 }
613
614 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
615                                       struct device_node *dn)
616 {
617         u32 m[2] = { 0, 0 };
618         int sz;
619
620         /* Don't error out if this optional property isn't found */
621         sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
622         if (sz < 0 && sz != -EINVAL)
623                 return sz;
624
625         ds->index = m[1];
626         if (ds->index >= DSA_MAX_SWITCHES)
627                 return -EINVAL;
628
629         ds->dst = dsa_tree_touch(m[0]);
630         if (!ds->dst)
631                 return -ENOMEM;
632
633         return 0;
634 }
635
636 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
637 {
638         int err;
639
640         err = dsa_switch_parse_member_of(ds, dn);
641         if (err)
642                 return err;
643
644         return dsa_switch_parse_ports_of(ds, dn);
645 }
646
647 static int dsa_port_parse(struct dsa_port *dp, const char *name,
648                           struct device *dev)
649 {
650         if (!strcmp(name, "cpu")) {
651                 struct net_device *master;
652
653                 master = dsa_dev_to_net_device(dev);
654                 if (!master)
655                         return -EPROBE_DEFER;
656
657                 dev_put(master);
658
659                 return dsa_port_parse_cpu(dp, master);
660         }
661
662         if (!strcmp(name, "dsa"))
663                 return dsa_port_parse_dsa(dp);
664
665         return dsa_port_parse_user(dp, name);
666 }
667
668 static int dsa_switch_parse_ports(struct dsa_switch *ds,
669                                   struct dsa_chip_data *cd)
670 {
671         bool valid_name_found = false;
672         struct dsa_port *dp;
673         struct device *dev;
674         const char *name;
675         unsigned int i;
676         int err;
677
678         for (i = 0; i < DSA_MAX_PORTS; i++) {
679                 name = cd->port_names[i];
680                 dev = cd->netdev[i];
681                 dp = &ds->ports[i];
682
683                 if (!name)
684                         continue;
685
686                 err = dsa_port_parse(dp, name, dev);
687                 if (err)
688                         return err;
689
690                 valid_name_found = true;
691         }
692
693         if (!valid_name_found && i == DSA_MAX_PORTS)
694                 return -EINVAL;
695
696         return 0;
697 }
698
699 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
700 {
701         ds->cd = cd;
702
703         /* We don't support interconnected switches nor multiple trees via
704          * platform data, so this is the unique switch of the tree.
705          */
706         ds->index = 0;
707         ds->dst = dsa_tree_touch(0);
708         if (!ds->dst)
709                 return -ENOMEM;
710
711         return dsa_switch_parse_ports(ds, cd);
712 }
713
714 static int dsa_switch_add(struct dsa_switch *ds)
715 {
716         struct dsa_switch_tree *dst = ds->dst;
717
718         return dsa_tree_add_switch(dst, ds);
719 }
720
721 static int dsa_switch_probe(struct dsa_switch *ds)
722 {
723         struct dsa_chip_data *pdata = ds->dev->platform_data;
724         struct device_node *np = ds->dev->of_node;
725         int err;
726
727         if (np)
728                 err = dsa_switch_parse_of(ds, np);
729         else if (pdata)
730                 err = dsa_switch_parse(ds, pdata);
731         else
732                 err = -ENODEV;
733
734         if (err)
735                 return err;
736
737         return dsa_switch_add(ds);
738 }
739
740 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
741 {
742         size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
743         struct dsa_switch *ds;
744         int i;
745
746         ds = devm_kzalloc(dev, size, GFP_KERNEL);
747         if (!ds)
748                 return NULL;
749
750         ds->dev = dev;
751         ds->num_ports = n;
752
753         for (i = 0; i < ds->num_ports; ++i) {
754                 ds->ports[i].index = i;
755                 ds->ports[i].ds = ds;
756         }
757
758         return ds;
759 }
760 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
761
762 int dsa_register_switch(struct dsa_switch *ds)
763 {
764         int err;
765
766         mutex_lock(&dsa2_mutex);
767         err = dsa_switch_probe(ds);
768         mutex_unlock(&dsa2_mutex);
769
770         return err;
771 }
772 EXPORT_SYMBOL_GPL(dsa_register_switch);
773
774 static void dsa_switch_remove(struct dsa_switch *ds)
775 {
776         struct dsa_switch_tree *dst = ds->dst;
777         unsigned int index = ds->index;
778
779         dsa_tree_remove_switch(dst, index);
780 }
781
782 void dsa_unregister_switch(struct dsa_switch *ds)
783 {
784         mutex_lock(&dsa2_mutex);
785         dsa_switch_remove(ds);
786         mutex_unlock(&dsa2_mutex);
787 }
788 EXPORT_SYMBOL_GPL(dsa_unregister_switch);