Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[firefly-linux-kernel-4.4.55.git] / net / dsa / dsa.c
1 /*
2  * net/dsa/dsa.c - Hardware switch handling
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/list.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <net/dsa.h>
17 #include <linux/of.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_platform.h>
20 #include "dsa_priv.h"
21
22 char dsa_driver_version[] = "0.1";
23
24
25 /* switch driver registration ***********************************************/
26 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
27 static LIST_HEAD(dsa_switch_drivers);
28
29 void register_switch_driver(struct dsa_switch_driver *drv)
30 {
31         mutex_lock(&dsa_switch_drivers_mutex);
32         list_add_tail(&drv->list, &dsa_switch_drivers);
33         mutex_unlock(&dsa_switch_drivers_mutex);
34 }
35 EXPORT_SYMBOL_GPL(register_switch_driver);
36
37 void unregister_switch_driver(struct dsa_switch_driver *drv)
38 {
39         mutex_lock(&dsa_switch_drivers_mutex);
40         list_del_init(&drv->list);
41         mutex_unlock(&dsa_switch_drivers_mutex);
42 }
43 EXPORT_SYMBOL_GPL(unregister_switch_driver);
44
45 static struct dsa_switch_driver *
46 dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
47 {
48         struct dsa_switch_driver *ret;
49         struct list_head *list;
50         char *name;
51
52         ret = NULL;
53         name = NULL;
54
55         mutex_lock(&dsa_switch_drivers_mutex);
56         list_for_each(list, &dsa_switch_drivers) {
57                 struct dsa_switch_driver *drv;
58
59                 drv = list_entry(list, struct dsa_switch_driver, list);
60
61                 name = drv->probe(host_dev, sw_addr);
62                 if (name != NULL) {
63                         ret = drv;
64                         break;
65                 }
66         }
67         mutex_unlock(&dsa_switch_drivers_mutex);
68
69         *_name = name;
70
71         return ret;
72 }
73
74
75 /* basic switch operations **************************************************/
76 static struct dsa_switch *
77 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
78                  struct device *parent, struct device *host_dev)
79 {
80         struct dsa_chip_data *pd = dst->pd->chip + index;
81         struct dsa_switch_driver *drv;
82         struct dsa_switch *ds;
83         int ret;
84         char *name;
85         int i;
86         bool valid_name_found = false;
87
88         /*
89          * Probe for switch model.
90          */
91         drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
92         if (drv == NULL) {
93                 printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
94                        dst->master_netdev->name, index);
95                 return ERR_PTR(-EINVAL);
96         }
97         printk(KERN_INFO "%s[%d]: detected a %s switch\n",
98                 dst->master_netdev->name, index, name);
99
100
101         /*
102          * Allocate and initialise switch state.
103          */
104         ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
105         if (ds == NULL)
106                 return ERR_PTR(-ENOMEM);
107
108         ds->dst = dst;
109         ds->index = index;
110         ds->pd = dst->pd->chip + index;
111         ds->drv = drv;
112         ds->master_dev = host_dev;
113
114         /*
115          * Validate supplied switch configuration.
116          */
117         for (i = 0; i < DSA_MAX_PORTS; i++) {
118                 char *name;
119
120                 name = pd->port_names[i];
121                 if (name == NULL)
122                         continue;
123
124                 if (!strcmp(name, "cpu")) {
125                         if (dst->cpu_switch != -1) {
126                                 printk(KERN_ERR "multiple cpu ports?!\n");
127                                 ret = -EINVAL;
128                                 goto out;
129                         }
130                         dst->cpu_switch = index;
131                         dst->cpu_port = i;
132                 } else if (!strcmp(name, "dsa")) {
133                         ds->dsa_port_mask |= 1 << i;
134                 } else {
135                         ds->phys_port_mask |= 1 << i;
136                 }
137                 valid_name_found = true;
138         }
139
140         if (!valid_name_found && i == DSA_MAX_PORTS) {
141                 ret = -EINVAL;
142                 goto out;
143         }
144
145         /* Make the built-in MII bus mask match the number of ports,
146          * switch drivers can override this later
147          */
148         ds->phys_mii_mask = ds->phys_port_mask;
149
150         /*
151          * If the CPU connects to this switch, set the switch tree
152          * tagging protocol to the preferred tagging format of this
153          * switch.
154          */
155         if (dst->cpu_switch == index) {
156                 switch (drv->tag_protocol) {
157 #ifdef CONFIG_NET_DSA_TAG_DSA
158                 case DSA_TAG_PROTO_DSA:
159                         dst->rcv = dsa_netdev_ops.rcv;
160                         break;
161 #endif
162 #ifdef CONFIG_NET_DSA_TAG_EDSA
163                 case DSA_TAG_PROTO_EDSA:
164                         dst->rcv = edsa_netdev_ops.rcv;
165                         break;
166 #endif
167 #ifdef CONFIG_NET_DSA_TAG_TRAILER
168                 case DSA_TAG_PROTO_TRAILER:
169                         dst->rcv = trailer_netdev_ops.rcv;
170                         break;
171 #endif
172 #ifdef CONFIG_NET_DSA_TAG_BRCM
173                 case DSA_TAG_PROTO_BRCM:
174                         dst->rcv = brcm_netdev_ops.rcv;
175                         break;
176 #endif
177                 default:
178                         break;
179                 }
180
181                 dst->tag_protocol = drv->tag_protocol;
182         }
183
184         /*
185          * Do basic register setup.
186          */
187         ret = drv->setup(ds);
188         if (ret < 0)
189                 goto out;
190
191         ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
192         if (ret < 0)
193                 goto out;
194
195         ds->slave_mii_bus = mdiobus_alloc();
196         if (ds->slave_mii_bus == NULL) {
197                 ret = -ENOMEM;
198                 goto out;
199         }
200         dsa_slave_mii_bus_init(ds);
201
202         ret = mdiobus_register(ds->slave_mii_bus);
203         if (ret < 0)
204                 goto out_free;
205
206
207         /*
208          * Create network devices for physical switch ports.
209          */
210         for (i = 0; i < DSA_MAX_PORTS; i++) {
211                 struct net_device *slave_dev;
212
213                 if (!(ds->phys_port_mask & (1 << i)))
214                         continue;
215
216                 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
217                 if (slave_dev == NULL) {
218                         printk(KERN_ERR "%s[%d]: can't create dsa "
219                                "slave device for port %d(%s)\n",
220                                dst->master_netdev->name,
221                                index, i, pd->port_names[i]);
222                         continue;
223                 }
224
225                 ds->ports[i] = slave_dev;
226         }
227
228         return ds;
229
230 out_free:
231         mdiobus_free(ds->slave_mii_bus);
232 out:
233         kfree(ds);
234         return ERR_PTR(ret);
235 }
236
237 static void dsa_switch_destroy(struct dsa_switch *ds)
238 {
239 }
240
241 static int dsa_switch_suspend(struct dsa_switch *ds)
242 {
243         int i, ret = 0;
244
245         /* Suspend slave network devices */
246         for (i = 0; i < DSA_MAX_PORTS; i++) {
247                 if (!(ds->phys_port_mask & (1 << i)))
248                         continue;
249
250                 ret = dsa_slave_suspend(ds->ports[i]);
251                 if (ret)
252                         return ret;
253         }
254
255         if (ds->drv->suspend)
256                 ret = ds->drv->suspend(ds);
257
258         return ret;
259 }
260
261 static int dsa_switch_resume(struct dsa_switch *ds)
262 {
263         int i, ret = 0;
264
265         if (ds->drv->resume)
266                 ret = ds->drv->resume(ds);
267
268         if (ret)
269                 return ret;
270
271         /* Resume slave network devices */
272         for (i = 0; i < DSA_MAX_PORTS; i++) {
273                 if (!(ds->phys_port_mask & (1 << i)))
274                         continue;
275
276                 ret = dsa_slave_resume(ds->ports[i]);
277                 if (ret)
278                         return ret;
279         }
280
281         return 0;
282 }
283
284
285 /* link polling *************************************************************/
286 static void dsa_link_poll_work(struct work_struct *ugly)
287 {
288         struct dsa_switch_tree *dst;
289         int i;
290
291         dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
292
293         for (i = 0; i < dst->pd->nr_chips; i++) {
294                 struct dsa_switch *ds = dst->ds[i];
295
296                 if (ds != NULL && ds->drv->poll_link != NULL)
297                         ds->drv->poll_link(ds);
298         }
299
300         mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
301 }
302
303 static void dsa_link_poll_timer(unsigned long _dst)
304 {
305         struct dsa_switch_tree *dst = (void *)_dst;
306
307         schedule_work(&dst->link_poll_work);
308 }
309
310
311 /* platform driver init and cleanup *****************************************/
312 static int dev_is_class(struct device *dev, void *class)
313 {
314         if (dev->class != NULL && !strcmp(dev->class->name, class))
315                 return 1;
316
317         return 0;
318 }
319
320 static struct device *dev_find_class(struct device *parent, char *class)
321 {
322         if (dev_is_class(parent, class)) {
323                 get_device(parent);
324                 return parent;
325         }
326
327         return device_find_child(parent, class, dev_is_class);
328 }
329
330 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
331 {
332         struct device *d;
333
334         d = dev_find_class(dev, "mdio_bus");
335         if (d != NULL) {
336                 struct mii_bus *bus;
337
338                 bus = to_mii_bus(d);
339                 put_device(d);
340
341                 return bus;
342         }
343
344         return NULL;
345 }
346 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
347
348 static struct net_device *dev_to_net_device(struct device *dev)
349 {
350         struct device *d;
351
352         d = dev_find_class(dev, "net");
353         if (d != NULL) {
354                 struct net_device *nd;
355
356                 nd = to_net_dev(d);
357                 dev_hold(nd);
358                 put_device(d);
359
360                 return nd;
361         }
362
363         return NULL;
364 }
365
366 #ifdef CONFIG_OF
367 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
368                                         struct dsa_chip_data *cd,
369                                         int chip_index,
370                                         struct device_node *link)
371 {
372         int ret;
373         const __be32 *reg;
374         int link_port_addr;
375         int link_sw_addr;
376         struct device_node *parent_sw;
377         int len;
378
379         parent_sw = of_get_parent(link);
380         if (!parent_sw)
381                 return -EINVAL;
382
383         reg = of_get_property(parent_sw, "reg", &len);
384         if (!reg || (len != sizeof(*reg) * 2))
385                 return -EINVAL;
386
387         link_sw_addr = be32_to_cpup(reg + 1);
388
389         if (link_sw_addr >= pd->nr_chips)
390                 return -EINVAL;
391
392         /* First time routing table allocation */
393         if (!cd->rtable) {
394                 cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL);
395                 if (!cd->rtable)
396                         return -ENOMEM;
397
398                 /* default to no valid uplink/downlink */
399                 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
400         }
401
402         reg = of_get_property(link, "reg", NULL);
403         if (!reg) {
404                 ret = -EINVAL;
405                 goto out;
406         }
407
408         link_port_addr = be32_to_cpup(reg);
409
410         cd->rtable[link_sw_addr] = link_port_addr;
411
412         return 0;
413 out:
414         kfree(cd->rtable);
415         return ret;
416 }
417
418 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
419 {
420         int i;
421         int port_index;
422
423         for (i = 0; i < pd->nr_chips; i++) {
424                 port_index = 0;
425                 while (port_index < DSA_MAX_PORTS) {
426                         kfree(pd->chip[i].port_names[port_index]);
427                         port_index++;
428                 }
429                 kfree(pd->chip[i].rtable);
430         }
431         kfree(pd->chip);
432 }
433
434 static int dsa_of_probe(struct platform_device *pdev)
435 {
436         struct device_node *np = pdev->dev.of_node;
437         struct device_node *child, *mdio, *ethernet, *port, *link;
438         struct mii_bus *mdio_bus;
439         struct platform_device *ethernet_dev;
440         struct dsa_platform_data *pd;
441         struct dsa_chip_data *cd;
442         const char *port_name;
443         int chip_index, port_index;
444         const unsigned int *sw_addr, *port_reg;
445         int ret;
446
447         mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
448         if (!mdio)
449                 return -EINVAL;
450
451         mdio_bus = of_mdio_find_bus(mdio);
452         if (!mdio_bus)
453                 return -EINVAL;
454
455         ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
456         if (!ethernet)
457                 return -EINVAL;
458
459         ethernet_dev = of_find_device_by_node(ethernet);
460         if (!ethernet_dev)
461                 return -ENODEV;
462
463         pd = kzalloc(sizeof(*pd), GFP_KERNEL);
464         if (!pd)
465                 return -ENOMEM;
466
467         pdev->dev.platform_data = pd;
468         pd->netdev = &ethernet_dev->dev;
469         pd->nr_chips = of_get_child_count(np);
470         if (pd->nr_chips > DSA_MAX_SWITCHES)
471                 pd->nr_chips = DSA_MAX_SWITCHES;
472
473         pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data),
474                         GFP_KERNEL);
475         if (!pd->chip) {
476                 ret = -ENOMEM;
477                 goto out_free;
478         }
479
480         chip_index = -1;
481         for_each_available_child_of_node(np, child) {
482                 chip_index++;
483                 cd = &pd->chip[chip_index];
484
485                 cd->of_node = child;
486                 cd->host_dev = &mdio_bus->dev;
487
488                 sw_addr = of_get_property(child, "reg", NULL);
489                 if (!sw_addr)
490                         continue;
491
492                 cd->sw_addr = be32_to_cpup(sw_addr);
493                 if (cd->sw_addr > PHY_MAX_ADDR)
494                         continue;
495
496                 for_each_available_child_of_node(child, port) {
497                         port_reg = of_get_property(port, "reg", NULL);
498                         if (!port_reg)
499                                 continue;
500
501                         port_index = be32_to_cpup(port_reg);
502
503                         port_name = of_get_property(port, "label", NULL);
504                         if (!port_name)
505                                 continue;
506
507                         cd->port_dn[port_index] = port;
508
509                         cd->port_names[port_index] = kstrdup(port_name,
510                                         GFP_KERNEL);
511                         if (!cd->port_names[port_index]) {
512                                 ret = -ENOMEM;
513                                 goto out_free_chip;
514                         }
515
516                         link = of_parse_phandle(port, "link", 0);
517
518                         if (!strcmp(port_name, "dsa") && link &&
519                                         pd->nr_chips > 1) {
520                                 ret = dsa_of_setup_routing_table(pd, cd,
521                                                 chip_index, link);
522                                 if (ret)
523                                         goto out_free_chip;
524                         }
525
526                         if (port_index == DSA_MAX_PORTS)
527                                 break;
528                 }
529         }
530
531         return 0;
532
533 out_free_chip:
534         dsa_of_free_platform_data(pd);
535 out_free:
536         kfree(pd);
537         pdev->dev.platform_data = NULL;
538         return ret;
539 }
540
541 static void dsa_of_remove(struct platform_device *pdev)
542 {
543         struct dsa_platform_data *pd = pdev->dev.platform_data;
544
545         if (!pdev->dev.of_node)
546                 return;
547
548         dsa_of_free_platform_data(pd);
549         kfree(pd);
550 }
551 #else
552 static inline int dsa_of_probe(struct platform_device *pdev)
553 {
554         return 0;
555 }
556
557 static inline void dsa_of_remove(struct platform_device *pdev)
558 {
559 }
560 #endif
561
562 static int dsa_probe(struct platform_device *pdev)
563 {
564         static int dsa_version_printed;
565         struct dsa_platform_data *pd = pdev->dev.platform_data;
566         struct net_device *dev;
567         struct dsa_switch_tree *dst;
568         int i, ret;
569
570         if (!dsa_version_printed++)
571                 printk(KERN_NOTICE "Distributed Switch Architecture "
572                         "driver version %s\n", dsa_driver_version);
573
574         if (pdev->dev.of_node) {
575                 ret = dsa_of_probe(pdev);
576                 if (ret)
577                         return ret;
578
579                 pd = pdev->dev.platform_data;
580         }
581
582         if (pd == NULL || pd->netdev == NULL)
583                 return -EINVAL;
584
585         dev = dev_to_net_device(pd->netdev);
586         if (dev == NULL) {
587                 ret = -EINVAL;
588                 goto out;
589         }
590
591         if (dev->dsa_ptr != NULL) {
592                 dev_put(dev);
593                 ret = -EEXIST;
594                 goto out;
595         }
596
597         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
598         if (dst == NULL) {
599                 dev_put(dev);
600                 ret = -ENOMEM;
601                 goto out;
602         }
603
604         platform_set_drvdata(pdev, dst);
605
606         dst->pd = pd;
607         dst->master_netdev = dev;
608         dst->cpu_switch = -1;
609         dst->cpu_port = -1;
610
611         for (i = 0; i < pd->nr_chips; i++) {
612                 struct dsa_switch *ds;
613
614                 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
615                 if (IS_ERR(ds)) {
616                         printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
617                                 "instance (error %ld)\n", dev->name, i,
618                                 PTR_ERR(ds));
619                         continue;
620                 }
621
622                 dst->ds[i] = ds;
623                 if (ds->drv->poll_link != NULL)
624                         dst->link_poll_needed = 1;
625         }
626
627         /*
628          * If we use a tagging format that doesn't have an ethertype
629          * field, make sure that all packets from this point on get
630          * sent to the tag format's receive function.
631          */
632         wmb();
633         dev->dsa_ptr = (void *)dst;
634
635         if (dst->link_poll_needed) {
636                 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
637                 init_timer(&dst->link_poll_timer);
638                 dst->link_poll_timer.data = (unsigned long)dst;
639                 dst->link_poll_timer.function = dsa_link_poll_timer;
640                 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
641                 add_timer(&dst->link_poll_timer);
642         }
643
644         return 0;
645
646 out:
647         dsa_of_remove(pdev);
648
649         return ret;
650 }
651
652 static int dsa_remove(struct platform_device *pdev)
653 {
654         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
655         int i;
656
657         if (dst->link_poll_needed)
658                 del_timer_sync(&dst->link_poll_timer);
659
660         flush_work(&dst->link_poll_work);
661
662         for (i = 0; i < dst->pd->nr_chips; i++) {
663                 struct dsa_switch *ds = dst->ds[i];
664
665                 if (ds != NULL)
666                         dsa_switch_destroy(ds);
667         }
668
669         dsa_of_remove(pdev);
670
671         return 0;
672 }
673
674 static void dsa_shutdown(struct platform_device *pdev)
675 {
676 }
677
678 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
679                           struct packet_type *pt, struct net_device *orig_dev)
680 {
681         struct dsa_switch_tree *dst = dev->dsa_ptr;
682
683         if (unlikely(dst == NULL)) {
684                 kfree_skb(skb);
685                 return 0;
686         }
687
688         return dst->rcv(skb, dev, pt, orig_dev);
689 }
690
691 static struct packet_type dsa_pack_type __read_mostly = {
692         .type   = cpu_to_be16(ETH_P_XDSA),
693         .func   = dsa_switch_rcv,
694 };
695
696 #ifdef CONFIG_PM_SLEEP
697 static int dsa_suspend(struct device *d)
698 {
699         struct platform_device *pdev = to_platform_device(d);
700         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
701         int i, ret = 0;
702
703         for (i = 0; i < dst->pd->nr_chips; i++) {
704                 struct dsa_switch *ds = dst->ds[i];
705
706                 if (ds != NULL)
707                         ret = dsa_switch_suspend(ds);
708         }
709
710         return ret;
711 }
712
713 static int dsa_resume(struct device *d)
714 {
715         struct platform_device *pdev = to_platform_device(d);
716         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
717         int i, ret = 0;
718
719         for (i = 0; i < dst->pd->nr_chips; i++) {
720                 struct dsa_switch *ds = dst->ds[i];
721
722                 if (ds != NULL)
723                         ret = dsa_switch_resume(ds);
724         }
725
726         return ret;
727 }
728 #endif
729
730 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
731
732 static const struct of_device_id dsa_of_match_table[] = {
733         { .compatible = "brcm,bcm7445-switch-v4.0" },
734         { .compatible = "marvell,dsa", },
735         {}
736 };
737 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
738
739 static struct platform_driver dsa_driver = {
740         .probe          = dsa_probe,
741         .remove         = dsa_remove,
742         .shutdown       = dsa_shutdown,
743         .driver = {
744                 .name   = "dsa",
745                 .owner  = THIS_MODULE,
746                 .of_match_table = dsa_of_match_table,
747                 .pm     = &dsa_pm_ops,
748         },
749 };
750
751 static int __init dsa_init_module(void)
752 {
753         int rc;
754
755         rc = platform_driver_register(&dsa_driver);
756         if (rc)
757                 return rc;
758
759         dev_add_pack(&dsa_pack_type);
760
761         return 0;
762 }
763 module_init(dsa_init_module);
764
765 static void __exit dsa_cleanup_module(void)
766 {
767         dev_remove_pack(&dsa_pack_type);
768         platform_driver_unregister(&dsa_driver);
769 }
770 module_exit(dsa_cleanup_module);
771
772 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
773 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
774 MODULE_LICENSE("GPL");
775 MODULE_ALIAS("platform:dsa");