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