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