]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/dsa/dsa2.c
mtd: nand: atmel: Relax tADL_min constraint
[mirror_ubuntu-artful-kernel.git] / net / dsa / dsa2.c
CommitLineData
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
24static LIST_HEAD(dsa_switch_trees);
25static DEFINE_MUTEX(dsa2_mutex);
26
96567d5d
AL
27static const struct devlink_ops dsa_devlink_ops = {
28};
29
83c0afae
AL
30static 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
42static 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
51static void dsa_put_dst(struct dsa_switch_tree *dst)
52{
53 kref_put(&dst->refcount, dsa_free_dst);
54}
55
56static 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
71static 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
78static 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 88static bool dsa_port_is_valid(struct dsa_port *port)
83c0afae 89{
71e0bbde 90 return !!(port->dn || port->name);
83c0afae
AL
91}
92
293784a8 93static 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
101static 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
109static 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
120static 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
138static 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 */
169static 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 */
198static 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 217static 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
e41c1b50 222 err = dsa_cpu_dsa_setup(ds, ds->dev, port, port->index);
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 235static 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 241static 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
e41c1b50 246 err = dsa_cpu_dsa_setup(ds, ds->dev, port, port->index);
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 259static 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 267static 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
e41c1b50 278 err = dsa_slave_create(ds, ds->dev, port->index, name);
83c0afae
AL
279 if (err) {
280 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
e41c1b50
FF
281 port->index, err);
282 port->netdev = 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
e41c1b50 292 devlink_port_type_eth_set(&port->devlink_port, port->netdev);
96567d5d 293
83c0afae
AL
294 return 0;
295}
296
e41c1b50 297static void dsa_user_port_unapply(struct dsa_port *port)
83c0afae 298{
e41c1b50
FF
299 devlink_port_unregister(&port->devlink_port);
300 if (port->netdev) {
301 dsa_slave_destroy(port->netdev);
302 port->netdev = NULL;
303 port->ds->enabled_port_mask &= ~(1 << port->index);
83c0afae
AL
304 }
305}
306
307static 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
092183df 339 if (ds->ops->set_addr) {
6d3c8c0d 340 err = ds->ops->set_addr(ds, dst->cpu_dp->netdev->dev_addr);
092183df
JC
341 if (err < 0)
342 return err;
343 }
83c0afae 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
26895e29 357 for (index = 0; index < ds->num_ports; index++) {
293784a8
FF
358 port = &ds->ports[index];
359 if (!dsa_port_is_valid(port))
83c0afae
AL
360 continue;
361
362 if (dsa_port_is_dsa(port)) {
e41c1b50 363 err = dsa_dsa_port_apply(port);
83c0afae
AL
364 if (err)
365 return err;
366 continue;
367 }
368
369 if (dsa_port_is_cpu(port)) {
e41c1b50 370 err = dsa_cpu_port_apply(port);
83c0afae
AL
371 if (err)
372 return err;
373 continue;
374 }
375
e41c1b50 376 err = dsa_user_port_apply(port);
83c0afae
AL
377 if (err)
378 continue;
379 }
380
381 return 0;
382}
383
384static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
385{
293784a8 386 struct dsa_port *port;
83c0afae
AL
387 u32 index;
388
26895e29 389 for (index = 0; index < ds->num_ports; index++) {
293784a8
FF
390 port = &ds->ports[index];
391 if (!dsa_port_is_valid(port))
83c0afae
AL
392 continue;
393
394 if (dsa_port_is_dsa(port)) {
e41c1b50 395 dsa_dsa_port_unapply(port);
83c0afae
AL
396 continue;
397 }
398
399 if (dsa_port_is_cpu(port)) {
e41c1b50 400 dsa_cpu_port_unapply(port);
83c0afae
AL
401 continue;
402 }
403
e41c1b50 404 dsa_user_port_unapply(port);
83c0afae 405 }
1eb59443 406
9d490b4e 407 if (ds->slave_mii_bus && ds->ops->phy_read)
1eb59443 408 mdiobus_unregister(ds->slave_mii_bus);
f515f192
VD
409
410 dsa_switch_unregister_notifier(ds);
96567d5d
AL
411
412 if (ds->devlink) {
413 devlink_unregister(ds->devlink);
414 devlink_free(ds->devlink);
415 ds->devlink = NULL;
416 }
417
83c0afae
AL
418}
419
420static int dsa_dst_apply(struct dsa_switch_tree *dst)
421{
422 struct dsa_switch *ds;
423 u32 index;
424 int err;
425
426 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
427 ds = dst->ds[index];
428 if (!ds)
429 continue;
430
431 err = dsa_ds_apply(dst, ds);
432 if (err)
433 return err;
434 }
435
8b0d3ea5 436 if (dst->cpu_dp) {
937c7df8 437 err = dsa_cpu_port_ethtool_setup(dst->cpu_dp);
faf3a932
FF
438 if (err)
439 return err;
440 }
0c73c523 441
83c0afae
AL
442 /* If we use a tagging format that doesn't have an ethertype
443 * field, make sure that all packets from this point on get
444 * sent to the tag format's receive function.
445 */
446 wmb();
6d3c8c0d 447 dst->cpu_dp->netdev->dsa_ptr = dst;
83c0afae
AL
448 dst->applied = true;
449
450 return 0;
451}
452
453static void dsa_dst_unapply(struct dsa_switch_tree *dst)
454{
455 struct dsa_switch *ds;
456 u32 index;
457
458 if (!dst->applied)
459 return;
460
6d3c8c0d 461 dst->cpu_dp->netdev->dsa_ptr = NULL;
83c0afae
AL
462
463 /* If we used a tagging format that doesn't have an ethertype
464 * field, make sure that all packets from this point get sent
465 * without the tag and go through the regular receive path.
466 */
467 wmb();
468
469 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
470 ds = dst->ds[index];
471 if (!ds)
472 continue;
473
474 dsa_ds_unapply(dst, ds);
475 }
476
216fe8f0 477 if (dst->cpu_dp) {
937c7df8 478 dsa_cpu_port_ethtool_restore(dst->cpu_dp);
216fe8f0 479 dst->cpu_dp = NULL;
b07ac989 480 }
0c73c523 481
83c0afae
AL
482 pr_info("DSA: tree %d unapplied\n", dst->tree);
483 dst->applied = false;
484}
485
293784a8 486static int dsa_cpu_parse(struct dsa_port *port, u32 index,
83c0afae
AL
487 struct dsa_switch_tree *dst,
488 struct dsa_switch *ds)
489{
7b314362 490 enum dsa_tag_protocol tag_protocol;
83c0afae
AL
491 struct net_device *ethernet_dev;
492 struct device_node *ethernet;
493
71e0bbde
FF
494 if (port->dn) {
495 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
496 if (!ethernet)
497 return -EINVAL;
498 ethernet_dev = of_find_net_device_by_node(ethernet);
499 } else {
500 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
501 dev_put(ethernet_dev);
502 }
83c0afae 503
83c0afae
AL
504 if (!ethernet_dev)
505 return -EPROBE_DEFER;
506
6d3c8c0d 507 if (!dst->cpu_dp) {
8b0d3ea5 508 dst->cpu_dp = port;
6d3c8c0d
FF
509 dst->cpu_dp->netdev = ethernet_dev;
510 }
83c0afae 511
9d490b4e 512 tag_protocol = ds->ops->get_tag_protocol(ds);
7b314362 513 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
83c0afae
AL
514 if (IS_ERR(dst->tag_ops)) {
515 dev_warn(ds->dev, "No tagger for this switch\n");
516 return PTR_ERR(dst->tag_ops);
517 }
518
519 dst->rcv = dst->tag_ops->rcv;
520
14be36c2
FF
521 /* Initialize cpu_port_mask now for drv->setup()
522 * to have access to a correct value, just like what
523 * net/dsa/dsa.c::dsa_switch_setup_one does.
524 */
525 ds->cpu_port_mask |= BIT(index);
526
83c0afae
AL
527 return 0;
528}
529
530static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
531{
293784a8 532 struct dsa_port *port;
83c0afae
AL
533 u32 index;
534 int err;
535
26895e29 536 for (index = 0; index < ds->num_ports; index++) {
293784a8 537 port = &ds->ports[index];
14be36c2
FF
538 if (!dsa_port_is_valid(port) ||
539 dsa_port_is_dsa(port))
83c0afae
AL
540 continue;
541
542 if (dsa_port_is_cpu(port)) {
543 err = dsa_cpu_parse(port, index, dst, ds);
544 if (err)
545 return err;
14be36c2
FF
546 } else {
547 /* Initialize enabled_port_mask now for drv->setup()
548 * to have access to a correct value, just like what
549 * net/dsa/dsa.c::dsa_switch_setup_one does.
550 */
551 ds->enabled_port_mask |= BIT(index);
83c0afae 552 }
14be36c2 553
83c0afae
AL
554 }
555
556 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
557
558 return 0;
559}
560
561static int dsa_dst_parse(struct dsa_switch_tree *dst)
562{
563 struct dsa_switch *ds;
e4b77787 564 struct dsa_port *dp;
83c0afae 565 u32 index;
e4b77787 566 int port;
83c0afae
AL
567 int err;
568
569 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
570 ds = dst->ds[index];
571 if (!ds)
572 continue;
573
574 err = dsa_ds_parse(dst, ds);
575 if (err)
576 return err;
577 }
578
6d3c8c0d 579 if (!dst->cpu_dp->netdev) {
83c0afae
AL
580 pr_warn("Tree has no master device\n");
581 return -EINVAL;
582 }
583
e4b77787
VD
584 /* Assign the default CPU port to all ports of the fabric */
585 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
586 ds = dst->ds[index];
587 if (!ds)
588 continue;
589
590 for (port = 0; port < ds->num_ports; port++) {
591 dp = &ds->ports[port];
592 if (!dsa_port_is_valid(dp) ||
593 dsa_port_is_dsa(dp) ||
594 dsa_port_is_cpu(dp))
595 continue;
596
597 dp->cpu_dp = dst->cpu_dp;
598 }
599 }
600
83c0afae
AL
601 pr_info("DSA: tree %d parsed\n", dst->tree);
602
603 return 0;
604}
605
606static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
607{
608 struct device_node *port;
609 int err;
610 u32 reg;
611
612 for_each_available_child_of_node(ports, port) {
613 err = of_property_read_u32(port, "reg", &reg);
614 if (err)
615 return err;
616
26895e29 617 if (reg >= ds->num_ports)
83c0afae
AL
618 return -EINVAL;
619
620 ds->ports[reg].dn = port;
621 }
622
623 return 0;
624}
625
71e0bbde
FF
626static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
627{
628 bool valid_name_found = false;
629 unsigned int i;
630
631 for (i = 0; i < DSA_MAX_PORTS; i++) {
632 if (!cd->port_names[i])
633 continue;
634
635 ds->ports[i].name = cd->port_names[i];
71e0bbde
FF
636 valid_name_found = true;
637 }
638
639 if (!valid_name_found && i == DSA_MAX_PORTS)
640 return -EINVAL;
641
642 return 0;
643}
644
3512a8e9 645static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
83c0afae
AL
646{
647 int err;
648
649 *tree = *index = 0;
650
651 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
652 if (err) {
653 /* Does not exist, but it is optional */
654 if (err == -EINVAL)
655 return 0;
656 return err;
657 }
658
659 err = of_property_read_u32_index(np, "dsa,member", 1, index);
660 if (err)
661 return err;
662
663 if (*index >= DSA_MAX_SWITCHES)
664 return -EINVAL;
665
666 return 0;
667}
668
71e0bbde
FF
669static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
670{
671 if (!pd)
672 return -ENODEV;
673
674 /* We do not support complex trees with dsa_chip_data */
675 *tree = 0;
676 *index = 0;
677
678 return 0;
679}
680
83c0afae
AL
681static struct device_node *dsa_get_ports(struct dsa_switch *ds,
682 struct device_node *np)
683{
684 struct device_node *ports;
685
686 ports = of_get_child_by_name(np, "ports");
687 if (!ports) {
688 dev_err(ds->dev, "no ports child node found\n");
689 return ERR_PTR(-EINVAL);
690 }
691
692 return ports;
693}
694
23c9ee49 695static int _dsa_register_switch(struct dsa_switch *ds)
83c0afae 696{
23c9ee49
VD
697 struct dsa_chip_data *pdata = ds->dev->platform_data;
698 struct device_node *np = ds->dev->of_node;
83c0afae 699 struct dsa_switch_tree *dst;
bc1727d2 700 struct device_node *ports;
83c0afae 701 u32 tree, index;
d390238c 702 int i, err;
83c0afae 703
71e0bbde
FF
704 if (np) {
705 err = dsa_parse_member_dn(np, &tree, &index);
706 if (err)
707 return err;
83c0afae 708
71e0bbde
FF
709 ports = dsa_get_ports(ds, np);
710 if (IS_ERR(ports))
711 return PTR_ERR(ports);
83c0afae 712
71e0bbde
FF
713 err = dsa_parse_ports_dn(ports, ds);
714 if (err)
715 return err;
716 } else {
717 err = dsa_parse_member(pdata, &tree, &index);
718 if (err)
719 return err;
720
721 err = dsa_parse_ports(pdata, ds);
722 if (err)
723 return err;
724 }
83c0afae
AL
725
726 dst = dsa_get_dst(tree);
727 if (!dst) {
728 dst = dsa_add_dst(tree);
729 if (!dst)
730 return -ENOMEM;
731 }
732
733 if (dst->ds[index]) {
734 err = -EBUSY;
735 goto out;
736 }
737
738 ds->dst = dst;
739 ds->index = index;
71e0bbde 740 ds->cd = pdata;
d390238c
VD
741
742 /* Initialize the routing table */
743 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
744 ds->rtable[i] = DSA_RTABLE_NONE;
745
83c0afae
AL
746 dsa_dst_add_ds(dst, ds, index);
747
748 err = dsa_dst_complete(dst);
749 if (err < 0)
750 goto out_del_dst;
751
752 if (err == 1) {
753 /* Not all switches registered yet */
754 err = 0;
755 goto out;
756 }
757
758 if (dst->applied) {
759 pr_info("DSA: Disjoint trees?\n");
760 return -EINVAL;
761 }
762
763 err = dsa_dst_parse(dst);
5e6eb456
VB
764 if (err) {
765 if (err == -EPROBE_DEFER) {
766 dsa_dst_del_ds(dst, ds, ds->index);
767 return err;
768 }
769
83c0afae 770 goto out_del_dst;
5e6eb456 771 }
83c0afae
AL
772
773 err = dsa_dst_apply(dst);
774 if (err) {
775 dsa_dst_unapply(dst);
776 goto out_del_dst;
777 }
778
779 dsa_put_dst(dst);
780 return 0;
781
782out_del_dst:
783 dsa_dst_del_ds(dst, ds, ds->index);
784out:
785 dsa_put_dst(dst);
786
787 return err;
788}
789
a0c02161
VD
790struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
791{
792 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
793 struct dsa_switch *ds;
818be848 794 int i;
a0c02161
VD
795
796 ds = devm_kzalloc(dev, size, GFP_KERNEL);
797 if (!ds)
798 return NULL;
799
800 ds->dev = dev;
801 ds->num_ports = n;
802
818be848
VD
803 for (i = 0; i < ds->num_ports; ++i) {
804 ds->ports[i].index = i;
805 ds->ports[i].ds = ds;
806 }
807
a0c02161
VD
808 return ds;
809}
810EXPORT_SYMBOL_GPL(dsa_switch_alloc);
811
23c9ee49 812int dsa_register_switch(struct dsa_switch *ds)
83c0afae
AL
813{
814 int err;
815
816 mutex_lock(&dsa2_mutex);
23c9ee49 817 err = _dsa_register_switch(ds);
83c0afae
AL
818 mutex_unlock(&dsa2_mutex);
819
820 return err;
821}
822EXPORT_SYMBOL_GPL(dsa_register_switch);
823
85c22bad 824static void _dsa_unregister_switch(struct dsa_switch *ds)
83c0afae
AL
825{
826 struct dsa_switch_tree *dst = ds->dst;
827
828 dsa_dst_unapply(dst);
829
830 dsa_dst_del_ds(dst, ds, ds->index);
831}
832
833void dsa_unregister_switch(struct dsa_switch *ds)
834{
835 mutex_lock(&dsa2_mutex);
836 _dsa_unregister_switch(ds);
837 mutex_unlock(&dsa2_mutex);
838}
839EXPORT_SYMBOL_GPL(dsa_unregister_switch);