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