blob: 5edbbca89f1f876fbd034d0b092aeee2e633a7d6 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/dsa.c - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Florian Fainelli5e95329b2013-03-22 10:50:50 +00004 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 *
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
Guenter Roeck51579c32014-10-29 10:44:58 -070012#include <linux/ctype.h>
13#include <linux/device.h>
14#include <linux/hwmon.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000015#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040018#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019#include <net/dsa.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000020#include <linux/of.h>
21#include <linux/of_mdio.h>
22#include <linux/of_platform.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070023#include <linux/sysfs.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000024#include "dsa_priv.h"
25
26char dsa_driver_version[] = "0.1";
27
28
29/* switch driver registration ***********************************************/
30static DEFINE_MUTEX(dsa_switch_drivers_mutex);
31static LIST_HEAD(dsa_switch_drivers);
32
33void register_switch_driver(struct dsa_switch_driver *drv)
34{
35 mutex_lock(&dsa_switch_drivers_mutex);
36 list_add_tail(&drv->list, &dsa_switch_drivers);
37 mutex_unlock(&dsa_switch_drivers_mutex);
38}
Ben Hutchingsad293b82011-11-25 14:34:07 +000039EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000040
41void unregister_switch_driver(struct dsa_switch_driver *drv)
42{
43 mutex_lock(&dsa_switch_drivers_mutex);
44 list_del_init(&drv->list);
45 mutex_unlock(&dsa_switch_drivers_mutex);
46}
Ben Hutchingsad293b82011-11-25 14:34:07 +000047EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000048
49static struct dsa_switch_driver *
Alexander Duyckb4d23942014-09-15 13:00:27 -040050dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000051{
52 struct dsa_switch_driver *ret;
53 struct list_head *list;
54 char *name;
55
56 ret = NULL;
57 name = NULL;
58
59 mutex_lock(&dsa_switch_drivers_mutex);
60 list_for_each(list, &dsa_switch_drivers) {
61 struct dsa_switch_driver *drv;
62
63 drv = list_entry(list, struct dsa_switch_driver, list);
64
Alexander Duyckb4d23942014-09-15 13:00:27 -040065 name = drv->probe(host_dev, sw_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000066 if (name != NULL) {
67 ret = drv;
68 break;
69 }
70 }
71 mutex_unlock(&dsa_switch_drivers_mutex);
72
73 *_name = name;
74
75 return ret;
76}
77
Guenter Roeck51579c32014-10-29 10:44:58 -070078/* hwmon support ************************************************************/
79
80#ifdef CONFIG_NET_DSA_HWMON
81
82static ssize_t temp1_input_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
84{
85 struct dsa_switch *ds = dev_get_drvdata(dev);
86 int temp, ret;
87
88 ret = ds->drv->get_temp(ds, &temp);
89 if (ret < 0)
90 return ret;
91
92 return sprintf(buf, "%d\n", temp * 1000);
93}
94static DEVICE_ATTR_RO(temp1_input);
95
96static ssize_t temp1_max_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
98{
99 struct dsa_switch *ds = dev_get_drvdata(dev);
100 int temp, ret;
101
102 ret = ds->drv->get_temp_limit(ds, &temp);
103 if (ret < 0)
104 return ret;
105
106 return sprintf(buf, "%d\n", temp * 1000);
107}
108
109static ssize_t temp1_max_store(struct device *dev,
110 struct device_attribute *attr, const char *buf,
111 size_t count)
112{
113 struct dsa_switch *ds = dev_get_drvdata(dev);
114 int temp, ret;
115
116 ret = kstrtoint(buf, 0, &temp);
117 if (ret < 0)
118 return ret;
119
120 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
121 if (ret < 0)
122 return ret;
123
124 return count;
125}
126static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store);
127
128static ssize_t temp1_max_alarm_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
130{
131 struct dsa_switch *ds = dev_get_drvdata(dev);
132 bool alarm;
133 int ret;
134
135 ret = ds->drv->get_temp_alarm(ds, &alarm);
136 if (ret < 0)
137 return ret;
138
139 return sprintf(buf, "%d\n", alarm);
140}
141static DEVICE_ATTR_RO(temp1_max_alarm);
142
143static struct attribute *dsa_hwmon_attrs[] = {
144 &dev_attr_temp1_input.attr, /* 0 */
145 &dev_attr_temp1_max.attr, /* 1 */
146 &dev_attr_temp1_max_alarm.attr, /* 2 */
147 NULL
148};
149
150static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
151 struct attribute *attr, int index)
152{
153 struct device *dev = container_of(kobj, struct device, kobj);
154 struct dsa_switch *ds = dev_get_drvdata(dev);
155 struct dsa_switch_driver *drv = ds->drv;
156 umode_t mode = attr->mode;
157
158 if (index == 1) {
159 if (!drv->get_temp_limit)
160 mode = 0;
161 else if (drv->set_temp_limit)
162 mode |= S_IWUSR;
163 } else if (index == 2 && !drv->get_temp_alarm) {
164 mode = 0;
165 }
166 return mode;
167}
168
169static const struct attribute_group dsa_hwmon_group = {
170 .attrs = dsa_hwmon_attrs,
171 .is_visible = dsa_hwmon_attrs_visible,
172};
173__ATTRIBUTE_GROUPS(dsa_hwmon);
174
175#endif /* CONFIG_NET_DSA_HWMON */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176
177/* basic switch operations **************************************************/
178static struct dsa_switch *
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000179dsa_switch_setup(struct dsa_switch_tree *dst, int index,
Alexander Duyckb4d23942014-09-15 13:00:27 -0400180 struct device *parent, struct device *host_dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000182 struct dsa_chip_data *pd = dst->pd->chip + index;
183 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000184 struct dsa_switch *ds;
185 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000186 char *name;
187 int i;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000188 bool valid_name_found = false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000189
190 /*
191 * Probe for switch model.
192 */
Alexander Duyckb4d23942014-09-15 13:00:27 -0400193 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000194 if (drv == NULL) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000195 printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
196 dst->master_netdev->name, index);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000197 return ERR_PTR(-EINVAL);
198 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000199 printk(KERN_INFO "%s[%d]: detected a %s switch\n",
200 dst->master_netdev->name, index, name);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201
202
203 /*
204 * Allocate and initialise switch state.
205 */
206 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
207 if (ds == NULL)
208 return ERR_PTR(-ENOMEM);
209
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000210 ds->dst = dst;
211 ds->index = index;
212 ds->pd = dst->pd->chip + index;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213 ds->drv = drv;
Alexander Duyckb4d23942014-09-15 13:00:27 -0400214 ds->master_dev = host_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215
216 /*
217 * Validate supplied switch configuration.
218 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000219 for (i = 0; i < DSA_MAX_PORTS; i++) {
220 char *name;
221
222 name = pd->port_names[i];
223 if (name == NULL)
224 continue;
225
226 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000227 if (dst->cpu_switch != -1) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000228 printk(KERN_ERR "multiple cpu ports?!\n");
229 ret = -EINVAL;
230 goto out;
231 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000232 dst->cpu_switch = index;
233 dst->cpu_port = i;
234 } else if (!strcmp(name, "dsa")) {
235 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000236 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000237 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000239 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 }
241
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000242 if (!valid_name_found && i == DSA_MAX_PORTS) {
243 ret = -EINVAL;
244 goto out;
245 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700247 /* Make the built-in MII bus mask match the number of ports,
248 * switch drivers can override this later
249 */
250 ds->phys_mii_mask = ds->phys_port_mask;
251
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000252 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000253 * If the CPU connects to this switch, set the switch tree
254 * tagging protocol to the preferred tagging format of this
255 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256 */
Alexander Duyck50753142014-09-15 13:00:19 -0400257 if (dst->cpu_switch == index) {
258 switch (drv->tag_protocol) {
259#ifdef CONFIG_NET_DSA_TAG_DSA
260 case DSA_TAG_PROTO_DSA:
261 dst->rcv = dsa_netdev_ops.rcv;
262 break;
263#endif
264#ifdef CONFIG_NET_DSA_TAG_EDSA
265 case DSA_TAG_PROTO_EDSA:
266 dst->rcv = edsa_netdev_ops.rcv;
267 break;
268#endif
269#ifdef CONFIG_NET_DSA_TAG_TRAILER
270 case DSA_TAG_PROTO_TRAILER:
271 dst->rcv = trailer_netdev_ops.rcv;
272 break;
273#endif
274#ifdef CONFIG_NET_DSA_TAG_BRCM
275 case DSA_TAG_PROTO_BRCM:
276 dst->rcv = brcm_netdev_ops.rcv;
277 break;
278#endif
279 default:
280 break;
281 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282
Alexander Duyck50753142014-09-15 13:00:19 -0400283 dst->tag_protocol = drv->tag_protocol;
284 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000285
286 /*
287 * Do basic register setup.
288 */
289 ret = drv->setup(ds);
290 if (ret < 0)
291 goto out;
292
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000293 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000294 if (ret < 0)
295 goto out;
296
297 ds->slave_mii_bus = mdiobus_alloc();
298 if (ds->slave_mii_bus == NULL) {
299 ret = -ENOMEM;
300 goto out;
301 }
302 dsa_slave_mii_bus_init(ds);
303
304 ret = mdiobus_register(ds->slave_mii_bus);
305 if (ret < 0)
306 goto out_free;
307
308
309 /*
310 * Create network devices for physical switch ports.
311 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000312 for (i = 0; i < DSA_MAX_PORTS; i++) {
313 struct net_device *slave_dev;
314
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000315 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000316 continue;
317
318 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
319 if (slave_dev == NULL) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000320 printk(KERN_ERR "%s[%d]: can't create dsa "
321 "slave device for port %d(%s)\n",
322 dst->master_netdev->name,
323 index, i, pd->port_names[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000324 continue;
325 }
326
327 ds->ports[i] = slave_dev;
328 }
329
Guenter Roeck51579c32014-10-29 10:44:58 -0700330#ifdef CONFIG_NET_DSA_HWMON
331 /* If the switch provides a temperature sensor,
332 * register with hardware monitoring subsystem.
333 * Treat registration error as non-fatal and ignore it.
334 */
335 if (drv->get_temp) {
336 const char *netname = netdev_name(dst->master_netdev);
337 char hname[IFNAMSIZ + 1];
338 int i, j;
339
340 /* Create valid hwmon 'name' attribute */
341 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
342 if (isalnum(netname[i]))
343 hname[j++] = netname[i];
344 }
345 hname[j] = '\0';
346 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
347 hname, index);
348 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
349 ds->hwmon_name, ds, dsa_hwmon_groups);
350 if (IS_ERR(ds->hwmon_dev))
351 ds->hwmon_dev = NULL;
352 }
353#endif /* CONFIG_NET_DSA_HWMON */
354
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000355 return ds;
356
357out_free:
358 mdiobus_free(ds->slave_mii_bus);
359out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000360 kfree(ds);
361 return ERR_PTR(ret);
362}
363
364static void dsa_switch_destroy(struct dsa_switch *ds)
365{
Guenter Roeck51579c32014-10-29 10:44:58 -0700366#ifdef CONFIG_NET_DSA_HWMON
367 if (ds->hwmon_dev)
368 hwmon_device_unregister(ds->hwmon_dev);
369#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000370}
371
Thierry Redinge506d402014-10-01 13:59:00 +0200372#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700373static int dsa_switch_suspend(struct dsa_switch *ds)
374{
375 int i, ret = 0;
376
377 /* Suspend slave network devices */
378 for (i = 0; i < DSA_MAX_PORTS; i++) {
379 if (!(ds->phys_port_mask & (1 << i)))
380 continue;
381
382 ret = dsa_slave_suspend(ds->ports[i]);
383 if (ret)
384 return ret;
385 }
386
387 if (ds->drv->suspend)
388 ret = ds->drv->suspend(ds);
389
390 return ret;
391}
392
393static int dsa_switch_resume(struct dsa_switch *ds)
394{
395 int i, ret = 0;
396
397 if (ds->drv->resume)
398 ret = ds->drv->resume(ds);
399
400 if (ret)
401 return ret;
402
403 /* Resume slave network devices */
404 for (i = 0; i < DSA_MAX_PORTS; i++) {
405 if (!(ds->phys_port_mask & (1 << i)))
406 continue;
407
408 ret = dsa_slave_resume(ds->ports[i]);
409 if (ret)
410 return ret;
411 }
412
413 return 0;
414}
Thierry Redinge506d402014-10-01 13:59:00 +0200415#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700416
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000417
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000418/* link polling *************************************************************/
419static void dsa_link_poll_work(struct work_struct *ugly)
420{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000421 struct dsa_switch_tree *dst;
422 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000423
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000424 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000425
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000426 for (i = 0; i < dst->pd->nr_chips; i++) {
427 struct dsa_switch *ds = dst->ds[i];
428
429 if (ds != NULL && ds->drv->poll_link != NULL)
430 ds->drv->poll_link(ds);
431 }
432
433 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000434}
435
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000436static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000437{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000438 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000439
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000440 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000441}
442
443
444/* platform driver init and cleanup *****************************************/
445static int dev_is_class(struct device *dev, void *class)
446{
447 if (dev->class != NULL && !strcmp(dev->class->name, class))
448 return 1;
449
450 return 0;
451}
452
453static struct device *dev_find_class(struct device *parent, char *class)
454{
455 if (dev_is_class(parent, class)) {
456 get_device(parent);
457 return parent;
458 }
459
460 return device_find_child(parent, class, dev_is_class);
461}
462
Alexander Duyckb4d23942014-09-15 13:00:27 -0400463struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000464{
465 struct device *d;
466
467 d = dev_find_class(dev, "mdio_bus");
468 if (d != NULL) {
469 struct mii_bus *bus;
470
471 bus = to_mii_bus(d);
472 put_device(d);
473
474 return bus;
475 }
476
477 return NULL;
478}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400479EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000480
481static struct net_device *dev_to_net_device(struct device *dev)
482{
483 struct device *d;
484
485 d = dev_find_class(dev, "net");
486 if (d != NULL) {
487 struct net_device *nd;
488
489 nd = to_net_dev(d);
490 dev_hold(nd);
491 put_device(d);
492
493 return nd;
494 }
495
496 return NULL;
497}
498
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000499#ifdef CONFIG_OF
500static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
501 struct dsa_chip_data *cd,
502 int chip_index,
503 struct device_node *link)
504{
505 int ret;
506 const __be32 *reg;
507 int link_port_addr;
508 int link_sw_addr;
509 struct device_node *parent_sw;
510 int len;
511
512 parent_sw = of_get_parent(link);
513 if (!parent_sw)
514 return -EINVAL;
515
516 reg = of_get_property(parent_sw, "reg", &len);
517 if (!reg || (len != sizeof(*reg) * 2))
518 return -EINVAL;
519
520 link_sw_addr = be32_to_cpup(reg + 1);
521
522 if (link_sw_addr >= pd->nr_chips)
523 return -EINVAL;
524
525 /* First time routing table allocation */
526 if (!cd->rtable) {
527 cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL);
528 if (!cd->rtable)
529 return -ENOMEM;
530
531 /* default to no valid uplink/downlink */
532 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
533 }
534
535 reg = of_get_property(link, "reg", NULL);
536 if (!reg) {
537 ret = -EINVAL;
538 goto out;
539 }
540
541 link_port_addr = be32_to_cpup(reg);
542
543 cd->rtable[link_sw_addr] = link_port_addr;
544
545 return 0;
546out:
547 kfree(cd->rtable);
548 return ret;
549}
550
Florian Fainelli21168242013-03-25 05:03:39 +0000551static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
552{
553 int i;
554 int port_index;
555
556 for (i = 0; i < pd->nr_chips; i++) {
557 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000558 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200559 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000560 port_index++;
561 }
Florian Fainelli21168242013-03-25 05:03:39 +0000562 kfree(pd->chip[i].rtable);
563 }
564 kfree(pd->chip);
565}
566
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000567static int dsa_of_probe(struct platform_device *pdev)
568{
569 struct device_node *np = pdev->dev.of_node;
570 struct device_node *child, *mdio, *ethernet, *port, *link;
571 struct mii_bus *mdio_bus;
572 struct platform_device *ethernet_dev;
573 struct dsa_platform_data *pd;
574 struct dsa_chip_data *cd;
575 const char *port_name;
576 int chip_index, port_index;
577 const unsigned int *sw_addr, *port_reg;
Florian Fainelli21168242013-03-25 05:03:39 +0000578 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000579
580 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
581 if (!mdio)
582 return -EINVAL;
583
584 mdio_bus = of_mdio_find_bus(mdio);
585 if (!mdio_bus)
586 return -EINVAL;
587
588 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
589 if (!ethernet)
590 return -EINVAL;
591
592 ethernet_dev = of_find_device_by_node(ethernet);
593 if (!ethernet_dev)
594 return -ENODEV;
595
596 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
597 if (!pd)
598 return -ENOMEM;
599
600 pdev->dev.platform_data = pd;
601 pd->netdev = &ethernet_dev->dev;
602 pd->nr_chips = of_get_child_count(np);
603 if (pd->nr_chips > DSA_MAX_SWITCHES)
604 pd->nr_chips = DSA_MAX_SWITCHES;
605
606 pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data),
607 GFP_KERNEL);
608 if (!pd->chip) {
609 ret = -ENOMEM;
610 goto out_free;
611 }
612
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200613 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000614 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200615 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000616 cd = &pd->chip[chip_index];
617
Florian Fainellifa981d92014-08-27 17:04:49 -0700618 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700619 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000620
621 sw_addr = of_get_property(child, "reg", NULL);
622 if (!sw_addr)
623 continue;
624
625 cd->sw_addr = be32_to_cpup(sw_addr);
626 if (cd->sw_addr > PHY_MAX_ADDR)
627 continue;
628
629 for_each_available_child_of_node(child, port) {
630 port_reg = of_get_property(port, "reg", NULL);
631 if (!port_reg)
632 continue;
633
634 port_index = be32_to_cpup(port_reg);
635
636 port_name = of_get_property(port, "label", NULL);
637 if (!port_name)
638 continue;
639
Florian Fainellibd474972014-08-27 17:04:50 -0700640 cd->port_dn[port_index] = port;
641
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000642 cd->port_names[port_index] = kstrdup(port_name,
643 GFP_KERNEL);
644 if (!cd->port_names[port_index]) {
645 ret = -ENOMEM;
646 goto out_free_chip;
647 }
648
649 link = of_parse_phandle(port, "link", 0);
650
651 if (!strcmp(port_name, "dsa") && link &&
652 pd->nr_chips > 1) {
653 ret = dsa_of_setup_routing_table(pd, cd,
654 chip_index, link);
655 if (ret)
656 goto out_free_chip;
657 }
658
659 if (port_index == DSA_MAX_PORTS)
660 break;
661 }
662 }
663
664 return 0;
665
666out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000667 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000668out_free:
669 kfree(pd);
670 pdev->dev.platform_data = NULL;
671 return ret;
672}
673
674static void dsa_of_remove(struct platform_device *pdev)
675{
676 struct dsa_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000677
678 if (!pdev->dev.of_node)
679 return;
680
Florian Fainelli21168242013-03-25 05:03:39 +0000681 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000682 kfree(pd);
683}
684#else
685static inline int dsa_of_probe(struct platform_device *pdev)
686{
687 return 0;
688}
689
690static inline void dsa_of_remove(struct platform_device *pdev)
691{
692}
693#endif
694
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695static int dsa_probe(struct platform_device *pdev)
696{
697 static int dsa_version_printed;
698 struct dsa_platform_data *pd = pdev->dev.platform_data;
699 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000700 struct dsa_switch_tree *dst;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000701 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702
703 if (!dsa_version_printed++)
704 printk(KERN_NOTICE "Distributed Switch Architecture "
705 "driver version %s\n", dsa_driver_version);
706
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000707 if (pdev->dev.of_node) {
708 ret = dsa_of_probe(pdev);
709 if (ret)
710 return ret;
711
712 pd = pdev->dev.platform_data;
713 }
714
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000715 if (pd == NULL || pd->netdev == NULL)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000716 return -EINVAL;
717
718 dev = dev_to_net_device(pd->netdev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000719 if (dev == NULL) {
720 ret = -EINVAL;
721 goto out;
722 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000723
724 if (dev->dsa_ptr != NULL) {
725 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000726 ret = -EEXIST;
727 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000728 }
729
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000730 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
731 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000732 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000733 ret = -ENOMEM;
734 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000735 }
736
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000737 platform_set_drvdata(pdev, dst);
738
739 dst->pd = pd;
740 dst->master_netdev = dev;
741 dst->cpu_switch = -1;
742 dst->cpu_port = -1;
743
744 for (i = 0; i < pd->nr_chips; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000745 struct dsa_switch *ds;
746
Alexander Duyckb4d23942014-09-15 13:00:27 -0400747 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000748 if (IS_ERR(ds)) {
749 printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
750 "instance (error %ld)\n", dev->name, i,
751 PTR_ERR(ds));
752 continue;
753 }
754
755 dst->ds[i] = ds;
756 if (ds->drv->poll_link != NULL)
757 dst->link_poll_needed = 1;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000758 }
759
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000760 /*
761 * If we use a tagging format that doesn't have an ethertype
762 * field, make sure that all packets from this point on get
763 * sent to the tag format's receive function.
764 */
765 wmb();
766 dev->dsa_ptr = (void *)dst;
767
768 if (dst->link_poll_needed) {
769 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
770 init_timer(&dst->link_poll_timer);
771 dst->link_poll_timer.data = (unsigned long)dst;
772 dst->link_poll_timer.function = dsa_link_poll_timer;
773 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
774 add_timer(&dst->link_poll_timer);
775 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000776
777 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000778
779out:
780 dsa_of_remove(pdev);
781
782 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000783}
784
785static int dsa_remove(struct platform_device *pdev)
786{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000787 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
788 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000790 if (dst->link_poll_needed)
791 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000792
Tejun Heo43829732012-08-20 14:51:24 -0700793 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000795 for (i = 0; i < dst->pd->nr_chips; i++) {
796 struct dsa_switch *ds = dst->ds[i];
797
798 if (ds != NULL)
799 dsa_switch_destroy(ds);
800 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000802 dsa_of_remove(pdev);
803
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000804 return 0;
805}
806
807static void dsa_shutdown(struct platform_device *pdev)
808{
809}
810
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700811static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
812 struct packet_type *pt, struct net_device *orig_dev)
813{
814 struct dsa_switch_tree *dst = dev->dsa_ptr;
815
816 if (unlikely(dst == NULL)) {
817 kfree_skb(skb);
818 return 0;
819 }
820
Alexander Duyck50753142014-09-15 13:00:19 -0400821 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700822}
823
Florian Fainelli61b73632014-08-29 12:42:07 -0700824static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700825 .type = cpu_to_be16(ETH_P_XDSA),
826 .func = dsa_switch_rcv,
827};
828
Florian Fainelli24462542014-09-18 17:31:22 -0700829#ifdef CONFIG_PM_SLEEP
830static int dsa_suspend(struct device *d)
831{
832 struct platform_device *pdev = to_platform_device(d);
833 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
834 int i, ret = 0;
835
836 for (i = 0; i < dst->pd->nr_chips; i++) {
837 struct dsa_switch *ds = dst->ds[i];
838
839 if (ds != NULL)
840 ret = dsa_switch_suspend(ds);
841 }
842
843 return ret;
844}
845
846static int dsa_resume(struct device *d)
847{
848 struct platform_device *pdev = to_platform_device(d);
849 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
850 int i, ret = 0;
851
852 for (i = 0; i < dst->pd->nr_chips; i++) {
853 struct dsa_switch *ds = dst->ds[i];
854
855 if (ds != NULL)
856 ret = dsa_switch_resume(ds);
857 }
858
859 return ret;
860}
861#endif
862
863static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
864
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000865static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700866 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000867 { .compatible = "marvell,dsa", },
868 {}
869};
870MODULE_DEVICE_TABLE(of, dsa_of_match_table);
871
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000872static struct platform_driver dsa_driver = {
873 .probe = dsa_probe,
874 .remove = dsa_remove,
875 .shutdown = dsa_shutdown,
876 .driver = {
877 .name = "dsa",
878 .owner = THIS_MODULE,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000879 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700880 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000881 },
882};
883
884static int __init dsa_init_module(void)
885{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000886 int rc;
887
888 rc = platform_driver_register(&dsa_driver);
889 if (rc)
890 return rc;
891
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700892 dev_add_pack(&dsa_pack_type);
893
Ben Hutchings7df899c2011-11-25 14:35:02 +0000894 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000895}
896module_init(dsa_init_module);
897
898static void __exit dsa_cleanup_module(void)
899{
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700900 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000901 platform_driver_unregister(&dsa_driver);
902}
903module_exit(dsa_cleanup_module);
904
Rusty Russell577d6a72011-01-24 14:32:52 -0600905MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000906MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
907MODULE_LICENSE("GPL");
908MODULE_ALIAS("platform:dsa");