]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/dsa/dsa.c
net: dsa: Copy the routing table into the switch structure
[mirror_ubuntu-zesty-kernel.git] / net / dsa / dsa.c
1 /*
2 * net/dsa/dsa.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/ctype.h>
13 #include <linux/device.h>
14 #include <linux/hwmon.h>
15 #include <linux/list.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/dsa.h>
20 #include <linux/of.h>
21 #include <linux/of_mdio.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_net.h>
24 #include <linux/of_gpio.h>
25 #include <linux/sysfs.h>
26 #include <linux/phy_fixed.h>
27 #include <linux/gpio/consumer.h>
28 #include "dsa_priv.h"
29
30 char dsa_driver_version[] = "0.1";
31
32
33 /* switch driver registration ***********************************************/
34 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
35 static LIST_HEAD(dsa_switch_drivers);
36
37 void register_switch_driver(struct dsa_switch_driver *drv)
38 {
39 mutex_lock(&dsa_switch_drivers_mutex);
40 list_add_tail(&drv->list, &dsa_switch_drivers);
41 mutex_unlock(&dsa_switch_drivers_mutex);
42 }
43 EXPORT_SYMBOL_GPL(register_switch_driver);
44
45 void unregister_switch_driver(struct dsa_switch_driver *drv)
46 {
47 mutex_lock(&dsa_switch_drivers_mutex);
48 list_del_init(&drv->list);
49 mutex_unlock(&dsa_switch_drivers_mutex);
50 }
51 EXPORT_SYMBOL_GPL(unregister_switch_driver);
52
53 static struct dsa_switch_driver *
54 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
55 const char **_name, void **priv)
56 {
57 struct dsa_switch_driver *ret;
58 struct list_head *list;
59 const char *name;
60
61 ret = NULL;
62 name = NULL;
63
64 mutex_lock(&dsa_switch_drivers_mutex);
65 list_for_each(list, &dsa_switch_drivers) {
66 struct dsa_switch_driver *drv;
67
68 drv = list_entry(list, struct dsa_switch_driver, list);
69
70 name = drv->probe(parent, host_dev, sw_addr, priv);
71 if (name != NULL) {
72 ret = drv;
73 break;
74 }
75 }
76 mutex_unlock(&dsa_switch_drivers_mutex);
77
78 *_name = name;
79
80 return ret;
81 }
82
83 /* hwmon support ************************************************************/
84
85 #ifdef CONFIG_NET_DSA_HWMON
86
87 static ssize_t temp1_input_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
89 {
90 struct dsa_switch *ds = dev_get_drvdata(dev);
91 int temp, ret;
92
93 ret = ds->drv->get_temp(ds, &temp);
94 if (ret < 0)
95 return ret;
96
97 return sprintf(buf, "%d\n", temp * 1000);
98 }
99 static DEVICE_ATTR_RO(temp1_input);
100
101 static ssize_t temp1_max_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
103 {
104 struct dsa_switch *ds = dev_get_drvdata(dev);
105 int temp, ret;
106
107 ret = ds->drv->get_temp_limit(ds, &temp);
108 if (ret < 0)
109 return ret;
110
111 return sprintf(buf, "%d\n", temp * 1000);
112 }
113
114 static ssize_t temp1_max_store(struct device *dev,
115 struct device_attribute *attr, const char *buf,
116 size_t count)
117 {
118 struct dsa_switch *ds = dev_get_drvdata(dev);
119 int temp, ret;
120
121 ret = kstrtoint(buf, 0, &temp);
122 if (ret < 0)
123 return ret;
124
125 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
126 if (ret < 0)
127 return ret;
128
129 return count;
130 }
131 static DEVICE_ATTR_RW(temp1_max);
132
133 static ssize_t temp1_max_alarm_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135 {
136 struct dsa_switch *ds = dev_get_drvdata(dev);
137 bool alarm;
138 int ret;
139
140 ret = ds->drv->get_temp_alarm(ds, &alarm);
141 if (ret < 0)
142 return ret;
143
144 return sprintf(buf, "%d\n", alarm);
145 }
146 static DEVICE_ATTR_RO(temp1_max_alarm);
147
148 static struct attribute *dsa_hwmon_attrs[] = {
149 &dev_attr_temp1_input.attr, /* 0 */
150 &dev_attr_temp1_max.attr, /* 1 */
151 &dev_attr_temp1_max_alarm.attr, /* 2 */
152 NULL
153 };
154
155 static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
156 struct attribute *attr, int index)
157 {
158 struct device *dev = container_of(kobj, struct device, kobj);
159 struct dsa_switch *ds = dev_get_drvdata(dev);
160 struct dsa_switch_driver *drv = ds->drv;
161 umode_t mode = attr->mode;
162
163 if (index == 1) {
164 if (!drv->get_temp_limit)
165 mode = 0;
166 else if (!drv->set_temp_limit)
167 mode &= ~S_IWUSR;
168 } else if (index == 2 && !drv->get_temp_alarm) {
169 mode = 0;
170 }
171 return mode;
172 }
173
174 static const struct attribute_group dsa_hwmon_group = {
175 .attrs = dsa_hwmon_attrs,
176 .is_visible = dsa_hwmon_attrs_visible,
177 };
178 __ATTRIBUTE_GROUPS(dsa_hwmon);
179
180 #endif /* CONFIG_NET_DSA_HWMON */
181
182 /* basic switch operations **************************************************/
183 static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
184 {
185 struct device_node *port_dn;
186 struct phy_device *phydev;
187 int ret, port, mode;
188
189 for (port = 0; port < DSA_MAX_PORTS; port++) {
190 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
191 continue;
192
193 port_dn = ds->ports[port].dn;
194 if (of_phy_is_fixed_link(port_dn)) {
195 ret = of_phy_register_fixed_link(port_dn);
196 if (ret) {
197 netdev_err(master,
198 "failed to register fixed PHY\n");
199 return ret;
200 }
201 phydev = of_phy_find_device(port_dn);
202
203 mode = of_get_phy_mode(port_dn);
204 if (mode < 0)
205 mode = PHY_INTERFACE_MODE_NA;
206 phydev->interface = mode;
207
208 genphy_config_init(phydev);
209 genphy_read_status(phydev);
210 if (ds->drv->adjust_link)
211 ds->drv->adjust_link(ds, port, phydev);
212 }
213 }
214 return 0;
215 }
216
217 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
218 {
219 struct dsa_switch_driver *drv = ds->drv;
220 struct dsa_switch_tree *dst = ds->dst;
221 struct dsa_chip_data *cd = ds->cd;
222 bool valid_name_found = false;
223 int index = ds->index;
224 int i, ret;
225
226 /*
227 * Validate supplied switch configuration.
228 */
229 for (i = 0; i < DSA_MAX_PORTS; i++) {
230 char *name;
231
232 name = cd->port_names[i];
233 if (name == NULL)
234 continue;
235
236 if (!strcmp(name, "cpu")) {
237 if (dst->cpu_switch != -1) {
238 netdev_err(dst->master_netdev,
239 "multiple cpu ports?!\n");
240 ret = -EINVAL;
241 goto out;
242 }
243 dst->cpu_switch = index;
244 dst->cpu_port = i;
245 } else if (!strcmp(name, "dsa")) {
246 ds->dsa_port_mask |= 1 << i;
247 } else {
248 ds->enabled_port_mask |= 1 << i;
249 }
250 valid_name_found = true;
251 }
252
253 if (!valid_name_found && i == DSA_MAX_PORTS) {
254 ret = -EINVAL;
255 goto out;
256 }
257
258 /* Make the built-in MII bus mask match the number of ports,
259 * switch drivers can override this later
260 */
261 ds->phys_mii_mask = ds->enabled_port_mask;
262
263 /*
264 * If the CPU connects to this switch, set the switch tree
265 * tagging protocol to the preferred tagging format of this
266 * switch.
267 */
268 if (dst->cpu_switch == index) {
269 switch (drv->tag_protocol) {
270 #ifdef CONFIG_NET_DSA_TAG_DSA
271 case DSA_TAG_PROTO_DSA:
272 dst->rcv = dsa_netdev_ops.rcv;
273 break;
274 #endif
275 #ifdef CONFIG_NET_DSA_TAG_EDSA
276 case DSA_TAG_PROTO_EDSA:
277 dst->rcv = edsa_netdev_ops.rcv;
278 break;
279 #endif
280 #ifdef CONFIG_NET_DSA_TAG_TRAILER
281 case DSA_TAG_PROTO_TRAILER:
282 dst->rcv = trailer_netdev_ops.rcv;
283 break;
284 #endif
285 #ifdef CONFIG_NET_DSA_TAG_BRCM
286 case DSA_TAG_PROTO_BRCM:
287 dst->rcv = brcm_netdev_ops.rcv;
288 break;
289 #endif
290 case DSA_TAG_PROTO_NONE:
291 break;
292 default:
293 ret = -ENOPROTOOPT;
294 goto out;
295 }
296
297 dst->tag_protocol = drv->tag_protocol;
298 }
299
300 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
301
302 /*
303 * Do basic register setup.
304 */
305 ret = drv->setup(ds);
306 if (ret < 0)
307 goto out;
308
309 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
310 if (ret < 0)
311 goto out;
312
313 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
314 if (ds->slave_mii_bus == NULL) {
315 ret = -ENOMEM;
316 goto out;
317 }
318 dsa_slave_mii_bus_init(ds);
319
320 ret = mdiobus_register(ds->slave_mii_bus);
321 if (ret < 0)
322 goto out;
323
324
325 /*
326 * Create network devices for physical switch ports.
327 */
328 for (i = 0; i < DSA_MAX_PORTS; i++) {
329 ds->ports[i].dn = cd->port_dn[i];
330
331 if (!(ds->enabled_port_mask & (1 << i)))
332 continue;
333
334 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
335 if (ret < 0) {
336 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
337 index, i, cd->port_names[i], ret);
338 ret = 0;
339 }
340 }
341
342 /* Perform configuration of the CPU and DSA ports */
343 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
344 if (ret < 0) {
345 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
346 index);
347 ret = 0;
348 }
349
350 #ifdef CONFIG_NET_DSA_HWMON
351 /* If the switch provides a temperature sensor,
352 * register with hardware monitoring subsystem.
353 * Treat registration error as non-fatal and ignore it.
354 */
355 if (drv->get_temp) {
356 const char *netname = netdev_name(dst->master_netdev);
357 char hname[IFNAMSIZ + 1];
358 int i, j;
359
360 /* Create valid hwmon 'name' attribute */
361 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
362 if (isalnum(netname[i]))
363 hname[j++] = netname[i];
364 }
365 hname[j] = '\0';
366 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
367 hname, index);
368 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
369 ds->hwmon_name, ds, dsa_hwmon_groups);
370 if (IS_ERR(ds->hwmon_dev))
371 ds->hwmon_dev = NULL;
372 }
373 #endif /* CONFIG_NET_DSA_HWMON */
374
375 return ret;
376
377 out:
378 return ret;
379 }
380
381 static struct dsa_switch *
382 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
383 struct device *parent, struct device *host_dev)
384 {
385 struct dsa_chip_data *cd = dst->pd->chip + index;
386 struct dsa_switch_driver *drv;
387 struct dsa_switch *ds;
388 int ret;
389 const char *name;
390 void *priv;
391
392 /*
393 * Probe for switch model.
394 */
395 drv = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
396 if (drv == NULL) {
397 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
398 index);
399 return ERR_PTR(-EINVAL);
400 }
401 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
402 index, name);
403
404
405 /*
406 * Allocate and initialise switch state.
407 */
408 ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
409 if (ds == NULL)
410 return ERR_PTR(-ENOMEM);
411
412 ds->dst = dst;
413 ds->index = index;
414 ds->cd = cd;
415 ds->drv = drv;
416 ds->priv = priv;
417 ds->dev = parent;
418
419 ret = dsa_switch_setup_one(ds, parent);
420 if (ret)
421 return ERR_PTR(ret);
422
423 return ds;
424 }
425
426 static void dsa_switch_destroy(struct dsa_switch *ds)
427 {
428 struct device_node *port_dn;
429 struct phy_device *phydev;
430 int port;
431
432 #ifdef CONFIG_NET_DSA_HWMON
433 if (ds->hwmon_dev)
434 hwmon_device_unregister(ds->hwmon_dev);
435 #endif
436
437 /* Destroy network devices for physical switch ports. */
438 for (port = 0; port < DSA_MAX_PORTS; port++) {
439 if (!(ds->enabled_port_mask & (1 << port)))
440 continue;
441
442 if (!ds->ports[port].netdev)
443 continue;
444
445 dsa_slave_destroy(ds->ports[port].netdev);
446 }
447
448 /* Remove any fixed link PHYs */
449 for (port = 0; port < DSA_MAX_PORTS; port++) {
450 port_dn = ds->ports[port].dn;
451 if (of_phy_is_fixed_link(port_dn)) {
452 phydev = of_phy_find_device(port_dn);
453 if (phydev) {
454 phy_device_free(phydev);
455 of_node_put(port_dn);
456 fixed_phy_unregister(phydev);
457 }
458 }
459 }
460
461 mdiobus_unregister(ds->slave_mii_bus);
462 }
463
464 #ifdef CONFIG_PM_SLEEP
465 static int dsa_switch_suspend(struct dsa_switch *ds)
466 {
467 int i, ret = 0;
468
469 /* Suspend slave network devices */
470 for (i = 0; i < DSA_MAX_PORTS; i++) {
471 if (!dsa_is_port_initialized(ds, i))
472 continue;
473
474 ret = dsa_slave_suspend(ds->ports[i].netdev);
475 if (ret)
476 return ret;
477 }
478
479 if (ds->drv->suspend)
480 ret = ds->drv->suspend(ds);
481
482 return ret;
483 }
484
485 static int dsa_switch_resume(struct dsa_switch *ds)
486 {
487 int i, ret = 0;
488
489 if (ds->drv->resume)
490 ret = ds->drv->resume(ds);
491
492 if (ret)
493 return ret;
494
495 /* Resume slave network devices */
496 for (i = 0; i < DSA_MAX_PORTS; i++) {
497 if (!dsa_is_port_initialized(ds, i))
498 continue;
499
500 ret = dsa_slave_resume(ds->ports[i].netdev);
501 if (ret)
502 return ret;
503 }
504
505 return 0;
506 }
507 #endif
508
509 /* platform driver init and cleanup *****************************************/
510 static int dev_is_class(struct device *dev, void *class)
511 {
512 if (dev->class != NULL && !strcmp(dev->class->name, class))
513 return 1;
514
515 return 0;
516 }
517
518 static struct device *dev_find_class(struct device *parent, char *class)
519 {
520 if (dev_is_class(parent, class)) {
521 get_device(parent);
522 return parent;
523 }
524
525 return device_find_child(parent, class, dev_is_class);
526 }
527
528 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
529 {
530 struct device *d;
531
532 d = dev_find_class(dev, "mdio_bus");
533 if (d != NULL) {
534 struct mii_bus *bus;
535
536 bus = to_mii_bus(d);
537 put_device(d);
538
539 return bus;
540 }
541
542 return NULL;
543 }
544 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
545
546 static struct net_device *dev_to_net_device(struct device *dev)
547 {
548 struct device *d;
549
550 d = dev_find_class(dev, "net");
551 if (d != NULL) {
552 struct net_device *nd;
553
554 nd = to_net_dev(d);
555 dev_hold(nd);
556 put_device(d);
557
558 return nd;
559 }
560
561 return NULL;
562 }
563
564 #ifdef CONFIG_OF
565 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
566 struct dsa_chip_data *cd,
567 int chip_index, int port_index,
568 struct device_node *link)
569 {
570 const __be32 *reg;
571 int link_sw_addr;
572 struct device_node *parent_sw;
573 int len;
574
575 parent_sw = of_get_parent(link);
576 if (!parent_sw)
577 return -EINVAL;
578
579 reg = of_get_property(parent_sw, "reg", &len);
580 if (!reg || (len != sizeof(*reg) * 2))
581 return -EINVAL;
582
583 /*
584 * Get the destination switch number from the second field of its 'reg'
585 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
586 */
587 link_sw_addr = be32_to_cpup(reg + 1);
588
589 if (link_sw_addr >= pd->nr_chips)
590 return -EINVAL;
591
592 cd->rtable[link_sw_addr] = port_index;
593
594 return 0;
595 }
596
597 static int dsa_of_probe_links(struct dsa_platform_data *pd,
598 struct dsa_chip_data *cd,
599 int chip_index, int port_index,
600 struct device_node *port,
601 const char *port_name)
602 {
603 struct device_node *link;
604 int link_index;
605 int ret;
606
607 for (link_index = 0;; link_index++) {
608 link = of_parse_phandle(port, "link", link_index);
609 if (!link)
610 break;
611
612 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
613 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
614 port_index, link);
615 if (ret)
616 return ret;
617 }
618 }
619 return 0;
620 }
621
622 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
623 {
624 int i;
625 int port_index;
626
627 for (i = 0; i < pd->nr_chips; i++) {
628 port_index = 0;
629 while (port_index < DSA_MAX_PORTS) {
630 kfree(pd->chip[i].port_names[port_index]);
631 port_index++;
632 }
633
634 /* Drop our reference to the MDIO bus device */
635 if (pd->chip[i].host_dev)
636 put_device(pd->chip[i].host_dev);
637 }
638 kfree(pd->chip);
639 }
640
641 static int dsa_of_probe(struct device *dev)
642 {
643 struct device_node *np = dev->of_node;
644 struct device_node *child, *mdio, *ethernet, *port;
645 struct mii_bus *mdio_bus, *mdio_bus_switch;
646 struct net_device *ethernet_dev;
647 struct dsa_platform_data *pd;
648 struct dsa_chip_data *cd;
649 const char *port_name;
650 int chip_index, port_index;
651 const unsigned int *sw_addr, *port_reg;
652 u32 eeprom_len;
653 int ret;
654
655 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
656 if (!mdio)
657 return -EINVAL;
658
659 mdio_bus = of_mdio_find_bus(mdio);
660 if (!mdio_bus)
661 return -EPROBE_DEFER;
662
663 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
664 if (!ethernet) {
665 ret = -EINVAL;
666 goto out_put_mdio;
667 }
668
669 ethernet_dev = of_find_net_device_by_node(ethernet);
670 if (!ethernet_dev) {
671 ret = -EPROBE_DEFER;
672 goto out_put_mdio;
673 }
674
675 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
676 if (!pd) {
677 ret = -ENOMEM;
678 goto out_put_ethernet;
679 }
680
681 dev->platform_data = pd;
682 pd->of_netdev = ethernet_dev;
683 pd->nr_chips = of_get_available_child_count(np);
684 if (pd->nr_chips > DSA_MAX_SWITCHES)
685 pd->nr_chips = DSA_MAX_SWITCHES;
686
687 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
688 GFP_KERNEL);
689 if (!pd->chip) {
690 ret = -ENOMEM;
691 goto out_free;
692 }
693
694 chip_index = -1;
695 for_each_available_child_of_node(np, child) {
696 chip_index++;
697 cd = &pd->chip[chip_index];
698
699 cd->of_node = child;
700
701 /* When assigning the host device, increment its refcount */
702 cd->host_dev = get_device(&mdio_bus->dev);
703
704 sw_addr = of_get_property(child, "reg", NULL);
705 if (!sw_addr)
706 continue;
707
708 cd->sw_addr = be32_to_cpup(sw_addr);
709 if (cd->sw_addr >= PHY_MAX_ADDR)
710 continue;
711
712 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
713 cd->eeprom_len = eeprom_len;
714
715 mdio = of_parse_phandle(child, "mii-bus", 0);
716 if (mdio) {
717 mdio_bus_switch = of_mdio_find_bus(mdio);
718 if (!mdio_bus_switch) {
719 ret = -EPROBE_DEFER;
720 goto out_free_chip;
721 }
722
723 /* Drop the mdio_bus device ref, replacing the host
724 * device with the mdio_bus_switch device, keeping
725 * the refcount from of_mdio_find_bus() above.
726 */
727 put_device(cd->host_dev);
728 cd->host_dev = &mdio_bus_switch->dev;
729 }
730
731 for_each_available_child_of_node(child, port) {
732 port_reg = of_get_property(port, "reg", NULL);
733 if (!port_reg)
734 continue;
735
736 port_index = be32_to_cpup(port_reg);
737 if (port_index >= DSA_MAX_PORTS)
738 break;
739
740 port_name = of_get_property(port, "label", NULL);
741 if (!port_name)
742 continue;
743
744 cd->port_dn[port_index] = port;
745
746 cd->port_names[port_index] = kstrdup(port_name,
747 GFP_KERNEL);
748 if (!cd->port_names[port_index]) {
749 ret = -ENOMEM;
750 goto out_free_chip;
751 }
752
753 ret = dsa_of_probe_links(pd, cd, chip_index,
754 port_index, port, port_name);
755 if (ret)
756 goto out_free_chip;
757
758 }
759 }
760
761 /* The individual chips hold their own refcount on the mdio bus,
762 * so drop ours */
763 put_device(&mdio_bus->dev);
764
765 return 0;
766
767 out_free_chip:
768 dsa_of_free_platform_data(pd);
769 out_free:
770 kfree(pd);
771 dev->platform_data = NULL;
772 out_put_ethernet:
773 put_device(&ethernet_dev->dev);
774 out_put_mdio:
775 put_device(&mdio_bus->dev);
776 return ret;
777 }
778
779 static void dsa_of_remove(struct device *dev)
780 {
781 struct dsa_platform_data *pd = dev->platform_data;
782
783 if (!dev->of_node)
784 return;
785
786 dsa_of_free_platform_data(pd);
787 put_device(&pd->of_netdev->dev);
788 kfree(pd);
789 }
790 #else
791 static inline int dsa_of_probe(struct device *dev)
792 {
793 return 0;
794 }
795
796 static inline void dsa_of_remove(struct device *dev)
797 {
798 }
799 #endif
800
801 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
802 struct device *parent, struct dsa_platform_data *pd)
803 {
804 int i;
805 unsigned configured = 0;
806
807 dst->pd = pd;
808 dst->master_netdev = dev;
809 dst->cpu_switch = -1;
810 dst->cpu_port = -1;
811
812 for (i = 0; i < pd->nr_chips; i++) {
813 struct dsa_switch *ds;
814
815 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
816 if (IS_ERR(ds)) {
817 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
818 i, PTR_ERR(ds));
819 continue;
820 }
821
822 dst->ds[i] = ds;
823
824 ++configured;
825 }
826
827 /*
828 * If no switch was found, exit cleanly
829 */
830 if (!configured)
831 return -EPROBE_DEFER;
832
833 /*
834 * If we use a tagging format that doesn't have an ethertype
835 * field, make sure that all packets from this point on get
836 * sent to the tag format's receive function.
837 */
838 wmb();
839 dev->dsa_ptr = (void *)dst;
840
841 return 0;
842 }
843
844 static int dsa_probe(struct platform_device *pdev)
845 {
846 struct dsa_platform_data *pd = pdev->dev.platform_data;
847 struct net_device *dev;
848 struct dsa_switch_tree *dst;
849 int ret;
850
851 pr_notice_once("Distributed Switch Architecture driver version %s\n",
852 dsa_driver_version);
853
854 if (pdev->dev.of_node) {
855 ret = dsa_of_probe(&pdev->dev);
856 if (ret)
857 return ret;
858
859 pd = pdev->dev.platform_data;
860 }
861
862 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
863 return -EINVAL;
864
865 if (pd->of_netdev) {
866 dev = pd->of_netdev;
867 dev_hold(dev);
868 } else {
869 dev = dev_to_net_device(pd->netdev);
870 }
871 if (dev == NULL) {
872 ret = -EPROBE_DEFER;
873 goto out;
874 }
875
876 if (dev->dsa_ptr != NULL) {
877 dev_put(dev);
878 ret = -EEXIST;
879 goto out;
880 }
881
882 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
883 if (dst == NULL) {
884 dev_put(dev);
885 ret = -ENOMEM;
886 goto out;
887 }
888
889 platform_set_drvdata(pdev, dst);
890
891 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
892 if (ret) {
893 dev_put(dev);
894 goto out;
895 }
896
897 return 0;
898
899 out:
900 dsa_of_remove(&pdev->dev);
901
902 return ret;
903 }
904
905 static void dsa_remove_dst(struct dsa_switch_tree *dst)
906 {
907 int i;
908
909 dst->master_netdev->dsa_ptr = NULL;
910
911 /* If we used a tagging format that doesn't have an ethertype
912 * field, make sure that all packets from this point get sent
913 * without the tag and go through the regular receive path.
914 */
915 wmb();
916
917 for (i = 0; i < dst->pd->nr_chips; i++) {
918 struct dsa_switch *ds = dst->ds[i];
919
920 if (ds)
921 dsa_switch_destroy(ds);
922 }
923
924 dev_put(dst->master_netdev);
925 }
926
927 static int dsa_remove(struct platform_device *pdev)
928 {
929 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
930
931 dsa_remove_dst(dst);
932 dsa_of_remove(&pdev->dev);
933
934 return 0;
935 }
936
937 static void dsa_shutdown(struct platform_device *pdev)
938 {
939 }
940
941 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
942 struct packet_type *pt, struct net_device *orig_dev)
943 {
944 struct dsa_switch_tree *dst = dev->dsa_ptr;
945
946 if (unlikely(dst == NULL)) {
947 kfree_skb(skb);
948 return 0;
949 }
950
951 return dst->rcv(skb, dev, pt, orig_dev);
952 }
953
954 static struct packet_type dsa_pack_type __read_mostly = {
955 .type = cpu_to_be16(ETH_P_XDSA),
956 .func = dsa_switch_rcv,
957 };
958
959 static struct notifier_block dsa_netdevice_nb __read_mostly = {
960 .notifier_call = dsa_slave_netdevice_event,
961 };
962
963 #ifdef CONFIG_PM_SLEEP
964 static int dsa_suspend(struct device *d)
965 {
966 struct platform_device *pdev = to_platform_device(d);
967 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
968 int i, ret = 0;
969
970 for (i = 0; i < dst->pd->nr_chips; i++) {
971 struct dsa_switch *ds = dst->ds[i];
972
973 if (ds != NULL)
974 ret = dsa_switch_suspend(ds);
975 }
976
977 return ret;
978 }
979
980 static int dsa_resume(struct device *d)
981 {
982 struct platform_device *pdev = to_platform_device(d);
983 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
984 int i, ret = 0;
985
986 for (i = 0; i < dst->pd->nr_chips; i++) {
987 struct dsa_switch *ds = dst->ds[i];
988
989 if (ds != NULL)
990 ret = dsa_switch_resume(ds);
991 }
992
993 return ret;
994 }
995 #endif
996
997 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
998
999 static const struct of_device_id dsa_of_match_table[] = {
1000 { .compatible = "brcm,bcm7445-switch-v4.0" },
1001 { .compatible = "marvell,dsa", },
1002 {}
1003 };
1004 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
1005
1006 static struct platform_driver dsa_driver = {
1007 .probe = dsa_probe,
1008 .remove = dsa_remove,
1009 .shutdown = dsa_shutdown,
1010 .driver = {
1011 .name = "dsa",
1012 .of_match_table = dsa_of_match_table,
1013 .pm = &dsa_pm_ops,
1014 },
1015 };
1016
1017 static int __init dsa_init_module(void)
1018 {
1019 int rc;
1020
1021 register_netdevice_notifier(&dsa_netdevice_nb);
1022
1023 rc = platform_driver_register(&dsa_driver);
1024 if (rc)
1025 return rc;
1026
1027 dev_add_pack(&dsa_pack_type);
1028
1029 return 0;
1030 }
1031 module_init(dsa_init_module);
1032
1033 static void __exit dsa_cleanup_module(void)
1034 {
1035 unregister_netdevice_notifier(&dsa_netdevice_nb);
1036 dev_remove_pack(&dsa_pack_type);
1037 platform_driver_unregister(&dsa_driver);
1038 }
1039 module_exit(dsa_cleanup_module);
1040
1041 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1042 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1043 MODULE_LICENSE("GPL");
1044 MODULE_ALIAS("platform:dsa");