]>
Commit | Line | Data |
---|---|---|
83c0afae AL |
1 | /* |
2 | * net/dsa/dsa2.c - Hardware switch handling, binding version 2 | |
3 | * Copyright (c) 2008-2009 Marvell Semiconductor | |
4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> | |
5 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | */ | |
12 | ||
13 | #include <linux/device.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/list.h> | |
c6e970a0 | 16 | #include <linux/netdevice.h> |
83c0afae AL |
17 | #include <linux/slab.h> |
18 | #include <linux/rtnetlink.h> | |
83c0afae AL |
19 | #include <linux/of.h> |
20 | #include <linux/of_net.h> | |
ea5dd34b | 21 | |
83c0afae AL |
22 | #include "dsa_priv.h" |
23 | ||
1ca28ec9 | 24 | static LIST_HEAD(dsa_tree_list); |
83c0afae AL |
25 | static DEFINE_MUTEX(dsa2_mutex); |
26 | ||
96567d5d AL |
27 | static const struct devlink_ops dsa_devlink_ops = { |
28 | }; | |
29 | ||
1ca28ec9 | 30 | static struct dsa_switch_tree *dsa_tree_find(int index) |
83c0afae AL |
31 | { |
32 | struct dsa_switch_tree *dst; | |
33 | ||
1ca28ec9 | 34 | list_for_each_entry(dst, &dsa_tree_list, list) |
8e5bf975 | 35 | if (dst->index == index) |
83c0afae | 36 | return dst; |
8e5bf975 | 37 | |
83c0afae AL |
38 | return NULL; |
39 | } | |
40 | ||
1ca28ec9 | 41 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
83c0afae AL |
42 | { |
43 | struct dsa_switch_tree *dst; | |
44 | ||
45 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
46 | if (!dst) | |
47 | return NULL; | |
1ca28ec9 | 48 | |
49463b7f | 49 | dst->index = index; |
1ca28ec9 | 50 | |
83c0afae | 51 | INIT_LIST_HEAD(&dst->list); |
1ca28ec9 | 52 | list_add_tail(&dsa_tree_list, &dst->list); |
8e5bf975 | 53 | |
83c0afae AL |
54 | kref_init(&dst->refcount); |
55 | ||
56 | return dst; | |
57 | } | |
58 | ||
65254108 VD |
59 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
60 | { | |
61 | list_del(&dst->list); | |
62 | kfree(dst); | |
63 | } | |
64 | ||
9e741045 | 65 | static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst) |
1ca28ec9 | 66 | { |
9e741045 VD |
67 | if (dst) |
68 | kref_get(&dst->refcount); | |
1ca28ec9 VD |
69 | |
70 | return dst; | |
71 | } | |
72 | ||
9e741045 | 73 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
65254108 | 74 | { |
9e741045 VD |
75 | struct dsa_switch_tree *dst; |
76 | ||
77 | dst = dsa_tree_find(index); | |
78 | if (dst) | |
79 | return dsa_tree_get(dst); | |
80 | else | |
81 | return dsa_tree_alloc(index); | |
65254108 VD |
82 | } |
83 | ||
84 | static void dsa_tree_release(struct kref *ref) | |
85 | { | |
86 | struct dsa_switch_tree *dst; | |
87 | ||
88 | dst = container_of(ref, struct dsa_switch_tree, refcount); | |
89 | ||
90 | dsa_tree_free(dst); | |
91 | } | |
92 | ||
93 | static void dsa_tree_put(struct dsa_switch_tree *dst) | |
94 | { | |
9e741045 VD |
95 | if (dst) |
96 | kref_put(&dst->refcount, dsa_tree_release); | |
65254108 VD |
97 | } |
98 | ||
293784a8 | 99 | static bool dsa_port_is_dsa(struct dsa_port *port) |
83c0afae | 100 | { |
6d4e5c57 | 101 | return port->type == DSA_PORT_TYPE_DSA; |
293784a8 FF |
102 | } |
103 | ||
104 | static bool dsa_port_is_cpu(struct dsa_port *port) | |
105 | { | |
6d4e5c57 | 106 | return port->type == DSA_PORT_TYPE_CPU; |
83c0afae AL |
107 | } |
108 | ||
f070464c VD |
109 | static bool dsa_port_is_user(struct dsa_port *dp) |
110 | { | |
111 | return dp->type == DSA_PORT_TYPE_USER; | |
112 | } | |
113 | ||
f163da88 VD |
114 | static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst, |
115 | struct device_node *dn) | |
83c0afae AL |
116 | { |
117 | struct dsa_switch *ds; | |
f163da88 VD |
118 | struct dsa_port *dp; |
119 | int device, port; | |
83c0afae | 120 | |
f163da88 VD |
121 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { |
122 | ds = dst->ds[device]; | |
83c0afae AL |
123 | if (!ds) |
124 | continue; | |
125 | ||
f163da88 VD |
126 | for (port = 0; port < ds->num_ports; port++) { |
127 | dp = &ds->ports[port]; | |
128 | ||
129 | if (dp->dn == dn) | |
130 | return dp; | |
131 | } | |
83c0afae AL |
132 | } |
133 | ||
134 | return NULL; | |
135 | } | |
136 | ||
34c09a89 | 137 | static bool dsa_port_setup_routing_table(struct dsa_port *dp) |
83c0afae | 138 | { |
34c09a89 VD |
139 | struct dsa_switch *ds = dp->ds; |
140 | struct dsa_switch_tree *dst = ds->dst; | |
141 | struct device_node *dn = dp->dn; | |
c5286665 | 142 | struct of_phandle_iterator it; |
f163da88 | 143 | struct dsa_port *link_dp; |
c5286665 | 144 | int err; |
83c0afae | 145 | |
c5286665 VD |
146 | of_for_each_phandle(&it, err, dn, "link", NULL, 0) { |
147 | link_dp = dsa_tree_find_port_by_node(dst, it.node); | |
148 | if (!link_dp) { | |
149 | of_node_put(it.node); | |
34c09a89 | 150 | return false; |
c5286665 | 151 | } |
83c0afae | 152 | |
34c09a89 | 153 | ds->rtable[link_dp->ds->index] = dp->index; |
83c0afae AL |
154 | } |
155 | ||
34c09a89 | 156 | return true; |
83c0afae AL |
157 | } |
158 | ||
34c09a89 | 159 | static bool dsa_switch_setup_routing_table(struct dsa_switch *ds) |
83c0afae | 160 | { |
34c09a89 VD |
161 | bool complete = true; |
162 | struct dsa_port *dp; | |
163 | int i; | |
83c0afae | 164 | |
34c09a89 VD |
165 | for (i = 0; i < DSA_MAX_SWITCHES; i++) |
166 | ds->rtable[i] = DSA_RTABLE_NONE; | |
83c0afae | 167 | |
34c09a89 VD |
168 | for (i = 0; i < ds->num_ports; i++) { |
169 | dp = &ds->ports[i]; | |
83c0afae | 170 | |
34c09a89 VD |
171 | if (dsa_port_is_dsa(dp)) { |
172 | complete = dsa_port_setup_routing_table(dp); | |
173 | if (!complete) | |
174 | break; | |
175 | } | |
83c0afae AL |
176 | } |
177 | ||
34c09a89 | 178 | return complete; |
83c0afae AL |
179 | } |
180 | ||
34c09a89 | 181 | static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst) |
83c0afae AL |
182 | { |
183 | struct dsa_switch *ds; | |
34c09a89 VD |
184 | bool complete = true; |
185 | int device; | |
83c0afae | 186 | |
34c09a89 VD |
187 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { |
188 | ds = dst->ds[device]; | |
83c0afae AL |
189 | if (!ds) |
190 | continue; | |
191 | ||
34c09a89 VD |
192 | complete = dsa_switch_setup_routing_table(ds); |
193 | if (!complete) | |
194 | break; | |
83c0afae AL |
195 | } |
196 | ||
34c09a89 | 197 | return complete; |
83c0afae AL |
198 | } |
199 | ||
f070464c VD |
200 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
201 | { | |
202 | struct dsa_switch *ds; | |
203 | struct dsa_port *dp; | |
204 | int device, port; | |
205 | ||
206 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
207 | ds = dst->ds[device]; | |
208 | if (!ds) | |
209 | continue; | |
210 | ||
211 | for (port = 0; port < ds->num_ports; port++) { | |
212 | dp = &ds->ports[port]; | |
213 | ||
214 | if (dsa_port_is_cpu(dp)) | |
215 | return dp; | |
216 | } | |
217 | } | |
218 | ||
219 | return NULL; | |
220 | } | |
221 | ||
222 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) | |
223 | { | |
224 | struct dsa_switch *ds; | |
225 | struct dsa_port *dp; | |
226 | int device, port; | |
227 | ||
228 | /* DSA currently only supports a single CPU port */ | |
229 | dst->cpu_dp = dsa_tree_find_first_cpu(dst); | |
230 | if (!dst->cpu_dp) { | |
231 | pr_warn("Tree has no master device\n"); | |
232 | return -EINVAL; | |
233 | } | |
234 | ||
235 | /* Assign the default CPU port to all ports of the fabric */ | |
236 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
237 | ds = dst->ds[device]; | |
238 | if (!ds) | |
239 | continue; | |
240 | ||
241 | for (port = 0; port < ds->num_ports; port++) { | |
242 | dp = &ds->ports[port]; | |
243 | ||
244 | if (dsa_port_is_user(dp)) | |
245 | dp->cpu_dp = dst->cpu_dp; | |
246 | } | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) | |
253 | { | |
254 | /* DSA currently only supports a single CPU port */ | |
255 | dst->cpu_dp = NULL; | |
256 | } | |
257 | ||
1d27732f | 258 | static int dsa_port_setup(struct dsa_port *dp) |
83c0afae | 259 | { |
1d27732f | 260 | struct dsa_switch *ds = dp->ds; |
36d88f50 | 261 | int err = 0; |
83c0afae | 262 | |
1d27732f | 263 | memset(&dp->devlink_port, 0, sizeof(dp->devlink_port)); |
83c0afae | 264 | |
36d88f50 FF |
265 | if (dp->type != DSA_PORT_TYPE_UNUSED) |
266 | err = devlink_port_register(ds->devlink, &dp->devlink_port, | |
267 | dp->index); | |
1d27732f | 268 | if (err) |
83c0afae | 269 | return err; |
83c0afae | 270 | |
1d27732f VD |
271 | switch (dp->type) { |
272 | case DSA_PORT_TYPE_UNUSED: | |
273 | break; | |
274 | case DSA_PORT_TYPE_CPU: | |
275 | case DSA_PORT_TYPE_DSA: | |
276 | err = dsa_port_fixed_link_register_of(dp); | |
277 | if (err) { | |
278 | dev_err(ds->dev, "failed to register fixed link for port %d.%d\n", | |
279 | ds->index, dp->index); | |
280 | return err; | |
281 | } | |
83c0afae | 282 | |
1d27732f VD |
283 | break; |
284 | case DSA_PORT_TYPE_USER: | |
285 | err = dsa_slave_create(dp); | |
286 | if (err) | |
287 | dev_err(ds->dev, "failed to create slave for port %d.%d\n", | |
288 | ds->index, dp->index); | |
289 | else | |
290 | devlink_port_type_eth_set(&dp->devlink_port, dp->slave); | |
291 | break; | |
83c0afae AL |
292 | } |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
1d27732f | 297 | static void dsa_port_teardown(struct dsa_port *dp) |
83c0afae | 298 | { |
36d88f50 FF |
299 | if (dp->type != DSA_PORT_TYPE_UNUSED) |
300 | devlink_port_unregister(&dp->devlink_port); | |
1d27732f VD |
301 | |
302 | switch (dp->type) { | |
303 | case DSA_PORT_TYPE_UNUSED: | |
304 | break; | |
305 | case DSA_PORT_TYPE_CPU: | |
306 | case DSA_PORT_TYPE_DSA: | |
307 | dsa_port_fixed_link_unregister_of(dp); | |
308 | break; | |
309 | case DSA_PORT_TYPE_USER: | |
310 | if (dp->slave) { | |
311 | dsa_slave_destroy(dp->slave); | |
312 | dp->slave = NULL; | |
313 | } | |
314 | break; | |
83c0afae AL |
315 | } |
316 | } | |
317 | ||
1f08f9e9 | 318 | static int dsa_switch_setup(struct dsa_switch *ds) |
83c0afae | 319 | { |
83c0afae AL |
320 | int err; |
321 | ||
6e830d8f | 322 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
9d490b4e | 323 | * driver and before ops->setup() has run, since the switch drivers and |
6e830d8f FF |
324 | * the slave MDIO bus driver rely on these values for probing PHY |
325 | * devices or not | |
326 | */ | |
02bc6e54 | 327 | ds->phys_mii_mask |= dsa_user_ports(ds); |
6e830d8f | 328 | |
96567d5d AL |
329 | /* Add the switch to devlink before calling setup, so that setup can |
330 | * add dpipe tables | |
331 | */ | |
332 | ds->devlink = devlink_alloc(&dsa_devlink_ops, 0); | |
333 | if (!ds->devlink) | |
334 | return -ENOMEM; | |
335 | ||
336 | err = devlink_register(ds->devlink, ds->dev); | |
337 | if (err) | |
338 | return err; | |
339 | ||
9d490b4e | 340 | err = ds->ops->setup(ds); |
83c0afae AL |
341 | if (err < 0) |
342 | return err; | |
343 | ||
f515f192 VD |
344 | err = dsa_switch_register_notifier(ds); |
345 | if (err) | |
346 | return err; | |
347 | ||
9d490b4e | 348 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
1eb59443 FF |
349 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
350 | if (!ds->slave_mii_bus) | |
351 | return -ENOMEM; | |
352 | ||
353 | dsa_slave_mii_bus_init(ds); | |
354 | ||
355 | err = mdiobus_register(ds->slave_mii_bus); | |
356 | if (err < 0) | |
357 | return err; | |
358 | } | |
359 | ||
83c0afae AL |
360 | return 0; |
361 | } | |
362 | ||
1f08f9e9 | 363 | static void dsa_switch_teardown(struct dsa_switch *ds) |
83c0afae | 364 | { |
9d490b4e | 365 | if (ds->slave_mii_bus && ds->ops->phy_read) |
1eb59443 | 366 | mdiobus_unregister(ds->slave_mii_bus); |
f515f192 VD |
367 | |
368 | dsa_switch_unregister_notifier(ds); | |
96567d5d AL |
369 | |
370 | if (ds->devlink) { | |
371 | devlink_unregister(ds->devlink); | |
372 | devlink_free(ds->devlink); | |
373 | ds->devlink = NULL; | |
374 | } | |
375 | ||
83c0afae AL |
376 | } |
377 | ||
1f08f9e9 VD |
378 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
379 | { | |
380 | struct dsa_switch *ds; | |
1d27732f VD |
381 | struct dsa_port *dp; |
382 | int device, port; | |
1f08f9e9 VD |
383 | int err; |
384 | ||
385 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
386 | ds = dst->ds[device]; | |
387 | if (!ds) | |
388 | continue; | |
389 | ||
390 | err = dsa_switch_setup(ds); | |
391 | if (err) | |
392 | return err; | |
1d27732f VD |
393 | |
394 | for (port = 0; port < ds->num_ports; port++) { | |
395 | dp = &ds->ports[port]; | |
396 | ||
397 | err = dsa_port_setup(dp); | |
398 | if (err) | |
399 | return err; | |
400 | } | |
1f08f9e9 VD |
401 | } |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) | |
407 | { | |
408 | struct dsa_switch *ds; | |
1d27732f VD |
409 | struct dsa_port *dp; |
410 | int device, port; | |
1f08f9e9 VD |
411 | |
412 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
413 | ds = dst->ds[device]; | |
414 | if (!ds) | |
415 | continue; | |
416 | ||
1d27732f VD |
417 | for (port = 0; port < ds->num_ports; port++) { |
418 | dp = &ds->ports[port]; | |
419 | ||
420 | dsa_port_teardown(dp); | |
421 | } | |
422 | ||
1f08f9e9 VD |
423 | dsa_switch_teardown(ds); |
424 | } | |
425 | } | |
426 | ||
17a22fcf VD |
427 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
428 | { | |
429 | struct dsa_port *cpu_dp = dst->cpu_dp; | |
430 | struct net_device *master = cpu_dp->master; | |
431 | ||
432 | /* DSA currently supports a single pair of CPU port and master device */ | |
433 | return dsa_master_setup(master, cpu_dp); | |
434 | } | |
435 | ||
436 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) | |
437 | { | |
438 | struct dsa_port *cpu_dp = dst->cpu_dp; | |
439 | struct net_device *master = cpu_dp->master; | |
440 | ||
441 | return dsa_master_teardown(master); | |
442 | } | |
443 | ||
ec15dd42 | 444 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
83c0afae | 445 | { |
34c09a89 | 446 | bool complete; |
83c0afae AL |
447 | int err; |
448 | ||
ec15dd42 VD |
449 | if (dst->setup) { |
450 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", | |
451 | dst->index); | |
452 | return -EEXIST; | |
453 | } | |
454 | ||
34c09a89 VD |
455 | complete = dsa_tree_setup_routing_table(dst); |
456 | if (!complete) | |
457 | return 0; | |
458 | ||
f070464c VD |
459 | err = dsa_tree_setup_default_cpu(dst); |
460 | if (err) | |
461 | return err; | |
462 | ||
1f08f9e9 VD |
463 | err = dsa_tree_setup_switches(dst); |
464 | if (err) | |
465 | return err; | |
83c0afae | 466 | |
17a22fcf | 467 | err = dsa_tree_setup_master(dst); |
1943563d VD |
468 | if (err) |
469 | return err; | |
470 | ||
ec15dd42 VD |
471 | dst->setup = true; |
472 | ||
473 | pr_info("DSA: tree %d setup\n", dst->index); | |
83c0afae AL |
474 | |
475 | return 0; | |
476 | } | |
477 | ||
ec15dd42 | 478 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
83c0afae | 479 | { |
ec15dd42 | 480 | if (!dst->setup) |
83c0afae AL |
481 | return; |
482 | ||
17a22fcf | 483 | dsa_tree_teardown_master(dst); |
83c0afae | 484 | |
1f08f9e9 | 485 | dsa_tree_teardown_switches(dst); |
83c0afae | 486 | |
f070464c | 487 | dsa_tree_teardown_default_cpu(dst); |
0c73c523 | 488 | |
ec15dd42 VD |
489 | pr_info("DSA: tree %d torn down\n", dst->index); |
490 | ||
491 | dst->setup = false; | |
83c0afae AL |
492 | } |
493 | ||
6da2a940 VD |
494 | static void dsa_tree_remove_switch(struct dsa_switch_tree *dst, |
495 | unsigned int index) | |
496 | { | |
30817354 VD |
497 | dsa_tree_teardown(dst); |
498 | ||
6da2a940 VD |
499 | dst->ds[index] = NULL; |
500 | dsa_tree_put(dst); | |
501 | } | |
502 | ||
503 | static int dsa_tree_add_switch(struct dsa_switch_tree *dst, | |
504 | struct dsa_switch *ds) | |
505 | { | |
506 | unsigned int index = ds->index; | |
30817354 | 507 | int err; |
6da2a940 VD |
508 | |
509 | if (dst->ds[index]) | |
510 | return -EBUSY; | |
511 | ||
512 | dsa_tree_get(dst); | |
513 | dst->ds[index] = ds; | |
514 | ||
30817354 VD |
515 | err = dsa_tree_setup(dst); |
516 | if (err) | |
517 | dsa_tree_remove_switch(dst, index); | |
518 | ||
519 | return err; | |
6da2a940 VD |
520 | } |
521 | ||
06e24d08 VD |
522 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
523 | { | |
524 | if (!name) | |
525 | name = "eth%d"; | |
526 | ||
527 | dp->type = DSA_PORT_TYPE_USER; | |
528 | dp->name = name; | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
533 | static int dsa_port_parse_dsa(struct dsa_port *dp) | |
534 | { | |
535 | dp->type = DSA_PORT_TYPE_DSA; | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) | |
541 | { | |
7354fcb0 VD |
542 | struct dsa_switch *ds = dp->ds; |
543 | struct dsa_switch_tree *dst = ds->dst; | |
544 | const struct dsa_device_ops *tag_ops; | |
545 | enum dsa_tag_protocol tag_protocol; | |
546 | ||
5ed4e3eb | 547 | tag_protocol = ds->ops->get_tag_protocol(ds, dp->index); |
7354fcb0 VD |
548 | tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
549 | if (IS_ERR(tag_ops)) { | |
550 | dev_warn(ds->dev, "No tagger for this switch\n"); | |
551 | return PTR_ERR(tag_ops); | |
552 | } | |
553 | ||
06e24d08 | 554 | dp->type = DSA_PORT_TYPE_CPU; |
7354fcb0 VD |
555 | dp->rcv = tag_ops->rcv; |
556 | dp->tag_ops = tag_ops; | |
06e24d08 | 557 | dp->master = master; |
7354fcb0 | 558 | dp->dst = dst; |
06e24d08 VD |
559 | |
560 | return 0; | |
561 | } | |
562 | ||
fd223e2e VD |
563 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
564 | { | |
6d4e5c57 | 565 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
1838fa89 | 566 | const char *name = of_get_property(dn, "label", NULL); |
54df6fa9 | 567 | bool link = of_property_read_bool(dn, "link"); |
6d4e5c57 | 568 | |
06e24d08 VD |
569 | dp->dn = dn; |
570 | ||
6d4e5c57 | 571 | if (ethernet) { |
cbabb0ac VD |
572 | struct net_device *master; |
573 | ||
574 | master = of_find_net_device_by_node(ethernet); | |
575 | if (!master) | |
576 | return -EPROBE_DEFER; | |
577 | ||
06e24d08 | 578 | return dsa_port_parse_cpu(dp, master); |
6d4e5c57 VD |
579 | } |
580 | ||
06e24d08 VD |
581 | if (link) |
582 | return dsa_port_parse_dsa(dp); | |
fd223e2e | 583 | |
06e24d08 | 584 | return dsa_port_parse_user(dp, name); |
fd223e2e VD |
585 | } |
586 | ||
975e6e32 VD |
587 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
588 | struct device_node *dn) | |
83c0afae | 589 | { |
5b32fe07 | 590 | struct device_node *ports, *port; |
fd223e2e | 591 | struct dsa_port *dp; |
83c0afae | 592 | u32 reg; |
5b32fe07 VD |
593 | int err; |
594 | ||
595 | ports = of_get_child_by_name(dn, "ports"); | |
596 | if (!ports) { | |
597 | dev_err(ds->dev, "no ports child node found\n"); | |
598 | return -EINVAL; | |
599 | } | |
83c0afae AL |
600 | |
601 | for_each_available_child_of_node(ports, port) { | |
602 | err = of_property_read_u32(port, "reg", ®); | |
603 | if (err) | |
604 | return err; | |
605 | ||
26895e29 | 606 | if (reg >= ds->num_ports) |
83c0afae AL |
607 | return -EINVAL; |
608 | ||
fd223e2e VD |
609 | dp = &ds->ports[reg]; |
610 | ||
611 | err = dsa_port_parse_of(dp, port); | |
612 | if (err) | |
613 | return err; | |
83c0afae AL |
614 | } |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
975e6e32 VD |
619 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
620 | struct device_node *dn) | |
621 | { | |
622 | u32 m[2] = { 0, 0 }; | |
623 | int sz; | |
624 | ||
625 | /* Don't error out if this optional property isn't found */ | |
626 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); | |
627 | if (sz < 0 && sz != -EINVAL) | |
628 | return sz; | |
629 | ||
630 | ds->index = m[1]; | |
631 | if (ds->index >= DSA_MAX_SWITCHES) | |
632 | return -EINVAL; | |
633 | ||
634 | ds->dst = dsa_tree_touch(m[0]); | |
635 | if (!ds->dst) | |
636 | return -ENOMEM; | |
637 | ||
638 | return 0; | |
639 | } | |
640 | ||
641 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) | |
642 | { | |
643 | int err; | |
644 | ||
645 | err = dsa_switch_parse_member_of(ds, dn); | |
646 | if (err) | |
647 | return err; | |
648 | ||
649 | return dsa_switch_parse_ports_of(ds, dn); | |
650 | } | |
651 | ||
fd223e2e VD |
652 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
653 | struct device *dev) | |
654 | { | |
6d4e5c57 | 655 | if (!strcmp(name, "cpu")) { |
cbabb0ac VD |
656 | struct net_device *master; |
657 | ||
658 | master = dsa_dev_to_net_device(dev); | |
659 | if (!master) | |
660 | return -EPROBE_DEFER; | |
661 | ||
662 | dev_put(master); | |
663 | ||
06e24d08 | 664 | return dsa_port_parse_cpu(dp, master); |
6d4e5c57 VD |
665 | } |
666 | ||
06e24d08 VD |
667 | if (!strcmp(name, "dsa")) |
668 | return dsa_port_parse_dsa(dp); | |
fd223e2e | 669 | |
06e24d08 | 670 | return dsa_port_parse_user(dp, name); |
fd223e2e VD |
671 | } |
672 | ||
975e6e32 VD |
673 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
674 | struct dsa_chip_data *cd) | |
71e0bbde FF |
675 | { |
676 | bool valid_name_found = false; | |
fd223e2e VD |
677 | struct dsa_port *dp; |
678 | struct device *dev; | |
679 | const char *name; | |
71e0bbde | 680 | unsigned int i; |
fd223e2e | 681 | int err; |
71e0bbde FF |
682 | |
683 | for (i = 0; i < DSA_MAX_PORTS; i++) { | |
fd223e2e VD |
684 | name = cd->port_names[i]; |
685 | dev = cd->netdev[i]; | |
686 | dp = &ds->ports[i]; | |
687 | ||
688 | if (!name) | |
71e0bbde FF |
689 | continue; |
690 | ||
fd223e2e VD |
691 | err = dsa_port_parse(dp, name, dev); |
692 | if (err) | |
693 | return err; | |
694 | ||
71e0bbde FF |
695 | valid_name_found = true; |
696 | } | |
697 | ||
698 | if (!valid_name_found && i == DSA_MAX_PORTS) | |
699 | return -EINVAL; | |
700 | ||
701 | return 0; | |
702 | } | |
703 | ||
975e6e32 | 704 | static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd) |
71e0bbde | 705 | { |
975e6e32 | 706 | ds->cd = cd; |
71e0bbde | 707 | |
975e6e32 VD |
708 | /* We don't support interconnected switches nor multiple trees via |
709 | * platform data, so this is the unique switch of the tree. | |
710 | */ | |
711 | ds->index = 0; | |
712 | ds->dst = dsa_tree_touch(0); | |
713 | if (!ds->dst) | |
714 | return -ENOMEM; | |
71e0bbde | 715 | |
975e6e32 | 716 | return dsa_switch_parse_ports(ds, cd); |
71e0bbde FF |
717 | } |
718 | ||
30817354 VD |
719 | static int dsa_switch_add(struct dsa_switch *ds) |
720 | { | |
721 | struct dsa_switch_tree *dst = ds->dst; | |
722 | ||
723 | return dsa_tree_add_switch(dst, ds); | |
724 | } | |
725 | ||
b4fbb347 | 726 | static int dsa_switch_probe(struct dsa_switch *ds) |
83c0afae | 727 | { |
23c9ee49 VD |
728 | struct dsa_chip_data *pdata = ds->dev->platform_data; |
729 | struct device_node *np = ds->dev->of_node; | |
34c09a89 | 730 | int err; |
83c0afae | 731 | |
975e6e32 VD |
732 | if (np) |
733 | err = dsa_switch_parse_of(ds, np); | |
734 | else if (pdata) | |
735 | err = dsa_switch_parse(ds, pdata); | |
736 | else | |
737 | err = -ENODEV; | |
83c0afae | 738 | |
975e6e32 VD |
739 | if (err) |
740 | return err; | |
d390238c | 741 | |
30817354 | 742 | return dsa_switch_add(ds); |
83c0afae AL |
743 | } |
744 | ||
a0c02161 VD |
745 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
746 | { | |
747 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); | |
748 | struct dsa_switch *ds; | |
818be848 | 749 | int i; |
a0c02161 VD |
750 | |
751 | ds = devm_kzalloc(dev, size, GFP_KERNEL); | |
752 | if (!ds) | |
753 | return NULL; | |
754 | ||
755 | ds->dev = dev; | |
756 | ds->num_ports = n; | |
757 | ||
818be848 VD |
758 | for (i = 0; i < ds->num_ports; ++i) { |
759 | ds->ports[i].index = i; | |
760 | ds->ports[i].ds = ds; | |
761 | } | |
762 | ||
a0c02161 VD |
763 | return ds; |
764 | } | |
765 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); | |
766 | ||
23c9ee49 | 767 | int dsa_register_switch(struct dsa_switch *ds) |
83c0afae AL |
768 | { |
769 | int err; | |
770 | ||
771 | mutex_lock(&dsa2_mutex); | |
b4fbb347 | 772 | err = dsa_switch_probe(ds); |
9e741045 | 773 | dsa_tree_put(ds->dst); |
83c0afae AL |
774 | mutex_unlock(&dsa2_mutex); |
775 | ||
776 | return err; | |
777 | } | |
778 | EXPORT_SYMBOL_GPL(dsa_register_switch); | |
779 | ||
b4fbb347 | 780 | static void dsa_switch_remove(struct dsa_switch *ds) |
83c0afae AL |
781 | { |
782 | struct dsa_switch_tree *dst = ds->dst; | |
6da2a940 | 783 | unsigned int index = ds->index; |
83c0afae | 784 | |
6da2a940 | 785 | dsa_tree_remove_switch(dst, index); |
83c0afae AL |
786 | } |
787 | ||
788 | void dsa_unregister_switch(struct dsa_switch *ds) | |
789 | { | |
790 | mutex_lock(&dsa2_mutex); | |
b4fbb347 | 791 | dsa_switch_remove(ds); |
83c0afae AL |
792 | mutex_unlock(&dsa2_mutex); |
793 | } | |
794 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |