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