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