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