blob: ad345c8b0b0693cc214b212ccbb402859d910134 [file] [log] [blame]
Vivien Didelota6a71f12017-04-12 12:45:03 -04001/*
2 * net/dsa/legacy.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/device.h>
13#include <linux/list.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
20#include <linux/of_net.h>
21#include <linux/netdevice.h>
22#include <linux/sysfs.h>
23#include <linux/phy_fixed.h>
24#include <linux/etherdevice.h>
25#include <net/dsa.h>
26#include "dsa_priv.h"
27
28/* switch driver registration ***********************************************/
29static DEFINE_MUTEX(dsa_switch_drivers_mutex);
30static LIST_HEAD(dsa_switch_drivers);
31
32void register_switch_driver(struct dsa_switch_driver *drv)
33{
34 mutex_lock(&dsa_switch_drivers_mutex);
35 list_add_tail(&drv->list, &dsa_switch_drivers);
36 mutex_unlock(&dsa_switch_drivers_mutex);
37}
38EXPORT_SYMBOL_GPL(register_switch_driver);
39
40void unregister_switch_driver(struct dsa_switch_driver *drv)
41{
42 mutex_lock(&dsa_switch_drivers_mutex);
43 list_del_init(&drv->list);
44 mutex_unlock(&dsa_switch_drivers_mutex);
45}
46EXPORT_SYMBOL_GPL(unregister_switch_driver);
47
48static const struct dsa_switch_ops *
49dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
50 const char **_name, void **priv)
51{
52 const struct dsa_switch_ops *ret;
53 struct list_head *list;
54 const char *name;
55
56 ret = NULL;
57 name = NULL;
58
59 mutex_lock(&dsa_switch_drivers_mutex);
60 list_for_each(list, &dsa_switch_drivers) {
61 const struct dsa_switch_ops *ops;
62 struct dsa_switch_driver *drv;
63
64 drv = list_entry(list, struct dsa_switch_driver, list);
65 ops = drv->ops;
66
67 name = ops->probe(parent, host_dev, sw_addr, priv);
68 if (name != NULL) {
69 ret = ops;
70 break;
71 }
72 }
73 mutex_unlock(&dsa_switch_drivers_mutex);
74
75 *_name = name;
76
77 return ret;
78}
79
80/* basic switch operations **************************************************/
81static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
82{
83 struct dsa_port *dport;
84 int ret, port;
85
86 for (port = 0; port < ds->num_ports; port++) {
87 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
88 continue;
89
90 dport = &ds->ports[port];
91 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
92 if (ret)
93 return ret;
94 }
95 return 0;
96}
97
98static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
99{
100 const struct dsa_switch_ops *ops = ds->ops;
101 struct dsa_switch_tree *dst = ds->dst;
102 struct dsa_chip_data *cd = ds->cd;
103 bool valid_name_found = false;
104 int index = ds->index;
105 int i, ret;
106
107 /*
108 * Validate supplied switch configuration.
109 */
110 for (i = 0; i < ds->num_ports; i++) {
111 char *name;
112
113 name = cd->port_names[i];
114 if (name == NULL)
115 continue;
116
117 if (!strcmp(name, "cpu")) {
118 if (dst->cpu_switch) {
119 netdev_err(dst->master_netdev,
120 "multiple cpu ports?!\n");
121 return -EINVAL;
122 }
123 dst->cpu_switch = ds;
124 dst->cpu_port = i;
125 ds->cpu_port_mask |= 1 << i;
126 } else if (!strcmp(name, "dsa")) {
127 ds->dsa_port_mask |= 1 << i;
128 } else {
129 ds->enabled_port_mask |= 1 << i;
130 }
131 valid_name_found = true;
132 }
133
134 if (!valid_name_found && i == ds->num_ports)
135 return -EINVAL;
136
137 /* Make the built-in MII bus mask match the number of ports,
138 * switch drivers can override this later
139 */
140 ds->phys_mii_mask = ds->enabled_port_mask;
141
142 /*
143 * If the CPU connects to this switch, set the switch tree
144 * tagging protocol to the preferred tagging format of this
145 * switch.
146 */
147 if (dst->cpu_switch == ds) {
148 enum dsa_tag_protocol tag_protocol;
149
150 tag_protocol = ops->get_tag_protocol(ds);
151 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
152 if (IS_ERR(dst->tag_ops))
153 return PTR_ERR(dst->tag_ops);
154
155 dst->rcv = dst->tag_ops->rcv;
156 }
157
158 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
159
160 /*
161 * Do basic register setup.
162 */
163 ret = ops->setup(ds);
164 if (ret < 0)
165 return ret;
166
167 ret = dsa_switch_register_notifier(ds);
168 if (ret)
169 return ret;
170
171 if (ops->set_addr) {
172 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
173 if (ret < 0)
174 return ret;
175 }
176
177 if (!ds->slave_mii_bus && ops->phy_read) {
178 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
179 if (!ds->slave_mii_bus)
180 return -ENOMEM;
181 dsa_slave_mii_bus_init(ds);
182
183 ret = mdiobus_register(ds->slave_mii_bus);
184 if (ret < 0)
185 return ret;
186 }
187
188 /*
189 * Create network devices for physical switch ports.
190 */
191 for (i = 0; i < ds->num_ports; i++) {
192 ds->ports[i].dn = cd->port_dn[i];
193
194 if (!(ds->enabled_port_mask & (1 << i)))
195 continue;
196
197 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
198 if (ret < 0)
199 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
200 index, i, cd->port_names[i], ret);
201 }
202
203 /* Perform configuration of the CPU and DSA ports */
204 ret = dsa_cpu_dsa_setups(ds, parent);
205 if (ret < 0)
206 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
207 index);
208
209 ret = dsa_cpu_port_ethtool_setup(ds);
210 if (ret)
211 return ret;
212
213 return 0;
214}
215
216static struct dsa_switch *
217dsa_switch_setup(struct dsa_switch_tree *dst, int index,
218 struct device *parent, struct device *host_dev)
219{
220 struct dsa_chip_data *cd = dst->pd->chip + index;
221 const struct dsa_switch_ops *ops;
222 struct dsa_switch *ds;
223 int ret;
224 const char *name;
225 void *priv;
226
227 /*
228 * Probe for switch model.
229 */
230 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
231 if (!ops) {
232 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
233 index);
234 return ERR_PTR(-EINVAL);
235 }
236 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
237 index, name);
238
239
240 /*
241 * Allocate and initialise switch state.
242 */
243 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
244 if (!ds)
245 return ERR_PTR(-ENOMEM);
246
247 ds->dst = dst;
248 ds->index = index;
249 ds->cd = cd;
250 ds->ops = ops;
251 ds->priv = priv;
252
253 ret = dsa_switch_setup_one(ds, parent);
254 if (ret)
255 return ERR_PTR(ret);
256
257 return ds;
258}
259
260static void dsa_switch_destroy(struct dsa_switch *ds)
261{
262 int port;
263
264 /* Destroy network devices for physical switch ports. */
265 for (port = 0; port < ds->num_ports; port++) {
266 if (!(ds->enabled_port_mask & (1 << port)))
267 continue;
268
269 if (!ds->ports[port].netdev)
270 continue;
271
272 dsa_slave_destroy(ds->ports[port].netdev);
273 }
274
275 /* Disable configuration of the CPU and DSA ports */
276 for (port = 0; port < ds->num_ports; port++) {
277 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
278 continue;
279 dsa_cpu_dsa_destroy(&ds->ports[port]);
280
281 /* Clearing a bit which is not set does no harm */
282 ds->cpu_port_mask |= ~(1 << port);
283 ds->dsa_port_mask |= ~(1 << port);
284 }
285
286 if (ds->slave_mii_bus && ds->ops->phy_read)
287 mdiobus_unregister(ds->slave_mii_bus);
288
289 dsa_switch_unregister_notifier(ds);
290}
291
292#ifdef CONFIG_PM_SLEEP
293int dsa_switch_suspend(struct dsa_switch *ds)
294{
295 int i, ret = 0;
296
297 /* Suspend slave network devices */
298 for (i = 0; i < ds->num_ports; i++) {
299 if (!dsa_is_port_initialized(ds, i))
300 continue;
301
302 ret = dsa_slave_suspend(ds->ports[i].netdev);
303 if (ret)
304 return ret;
305 }
306
307 if (ds->ops->suspend)
308 ret = ds->ops->suspend(ds);
309
310 return ret;
311}
312EXPORT_SYMBOL_GPL(dsa_switch_suspend);
313
314int dsa_switch_resume(struct dsa_switch *ds)
315{
316 int i, ret = 0;
317
318 if (ds->ops->resume)
319 ret = ds->ops->resume(ds);
320
321 if (ret)
322 return ret;
323
324 /* Resume slave network devices */
325 for (i = 0; i < ds->num_ports; i++) {
326 if (!dsa_is_port_initialized(ds, i))
327 continue;
328
329 ret = dsa_slave_resume(ds->ports[i].netdev);
330 if (ret)
331 return ret;
332 }
333
334 return 0;
335}
336EXPORT_SYMBOL_GPL(dsa_switch_resume);
337#endif
338
339/* platform driver init and cleanup *****************************************/
340static int dev_is_class(struct device *dev, void *class)
341{
342 if (dev->class != NULL && !strcmp(dev->class->name, class))
343 return 1;
344
345 return 0;
346}
347
348static struct device *dev_find_class(struct device *parent, char *class)
349{
350 if (dev_is_class(parent, class)) {
351 get_device(parent);
352 return parent;
353 }
354
355 return device_find_child(parent, class, dev_is_class);
356}
357
358struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
359{
360 struct device *d;
361
362 d = dev_find_class(dev, "mdio_bus");
363 if (d != NULL) {
364 struct mii_bus *bus;
365
366 bus = to_mii_bus(d);
367 put_device(d);
368
369 return bus;
370 }
371
372 return NULL;
373}
374EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
375
376#ifdef CONFIG_OF
377static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
378 struct dsa_chip_data *cd,
379 int chip_index, int port_index,
380 struct device_node *link)
381{
382 const __be32 *reg;
383 int link_sw_addr;
384 struct device_node *parent_sw;
385 int len;
386
387 parent_sw = of_get_parent(link);
388 if (!parent_sw)
389 return -EINVAL;
390
391 reg = of_get_property(parent_sw, "reg", &len);
392 if (!reg || (len != sizeof(*reg) * 2))
393 return -EINVAL;
394
395 /*
396 * Get the destination switch number from the second field of its 'reg'
397 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
398 */
399 link_sw_addr = be32_to_cpup(reg + 1);
400
401 if (link_sw_addr >= pd->nr_chips)
402 return -EINVAL;
403
404 cd->rtable[link_sw_addr] = port_index;
405
406 return 0;
407}
408
409static int dsa_of_probe_links(struct dsa_platform_data *pd,
410 struct dsa_chip_data *cd,
411 int chip_index, int port_index,
412 struct device_node *port,
413 const char *port_name)
414{
415 struct device_node *link;
416 int link_index;
417 int ret;
418
419 for (link_index = 0;; link_index++) {
420 link = of_parse_phandle(port, "link", link_index);
421 if (!link)
422 break;
423
424 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
425 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
426 port_index, link);
427 if (ret)
428 return ret;
429 }
430 }
431 return 0;
432}
433
434static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
435{
436 int i;
437 int port_index;
438
439 for (i = 0; i < pd->nr_chips; i++) {
440 port_index = 0;
441 while (port_index < DSA_MAX_PORTS) {
442 kfree(pd->chip[i].port_names[port_index]);
443 port_index++;
444 }
445
446 /* Drop our reference to the MDIO bus device */
447 if (pd->chip[i].host_dev)
448 put_device(pd->chip[i].host_dev);
449 }
450 kfree(pd->chip);
451}
452
453static int dsa_of_probe(struct device *dev)
454{
455 struct device_node *np = dev->of_node;
456 struct device_node *child, *mdio, *ethernet, *port;
457 struct mii_bus *mdio_bus, *mdio_bus_switch;
458 struct net_device *ethernet_dev;
459 struct dsa_platform_data *pd;
460 struct dsa_chip_data *cd;
461 const char *port_name;
462 int chip_index, port_index;
463 const unsigned int *sw_addr, *port_reg;
464 u32 eeprom_len;
465 int ret;
466
467 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
468 if (!mdio)
469 return -EINVAL;
470
471 mdio_bus = of_mdio_find_bus(mdio);
472 if (!mdio_bus)
473 return -EPROBE_DEFER;
474
475 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
476 if (!ethernet) {
477 ret = -EINVAL;
478 goto out_put_mdio;
479 }
480
481 ethernet_dev = of_find_net_device_by_node(ethernet);
482 if (!ethernet_dev) {
483 ret = -EPROBE_DEFER;
484 goto out_put_mdio;
485 }
486
487 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
488 if (!pd) {
489 ret = -ENOMEM;
490 goto out_put_ethernet;
491 }
492
493 dev->platform_data = pd;
494 pd->of_netdev = ethernet_dev;
495 pd->nr_chips = of_get_available_child_count(np);
496 if (pd->nr_chips > DSA_MAX_SWITCHES)
497 pd->nr_chips = DSA_MAX_SWITCHES;
498
499 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
500 GFP_KERNEL);
501 if (!pd->chip) {
502 ret = -ENOMEM;
503 goto out_free;
504 }
505
506 chip_index = -1;
507 for_each_available_child_of_node(np, child) {
508 int i;
509
510 chip_index++;
511 cd = &pd->chip[chip_index];
512
513 cd->of_node = child;
514
515 /* Initialize the routing table */
516 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
517 cd->rtable[i] = DSA_RTABLE_NONE;
518
519 /* When assigning the host device, increment its refcount */
520 cd->host_dev = get_device(&mdio_bus->dev);
521
522 sw_addr = of_get_property(child, "reg", NULL);
523 if (!sw_addr)
524 continue;
525
526 cd->sw_addr = be32_to_cpup(sw_addr);
527 if (cd->sw_addr >= PHY_MAX_ADDR)
528 continue;
529
530 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
531 cd->eeprom_len = eeprom_len;
532
533 mdio = of_parse_phandle(child, "mii-bus", 0);
534 if (mdio) {
535 mdio_bus_switch = of_mdio_find_bus(mdio);
536 if (!mdio_bus_switch) {
537 ret = -EPROBE_DEFER;
538 goto out_free_chip;
539 }
540
541 /* Drop the mdio_bus device ref, replacing the host
542 * device with the mdio_bus_switch device, keeping
543 * the refcount from of_mdio_find_bus() above.
544 */
545 put_device(cd->host_dev);
546 cd->host_dev = &mdio_bus_switch->dev;
547 }
548
549 for_each_available_child_of_node(child, port) {
550 port_reg = of_get_property(port, "reg", NULL);
551 if (!port_reg)
552 continue;
553
554 port_index = be32_to_cpup(port_reg);
555 if (port_index >= DSA_MAX_PORTS)
556 break;
557
558 port_name = of_get_property(port, "label", NULL);
559 if (!port_name)
560 continue;
561
562 cd->port_dn[port_index] = port;
563
564 cd->port_names[port_index] = kstrdup(port_name,
565 GFP_KERNEL);
566 if (!cd->port_names[port_index]) {
567 ret = -ENOMEM;
568 goto out_free_chip;
569 }
570
571 ret = dsa_of_probe_links(pd, cd, chip_index,
572 port_index, port, port_name);
573 if (ret)
574 goto out_free_chip;
575
576 }
577 }
578
579 /* The individual chips hold their own refcount on the mdio bus,
580 * so drop ours */
581 put_device(&mdio_bus->dev);
582
583 return 0;
584
585out_free_chip:
586 dsa_of_free_platform_data(pd);
587out_free:
588 kfree(pd);
589 dev->platform_data = NULL;
590out_put_ethernet:
591 put_device(&ethernet_dev->dev);
592out_put_mdio:
593 put_device(&mdio_bus->dev);
594 return ret;
595}
596
597static void dsa_of_remove(struct device *dev)
598{
599 struct dsa_platform_data *pd = dev->platform_data;
600
601 if (!dev->of_node)
602 return;
603
604 dsa_of_free_platform_data(pd);
605 put_device(&pd->of_netdev->dev);
606 kfree(pd);
607}
608#else
609static inline int dsa_of_probe(struct device *dev)
610{
611 return 0;
612}
613
614static inline void dsa_of_remove(struct device *dev)
615{
616}
617#endif
618
619static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
620 struct device *parent, struct dsa_platform_data *pd)
621{
622 int i;
623 unsigned configured = 0;
624
625 dst->pd = pd;
626 dst->master_netdev = dev;
627 dst->cpu_port = -1;
628
629 for (i = 0; i < pd->nr_chips; i++) {
630 struct dsa_switch *ds;
631
632 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
633 if (IS_ERR(ds)) {
634 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
635 i, PTR_ERR(ds));
636 continue;
637 }
638
639 dst->ds[i] = ds;
640
641 ++configured;
642 }
643
644 /*
645 * If no switch was found, exit cleanly
646 */
647 if (!configured)
648 return -EPROBE_DEFER;
649
650 /*
651 * If we use a tagging format that doesn't have an ethertype
652 * field, make sure that all packets from this point on get
653 * sent to the tag format's receive function.
654 */
655 wmb();
656 dev->dsa_ptr = (void *)dst;
657
658 return 0;
659}
660
661static int dsa_probe(struct platform_device *pdev)
662{
663 struct dsa_platform_data *pd = pdev->dev.platform_data;
664 struct net_device *dev;
665 struct dsa_switch_tree *dst;
666 int ret;
667
668 if (pdev->dev.of_node) {
669 ret = dsa_of_probe(&pdev->dev);
670 if (ret)
671 return ret;
672
673 pd = pdev->dev.platform_data;
674 }
675
676 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
677 return -EINVAL;
678
679 if (pd->of_netdev) {
680 dev = pd->of_netdev;
681 dev_hold(dev);
682 } else {
683 dev = dsa_dev_to_net_device(pd->netdev);
684 }
685 if (dev == NULL) {
686 ret = -EPROBE_DEFER;
687 goto out;
688 }
689
690 if (dev->dsa_ptr != NULL) {
691 dev_put(dev);
692 ret = -EEXIST;
693 goto out;
694 }
695
696 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
697 if (dst == NULL) {
698 dev_put(dev);
699 ret = -ENOMEM;
700 goto out;
701 }
702
703 platform_set_drvdata(pdev, dst);
704
705 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
706 if (ret) {
707 dev_put(dev);
708 goto out;
709 }
710
711 return 0;
712
713out:
714 dsa_of_remove(&pdev->dev);
715
716 return ret;
717}
718
719static void dsa_remove_dst(struct dsa_switch_tree *dst)
720{
721 int i;
722
723 dst->master_netdev->dsa_ptr = NULL;
724
725 /* If we used a tagging format that doesn't have an ethertype
726 * field, make sure that all packets from this point get sent
727 * without the tag and go through the regular receive path.
728 */
729 wmb();
730
731 for (i = 0; i < dst->pd->nr_chips; i++) {
732 struct dsa_switch *ds = dst->ds[i];
733
734 if (ds)
735 dsa_switch_destroy(ds);
736 }
737
738 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
739
740 dev_put(dst->master_netdev);
741}
742
743static int dsa_remove(struct platform_device *pdev)
744{
745 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
746
747 dsa_remove_dst(dst);
748 dsa_of_remove(&pdev->dev);
749
750 return 0;
751}
752
753static void dsa_shutdown(struct platform_device *pdev)
754{
755}
756
757#ifdef CONFIG_PM_SLEEP
758static int dsa_suspend(struct device *d)
759{
760 struct platform_device *pdev = to_platform_device(d);
761 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
762 int i, ret = 0;
763
764 for (i = 0; i < dst->pd->nr_chips; i++) {
765 struct dsa_switch *ds = dst->ds[i];
766
767 if (ds != NULL)
768 ret = dsa_switch_suspend(ds);
769 }
770
771 return ret;
772}
773
774static int dsa_resume(struct device *d)
775{
776 struct platform_device *pdev = to_platform_device(d);
777 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
778 int i, ret = 0;
779
780 for (i = 0; i < dst->pd->nr_chips; i++) {
781 struct dsa_switch *ds = dst->ds[i];
782
783 if (ds != NULL)
784 ret = dsa_switch_resume(ds);
785 }
786
787 return ret;
788}
789#endif
790
791static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
792
793static const struct of_device_id dsa_of_match_table[] = {
794 { .compatible = "marvell,dsa", },
795 {}
796};
797MODULE_DEVICE_TABLE(of, dsa_of_match_table);
798
799static struct platform_driver dsa_driver = {
800 .probe = dsa_probe,
801 .remove = dsa_remove,
802 .shutdown = dsa_shutdown,
803 .driver = {
804 .name = "dsa",
805 .of_match_table = dsa_of_match_table,
806 .pm = &dsa_pm_ops,
807 },
808};
809
810int dsa_legacy_register(void)
811{
812 return platform_driver_register(&dsa_driver);
813}
814
815void dsa_legacy_unregister(void)
816{
817 platform_driver_unregister(&dsa_driver);
818}