]>
Commit | Line | Data |
---|---|---|
91da11f8 LB |
1 | /* |
2 | * net/dsa/dsa.c - Hardware switch handling | |
e84665c9 | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
5e95329b | 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
91da11f8 LB |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/list.h> | |
91da11f8 | 13 | #include <linux/platform_device.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
3a9a231d | 15 | #include <linux/module.h> |
91da11f8 | 16 | #include <net/dsa.h> |
5e95329b FF |
17 | #include <linux/of.h> |
18 | #include <linux/of_mdio.h> | |
19 | #include <linux/of_platform.h> | |
91da11f8 LB |
20 | #include "dsa_priv.h" |
21 | ||
22 | char dsa_driver_version[] = "0.1"; | |
23 | ||
24 | ||
25 | /* switch driver registration ***********************************************/ | |
26 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); | |
27 | static LIST_HEAD(dsa_switch_drivers); | |
28 | ||
29 | void 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 | } | |
ad293b8a | 35 | EXPORT_SYMBOL_GPL(register_switch_driver); |
91da11f8 LB |
36 | |
37 | void 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 | } | |
ad293b8a | 43 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
91da11f8 LB |
44 | |
45 | static struct dsa_switch_driver * | |
b4d2394d | 46 | dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name) |
91da11f8 LB |
47 | { |
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 | ||
b4d2394d | 61 | name = drv->probe(host_dev, sw_addr); |
91da11f8 LB |
62 | 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 **************************************************/ | |
76 | static struct dsa_switch * | |
e84665c9 | 77 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, |
b4d2394d | 78 | struct device *parent, struct device *host_dev) |
91da11f8 | 79 | { |
e84665c9 LB |
80 | struct dsa_chip_data *pd = dst->pd->chip + index; |
81 | struct dsa_switch_driver *drv; | |
91da11f8 LB |
82 | struct dsa_switch *ds; |
83 | int ret; | |
91da11f8 LB |
84 | char *name; |
85 | int i; | |
f9bf5a2c | 86 | bool valid_name_found = false; |
91da11f8 LB |
87 | |
88 | /* | |
89 | * Probe for switch model. | |
90 | */ | |
b4d2394d | 91 | drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); |
91da11f8 | 92 | if (drv == NULL) { |
e84665c9 LB |
93 | printk(KERN_ERR "%s[%d]: could not detect attached switch\n", |
94 | dst->master_netdev->name, index); | |
91da11f8 LB |
95 | return ERR_PTR(-EINVAL); |
96 | } | |
e84665c9 LB |
97 | printk(KERN_INFO "%s[%d]: detected a %s switch\n", |
98 | dst->master_netdev->name, index, name); | |
91da11f8 LB |
99 | |
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 | ||
e84665c9 LB |
108 | ds->dst = dst; |
109 | ds->index = index; | |
110 | ds->pd = dst->pd->chip + index; | |
91da11f8 | 111 | ds->drv = drv; |
b4d2394d | 112 | ds->master_dev = host_dev; |
91da11f8 LB |
113 | |
114 | /* | |
115 | * Validate supplied switch configuration. | |
116 | */ | |
91da11f8 LB |
117 | 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")) { | |
e84665c9 | 125 | if (dst->cpu_switch != -1) { |
91da11f8 LB |
126 | printk(KERN_ERR "multiple cpu ports?!\n"); |
127 | ret = -EINVAL; | |
128 | goto out; | |
129 | } | |
e84665c9 LB |
130 | dst->cpu_switch = index; |
131 | dst->cpu_port = i; | |
132 | } else if (!strcmp(name, "dsa")) { | |
133 | ds->dsa_port_mask |= 1 << i; | |
91da11f8 | 134 | } else { |
e84665c9 | 135 | ds->phys_port_mask |= 1 << i; |
91da11f8 | 136 | } |
f9bf5a2c | 137 | valid_name_found = true; |
91da11f8 LB |
138 | } |
139 | ||
f9bf5a2c FF |
140 | if (!valid_name_found && i == DSA_MAX_PORTS) { |
141 | ret = -EINVAL; | |
142 | goto out; | |
143 | } | |
91da11f8 | 144 | |
0d8bcdd3 FF |
145 | /* 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 | ||
91da11f8 | 150 | /* |
e84665c9 LB |
151 | * If the CPU connects to this switch, set the switch tree |
152 | * tagging protocol to the preferred tagging format of this | |
153 | * switch. | |
91da11f8 | 154 | */ |
5075314e AD |
155 | 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 | |
ae439286 | 177 | case DSA_TAG_PROTO_NONE: |
5075314e | 178 | break; |
ae439286 AL |
179 | default: |
180 | ret = -ENOPROTOOPT; | |
181 | goto out; | |
5075314e | 182 | } |
91da11f8 | 183 | |
5075314e AD |
184 | dst->tag_protocol = drv->tag_protocol; |
185 | } | |
91da11f8 LB |
186 | |
187 | /* | |
188 | * Do basic register setup. | |
189 | */ | |
190 | ret = drv->setup(ds); | |
191 | if (ret < 0) | |
192 | goto out; | |
193 | ||
e84665c9 | 194 | ret = drv->set_addr(ds, dst->master_netdev->dev_addr); |
91da11f8 LB |
195 | 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 | */ | |
91da11f8 LB |
213 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
214 | struct net_device *slave_dev; | |
215 | ||
e84665c9 | 216 | if (!(ds->phys_port_mask & (1 << i))) |
91da11f8 LB |
217 | continue; |
218 | ||
219 | slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]); | |
220 | if (slave_dev == NULL) { | |
e84665c9 LB |
221 | 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]); | |
91da11f8 LB |
225 | continue; |
226 | } | |
227 | ||
228 | ds->ports[i] = slave_dev; | |
229 | } | |
230 | ||
231 | return ds; | |
232 | ||
233 | out_free: | |
234 | mdiobus_free(ds->slave_mii_bus); | |
235 | out: | |
91da11f8 LB |
236 | kfree(ds); |
237 | return ERR_PTR(ret); | |
238 | } | |
239 | ||
240 | static void dsa_switch_destroy(struct dsa_switch *ds) | |
241 | { | |
242 | } | |
243 | ||
e506d405 | 244 | #ifdef CONFIG_PM_SLEEP |
24462549 FF |
245 | static 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 | ||
265 | static 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 | } | |
e506d405 | 287 | #endif |
24462549 | 288 | |
91da11f8 LB |
289 | |
290 | /* link polling *************************************************************/ | |
291 | static void dsa_link_poll_work(struct work_struct *ugly) | |
292 | { | |
e84665c9 LB |
293 | struct dsa_switch_tree *dst; |
294 | int i; | |
295 | ||
296 | dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); | |
91da11f8 | 297 | |
e84665c9 LB |
298 | for (i = 0; i < dst->pd->nr_chips; i++) { |
299 | struct dsa_switch *ds = dst->ds[i]; | |
91da11f8 | 300 | |
e84665c9 LB |
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)); | |
91da11f8 LB |
306 | } |
307 | ||
e84665c9 | 308 | static void dsa_link_poll_timer(unsigned long _dst) |
91da11f8 | 309 | { |
e84665c9 | 310 | struct dsa_switch_tree *dst = (void *)_dst; |
91da11f8 | 311 | |
e84665c9 | 312 | schedule_work(&dst->link_poll_work); |
91da11f8 LB |
313 | } |
314 | ||
315 | ||
316 | /* platform driver init and cleanup *****************************************/ | |
317 | static 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 | ||
325 | static 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 | ||
b4d2394d | 335 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
91da11f8 LB |
336 | { |
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 | } | |
b4d2394d | 351 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
91da11f8 LB |
352 | |
353 | static 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 | ||
5e95329b FF |
371 | #ifdef CONFIG_OF |
372 | static 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; | |
418 | out: | |
419 | kfree(cd->rtable); | |
420 | return ret; | |
421 | } | |
422 | ||
21168245 FF |
423 | static 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; | |
5f64a7db | 430 | while (port_index < DSA_MAX_PORTS) { |
1f74714f | 431 | kfree(pd->chip[i].port_names[port_index]); |
5f64a7db FF |
432 | port_index++; |
433 | } | |
21168245 FF |
434 | kfree(pd->chip[i].rtable); |
435 | } | |
436 | kfree(pd->chip); | |
437 | } | |
438 | ||
5e95329b FF |
439 | static 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; | |
21168245 | 450 | int ret; |
5e95329b FF |
451 | |
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 = ðernet_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 | ||
d1c0b471 | 485 | chip_index = -1; |
5e95329b | 486 | for_each_available_child_of_node(np, child) { |
d1c0b471 | 487 | chip_index++; |
5e95329b FF |
488 | cd = &pd->chip[chip_index]; |
489 | ||
fa981d9a | 490 | cd->of_node = child; |
c1f570a6 | 491 | cd->host_dev = &mdio_bus->dev; |
5e95329b FF |
492 | |
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 | ||
bd47497a FF |
512 | cd->port_dn[port_index] = port; |
513 | ||
5e95329b FF |
514 | 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 | ||
538 | out_free_chip: | |
21168245 | 539 | dsa_of_free_platform_data(pd); |
5e95329b FF |
540 | out_free: |
541 | kfree(pd); | |
542 | pdev->dev.platform_data = NULL; | |
543 | return ret; | |
544 | } | |
545 | ||
546 | static void dsa_of_remove(struct platform_device *pdev) | |
547 | { | |
548 | struct dsa_platform_data *pd = pdev->dev.platform_data; | |
5e95329b FF |
549 | |
550 | if (!pdev->dev.of_node) | |
551 | return; | |
552 | ||
21168245 | 553 | dsa_of_free_platform_data(pd); |
5e95329b FF |
554 | kfree(pd); |
555 | } | |
556 | #else | |
557 | static inline int dsa_of_probe(struct platform_device *pdev) | |
558 | { | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static inline void dsa_of_remove(struct platform_device *pdev) | |
563 | { | |
564 | } | |
565 | #endif | |
566 | ||
91da11f8 LB |
567 | static 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; | |
e84665c9 | 572 | struct dsa_switch_tree *dst; |
5e95329b | 573 | int i, ret; |
91da11f8 LB |
574 | |
575 | if (!dsa_version_printed++) | |
576 | printk(KERN_NOTICE "Distributed Switch Architecture " | |
577 | "driver version %s\n", dsa_driver_version); | |
578 | ||
5e95329b FF |
579 | 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 | ||
e84665c9 | 587 | if (pd == NULL || pd->netdev == NULL) |
91da11f8 LB |
588 | return -EINVAL; |
589 | ||
590 | dev = dev_to_net_device(pd->netdev); | |
5e95329b FF |
591 | if (dev == NULL) { |
592 | ret = -EINVAL; | |
593 | goto out; | |
594 | } | |
91da11f8 LB |
595 | |
596 | if (dev->dsa_ptr != NULL) { | |
597 | dev_put(dev); | |
5e95329b FF |
598 | ret = -EEXIST; |
599 | goto out; | |
91da11f8 LB |
600 | } |
601 | ||
e84665c9 LB |
602 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
603 | if (dst == NULL) { | |
91da11f8 | 604 | dev_put(dev); |
5e95329b FF |
605 | ret = -ENOMEM; |
606 | goto out; | |
91da11f8 LB |
607 | } |
608 | ||
e84665c9 LB |
609 | 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++) { | |
e84665c9 LB |
617 | struct dsa_switch *ds; |
618 | ||
b4d2394d | 619 | ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev); |
e84665c9 LB |
620 | 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; | |
91da11f8 LB |
630 | } |
631 | ||
e84665c9 LB |
632 | /* |
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 | } | |
91da11f8 LB |
648 | |
649 | return 0; | |
5e95329b FF |
650 | |
651 | out: | |
652 | dsa_of_remove(pdev); | |
653 | ||
654 | return ret; | |
91da11f8 LB |
655 | } |
656 | ||
657 | static int dsa_remove(struct platform_device *pdev) | |
658 | { | |
e84665c9 LB |
659 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
660 | int i; | |
91da11f8 | 661 | |
e84665c9 LB |
662 | if (dst->link_poll_needed) |
663 | del_timer_sync(&dst->link_poll_timer); | |
91da11f8 | 664 | |
43829731 | 665 | flush_work(&dst->link_poll_work); |
91da11f8 | 666 | |
e84665c9 LB |
667 | 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 | } | |
91da11f8 | 673 | |
5e95329b FF |
674 | dsa_of_remove(pdev); |
675 | ||
91da11f8 LB |
676 | return 0; |
677 | } | |
678 | ||
679 | static void dsa_shutdown(struct platform_device *pdev) | |
680 | { | |
681 | } | |
682 | ||
3e8a72d1 FF |
683 | static 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 | ||
5075314e | 693 | return dst->rcv(skb, dev, pt, orig_dev); |
3e8a72d1 FF |
694 | } |
695 | ||
61b7363f | 696 | static struct packet_type dsa_pack_type __read_mostly = { |
3e8a72d1 FF |
697 | .type = cpu_to_be16(ETH_P_XDSA), |
698 | .func = dsa_switch_rcv, | |
699 | }; | |
700 | ||
24462549 FF |
701 | #ifdef CONFIG_PM_SLEEP |
702 | static 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 | ||
718 | static 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 | ||
735 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); | |
736 | ||
5e95329b | 737 | static const struct of_device_id dsa_of_match_table[] = { |
246d7f77 | 738 | { .compatible = "brcm,bcm7445-switch-v4.0" }, |
5e95329b FF |
739 | { .compatible = "marvell,dsa", }, |
740 | {} | |
741 | }; | |
742 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); | |
743 | ||
91da11f8 LB |
744 | static 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, | |
5e95329b | 751 | .of_match_table = dsa_of_match_table, |
24462549 | 752 | .pm = &dsa_pm_ops, |
91da11f8 LB |
753 | }, |
754 | }; | |
755 | ||
756 | static int __init dsa_init_module(void) | |
757 | { | |
7df899c3 BH |
758 | int rc; |
759 | ||
760 | rc = platform_driver_register(&dsa_driver); | |
761 | if (rc) | |
762 | return rc; | |
763 | ||
3e8a72d1 FF |
764 | dev_add_pack(&dsa_pack_type); |
765 | ||
7df899c3 | 766 | return 0; |
91da11f8 LB |
767 | } |
768 | module_init(dsa_init_module); | |
769 | ||
770 | static void __exit dsa_cleanup_module(void) | |
771 | { | |
3e8a72d1 | 772 | dev_remove_pack(&dsa_pack_type); |
91da11f8 LB |
773 | platform_driver_unregister(&dsa_driver); |
774 | } | |
775 | module_exit(dsa_cleanup_module); | |
776 | ||
577d6a7c | 777 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
91da11f8 LB |
778 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
779 | MODULE_LICENSE("GPL"); | |
780 | MODULE_ALIAS("platform:dsa"); |