]>
Commit | Line | Data |
---|---|---|
a6a71f19 VD |
1 | /* |
2 | * net/dsa/legacy.c - Hardware switch handling | |
3 | * Copyright (c) 2008-2009 Marvell Semiconductor | |
4 | * Copyright (c) 2013 Florian Fainelli <[email protected]> | |
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/device.h> | |
13 | #include <linux/list.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/of.h> | |
18 | #include <linux/of_mdio.h> | |
19 | #include <linux/of_platform.h> | |
20 | #include <linux/of_net.h> | |
21 | #include <linux/netdevice.h> | |
22 | #include <linux/sysfs.h> | |
23 | #include <linux/phy_fixed.h> | |
24 | #include <linux/etherdevice.h> | |
ea5dd34b | 25 | |
a6a71f19 VD |
26 | #include "dsa_priv.h" |
27 | ||
28 | /* switch driver registration ***********************************************/ | |
29 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); | |
30 | static LIST_HEAD(dsa_switch_drivers); | |
31 | ||
32 | void register_switch_driver(struct dsa_switch_driver *drv) | |
33 | { | |
34 | mutex_lock(&dsa_switch_drivers_mutex); | |
35 | list_add_tail(&drv->list, &dsa_switch_drivers); | |
36 | mutex_unlock(&dsa_switch_drivers_mutex); | |
37 | } | |
38 | EXPORT_SYMBOL_GPL(register_switch_driver); | |
39 | ||
40 | void unregister_switch_driver(struct dsa_switch_driver *drv) | |
41 | { | |
42 | mutex_lock(&dsa_switch_drivers_mutex); | |
43 | list_del_init(&drv->list); | |
44 | mutex_unlock(&dsa_switch_drivers_mutex); | |
45 | } | |
46 | EXPORT_SYMBOL_GPL(unregister_switch_driver); | |
47 | ||
48 | static const struct dsa_switch_ops * | |
49 | dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr, | |
50 | const char **_name, void **priv) | |
51 | { | |
52 | const struct dsa_switch_ops *ret; | |
53 | struct list_head *list; | |
54 | const 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 | const struct dsa_switch_ops *ops; | |
62 | struct dsa_switch_driver *drv; | |
63 | ||
64 | drv = list_entry(list, struct dsa_switch_driver, list); | |
65 | ops = drv->ops; | |
66 | ||
67 | name = ops->probe(parent, host_dev, sw_addr, priv); | |
68 | if (name != NULL) { | |
69 | ret = ops; | |
70 | break; | |
71 | } | |
72 | } | |
73 | mutex_unlock(&dsa_switch_drivers_mutex); | |
74 | ||
75 | *_name = name; | |
76 | ||
77 | return ret; | |
78 | } | |
79 | ||
80 | /* basic switch operations **************************************************/ | |
81 | static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev) | |
82 | { | |
83 | struct dsa_port *dport; | |
84 | int ret, port; | |
85 | ||
86 | for (port = 0; port < ds->num_ports; port++) { | |
87 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) | |
88 | continue; | |
89 | ||
90 | dport = &ds->ports[port]; | |
91 | ret = dsa_cpu_dsa_setup(ds, dev, dport, port); | |
92 | if (ret) | |
93 | return ret; | |
94 | } | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent) | |
99 | { | |
100 | const struct dsa_switch_ops *ops = ds->ops; | |
101 | struct dsa_switch_tree *dst = ds->dst; | |
102 | struct dsa_chip_data *cd = ds->cd; | |
103 | bool valid_name_found = false; | |
104 | int index = ds->index; | |
105 | int i, ret; | |
106 | ||
107 | /* | |
108 | * Validate supplied switch configuration. | |
109 | */ | |
110 | for (i = 0; i < ds->num_ports; i++) { | |
111 | char *name; | |
112 | ||
113 | name = cd->port_names[i]; | |
114 | if (name == NULL) | |
115 | continue; | |
116 | ||
117 | if (!strcmp(name, "cpu")) { | |
8b0d3ea5 | 118 | if (dst->cpu_dp) { |
a6a71f19 VD |
119 | netdev_err(dst->master_netdev, |
120 | "multiple cpu ports?!\n"); | |
121 | return -EINVAL; | |
122 | } | |
8b0d3ea5 | 123 | dst->cpu_dp = &ds->ports[i]; |
a6a71f19 VD |
124 | ds->cpu_port_mask |= 1 << i; |
125 | } else if (!strcmp(name, "dsa")) { | |
126 | ds->dsa_port_mask |= 1 << i; | |
127 | } else { | |
128 | ds->enabled_port_mask |= 1 << i; | |
129 | } | |
130 | valid_name_found = true; | |
131 | } | |
132 | ||
133 | if (!valid_name_found && i == ds->num_ports) | |
134 | return -EINVAL; | |
135 | ||
136 | /* Make the built-in MII bus mask match the number of ports, | |
137 | * switch drivers can override this later | |
138 | */ | |
139 | ds->phys_mii_mask = ds->enabled_port_mask; | |
140 | ||
141 | /* | |
142 | * If the CPU connects to this switch, set the switch tree | |
143 | * tagging protocol to the preferred tagging format of this | |
144 | * switch. | |
145 | */ | |
8b0d3ea5 | 146 | if (dst->cpu_dp->ds == ds) { |
a6a71f19 VD |
147 | enum dsa_tag_protocol tag_protocol; |
148 | ||
149 | tag_protocol = ops->get_tag_protocol(ds); | |
150 | dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); | |
151 | if (IS_ERR(dst->tag_ops)) | |
152 | return PTR_ERR(dst->tag_ops); | |
153 | ||
154 | dst->rcv = dst->tag_ops->rcv; | |
155 | } | |
156 | ||
157 | memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable)); | |
158 | ||
159 | /* | |
160 | * Do basic register setup. | |
161 | */ | |
162 | ret = ops->setup(ds); | |
163 | if (ret < 0) | |
164 | return ret; | |
165 | ||
166 | ret = dsa_switch_register_notifier(ds); | |
167 | if (ret) | |
168 | return ret; | |
169 | ||
170 | if (ops->set_addr) { | |
171 | ret = ops->set_addr(ds, dst->master_netdev->dev_addr); | |
172 | if (ret < 0) | |
173 | return ret; | |
174 | } | |
175 | ||
176 | if (!ds->slave_mii_bus && ops->phy_read) { | |
177 | ds->slave_mii_bus = devm_mdiobus_alloc(parent); | |
178 | if (!ds->slave_mii_bus) | |
179 | return -ENOMEM; | |
180 | dsa_slave_mii_bus_init(ds); | |
181 | ||
182 | ret = mdiobus_register(ds->slave_mii_bus); | |
183 | if (ret < 0) | |
184 | return ret; | |
185 | } | |
186 | ||
187 | /* | |
188 | * Create network devices for physical switch ports. | |
189 | */ | |
190 | for (i = 0; i < ds->num_ports; i++) { | |
191 | ds->ports[i].dn = cd->port_dn[i]; | |
192 | ||
193 | if (!(ds->enabled_port_mask & (1 << i))) | |
194 | continue; | |
195 | ||
196 | ret = dsa_slave_create(ds, parent, i, cd->port_names[i]); | |
197 | if (ret < 0) | |
198 | netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n", | |
199 | index, i, cd->port_names[i], ret); | |
200 | } | |
201 | ||
202 | /* Perform configuration of the CPU and DSA ports */ | |
203 | ret = dsa_cpu_dsa_setups(ds, parent); | |
204 | if (ret < 0) | |
205 | netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n", | |
206 | index); | |
207 | ||
937c7df8 | 208 | ret = dsa_cpu_port_ethtool_setup(ds->dst->cpu_dp); |
a6a71f19 VD |
209 | if (ret) |
210 | return ret; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static struct dsa_switch * | |
216 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, | |
217 | struct device *parent, struct device *host_dev) | |
218 | { | |
219 | struct dsa_chip_data *cd = dst->pd->chip + index; | |
220 | const struct dsa_switch_ops *ops; | |
221 | struct dsa_switch *ds; | |
222 | int ret; | |
223 | const char *name; | |
224 | void *priv; | |
225 | ||
226 | /* | |
227 | * Probe for switch model. | |
228 | */ | |
229 | ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv); | |
230 | if (!ops) { | |
231 | netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", | |
232 | index); | |
233 | return ERR_PTR(-EINVAL); | |
234 | } | |
235 | netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", | |
236 | index, name); | |
237 | ||
238 | ||
239 | /* | |
240 | * Allocate and initialise switch state. | |
241 | */ | |
242 | ds = dsa_switch_alloc(parent, DSA_MAX_PORTS); | |
243 | if (!ds) | |
244 | return ERR_PTR(-ENOMEM); | |
245 | ||
246 | ds->dst = dst; | |
247 | ds->index = index; | |
248 | ds->cd = cd; | |
249 | ds->ops = ops; | |
250 | ds->priv = priv; | |
251 | ||
252 | ret = dsa_switch_setup_one(ds, parent); | |
253 | if (ret) | |
254 | return ERR_PTR(ret); | |
255 | ||
256 | return ds; | |
257 | } | |
258 | ||
259 | static void dsa_switch_destroy(struct dsa_switch *ds) | |
260 | { | |
261 | int port; | |
262 | ||
263 | /* Destroy network devices for physical switch ports. */ | |
264 | for (port = 0; port < ds->num_ports; port++) { | |
265 | if (!(ds->enabled_port_mask & (1 << port))) | |
266 | continue; | |
267 | ||
268 | if (!ds->ports[port].netdev) | |
269 | continue; | |
270 | ||
271 | dsa_slave_destroy(ds->ports[port].netdev); | |
272 | } | |
273 | ||
274 | /* Disable configuration of the CPU and DSA ports */ | |
275 | for (port = 0; port < ds->num_ports; port++) { | |
276 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) | |
277 | continue; | |
278 | dsa_cpu_dsa_destroy(&ds->ports[port]); | |
279 | ||
280 | /* Clearing a bit which is not set does no harm */ | |
281 | ds->cpu_port_mask |= ~(1 << port); | |
282 | ds->dsa_port_mask |= ~(1 << port); | |
283 | } | |
284 | ||
285 | if (ds->slave_mii_bus && ds->ops->phy_read) | |
286 | mdiobus_unregister(ds->slave_mii_bus); | |
287 | ||
288 | dsa_switch_unregister_notifier(ds); | |
289 | } | |
290 | ||
291 | #ifdef CONFIG_PM_SLEEP | |
292 | int dsa_switch_suspend(struct dsa_switch *ds) | |
293 | { | |
294 | int i, ret = 0; | |
295 | ||
296 | /* Suspend slave network devices */ | |
297 | for (i = 0; i < ds->num_ports; i++) { | |
298 | if (!dsa_is_port_initialized(ds, i)) | |
299 | continue; | |
300 | ||
301 | ret = dsa_slave_suspend(ds->ports[i].netdev); | |
302 | if (ret) | |
303 | return ret; | |
304 | } | |
305 | ||
306 | if (ds->ops->suspend) | |
307 | ret = ds->ops->suspend(ds); | |
308 | ||
309 | return ret; | |
310 | } | |
311 | EXPORT_SYMBOL_GPL(dsa_switch_suspend); | |
312 | ||
313 | int dsa_switch_resume(struct dsa_switch *ds) | |
314 | { | |
315 | int i, ret = 0; | |
316 | ||
317 | if (ds->ops->resume) | |
318 | ret = ds->ops->resume(ds); | |
319 | ||
320 | if (ret) | |
321 | return ret; | |
322 | ||
323 | /* Resume slave network devices */ | |
324 | for (i = 0; i < ds->num_ports; i++) { | |
325 | if (!dsa_is_port_initialized(ds, i)) | |
326 | continue; | |
327 | ||
328 | ret = dsa_slave_resume(ds->ports[i].netdev); | |
329 | if (ret) | |
330 | return ret; | |
331 | } | |
332 | ||
333 | return 0; | |
334 | } | |
335 | EXPORT_SYMBOL_GPL(dsa_switch_resume); | |
336 | #endif | |
337 | ||
338 | /* platform driver init and cleanup *****************************************/ | |
339 | static int dev_is_class(struct device *dev, void *class) | |
340 | { | |
341 | if (dev->class != NULL && !strcmp(dev->class->name, class)) | |
342 | return 1; | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
347 | static struct device *dev_find_class(struct device *parent, char *class) | |
348 | { | |
349 | if (dev_is_class(parent, class)) { | |
350 | get_device(parent); | |
351 | return parent; | |
352 | } | |
353 | ||
354 | return device_find_child(parent, class, dev_is_class); | |
355 | } | |
356 | ||
357 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) | |
358 | { | |
359 | struct device *d; | |
360 | ||
361 | d = dev_find_class(dev, "mdio_bus"); | |
362 | if (d != NULL) { | |
363 | struct mii_bus *bus; | |
364 | ||
365 | bus = to_mii_bus(d); | |
366 | put_device(d); | |
367 | ||
368 | return bus; | |
369 | } | |
370 | ||
371 | return NULL; | |
372 | } | |
373 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); | |
374 | ||
375 | #ifdef CONFIG_OF | |
376 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, | |
377 | struct dsa_chip_data *cd, | |
378 | int chip_index, int port_index, | |
379 | struct device_node *link) | |
380 | { | |
381 | const __be32 *reg; | |
382 | int link_sw_addr; | |
383 | struct device_node *parent_sw; | |
384 | int len; | |
385 | ||
386 | parent_sw = of_get_parent(link); | |
387 | if (!parent_sw) | |
388 | return -EINVAL; | |
389 | ||
390 | reg = of_get_property(parent_sw, "reg", &len); | |
391 | if (!reg || (len != sizeof(*reg) * 2)) | |
392 | return -EINVAL; | |
393 | ||
394 | /* | |
395 | * Get the destination switch number from the second field of its 'reg' | |
396 | * property, i.e. for "reg = <0x19 1>" sw_addr is '1'. | |
397 | */ | |
398 | link_sw_addr = be32_to_cpup(reg + 1); | |
399 | ||
400 | if (link_sw_addr >= pd->nr_chips) | |
401 | return -EINVAL; | |
402 | ||
403 | cd->rtable[link_sw_addr] = port_index; | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | static int dsa_of_probe_links(struct dsa_platform_data *pd, | |
409 | struct dsa_chip_data *cd, | |
410 | int chip_index, int port_index, | |
411 | struct device_node *port, | |
412 | const char *port_name) | |
413 | { | |
414 | struct device_node *link; | |
415 | int link_index; | |
416 | int ret; | |
417 | ||
418 | for (link_index = 0;; link_index++) { | |
419 | link = of_parse_phandle(port, "link", link_index); | |
420 | if (!link) | |
421 | break; | |
422 | ||
423 | if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) { | |
424 | ret = dsa_of_setup_routing_table(pd, cd, chip_index, | |
425 | port_index, link); | |
426 | if (ret) | |
427 | return ret; | |
428 | } | |
429 | } | |
430 | return 0; | |
431 | } | |
432 | ||
433 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) | |
434 | { | |
435 | int i; | |
436 | int port_index; | |
437 | ||
438 | for (i = 0; i < pd->nr_chips; i++) { | |
439 | port_index = 0; | |
440 | while (port_index < DSA_MAX_PORTS) { | |
441 | kfree(pd->chip[i].port_names[port_index]); | |
442 | port_index++; | |
443 | } | |
444 | ||
445 | /* Drop our reference to the MDIO bus device */ | |
446 | if (pd->chip[i].host_dev) | |
447 | put_device(pd->chip[i].host_dev); | |
448 | } | |
449 | kfree(pd->chip); | |
450 | } | |
451 | ||
452 | static int dsa_of_probe(struct device *dev) | |
453 | { | |
454 | struct device_node *np = dev->of_node; | |
455 | struct device_node *child, *mdio, *ethernet, *port; | |
456 | struct mii_bus *mdio_bus, *mdio_bus_switch; | |
457 | struct net_device *ethernet_dev; | |
458 | struct dsa_platform_data *pd; | |
459 | struct dsa_chip_data *cd; | |
460 | const char *port_name; | |
461 | int chip_index, port_index; | |
462 | const unsigned int *sw_addr, *port_reg; | |
463 | u32 eeprom_len; | |
464 | int ret; | |
465 | ||
466 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); | |
467 | if (!mdio) | |
468 | return -EINVAL; | |
469 | ||
470 | mdio_bus = of_mdio_find_bus(mdio); | |
471 | if (!mdio_bus) | |
472 | return -EPROBE_DEFER; | |
473 | ||
474 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); | |
475 | if (!ethernet) { | |
476 | ret = -EINVAL; | |
477 | goto out_put_mdio; | |
478 | } | |
479 | ||
480 | ethernet_dev = of_find_net_device_by_node(ethernet); | |
481 | if (!ethernet_dev) { | |
482 | ret = -EPROBE_DEFER; | |
483 | goto out_put_mdio; | |
484 | } | |
485 | ||
486 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); | |
487 | if (!pd) { | |
488 | ret = -ENOMEM; | |
489 | goto out_put_ethernet; | |
490 | } | |
491 | ||
492 | dev->platform_data = pd; | |
493 | pd->of_netdev = ethernet_dev; | |
494 | pd->nr_chips = of_get_available_child_count(np); | |
495 | if (pd->nr_chips > DSA_MAX_SWITCHES) | |
496 | pd->nr_chips = DSA_MAX_SWITCHES; | |
497 | ||
498 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), | |
499 | GFP_KERNEL); | |
500 | if (!pd->chip) { | |
501 | ret = -ENOMEM; | |
502 | goto out_free; | |
503 | } | |
504 | ||
505 | chip_index = -1; | |
506 | for_each_available_child_of_node(np, child) { | |
507 | int i; | |
508 | ||
509 | chip_index++; | |
510 | cd = &pd->chip[chip_index]; | |
511 | ||
512 | cd->of_node = child; | |
513 | ||
514 | /* Initialize the routing table */ | |
515 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) | |
516 | cd->rtable[i] = DSA_RTABLE_NONE; | |
517 | ||
518 | /* When assigning the host device, increment its refcount */ | |
519 | cd->host_dev = get_device(&mdio_bus->dev); | |
520 | ||
521 | sw_addr = of_get_property(child, "reg", NULL); | |
522 | if (!sw_addr) | |
523 | continue; | |
524 | ||
525 | cd->sw_addr = be32_to_cpup(sw_addr); | |
526 | if (cd->sw_addr >= PHY_MAX_ADDR) | |
527 | continue; | |
528 | ||
529 | if (!of_property_read_u32(child, "eeprom-length", &eeprom_len)) | |
530 | cd->eeprom_len = eeprom_len; | |
531 | ||
532 | mdio = of_parse_phandle(child, "mii-bus", 0); | |
533 | if (mdio) { | |
534 | mdio_bus_switch = of_mdio_find_bus(mdio); | |
535 | if (!mdio_bus_switch) { | |
536 | ret = -EPROBE_DEFER; | |
537 | goto out_free_chip; | |
538 | } | |
539 | ||
540 | /* Drop the mdio_bus device ref, replacing the host | |
541 | * device with the mdio_bus_switch device, keeping | |
542 | * the refcount from of_mdio_find_bus() above. | |
543 | */ | |
544 | put_device(cd->host_dev); | |
545 | cd->host_dev = &mdio_bus_switch->dev; | |
546 | } | |
547 | ||
548 | for_each_available_child_of_node(child, port) { | |
549 | port_reg = of_get_property(port, "reg", NULL); | |
550 | if (!port_reg) | |
551 | continue; | |
552 | ||
553 | port_index = be32_to_cpup(port_reg); | |
554 | if (port_index >= DSA_MAX_PORTS) | |
555 | break; | |
556 | ||
557 | port_name = of_get_property(port, "label", NULL); | |
558 | if (!port_name) | |
559 | continue; | |
560 | ||
561 | cd->port_dn[port_index] = port; | |
562 | ||
563 | cd->port_names[port_index] = kstrdup(port_name, | |
564 | GFP_KERNEL); | |
565 | if (!cd->port_names[port_index]) { | |
566 | ret = -ENOMEM; | |
567 | goto out_free_chip; | |
568 | } | |
569 | ||
570 | ret = dsa_of_probe_links(pd, cd, chip_index, | |
571 | port_index, port, port_name); | |
572 | if (ret) | |
573 | goto out_free_chip; | |
574 | ||
575 | } | |
576 | } | |
577 | ||
578 | /* The individual chips hold their own refcount on the mdio bus, | |
579 | * so drop ours */ | |
580 | put_device(&mdio_bus->dev); | |
581 | ||
582 | return 0; | |
583 | ||
584 | out_free_chip: | |
585 | dsa_of_free_platform_data(pd); | |
586 | out_free: | |
587 | kfree(pd); | |
588 | dev->platform_data = NULL; | |
589 | out_put_ethernet: | |
590 | put_device(ðernet_dev->dev); | |
591 | out_put_mdio: | |
592 | put_device(&mdio_bus->dev); | |
593 | return ret; | |
594 | } | |
595 | ||
596 | static void dsa_of_remove(struct device *dev) | |
597 | { | |
598 | struct dsa_platform_data *pd = dev->platform_data; | |
599 | ||
600 | if (!dev->of_node) | |
601 | return; | |
602 | ||
603 | dsa_of_free_platform_data(pd); | |
604 | put_device(&pd->of_netdev->dev); | |
605 | kfree(pd); | |
606 | } | |
607 | #else | |
608 | static inline int dsa_of_probe(struct device *dev) | |
609 | { | |
610 | return 0; | |
611 | } | |
612 | ||
613 | static inline void dsa_of_remove(struct device *dev) | |
614 | { | |
615 | } | |
616 | #endif | |
617 | ||
618 | static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev, | |
619 | struct device *parent, struct dsa_platform_data *pd) | |
620 | { | |
621 | int i; | |
622 | unsigned configured = 0; | |
623 | ||
624 | dst->pd = pd; | |
625 | dst->master_netdev = dev; | |
a6a71f19 VD |
626 | |
627 | for (i = 0; i < pd->nr_chips; i++) { | |
628 | struct dsa_switch *ds; | |
629 | ||
630 | ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev); | |
631 | if (IS_ERR(ds)) { | |
632 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", | |
633 | i, PTR_ERR(ds)); | |
634 | continue; | |
635 | } | |
636 | ||
637 | dst->ds[i] = ds; | |
638 | ||
639 | ++configured; | |
640 | } | |
641 | ||
642 | /* | |
643 | * If no switch was found, exit cleanly | |
644 | */ | |
645 | if (!configured) | |
646 | return -EPROBE_DEFER; | |
647 | ||
648 | /* | |
649 | * If we use a tagging format that doesn't have an ethertype | |
650 | * field, make sure that all packets from this point on get | |
651 | * sent to the tag format's receive function. | |
652 | */ | |
653 | wmb(); | |
02f840cb | 654 | dev->dsa_ptr = dst; |
a6a71f19 VD |
655 | |
656 | return 0; | |
657 | } | |
658 | ||
659 | static int dsa_probe(struct platform_device *pdev) | |
660 | { | |
661 | struct dsa_platform_data *pd = pdev->dev.platform_data; | |
662 | struct net_device *dev; | |
663 | struct dsa_switch_tree *dst; | |
664 | int ret; | |
665 | ||
666 | if (pdev->dev.of_node) { | |
667 | ret = dsa_of_probe(&pdev->dev); | |
668 | if (ret) | |
669 | return ret; | |
670 | ||
671 | pd = pdev->dev.platform_data; | |
672 | } | |
673 | ||
674 | if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL)) | |
675 | return -EINVAL; | |
676 | ||
677 | if (pd->of_netdev) { | |
678 | dev = pd->of_netdev; | |
679 | dev_hold(dev); | |
680 | } else { | |
681 | dev = dsa_dev_to_net_device(pd->netdev); | |
682 | } | |
683 | if (dev == NULL) { | |
684 | ret = -EPROBE_DEFER; | |
685 | goto out; | |
686 | } | |
687 | ||
688 | if (dev->dsa_ptr != NULL) { | |
689 | dev_put(dev); | |
690 | ret = -EEXIST; | |
691 | goto out; | |
692 | } | |
693 | ||
694 | dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL); | |
695 | if (dst == NULL) { | |
696 | dev_put(dev); | |
697 | ret = -ENOMEM; | |
698 | goto out; | |
699 | } | |
700 | ||
701 | platform_set_drvdata(pdev, dst); | |
702 | ||
703 | ret = dsa_setup_dst(dst, dev, &pdev->dev, pd); | |
704 | if (ret) { | |
705 | dev_put(dev); | |
706 | goto out; | |
707 | } | |
708 | ||
709 | return 0; | |
710 | ||
711 | out: | |
712 | dsa_of_remove(&pdev->dev); | |
713 | ||
714 | return ret; | |
715 | } | |
716 | ||
717 | static void dsa_remove_dst(struct dsa_switch_tree *dst) | |
718 | { | |
719 | int i; | |
720 | ||
721 | dst->master_netdev->dsa_ptr = NULL; | |
722 | ||
723 | /* If we used a tagging format that doesn't have an ethertype | |
724 | * field, make sure that all packets from this point get sent | |
725 | * without the tag and go through the regular receive path. | |
726 | */ | |
727 | wmb(); | |
728 | ||
729 | for (i = 0; i < dst->pd->nr_chips; i++) { | |
730 | struct dsa_switch *ds = dst->ds[i]; | |
731 | ||
732 | if (ds) | |
733 | dsa_switch_destroy(ds); | |
734 | } | |
735 | ||
937c7df8 | 736 | dsa_cpu_port_ethtool_restore(dst->cpu_dp); |
a6a71f19 VD |
737 | |
738 | dev_put(dst->master_netdev); | |
739 | } | |
740 | ||
741 | static int dsa_remove(struct platform_device *pdev) | |
742 | { | |
743 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); | |
744 | ||
745 | dsa_remove_dst(dst); | |
746 | dsa_of_remove(&pdev->dev); | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static void dsa_shutdown(struct platform_device *pdev) | |
752 | { | |
753 | } | |
754 | ||
755 | #ifdef CONFIG_PM_SLEEP | |
756 | static int dsa_suspend(struct device *d) | |
757 | { | |
758 | struct platform_device *pdev = to_platform_device(d); | |
759 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); | |
760 | int i, ret = 0; | |
761 | ||
762 | for (i = 0; i < dst->pd->nr_chips; i++) { | |
763 | struct dsa_switch *ds = dst->ds[i]; | |
764 | ||
765 | if (ds != NULL) | |
766 | ret = dsa_switch_suspend(ds); | |
767 | } | |
768 | ||
769 | return ret; | |
770 | } | |
771 | ||
772 | static int dsa_resume(struct device *d) | |
773 | { | |
774 | struct platform_device *pdev = to_platform_device(d); | |
775 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); | |
776 | int i, ret = 0; | |
777 | ||
778 | for (i = 0; i < dst->pd->nr_chips; i++) { | |
779 | struct dsa_switch *ds = dst->ds[i]; | |
780 | ||
781 | if (ds != NULL) | |
782 | ret = dsa_switch_resume(ds); | |
783 | } | |
784 | ||
785 | return ret; | |
786 | } | |
787 | #endif | |
788 | ||
789 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); | |
790 | ||
791 | static const struct of_device_id dsa_of_match_table[] = { | |
792 | { .compatible = "marvell,dsa", }, | |
793 | {} | |
794 | }; | |
795 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); | |
796 | ||
797 | static struct platform_driver dsa_driver = { | |
798 | .probe = dsa_probe, | |
799 | .remove = dsa_remove, | |
800 | .shutdown = dsa_shutdown, | |
801 | .driver = { | |
802 | .name = "dsa", | |
803 | .of_match_table = dsa_of_match_table, | |
804 | .pm = &dsa_pm_ops, | |
805 | }, | |
806 | }; | |
807 | ||
808 | int dsa_legacy_register(void) | |
809 | { | |
810 | return platform_driver_register(&dsa_driver); | |
811 | } | |
812 | ||
813 | void dsa_legacy_unregister(void) | |
814 | { | |
815 | platform_driver_unregister(&dsa_driver); | |
816 | } |