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 | ||
986d7ccf | 244 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
f070464c VD |
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; |
83c0afae AL |
261 | int err; |
262 | ||
1d27732f | 263 | memset(&dp->devlink_port, 0, sizeof(dp->devlink_port)); |
83c0afae | 264 | |
1d27732f VD |
265 | err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index); |
266 | if (err) | |
83c0afae | 267 | return err; |
83c0afae | 268 | |
1d27732f VD |
269 | switch (dp->type) { |
270 | case DSA_PORT_TYPE_UNUSED: | |
271 | break; | |
272 | case DSA_PORT_TYPE_CPU: | |
273 | case DSA_PORT_TYPE_DSA: | |
33615367 | 274 | err = dsa_port_link_register_of(dp); |
1d27732f | 275 | if (err) { |
33615367 | 276 | dev_err(ds->dev, "failed to setup link for port %d.%d\n", |
1d27732f VD |
277 | ds->index, dp->index); |
278 | return err; | |
279 | } | |
1d27732f VD |
280 | break; |
281 | case DSA_PORT_TYPE_USER: | |
282 | err = dsa_slave_create(dp); | |
283 | if (err) | |
284 | dev_err(ds->dev, "failed to create slave for port %d.%d\n", | |
285 | ds->index, dp->index); | |
286 | else | |
287 | devlink_port_type_eth_set(&dp->devlink_port, dp->slave); | |
288 | break; | |
83c0afae AL |
289 | } |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
1d27732f | 294 | static void dsa_port_teardown(struct dsa_port *dp) |
83c0afae | 295 | { |
1d27732f VD |
296 | devlink_port_unregister(&dp->devlink_port); |
297 | ||
298 | switch (dp->type) { | |
299 | case DSA_PORT_TYPE_UNUSED: | |
300 | break; | |
301 | case DSA_PORT_TYPE_CPU: | |
302 | case DSA_PORT_TYPE_DSA: | |
33615367 | 303 | dsa_port_link_unregister_of(dp); |
1d27732f VD |
304 | break; |
305 | case DSA_PORT_TYPE_USER: | |
306 | if (dp->slave) { | |
307 | dsa_slave_destroy(dp->slave); | |
308 | dp->slave = NULL; | |
309 | } | |
310 | break; | |
83c0afae AL |
311 | } |
312 | } | |
313 | ||
1f08f9e9 | 314 | static int dsa_switch_setup(struct dsa_switch *ds) |
83c0afae | 315 | { |
83c0afae AL |
316 | int err; |
317 | ||
6e830d8f | 318 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
9d490b4e | 319 | * driver and before ops->setup() has run, since the switch drivers and |
6e830d8f FF |
320 | * the slave MDIO bus driver rely on these values for probing PHY |
321 | * devices or not | |
322 | */ | |
02bc6e54 | 323 | ds->phys_mii_mask |= dsa_user_ports(ds); |
6e830d8f | 324 | |
96567d5d AL |
325 | /* Add the switch to devlink before calling setup, so that setup can |
326 | * add dpipe tables | |
327 | */ | |
328 | ds->devlink = devlink_alloc(&dsa_devlink_ops, 0); | |
329 | if (!ds->devlink) | |
330 | return -ENOMEM; | |
331 | ||
332 | err = devlink_register(ds->devlink, ds->dev); | |
333 | if (err) | |
334 | return err; | |
335 | ||
9d490b4e | 336 | err = ds->ops->setup(ds); |
83c0afae AL |
337 | if (err < 0) |
338 | return err; | |
339 | ||
f515f192 VD |
340 | err = dsa_switch_register_notifier(ds); |
341 | if (err) | |
342 | return err; | |
343 | ||
9d490b4e | 344 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
1eb59443 FF |
345 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
346 | if (!ds->slave_mii_bus) | |
347 | return -ENOMEM; | |
348 | ||
349 | dsa_slave_mii_bus_init(ds); | |
350 | ||
351 | err = mdiobus_register(ds->slave_mii_bus); | |
352 | if (err < 0) | |
353 | return err; | |
354 | } | |
355 | ||
83c0afae AL |
356 | return 0; |
357 | } | |
358 | ||
1f08f9e9 | 359 | static void dsa_switch_teardown(struct dsa_switch *ds) |
83c0afae | 360 | { |
9d490b4e | 361 | if (ds->slave_mii_bus && ds->ops->phy_read) |
1eb59443 | 362 | mdiobus_unregister(ds->slave_mii_bus); |
f515f192 VD |
363 | |
364 | dsa_switch_unregister_notifier(ds); | |
96567d5d AL |
365 | |
366 | if (ds->devlink) { | |
367 | devlink_unregister(ds->devlink); | |
368 | devlink_free(ds->devlink); | |
369 | ds->devlink = NULL; | |
370 | } | |
371 | ||
83c0afae AL |
372 | } |
373 | ||
1f08f9e9 VD |
374 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
375 | { | |
376 | struct dsa_switch *ds; | |
1d27732f VD |
377 | struct dsa_port *dp; |
378 | int device, port; | |
1f08f9e9 VD |
379 | int err; |
380 | ||
381 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
382 | ds = dst->ds[device]; | |
383 | if (!ds) | |
384 | continue; | |
385 | ||
386 | err = dsa_switch_setup(ds); | |
387 | if (err) | |
388 | return err; | |
1d27732f VD |
389 | |
390 | for (port = 0; port < ds->num_ports; port++) { | |
391 | dp = &ds->ports[port]; | |
392 | ||
393 | err = dsa_port_setup(dp); | |
394 | if (err) | |
395 | return err; | |
396 | } | |
1f08f9e9 VD |
397 | } |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) | |
403 | { | |
404 | struct dsa_switch *ds; | |
1d27732f VD |
405 | struct dsa_port *dp; |
406 | int device, port; | |
1f08f9e9 VD |
407 | |
408 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { | |
409 | ds = dst->ds[device]; | |
410 | if (!ds) | |
411 | continue; | |
412 | ||
1d27732f VD |
413 | for (port = 0; port < ds->num_ports; port++) { |
414 | dp = &ds->ports[port]; | |
415 | ||
416 | dsa_port_teardown(dp); | |
417 | } | |
418 | ||
1f08f9e9 VD |
419 | dsa_switch_teardown(ds); |
420 | } | |
421 | } | |
422 | ||
17a22fcf VD |
423 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
424 | { | |
425 | struct dsa_port *cpu_dp = dst->cpu_dp; | |
426 | struct net_device *master = cpu_dp->master; | |
427 | ||
428 | /* DSA currently supports a single pair of CPU port and master device */ | |
429 | return dsa_master_setup(master, cpu_dp); | |
430 | } | |
431 | ||
432 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) | |
433 | { | |
434 | struct dsa_port *cpu_dp = dst->cpu_dp; | |
435 | struct net_device *master = cpu_dp->master; | |
436 | ||
437 | return dsa_master_teardown(master); | |
438 | } | |
439 | ||
ec15dd42 | 440 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
83c0afae | 441 | { |
34c09a89 | 442 | bool complete; |
83c0afae AL |
443 | int err; |
444 | ||
ec15dd42 VD |
445 | if (dst->setup) { |
446 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", | |
447 | dst->index); | |
448 | return -EEXIST; | |
449 | } | |
450 | ||
34c09a89 VD |
451 | complete = dsa_tree_setup_routing_table(dst); |
452 | if (!complete) | |
453 | return 0; | |
454 | ||
f070464c VD |
455 | err = dsa_tree_setup_default_cpu(dst); |
456 | if (err) | |
457 | return err; | |
458 | ||
1f08f9e9 VD |
459 | err = dsa_tree_setup_switches(dst); |
460 | if (err) | |
461 | return err; | |
83c0afae | 462 | |
17a22fcf | 463 | err = dsa_tree_setup_master(dst); |
1943563d VD |
464 | if (err) |
465 | return err; | |
466 | ||
ec15dd42 VD |
467 | dst->setup = true; |
468 | ||
469 | pr_info("DSA: tree %d setup\n", dst->index); | |
83c0afae AL |
470 | |
471 | return 0; | |
472 | } | |
473 | ||
ec15dd42 | 474 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
83c0afae | 475 | { |
ec15dd42 | 476 | if (!dst->setup) |
83c0afae AL |
477 | return; |
478 | ||
17a22fcf | 479 | dsa_tree_teardown_master(dst); |
83c0afae | 480 | |
1f08f9e9 | 481 | dsa_tree_teardown_switches(dst); |
83c0afae | 482 | |
f070464c | 483 | dsa_tree_teardown_default_cpu(dst); |
0c73c523 | 484 | |
ec15dd42 VD |
485 | pr_info("DSA: tree %d torn down\n", dst->index); |
486 | ||
487 | dst->setup = false; | |
83c0afae AL |
488 | } |
489 | ||
6da2a940 VD |
490 | static void dsa_tree_remove_switch(struct dsa_switch_tree *dst, |
491 | unsigned int index) | |
492 | { | |
30817354 VD |
493 | dsa_tree_teardown(dst); |
494 | ||
6da2a940 VD |
495 | dst->ds[index] = NULL; |
496 | dsa_tree_put(dst); | |
497 | } | |
498 | ||
499 | static int dsa_tree_add_switch(struct dsa_switch_tree *dst, | |
500 | struct dsa_switch *ds) | |
501 | { | |
502 | unsigned int index = ds->index; | |
30817354 | 503 | int err; |
6da2a940 VD |
504 | |
505 | if (dst->ds[index]) | |
506 | return -EBUSY; | |
507 | ||
508 | dsa_tree_get(dst); | |
509 | dst->ds[index] = ds; | |
510 | ||
30817354 VD |
511 | err = dsa_tree_setup(dst); |
512 | if (err) | |
513 | dsa_tree_remove_switch(dst, index); | |
514 | ||
515 | return err; | |
6da2a940 VD |
516 | } |
517 | ||
06e24d08 VD |
518 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
519 | { | |
520 | if (!name) | |
521 | name = "eth%d"; | |
522 | ||
523 | dp->type = DSA_PORT_TYPE_USER; | |
524 | dp->name = name; | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | static int dsa_port_parse_dsa(struct dsa_port *dp) | |
530 | { | |
531 | dp->type = DSA_PORT_TYPE_DSA; | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
536 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) | |
537 | { | |
7354fcb0 VD |
538 | struct dsa_switch *ds = dp->ds; |
539 | struct dsa_switch_tree *dst = ds->dst; | |
540 | const struct dsa_device_ops *tag_ops; | |
541 | enum dsa_tag_protocol tag_protocol; | |
542 | ||
5ed4e3eb | 543 | tag_protocol = ds->ops->get_tag_protocol(ds, dp->index); |
7354fcb0 VD |
544 | tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
545 | if (IS_ERR(tag_ops)) { | |
546 | dev_warn(ds->dev, "No tagger for this switch\n"); | |
547 | return PTR_ERR(tag_ops); | |
548 | } | |
549 | ||
06e24d08 | 550 | dp->type = DSA_PORT_TYPE_CPU; |
7354fcb0 VD |
551 | dp->rcv = tag_ops->rcv; |
552 | dp->tag_ops = tag_ops; | |
06e24d08 | 553 | dp->master = master; |
7354fcb0 | 554 | dp->dst = dst; |
06e24d08 VD |
555 | |
556 | return 0; | |
557 | } | |
558 | ||
fd223e2e VD |
559 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
560 | { | |
6d4e5c57 | 561 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
1838fa89 | 562 | const char *name = of_get_property(dn, "label", NULL); |
54df6fa9 | 563 | bool link = of_property_read_bool(dn, "link"); |
6d4e5c57 | 564 | |
06e24d08 VD |
565 | dp->dn = dn; |
566 | ||
6d4e5c57 | 567 | if (ethernet) { |
cbabb0ac VD |
568 | struct net_device *master; |
569 | ||
570 | master = of_find_net_device_by_node(ethernet); | |
571 | if (!master) | |
572 | return -EPROBE_DEFER; | |
573 | ||
06e24d08 | 574 | return dsa_port_parse_cpu(dp, master); |
6d4e5c57 VD |
575 | } |
576 | ||
06e24d08 VD |
577 | if (link) |
578 | return dsa_port_parse_dsa(dp); | |
fd223e2e | 579 | |
06e24d08 | 580 | return dsa_port_parse_user(dp, name); |
fd223e2e VD |
581 | } |
582 | ||
975e6e32 VD |
583 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
584 | struct device_node *dn) | |
83c0afae | 585 | { |
5b32fe07 | 586 | struct device_node *ports, *port; |
fd223e2e | 587 | struct dsa_port *dp; |
83c0afae | 588 | u32 reg; |
5b32fe07 VD |
589 | int err; |
590 | ||
591 | ports = of_get_child_by_name(dn, "ports"); | |
592 | if (!ports) { | |
593 | dev_err(ds->dev, "no ports child node found\n"); | |
594 | return -EINVAL; | |
595 | } | |
83c0afae AL |
596 | |
597 | for_each_available_child_of_node(ports, port) { | |
598 | err = of_property_read_u32(port, "reg", ®); | |
599 | if (err) | |
600 | return err; | |
601 | ||
26895e29 | 602 | if (reg >= ds->num_ports) |
83c0afae AL |
603 | return -EINVAL; |
604 | ||
fd223e2e VD |
605 | dp = &ds->ports[reg]; |
606 | ||
607 | err = dsa_port_parse_of(dp, port); | |
608 | if (err) | |
609 | return err; | |
83c0afae AL |
610 | } |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
975e6e32 VD |
615 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
616 | struct device_node *dn) | |
617 | { | |
618 | u32 m[2] = { 0, 0 }; | |
619 | int sz; | |
620 | ||
621 | /* Don't error out if this optional property isn't found */ | |
622 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); | |
623 | if (sz < 0 && sz != -EINVAL) | |
624 | return sz; | |
625 | ||
626 | ds->index = m[1]; | |
627 | if (ds->index >= DSA_MAX_SWITCHES) | |
628 | return -EINVAL; | |
629 | ||
630 | ds->dst = dsa_tree_touch(m[0]); | |
631 | if (!ds->dst) | |
632 | return -ENOMEM; | |
633 | ||
634 | return 0; | |
635 | } | |
636 | ||
637 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) | |
638 | { | |
639 | int err; | |
640 | ||
641 | err = dsa_switch_parse_member_of(ds, dn); | |
642 | if (err) | |
643 | return err; | |
644 | ||
645 | return dsa_switch_parse_ports_of(ds, dn); | |
646 | } | |
647 | ||
fd223e2e VD |
648 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
649 | struct device *dev) | |
650 | { | |
6d4e5c57 | 651 | if (!strcmp(name, "cpu")) { |
cbabb0ac VD |
652 | struct net_device *master; |
653 | ||
654 | master = dsa_dev_to_net_device(dev); | |
655 | if (!master) | |
656 | return -EPROBE_DEFER; | |
657 | ||
658 | dev_put(master); | |
659 | ||
06e24d08 | 660 | return dsa_port_parse_cpu(dp, master); |
6d4e5c57 VD |
661 | } |
662 | ||
06e24d08 VD |
663 | if (!strcmp(name, "dsa")) |
664 | return dsa_port_parse_dsa(dp); | |
fd223e2e | 665 | |
06e24d08 | 666 | return dsa_port_parse_user(dp, name); |
fd223e2e VD |
667 | } |
668 | ||
975e6e32 VD |
669 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
670 | struct dsa_chip_data *cd) | |
71e0bbde FF |
671 | { |
672 | bool valid_name_found = false; | |
fd223e2e VD |
673 | struct dsa_port *dp; |
674 | struct device *dev; | |
675 | const char *name; | |
71e0bbde | 676 | unsigned int i; |
fd223e2e | 677 | int err; |
71e0bbde FF |
678 | |
679 | for (i = 0; i < DSA_MAX_PORTS; i++) { | |
fd223e2e VD |
680 | name = cd->port_names[i]; |
681 | dev = cd->netdev[i]; | |
682 | dp = &ds->ports[i]; | |
683 | ||
684 | if (!name) | |
71e0bbde FF |
685 | continue; |
686 | ||
fd223e2e VD |
687 | err = dsa_port_parse(dp, name, dev); |
688 | if (err) | |
689 | return err; | |
690 | ||
71e0bbde FF |
691 | valid_name_found = true; |
692 | } | |
693 | ||
694 | if (!valid_name_found && i == DSA_MAX_PORTS) | |
695 | return -EINVAL; | |
696 | ||
697 | return 0; | |
698 | } | |
699 | ||
975e6e32 | 700 | static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd) |
71e0bbde | 701 | { |
975e6e32 | 702 | ds->cd = cd; |
71e0bbde | 703 | |
975e6e32 VD |
704 | /* We don't support interconnected switches nor multiple trees via |
705 | * platform data, so this is the unique switch of the tree. | |
706 | */ | |
707 | ds->index = 0; | |
708 | ds->dst = dsa_tree_touch(0); | |
709 | if (!ds->dst) | |
710 | return -ENOMEM; | |
71e0bbde | 711 | |
975e6e32 | 712 | return dsa_switch_parse_ports(ds, cd); |
71e0bbde FF |
713 | } |
714 | ||
30817354 VD |
715 | static int dsa_switch_add(struct dsa_switch *ds) |
716 | { | |
717 | struct dsa_switch_tree *dst = ds->dst; | |
718 | ||
719 | return dsa_tree_add_switch(dst, ds); | |
720 | } | |
721 | ||
b4fbb347 | 722 | static int dsa_switch_probe(struct dsa_switch *ds) |
83c0afae | 723 | { |
23c9ee49 VD |
724 | struct dsa_chip_data *pdata = ds->dev->platform_data; |
725 | struct device_node *np = ds->dev->of_node; | |
34c09a89 | 726 | int err; |
83c0afae | 727 | |
975e6e32 VD |
728 | if (np) |
729 | err = dsa_switch_parse_of(ds, np); | |
730 | else if (pdata) | |
731 | err = dsa_switch_parse(ds, pdata); | |
732 | else | |
733 | err = -ENODEV; | |
83c0afae | 734 | |
975e6e32 VD |
735 | if (err) |
736 | return err; | |
d390238c | 737 | |
30817354 | 738 | return dsa_switch_add(ds); |
83c0afae AL |
739 | } |
740 | ||
a0c02161 VD |
741 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
742 | { | |
743 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); | |
744 | struct dsa_switch *ds; | |
818be848 | 745 | int i; |
a0c02161 VD |
746 | |
747 | ds = devm_kzalloc(dev, size, GFP_KERNEL); | |
748 | if (!ds) | |
749 | return NULL; | |
750 | ||
751 | ds->dev = dev; | |
752 | ds->num_ports = n; | |
753 | ||
818be848 VD |
754 | for (i = 0; i < ds->num_ports; ++i) { |
755 | ds->ports[i].index = i; | |
756 | ds->ports[i].ds = ds; | |
757 | } | |
758 | ||
a0c02161 VD |
759 | return ds; |
760 | } | |
761 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); | |
762 | ||
23c9ee49 | 763 | int dsa_register_switch(struct dsa_switch *ds) |
83c0afae AL |
764 | { |
765 | int err; | |
766 | ||
767 | mutex_lock(&dsa2_mutex); | |
b4fbb347 | 768 | err = dsa_switch_probe(ds); |
9e741045 | 769 | dsa_tree_put(ds->dst); |
83c0afae AL |
770 | mutex_unlock(&dsa2_mutex); |
771 | ||
772 | return err; | |
773 | } | |
774 | EXPORT_SYMBOL_GPL(dsa_register_switch); | |
775 | ||
b4fbb347 | 776 | static void dsa_switch_remove(struct dsa_switch *ds) |
83c0afae AL |
777 | { |
778 | struct dsa_switch_tree *dst = ds->dst; | |
6da2a940 | 779 | unsigned int index = ds->index; |
83c0afae | 780 | |
6da2a940 | 781 | dsa_tree_remove_switch(dst, index); |
83c0afae AL |
782 | } |
783 | ||
784 | void dsa_unregister_switch(struct dsa_switch *ds) | |
785 | { | |
786 | mutex_lock(&dsa2_mutex); | |
b4fbb347 | 787 | dsa_switch_remove(ds); |
83c0afae AL |
788 | mutex_unlock(&dsa2_mutex); |
789 | } | |
790 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |