]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/dsa/dsa.c
net: dsa: Allow configuration of CPU & DSA port speeds/duplex
[mirror_ubuntu-zesty-kernel.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 **************************************************/
39b0c705
AL
179static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
180{
181 struct dsa_chip_data *cd = ds->pd;
182 struct device_node *port_dn;
183 struct phy_device *phydev;
184 int ret, port;
185
186 for (port = 0; port < DSA_MAX_PORTS; port++) {
187 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
188 continue;
189
190 port_dn = cd->port_dn[port];
191 if (of_phy_is_fixed_link(port_dn)) {
192 ret = of_phy_register_fixed_link(port_dn);
193 if (ret) {
194 netdev_err(master,
195 "failed to register fixed PHY\n");
196 return ret;
197 }
198 phydev = of_phy_find_device(port_dn);
199 genphy_config_init(phydev);
200 genphy_read_status(phydev);
201 if (ds->drv->adjust_link)
202 ds->drv->adjust_link(ds, port, phydev);
203 }
204 }
205 return 0;
206}
207
df197195 208static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
91da11f8 209{
df197195
FF
210 struct dsa_switch_driver *drv = ds->drv;
211 struct dsa_switch_tree *dst = ds->dst;
212 struct dsa_chip_data *pd = ds->pd;
f9bf5a2c 213 bool valid_name_found = false;
df197195
FF
214 int index = ds->index;
215 int i, ret;
91da11f8
LB
216
217 /*
218 * Validate supplied switch configuration.
219 */
91da11f8
LB
220 for (i = 0; i < DSA_MAX_PORTS; i++) {
221 char *name;
222
223 name = pd->port_names[i];
224 if (name == NULL)
225 continue;
226
227 if (!strcmp(name, "cpu")) {
e84665c9 228 if (dst->cpu_switch != -1) {
a2ae6007
JP
229 netdev_err(dst->master_netdev,
230 "multiple cpu ports?!\n");
91da11f8
LB
231 ret = -EINVAL;
232 goto out;
233 }
e84665c9
LB
234 dst->cpu_switch = index;
235 dst->cpu_port = i;
236 } else if (!strcmp(name, "dsa")) {
237 ds->dsa_port_mask |= 1 << i;
91da11f8 238 } else {
e84665c9 239 ds->phys_port_mask |= 1 << i;
91da11f8 240 }
f9bf5a2c 241 valid_name_found = true;
91da11f8
LB
242 }
243
f9bf5a2c
FF
244 if (!valid_name_found && i == DSA_MAX_PORTS) {
245 ret = -EINVAL;
246 goto out;
247 }
91da11f8 248
0d8bcdd3
FF
249 /* Make the built-in MII bus mask match the number of ports,
250 * switch drivers can override this later
251 */
252 ds->phys_mii_mask = ds->phys_port_mask;
253
91da11f8 254 /*
e84665c9
LB
255 * If the CPU connects to this switch, set the switch tree
256 * tagging protocol to the preferred tagging format of this
257 * switch.
91da11f8 258 */
5075314e 259 if (dst->cpu_switch == index) {
59299031 260 switch (ds->tag_protocol) {
5075314e
AD
261#ifdef CONFIG_NET_DSA_TAG_DSA
262 case DSA_TAG_PROTO_DSA:
263 dst->rcv = dsa_netdev_ops.rcv;
264 break;
265#endif
266#ifdef CONFIG_NET_DSA_TAG_EDSA
267 case DSA_TAG_PROTO_EDSA:
268 dst->rcv = edsa_netdev_ops.rcv;
269 break;
270#endif
271#ifdef CONFIG_NET_DSA_TAG_TRAILER
272 case DSA_TAG_PROTO_TRAILER:
273 dst->rcv = trailer_netdev_ops.rcv;
274 break;
275#endif
276#ifdef CONFIG_NET_DSA_TAG_BRCM
277 case DSA_TAG_PROTO_BRCM:
278 dst->rcv = brcm_netdev_ops.rcv;
279 break;
280#endif
ae439286 281 case DSA_TAG_PROTO_NONE:
5075314e 282 break;
ae439286
AL
283 default:
284 ret = -ENOPROTOOPT;
285 goto out;
5075314e 286 }
91da11f8 287
59299031 288 dst->tag_protocol = ds->tag_protocol;
5075314e 289 }
91da11f8
LB
290
291 /*
292 * Do basic register setup.
293 */
294 ret = drv->setup(ds);
295 if (ret < 0)
296 goto out;
297
e84665c9 298 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
91da11f8
LB
299 if (ret < 0)
300 goto out;
301
302 ds->slave_mii_bus = mdiobus_alloc();
303 if (ds->slave_mii_bus == NULL) {
304 ret = -ENOMEM;
305 goto out;
306 }
307 dsa_slave_mii_bus_init(ds);
308
309 ret = mdiobus_register(ds->slave_mii_bus);
310 if (ret < 0)
311 goto out_free;
312
313
314 /*
315 * Create network devices for physical switch ports.
316 */
91da11f8 317 for (i = 0; i < DSA_MAX_PORTS; i++) {
e84665c9 318 if (!(ds->phys_port_mask & (1 << i)))
91da11f8
LB
319 continue;
320
d87d6f44
GR
321 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
322 if (ret < 0) {
a2ae6007
JP
323 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
324 index, i, pd->port_names[i]);
d87d6f44 325 ret = 0;
91da11f8 326 }
91da11f8
LB
327 }
328
39b0c705
AL
329 /* Perform configuration of the CPU and DSA ports */
330 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
331 if (ret < 0) {
332 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
333 index);
334 ret = 0;
335 }
336
51579c3f
GR
337#ifdef CONFIG_NET_DSA_HWMON
338 /* If the switch provides a temperature sensor,
339 * register with hardware monitoring subsystem.
340 * Treat registration error as non-fatal and ignore it.
341 */
342 if (drv->get_temp) {
343 const char *netname = netdev_name(dst->master_netdev);
344 char hname[IFNAMSIZ + 1];
345 int i, j;
346
347 /* Create valid hwmon 'name' attribute */
348 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
349 if (isalnum(netname[i]))
350 hname[j++] = netname[i];
351 }
352 hname[j] = '\0';
353 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
354 hname, index);
355 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
356 ds->hwmon_name, ds, dsa_hwmon_groups);
357 if (IS_ERR(ds->hwmon_dev))
358 ds->hwmon_dev = NULL;
359 }
360#endif /* CONFIG_NET_DSA_HWMON */
361
df197195 362 return ret;
91da11f8
LB
363
364out_free:
365 mdiobus_free(ds->slave_mii_bus);
366out:
91da11f8 367 kfree(ds);
df197195
FF
368 return ret;
369}
370
371static struct dsa_switch *
372dsa_switch_setup(struct dsa_switch_tree *dst, int index,
373 struct device *parent, struct device *host_dev)
374{
375 struct dsa_chip_data *pd = dst->pd->chip + index;
376 struct dsa_switch_driver *drv;
377 struct dsa_switch *ds;
378 int ret;
379 char *name;
380
381 /*
382 * Probe for switch model.
383 */
384 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
385 if (drv == NULL) {
386 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
387 index);
388 return ERR_PTR(-EINVAL);
389 }
390 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
391 index, name);
392
393
394 /*
395 * Allocate and initialise switch state.
396 */
397 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
398 if (ds == NULL)
24595346 399 return ERR_PTR(-ENOMEM);
df197195
FF
400
401 ds->dst = dst;
402 ds->index = index;
403 ds->pd = pd;
404 ds->drv = drv;
59299031 405 ds->tag_protocol = drv->tag_protocol;
df197195
FF
406 ds->master_dev = host_dev;
407
408 ret = dsa_switch_setup_one(ds, parent);
409 if (ret)
24595346 410 return ERR_PTR(ret);
df197195
FF
411
412 return ds;
91da11f8
LB
413}
414
415static void dsa_switch_destroy(struct dsa_switch *ds)
416{
51579c3f
GR
417#ifdef CONFIG_NET_DSA_HWMON
418 if (ds->hwmon_dev)
419 hwmon_device_unregister(ds->hwmon_dev);
420#endif
91da11f8
LB
421}
422
e506d405 423#ifdef CONFIG_PM_SLEEP
24462549
FF
424static int dsa_switch_suspend(struct dsa_switch *ds)
425{
426 int i, ret = 0;
427
428 /* Suspend slave network devices */
429 for (i = 0; i < DSA_MAX_PORTS; i++) {
d79d2107 430 if (!dsa_is_port_initialized(ds, i))
24462549
FF
431 continue;
432
433 ret = dsa_slave_suspend(ds->ports[i]);
434 if (ret)
435 return ret;
436 }
437
438 if (ds->drv->suspend)
439 ret = ds->drv->suspend(ds);
440
441 return ret;
442}
443
444static int dsa_switch_resume(struct dsa_switch *ds)
445{
446 int i, ret = 0;
447
448 if (ds->drv->resume)
449 ret = ds->drv->resume(ds);
450
451 if (ret)
452 return ret;
453
454 /* Resume slave network devices */
455 for (i = 0; i < DSA_MAX_PORTS; i++) {
d79d2107 456 if (!dsa_is_port_initialized(ds, i))
24462549
FF
457 continue;
458
459 ret = dsa_slave_resume(ds->ports[i]);
460 if (ret)
461 return ret;
462 }
463
464 return 0;
465}
e506d405 466#endif
24462549 467
91da11f8
LB
468
469/* link polling *************************************************************/
470static void dsa_link_poll_work(struct work_struct *ugly)
471{
e84665c9
LB
472 struct dsa_switch_tree *dst;
473 int i;
474
475 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
91da11f8 476
e84665c9
LB
477 for (i = 0; i < dst->pd->nr_chips; i++) {
478 struct dsa_switch *ds = dst->ds[i];
91da11f8 479
e84665c9
LB
480 if (ds != NULL && ds->drv->poll_link != NULL)
481 ds->drv->poll_link(ds);
482 }
483
484 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
91da11f8
LB
485}
486
e84665c9 487static void dsa_link_poll_timer(unsigned long _dst)
91da11f8 488{
e84665c9 489 struct dsa_switch_tree *dst = (void *)_dst;
91da11f8 490
e84665c9 491 schedule_work(&dst->link_poll_work);
91da11f8
LB
492}
493
494
495/* platform driver init and cleanup *****************************************/
496static int dev_is_class(struct device *dev, void *class)
497{
498 if (dev->class != NULL && !strcmp(dev->class->name, class))
499 return 1;
500
501 return 0;
502}
503
504static struct device *dev_find_class(struct device *parent, char *class)
505{
506 if (dev_is_class(parent, class)) {
507 get_device(parent);
508 return parent;
509 }
510
511 return device_find_child(parent, class, dev_is_class);
512}
513
b4d2394d 514struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
91da11f8
LB
515{
516 struct device *d;
517
518 d = dev_find_class(dev, "mdio_bus");
519 if (d != NULL) {
520 struct mii_bus *bus;
521
522 bus = to_mii_bus(d);
523 put_device(d);
524
525 return bus;
526 }
527
528 return NULL;
529}
b4d2394d 530EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
91da11f8
LB
531
532static struct net_device *dev_to_net_device(struct device *dev)
533{
534 struct device *d;
535
536 d = dev_find_class(dev, "net");
537 if (d != NULL) {
538 struct net_device *nd;
539
540 nd = to_net_dev(d);
541 dev_hold(nd);
542 put_device(d);
543
544 return nd;
545 }
546
547 return NULL;
548}
549
5e95329b
FF
550#ifdef CONFIG_OF
551static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
552 struct dsa_chip_data *cd,
30303813 553 int chip_index, int port_index,
5e95329b
FF
554 struct device_node *link)
555{
5e95329b 556 const __be32 *reg;
5e95329b
FF
557 int link_sw_addr;
558 struct device_node *parent_sw;
559 int len;
560
561 parent_sw = of_get_parent(link);
562 if (!parent_sw)
563 return -EINVAL;
564
565 reg = of_get_property(parent_sw, "reg", &len);
566 if (!reg || (len != sizeof(*reg) * 2))
567 return -EINVAL;
568
30303813
PN
569 /*
570 * Get the destination switch number from the second field of its 'reg'
571 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
572 */
5e95329b
FF
573 link_sw_addr = be32_to_cpup(reg + 1);
574
575 if (link_sw_addr >= pd->nr_chips)
576 return -EINVAL;
577
578 /* First time routing table allocation */
579 if (!cd->rtable) {
5bc4b46a
FF
580 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
581 GFP_KERNEL);
5e95329b
FF
582 if (!cd->rtable)
583 return -ENOMEM;
584
585 /* default to no valid uplink/downlink */
586 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
587 }
588
30303813 589 cd->rtable[link_sw_addr] = port_index;
5e95329b
FF
590
591 return 0;
5e95329b
FF
592}
593
1e72e6f8
AL
594static int dsa_of_probe_links(struct dsa_platform_data *pd,
595 struct dsa_chip_data *cd,
596 int chip_index, int port_index,
597 struct device_node *port,
598 const char *port_name)
599{
600 struct device_node *link;
601 int link_index;
602 int ret;
603
604 for (link_index = 0;; link_index++) {
605 link = of_parse_phandle(port, "link", link_index);
606 if (!link)
607 break;
608
609 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
610 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
611 port_index, link);
612 if (ret)
613 return ret;
614 }
615 }
616 return 0;
617}
618
21168245
FF
619static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
620{
621 int i;
622 int port_index;
623
624 for (i = 0; i < pd->nr_chips; i++) {
625 port_index = 0;
5f64a7db 626 while (port_index < DSA_MAX_PORTS) {
1f74714f 627 kfree(pd->chip[i].port_names[port_index]);
5f64a7db
FF
628 port_index++;
629 }
21168245
FF
630 kfree(pd->chip[i].rtable);
631 }
632 kfree(pd->chip);
633}
634
f1a26a06 635static int dsa_of_probe(struct device *dev)
5e95329b 636{
f1a26a06 637 struct device_node *np = dev->of_node;
1e72e6f8 638 struct device_node *child, *mdio, *ethernet, *port;
6bc6d0a8 639 struct mii_bus *mdio_bus, *mdio_bus_switch;
769a0202 640 struct net_device *ethernet_dev;
5e95329b
FF
641 struct dsa_platform_data *pd;
642 struct dsa_chip_data *cd;
643 const char *port_name;
644 int chip_index, port_index;
645 const unsigned int *sw_addr, *port_reg;
6793abb4 646 u32 eeprom_len;
21168245 647 int ret;
5e95329b
FF
648
649 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
650 if (!mdio)
651 return -EINVAL;
652
653 mdio_bus = of_mdio_find_bus(mdio);
654 if (!mdio_bus)
b324c07a 655 return -EPROBE_DEFER;
5e95329b
FF
656
657 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
658 if (!ethernet)
659 return -EINVAL;
660
769a0202 661 ethernet_dev = of_find_net_device_by_node(ethernet);
5e95329b 662 if (!ethernet_dev)
b324c07a 663 return -EPROBE_DEFER;
5e95329b
FF
664
665 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
666 if (!pd)
667 return -ENOMEM;
668
f1a26a06 669 dev->platform_data = pd;
769a0202 670 pd->of_netdev = ethernet_dev;
e04449fc 671 pd->nr_chips = of_get_available_child_count(np);
5e95329b
FF
672 if (pd->nr_chips > DSA_MAX_SWITCHES)
673 pd->nr_chips = DSA_MAX_SWITCHES;
674
6f2aed6a
FF
675 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
676 GFP_KERNEL);
5e95329b
FF
677 if (!pd->chip) {
678 ret = -ENOMEM;
679 goto out_free;
680 }
681
d1c0b471 682 chip_index = -1;
5e95329b 683 for_each_available_child_of_node(np, child) {
d1c0b471 684 chip_index++;
5e95329b
FF
685 cd = &pd->chip[chip_index];
686
fa981d9a 687 cd->of_node = child;
c1f570a6 688 cd->host_dev = &mdio_bus->dev;
5e95329b
FF
689
690 sw_addr = of_get_property(child, "reg", NULL);
691 if (!sw_addr)
692 continue;
693
694 cd->sw_addr = be32_to_cpup(sw_addr);
c8cf89f7 695 if (cd->sw_addr >= PHY_MAX_ADDR)
5e95329b
FF
696 continue;
697
50d4964f 698 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
6793abb4
GR
699 cd->eeprom_len = eeprom_len;
700
6bc6d0a8
AL
701 mdio = of_parse_phandle(child, "mii-bus", 0);
702 if (mdio) {
703 mdio_bus_switch = of_mdio_find_bus(mdio);
704 if (!mdio_bus_switch) {
705 ret = -EPROBE_DEFER;
706 goto out_free_chip;
707 }
708 cd->host_dev = &mdio_bus_switch->dev;
709 }
710
5e95329b
FF
711 for_each_available_child_of_node(child, port) {
712 port_reg = of_get_property(port, "reg", NULL);
713 if (!port_reg)
714 continue;
715
716 port_index = be32_to_cpup(port_reg);
8f5063e9
FF
717 if (port_index >= DSA_MAX_PORTS)
718 break;
5e95329b
FF
719
720 port_name = of_get_property(port, "label", NULL);
721 if (!port_name)
722 continue;
723
bd47497a
FF
724 cd->port_dn[port_index] = port;
725
5e95329b
FF
726 cd->port_names[port_index] = kstrdup(port_name,
727 GFP_KERNEL);
728 if (!cd->port_names[port_index]) {
729 ret = -ENOMEM;
730 goto out_free_chip;
731 }
732
1e72e6f8
AL
733 ret = dsa_of_probe_links(pd, cd, chip_index,
734 port_index, port, port_name);
735 if (ret)
736 goto out_free_chip;
5e95329b 737
5e95329b
FF
738 }
739 }
740
741 return 0;
742
743out_free_chip:
21168245 744 dsa_of_free_platform_data(pd);
5e95329b
FF
745out_free:
746 kfree(pd);
f1a26a06 747 dev->platform_data = NULL;
5e95329b
FF
748 return ret;
749}
750
f1a26a06 751static void dsa_of_remove(struct device *dev)
5e95329b 752{
f1a26a06 753 struct dsa_platform_data *pd = dev->platform_data;
5e95329b 754
f1a26a06 755 if (!dev->of_node)
5e95329b
FF
756 return;
757
21168245 758 dsa_of_free_platform_data(pd);
5e95329b
FF
759 kfree(pd);
760}
761#else
f1a26a06 762static inline int dsa_of_probe(struct device *dev)
5e95329b
FF
763{
764 return 0;
765}
766
f1a26a06 767static inline void dsa_of_remove(struct device *dev)
5e95329b
FF
768{
769}
770#endif
771
c86e59b9
FF
772static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
773 struct device *parent, struct dsa_platform_data *pd)
774{
775 int i;
776
777 dst->pd = pd;
778 dst->master_netdev = dev;
779 dst->cpu_switch = -1;
780 dst->cpu_port = -1;
781
782 for (i = 0; i < pd->nr_chips; i++) {
783 struct dsa_switch *ds;
784
785 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
786 if (IS_ERR(ds)) {
787 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
788 i, PTR_ERR(ds));
789 continue;
790 }
791
792 dst->ds[i] = ds;
793 if (ds->drv->poll_link != NULL)
794 dst->link_poll_needed = 1;
795 }
796
797 /*
798 * If we use a tagging format that doesn't have an ethertype
799 * field, make sure that all packets from this point on get
800 * sent to the tag format's receive function.
801 */
802 wmb();
803 dev->dsa_ptr = (void *)dst;
804
805 if (dst->link_poll_needed) {
806 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
807 init_timer(&dst->link_poll_timer);
808 dst->link_poll_timer.data = (unsigned long)dst;
809 dst->link_poll_timer.function = dsa_link_poll_timer;
810 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
811 add_timer(&dst->link_poll_timer);
812 }
813}
814
91da11f8
LB
815static int dsa_probe(struct platform_device *pdev)
816{
91da11f8
LB
817 struct dsa_platform_data *pd = pdev->dev.platform_data;
818 struct net_device *dev;
e84665c9 819 struct dsa_switch_tree *dst;
c86e59b9 820 int ret;
91da11f8 821
a2ae6007
JP
822 pr_notice_once("Distributed Switch Architecture driver version %s\n",
823 dsa_driver_version);
91da11f8 824
5e95329b 825 if (pdev->dev.of_node) {
f1a26a06 826 ret = dsa_of_probe(&pdev->dev);
5e95329b
FF
827 if (ret)
828 return ret;
829
830 pd = pdev->dev.platform_data;
831 }
832
769a0202 833 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
91da11f8
LB
834 return -EINVAL;
835
769a0202
FF
836 if (pd->of_netdev) {
837 dev = pd->of_netdev;
838 dev_hold(dev);
839 } else {
840 dev = dev_to_net_device(pd->netdev);
841 }
5e95329b 842 if (dev == NULL) {
b324c07a 843 ret = -EPROBE_DEFER;
5e95329b
FF
844 goto out;
845 }
91da11f8
LB
846
847 if (dev->dsa_ptr != NULL) {
848 dev_put(dev);
5e95329b
FF
849 ret = -EEXIST;
850 goto out;
91da11f8
LB
851 }
852
e84665c9
LB
853 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
854 if (dst == NULL) {
91da11f8 855 dev_put(dev);
5e95329b
FF
856 ret = -ENOMEM;
857 goto out;
91da11f8
LB
858 }
859
e84665c9
LB
860 platform_set_drvdata(pdev, dst);
861
c86e59b9 862 dsa_setup_dst(dst, dev, &pdev->dev, pd);
91da11f8
LB
863
864 return 0;
5e95329b
FF
865
866out:
f1a26a06 867 dsa_of_remove(&pdev->dev);
5e95329b
FF
868
869 return ret;
91da11f8
LB
870}
871
c86e59b9 872static void dsa_remove_dst(struct dsa_switch_tree *dst)
91da11f8 873{
e84665c9 874 int i;
91da11f8 875
e84665c9
LB
876 if (dst->link_poll_needed)
877 del_timer_sync(&dst->link_poll_timer);
91da11f8 878
43829731 879 flush_work(&dst->link_poll_work);
91da11f8 880
e84665c9
LB
881 for (i = 0; i < dst->pd->nr_chips; i++) {
882 struct dsa_switch *ds = dst->ds[i];
883
884 if (ds != NULL)
885 dsa_switch_destroy(ds);
886 }
c86e59b9
FF
887}
888
889static int dsa_remove(struct platform_device *pdev)
890{
891 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
91da11f8 892
c86e59b9 893 dsa_remove_dst(dst);
f1a26a06 894 dsa_of_remove(&pdev->dev);
5e95329b 895
91da11f8
LB
896 return 0;
897}
898
899static void dsa_shutdown(struct platform_device *pdev)
900{
901}
902
3e8a72d1
FF
903static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
904 struct packet_type *pt, struct net_device *orig_dev)
905{
906 struct dsa_switch_tree *dst = dev->dsa_ptr;
907
908 if (unlikely(dst == NULL)) {
909 kfree_skb(skb);
910 return 0;
911 }
912
5075314e 913 return dst->rcv(skb, dev, pt, orig_dev);
3e8a72d1
FF
914}
915
61b7363f 916static struct packet_type dsa_pack_type __read_mostly = {
3e8a72d1
FF
917 .type = cpu_to_be16(ETH_P_XDSA),
918 .func = dsa_switch_rcv,
919};
920
b73adef6
FF
921static struct notifier_block dsa_netdevice_nb __read_mostly = {
922 .notifier_call = dsa_slave_netdevice_event,
923};
924
24462549
FF
925#ifdef CONFIG_PM_SLEEP
926static int dsa_suspend(struct device *d)
927{
928 struct platform_device *pdev = to_platform_device(d);
929 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
930 int i, ret = 0;
931
932 for (i = 0; i < dst->pd->nr_chips; i++) {
933 struct dsa_switch *ds = dst->ds[i];
934
935 if (ds != NULL)
936 ret = dsa_switch_suspend(ds);
937 }
938
939 return ret;
940}
941
942static int dsa_resume(struct device *d)
943{
944 struct platform_device *pdev = to_platform_device(d);
945 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
946 int i, ret = 0;
947
948 for (i = 0; i < dst->pd->nr_chips; i++) {
949 struct dsa_switch *ds = dst->ds[i];
950
951 if (ds != NULL)
952 ret = dsa_switch_resume(ds);
953 }
954
955 return ret;
956}
957#endif
958
959static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
960
5e95329b 961static const struct of_device_id dsa_of_match_table[] = {
246d7f77 962 { .compatible = "brcm,bcm7445-switch-v4.0" },
5e95329b
FF
963 { .compatible = "marvell,dsa", },
964 {}
965};
966MODULE_DEVICE_TABLE(of, dsa_of_match_table);
967
91da11f8
LB
968static struct platform_driver dsa_driver = {
969 .probe = dsa_probe,
970 .remove = dsa_remove,
971 .shutdown = dsa_shutdown,
972 .driver = {
973 .name = "dsa",
5e95329b 974 .of_match_table = dsa_of_match_table,
24462549 975 .pm = &dsa_pm_ops,
91da11f8
LB
976 },
977};
978
979static int __init dsa_init_module(void)
980{
7df899c3
BH
981 int rc;
982
b73adef6
FF
983 register_netdevice_notifier(&dsa_netdevice_nb);
984
7df899c3
BH
985 rc = platform_driver_register(&dsa_driver);
986 if (rc)
987 return rc;
988
3e8a72d1
FF
989 dev_add_pack(&dsa_pack_type);
990
7df899c3 991 return 0;
91da11f8
LB
992}
993module_init(dsa_init_module);
994
995static void __exit dsa_cleanup_module(void)
996{
b73adef6 997 unregister_netdevice_notifier(&dsa_netdevice_nb);
3e8a72d1 998 dev_remove_pack(&dsa_pack_type);
91da11f8
LB
999 platform_driver_unregister(&dsa_driver);
1000}
1001module_exit(dsa_cleanup_module);
1002
577d6a7c 1003MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
91da11f8
LB
1004MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1005MODULE_LICENSE("GPL");
1006MODULE_ALIAS("platform:dsa");