]>
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 | ||
24 | static LIST_HEAD(dsa_switch_trees); | |
25 | static DEFINE_MUTEX(dsa2_mutex); | |
26 | ||
96567d5d AL |
27 | static const struct devlink_ops dsa_devlink_ops = { |
28 | }; | |
29 | ||
83c0afae AL |
30 | static struct dsa_switch_tree *dsa_get_dst(u32 tree) |
31 | { | |
32 | struct dsa_switch_tree *dst; | |
33 | ||
34 | list_for_each_entry(dst, &dsa_switch_trees, list) | |
7a99cd6e NY |
35 | if (dst->tree == tree) { |
36 | kref_get(&dst->refcount); | |
83c0afae | 37 | return dst; |
7a99cd6e | 38 | } |
83c0afae AL |
39 | return NULL; |
40 | } | |
41 | ||
42 | static void dsa_free_dst(struct kref *ref) | |
43 | { | |
44 | struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree, | |
45 | refcount); | |
46 | ||
47 | list_del(&dst->list); | |
48 | kfree(dst); | |
49 | } | |
50 | ||
51 | static void dsa_put_dst(struct dsa_switch_tree *dst) | |
52 | { | |
53 | kref_put(&dst->refcount, dsa_free_dst); | |
54 | } | |
55 | ||
56 | static struct dsa_switch_tree *dsa_add_dst(u32 tree) | |
57 | { | |
58 | struct dsa_switch_tree *dst; | |
59 | ||
60 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); | |
61 | if (!dst) | |
62 | return NULL; | |
63 | dst->tree = tree; | |
83c0afae AL |
64 | INIT_LIST_HEAD(&dst->list); |
65 | list_add_tail(&dsa_switch_trees, &dst->list); | |
66 | kref_init(&dst->refcount); | |
67 | ||
68 | return dst; | |
69 | } | |
70 | ||
71 | static void dsa_dst_add_ds(struct dsa_switch_tree *dst, | |
72 | struct dsa_switch *ds, u32 index) | |
73 | { | |
74 | kref_get(&dst->refcount); | |
75 | dst->ds[index] = ds; | |
76 | } | |
77 | ||
78 | static void dsa_dst_del_ds(struct dsa_switch_tree *dst, | |
79 | struct dsa_switch *ds, u32 index) | |
80 | { | |
81 | dst->ds[index] = NULL; | |
82 | kref_put(&dst->refcount, dsa_free_dst); | |
83 | } | |
84 | ||
71e0bbde FF |
85 | /* For platform data configurations, we need to have a valid name argument to |
86 | * differentiate a disabled port from an enabled one | |
87 | */ | |
293784a8 | 88 | static bool dsa_port_is_valid(struct dsa_port *port) |
83c0afae | 89 | { |
71e0bbde | 90 | return !!(port->dn || port->name); |
83c0afae AL |
91 | } |
92 | ||
293784a8 | 93 | static bool dsa_port_is_dsa(struct dsa_port *port) |
83c0afae | 94 | { |
71e0bbde FF |
95 | if (port->name && !strcmp(port->name, "dsa")) |
96 | return true; | |
97 | else | |
98 | return !!of_parse_phandle(port->dn, "link", 0); | |
293784a8 FF |
99 | } |
100 | ||
101 | static bool dsa_port_is_cpu(struct dsa_port *port) | |
102 | { | |
71e0bbde FF |
103 | if (port->name && !strcmp(port->name, "cpu")) |
104 | return true; | |
105 | else | |
106 | return !!of_parse_phandle(port->dn, "ethernet", 0); | |
83c0afae AL |
107 | } |
108 | ||
3512a8e9 FF |
109 | static bool dsa_ds_find_port_dn(struct dsa_switch *ds, |
110 | struct device_node *port) | |
83c0afae AL |
111 | { |
112 | u32 index; | |
113 | ||
26895e29 | 114 | for (index = 0; index < ds->num_ports; index++) |
83c0afae AL |
115 | if (ds->ports[index].dn == port) |
116 | return true; | |
117 | return false; | |
118 | } | |
119 | ||
3512a8e9 FF |
120 | static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst, |
121 | struct device_node *port) | |
83c0afae AL |
122 | { |
123 | struct dsa_switch *ds; | |
124 | u32 index; | |
125 | ||
126 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
127 | ds = dst->ds[index]; | |
128 | if (!ds) | |
129 | continue; | |
130 | ||
3512a8e9 | 131 | if (dsa_ds_find_port_dn(ds, port)) |
83c0afae AL |
132 | return ds; |
133 | } | |
134 | ||
135 | return NULL; | |
136 | } | |
137 | ||
138 | static int dsa_port_complete(struct dsa_switch_tree *dst, | |
139 | struct dsa_switch *src_ds, | |
293784a8 | 140 | struct dsa_port *port, |
83c0afae AL |
141 | u32 src_port) |
142 | { | |
143 | struct device_node *link; | |
144 | int index; | |
145 | struct dsa_switch *dst_ds; | |
146 | ||
147 | for (index = 0;; index++) { | |
293784a8 | 148 | link = of_parse_phandle(port->dn, "link", index); |
83c0afae AL |
149 | if (!link) |
150 | break; | |
151 | ||
3512a8e9 | 152 | dst_ds = dsa_dst_find_port_dn(dst, link); |
83c0afae AL |
153 | of_node_put(link); |
154 | ||
155 | if (!dst_ds) | |
156 | return 1; | |
157 | ||
158 | src_ds->rtable[dst_ds->index] = src_port; | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | /* A switch is complete if all the DSA ports phandles point to ports | |
165 | * known in the tree. A return value of 1 means the tree is not | |
166 | * complete. This is not an error condition. A value of 0 is | |
167 | * success. | |
168 | */ | |
169 | static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds) | |
170 | { | |
293784a8 | 171 | struct dsa_port *port; |
83c0afae AL |
172 | u32 index; |
173 | int err; | |
174 | ||
26895e29 | 175 | for (index = 0; index < ds->num_ports; index++) { |
293784a8 FF |
176 | port = &ds->ports[index]; |
177 | if (!dsa_port_is_valid(port)) | |
83c0afae AL |
178 | continue; |
179 | ||
180 | if (!dsa_port_is_dsa(port)) | |
181 | continue; | |
182 | ||
183 | err = dsa_port_complete(dst, ds, port, index); | |
184 | if (err != 0) | |
185 | return err; | |
186 | ||
187 | ds->dsa_port_mask |= BIT(index); | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | /* A tree is complete if all the DSA ports phandles point to ports | |
194 | * known in the tree. A return value of 1 means the tree is not | |
195 | * complete. This is not an error condition. A value of 0 is | |
196 | * success. | |
197 | */ | |
198 | static int dsa_dst_complete(struct dsa_switch_tree *dst) | |
199 | { | |
200 | struct dsa_switch *ds; | |
201 | u32 index; | |
202 | int err; | |
203 | ||
204 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
205 | ds = dst->ds[index]; | |
206 | if (!ds) | |
207 | continue; | |
208 | ||
209 | err = dsa_ds_complete(dst, ds); | |
210 | if (err != 0) | |
211 | return err; | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
e41c1b50 | 217 | static int dsa_dsa_port_apply(struct dsa_port *port) |
83c0afae | 218 | { |
e41c1b50 | 219 | struct dsa_switch *ds = port->ds; |
83c0afae AL |
220 | int err; |
221 | ||
47d0dcc3 | 222 | err = dsa_cpu_dsa_setup(port); |
83c0afae AL |
223 | if (err) { |
224 | dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n", | |
e41c1b50 | 225 | port->index, err); |
83c0afae AL |
226 | return err; |
227 | } | |
228 | ||
e41c1b50 | 229 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
96567d5d | 230 | |
e41c1b50 FF |
231 | return devlink_port_register(ds->devlink, &port->devlink_port, |
232 | port->index); | |
83c0afae AL |
233 | } |
234 | ||
e41c1b50 | 235 | static void dsa_dsa_port_unapply(struct dsa_port *port) |
83c0afae | 236 | { |
e41c1b50 | 237 | devlink_port_unregister(&port->devlink_port); |
83c0afae AL |
238 | dsa_cpu_dsa_destroy(port); |
239 | } | |
240 | ||
e41c1b50 | 241 | static int dsa_cpu_port_apply(struct dsa_port *port) |
83c0afae | 242 | { |
e41c1b50 | 243 | struct dsa_switch *ds = port->ds; |
83c0afae AL |
244 | int err; |
245 | ||
47d0dcc3 | 246 | err = dsa_cpu_dsa_setup(port); |
83c0afae AL |
247 | if (err) { |
248 | dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n", | |
e41c1b50 | 249 | port->index, err); |
83c0afae AL |
250 | return err; |
251 | } | |
252 | ||
e41c1b50 FF |
253 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
254 | err = devlink_port_register(ds->devlink, &port->devlink_port, | |
255 | port->index); | |
96567d5d | 256 | return err; |
83c0afae AL |
257 | } |
258 | ||
e41c1b50 | 259 | static void dsa_cpu_port_unapply(struct dsa_port *port) |
83c0afae | 260 | { |
e41c1b50 | 261 | devlink_port_unregister(&port->devlink_port); |
83c0afae | 262 | dsa_cpu_dsa_destroy(port); |
e41c1b50 | 263 | port->ds->cpu_port_mask &= ~BIT(port->index); |
83c0afae AL |
264 | |
265 | } | |
266 | ||
e41c1b50 | 267 | static int dsa_user_port_apply(struct dsa_port *port) |
83c0afae | 268 | { |
e41c1b50 | 269 | struct dsa_switch *ds = port->ds; |
71e0bbde | 270 | const char *name = port->name; |
83c0afae AL |
271 | int err; |
272 | ||
71e0bbde FF |
273 | if (port->dn) |
274 | name = of_get_property(port->dn, "label", NULL); | |
9f91484f VD |
275 | if (!name) |
276 | name = "eth%d"; | |
83c0afae | 277 | |
4cfbf09c | 278 | err = dsa_slave_create(port, name); |
83c0afae AL |
279 | if (err) { |
280 | dev_warn(ds->dev, "Failed to create slave %d: %d\n", | |
e41c1b50 | 281 | port->index, err); |
f8b8b1cd | 282 | port->slave = NULL; |
83c0afae AL |
283 | return err; |
284 | } | |
285 | ||
e41c1b50 FF |
286 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
287 | err = devlink_port_register(ds->devlink, &port->devlink_port, | |
288 | port->index); | |
96567d5d AL |
289 | if (err) |
290 | return err; | |
291 | ||
f8b8b1cd | 292 | devlink_port_type_eth_set(&port->devlink_port, port->slave); |
96567d5d | 293 | |
83c0afae AL |
294 | return 0; |
295 | } | |
296 | ||
e41c1b50 | 297 | static void dsa_user_port_unapply(struct dsa_port *port) |
83c0afae | 298 | { |
e41c1b50 | 299 | devlink_port_unregister(&port->devlink_port); |
f8b8b1cd VD |
300 | if (port->slave) { |
301 | dsa_slave_destroy(port->slave); | |
302 | port->slave = NULL; | |
e41c1b50 | 303 | port->ds->enabled_port_mask &= ~(1 << port->index); |
83c0afae AL |
304 | } |
305 | } | |
306 | ||
307 | static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds) | |
308 | { | |
293784a8 | 309 | struct dsa_port *port; |
83c0afae AL |
310 | u32 index; |
311 | int err; | |
312 | ||
6e830d8f | 313 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
9d490b4e | 314 | * driver and before ops->setup() has run, since the switch drivers and |
6e830d8f FF |
315 | * the slave MDIO bus driver rely on these values for probing PHY |
316 | * devices or not | |
317 | */ | |
318 | ds->phys_mii_mask = ds->enabled_port_mask; | |
319 | ||
96567d5d AL |
320 | /* Add the switch to devlink before calling setup, so that setup can |
321 | * add dpipe tables | |
322 | */ | |
323 | ds->devlink = devlink_alloc(&dsa_devlink_ops, 0); | |
324 | if (!ds->devlink) | |
325 | return -ENOMEM; | |
326 | ||
327 | err = devlink_register(ds->devlink, ds->dev); | |
328 | if (err) | |
329 | return err; | |
330 | ||
9d490b4e | 331 | err = ds->ops->setup(ds); |
83c0afae AL |
332 | if (err < 0) |
333 | return err; | |
334 | ||
f515f192 VD |
335 | err = dsa_switch_register_notifier(ds); |
336 | if (err) | |
337 | return err; | |
338 | ||
9d490b4e | 339 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
1eb59443 FF |
340 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
341 | if (!ds->slave_mii_bus) | |
342 | return -ENOMEM; | |
343 | ||
344 | dsa_slave_mii_bus_init(ds); | |
345 | ||
346 | err = mdiobus_register(ds->slave_mii_bus); | |
347 | if (err < 0) | |
348 | return err; | |
349 | } | |
350 | ||
26895e29 | 351 | for (index = 0; index < ds->num_ports; index++) { |
293784a8 FF |
352 | port = &ds->ports[index]; |
353 | if (!dsa_port_is_valid(port)) | |
83c0afae AL |
354 | continue; |
355 | ||
356 | if (dsa_port_is_dsa(port)) { | |
e41c1b50 | 357 | err = dsa_dsa_port_apply(port); |
83c0afae AL |
358 | if (err) |
359 | return err; | |
360 | continue; | |
361 | } | |
362 | ||
363 | if (dsa_port_is_cpu(port)) { | |
e41c1b50 | 364 | err = dsa_cpu_port_apply(port); |
83c0afae AL |
365 | if (err) |
366 | return err; | |
367 | continue; | |
368 | } | |
369 | ||
e41c1b50 | 370 | err = dsa_user_port_apply(port); |
83c0afae AL |
371 | if (err) |
372 | continue; | |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds) | |
379 | { | |
293784a8 | 380 | struct dsa_port *port; |
83c0afae AL |
381 | u32 index; |
382 | ||
26895e29 | 383 | for (index = 0; index < ds->num_ports; index++) { |
293784a8 FF |
384 | port = &ds->ports[index]; |
385 | if (!dsa_port_is_valid(port)) | |
83c0afae AL |
386 | continue; |
387 | ||
388 | if (dsa_port_is_dsa(port)) { | |
e41c1b50 | 389 | dsa_dsa_port_unapply(port); |
83c0afae AL |
390 | continue; |
391 | } | |
392 | ||
393 | if (dsa_port_is_cpu(port)) { | |
e41c1b50 | 394 | dsa_cpu_port_unapply(port); |
83c0afae AL |
395 | continue; |
396 | } | |
397 | ||
e41c1b50 | 398 | dsa_user_port_unapply(port); |
83c0afae | 399 | } |
1eb59443 | 400 | |
9d490b4e | 401 | if (ds->slave_mii_bus && ds->ops->phy_read) |
1eb59443 | 402 | mdiobus_unregister(ds->slave_mii_bus); |
f515f192 VD |
403 | |
404 | dsa_switch_unregister_notifier(ds); | |
96567d5d AL |
405 | |
406 | if (ds->devlink) { | |
407 | devlink_unregister(ds->devlink); | |
408 | devlink_free(ds->devlink); | |
409 | ds->devlink = NULL; | |
410 | } | |
411 | ||
83c0afae AL |
412 | } |
413 | ||
414 | static int dsa_dst_apply(struct dsa_switch_tree *dst) | |
415 | { | |
416 | struct dsa_switch *ds; | |
417 | u32 index; | |
418 | int err; | |
419 | ||
420 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
421 | ds = dst->ds[index]; | |
422 | if (!ds) | |
423 | continue; | |
424 | ||
425 | err = dsa_ds_apply(dst, ds); | |
426 | if (err) | |
427 | return err; | |
428 | } | |
429 | ||
430 | /* If we use a tagging format that doesn't have an ethertype | |
431 | * field, make sure that all packets from this point on get | |
432 | * sent to the tag format's receive function. | |
433 | */ | |
434 | wmb(); | |
f8b8b1cd | 435 | dst->cpu_dp->master->dsa_ptr = dst->cpu_dp; |
1943563d | 436 | |
f8b8b1cd | 437 | err = dsa_master_ethtool_setup(dst->cpu_dp->master); |
1943563d VD |
438 | if (err) |
439 | return err; | |
440 | ||
83c0afae AL |
441 | dst->applied = true; |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | static void dsa_dst_unapply(struct dsa_switch_tree *dst) | |
447 | { | |
448 | struct dsa_switch *ds; | |
449 | u32 index; | |
450 | ||
451 | if (!dst->applied) | |
452 | return; | |
453 | ||
f8b8b1cd | 454 | dsa_master_ethtool_restore(dst->cpu_dp->master); |
1943563d | 455 | |
f8b8b1cd | 456 | dst->cpu_dp->master->dsa_ptr = NULL; |
83c0afae AL |
457 | |
458 | /* If we used a tagging format that doesn't have an ethertype | |
459 | * field, make sure that all packets from this point get sent | |
460 | * without the tag and go through the regular receive path. | |
461 | */ | |
462 | wmb(); | |
463 | ||
464 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
465 | ds = dst->ds[index]; | |
466 | if (!ds) | |
467 | continue; | |
468 | ||
469 | dsa_ds_unapply(dst, ds); | |
470 | } | |
471 | ||
cd8d7dd4 | 472 | dst->cpu_dp = NULL; |
0c73c523 | 473 | |
83c0afae AL |
474 | pr_info("DSA: tree %d unapplied\n", dst->tree); |
475 | dst->applied = false; | |
476 | } | |
477 | ||
293784a8 | 478 | static int dsa_cpu_parse(struct dsa_port *port, u32 index, |
83c0afae AL |
479 | struct dsa_switch_tree *dst, |
480 | struct dsa_switch *ds) | |
481 | { | |
62fc9587 | 482 | const struct dsa_device_ops *tag_ops; |
7b314362 | 483 | enum dsa_tag_protocol tag_protocol; |
83c0afae AL |
484 | struct net_device *ethernet_dev; |
485 | struct device_node *ethernet; | |
486 | ||
71e0bbde FF |
487 | if (port->dn) { |
488 | ethernet = of_parse_phandle(port->dn, "ethernet", 0); | |
489 | if (!ethernet) | |
490 | return -EINVAL; | |
491 | ethernet_dev = of_find_net_device_by_node(ethernet); | |
492 | } else { | |
493 | ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]); | |
494 | dev_put(ethernet_dev); | |
495 | } | |
83c0afae | 496 | |
83c0afae AL |
497 | if (!ethernet_dev) |
498 | return -EPROBE_DEFER; | |
499 | ||
6d3c8c0d | 500 | if (!dst->cpu_dp) { |
8b0d3ea5 | 501 | dst->cpu_dp = port; |
f8b8b1cd | 502 | dst->cpu_dp->master = ethernet_dev; |
6d3c8c0d | 503 | } |
83c0afae | 504 | |
9f9e772d FF |
505 | /* Initialize cpu_port_mask now for drv->setup() |
506 | * to have access to a correct value, just like what | |
507 | * net/dsa/dsa.c::dsa_switch_setup_one does. | |
508 | */ | |
509 | ds->cpu_port_mask |= BIT(index); | |
510 | ||
9d490b4e | 511 | tag_protocol = ds->ops->get_tag_protocol(ds); |
62fc9587 VD |
512 | tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
513 | if (IS_ERR(tag_ops)) { | |
83c0afae | 514 | dev_warn(ds->dev, "No tagger for this switch\n"); |
9f9e772d | 515 | ds->cpu_port_mask &= ~BIT(index); |
62fc9587 | 516 | return PTR_ERR(tag_ops); |
83c0afae AL |
517 | } |
518 | ||
15240248 | 519 | dst->cpu_dp->tag_ops = tag_ops; |
3e41f93b VD |
520 | |
521 | /* Make a few copies for faster access in master receive hot path */ | |
522 | dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv; | |
3e41f93b | 523 | dst->cpu_dp->dst = dst; |
83c0afae AL |
524 | |
525 | return 0; | |
526 | } | |
527 | ||
528 | static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds) | |
529 | { | |
293784a8 | 530 | struct dsa_port *port; |
83c0afae AL |
531 | u32 index; |
532 | int err; | |
533 | ||
26895e29 | 534 | for (index = 0; index < ds->num_ports; index++) { |
293784a8 | 535 | port = &ds->ports[index]; |
14be36c2 FF |
536 | if (!dsa_port_is_valid(port) || |
537 | dsa_port_is_dsa(port)) | |
83c0afae AL |
538 | continue; |
539 | ||
540 | if (dsa_port_is_cpu(port)) { | |
541 | err = dsa_cpu_parse(port, index, dst, ds); | |
542 | if (err) | |
543 | return err; | |
14be36c2 FF |
544 | } else { |
545 | /* Initialize enabled_port_mask now for drv->setup() | |
546 | * to have access to a correct value, just like what | |
547 | * net/dsa/dsa.c::dsa_switch_setup_one does. | |
548 | */ | |
549 | ds->enabled_port_mask |= BIT(index); | |
83c0afae | 550 | } |
14be36c2 | 551 | |
83c0afae AL |
552 | } |
553 | ||
554 | pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index); | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | static int dsa_dst_parse(struct dsa_switch_tree *dst) | |
560 | { | |
561 | struct dsa_switch *ds; | |
e4b77787 | 562 | struct dsa_port *dp; |
83c0afae | 563 | u32 index; |
e4b77787 | 564 | int port; |
83c0afae AL |
565 | int err; |
566 | ||
567 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
568 | ds = dst->ds[index]; | |
569 | if (!ds) | |
570 | continue; | |
571 | ||
572 | err = dsa_ds_parse(dst, ds); | |
573 | if (err) | |
574 | return err; | |
575 | } | |
576 | ||
c7848399 | 577 | if (!dst->cpu_dp) { |
83c0afae AL |
578 | pr_warn("Tree has no master device\n"); |
579 | return -EINVAL; | |
580 | } | |
581 | ||
e4b77787 VD |
582 | /* Assign the default CPU port to all ports of the fabric */ |
583 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { | |
584 | ds = dst->ds[index]; | |
585 | if (!ds) | |
586 | continue; | |
587 | ||
588 | for (port = 0; port < ds->num_ports; port++) { | |
589 | dp = &ds->ports[port]; | |
590 | if (!dsa_port_is_valid(dp) || | |
591 | dsa_port_is_dsa(dp) || | |
592 | dsa_port_is_cpu(dp)) | |
593 | continue; | |
594 | ||
595 | dp->cpu_dp = dst->cpu_dp; | |
596 | } | |
597 | } | |
598 | ||
83c0afae AL |
599 | pr_info("DSA: tree %d parsed\n", dst->tree); |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
604 | static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds) | |
605 | { | |
606 | struct device_node *port; | |
607 | int err; | |
608 | u32 reg; | |
609 | ||
610 | for_each_available_child_of_node(ports, port) { | |
611 | err = of_property_read_u32(port, "reg", ®); | |
612 | if (err) | |
613 | return err; | |
614 | ||
26895e29 | 615 | if (reg >= ds->num_ports) |
83c0afae AL |
616 | return -EINVAL; |
617 | ||
618 | ds->ports[reg].dn = port; | |
619 | } | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
71e0bbde FF |
624 | static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds) |
625 | { | |
626 | bool valid_name_found = false; | |
627 | unsigned int i; | |
628 | ||
629 | for (i = 0; i < DSA_MAX_PORTS; i++) { | |
630 | if (!cd->port_names[i]) | |
631 | continue; | |
632 | ||
633 | ds->ports[i].name = cd->port_names[i]; | |
71e0bbde FF |
634 | valid_name_found = true; |
635 | } | |
636 | ||
637 | if (!valid_name_found && i == DSA_MAX_PORTS) | |
638 | return -EINVAL; | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
3512a8e9 | 643 | static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index) |
83c0afae AL |
644 | { |
645 | int err; | |
646 | ||
647 | *tree = *index = 0; | |
648 | ||
649 | err = of_property_read_u32_index(np, "dsa,member", 0, tree); | |
650 | if (err) { | |
651 | /* Does not exist, but it is optional */ | |
652 | if (err == -EINVAL) | |
653 | return 0; | |
654 | return err; | |
655 | } | |
656 | ||
657 | err = of_property_read_u32_index(np, "dsa,member", 1, index); | |
658 | if (err) | |
659 | return err; | |
660 | ||
661 | if (*index >= DSA_MAX_SWITCHES) | |
662 | return -EINVAL; | |
663 | ||
664 | return 0; | |
665 | } | |
666 | ||
71e0bbde FF |
667 | static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index) |
668 | { | |
669 | if (!pd) | |
670 | return -ENODEV; | |
671 | ||
672 | /* We do not support complex trees with dsa_chip_data */ | |
673 | *tree = 0; | |
674 | *index = 0; | |
675 | ||
676 | return 0; | |
677 | } | |
678 | ||
83c0afae AL |
679 | static struct device_node *dsa_get_ports(struct dsa_switch *ds, |
680 | struct device_node *np) | |
681 | { | |
682 | struct device_node *ports; | |
683 | ||
684 | ports = of_get_child_by_name(np, "ports"); | |
685 | if (!ports) { | |
686 | dev_err(ds->dev, "no ports child node found\n"); | |
687 | return ERR_PTR(-EINVAL); | |
688 | } | |
689 | ||
690 | return ports; | |
691 | } | |
692 | ||
23c9ee49 | 693 | static int _dsa_register_switch(struct dsa_switch *ds) |
83c0afae | 694 | { |
23c9ee49 VD |
695 | struct dsa_chip_data *pdata = ds->dev->platform_data; |
696 | struct device_node *np = ds->dev->of_node; | |
83c0afae | 697 | struct dsa_switch_tree *dst; |
bc1727d2 | 698 | struct device_node *ports; |
83c0afae | 699 | u32 tree, index; |
d390238c | 700 | int i, err; |
83c0afae | 701 | |
71e0bbde FF |
702 | if (np) { |
703 | err = dsa_parse_member_dn(np, &tree, &index); | |
704 | if (err) | |
705 | return err; | |
83c0afae | 706 | |
71e0bbde FF |
707 | ports = dsa_get_ports(ds, np); |
708 | if (IS_ERR(ports)) | |
709 | return PTR_ERR(ports); | |
83c0afae | 710 | |
71e0bbde FF |
711 | err = dsa_parse_ports_dn(ports, ds); |
712 | if (err) | |
713 | return err; | |
714 | } else { | |
715 | err = dsa_parse_member(pdata, &tree, &index); | |
716 | if (err) | |
717 | return err; | |
718 | ||
719 | err = dsa_parse_ports(pdata, ds); | |
720 | if (err) | |
721 | return err; | |
722 | } | |
83c0afae AL |
723 | |
724 | dst = dsa_get_dst(tree); | |
725 | if (!dst) { | |
726 | dst = dsa_add_dst(tree); | |
727 | if (!dst) | |
728 | return -ENOMEM; | |
729 | } | |
730 | ||
731 | if (dst->ds[index]) { | |
732 | err = -EBUSY; | |
733 | goto out; | |
734 | } | |
735 | ||
736 | ds->dst = dst; | |
737 | ds->index = index; | |
71e0bbde | 738 | ds->cd = pdata; |
d390238c VD |
739 | |
740 | /* Initialize the routing table */ | |
741 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) | |
742 | ds->rtable[i] = DSA_RTABLE_NONE; | |
743 | ||
83c0afae AL |
744 | dsa_dst_add_ds(dst, ds, index); |
745 | ||
746 | err = dsa_dst_complete(dst); | |
747 | if (err < 0) | |
748 | goto out_del_dst; | |
749 | ||
750 | if (err == 1) { | |
751 | /* Not all switches registered yet */ | |
752 | err = 0; | |
753 | goto out; | |
754 | } | |
755 | ||
756 | if (dst->applied) { | |
757 | pr_info("DSA: Disjoint trees?\n"); | |
758 | return -EINVAL; | |
759 | } | |
760 | ||
761 | err = dsa_dst_parse(dst); | |
5e6eb456 VB |
762 | if (err) { |
763 | if (err == -EPROBE_DEFER) { | |
764 | dsa_dst_del_ds(dst, ds, ds->index); | |
765 | return err; | |
766 | } | |
767 | ||
83c0afae | 768 | goto out_del_dst; |
5e6eb456 | 769 | } |
83c0afae AL |
770 | |
771 | err = dsa_dst_apply(dst); | |
772 | if (err) { | |
773 | dsa_dst_unapply(dst); | |
774 | goto out_del_dst; | |
775 | } | |
776 | ||
777 | dsa_put_dst(dst); | |
778 | return 0; | |
779 | ||
780 | out_del_dst: | |
781 | dsa_dst_del_ds(dst, ds, ds->index); | |
782 | out: | |
783 | dsa_put_dst(dst); | |
784 | ||
785 | return err; | |
786 | } | |
787 | ||
a0c02161 VD |
788 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
789 | { | |
790 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); | |
791 | struct dsa_switch *ds; | |
818be848 | 792 | int i; |
a0c02161 VD |
793 | |
794 | ds = devm_kzalloc(dev, size, GFP_KERNEL); | |
795 | if (!ds) | |
796 | return NULL; | |
797 | ||
798 | ds->dev = dev; | |
799 | ds->num_ports = n; | |
800 | ||
818be848 VD |
801 | for (i = 0; i < ds->num_ports; ++i) { |
802 | ds->ports[i].index = i; | |
803 | ds->ports[i].ds = ds; | |
804 | } | |
805 | ||
a0c02161 VD |
806 | return ds; |
807 | } | |
808 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); | |
809 | ||
23c9ee49 | 810 | int dsa_register_switch(struct dsa_switch *ds) |
83c0afae AL |
811 | { |
812 | int err; | |
813 | ||
814 | mutex_lock(&dsa2_mutex); | |
23c9ee49 | 815 | err = _dsa_register_switch(ds); |
83c0afae AL |
816 | mutex_unlock(&dsa2_mutex); |
817 | ||
818 | return err; | |
819 | } | |
820 | EXPORT_SYMBOL_GPL(dsa_register_switch); | |
821 | ||
85c22bad | 822 | static void _dsa_unregister_switch(struct dsa_switch *ds) |
83c0afae AL |
823 | { |
824 | struct dsa_switch_tree *dst = ds->dst; | |
825 | ||
826 | dsa_dst_unapply(dst); | |
827 | ||
828 | dsa_dst_del_ds(dst, ds, ds->index); | |
829 | } | |
830 | ||
831 | void dsa_unregister_switch(struct dsa_switch *ds) | |
832 | { | |
833 | mutex_lock(&dsa2_mutex); | |
834 | _dsa_unregister_switch(ds); | |
835 | mutex_unlock(&dsa2_mutex); | |
836 | } | |
837 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |