]>
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 | ||
51579c3f GR |
12 | #include <linux/ctype.h> |
13 | #include <linux/device.h> | |
14 | #include <linux/hwmon.h> | |
91da11f8 | 15 | #include <linux/list.h> |
91da11f8 | 16 | #include <linux/platform_device.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
3a9a231d | 18 | #include <linux/module.h> |
91da11f8 | 19 | #include <net/dsa.h> |
5e95329b FF |
20 | #include <linux/of.h> |
21 | #include <linux/of_mdio.h> | |
22 | #include <linux/of_platform.h> | |
51579c3f | 23 | #include <linux/sysfs.h> |
91da11f8 LB |
24 | #include "dsa_priv.h" |
25 | ||
26 | char dsa_driver_version[] = "0.1"; | |
27 | ||
28 | ||
29 | /* switch driver registration ***********************************************/ | |
30 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); | |
31 | static LIST_HEAD(dsa_switch_drivers); | |
32 | ||
33 | void 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 | } | |
ad293b8a | 39 | EXPORT_SYMBOL_GPL(register_switch_driver); |
91da11f8 LB |
40 | |
41 | void 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 | } | |
ad293b8a | 47 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
91da11f8 LB |
48 | |
49 | static struct dsa_switch_driver * | |
b4d2394d | 50 | dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name) |
91da11f8 LB |
51 | { |
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 | ||
b4d2394d | 65 | name = drv->probe(host_dev, sw_addr); |
91da11f8 LB |
66 | 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 | ||
51579c3f GR |
78 | /* hwmon support ************************************************************/ |
79 | ||
80 | #ifdef CONFIG_NET_DSA_HWMON | |
81 | ||
82 | static 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 | } | |
94 | static DEVICE_ATTR_RO(temp1_input); | |
95 | ||
96 | static 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 | ||
109 | static 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 | } | |
126 | static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store); | |
127 | ||
128 | static 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 | } | |
141 | static DEVICE_ATTR_RO(temp1_max_alarm); | |
142 | ||
143 | static 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 | ||
150 | static 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 | ||
169 | static 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 */ | |
91da11f8 LB |
176 | |
177 | /* basic switch operations **************************************************/ | |
df197195 | 178 | static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent) |
91da11f8 | 179 | { |
df197195 FF |
180 | struct dsa_switch_driver *drv = ds->drv; |
181 | struct dsa_switch_tree *dst = ds->dst; | |
182 | struct dsa_chip_data *pd = ds->pd; | |
f9bf5a2c | 183 | bool valid_name_found = false; |
df197195 FF |
184 | int index = ds->index; |
185 | int i, ret; | |
91da11f8 LB |
186 | |
187 | /* | |
188 | * Validate supplied switch configuration. | |
189 | */ | |
91da11f8 LB |
190 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
191 | char *name; | |
192 | ||
193 | name = pd->port_names[i]; | |
194 | if (name == NULL) | |
195 | continue; | |
196 | ||
197 | if (!strcmp(name, "cpu")) { | |
e84665c9 | 198 | if (dst->cpu_switch != -1) { |
a2ae6007 JP |
199 | netdev_err(dst->master_netdev, |
200 | "multiple cpu ports?!\n"); | |
91da11f8 LB |
201 | ret = -EINVAL; |
202 | goto out; | |
203 | } | |
e84665c9 LB |
204 | dst->cpu_switch = index; |
205 | dst->cpu_port = i; | |
206 | } else if (!strcmp(name, "dsa")) { | |
207 | ds->dsa_port_mask |= 1 << i; | |
91da11f8 | 208 | } else { |
e84665c9 | 209 | ds->phys_port_mask |= 1 << i; |
91da11f8 | 210 | } |
f9bf5a2c | 211 | valid_name_found = true; |
91da11f8 LB |
212 | } |
213 | ||
f9bf5a2c FF |
214 | if (!valid_name_found && i == DSA_MAX_PORTS) { |
215 | ret = -EINVAL; | |
216 | goto out; | |
217 | } | |
91da11f8 | 218 | |
0d8bcdd3 FF |
219 | /* Make the built-in MII bus mask match the number of ports, |
220 | * switch drivers can override this later | |
221 | */ | |
222 | ds->phys_mii_mask = ds->phys_port_mask; | |
223 | ||
91da11f8 | 224 | /* |
e84665c9 LB |
225 | * If the CPU connects to this switch, set the switch tree |
226 | * tagging protocol to the preferred tagging format of this | |
227 | * switch. | |
91da11f8 | 228 | */ |
5075314e | 229 | if (dst->cpu_switch == index) { |
59299031 | 230 | switch (ds->tag_protocol) { |
5075314e AD |
231 | #ifdef CONFIG_NET_DSA_TAG_DSA |
232 | case DSA_TAG_PROTO_DSA: | |
233 | dst->rcv = dsa_netdev_ops.rcv; | |
234 | break; | |
235 | #endif | |
236 | #ifdef CONFIG_NET_DSA_TAG_EDSA | |
237 | case DSA_TAG_PROTO_EDSA: | |
238 | dst->rcv = edsa_netdev_ops.rcv; | |
239 | break; | |
240 | #endif | |
241 | #ifdef CONFIG_NET_DSA_TAG_TRAILER | |
242 | case DSA_TAG_PROTO_TRAILER: | |
243 | dst->rcv = trailer_netdev_ops.rcv; | |
244 | break; | |
245 | #endif | |
246 | #ifdef CONFIG_NET_DSA_TAG_BRCM | |
247 | case DSA_TAG_PROTO_BRCM: | |
248 | dst->rcv = brcm_netdev_ops.rcv; | |
249 | break; | |
250 | #endif | |
ae439286 | 251 | case DSA_TAG_PROTO_NONE: |
5075314e | 252 | break; |
ae439286 AL |
253 | default: |
254 | ret = -ENOPROTOOPT; | |
255 | goto out; | |
5075314e | 256 | } |
91da11f8 | 257 | |
59299031 | 258 | dst->tag_protocol = ds->tag_protocol; |
5075314e | 259 | } |
91da11f8 LB |
260 | |
261 | /* | |
262 | * Do basic register setup. | |
263 | */ | |
264 | ret = drv->setup(ds); | |
265 | if (ret < 0) | |
266 | goto out; | |
267 | ||
e84665c9 | 268 | ret = drv->set_addr(ds, dst->master_netdev->dev_addr); |
91da11f8 LB |
269 | if (ret < 0) |
270 | goto out; | |
271 | ||
272 | ds->slave_mii_bus = mdiobus_alloc(); | |
273 | if (ds->slave_mii_bus == NULL) { | |
274 | ret = -ENOMEM; | |
275 | goto out; | |
276 | } | |
277 | dsa_slave_mii_bus_init(ds); | |
278 | ||
279 | ret = mdiobus_register(ds->slave_mii_bus); | |
280 | if (ret < 0) | |
281 | goto out_free; | |
282 | ||
283 | ||
284 | /* | |
285 | * Create network devices for physical switch ports. | |
286 | */ | |
91da11f8 | 287 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
e84665c9 | 288 | if (!(ds->phys_port_mask & (1 << i))) |
91da11f8 LB |
289 | continue; |
290 | ||
d87d6f44 GR |
291 | ret = dsa_slave_create(ds, parent, i, pd->port_names[i]); |
292 | if (ret < 0) { | |
a2ae6007 JP |
293 | netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n", |
294 | index, i, pd->port_names[i]); | |
d87d6f44 | 295 | ret = 0; |
91da11f8 | 296 | } |
91da11f8 LB |
297 | } |
298 | ||
51579c3f GR |
299 | #ifdef CONFIG_NET_DSA_HWMON |
300 | /* If the switch provides a temperature sensor, | |
301 | * register with hardware monitoring subsystem. | |
302 | * Treat registration error as non-fatal and ignore it. | |
303 | */ | |
304 | if (drv->get_temp) { | |
305 | const char *netname = netdev_name(dst->master_netdev); | |
306 | char hname[IFNAMSIZ + 1]; | |
307 | int i, j; | |
308 | ||
309 | /* Create valid hwmon 'name' attribute */ | |
310 | for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) { | |
311 | if (isalnum(netname[i])) | |
312 | hname[j++] = netname[i]; | |
313 | } | |
314 | hname[j] = '\0'; | |
315 | scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d", | |
316 | hname, index); | |
317 | ds->hwmon_dev = hwmon_device_register_with_groups(NULL, | |
318 | ds->hwmon_name, ds, dsa_hwmon_groups); | |
319 | if (IS_ERR(ds->hwmon_dev)) | |
320 | ds->hwmon_dev = NULL; | |
321 | } | |
322 | #endif /* CONFIG_NET_DSA_HWMON */ | |
323 | ||
df197195 | 324 | return ret; |
91da11f8 LB |
325 | |
326 | out_free: | |
327 | mdiobus_free(ds->slave_mii_bus); | |
328 | out: | |
91da11f8 | 329 | kfree(ds); |
df197195 FF |
330 | return ret; |
331 | } | |
332 | ||
333 | static struct dsa_switch * | |
334 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, | |
335 | struct device *parent, struct device *host_dev) | |
336 | { | |
337 | struct dsa_chip_data *pd = dst->pd->chip + index; | |
338 | struct dsa_switch_driver *drv; | |
339 | struct dsa_switch *ds; | |
340 | int ret; | |
341 | char *name; | |
342 | ||
343 | /* | |
344 | * Probe for switch model. | |
345 | */ | |
346 | drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); | |
347 | if (drv == NULL) { | |
348 | netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", | |
349 | index); | |
350 | return ERR_PTR(-EINVAL); | |
351 | } | |
352 | netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", | |
353 | index, name); | |
354 | ||
355 | ||
356 | /* | |
357 | * Allocate and initialise switch state. | |
358 | */ | |
359 | ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL); | |
360 | if (ds == NULL) | |
361 | return NULL; | |
362 | ||
363 | ds->dst = dst; | |
364 | ds->index = index; | |
365 | ds->pd = pd; | |
366 | ds->drv = drv; | |
59299031 | 367 | ds->tag_protocol = drv->tag_protocol; |
df197195 FF |
368 | ds->master_dev = host_dev; |
369 | ||
370 | ret = dsa_switch_setup_one(ds, parent); | |
371 | if (ret) | |
372 | return NULL; | |
373 | ||
374 | return ds; | |
91da11f8 LB |
375 | } |
376 | ||
377 | static void dsa_switch_destroy(struct dsa_switch *ds) | |
378 | { | |
51579c3f GR |
379 | #ifdef CONFIG_NET_DSA_HWMON |
380 | if (ds->hwmon_dev) | |
381 | hwmon_device_unregister(ds->hwmon_dev); | |
382 | #endif | |
91da11f8 LB |
383 | } |
384 | ||
e506d405 | 385 | #ifdef CONFIG_PM_SLEEP |
24462549 FF |
386 | static int dsa_switch_suspend(struct dsa_switch *ds) |
387 | { | |
388 | int i, ret = 0; | |
389 | ||
390 | /* Suspend slave network devices */ | |
391 | for (i = 0; i < DSA_MAX_PORTS; i++) { | |
d79d2107 | 392 | if (!dsa_is_port_initialized(ds, i)) |
24462549 FF |
393 | continue; |
394 | ||
395 | ret = dsa_slave_suspend(ds->ports[i]); | |
396 | if (ret) | |
397 | return ret; | |
398 | } | |
399 | ||
400 | if (ds->drv->suspend) | |
401 | ret = ds->drv->suspend(ds); | |
402 | ||
403 | return ret; | |
404 | } | |
405 | ||
406 | static int dsa_switch_resume(struct dsa_switch *ds) | |
407 | { | |
408 | int i, ret = 0; | |
409 | ||
410 | if (ds->drv->resume) | |
411 | ret = ds->drv->resume(ds); | |
412 | ||
413 | if (ret) | |
414 | return ret; | |
415 | ||
416 | /* Resume slave network devices */ | |
417 | for (i = 0; i < DSA_MAX_PORTS; i++) { | |
d79d2107 | 418 | if (!dsa_is_port_initialized(ds, i)) |
24462549 FF |
419 | continue; |
420 | ||
421 | ret = dsa_slave_resume(ds->ports[i]); | |
422 | if (ret) | |
423 | return ret; | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
e506d405 | 428 | #endif |
24462549 | 429 | |
91da11f8 LB |
430 | |
431 | /* link polling *************************************************************/ | |
432 | static void dsa_link_poll_work(struct work_struct *ugly) | |
433 | { | |
e84665c9 LB |
434 | struct dsa_switch_tree *dst; |
435 | int i; | |
436 | ||
437 | dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); | |
91da11f8 | 438 | |
e84665c9 LB |
439 | for (i = 0; i < dst->pd->nr_chips; i++) { |
440 | struct dsa_switch *ds = dst->ds[i]; | |
91da11f8 | 441 | |
e84665c9 LB |
442 | if (ds != NULL && ds->drv->poll_link != NULL) |
443 | ds->drv->poll_link(ds); | |
444 | } | |
445 | ||
446 | mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ)); | |
91da11f8 LB |
447 | } |
448 | ||
e84665c9 | 449 | static void dsa_link_poll_timer(unsigned long _dst) |
91da11f8 | 450 | { |
e84665c9 | 451 | struct dsa_switch_tree *dst = (void *)_dst; |
91da11f8 | 452 | |
e84665c9 | 453 | schedule_work(&dst->link_poll_work); |
91da11f8 LB |
454 | } |
455 | ||
456 | ||
457 | /* platform driver init and cleanup *****************************************/ | |
458 | static int dev_is_class(struct device *dev, void *class) | |
459 | { | |
460 | if (dev->class != NULL && !strcmp(dev->class->name, class)) | |
461 | return 1; | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | static struct device *dev_find_class(struct device *parent, char *class) | |
467 | { | |
468 | if (dev_is_class(parent, class)) { | |
469 | get_device(parent); | |
470 | return parent; | |
471 | } | |
472 | ||
473 | return device_find_child(parent, class, dev_is_class); | |
474 | } | |
475 | ||
b4d2394d | 476 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
91da11f8 LB |
477 | { |
478 | struct device *d; | |
479 | ||
480 | d = dev_find_class(dev, "mdio_bus"); | |
481 | if (d != NULL) { | |
482 | struct mii_bus *bus; | |
483 | ||
484 | bus = to_mii_bus(d); | |
485 | put_device(d); | |
486 | ||
487 | return bus; | |
488 | } | |
489 | ||
490 | return NULL; | |
491 | } | |
b4d2394d | 492 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
91da11f8 LB |
493 | |
494 | static struct net_device *dev_to_net_device(struct device *dev) | |
495 | { | |
496 | struct device *d; | |
497 | ||
498 | d = dev_find_class(dev, "net"); | |
499 | if (d != NULL) { | |
500 | struct net_device *nd; | |
501 | ||
502 | nd = to_net_dev(d); | |
503 | dev_hold(nd); | |
504 | put_device(d); | |
505 | ||
506 | return nd; | |
507 | } | |
508 | ||
509 | return NULL; | |
510 | } | |
511 | ||
5e95329b FF |
512 | #ifdef CONFIG_OF |
513 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, | |
514 | struct dsa_chip_data *cd, | |
515 | int chip_index, | |
516 | struct device_node *link) | |
517 | { | |
518 | int ret; | |
519 | const __be32 *reg; | |
520 | int link_port_addr; | |
521 | int link_sw_addr; | |
522 | struct device_node *parent_sw; | |
523 | int len; | |
524 | ||
525 | parent_sw = of_get_parent(link); | |
526 | if (!parent_sw) | |
527 | return -EINVAL; | |
528 | ||
529 | reg = of_get_property(parent_sw, "reg", &len); | |
530 | if (!reg || (len != sizeof(*reg) * 2)) | |
531 | return -EINVAL; | |
532 | ||
533 | link_sw_addr = be32_to_cpup(reg + 1); | |
534 | ||
535 | if (link_sw_addr >= pd->nr_chips) | |
536 | return -EINVAL; | |
537 | ||
538 | /* First time routing table allocation */ | |
539 | if (!cd->rtable) { | |
5bc4b46a FF |
540 | cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8), |
541 | GFP_KERNEL); | |
5e95329b FF |
542 | if (!cd->rtable) |
543 | return -ENOMEM; | |
544 | ||
545 | /* default to no valid uplink/downlink */ | |
546 | memset(cd->rtable, -1, pd->nr_chips * sizeof(s8)); | |
547 | } | |
548 | ||
549 | reg = of_get_property(link, "reg", NULL); | |
550 | if (!reg) { | |
551 | ret = -EINVAL; | |
552 | goto out; | |
553 | } | |
554 | ||
555 | link_port_addr = be32_to_cpup(reg); | |
556 | ||
557 | cd->rtable[link_sw_addr] = link_port_addr; | |
558 | ||
559 | return 0; | |
560 | out: | |
561 | kfree(cd->rtable); | |
562 | return ret; | |
563 | } | |
564 | ||
21168245 FF |
565 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
566 | { | |
567 | int i; | |
568 | int port_index; | |
569 | ||
570 | for (i = 0; i < pd->nr_chips; i++) { | |
571 | port_index = 0; | |
5f64a7db | 572 | while (port_index < DSA_MAX_PORTS) { |
1f74714f | 573 | kfree(pd->chip[i].port_names[port_index]); |
5f64a7db FF |
574 | port_index++; |
575 | } | |
21168245 FF |
576 | kfree(pd->chip[i].rtable); |
577 | } | |
578 | kfree(pd->chip); | |
579 | } | |
580 | ||
f1a26a06 | 581 | static int dsa_of_probe(struct device *dev) |
5e95329b | 582 | { |
f1a26a06 | 583 | struct device_node *np = dev->of_node; |
5e95329b FF |
584 | struct device_node *child, *mdio, *ethernet, *port, *link; |
585 | struct mii_bus *mdio_bus; | |
586 | struct platform_device *ethernet_dev; | |
587 | struct dsa_platform_data *pd; | |
588 | struct dsa_chip_data *cd; | |
589 | const char *port_name; | |
590 | int chip_index, port_index; | |
591 | const unsigned int *sw_addr, *port_reg; | |
6793abb4 | 592 | u32 eeprom_len; |
21168245 | 593 | int ret; |
5e95329b FF |
594 | |
595 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); | |
596 | if (!mdio) | |
597 | return -EINVAL; | |
598 | ||
599 | mdio_bus = of_mdio_find_bus(mdio); | |
600 | if (!mdio_bus) | |
b324c07a | 601 | return -EPROBE_DEFER; |
5e95329b FF |
602 | |
603 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); | |
604 | if (!ethernet) | |
605 | return -EINVAL; | |
606 | ||
607 | ethernet_dev = of_find_device_by_node(ethernet); | |
608 | if (!ethernet_dev) | |
b324c07a | 609 | return -EPROBE_DEFER; |
5e95329b FF |
610 | |
611 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); | |
612 | if (!pd) | |
613 | return -ENOMEM; | |
614 | ||
f1a26a06 | 615 | dev->platform_data = pd; |
5e95329b | 616 | pd->netdev = ðernet_dev->dev; |
e04449fc | 617 | pd->nr_chips = of_get_available_child_count(np); |
5e95329b FF |
618 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
619 | pd->nr_chips = DSA_MAX_SWITCHES; | |
620 | ||
6f2aed6a FF |
621 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), |
622 | GFP_KERNEL); | |
5e95329b FF |
623 | if (!pd->chip) { |
624 | ret = -ENOMEM; | |
625 | goto out_free; | |
626 | } | |
627 | ||
d1c0b471 | 628 | chip_index = -1; |
5e95329b | 629 | for_each_available_child_of_node(np, child) { |
d1c0b471 | 630 | chip_index++; |
5e95329b FF |
631 | cd = &pd->chip[chip_index]; |
632 | ||
fa981d9a | 633 | cd->of_node = child; |
c1f570a6 | 634 | cd->host_dev = &mdio_bus->dev; |
5e95329b FF |
635 | |
636 | sw_addr = of_get_property(child, "reg", NULL); | |
637 | if (!sw_addr) | |
638 | continue; | |
639 | ||
640 | cd->sw_addr = be32_to_cpup(sw_addr); | |
641 | if (cd->sw_addr > PHY_MAX_ADDR) | |
642 | continue; | |
643 | ||
6793abb4 GR |
644 | if (!of_property_read_u32(np, "eeprom-length", &eeprom_len)) |
645 | cd->eeprom_len = eeprom_len; | |
646 | ||
5e95329b FF |
647 | for_each_available_child_of_node(child, port) { |
648 | port_reg = of_get_property(port, "reg", NULL); | |
649 | if (!port_reg) | |
650 | continue; | |
651 | ||
652 | port_index = be32_to_cpup(port_reg); | |
653 | ||
654 | port_name = of_get_property(port, "label", NULL); | |
655 | if (!port_name) | |
656 | continue; | |
657 | ||
bd47497a FF |
658 | cd->port_dn[port_index] = port; |
659 | ||
5e95329b FF |
660 | cd->port_names[port_index] = kstrdup(port_name, |
661 | GFP_KERNEL); | |
662 | if (!cd->port_names[port_index]) { | |
663 | ret = -ENOMEM; | |
664 | goto out_free_chip; | |
665 | } | |
666 | ||
667 | link = of_parse_phandle(port, "link", 0); | |
668 | ||
669 | if (!strcmp(port_name, "dsa") && link && | |
670 | pd->nr_chips > 1) { | |
671 | ret = dsa_of_setup_routing_table(pd, cd, | |
672 | chip_index, link); | |
673 | if (ret) | |
674 | goto out_free_chip; | |
675 | } | |
676 | ||
677 | if (port_index == DSA_MAX_PORTS) | |
678 | break; | |
679 | } | |
680 | } | |
681 | ||
682 | return 0; | |
683 | ||
684 | out_free_chip: | |
21168245 | 685 | dsa_of_free_platform_data(pd); |
5e95329b FF |
686 | out_free: |
687 | kfree(pd); | |
f1a26a06 | 688 | dev->platform_data = NULL; |
5e95329b FF |
689 | return ret; |
690 | } | |
691 | ||
f1a26a06 | 692 | static void dsa_of_remove(struct device *dev) |
5e95329b | 693 | { |
f1a26a06 | 694 | struct dsa_platform_data *pd = dev->platform_data; |
5e95329b | 695 | |
f1a26a06 | 696 | if (!dev->of_node) |
5e95329b FF |
697 | return; |
698 | ||
21168245 | 699 | dsa_of_free_platform_data(pd); |
5e95329b FF |
700 | kfree(pd); |
701 | } | |
702 | #else | |
f1a26a06 | 703 | static inline int dsa_of_probe(struct device *dev) |
5e95329b FF |
704 | { |
705 | return 0; | |
706 | } | |
707 | ||
f1a26a06 | 708 | static inline void dsa_of_remove(struct device *dev) |
5e95329b FF |
709 | { |
710 | } | |
711 | #endif | |
712 | ||
91da11f8 LB |
713 | static int dsa_probe(struct platform_device *pdev) |
714 | { | |
91da11f8 LB |
715 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
716 | struct net_device *dev; | |
e84665c9 | 717 | struct dsa_switch_tree *dst; |
5e95329b | 718 | int i, ret; |
91da11f8 | 719 | |
a2ae6007 JP |
720 | pr_notice_once("Distributed Switch Architecture driver version %s\n", |
721 | dsa_driver_version); | |
91da11f8 | 722 | |
5e95329b | 723 | if (pdev->dev.of_node) { |
f1a26a06 | 724 | ret = dsa_of_probe(&pdev->dev); |
5e95329b FF |
725 | if (ret) |
726 | return ret; | |
727 | ||
728 | pd = pdev->dev.platform_data; | |
729 | } | |
730 | ||
e84665c9 | 731 | if (pd == NULL || pd->netdev == NULL) |
91da11f8 LB |
732 | return -EINVAL; |
733 | ||
734 | dev = dev_to_net_device(pd->netdev); | |
5e95329b | 735 | if (dev == NULL) { |
b324c07a | 736 | ret = -EPROBE_DEFER; |
5e95329b FF |
737 | goto out; |
738 | } | |
91da11f8 LB |
739 | |
740 | if (dev->dsa_ptr != NULL) { | |
741 | dev_put(dev); | |
5e95329b FF |
742 | ret = -EEXIST; |
743 | goto out; | |
91da11f8 LB |
744 | } |
745 | ||
e84665c9 LB |
746 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
747 | if (dst == NULL) { | |
91da11f8 | 748 | dev_put(dev); |
5e95329b FF |
749 | ret = -ENOMEM; |
750 | goto out; | |
91da11f8 LB |
751 | } |
752 | ||
e84665c9 LB |
753 | platform_set_drvdata(pdev, dst); |
754 | ||
755 | dst->pd = pd; | |
756 | dst->master_netdev = dev; | |
757 | dst->cpu_switch = -1; | |
758 | dst->cpu_port = -1; | |
759 | ||
760 | for (i = 0; i < pd->nr_chips; i++) { | |
e84665c9 LB |
761 | struct dsa_switch *ds; |
762 | ||
b4d2394d | 763 | ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev); |
e84665c9 | 764 | if (IS_ERR(ds)) { |
a2ae6007 JP |
765 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", |
766 | i, PTR_ERR(ds)); | |
e84665c9 LB |
767 | continue; |
768 | } | |
769 | ||
770 | dst->ds[i] = ds; | |
771 | if (ds->drv->poll_link != NULL) | |
772 | dst->link_poll_needed = 1; | |
91da11f8 LB |
773 | } |
774 | ||
e84665c9 LB |
775 | /* |
776 | * If we use a tagging format that doesn't have an ethertype | |
777 | * field, make sure that all packets from this point on get | |
778 | * sent to the tag format's receive function. | |
779 | */ | |
780 | wmb(); | |
781 | dev->dsa_ptr = (void *)dst; | |
782 | ||
783 | if (dst->link_poll_needed) { | |
784 | INIT_WORK(&dst->link_poll_work, dsa_link_poll_work); | |
785 | init_timer(&dst->link_poll_timer); | |
786 | dst->link_poll_timer.data = (unsigned long)dst; | |
787 | dst->link_poll_timer.function = dsa_link_poll_timer; | |
788 | dst->link_poll_timer.expires = round_jiffies(jiffies + HZ); | |
789 | add_timer(&dst->link_poll_timer); | |
790 | } | |
91da11f8 LB |
791 | |
792 | return 0; | |
5e95329b FF |
793 | |
794 | out: | |
f1a26a06 | 795 | dsa_of_remove(&pdev->dev); |
5e95329b FF |
796 | |
797 | return ret; | |
91da11f8 LB |
798 | } |
799 | ||
800 | static int dsa_remove(struct platform_device *pdev) | |
801 | { | |
e84665c9 LB |
802 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
803 | int i; | |
91da11f8 | 804 | |
e84665c9 LB |
805 | if (dst->link_poll_needed) |
806 | del_timer_sync(&dst->link_poll_timer); | |
91da11f8 | 807 | |
43829731 | 808 | flush_work(&dst->link_poll_work); |
91da11f8 | 809 | |
e84665c9 LB |
810 | for (i = 0; i < dst->pd->nr_chips; i++) { |
811 | struct dsa_switch *ds = dst->ds[i]; | |
812 | ||
813 | if (ds != NULL) | |
814 | dsa_switch_destroy(ds); | |
815 | } | |
91da11f8 | 816 | |
f1a26a06 | 817 | dsa_of_remove(&pdev->dev); |
5e95329b | 818 | |
91da11f8 LB |
819 | return 0; |
820 | } | |
821 | ||
822 | static void dsa_shutdown(struct platform_device *pdev) | |
823 | { | |
824 | } | |
825 | ||
3e8a72d1 FF |
826 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
827 | struct packet_type *pt, struct net_device *orig_dev) | |
828 | { | |
829 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
830 | ||
831 | if (unlikely(dst == NULL)) { | |
832 | kfree_skb(skb); | |
833 | return 0; | |
834 | } | |
835 | ||
5075314e | 836 | return dst->rcv(skb, dev, pt, orig_dev); |
3e8a72d1 FF |
837 | } |
838 | ||
61b7363f | 839 | static struct packet_type dsa_pack_type __read_mostly = { |
3e8a72d1 FF |
840 | .type = cpu_to_be16(ETH_P_XDSA), |
841 | .func = dsa_switch_rcv, | |
842 | }; | |
843 | ||
b73adef6 FF |
844 | static struct notifier_block dsa_netdevice_nb __read_mostly = { |
845 | .notifier_call = dsa_slave_netdevice_event, | |
846 | }; | |
847 | ||
24462549 FF |
848 | #ifdef CONFIG_PM_SLEEP |
849 | static int dsa_suspend(struct device *d) | |
850 | { | |
851 | struct platform_device *pdev = to_platform_device(d); | |
852 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); | |
853 | int i, ret = 0; | |
854 | ||
855 | for (i = 0; i < dst->pd->nr_chips; i++) { | |
856 | struct dsa_switch *ds = dst->ds[i]; | |
857 | ||
858 | if (ds != NULL) | |
859 | ret = dsa_switch_suspend(ds); | |
860 | } | |
861 | ||
862 | return ret; | |
863 | } | |
864 | ||
865 | static int dsa_resume(struct device *d) | |
866 | { | |
867 | struct platform_device *pdev = to_platform_device(d); | |
868 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); | |
869 | int i, ret = 0; | |
870 | ||
871 | for (i = 0; i < dst->pd->nr_chips; i++) { | |
872 | struct dsa_switch *ds = dst->ds[i]; | |
873 | ||
874 | if (ds != NULL) | |
875 | ret = dsa_switch_resume(ds); | |
876 | } | |
877 | ||
878 | return ret; | |
879 | } | |
880 | #endif | |
881 | ||
882 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); | |
883 | ||
5e95329b | 884 | static const struct of_device_id dsa_of_match_table[] = { |
246d7f77 | 885 | { .compatible = "brcm,bcm7445-switch-v4.0" }, |
5e95329b FF |
886 | { .compatible = "marvell,dsa", }, |
887 | {} | |
888 | }; | |
889 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); | |
890 | ||
91da11f8 LB |
891 | static struct platform_driver dsa_driver = { |
892 | .probe = dsa_probe, | |
893 | .remove = dsa_remove, | |
894 | .shutdown = dsa_shutdown, | |
895 | .driver = { | |
896 | .name = "dsa", | |
5e95329b | 897 | .of_match_table = dsa_of_match_table, |
24462549 | 898 | .pm = &dsa_pm_ops, |
91da11f8 LB |
899 | }, |
900 | }; | |
901 | ||
902 | static int __init dsa_init_module(void) | |
903 | { | |
7df899c3 BH |
904 | int rc; |
905 | ||
b73adef6 FF |
906 | register_netdevice_notifier(&dsa_netdevice_nb); |
907 | ||
7df899c3 BH |
908 | rc = platform_driver_register(&dsa_driver); |
909 | if (rc) | |
910 | return rc; | |
911 | ||
3e8a72d1 FF |
912 | dev_add_pack(&dsa_pack_type); |
913 | ||
7df899c3 | 914 | return 0; |
91da11f8 LB |
915 | } |
916 | module_init(dsa_init_module); | |
917 | ||
918 | static void __exit dsa_cleanup_module(void) | |
919 | { | |
b73adef6 | 920 | unregister_netdevice_notifier(&dsa_netdevice_nb); |
3e8a72d1 | 921 | dev_remove_pack(&dsa_pack_type); |
91da11f8 LB |
922 | platform_driver_unregister(&dsa_driver); |
923 | } | |
924 | module_exit(dsa_cleanup_module); | |
925 | ||
577d6a7c | 926 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
91da11f8 LB |
927 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
928 | MODULE_LICENSE("GPL"); | |
929 | MODULE_ALIAS("platform:dsa"); |