blob: f543fca6dfcbfd718eca7fcc430b6691afc13a6f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/list.h>
12#include <linux/netdevice.h>
13#include <linux/slab.h>
14#include <linux/rtnetlink.h>
15#include <linux/of.h>
16#include <linux/of_net.h>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <net/devlink.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
19#include "dsa_priv.h"
20
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021static DEFINE_MUTEX(dsa2_mutex);
Olivier Deprez157378f2022-04-04 15:47:50 +020022LIST_HEAD(dsa_tree_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023
Olivier Deprez157378f2022-04-04 15:47:50 +020024struct 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044
45static struct dsa_switch_tree *dsa_tree_find(int index)
46{
47 struct dsa_switch_tree *dst;
48
49 list_for_each_entry(dst, &dsa_tree_list, list)
50 if (dst->index == index)
51 return dst;
52
53 return NULL;
54}
55
56static struct dsa_switch_tree *dsa_tree_alloc(int index)
57{
58 struct dsa_switch_tree *dst;
59
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61 if (!dst)
62 return NULL;
63
64 dst->index = index;
65
Olivier Deprez157378f2022-04-04 15:47:50 +020066 INIT_LIST_HEAD(&dst->rtable);
67
68 INIT_LIST_HEAD(&dst->ports);
69
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 INIT_LIST_HEAD(&dst->list);
David Brazdil0f672f62019-12-10 10:32:29 +000071 list_add_tail(&dst->list, &dsa_tree_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072
73 kref_init(&dst->refcount);
74
75 return dst;
76}
77
78static void dsa_tree_free(struct dsa_switch_tree *dst)
79{
80 list_del(&dst->list);
81 kfree(dst);
82}
83
84static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
85{
86 if (dst)
87 kref_get(&dst->refcount);
88
89 return dst;
90}
91
92static struct dsa_switch_tree *dsa_tree_touch(int index)
93{
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);
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{
114 if (dst)
115 kref_put(&dst->refcount, dsa_tree_release);
116}
117
118static bool dsa_port_is_dsa(struct dsa_port *port)
119{
120 return port->type == DSA_PORT_TYPE_DSA;
121}
122
123static bool dsa_port_is_cpu(struct dsa_port *port)
124{
125 return port->type == DSA_PORT_TYPE_CPU;
126}
127
128static bool dsa_port_is_user(struct dsa_port *dp)
129{
130 return dp->type == DSA_PORT_TYPE_USER;
131}
132
133static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
134 struct device_node *dn)
135{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137
Olivier Deprez157378f2022-04-04 15:47:50 +0200138 list_for_each_entry(dp, &dst->ports, list)
139 if (dp->dn == dn)
140 return dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141
142 return NULL;
143}
144
Olivier Deprez157378f2022-04-04 15:47:50 +0200145static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
146 struct dsa_port *link_dp)
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171static bool dsa_port_setup_routing_table(struct dsa_port *dp)
172{
173 struct dsa_switch *ds = dp->ds;
174 struct dsa_switch_tree *dst = ds->dst;
175 struct device_node *dn = dp->dn;
176 struct of_phandle_iterator it;
177 struct dsa_port *link_dp;
Olivier Deprez157378f2022-04-04 15:47:50 +0200178 struct dsa_link *dl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179 int err;
180
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);
185 return false;
186 }
187
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 dl = dsa_link_touch(dp, link_dp);
189 if (!dl) {
190 of_node_put(it.node);
191 return false;
192 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 }
194
195 return true;
196}
197
Olivier Deprez157378f2022-04-04 15:47:50 +0200198static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199{
200 bool complete = true;
201 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202
Olivier Deprez157378f2022-04-04 15:47:50 +0200203 list_for_each_entry(dp, &dst->ports, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204 if (dsa_port_is_dsa(dp)) {
205 complete = dsa_port_setup_routing_table(dp);
206 if (!complete)
207 break;
208 }
209 }
210
211 return complete;
212}
213
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
215{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217
Olivier Deprez157378f2022-04-04 15:47:50 +0200218 list_for_each_entry(dp, &dst->ports, list)
219 if (dsa_port_is_cpu(dp))
220 return dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221
222 return NULL;
223}
224
225static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
226{
Olivier Deprez157378f2022-04-04 15:47:50 +0200227 struct dsa_port *cpu_dp, *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228
Olivier Deprez157378f2022-04-04 15:47:50 +0200229 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 return -EINVAL;
233 }
234
235 /* Assign the default CPU port to all ports of the fabric */
Olivier Deprez157378f2022-04-04 15:47:50 +0200236 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239
240 return 0;
241}
242
243static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
244{
Olivier Deprez157378f2022-04-04 15:47:50 +0200245 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250}
251
252static int dsa_port_setup(struct dsa_port *dp)
253{
David Brazdil0f672f62019-12-10 10:32:29 +0000254 struct devlink_port *dlp = &dp->devlink_port;
255 bool dsa_port_link_registered = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000256 bool dsa_port_enabled = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 int err = 0;
258
Olivier Deprez157378f2022-04-04 15:47:50 +0200259 if (dp->setup)
260 return 0;
261
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 switch (dp->type) {
263 case DSA_PORT_TYPE_UNUSED:
David Brazdil0f672f62019-12-10 10:32:29 +0000264 dsa_port_disable(dp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265 break;
266 case DSA_PORT_TYPE_CPU:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 err = dsa_port_link_register_of(dp);
David Brazdil0f672f62019-12-10 10:32:29 +0000268 if (err)
269 break;
270 dsa_port_link_registered = true;
271
272 err = dsa_port_enable(dp, NULL);
273 if (err)
274 break;
275 dsa_port_enabled = true;
276
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277 break;
278 case DSA_PORT_TYPE_DSA:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 err = dsa_port_link_register_of(dp);
David Brazdil0f672f62019-12-10 10:32:29 +0000280 if (err)
281 break;
282 dsa_port_link_registered = true;
283
284 err = dsa_port_enable(dp, NULL);
285 if (err)
286 break;
287 dsa_port_enabled = true;
288
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289 break;
290 case DSA_PORT_TYPE_USER:
David Brazdil0f672f62019-12-10 10:32:29 +0000291 dp->mac = of_get_mac_address(dp->dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 err = dsa_slave_create(dp);
293 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000294 break;
295
296 devlink_port_type_eth_set(dlp, dp->slave);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 break;
298 }
299
David Brazdil0f672f62019-12-10 10:32:29 +0000300 if (err && dsa_port_enabled)
301 dsa_port_disable(dp);
302 if (err && dsa_port_link_registered)
303 dsa_port_link_unregister_of(dp);
Olivier Deprez157378f2022-04-04 15:47:50 +0200304 if (err)
305 return err;
306
307 dp->setup = true;
308
309 return 0;
310}
311
312static int dsa_port_devlink_setup(struct dsa_port *dp)
313{
314 struct devlink_port *dlp = &dp->devlink_port;
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;
David Brazdil0f672f62019-12-10 10:32:29 +0000350
351 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352}
353
354static void dsa_port_teardown(struct dsa_port *dp)
355{
David Brazdil0f672f62019-12-10 10:32:29 +0000356 struct devlink_port *dlp = &dp->devlink_port;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357
Olivier Deprez157378f2022-04-04 15:47:50 +0200358 if (!dp->setup)
359 return;
360
361 devlink_port_type_clear(dlp);
362
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000363 switch (dp->type) {
364 case DSA_PORT_TYPE_UNUSED:
365 break;
366 case DSA_PORT_TYPE_CPU:
David Brazdil0f672f62019-12-10 10:32:29 +0000367 dsa_port_disable(dp);
368 dsa_tag_driver_put(dp->tag_ops);
David Brazdil0f672f62019-12-10 10:32:29 +0000369 dsa_port_link_unregister_of(dp);
370 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371 case DSA_PORT_TYPE_DSA:
David Brazdil0f672f62019-12-10 10:32:29 +0000372 dsa_port_disable(dp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000373 dsa_port_link_unregister_of(dp);
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;
381 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200382
383 dp->setup = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384}
385
Olivier Deprez157378f2022-04-04 15:47:50 +0200386static 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
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411static int dsa_switch_setup(struct dsa_switch *ds)
412{
Olivier Deprez157378f2022-04-04 15:47:50 +0200413 struct dsa_devlink_priv *dl_priv;
414 struct dsa_port *dp;
415 int err;
416
417 if (ds->setup)
418 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419
420 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
421 * driver and before ops->setup() has run, since the switch drivers and
422 * the slave MDIO bus driver rely on these values for probing PHY
423 * devices or not
424 */
425 ds->phys_mii_mask |= dsa_user_ports(ds);
426
427 /* Add the switch to devlink before calling setup, so that setup can
428 * add dpipe tables
429 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200430 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431 if (!ds->devlink)
432 return -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +0200433 dl_priv = devlink_priv(ds->devlink);
434 dl_priv->ds = ds;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435
436 err = devlink_register(ds->devlink, ds->dev);
437 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000438 goto free_devlink;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439
Olivier Deprez157378f2022-04-04 15:47:50 +0200440 /* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451 err = dsa_switch_register_notifier(ds);
452 if (err)
Olivier Deprez157378f2022-04-04 15:47:50 +0200453 goto unregister_devlink_ports;
David Brazdil0f672f62019-12-10 10:32:29 +0000454
455 err = ds->ops->setup(ds);
456 if (err < 0)
457 goto unregister_notifier;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000458
Olivier Deprez157378f2022-04-04 15:47:50 +0200459 devlink_params_publish(ds->devlink);
460
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200462 ds->slave_mii_bus = mdiobus_alloc();
David Brazdil0f672f62019-12-10 10:32:29 +0000463 if (!ds->slave_mii_bus) {
464 err = -ENOMEM;
Olivier Deprez0e641232021-09-23 10:07:05 +0200465 goto teardown;
David Brazdil0f672f62019-12-10 10:32:29 +0000466 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467
468 dsa_slave_mii_bus_init(ds);
469
470 err = mdiobus_register(ds->slave_mii_bus);
471 if (err < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +0200472 goto free_slave_mii_bus;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 }
474
Olivier Deprez157378f2022-04-04 15:47:50 +0200475 ds->setup = true;
476
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000478
Olivier Deprez157378f2022-04-04 15:47:50 +0200479free_slave_mii_bus:
480 if (ds->slave_mii_bus && ds->ops->phy_read)
481 mdiobus_free(ds->slave_mii_bus);
Olivier Deprez0e641232021-09-23 10:07:05 +0200482teardown:
483 if (ds->ops->teardown)
484 ds->ops->teardown(ds);
David Brazdil0f672f62019-12-10 10:32:29 +0000485unregister_notifier:
486 dsa_switch_unregister_notifier(ds);
Olivier Deprez157378f2022-04-04 15:47:50 +0200487unregister_devlink_ports:
488 list_for_each_entry(dp, &ds->dst->ports, list)
489 if (dp->ds == ds)
490 dsa_port_devlink_teardown(dp);
David Brazdil0f672f62019-12-10 10:32:29 +0000491 devlink_unregister(ds->devlink);
492free_devlink:
493 devlink_free(ds->devlink);
494 ds->devlink = NULL;
495
496 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497}
498
499static void dsa_switch_teardown(struct dsa_switch *ds)
500{
Olivier Deprez157378f2022-04-04 15:47:50 +0200501 struct dsa_port *dp;
502
503 if (!ds->setup)
504 return;
505
506 if (ds->slave_mii_bus && ds->ops->phy_read) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000507 mdiobus_unregister(ds->slave_mii_bus);
Olivier Deprez157378f2022-04-04 15:47:50 +0200508 mdiobus_free(ds->slave_mii_bus);
509 ds->slave_mii_bus = NULL;
510 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511
512 dsa_switch_unregister_notifier(ds);
513
David Brazdil0f672f62019-12-10 10:32:29 +0000514 if (ds->ops->teardown)
515 ds->ops->teardown(ds);
516
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 if (ds->devlink) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200518 list_for_each_entry(dp, &ds->dst->ports, list)
519 if (dp->ds == ds)
520 dsa_port_devlink_teardown(dp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521 devlink_unregister(ds->devlink);
522 devlink_free(ds->devlink);
523 ds->devlink = NULL;
524 }
525
Olivier Deprez157378f2022-04-04 15:47:50 +0200526 ds->setup = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000527}
528
529static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
530{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000531 struct dsa_port *dp;
Olivier Deprez157378f2022-04-04 15:47:50 +0200532 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533
Olivier Deprez157378f2022-04-04 15:47:50 +0200534 list_for_each_entry(dp, &dst->ports, list) {
535 err = dsa_switch_setup(dp->ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536 if (err)
Olivier Deprez157378f2022-04-04 15:47:50 +0200537 goto teardown;
538 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000539
Olivier Deprez157378f2022-04-04 15:47:50 +0200540 list_for_each_entry(dp, &dst->ports, list) {
541 err = dsa_port_setup(dp);
542 if (err) {
543 dsa_port_devlink_teardown(dp);
544 dp->type = DSA_PORT_TYPE_UNUSED;
545 err = dsa_port_devlink_setup(dp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000546 if (err)
Olivier Deprez157378f2022-04-04 15:47:50 +0200547 goto teardown;
548 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549 }
550 }
551
552 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000553
Olivier Deprez157378f2022-04-04 15:47:50 +0200554teardown:
555 list_for_each_entry(dp, &dst->ports, list)
556 dsa_port_teardown(dp);
David Brazdil0f672f62019-12-10 10:32:29 +0000557
Olivier Deprez157378f2022-04-04 15:47:50 +0200558 list_for_each_entry(dp, &dst->ports, list)
559 dsa_switch_teardown(dp->ds);
David Brazdil0f672f62019-12-10 10:32:29 +0000560
561 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562}
563
564static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
565{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567
Olivier Deprez157378f2022-04-04 15:47:50 +0200568 list_for_each_entry(dp, &dst->ports, list)
569 dsa_port_teardown(dp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570
Olivier Deprez157378f2022-04-04 15:47:50 +0200571 list_for_each_entry(dp, &dst->ports, list)
572 dsa_switch_teardown(dp->ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000573}
574
575static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
576{
Olivier Deprez157378f2022-04-04 15:47:50 +0200577 struct dsa_port *dp;
578 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579
Olivier Deprez157378f2022-04-04 15:47:50 +0200580 list_for_each_entry(dp, &dst->ports, list) {
581 if (dsa_port_is_cpu(dp)) {
582 err = dsa_master_setup(dp->master, dp);
583 if (err)
584 return err;
585 }
586 }
587
588 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589}
590
591static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
592{
Olivier Deprez157378f2022-04-04 15:47:50 +0200593 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594
Olivier Deprez157378f2022-04-04 15:47:50 +0200595 list_for_each_entry(dp, &dst->ports, list)
596 if (dsa_port_is_cpu(dp))
597 dsa_master_teardown(dp->master);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598}
599
600static int dsa_tree_setup(struct dsa_switch_tree *dst)
601{
602 bool complete;
603 int err;
604
605 if (dst->setup) {
606 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
607 dst->index);
608 return -EEXIST;
609 }
610
611 complete = dsa_tree_setup_routing_table(dst);
612 if (!complete)
613 return 0;
614
615 err = dsa_tree_setup_default_cpu(dst);
616 if (err)
617 return err;
618
619 err = dsa_tree_setup_switches(dst);
620 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000621 goto teardown_default_cpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622
623 err = dsa_tree_setup_master(dst);
624 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000625 goto teardown_switches;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626
627 dst->setup = true;
628
629 pr_info("DSA: tree %d setup\n", dst->index);
630
631 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000632
633teardown_switches:
634 dsa_tree_teardown_switches(dst);
635teardown_default_cpu:
636 dsa_tree_teardown_default_cpu(dst);
637
638 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639}
640
641static void dsa_tree_teardown(struct dsa_switch_tree *dst)
642{
Olivier Deprez157378f2022-04-04 15:47:50 +0200643 struct dsa_link *dl, *next;
644
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 if (!dst->setup)
646 return;
647
648 dsa_tree_teardown_master(dst);
649
650 dsa_tree_teardown_switches(dst);
651
652 dsa_tree_teardown_default_cpu(dst);
653
Olivier Deprez157378f2022-04-04 15:47:50 +0200654 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
655 list_del(&dl->list);
656 kfree(dl);
657 }
658
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000659 pr_info("DSA: tree %d torn down\n", dst->index);
660
661 dst->setup = false;
662}
663
Olivier Deprez157378f2022-04-04 15:47:50 +0200664static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000665{
Olivier Deprez157378f2022-04-04 15:47:50 +0200666 struct dsa_switch_tree *dst = ds->dst;
667 struct dsa_port *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
Olivier Deprez157378f2022-04-04 15:47:50 +0200669 list_for_each_entry(dp, &dst->ports, list)
670 if (dp->ds == ds && dp->index == index)
671 return dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
674 if (!dp)
675 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000676
Olivier Deprez157378f2022-04-04 15:47:50 +0200677 dp->ds = ds;
678 dp->index = index;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679
Olivier Deprez157378f2022-04-04 15:47:50 +0200680 INIT_LIST_HEAD(&dp->list);
681 list_add_tail(&dp->list, &dst->ports);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682
Olivier Deprez157378f2022-04-04 15:47:50 +0200683 return dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684}
685
686static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
687{
688 if (!name)
689 name = "eth%d";
690
691 dp->type = DSA_PORT_TYPE_USER;
692 dp->name = name;
693
694 return 0;
695}
696
697static int dsa_port_parse_dsa(struct dsa_port *dp)
698{
699 dp->type = DSA_PORT_TYPE_DSA;
700
701 return 0;
702}
703
Olivier Deprez157378f2022-04-04 15:47:50 +0200704static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
705 struct net_device *master)
706{
707 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
708 struct dsa_switch *mds, *ds = dp->ds;
709 unsigned int mdp_upstream;
710 struct dsa_port *mdp;
711
712 /* It is possible to stack DSA switches onto one another when that
713 * happens the switch driver may want to know if its tagging protocol
714 * is going to work in such a configuration.
715 */
716 if (dsa_slave_dev_check(master)) {
717 mdp = dsa_slave_to_port(master);
718 mds = mdp->ds;
719 mdp_upstream = dsa_upstream_port(mds, mdp->index);
720 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
721 DSA_TAG_PROTO_NONE);
722 }
723
724 /* If the master device is not itself a DSA slave in a disjoint DSA
725 * tree, then return immediately.
726 */
727 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
728}
729
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000730static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
731{
732 struct dsa_switch *ds = dp->ds;
733 struct dsa_switch_tree *dst = ds->dst;
734 const struct dsa_device_ops *tag_ops;
735 enum dsa_tag_protocol tag_protocol;
736
Olivier Deprez157378f2022-04-04 15:47:50 +0200737 tag_protocol = dsa_get_tag_protocol(dp, master);
David Brazdil0f672f62019-12-10 10:32:29 +0000738 tag_ops = dsa_tag_driver_get(tag_protocol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 if (IS_ERR(tag_ops)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000740 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
741 return -EPROBE_DEFER;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742 dev_warn(ds->dev, "No tagger for this switch\n");
Olivier Deprez157378f2022-04-04 15:47:50 +0200743 dp->master = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000744 return PTR_ERR(tag_ops);
745 }
746
Olivier Deprez157378f2022-04-04 15:47:50 +0200747 dp->master = master;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000748 dp->type = DSA_PORT_TYPE_CPU;
David Brazdil0f672f62019-12-10 10:32:29 +0000749 dp->filter = tag_ops->filter;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 dp->rcv = tag_ops->rcv;
751 dp->tag_ops = tag_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000752 dp->dst = dst;
753
754 return 0;
755}
756
757static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
758{
759 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
760 const char *name = of_get_property(dn, "label", NULL);
761 bool link = of_property_read_bool(dn, "link");
762
763 dp->dn = dn;
764
765 if (ethernet) {
766 struct net_device *master;
767
768 master = of_find_net_device_by_node(ethernet);
Olivier Deprez157378f2022-04-04 15:47:50 +0200769 of_node_put(ethernet);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770 if (!master)
771 return -EPROBE_DEFER;
772
773 return dsa_port_parse_cpu(dp, master);
774 }
775
776 if (link)
777 return dsa_port_parse_dsa(dp);
778
779 return dsa_port_parse_user(dp, name);
780}
781
782static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
783 struct device_node *dn)
784{
785 struct device_node *ports, *port;
786 struct dsa_port *dp;
David Brazdil0f672f62019-12-10 10:32:29 +0000787 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000788 u32 reg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789
790 ports = of_get_child_by_name(dn, "ports");
791 if (!ports) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200792 /* The second possibility is "ethernet-ports" */
793 ports = of_get_child_by_name(dn, "ethernet-ports");
794 if (!ports) {
795 dev_err(ds->dev, "no ports child node found\n");
796 return -EINVAL;
797 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798 }
799
800 for_each_available_child_of_node(ports, port) {
801 err = of_property_read_u32(port, "reg", &reg);
802 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000803 goto out_put_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804
David Brazdil0f672f62019-12-10 10:32:29 +0000805 if (reg >= ds->num_ports) {
806 err = -EINVAL;
807 goto out_put_node;
808 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809
Olivier Deprez157378f2022-04-04 15:47:50 +0200810 dp = dsa_to_port(ds, reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000811
812 err = dsa_port_parse_of(dp, port);
813 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +0000814 goto out_put_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000815 }
816
David Brazdil0f672f62019-12-10 10:32:29 +0000817out_put_node:
818 of_node_put(ports);
819 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820}
821
822static int dsa_switch_parse_member_of(struct dsa_switch *ds,
823 struct device_node *dn)
824{
825 u32 m[2] = { 0, 0 };
826 int sz;
827
828 /* Don't error out if this optional property isn't found */
829 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
830 if (sz < 0 && sz != -EINVAL)
831 return sz;
832
833 ds->index = m[1];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834
835 ds->dst = dsa_tree_touch(m[0]);
836 if (!ds->dst)
837 return -ENOMEM;
838
839 return 0;
840}
841
Olivier Deprez157378f2022-04-04 15:47:50 +0200842static int dsa_switch_touch_ports(struct dsa_switch *ds)
843{
844 struct dsa_port *dp;
845 int port;
846
847 for (port = 0; port < ds->num_ports; port++) {
848 dp = dsa_port_touch(ds, port);
849 if (!dp)
850 return -ENOMEM;
851 }
852
853 return 0;
854}
855
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
857{
858 int err;
859
860 err = dsa_switch_parse_member_of(ds, dn);
861 if (err)
862 return err;
863
Olivier Deprez157378f2022-04-04 15:47:50 +0200864 err = dsa_switch_touch_ports(ds);
865 if (err)
866 return err;
867
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868 return dsa_switch_parse_ports_of(ds, dn);
869}
870
871static int dsa_port_parse(struct dsa_port *dp, const char *name,
872 struct device *dev)
873{
874 if (!strcmp(name, "cpu")) {
875 struct net_device *master;
876
877 master = dsa_dev_to_net_device(dev);
878 if (!master)
879 return -EPROBE_DEFER;
880
881 dev_put(master);
882
883 return dsa_port_parse_cpu(dp, master);
884 }
885
886 if (!strcmp(name, "dsa"))
887 return dsa_port_parse_dsa(dp);
888
889 return dsa_port_parse_user(dp, name);
890}
891
892static int dsa_switch_parse_ports(struct dsa_switch *ds,
893 struct dsa_chip_data *cd)
894{
895 bool valid_name_found = false;
896 struct dsa_port *dp;
897 struct device *dev;
898 const char *name;
899 unsigned int i;
900 int err;
901
902 for (i = 0; i < DSA_MAX_PORTS; i++) {
903 name = cd->port_names[i];
904 dev = cd->netdev[i];
Olivier Deprez157378f2022-04-04 15:47:50 +0200905 dp = dsa_to_port(ds, i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906
907 if (!name)
908 continue;
909
910 err = dsa_port_parse(dp, name, dev);
911 if (err)
912 return err;
913
914 valid_name_found = true;
915 }
916
917 if (!valid_name_found && i == DSA_MAX_PORTS)
918 return -EINVAL;
919
920 return 0;
921}
922
923static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
924{
Olivier Deprez157378f2022-04-04 15:47:50 +0200925 int err;
926
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 ds->cd = cd;
928
929 /* We don't support interconnected switches nor multiple trees via
930 * platform data, so this is the unique switch of the tree.
931 */
932 ds->index = 0;
933 ds->dst = dsa_tree_touch(0);
934 if (!ds->dst)
935 return -ENOMEM;
936
Olivier Deprez157378f2022-04-04 15:47:50 +0200937 err = dsa_switch_touch_ports(ds);
938 if (err)
939 return err;
940
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941 return dsa_switch_parse_ports(ds, cd);
942}
943
Olivier Deprez157378f2022-04-04 15:47:50 +0200944static void dsa_switch_release_ports(struct dsa_switch *ds)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945{
946 struct dsa_switch_tree *dst = ds->dst;
Olivier Deprez157378f2022-04-04 15:47:50 +0200947 struct dsa_port *dp, *next;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948
Olivier Deprez157378f2022-04-04 15:47:50 +0200949 list_for_each_entry_safe(dp, next, &dst->ports, list) {
950 if (dp->ds != ds)
951 continue;
952 list_del(&dp->list);
953 kfree(dp);
954 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955}
956
957static int dsa_switch_probe(struct dsa_switch *ds)
958{
Olivier Deprez157378f2022-04-04 15:47:50 +0200959 struct dsa_switch_tree *dst;
960 struct dsa_chip_data *pdata;
961 struct device_node *np;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962 int err;
963
Olivier Deprez157378f2022-04-04 15:47:50 +0200964 if (!ds->dev)
965 return -ENODEV;
966
967 pdata = ds->dev->platform_data;
968 np = ds->dev->of_node;
969
970 if (!ds->num_ports)
971 return -EINVAL;
972
973 if (np) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000974 err = dsa_switch_parse_of(ds, np);
Olivier Deprez157378f2022-04-04 15:47:50 +0200975 if (err)
976 dsa_switch_release_ports(ds);
977 } else if (pdata) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000978 err = dsa_switch_parse(ds, pdata);
Olivier Deprez157378f2022-04-04 15:47:50 +0200979 if (err)
980 dsa_switch_release_ports(ds);
981 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982 err = -ENODEV;
Olivier Deprez157378f2022-04-04 15:47:50 +0200983 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984
985 if (err)
986 return err;
987
Olivier Deprez157378f2022-04-04 15:47:50 +0200988 dst = ds->dst;
989 dsa_tree_get(dst);
990 err = dsa_tree_setup(dst);
991 if (err) {
992 dsa_switch_release_ports(ds);
993 dsa_tree_put(dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994 }
995
Olivier Deprez157378f2022-04-04 15:47:50 +0200996 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000997}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998
999int dsa_register_switch(struct dsa_switch *ds)
1000{
1001 int err;
1002
1003 mutex_lock(&dsa2_mutex);
1004 err = dsa_switch_probe(ds);
1005 dsa_tree_put(ds->dst);
1006 mutex_unlock(&dsa2_mutex);
1007
1008 return err;
1009}
1010EXPORT_SYMBOL_GPL(dsa_register_switch);
1011
1012static void dsa_switch_remove(struct dsa_switch *ds)
1013{
1014 struct dsa_switch_tree *dst = ds->dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001015
Olivier Deprez157378f2022-04-04 15:47:50 +02001016 dsa_tree_teardown(dst);
1017 dsa_switch_release_ports(ds);
1018 dsa_tree_put(dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019}
1020
1021void dsa_unregister_switch(struct dsa_switch *ds)
1022{
1023 mutex_lock(&dsa2_mutex);
1024 dsa_switch_remove(ds);
1025 mutex_unlock(&dsa2_mutex);
1026}
1027EXPORT_SYMBOL_GPL(dsa_unregister_switch);