blob: 4dea2e0681d16409016ae09616127b22bbab3a1e [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) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800195 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
196 index);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000197 return ERR_PTR(-EINVAL);
198 }
Joe Perchesa2ae6002014-11-09 16:32:46 -0800199 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
200 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) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800228 netdev_err(dst->master_netdev,
229 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230 ret = -EINVAL;
231 goto out;
232 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000233 dst->cpu_switch = index;
234 dst->cpu_port = i;
235 } else if (!strcmp(name, "dsa")) {
236 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000237 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000238 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000239 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000240 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000241 }
242
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000243 if (!valid_name_found && i == DSA_MAX_PORTS) {
244 ret = -EINVAL;
245 goto out;
246 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000247
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700248 /* Make the built-in MII bus mask match the number of ports,
249 * switch drivers can override this later
250 */
251 ds->phys_mii_mask = ds->phys_port_mask;
252
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000253 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000254 * If the CPU connects to this switch, set the switch tree
255 * tagging protocol to the preferred tagging format of this
256 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257 */
Alexander Duyck50753142014-09-15 13:00:19 -0400258 if (dst->cpu_switch == index) {
259 switch (drv->tag_protocol) {
260#ifdef CONFIG_NET_DSA_TAG_DSA
261 case DSA_TAG_PROTO_DSA:
262 dst->rcv = dsa_netdev_ops.rcv;
263 break;
264#endif
265#ifdef CONFIG_NET_DSA_TAG_EDSA
266 case DSA_TAG_PROTO_EDSA:
267 dst->rcv = edsa_netdev_ops.rcv;
268 break;
269#endif
270#ifdef CONFIG_NET_DSA_TAG_TRAILER
271 case DSA_TAG_PROTO_TRAILER:
272 dst->rcv = trailer_netdev_ops.rcv;
273 break;
274#endif
275#ifdef CONFIG_NET_DSA_TAG_BRCM
276 case DSA_TAG_PROTO_BRCM:
277 dst->rcv = brcm_netdev_ops.rcv;
278 break;
279#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200280 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400281 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200282 default:
283 ret = -ENOPROTOOPT;
284 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400285 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000286
Alexander Duyck50753142014-09-15 13:00:19 -0400287 dst->tag_protocol = drv->tag_protocol;
288 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000289
290 /*
291 * Do basic register setup.
292 */
293 ret = drv->setup(ds);
294 if (ret < 0)
295 goto out;
296
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000297 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298 if (ret < 0)
299 goto out;
300
301 ds->slave_mii_bus = mdiobus_alloc();
302 if (ds->slave_mii_bus == NULL) {
303 ret = -ENOMEM;
304 goto out;
305 }
306 dsa_slave_mii_bus_init(ds);
307
308 ret = mdiobus_register(ds->slave_mii_bus);
309 if (ret < 0)
310 goto out_free;
311
312
313 /*
314 * Create network devices for physical switch ports.
315 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000316 for (i = 0; i < DSA_MAX_PORTS; i++) {
317 struct net_device *slave_dev;
318
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000319 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000320 continue;
321
322 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
323 if (slave_dev == NULL) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800324 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
325 index, i, pd->port_names[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000326 continue;
327 }
328
329 ds->ports[i] = slave_dev;
330 }
331
Guenter Roeck51579c32014-10-29 10:44:58 -0700332#ifdef CONFIG_NET_DSA_HWMON
333 /* If the switch provides a temperature sensor,
334 * register with hardware monitoring subsystem.
335 * Treat registration error as non-fatal and ignore it.
336 */
337 if (drv->get_temp) {
338 const char *netname = netdev_name(dst->master_netdev);
339 char hname[IFNAMSIZ + 1];
340 int i, j;
341
342 /* Create valid hwmon 'name' attribute */
343 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
344 if (isalnum(netname[i]))
345 hname[j++] = netname[i];
346 }
347 hname[j] = '\0';
348 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
349 hname, index);
350 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
351 ds->hwmon_name, ds, dsa_hwmon_groups);
352 if (IS_ERR(ds->hwmon_dev))
353 ds->hwmon_dev = NULL;
354 }
355#endif /* CONFIG_NET_DSA_HWMON */
356
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000357 return ds;
358
359out_free:
360 mdiobus_free(ds->slave_mii_bus);
361out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000362 kfree(ds);
363 return ERR_PTR(ret);
364}
365
366static void dsa_switch_destroy(struct dsa_switch *ds)
367{
Guenter Roeck51579c32014-10-29 10:44:58 -0700368#ifdef CONFIG_NET_DSA_HWMON
369 if (ds->hwmon_dev)
370 hwmon_device_unregister(ds->hwmon_dev);
371#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000372}
373
Thierry Redinge506d402014-10-01 13:59:00 +0200374#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700375static int dsa_switch_suspend(struct dsa_switch *ds)
376{
377 int i, ret = 0;
378
379 /* Suspend slave network devices */
380 for (i = 0; i < DSA_MAX_PORTS; i++) {
381 if (!(ds->phys_port_mask & (1 << i)))
382 continue;
383
384 ret = dsa_slave_suspend(ds->ports[i]);
385 if (ret)
386 return ret;
387 }
388
389 if (ds->drv->suspend)
390 ret = ds->drv->suspend(ds);
391
392 return ret;
393}
394
395static int dsa_switch_resume(struct dsa_switch *ds)
396{
397 int i, ret = 0;
398
399 if (ds->drv->resume)
400 ret = ds->drv->resume(ds);
401
402 if (ret)
403 return ret;
404
405 /* Resume slave network devices */
406 for (i = 0; i < DSA_MAX_PORTS; i++) {
407 if (!(ds->phys_port_mask & (1 << i)))
408 continue;
409
410 ret = dsa_slave_resume(ds->ports[i]);
411 if (ret)
412 return ret;
413 }
414
415 return 0;
416}
Thierry Redinge506d402014-10-01 13:59:00 +0200417#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700418
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000419
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000420/* link polling *************************************************************/
421static void dsa_link_poll_work(struct work_struct *ugly)
422{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000423 struct dsa_switch_tree *dst;
424 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000425
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000426 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000427
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000428 for (i = 0; i < dst->pd->nr_chips; i++) {
429 struct dsa_switch *ds = dst->ds[i];
430
431 if (ds != NULL && ds->drv->poll_link != NULL)
432 ds->drv->poll_link(ds);
433 }
434
435 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000436}
437
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000438static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000439{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000440 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000441
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000442 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000443}
444
445
446/* platform driver init and cleanup *****************************************/
447static int dev_is_class(struct device *dev, void *class)
448{
449 if (dev->class != NULL && !strcmp(dev->class->name, class))
450 return 1;
451
452 return 0;
453}
454
455static struct device *dev_find_class(struct device *parent, char *class)
456{
457 if (dev_is_class(parent, class)) {
458 get_device(parent);
459 return parent;
460 }
461
462 return device_find_child(parent, class, dev_is_class);
463}
464
Alexander Duyckb4d23942014-09-15 13:00:27 -0400465struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000466{
467 struct device *d;
468
469 d = dev_find_class(dev, "mdio_bus");
470 if (d != NULL) {
471 struct mii_bus *bus;
472
473 bus = to_mii_bus(d);
474 put_device(d);
475
476 return bus;
477 }
478
479 return NULL;
480}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400481EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000482
483static struct net_device *dev_to_net_device(struct device *dev)
484{
485 struct device *d;
486
487 d = dev_find_class(dev, "net");
488 if (d != NULL) {
489 struct net_device *nd;
490
491 nd = to_net_dev(d);
492 dev_hold(nd);
493 put_device(d);
494
495 return nd;
496 }
497
498 return NULL;
499}
500
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000501#ifdef CONFIG_OF
502static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
503 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300504 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000505 struct device_node *link)
506{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000507 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000508 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
Pavel Nakonechny30303812015-04-05 00:46:21 +0300520 /*
521 * Get the destination switch number from the second field of its 'reg'
522 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
523 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000524 link_sw_addr = be32_to_cpup(reg + 1);
525
526 if (link_sw_addr >= pd->nr_chips)
527 return -EINVAL;
528
529 /* First time routing table allocation */
530 if (!cd->rtable) {
Fabian Frederick5bc4b462014-11-14 19:36:42 +0100531 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
532 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000533 if (!cd->rtable)
534 return -ENOMEM;
535
536 /* default to no valid uplink/downlink */
537 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
538 }
539
Pavel Nakonechny30303812015-04-05 00:46:21 +0300540 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000541
542 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000543}
544
Florian Fainelli21168242013-03-25 05:03:39 +0000545static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
546{
547 int i;
548 int port_index;
549
550 for (i = 0; i < pd->nr_chips; i++) {
551 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000552 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200553 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000554 port_index++;
555 }
Florian Fainelli21168242013-03-25 05:03:39 +0000556 kfree(pd->chip[i].rtable);
557 }
558 kfree(pd->chip);
559}
560
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000561static int dsa_of_probe(struct platform_device *pdev)
562{
563 struct device_node *np = pdev->dev.of_node;
564 struct device_node *child, *mdio, *ethernet, *port, *link;
565 struct mii_bus *mdio_bus;
566 struct platform_device *ethernet_dev;
567 struct dsa_platform_data *pd;
568 struct dsa_chip_data *cd;
569 const char *port_name;
570 int chip_index, port_index;
571 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700572 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000573 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000574
575 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
576 if (!mdio)
577 return -EINVAL;
578
579 mdio_bus = of_mdio_find_bus(mdio);
580 if (!mdio_bus)
581 return -EINVAL;
582
583 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
584 if (!ethernet)
585 return -EINVAL;
586
587 ethernet_dev = of_find_device_by_node(ethernet);
588 if (!ethernet_dev)
589 return -ENODEV;
590
591 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
592 if (!pd)
593 return -ENOMEM;
594
595 pdev->dev.platform_data = pd;
596 pd->netdev = &ethernet_dev->dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100597 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000598 if (pd->nr_chips > DSA_MAX_SWITCHES)
599 pd->nr_chips = DSA_MAX_SWITCHES;
600
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100601 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
602 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000603 if (!pd->chip) {
604 ret = -ENOMEM;
605 goto out_free;
606 }
607
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200608 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000609 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200610 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000611 cd = &pd->chip[chip_index];
612
Florian Fainellifa981d92014-08-27 17:04:49 -0700613 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700614 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000615
616 sw_addr = of_get_property(child, "reg", NULL);
617 if (!sw_addr)
618 continue;
619
620 cd->sw_addr = be32_to_cpup(sw_addr);
621 if (cd->sw_addr > PHY_MAX_ADDR)
622 continue;
623
Guenter Roeck6793abb2014-10-29 10:45:01 -0700624 if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
625 cd->eeprom_len = eeprom_len;
626
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000627 for_each_available_child_of_node(child, port) {
628 port_reg = of_get_property(port, "reg", NULL);
629 if (!port_reg)
630 continue;
631
632 port_index = be32_to_cpup(port_reg);
633
634 port_name = of_get_property(port, "label", NULL);
635 if (!port_name)
636 continue;
637
Florian Fainellibd474972014-08-27 17:04:50 -0700638 cd->port_dn[port_index] = port;
639
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000640 cd->port_names[port_index] = kstrdup(port_name,
641 GFP_KERNEL);
642 if (!cd->port_names[port_index]) {
643 ret = -ENOMEM;
644 goto out_free_chip;
645 }
646
647 link = of_parse_phandle(port, "link", 0);
648
649 if (!strcmp(port_name, "dsa") && link &&
650 pd->nr_chips > 1) {
651 ret = dsa_of_setup_routing_table(pd, cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300652 chip_index, port_index, link);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000653 if (ret)
654 goto out_free_chip;
655 }
656
657 if (port_index == DSA_MAX_PORTS)
658 break;
659 }
660 }
661
662 return 0;
663
664out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000665 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000666out_free:
667 kfree(pd);
668 pdev->dev.platform_data = NULL;
669 return ret;
670}
671
672static void dsa_of_remove(struct platform_device *pdev)
673{
674 struct dsa_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000675
676 if (!pdev->dev.of_node)
677 return;
678
Florian Fainelli21168242013-03-25 05:03:39 +0000679 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000680 kfree(pd);
681}
682#else
683static inline int dsa_of_probe(struct platform_device *pdev)
684{
685 return 0;
686}
687
688static inline void dsa_of_remove(struct platform_device *pdev)
689{
690}
691#endif
692
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000693static int dsa_probe(struct platform_device *pdev)
694{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000695 struct dsa_platform_data *pd = pdev->dev.platform_data;
696 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000697 struct dsa_switch_tree *dst;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000698 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000699
Joe Perchesa2ae6002014-11-09 16:32:46 -0800700 pr_notice_once("Distributed Switch Architecture driver version %s\n",
701 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000702
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000703 if (pdev->dev.of_node) {
704 ret = dsa_of_probe(pdev);
705 if (ret)
706 return ret;
707
708 pd = pdev->dev.platform_data;
709 }
710
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000711 if (pd == NULL || pd->netdev == NULL)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000712 return -EINVAL;
713
714 dev = dev_to_net_device(pd->netdev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000715 if (dev == NULL) {
716 ret = -EINVAL;
717 goto out;
718 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000719
720 if (dev->dsa_ptr != NULL) {
721 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000722 ret = -EEXIST;
723 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000724 }
725
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000726 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
727 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000728 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000729 ret = -ENOMEM;
730 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000731 }
732
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000733 platform_set_drvdata(pdev, dst);
734
735 dst->pd = pd;
736 dst->master_netdev = dev;
737 dst->cpu_switch = -1;
738 dst->cpu_port = -1;
739
740 for (i = 0; i < pd->nr_chips; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000741 struct dsa_switch *ds;
742
Alexander Duyckb4d23942014-09-15 13:00:27 -0400743 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000744 if (IS_ERR(ds)) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800745 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
746 i, PTR_ERR(ds));
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000747 continue;
748 }
749
750 dst->ds[i] = ds;
751 if (ds->drv->poll_link != NULL)
752 dst->link_poll_needed = 1;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753 }
754
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000755 /*
756 * If we use a tagging format that doesn't have an ethertype
757 * field, make sure that all packets from this point on get
758 * sent to the tag format's receive function.
759 */
760 wmb();
761 dev->dsa_ptr = (void *)dst;
762
763 if (dst->link_poll_needed) {
764 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
765 init_timer(&dst->link_poll_timer);
766 dst->link_poll_timer.data = (unsigned long)dst;
767 dst->link_poll_timer.function = dsa_link_poll_timer;
768 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
769 add_timer(&dst->link_poll_timer);
770 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000771
772 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000773
774out:
775 dsa_of_remove(pdev);
776
777 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000778}
779
780static int dsa_remove(struct platform_device *pdev)
781{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000782 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
783 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000785 if (dst->link_poll_needed)
786 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
Tejun Heo43829732012-08-20 14:51:24 -0700788 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000789
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000790 for (i = 0; i < dst->pd->nr_chips; i++) {
791 struct dsa_switch *ds = dst->ds[i];
792
793 if (ds != NULL)
794 dsa_switch_destroy(ds);
795 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000797 dsa_of_remove(pdev);
798
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000799 return 0;
800}
801
802static void dsa_shutdown(struct platform_device *pdev)
803{
804}
805
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700806static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
807 struct packet_type *pt, struct net_device *orig_dev)
808{
809 struct dsa_switch_tree *dst = dev->dsa_ptr;
810
811 if (unlikely(dst == NULL)) {
812 kfree_skb(skb);
813 return 0;
814 }
815
Alexander Duyck50753142014-09-15 13:00:19 -0400816 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700817}
818
Florian Fainelli61b73632014-08-29 12:42:07 -0700819static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700820 .type = cpu_to_be16(ETH_P_XDSA),
821 .func = dsa_switch_rcv,
822};
823
Florian Fainelli24462542014-09-18 17:31:22 -0700824#ifdef CONFIG_PM_SLEEP
825static int dsa_suspend(struct device *d)
826{
827 struct platform_device *pdev = to_platform_device(d);
828 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
829 int i, ret = 0;
830
831 for (i = 0; i < dst->pd->nr_chips; i++) {
832 struct dsa_switch *ds = dst->ds[i];
833
834 if (ds != NULL)
835 ret = dsa_switch_suspend(ds);
836 }
837
838 return ret;
839}
840
841static int dsa_resume(struct device *d)
842{
843 struct platform_device *pdev = to_platform_device(d);
844 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
845 int i, ret = 0;
846
847 for (i = 0; i < dst->pd->nr_chips; i++) {
848 struct dsa_switch *ds = dst->ds[i];
849
850 if (ds != NULL)
851 ret = dsa_switch_resume(ds);
852 }
853
854 return ret;
855}
856#endif
857
858static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
859
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000860static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700861 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000862 { .compatible = "marvell,dsa", },
863 {}
864};
865MODULE_DEVICE_TABLE(of, dsa_of_match_table);
866
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000867static struct platform_driver dsa_driver = {
868 .probe = dsa_probe,
869 .remove = dsa_remove,
870 .shutdown = dsa_shutdown,
871 .driver = {
872 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000873 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700874 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000875 },
876};
877
878static int __init dsa_init_module(void)
879{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000880 int rc;
881
882 rc = platform_driver_register(&dsa_driver);
883 if (rc)
884 return rc;
885
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700886 dev_add_pack(&dsa_pack_type);
887
Ben Hutchings7df899c2011-11-25 14:35:02 +0000888 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000889}
890module_init(dsa_init_module);
891
892static void __exit dsa_cleanup_module(void)
893{
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700894 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000895 platform_driver_unregister(&dsa_driver);
896}
897module_exit(dsa_cleanup_module);
898
Rusty Russell577d6a72011-01-24 14:32:52 -0600899MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000900MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
901MODULE_LICENSE("GPL");
902MODULE_ALIAS("platform:dsa");