blob: 6317b41c99b0a58bb82ede4a1205d8c3e7f94283 [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
12#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040015#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include <net/dsa.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000017#include <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000020#include "dsa_priv.h"
21
22char dsa_driver_version[] = "0.1";
23
24
25/* switch driver registration ***********************************************/
26static DEFINE_MUTEX(dsa_switch_drivers_mutex);
27static LIST_HEAD(dsa_switch_drivers);
28
29void register_switch_driver(struct dsa_switch_driver *drv)
30{
31 mutex_lock(&dsa_switch_drivers_mutex);
32 list_add_tail(&drv->list, &dsa_switch_drivers);
33 mutex_unlock(&dsa_switch_drivers_mutex);
34}
Ben Hutchingsad293b82011-11-25 14:34:07 +000035EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000036
37void unregister_switch_driver(struct dsa_switch_driver *drv)
38{
39 mutex_lock(&dsa_switch_drivers_mutex);
40 list_del_init(&drv->list);
41 mutex_unlock(&dsa_switch_drivers_mutex);
42}
Ben Hutchingsad293b82011-11-25 14:34:07 +000043EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000044
45static struct dsa_switch_driver *
Alexander Duyckb4d23942014-09-15 13:00:27 -040046dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047{
48 struct dsa_switch_driver *ret;
49 struct list_head *list;
50 char *name;
51
52 ret = NULL;
53 name = NULL;
54
55 mutex_lock(&dsa_switch_drivers_mutex);
56 list_for_each(list, &dsa_switch_drivers) {
57 struct dsa_switch_driver *drv;
58
59 drv = list_entry(list, struct dsa_switch_driver, list);
60
Alexander Duyckb4d23942014-09-15 13:00:27 -040061 name = drv->probe(host_dev, sw_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000062 if (name != NULL) {
63 ret = drv;
64 break;
65 }
66 }
67 mutex_unlock(&dsa_switch_drivers_mutex);
68
69 *_name = name;
70
71 return ret;
72}
73
74
75/* basic switch operations **************************************************/
76static struct dsa_switch *
Lennert Buytenheke84665c2009-03-20 09:52:09 +000077dsa_switch_setup(struct dsa_switch_tree *dst, int index,
Alexander Duyckb4d23942014-09-15 13:00:27 -040078 struct device *parent, struct device *host_dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000079{
Lennert Buytenheke84665c2009-03-20 09:52:09 +000080 struct dsa_chip_data *pd = dst->pd->chip + index;
81 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000082 struct dsa_switch *ds;
83 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000084 char *name;
85 int i;
Florian Fainellif9bf5a22013-01-21 09:58:51 +000086 bool valid_name_found = false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087
88 /*
89 * Probe for switch model.
90 */
Alexander Duyckb4d23942014-09-15 13:00:27 -040091 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000092 if (drv == NULL) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +000093 printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
94 dst->master_netdev->name, index);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095 return ERR_PTR(-EINVAL);
96 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +000097 printk(KERN_INFO "%s[%d]: detected a %s switch\n",
98 dst->master_netdev->name, index, name);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099
100
101 /*
102 * Allocate and initialise switch state.
103 */
104 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
105 if (ds == NULL)
106 return ERR_PTR(-ENOMEM);
107
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000108 ds->dst = dst;
109 ds->index = index;
110 ds->pd = dst->pd->chip + index;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000111 ds->drv = drv;
Alexander Duyckb4d23942014-09-15 13:00:27 -0400112 ds->master_dev = host_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000113
114 /*
115 * Validate supplied switch configuration.
116 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 for (i = 0; i < DSA_MAX_PORTS; i++) {
118 char *name;
119
120 name = pd->port_names[i];
121 if (name == NULL)
122 continue;
123
124 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000125 if (dst->cpu_switch != -1) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126 printk(KERN_ERR "multiple cpu ports?!\n");
127 ret = -EINVAL;
128 goto out;
129 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000130 dst->cpu_switch = index;
131 dst->cpu_port = i;
132 } else if (!strcmp(name, "dsa")) {
133 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000134 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000135 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000137 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 }
139
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000140 if (!valid_name_found && i == DSA_MAX_PORTS) {
141 ret = -EINVAL;
142 goto out;
143 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000144
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700145 /* Make the built-in MII bus mask match the number of ports,
146 * switch drivers can override this later
147 */
148 ds->phys_mii_mask = ds->phys_port_mask;
149
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000150 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000151 * If the CPU connects to this switch, set the switch tree
152 * tagging protocol to the preferred tagging format of this
153 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000154 */
Alexander Duyck50753142014-09-15 13:00:19 -0400155 if (dst->cpu_switch == index) {
156 switch (drv->tag_protocol) {
157#ifdef CONFIG_NET_DSA_TAG_DSA
158 case DSA_TAG_PROTO_DSA:
159 dst->rcv = dsa_netdev_ops.rcv;
160 break;
161#endif
162#ifdef CONFIG_NET_DSA_TAG_EDSA
163 case DSA_TAG_PROTO_EDSA:
164 dst->rcv = edsa_netdev_ops.rcv;
165 break;
166#endif
167#ifdef CONFIG_NET_DSA_TAG_TRAILER
168 case DSA_TAG_PROTO_TRAILER:
169 dst->rcv = trailer_netdev_ops.rcv;
170 break;
171#endif
172#ifdef CONFIG_NET_DSA_TAG_BRCM
173 case DSA_TAG_PROTO_BRCM:
174 dst->rcv = brcm_netdev_ops.rcv;
175 break;
176#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200177 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400178 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200179 default:
180 ret = -ENOPROTOOPT;
181 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400182 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000183
Alexander Duyck50753142014-09-15 13:00:19 -0400184 dst->tag_protocol = drv->tag_protocol;
185 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000186
187 /*
188 * Do basic register setup.
189 */
190 ret = drv->setup(ds);
191 if (ret < 0)
192 goto out;
193
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000194 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000195 if (ret < 0)
196 goto out;
197
198 ds->slave_mii_bus = mdiobus_alloc();
199 if (ds->slave_mii_bus == NULL) {
200 ret = -ENOMEM;
201 goto out;
202 }
203 dsa_slave_mii_bus_init(ds);
204
205 ret = mdiobus_register(ds->slave_mii_bus);
206 if (ret < 0)
207 goto out_free;
208
209
210 /*
211 * Create network devices for physical switch ports.
212 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213 for (i = 0; i < DSA_MAX_PORTS; i++) {
214 struct net_device *slave_dev;
215
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000216 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217 continue;
218
219 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
220 if (slave_dev == NULL) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000221 printk(KERN_ERR "%s[%d]: can't create dsa "
222 "slave device for port %d(%s)\n",
223 dst->master_netdev->name,
224 index, i, pd->port_names[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 continue;
226 }
227
228 ds->ports[i] = slave_dev;
229 }
230
231 return ds;
232
233out_free:
234 mdiobus_free(ds->slave_mii_bus);
235out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000236 kfree(ds);
237 return ERR_PTR(ret);
238}
239
240static void dsa_switch_destroy(struct dsa_switch *ds)
241{
242}
243
Thierry Redinge506d402014-10-01 13:59:00 +0200244#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700245static int dsa_switch_suspend(struct dsa_switch *ds)
246{
247 int i, ret = 0;
248
249 /* Suspend slave network devices */
250 for (i = 0; i < DSA_MAX_PORTS; i++) {
251 if (!(ds->phys_port_mask & (1 << i)))
252 continue;
253
254 ret = dsa_slave_suspend(ds->ports[i]);
255 if (ret)
256 return ret;
257 }
258
259 if (ds->drv->suspend)
260 ret = ds->drv->suspend(ds);
261
262 return ret;
263}
264
265static int dsa_switch_resume(struct dsa_switch *ds)
266{
267 int i, ret = 0;
268
269 if (ds->drv->resume)
270 ret = ds->drv->resume(ds);
271
272 if (ret)
273 return ret;
274
275 /* Resume slave network devices */
276 for (i = 0; i < DSA_MAX_PORTS; i++) {
277 if (!(ds->phys_port_mask & (1 << i)))
278 continue;
279
280 ret = dsa_slave_resume(ds->ports[i]);
281 if (ret)
282 return ret;
283 }
284
285 return 0;
286}
Thierry Redinge506d402014-10-01 13:59:00 +0200287#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700288
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000289
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000290/* link polling *************************************************************/
291static void dsa_link_poll_work(struct work_struct *ugly)
292{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000293 struct dsa_switch_tree *dst;
294 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000295
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000296 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000298 for (i = 0; i < dst->pd->nr_chips; i++) {
299 struct dsa_switch *ds = dst->ds[i];
300
301 if (ds != NULL && ds->drv->poll_link != NULL)
302 ds->drv->poll_link(ds);
303 }
304
305 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000306}
307
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000308static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000309{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000310 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000311
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000312 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000313}
314
315
316/* platform driver init and cleanup *****************************************/
317static int dev_is_class(struct device *dev, void *class)
318{
319 if (dev->class != NULL && !strcmp(dev->class->name, class))
320 return 1;
321
322 return 0;
323}
324
325static struct device *dev_find_class(struct device *parent, char *class)
326{
327 if (dev_is_class(parent, class)) {
328 get_device(parent);
329 return parent;
330 }
331
332 return device_find_child(parent, class, dev_is_class);
333}
334
Alexander Duyckb4d23942014-09-15 13:00:27 -0400335struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000336{
337 struct device *d;
338
339 d = dev_find_class(dev, "mdio_bus");
340 if (d != NULL) {
341 struct mii_bus *bus;
342
343 bus = to_mii_bus(d);
344 put_device(d);
345
346 return bus;
347 }
348
349 return NULL;
350}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400351EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000352
353static struct net_device *dev_to_net_device(struct device *dev)
354{
355 struct device *d;
356
357 d = dev_find_class(dev, "net");
358 if (d != NULL) {
359 struct net_device *nd;
360
361 nd = to_net_dev(d);
362 dev_hold(nd);
363 put_device(d);
364
365 return nd;
366 }
367
368 return NULL;
369}
370
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000371#ifdef CONFIG_OF
372static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
373 struct dsa_chip_data *cd,
374 int chip_index,
375 struct device_node *link)
376{
377 int ret;
378 const __be32 *reg;
379 int link_port_addr;
380 int link_sw_addr;
381 struct device_node *parent_sw;
382 int len;
383
384 parent_sw = of_get_parent(link);
385 if (!parent_sw)
386 return -EINVAL;
387
388 reg = of_get_property(parent_sw, "reg", &len);
389 if (!reg || (len != sizeof(*reg) * 2))
390 return -EINVAL;
391
392 link_sw_addr = be32_to_cpup(reg + 1);
393
394 if (link_sw_addr >= pd->nr_chips)
395 return -EINVAL;
396
397 /* First time routing table allocation */
398 if (!cd->rtable) {
399 cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL);
400 if (!cd->rtable)
401 return -ENOMEM;
402
403 /* default to no valid uplink/downlink */
404 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
405 }
406
407 reg = of_get_property(link, "reg", NULL);
408 if (!reg) {
409 ret = -EINVAL;
410 goto out;
411 }
412
413 link_port_addr = be32_to_cpup(reg);
414
415 cd->rtable[link_sw_addr] = link_port_addr;
416
417 return 0;
418out:
419 kfree(cd->rtable);
420 return ret;
421}
422
Florian Fainelli21168242013-03-25 05:03:39 +0000423static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
424{
425 int i;
426 int port_index;
427
428 for (i = 0; i < pd->nr_chips; i++) {
429 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000430 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200431 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000432 port_index++;
433 }
Florian Fainelli21168242013-03-25 05:03:39 +0000434 kfree(pd->chip[i].rtable);
435 }
436 kfree(pd->chip);
437}
438
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000439static int dsa_of_probe(struct platform_device *pdev)
440{
441 struct device_node *np = pdev->dev.of_node;
442 struct device_node *child, *mdio, *ethernet, *port, *link;
443 struct mii_bus *mdio_bus;
444 struct platform_device *ethernet_dev;
445 struct dsa_platform_data *pd;
446 struct dsa_chip_data *cd;
447 const char *port_name;
448 int chip_index, port_index;
449 const unsigned int *sw_addr, *port_reg;
Florian Fainelli21168242013-03-25 05:03:39 +0000450 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000451
452 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
453 if (!mdio)
454 return -EINVAL;
455
456 mdio_bus = of_mdio_find_bus(mdio);
457 if (!mdio_bus)
458 return -EINVAL;
459
460 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
461 if (!ethernet)
462 return -EINVAL;
463
464 ethernet_dev = of_find_device_by_node(ethernet);
465 if (!ethernet_dev)
466 return -ENODEV;
467
468 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
469 if (!pd)
470 return -ENOMEM;
471
472 pdev->dev.platform_data = pd;
473 pd->netdev = &ethernet_dev->dev;
474 pd->nr_chips = of_get_child_count(np);
475 if (pd->nr_chips > DSA_MAX_SWITCHES)
476 pd->nr_chips = DSA_MAX_SWITCHES;
477
478 pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data),
479 GFP_KERNEL);
480 if (!pd->chip) {
481 ret = -ENOMEM;
482 goto out_free;
483 }
484
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200485 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000486 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200487 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000488 cd = &pd->chip[chip_index];
489
Florian Fainellifa981d92014-08-27 17:04:49 -0700490 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700491 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000492
493 sw_addr = of_get_property(child, "reg", NULL);
494 if (!sw_addr)
495 continue;
496
497 cd->sw_addr = be32_to_cpup(sw_addr);
498 if (cd->sw_addr > PHY_MAX_ADDR)
499 continue;
500
501 for_each_available_child_of_node(child, port) {
502 port_reg = of_get_property(port, "reg", NULL);
503 if (!port_reg)
504 continue;
505
506 port_index = be32_to_cpup(port_reg);
507
508 port_name = of_get_property(port, "label", NULL);
509 if (!port_name)
510 continue;
511
Florian Fainellibd474972014-08-27 17:04:50 -0700512 cd->port_dn[port_index] = port;
513
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000514 cd->port_names[port_index] = kstrdup(port_name,
515 GFP_KERNEL);
516 if (!cd->port_names[port_index]) {
517 ret = -ENOMEM;
518 goto out_free_chip;
519 }
520
521 link = of_parse_phandle(port, "link", 0);
522
523 if (!strcmp(port_name, "dsa") && link &&
524 pd->nr_chips > 1) {
525 ret = dsa_of_setup_routing_table(pd, cd,
526 chip_index, link);
527 if (ret)
528 goto out_free_chip;
529 }
530
531 if (port_index == DSA_MAX_PORTS)
532 break;
533 }
534 }
535
536 return 0;
537
538out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000539 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000540out_free:
541 kfree(pd);
542 pdev->dev.platform_data = NULL;
543 return ret;
544}
545
546static void dsa_of_remove(struct platform_device *pdev)
547{
548 struct dsa_platform_data *pd = pdev->dev.platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000549
550 if (!pdev->dev.of_node)
551 return;
552
Florian Fainelli21168242013-03-25 05:03:39 +0000553 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000554 kfree(pd);
555}
556#else
557static inline int dsa_of_probe(struct platform_device *pdev)
558{
559 return 0;
560}
561
562static inline void dsa_of_remove(struct platform_device *pdev)
563{
564}
565#endif
566
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000567static int dsa_probe(struct platform_device *pdev)
568{
569 static int dsa_version_printed;
570 struct dsa_platform_data *pd = pdev->dev.platform_data;
571 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000572 struct dsa_switch_tree *dst;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000573 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000574
575 if (!dsa_version_printed++)
576 printk(KERN_NOTICE "Distributed Switch Architecture "
577 "driver version %s\n", dsa_driver_version);
578
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000579 if (pdev->dev.of_node) {
580 ret = dsa_of_probe(pdev);
581 if (ret)
582 return ret;
583
584 pd = pdev->dev.platform_data;
585 }
586
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000587 if (pd == NULL || pd->netdev == NULL)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588 return -EINVAL;
589
590 dev = dev_to_net_device(pd->netdev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000591 if (dev == NULL) {
592 ret = -EINVAL;
593 goto out;
594 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000595
596 if (dev->dsa_ptr != NULL) {
597 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000598 ret = -EEXIST;
599 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000600 }
601
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000602 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
603 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000604 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000605 ret = -ENOMEM;
606 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000607 }
608
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000609 platform_set_drvdata(pdev, dst);
610
611 dst->pd = pd;
612 dst->master_netdev = dev;
613 dst->cpu_switch = -1;
614 dst->cpu_port = -1;
615
616 for (i = 0; i < pd->nr_chips; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000617 struct dsa_switch *ds;
618
Alexander Duyckb4d23942014-09-15 13:00:27 -0400619 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000620 if (IS_ERR(ds)) {
621 printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
622 "instance (error %ld)\n", dev->name, i,
623 PTR_ERR(ds));
624 continue;
625 }
626
627 dst->ds[i] = ds;
628 if (ds->drv->poll_link != NULL)
629 dst->link_poll_needed = 1;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000630 }
631
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000632 /*
633 * If we use a tagging format that doesn't have an ethertype
634 * field, make sure that all packets from this point on get
635 * sent to the tag format's receive function.
636 */
637 wmb();
638 dev->dsa_ptr = (void *)dst;
639
640 if (dst->link_poll_needed) {
641 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
642 init_timer(&dst->link_poll_timer);
643 dst->link_poll_timer.data = (unsigned long)dst;
644 dst->link_poll_timer.function = dsa_link_poll_timer;
645 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
646 add_timer(&dst->link_poll_timer);
647 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000648
649 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000650
651out:
652 dsa_of_remove(pdev);
653
654 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000655}
656
657static int dsa_remove(struct platform_device *pdev)
658{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000659 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
660 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000661
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000662 if (dst->link_poll_needed)
663 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000664
Tejun Heo43829732012-08-20 14:51:24 -0700665 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000666
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000667 for (i = 0; i < dst->pd->nr_chips; i++) {
668 struct dsa_switch *ds = dst->ds[i];
669
670 if (ds != NULL)
671 dsa_switch_destroy(ds);
672 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000673
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000674 dsa_of_remove(pdev);
675
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000676 return 0;
677}
678
679static void dsa_shutdown(struct platform_device *pdev)
680{
681}
682
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700683static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
684 struct packet_type *pt, struct net_device *orig_dev)
685{
686 struct dsa_switch_tree *dst = dev->dsa_ptr;
687
688 if (unlikely(dst == NULL)) {
689 kfree_skb(skb);
690 return 0;
691 }
692
Alexander Duyck50753142014-09-15 13:00:19 -0400693 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700694}
695
Florian Fainelli61b73632014-08-29 12:42:07 -0700696static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700697 .type = cpu_to_be16(ETH_P_XDSA),
698 .func = dsa_switch_rcv,
699};
700
Florian Fainelli24462542014-09-18 17:31:22 -0700701#ifdef CONFIG_PM_SLEEP
702static int dsa_suspend(struct device *d)
703{
704 struct platform_device *pdev = to_platform_device(d);
705 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
706 int i, ret = 0;
707
708 for (i = 0; i < dst->pd->nr_chips; i++) {
709 struct dsa_switch *ds = dst->ds[i];
710
711 if (ds != NULL)
712 ret = dsa_switch_suspend(ds);
713 }
714
715 return ret;
716}
717
718static int dsa_resume(struct device *d)
719{
720 struct platform_device *pdev = to_platform_device(d);
721 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
722 int i, ret = 0;
723
724 for (i = 0; i < dst->pd->nr_chips; i++) {
725 struct dsa_switch *ds = dst->ds[i];
726
727 if (ds != NULL)
728 ret = dsa_switch_resume(ds);
729 }
730
731 return ret;
732}
733#endif
734
735static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
736
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000737static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700738 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000739 { .compatible = "marvell,dsa", },
740 {}
741};
742MODULE_DEVICE_TABLE(of, dsa_of_match_table);
743
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000744static struct platform_driver dsa_driver = {
745 .probe = dsa_probe,
746 .remove = dsa_remove,
747 .shutdown = dsa_shutdown,
748 .driver = {
749 .name = "dsa",
750 .owner = THIS_MODULE,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000751 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700752 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753 },
754};
755
756static int __init dsa_init_module(void)
757{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000758 int rc;
759
760 rc = platform_driver_register(&dsa_driver);
761 if (rc)
762 return rc;
763
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700764 dev_add_pack(&dsa_pack_type);
765
Ben Hutchings7df899c2011-11-25 14:35:02 +0000766 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000767}
768module_init(dsa_init_module);
769
770static void __exit dsa_cleanup_module(void)
771{
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700772 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000773 platform_driver_unregister(&dsa_driver);
774}
775module_exit(dsa_cleanup_module);
776
Rusty Russell577d6a72011-01-24 14:32:52 -0600777MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000778MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
779MODULE_LICENSE("GPL");
780MODULE_ALIAS("platform:dsa");