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