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