blob: 6f02ccc57593fe76adb12b985fcfdb6f7423e0c4 [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 **************************************************/
Florian Fainellidf197192015-03-05 12:35:06 -0800178static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000179{
Florian Fainellidf197192015-03-05 12:35:06 -0800180 struct dsa_switch_driver *drv = ds->drv;
181 struct dsa_switch_tree *dst = ds->dst;
182 struct dsa_chip_data *pd = ds->pd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000183 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800184 int index = ds->index;
185 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000186
187 /*
188 * Validate supplied switch configuration.
189 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000190 for (i = 0; i < DSA_MAX_PORTS; i++) {
191 char *name;
192
193 name = pd->port_names[i];
194 if (name == NULL)
195 continue;
196
197 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000198 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800199 netdev_err(dst->master_netdev,
200 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201 ret = -EINVAL;
202 goto out;
203 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000204 dst->cpu_switch = index;
205 dst->cpu_port = i;
206 } else if (!strcmp(name, "dsa")) {
207 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000208 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000209 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000210 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000211 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212 }
213
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000214 if (!valid_name_found && i == DSA_MAX_PORTS) {
215 ret = -EINVAL;
216 goto out;
217 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700219 /* Make the built-in MII bus mask match the number of ports,
220 * switch drivers can override this later
221 */
222 ds->phys_mii_mask = ds->phys_port_mask;
223
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000224 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000225 * If the CPU connects to this switch, set the switch tree
226 * tagging protocol to the preferred tagging format of this
227 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000228 */
Alexander Duyck50753142014-09-15 13:00:19 -0400229 if (dst->cpu_switch == index) {
230 switch (drv->tag_protocol) {
231#ifdef CONFIG_NET_DSA_TAG_DSA
232 case DSA_TAG_PROTO_DSA:
233 dst->rcv = dsa_netdev_ops.rcv;
234 break;
235#endif
236#ifdef CONFIG_NET_DSA_TAG_EDSA
237 case DSA_TAG_PROTO_EDSA:
238 dst->rcv = edsa_netdev_ops.rcv;
239 break;
240#endif
241#ifdef CONFIG_NET_DSA_TAG_TRAILER
242 case DSA_TAG_PROTO_TRAILER:
243 dst->rcv = trailer_netdev_ops.rcv;
244 break;
245#endif
246#ifdef CONFIG_NET_DSA_TAG_BRCM
247 case DSA_TAG_PROTO_BRCM:
248 dst->rcv = brcm_netdev_ops.rcv;
249 break;
250#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200251 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400252 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200253 default:
254 ret = -ENOPROTOOPT;
255 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400256 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257
Alexander Duyck50753142014-09-15 13:00:19 -0400258 dst->tag_protocol = drv->tag_protocol;
259 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000260
261 /*
262 * Do basic register setup.
263 */
264 ret = drv->setup(ds);
265 if (ret < 0)
266 goto out;
267
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000268 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000269 if (ret < 0)
270 goto out;
271
272 ds->slave_mii_bus = mdiobus_alloc();
273 if (ds->slave_mii_bus == NULL) {
274 ret = -ENOMEM;
275 goto out;
276 }
277 dsa_slave_mii_bus_init(ds);
278
279 ret = mdiobus_register(ds->slave_mii_bus);
280 if (ret < 0)
281 goto out_free;
282
283
284 /*
285 * Create network devices for physical switch ports.
286 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000287 for (i = 0; i < DSA_MAX_PORTS; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000288 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000289 continue;
290
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800291 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
292 if (ret < 0) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800293 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
294 index, i, pd->port_names[i]);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800295 ret = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000296 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297 }
298
Guenter Roeck51579c32014-10-29 10:44:58 -0700299#ifdef CONFIG_NET_DSA_HWMON
300 /* If the switch provides a temperature sensor,
301 * register with hardware monitoring subsystem.
302 * Treat registration error as non-fatal and ignore it.
303 */
304 if (drv->get_temp) {
305 const char *netname = netdev_name(dst->master_netdev);
306 char hname[IFNAMSIZ + 1];
307 int i, j;
308
309 /* Create valid hwmon 'name' attribute */
310 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
311 if (isalnum(netname[i]))
312 hname[j++] = netname[i];
313 }
314 hname[j] = '\0';
315 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
316 hname, index);
317 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
318 ds->hwmon_name, ds, dsa_hwmon_groups);
319 if (IS_ERR(ds->hwmon_dev))
320 ds->hwmon_dev = NULL;
321 }
322#endif /* CONFIG_NET_DSA_HWMON */
323
Florian Fainellidf197192015-03-05 12:35:06 -0800324 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000325
326out_free:
327 mdiobus_free(ds->slave_mii_bus);
328out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000329 kfree(ds);
Florian Fainellidf197192015-03-05 12:35:06 -0800330 return ret;
331}
332
333static struct dsa_switch *
334dsa_switch_setup(struct dsa_switch_tree *dst, int index,
335 struct device *parent, struct device *host_dev)
336{
337 struct dsa_chip_data *pd = dst->pd->chip + index;
338 struct dsa_switch_driver *drv;
339 struct dsa_switch *ds;
340 int ret;
341 char *name;
342
343 /*
344 * Probe for switch model.
345 */
346 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
347 if (drv == NULL) {
348 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
349 index);
350 return ERR_PTR(-EINVAL);
351 }
352 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
353 index, name);
354
355
356 /*
357 * Allocate and initialise switch state.
358 */
359 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
360 if (ds == NULL)
361 return NULL;
362
363 ds->dst = dst;
364 ds->index = index;
365 ds->pd = pd;
366 ds->drv = drv;
367 ds->master_dev = host_dev;
368
369 ret = dsa_switch_setup_one(ds, parent);
370 if (ret)
371 return NULL;
372
373 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000374}
375
376static void dsa_switch_destroy(struct dsa_switch *ds)
377{
Guenter Roeck51579c32014-10-29 10:44:58 -0700378#ifdef CONFIG_NET_DSA_HWMON
379 if (ds->hwmon_dev)
380 hwmon_device_unregister(ds->hwmon_dev);
381#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000382}
383
Thierry Redinge506d402014-10-01 13:59:00 +0200384#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700385static int dsa_switch_suspend(struct dsa_switch *ds)
386{
387 int i, ret = 0;
388
389 /* Suspend slave network devices */
390 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800391 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700392 continue;
393
394 ret = dsa_slave_suspend(ds->ports[i]);
395 if (ret)
396 return ret;
397 }
398
399 if (ds->drv->suspend)
400 ret = ds->drv->suspend(ds);
401
402 return ret;
403}
404
405static int dsa_switch_resume(struct dsa_switch *ds)
406{
407 int i, ret = 0;
408
409 if (ds->drv->resume)
410 ret = ds->drv->resume(ds);
411
412 if (ret)
413 return ret;
414
415 /* Resume slave network devices */
416 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800417 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700418 continue;
419
420 ret = dsa_slave_resume(ds->ports[i]);
421 if (ret)
422 return ret;
423 }
424
425 return 0;
426}
Thierry Redinge506d402014-10-01 13:59:00 +0200427#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700428
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000429
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000430/* link polling *************************************************************/
431static void dsa_link_poll_work(struct work_struct *ugly)
432{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000433 struct dsa_switch_tree *dst;
434 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000435
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000436 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000437
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000438 for (i = 0; i < dst->pd->nr_chips; i++) {
439 struct dsa_switch *ds = dst->ds[i];
440
441 if (ds != NULL && ds->drv->poll_link != NULL)
442 ds->drv->poll_link(ds);
443 }
444
445 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000446}
447
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000448static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000449{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000450 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000451
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000452 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000453}
454
455
456/* platform driver init and cleanup *****************************************/
457static int dev_is_class(struct device *dev, void *class)
458{
459 if (dev->class != NULL && !strcmp(dev->class->name, class))
460 return 1;
461
462 return 0;
463}
464
465static struct device *dev_find_class(struct device *parent, char *class)
466{
467 if (dev_is_class(parent, class)) {
468 get_device(parent);
469 return parent;
470 }
471
472 return device_find_child(parent, class, dev_is_class);
473}
474
Alexander Duyckb4d23942014-09-15 13:00:27 -0400475struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000476{
477 struct device *d;
478
479 d = dev_find_class(dev, "mdio_bus");
480 if (d != NULL) {
481 struct mii_bus *bus;
482
483 bus = to_mii_bus(d);
484 put_device(d);
485
486 return bus;
487 }
488
489 return NULL;
490}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400491EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000492
493static struct net_device *dev_to_net_device(struct device *dev)
494{
495 struct device *d;
496
497 d = dev_find_class(dev, "net");
498 if (d != NULL) {
499 struct net_device *nd;
500
501 nd = to_net_dev(d);
502 dev_hold(nd);
503 put_device(d);
504
505 return nd;
506 }
507
508 return NULL;
509}
510
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000511#ifdef CONFIG_OF
512static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
513 struct dsa_chip_data *cd,
514 int chip_index,
515 struct device_node *link)
516{
517 int ret;
518 const __be32 *reg;
519 int link_port_addr;
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
532 link_sw_addr = be32_to_cpup(reg + 1);
533
534 if (link_sw_addr >= pd->nr_chips)
535 return -EINVAL;
536
537 /* First time routing table allocation */
538 if (!cd->rtable) {
Fabian Frederick5bc4b462014-11-14 19:36:42 +0100539 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
540 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000541 if (!cd->rtable)
542 return -ENOMEM;
543
544 /* default to no valid uplink/downlink */
545 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
546 }
547
548 reg = of_get_property(link, "reg", NULL);
549 if (!reg) {
550 ret = -EINVAL;
551 goto out;
552 }
553
554 link_port_addr = be32_to_cpup(reg);
555
556 cd->rtable[link_sw_addr] = link_port_addr;
557
558 return 0;
559out:
560 kfree(cd->rtable);
561 return ret;
562}
563
Florian Fainelli21168242013-03-25 05:03:39 +0000564static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
565{
566 int i;
567 int port_index;
568
569 for (i = 0; i < pd->nr_chips; i++) {
570 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000571 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200572 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000573 port_index++;
574 }
Florian Fainelli21168242013-03-25 05:03:39 +0000575 kfree(pd->chip[i].rtable);
576 }
577 kfree(pd->chip);
578}
579
Florian Fainellif1a26a02015-03-05 12:35:04 -0800580static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000581{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800582 struct device_node *np = dev->of_node;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000583 struct device_node *child, *mdio, *ethernet, *port, *link;
584 struct mii_bus *mdio_bus;
585 struct platform_device *ethernet_dev;
586 struct dsa_platform_data *pd;
587 struct dsa_chip_data *cd;
588 const char *port_name;
589 int chip_index, port_index;
590 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700591 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000592 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000593
594 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
595 if (!mdio)
596 return -EINVAL;
597
598 mdio_bus = of_mdio_find_bus(mdio);
599 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800600 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000601
602 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
603 if (!ethernet)
604 return -EINVAL;
605
606 ethernet_dev = of_find_device_by_node(ethernet);
607 if (!ethernet_dev)
Florian Fainellib324c072015-03-05 12:35:05 -0800608 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000609
610 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
611 if (!pd)
612 return -ENOMEM;
613
Florian Fainellif1a26a02015-03-05 12:35:04 -0800614 dev->platform_data = pd;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000615 pd->netdev = &ethernet_dev->dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100616 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000617 if (pd->nr_chips > DSA_MAX_SWITCHES)
618 pd->nr_chips = DSA_MAX_SWITCHES;
619
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100620 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
621 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000622 if (!pd->chip) {
623 ret = -ENOMEM;
624 goto out_free;
625 }
626
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200627 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000628 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200629 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000630 cd = &pd->chip[chip_index];
631
Florian Fainellifa981d92014-08-27 17:04:49 -0700632 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700633 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000634
635 sw_addr = of_get_property(child, "reg", NULL);
636 if (!sw_addr)
637 continue;
638
639 cd->sw_addr = be32_to_cpup(sw_addr);
640 if (cd->sw_addr > PHY_MAX_ADDR)
641 continue;
642
Guenter Roeck6793abb2014-10-29 10:45:01 -0700643 if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
644 cd->eeprom_len = eeprom_len;
645
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000646 for_each_available_child_of_node(child, port) {
647 port_reg = of_get_property(port, "reg", NULL);
648 if (!port_reg)
649 continue;
650
651 port_index = be32_to_cpup(port_reg);
652
653 port_name = of_get_property(port, "label", NULL);
654 if (!port_name)
655 continue;
656
Florian Fainellibd474972014-08-27 17:04:50 -0700657 cd->port_dn[port_index] = port;
658
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000659 cd->port_names[port_index] = kstrdup(port_name,
660 GFP_KERNEL);
661 if (!cd->port_names[port_index]) {
662 ret = -ENOMEM;
663 goto out_free_chip;
664 }
665
666 link = of_parse_phandle(port, "link", 0);
667
668 if (!strcmp(port_name, "dsa") && link &&
669 pd->nr_chips > 1) {
670 ret = dsa_of_setup_routing_table(pd, cd,
671 chip_index, link);
672 if (ret)
673 goto out_free_chip;
674 }
675
676 if (port_index == DSA_MAX_PORTS)
677 break;
678 }
679 }
680
681 return 0;
682
683out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000684 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000685out_free:
686 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800687 dev->platform_data = NULL;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000688 return ret;
689}
690
Florian Fainellif1a26a02015-03-05 12:35:04 -0800691static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000692{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800693 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000694
Florian Fainellif1a26a02015-03-05 12:35:04 -0800695 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000696 return;
697
Florian Fainelli21168242013-03-25 05:03:39 +0000698 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000699 kfree(pd);
700}
701#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800702static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000703{
704 return 0;
705}
706
Florian Fainellif1a26a02015-03-05 12:35:04 -0800707static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000708{
709}
710#endif
711
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000712static int dsa_probe(struct platform_device *pdev)
713{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000714 struct dsa_platform_data *pd = pdev->dev.platform_data;
715 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000716 struct dsa_switch_tree *dst;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000717 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000718
Joe Perchesa2ae6002014-11-09 16:32:46 -0800719 pr_notice_once("Distributed Switch Architecture driver version %s\n",
720 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000721
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000722 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800723 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000724 if (ret)
725 return ret;
726
727 pd = pdev->dev.platform_data;
728 }
729
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000730 if (pd == NULL || pd->netdev == NULL)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000731 return -EINVAL;
732
733 dev = dev_to_net_device(pd->netdev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000734 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800735 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000736 goto out;
737 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000738
739 if (dev->dsa_ptr != NULL) {
740 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000741 ret = -EEXIST;
742 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000743 }
744
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000745 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
746 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000747 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000748 ret = -ENOMEM;
749 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000750 }
751
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000752 platform_set_drvdata(pdev, dst);
753
754 dst->pd = pd;
755 dst->master_netdev = dev;
756 dst->cpu_switch = -1;
757 dst->cpu_port = -1;
758
759 for (i = 0; i < pd->nr_chips; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000760 struct dsa_switch *ds;
761
Alexander Duyckb4d23942014-09-15 13:00:27 -0400762 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000763 if (IS_ERR(ds)) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800764 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
765 i, PTR_ERR(ds));
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000766 continue;
767 }
768
769 dst->ds[i] = ds;
770 if (ds->drv->poll_link != NULL)
771 dst->link_poll_needed = 1;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000772 }
773
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000774 /*
775 * If we use a tagging format that doesn't have an ethertype
776 * field, make sure that all packets from this point on get
777 * sent to the tag format's receive function.
778 */
779 wmb();
780 dev->dsa_ptr = (void *)dst;
781
782 if (dst->link_poll_needed) {
783 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
784 init_timer(&dst->link_poll_timer);
785 dst->link_poll_timer.data = (unsigned long)dst;
786 dst->link_poll_timer.function = dsa_link_poll_timer;
787 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
788 add_timer(&dst->link_poll_timer);
789 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000790
791 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000792
793out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800794 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000795
796 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000797}
798
799static int dsa_remove(struct platform_device *pdev)
800{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000801 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
802 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000804 if (dst->link_poll_needed)
805 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000806
Tejun Heo43829732012-08-20 14:51:24 -0700807 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000808
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000809 for (i = 0; i < dst->pd->nr_chips; i++) {
810 struct dsa_switch *ds = dst->ds[i];
811
812 if (ds != NULL)
813 dsa_switch_destroy(ds);
814 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000815
Florian Fainellif1a26a02015-03-05 12:35:04 -0800816 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000817
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000818 return 0;
819}
820
821static void dsa_shutdown(struct platform_device *pdev)
822{
823}
824
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700825static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
826 struct packet_type *pt, struct net_device *orig_dev)
827{
828 struct dsa_switch_tree *dst = dev->dsa_ptr;
829
830 if (unlikely(dst == NULL)) {
831 kfree_skb(skb);
832 return 0;
833 }
834
Alexander Duyck50753142014-09-15 13:00:19 -0400835 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700836}
837
Florian Fainelli61b73632014-08-29 12:42:07 -0700838static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700839 .type = cpu_to_be16(ETH_P_XDSA),
840 .func = dsa_switch_rcv,
841};
842
Florian Fainellib73adef2015-02-24 13:15:33 -0800843static struct notifier_block dsa_netdevice_nb __read_mostly = {
844 .notifier_call = dsa_slave_netdevice_event,
845};
846
Florian Fainelli24462542014-09-18 17:31:22 -0700847#ifdef CONFIG_PM_SLEEP
848static int dsa_suspend(struct device *d)
849{
850 struct platform_device *pdev = to_platform_device(d);
851 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
852 int i, ret = 0;
853
854 for (i = 0; i < dst->pd->nr_chips; i++) {
855 struct dsa_switch *ds = dst->ds[i];
856
857 if (ds != NULL)
858 ret = dsa_switch_suspend(ds);
859 }
860
861 return ret;
862}
863
864static int dsa_resume(struct device *d)
865{
866 struct platform_device *pdev = to_platform_device(d);
867 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
868 int i, ret = 0;
869
870 for (i = 0; i < dst->pd->nr_chips; i++) {
871 struct dsa_switch *ds = dst->ds[i];
872
873 if (ds != NULL)
874 ret = dsa_switch_resume(ds);
875 }
876
877 return ret;
878}
879#endif
880
881static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
882
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000883static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700884 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000885 { .compatible = "marvell,dsa", },
886 {}
887};
888MODULE_DEVICE_TABLE(of, dsa_of_match_table);
889
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000890static struct platform_driver dsa_driver = {
891 .probe = dsa_probe,
892 .remove = dsa_remove,
893 .shutdown = dsa_shutdown,
894 .driver = {
895 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000896 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700897 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000898 },
899};
900
901static int __init dsa_init_module(void)
902{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000903 int rc;
904
Florian Fainellib73adef2015-02-24 13:15:33 -0800905 register_netdevice_notifier(&dsa_netdevice_nb);
906
Ben Hutchings7df899c2011-11-25 14:35:02 +0000907 rc = platform_driver_register(&dsa_driver);
908 if (rc)
909 return rc;
910
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700911 dev_add_pack(&dsa_pack_type);
912
Ben Hutchings7df899c2011-11-25 14:35:02 +0000913 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000914}
915module_init(dsa_init_module);
916
917static void __exit dsa_cleanup_module(void)
918{
Florian Fainellib73adef2015-02-24 13:15:33 -0800919 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700920 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000921 platform_driver_unregister(&dsa_driver);
922}
923module_exit(dsa_cleanup_module);
924
Rusty Russell577d6a72011-01-24 14:32:52 -0600925MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000926MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
927MODULE_LICENSE("GPL");
928MODULE_ALIAS("platform:dsa");