]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/dsa/dsa2.c
Merge tag 'pm-5.11-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[mirror_ubuntu-jammy-kernel.git] / net / dsa / dsa2.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
83c0afae
AL
2/*
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
83c0afae
AL
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/list.h>
c6e970a0 12#include <linux/netdevice.h>
83c0afae
AL
13#include <linux/slab.h>
14#include <linux/rtnetlink.h>
83c0afae
AL
15#include <linux/of.h>
16#include <linux/of_net.h>
402f99e5 17#include <net/devlink.h>
ea5dd34b 18
83c0afae
AL
19#include "dsa_priv.h"
20
83c0afae 21static DEFINE_MUTEX(dsa2_mutex);
bff33f7e 22LIST_HEAD(dsa_tree_list);
83c0afae 23
3b7bc1f0
VO
24struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
25{
26 struct dsa_switch_tree *dst;
27 struct dsa_port *dp;
28
29 list_for_each_entry(dst, &dsa_tree_list, list) {
30 if (dst->index != tree_index)
31 continue;
32
33 list_for_each_entry(dp, &dst->ports, list) {
34 if (dp->ds->index != sw_index)
35 continue;
36
37 return dp->ds;
38 }
39 }
40
41 return NULL;
42}
43EXPORT_SYMBOL_GPL(dsa_switch_find);
44
1ca28ec9 45static struct dsa_switch_tree *dsa_tree_find(int index)
83c0afae
AL
46{
47 struct dsa_switch_tree *dst;
48
1ca28ec9 49 list_for_each_entry(dst, &dsa_tree_list, list)
8e5bf975 50 if (dst->index == index)
83c0afae 51 return dst;
8e5bf975 52
83c0afae
AL
53 return NULL;
54}
55
1ca28ec9 56static struct dsa_switch_tree *dsa_tree_alloc(int index)
83c0afae
AL
57{
58 struct dsa_switch_tree *dst;
59
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61 if (!dst)
62 return NULL;
1ca28ec9 63
49463b7f 64 dst->index = index;
1ca28ec9 65
c5f51765
VD
66 INIT_LIST_HEAD(&dst->rtable);
67
ab8ccae1
VD
68 INIT_LIST_HEAD(&dst->ports);
69
83c0afae 70 INIT_LIST_HEAD(&dst->list);
50c7d2ba 71 list_add_tail(&dst->list, &dsa_tree_list);
8e5bf975 72
83c0afae
AL
73 kref_init(&dst->refcount);
74
75 return dst;
76}
77
65254108
VD
78static void dsa_tree_free(struct dsa_switch_tree *dst)
79{
80 list_del(&dst->list);
81 kfree(dst);
82}
83
9e741045 84static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
1ca28ec9 85{
9e741045
VD
86 if (dst)
87 kref_get(&dst->refcount);
1ca28ec9
VD
88
89 return dst;
90}
91
9e741045 92static struct dsa_switch_tree *dsa_tree_touch(int index)
65254108 93{
9e741045
VD
94 struct dsa_switch_tree *dst;
95
96 dst = dsa_tree_find(index);
97 if (dst)
98 return dsa_tree_get(dst);
99 else
100 return dsa_tree_alloc(index);
65254108
VD
101}
102
103static void dsa_tree_release(struct kref *ref)
104{
105 struct dsa_switch_tree *dst;
106
107 dst = container_of(ref, struct dsa_switch_tree, refcount);
108
109 dsa_tree_free(dst);
110}
111
112static void dsa_tree_put(struct dsa_switch_tree *dst)
113{
9e741045
VD
114 if (dst)
115 kref_put(&dst->refcount, dsa_tree_release);
65254108
VD
116}
117
293784a8 118static bool dsa_port_is_dsa(struct dsa_port *port)
83c0afae 119{
6d4e5c57 120 return port->type == DSA_PORT_TYPE_DSA;
293784a8
FF
121}
122
123static bool dsa_port_is_cpu(struct dsa_port *port)
124{
6d4e5c57 125 return port->type == DSA_PORT_TYPE_CPU;
83c0afae
AL
126}
127
f070464c
VD
128static bool dsa_port_is_user(struct dsa_port *dp)
129{
130 return dp->type == DSA_PORT_TYPE_USER;
131}
132
f163da88
VD
133static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
134 struct device_node *dn)
83c0afae 135{
f163da88 136 struct dsa_port *dp;
83c0afae 137
764b7e62
VD
138 list_for_each_entry(dp, &dst->ports, list)
139 if (dp->dn == dn)
140 return dp;
83c0afae
AL
141
142 return NULL;
143}
144
4e2ce6e5
BDC
145static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
146 struct dsa_port *link_dp)
c5f51765
VD
147{
148 struct dsa_switch *ds = dp->ds;
149 struct dsa_switch_tree *dst;
150 struct dsa_link *dl;
151
152 dst = ds->dst;
153
154 list_for_each_entry(dl, &dst->rtable, list)
155 if (dl->dp == dp && dl->link_dp == link_dp)
156 return dl;
157
158 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
159 if (!dl)
160 return NULL;
161
162 dl->dp = dp;
163 dl->link_dp = link_dp;
164
165 INIT_LIST_HEAD(&dl->list);
166 list_add_tail(&dl->list, &dst->rtable);
167
168 return dl;
169}
170
34c09a89 171static bool dsa_port_setup_routing_table(struct dsa_port *dp)
83c0afae 172{
34c09a89
VD
173 struct dsa_switch *ds = dp->ds;
174 struct dsa_switch_tree *dst = ds->dst;
175 struct device_node *dn = dp->dn;
c5286665 176 struct of_phandle_iterator it;
f163da88 177 struct dsa_port *link_dp;
c5f51765 178 struct dsa_link *dl;
c5286665 179 int err;
83c0afae 180
c5286665
VD
181 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
182 link_dp = dsa_tree_find_port_by_node(dst, it.node);
183 if (!link_dp) {
184 of_node_put(it.node);
34c09a89 185 return false;
c5286665 186 }
83c0afae 187
c5f51765
VD
188 dl = dsa_link_touch(dp, link_dp);
189 if (!dl) {
190 of_node_put(it.node);
191 return false;
192 }
83c0afae
AL
193 }
194
34c09a89 195 return true;
83c0afae
AL
196}
197
3774ecdb 198static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
83c0afae 199{
34c09a89
VD
200 bool complete = true;
201 struct dsa_port *dp;
83c0afae 202
86bfb2c1 203 list_for_each_entry(dp, &dst->ports, list) {
3774ecdb 204 if (dsa_port_is_dsa(dp)) {
34c09a89
VD
205 complete = dsa_port_setup_routing_table(dp);
206 if (!complete)
207 break;
208 }
83c0afae
AL
209 }
210
34c09a89 211 return complete;
83c0afae
AL
212}
213
f070464c
VD
214static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
215{
f070464c 216 struct dsa_port *dp;
f070464c 217
c0b73628
VD
218 list_for_each_entry(dp, &dst->ports, list)
219 if (dsa_port_is_cpu(dp))
220 return dp;
f070464c
VD
221
222 return NULL;
223}
224
225static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
226{
da4561cd 227 struct dsa_port *cpu_dp, *dp;
f070464c 228
da4561cd
VD
229 cpu_dp = dsa_tree_find_first_cpu(dst);
230 if (!cpu_dp) {
231 pr_err("DSA: tree %d has no CPU port\n", dst->index);
f070464c
VD
232 return -EINVAL;
233 }
234
235 /* Assign the default CPU port to all ports of the fabric */
da4561cd
VD
236 list_for_each_entry(dp, &dst->ports, list)
237 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
238 dp->cpu_dp = cpu_dp;
f070464c
VD
239
240 return 0;
241}
242
243static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
244{
da4561cd
VD
245 struct dsa_port *dp;
246
247 list_for_each_entry(dp, &dst->ports, list)
248 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
249 dp->cpu_dp = NULL;
f070464c
VD
250}
251
1d27732f 252static int dsa_port_setup(struct dsa_port *dp)
83c0afae 253{
955222ca 254 struct devlink_port *dlp = &dp->devlink_port;
4ba0ebbc 255 bool dsa_port_link_registered = false;
4ba0ebbc
VO
256 bool dsa_port_enabled = false;
257 int err = 0;
83c0afae 258
fb35c60c
VD
259 if (dp->setup)
260 return 0;
261
1d27732f
VD
262 switch (dp->type) {
263 case DSA_PORT_TYPE_UNUSED:
0394a63a 264 dsa_port_disable(dp);
1d27732f
VD
265 break;
266 case DSA_PORT_TYPE_CPU:
da077392 267 err = dsa_port_link_register_of(dp);
0394a63a 268 if (err)
4ba0ebbc
VO
269 break;
270 dsa_port_link_registered = true;
0394a63a
VD
271
272 err = dsa_port_enable(dp, NULL);
e70c7aad 273 if (err)
4ba0ebbc
VO
274 break;
275 dsa_port_enabled = true;
276
da077392 277 break;
1d27732f 278 case DSA_PORT_TYPE_DSA:
33615367 279 err = dsa_port_link_register_of(dp);
0394a63a 280 if (err)
4ba0ebbc
VO
281 break;
282 dsa_port_link_registered = true;
0394a63a
VD
283
284 err = dsa_port_enable(dp, NULL);
e70c7aad 285 if (err)
4ba0ebbc
VO
286 break;
287 dsa_port_enabled = true;
288
1d27732f
VD
289 break;
290 case DSA_PORT_TYPE_USER:
955222ca 291 dp->mac = of_get_mac_address(dp->dn);
1d27732f
VD
292 err = dsa_slave_create(dp);
293 if (err)
4ba0ebbc 294 break;
955222ca
VD
295
296 devlink_port_type_eth_set(dlp, dp->slave);
1d27732f 297 break;
83c0afae
AL
298 }
299
4ba0ebbc
VO
300 if (err && dsa_port_enabled)
301 dsa_port_disable(dp);
302 if (err && dsa_port_link_registered)
303 dsa_port_link_unregister_of(dp);
fb35c60c
VD
304 if (err)
305 return err;
4ba0ebbc 306
fb35c60c
VD
307 dp->setup = true;
308
309 return 0;
83c0afae
AL
310}
311
3122433e 312static int dsa_port_devlink_setup(struct dsa_port *dp)
83c0afae 313{
955222ca 314 struct devlink_port *dlp = &dp->devlink_port;
3122433e
AL
315 struct dsa_switch_tree *dst = dp->ds->dst;
316 struct devlink_port_attrs attrs = {};
317 struct devlink *dl = dp->ds->devlink;
318 const unsigned char *id;
319 unsigned char len;
320 int err;
321
322 id = (const unsigned char *)&dst->index;
323 len = sizeof(dst->index);
324
325 attrs.phys.port_number = dp->index;
326 memcpy(attrs.switch_id.id, id, len);
327 attrs.switch_id.id_len = len;
328 memset(dlp, 0, sizeof(*dlp));
329
330 switch (dp->type) {
331 case DSA_PORT_TYPE_UNUSED:
332 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
333 break;
334 case DSA_PORT_TYPE_CPU:
335 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
336 break;
337 case DSA_PORT_TYPE_DSA:
338 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
339 break;
340 case DSA_PORT_TYPE_USER:
341 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
342 break;
343 }
344
345 devlink_port_attrs_set(dlp, &attrs);
346 err = devlink_port_register(dl, dlp, dp->index);
347
348 if (!err)
349 dp->devlink_port_setup = true;
1d27732f 350
3122433e
AL
351 return err;
352}
353
354static void dsa_port_teardown(struct dsa_port *dp)
355{
91158e16
VO
356 struct devlink_port *dlp = &dp->devlink_port;
357
fb35c60c
VD
358 if (!dp->setup)
359 return;
360
91158e16
VO
361 devlink_port_type_clear(dlp);
362
1d27732f
VD
363 switch (dp->type) {
364 case DSA_PORT_TYPE_UNUSED:
365 break;
366 case DSA_PORT_TYPE_CPU:
0394a63a 367 dsa_port_disable(dp);
4dad81ee 368 dsa_tag_driver_put(dp->tag_ops);
955222ca
VD
369 dsa_port_link_unregister_of(dp);
370 break;
1d27732f 371 case DSA_PORT_TYPE_DSA:
0394a63a 372 dsa_port_disable(dp);
33615367 373 dsa_port_link_unregister_of(dp);
1d27732f
VD
374 break;
375 case DSA_PORT_TYPE_USER:
376 if (dp->slave) {
377 dsa_slave_destroy(dp->slave);
378 dp->slave = NULL;
379 }
380 break;
83c0afae 381 }
fb35c60c
VD
382
383 dp->setup = false;
83c0afae
AL
384}
385
3122433e
AL
386static void dsa_port_devlink_teardown(struct dsa_port *dp)
387{
388 struct devlink_port *dlp = &dp->devlink_port;
389
390 if (dp->devlink_port_setup)
391 devlink_port_unregister(dlp);
392 dp->devlink_port_setup = false;
393}
394
0f06b855
AL
395static int dsa_devlink_info_get(struct devlink *dl,
396 struct devlink_info_req *req,
397 struct netlink_ext_ack *extack)
398{
399 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
400
401 if (ds->ops->devlink_info_get)
402 return ds->ops->devlink_info_get(ds, req, extack);
403
404 return -EOPNOTSUPP;
405}
406
407static const struct devlink_ops dsa_devlink_ops = {
408 .info_get = dsa_devlink_info_get,
409};
410
1f08f9e9 411static int dsa_switch_setup(struct dsa_switch *ds)
83c0afae 412{
6b297524 413 struct dsa_devlink_priv *dl_priv;
3122433e 414 struct dsa_port *dp;
fb35c60c
VD
415 int err;
416
417 if (ds->setup)
418 return 0;
83c0afae 419
6e830d8f 420 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
9d490b4e 421 * driver and before ops->setup() has run, since the switch drivers and
6e830d8f
FF
422 * the slave MDIO bus driver rely on these values for probing PHY
423 * devices or not
424 */
02bc6e54 425 ds->phys_mii_mask |= dsa_user_ports(ds);
6e830d8f 426
96567d5d
AL
427 /* Add the switch to devlink before calling setup, so that setup can
428 * add dpipe tables
429 */
6b297524 430 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
96567d5d
AL
431 if (!ds->devlink)
432 return -ENOMEM;
6b297524
AL
433 dl_priv = devlink_priv(ds->devlink);
434 dl_priv->ds = ds;
96567d5d
AL
435
436 err = devlink_register(ds->devlink, ds->dev);
437 if (err)
e70c7aad 438 goto free_devlink;
96567d5d 439
3122433e
AL
440 /* Setup devlink port instances now, so that the switch
441 * setup() can register regions etc, against the ports
442 */
443 list_for_each_entry(dp, &ds->dst->ports, list) {
444 if (dp->ds == ds) {
445 err = dsa_port_devlink_setup(dp);
446 if (err)
447 goto unregister_devlink_ports;
448 }
449 }
450
f515f192
VD
451 err = dsa_switch_register_notifier(ds);
452 if (err)
3122433e 453 goto unregister_devlink_ports;
f515f192 454
b2243b36
VO
455 err = ds->ops->setup(ds);
456 if (err < 0)
e70c7aad 457 goto unregister_notifier;
b2243b36 458
6b297524
AL
459 devlink_params_publish(ds->devlink);
460
9d490b4e 461 if (!ds->slave_mii_bus && ds->ops->phy_read) {
1eb59443 462 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
e70c7aad
IC
463 if (!ds->slave_mii_bus) {
464 err = -ENOMEM;
8fd54a73 465 goto teardown;
e70c7aad 466 }
1eb59443
FF
467
468 dsa_slave_mii_bus_init(ds);
469
470 err = mdiobus_register(ds->slave_mii_bus);
471 if (err < 0)
8fd54a73 472 goto teardown;
1eb59443
FF
473 }
474
fb35c60c
VD
475 ds->setup = true;
476
83c0afae 477 return 0;
e70c7aad 478
8fd54a73
VO
479teardown:
480 if (ds->ops->teardown)
481 ds->ops->teardown(ds);
e70c7aad
IC
482unregister_notifier:
483 dsa_switch_unregister_notifier(ds);
3122433e
AL
484unregister_devlink_ports:
485 list_for_each_entry(dp, &ds->dst->ports, list)
486 if (dp->ds == ds)
487 dsa_port_devlink_teardown(dp);
e70c7aad
IC
488 devlink_unregister(ds->devlink);
489free_devlink:
490 devlink_free(ds->devlink);
491 ds->devlink = NULL;
492
493 return err;
83c0afae
AL
494}
495
1f08f9e9 496static void dsa_switch_teardown(struct dsa_switch *ds)
83c0afae 497{
3122433e
AL
498 struct dsa_port *dp;
499
fb35c60c
VD
500 if (!ds->setup)
501 return;
502
9d490b4e 503 if (ds->slave_mii_bus && ds->ops->phy_read)
1eb59443 504 mdiobus_unregister(ds->slave_mii_bus);
f515f192
VD
505
506 dsa_switch_unregister_notifier(ds);
96567d5d 507
5e3f847a
VO
508 if (ds->ops->teardown)
509 ds->ops->teardown(ds);
510
96567d5d 511 if (ds->devlink) {
3122433e
AL
512 list_for_each_entry(dp, &ds->dst->ports, list)
513 if (dp->ds == ds)
514 dsa_port_devlink_teardown(dp);
96567d5d
AL
515 devlink_unregister(ds->devlink);
516 devlink_free(ds->devlink);
517 ds->devlink = NULL;
518 }
519
fb35c60c 520 ds->setup = false;
83c0afae
AL
521}
522
1f08f9e9
VD
523static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
524{
1d27732f 525 struct dsa_port *dp;
fb35c60c 526 int err;
1f08f9e9 527
fb35c60c
VD
528 list_for_each_entry(dp, &dst->ports, list) {
529 err = dsa_switch_setup(dp->ds);
1f08f9e9 530 if (err)
fb35c60c
VD
531 goto teardown;
532 }
1d27732f 533
fb35c60c
VD
534 list_for_each_entry(dp, &dst->ports, list) {
535 err = dsa_port_setup(dp);
536 if (err)
86f8b1c0 537 continue;
1f08f9e9
VD
538 }
539
540 return 0;
e70c7aad 541
fb35c60c
VD
542teardown:
543 list_for_each_entry(dp, &dst->ports, list)
544 dsa_port_teardown(dp);
e70c7aad 545
fb35c60c
VD
546 list_for_each_entry(dp, &dst->ports, list)
547 dsa_switch_teardown(dp->ds);
e70c7aad
IC
548
549 return err;
1f08f9e9
VD
550}
551
552static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
553{
1d27732f 554 struct dsa_port *dp;
1d27732f 555
fb35c60c
VD
556 list_for_each_entry(dp, &dst->ports, list)
557 dsa_port_teardown(dp);
1d27732f 558
fb35c60c
VD
559 list_for_each_entry(dp, &dst->ports, list)
560 dsa_switch_teardown(dp->ds);
1f08f9e9
VD
561}
562
17a22fcf
VD
563static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
564{
0cfec588
VD
565 struct dsa_port *dp;
566 int err;
17a22fcf 567
0cfec588
VD
568 list_for_each_entry(dp, &dst->ports, list) {
569 if (dsa_port_is_cpu(dp)) {
570 err = dsa_master_setup(dp->master, dp);
571 if (err)
572 return err;
573 }
574 }
575
576 return 0;
17a22fcf
VD
577}
578
579static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
580{
0cfec588 581 struct dsa_port *dp;
17a22fcf 582
0cfec588
VD
583 list_for_each_entry(dp, &dst->ports, list)
584 if (dsa_port_is_cpu(dp))
585 dsa_master_teardown(dp->master);
17a22fcf
VD
586}
587
ec15dd42 588static int dsa_tree_setup(struct dsa_switch_tree *dst)
83c0afae 589{
34c09a89 590 bool complete;
83c0afae
AL
591 int err;
592
ec15dd42
VD
593 if (dst->setup) {
594 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
595 dst->index);
596 return -EEXIST;
597 }
598
34c09a89
VD
599 complete = dsa_tree_setup_routing_table(dst);
600 if (!complete)
601 return 0;
602
f070464c
VD
603 err = dsa_tree_setup_default_cpu(dst);
604 if (err)
605 return err;
606
1f08f9e9
VD
607 err = dsa_tree_setup_switches(dst);
608 if (err)
e70c7aad 609 goto teardown_default_cpu;
83c0afae 610
17a22fcf 611 err = dsa_tree_setup_master(dst);
1943563d 612 if (err)
e70c7aad 613 goto teardown_switches;
1943563d 614
ec15dd42
VD
615 dst->setup = true;
616
617 pr_info("DSA: tree %d setup\n", dst->index);
83c0afae
AL
618
619 return 0;
e70c7aad
IC
620
621teardown_switches:
622 dsa_tree_teardown_switches(dst);
623teardown_default_cpu:
624 dsa_tree_teardown_default_cpu(dst);
625
626 return err;
83c0afae
AL
627}
628
ec15dd42 629static void dsa_tree_teardown(struct dsa_switch_tree *dst)
83c0afae 630{
c5f51765
VD
631 struct dsa_link *dl, *next;
632
ec15dd42 633 if (!dst->setup)
83c0afae
AL
634 return;
635
17a22fcf 636 dsa_tree_teardown_master(dst);
83c0afae 637
1f08f9e9 638 dsa_tree_teardown_switches(dst);
83c0afae 639
f070464c 640 dsa_tree_teardown_default_cpu(dst);
0c73c523 641
c5f51765
VD
642 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
643 list_del(&dl->list);
644 kfree(dl);
645 }
646
ec15dd42
VD
647 pr_info("DSA: tree %d torn down\n", dst->index);
648
649 dst->setup = false;
83c0afae
AL
650}
651
ab8ccae1
VD
652static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
653{
654 struct dsa_switch_tree *dst = ds->dst;
655 struct dsa_port *dp;
656
05f294a8
VD
657 list_for_each_entry(dp, &dst->ports, list)
658 if (dp->ds == ds && dp->index == index)
659 return dp;
660
661 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
662 if (!dp)
663 return NULL;
ab8ccae1
VD
664
665 dp->ds = ds;
666 dp->index = index;
667
668 INIT_LIST_HEAD(&dp->list);
669 list_add_tail(&dp->list, &dst->ports);
670
671 return dp;
672}
673
06e24d08
VD
674static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
675{
676 if (!name)
677 name = "eth%d";
678
679 dp->type = DSA_PORT_TYPE_USER;
680 dp->name = name;
681
682 return 0;
683}
684
685static int dsa_port_parse_dsa(struct dsa_port *dp)
686{
687 dp->type = DSA_PORT_TYPE_DSA;
688
689 return 0;
690}
691
4d776482
FF
692static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
693 struct net_device *master)
694{
695 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
696 struct dsa_switch *mds, *ds = dp->ds;
697 unsigned int mdp_upstream;
698 struct dsa_port *mdp;
699
700 /* It is possible to stack DSA switches onto one another when that
701 * happens the switch driver may want to know if its tagging protocol
702 * is going to work in such a configuration.
703 */
704 if (dsa_slave_dev_check(master)) {
705 mdp = dsa_slave_to_port(master);
706 mds = mdp->ds;
707 mdp_upstream = dsa_upstream_port(mds, mdp->index);
708 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
709 DSA_TAG_PROTO_NONE);
710 }
711
712 /* If the master device is not itself a DSA slave in a disjoint DSA
713 * tree, then return immediately.
714 */
715 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
716}
717
06e24d08
VD
718static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
719{
7354fcb0
VD
720 struct dsa_switch *ds = dp->ds;
721 struct dsa_switch_tree *dst = ds->dst;
722 const struct dsa_device_ops *tag_ops;
723 enum dsa_tag_protocol tag_protocol;
724
4d776482 725 tag_protocol = dsa_get_tag_protocol(dp, master);
c39e2a1d 726 tag_ops = dsa_tag_driver_get(tag_protocol);
7354fcb0 727 if (IS_ERR(tag_ops)) {
23426a25
AL
728 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
729 return -EPROBE_DEFER;
7354fcb0 730 dev_warn(ds->dev, "No tagger for this switch\n");
4d776482 731 dp->master = NULL;
7354fcb0
VD
732 return PTR_ERR(tag_ops);
733 }
734
4d776482 735 dp->master = master;
06e24d08 736 dp->type = DSA_PORT_TYPE_CPU;
cc1939e4 737 dp->filter = tag_ops->filter;
7354fcb0
VD
738 dp->rcv = tag_ops->rcv;
739 dp->tag_ops = tag_ops;
7354fcb0 740 dp->dst = dst;
06e24d08
VD
741
742 return 0;
743}
744
fd223e2e
VD
745static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
746{
6d4e5c57 747 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1838fa89 748 const char *name = of_get_property(dn, "label", NULL);
54df6fa9 749 bool link = of_property_read_bool(dn, "link");
6d4e5c57 750
06e24d08
VD
751 dp->dn = dn;
752
6d4e5c57 753 if (ethernet) {
cbabb0ac
VD
754 struct net_device *master;
755
756 master = of_find_net_device_by_node(ethernet);
757 if (!master)
758 return -EPROBE_DEFER;
759
06e24d08 760 return dsa_port_parse_cpu(dp, master);
6d4e5c57
VD
761 }
762
06e24d08
VD
763 if (link)
764 return dsa_port_parse_dsa(dp);
fd223e2e 765
06e24d08 766 return dsa_port_parse_user(dp, name);
fd223e2e
VD
767}
768
975e6e32
VD
769static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
770 struct device_node *dn)
83c0afae 771{
5b32fe07 772 struct device_node *ports, *port;
fd223e2e 773 struct dsa_port *dp;
9919a363 774 int err = 0;
83c0afae 775 u32 reg;
5b32fe07
VD
776
777 ports = of_get_child_by_name(dn, "ports");
778 if (!ports) {
85e05d26
KK
779 /* The second possibility is "ethernet-ports" */
780 ports = of_get_child_by_name(dn, "ethernet-ports");
781 if (!ports) {
782 dev_err(ds->dev, "no ports child node found\n");
783 return -EINVAL;
784 }
5b32fe07 785 }
83c0afae
AL
786
787 for_each_available_child_of_node(ports, port) {
788 err = of_property_read_u32(port, "reg", &reg);
789 if (err)
9919a363 790 goto out_put_node;
83c0afae 791
9919a363
WY
792 if (reg >= ds->num_ports) {
793 err = -EINVAL;
794 goto out_put_node;
795 }
83c0afae 796
68bb8ea8 797 dp = dsa_to_port(ds, reg);
fd223e2e
VD
798
799 err = dsa_port_parse_of(dp, port);
800 if (err)
9919a363 801 goto out_put_node;
83c0afae
AL
802 }
803
9919a363
WY
804out_put_node:
805 of_node_put(ports);
806 return err;
83c0afae
AL
807}
808
975e6e32
VD
809static int dsa_switch_parse_member_of(struct dsa_switch *ds,
810 struct device_node *dn)
811{
812 u32 m[2] = { 0, 0 };
813 int sz;
814
815 /* Don't error out if this optional property isn't found */
816 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
817 if (sz < 0 && sz != -EINVAL)
818 return sz;
819
820 ds->index = m[1];
975e6e32
VD
821
822 ds->dst = dsa_tree_touch(m[0]);
823 if (!ds->dst)
824 return -ENOMEM;
825
826 return 0;
827}
828
ab8ccae1
VD
829static int dsa_switch_touch_ports(struct dsa_switch *ds)
830{
831 struct dsa_port *dp;
832 int port;
833
834 for (port = 0; port < ds->num_ports; port++) {
835 dp = dsa_port_touch(ds, port);
836 if (!dp)
837 return -ENOMEM;
838 }
839
840 return 0;
841}
842
975e6e32
VD
843static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
844{
845 int err;
846
847 err = dsa_switch_parse_member_of(ds, dn);
848 if (err)
849 return err;
850
ab8ccae1
VD
851 err = dsa_switch_touch_ports(ds);
852 if (err)
853 return err;
854
975e6e32
VD
855 return dsa_switch_parse_ports_of(ds, dn);
856}
857
fd223e2e
VD
858static int dsa_port_parse(struct dsa_port *dp, const char *name,
859 struct device *dev)
860{
6d4e5c57 861 if (!strcmp(name, "cpu")) {
cbabb0ac
VD
862 struct net_device *master;
863
864 master = dsa_dev_to_net_device(dev);
865 if (!master)
866 return -EPROBE_DEFER;
867
868 dev_put(master);
869
06e24d08 870 return dsa_port_parse_cpu(dp, master);
6d4e5c57
VD
871 }
872
06e24d08
VD
873 if (!strcmp(name, "dsa"))
874 return dsa_port_parse_dsa(dp);
fd223e2e 875
06e24d08 876 return dsa_port_parse_user(dp, name);
fd223e2e
VD
877}
878
975e6e32
VD
879static int dsa_switch_parse_ports(struct dsa_switch *ds,
880 struct dsa_chip_data *cd)
71e0bbde
FF
881{
882 bool valid_name_found = false;
fd223e2e
VD
883 struct dsa_port *dp;
884 struct device *dev;
885 const char *name;
71e0bbde 886 unsigned int i;
fd223e2e 887 int err;
71e0bbde
FF
888
889 for (i = 0; i < DSA_MAX_PORTS; i++) {
fd223e2e
VD
890 name = cd->port_names[i];
891 dev = cd->netdev[i];
68bb8ea8 892 dp = dsa_to_port(ds, i);
fd223e2e
VD
893
894 if (!name)
71e0bbde
FF
895 continue;
896
fd223e2e
VD
897 err = dsa_port_parse(dp, name, dev);
898 if (err)
899 return err;
900
71e0bbde
FF
901 valid_name_found = true;
902 }
903
904 if (!valid_name_found && i == DSA_MAX_PORTS)
905 return -EINVAL;
906
907 return 0;
908}
909
975e6e32 910static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
71e0bbde 911{
ab8ccae1
VD
912 int err;
913
975e6e32 914 ds->cd = cd;
71e0bbde 915
975e6e32
VD
916 /* We don't support interconnected switches nor multiple trees via
917 * platform data, so this is the unique switch of the tree.
918 */
919 ds->index = 0;
920 ds->dst = dsa_tree_touch(0);
921 if (!ds->dst)
922 return -ENOMEM;
71e0bbde 923
ab8ccae1
VD
924 err = dsa_switch_touch_ports(ds);
925 if (err)
926 return err;
927
975e6e32 928 return dsa_switch_parse_ports(ds, cd);
71e0bbde
FF
929}
930
6dc43cd3
VO
931static void dsa_switch_release_ports(struct dsa_switch *ds)
932{
933 struct dsa_switch_tree *dst = ds->dst;
934 struct dsa_port *dp, *next;
935
936 list_for_each_entry_safe(dp, next, &dst->ports, list) {
937 if (dp->ds != ds)
938 continue;
939 list_del(&dp->list);
940 kfree(dp);
941 }
942}
943
b4fbb347 944static int dsa_switch_probe(struct dsa_switch *ds)
83c0afae 945{
8e5cb84c 946 struct dsa_switch_tree *dst;
556f124f
CIK
947 struct dsa_chip_data *pdata;
948 struct device_node *np;
34c09a89 949 int err;
83c0afae 950
7e99e347
VD
951 if (!ds->dev)
952 return -ENODEV;
953
556f124f
CIK
954 pdata = ds->dev->platform_data;
955 np = ds->dev->of_node;
956
7e99e347
VD
957 if (!ds->num_ports)
958 return -EINVAL;
959
6dc43cd3 960 if (np) {
975e6e32 961 err = dsa_switch_parse_of(ds, np);
6dc43cd3
VO
962 if (err)
963 dsa_switch_release_ports(ds);
964 } else if (pdata) {
975e6e32 965 err = dsa_switch_parse(ds, pdata);
6dc43cd3
VO
966 if (err)
967 dsa_switch_release_ports(ds);
968 } else {
975e6e32 969 err = -ENODEV;
6dc43cd3 970 }
83c0afae 971
975e6e32
VD
972 if (err)
973 return err;
d390238c 974
8e5cb84c
VD
975 dst = ds->dst;
976 dsa_tree_get(dst);
977 err = dsa_tree_setup(dst);
6dc43cd3
VO
978 if (err) {
979 dsa_switch_release_ports(ds);
8e5cb84c 980 dsa_tree_put(dst);
6dc43cd3 981 }
8e5cb84c
VD
982
983 return err;
83c0afae
AL
984}
985
23c9ee49 986int dsa_register_switch(struct dsa_switch *ds)
83c0afae
AL
987{
988 int err;
989
990 mutex_lock(&dsa2_mutex);
b4fbb347 991 err = dsa_switch_probe(ds);
9e741045 992 dsa_tree_put(ds->dst);
83c0afae
AL
993 mutex_unlock(&dsa2_mutex);
994
995 return err;
996}
997EXPORT_SYMBOL_GPL(dsa_register_switch);
998
b4fbb347 999static void dsa_switch_remove(struct dsa_switch *ds)
83c0afae
AL
1000{
1001 struct dsa_switch_tree *dst = ds->dst;
05f294a8 1002
c058f6df 1003 dsa_tree_teardown(dst);
6dc43cd3 1004 dsa_switch_release_ports(ds);
8e5cb84c 1005 dsa_tree_put(dst);
83c0afae
AL
1006}
1007
1008void dsa_unregister_switch(struct dsa_switch *ds)
1009{
1010 mutex_lock(&dsa2_mutex);
b4fbb347 1011 dsa_switch_remove(ds);
83c0afae
AL
1012 mutex_unlock(&dsa2_mutex);
1013}
1014EXPORT_SYMBOL_GPL(dsa_unregister_switch);