blob: 820dd8da57fcab73bebf8614370269d994ba95a5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Lunn83c0afa2016-06-04 21:17:07 +02002/*
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 Lunn83c0afa2016-06-04 21:17:07 +02007 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/list.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020012#include <linux/netdevice.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020013#include <linux/slab.h>
14#include <linux/rtnetlink.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020015#include <linux/of.h>
16#include <linux/of_net.h>
Jiri Pirko402f99e52019-03-24 11:14:26 +010017#include <net/devlink.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040018
Andrew Lunn83c0afa2016-06-04 21:17:07 +020019#include "dsa_priv.h"
20
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040021static LIST_HEAD(dsa_tree_list);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020022static DEFINE_MUTEX(dsa2_mutex);
23
Andrew Lunn96567d52017-03-28 23:45:07 +020024static const struct devlink_ops dsa_devlink_ops = {
25};
26
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040027static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020028{
29 struct dsa_switch_tree *dst;
30
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040031 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -040032 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020033 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -040034
Andrew Lunn83c0afa2016-06-04 21:17:07 +020035 return NULL;
36}
37
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040038static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020039{
40 struct dsa_switch_tree *dst;
41
42 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
43 if (!dst)
44 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040045
Vivien Didelot49463b72017-11-03 19:05:21 -040046 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040047
Andrew Lunn83c0afa2016-06-04 21:17:07 +020048 INIT_LIST_HEAD(&dst->list);
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040049 list_add_tail(&dsa_tree_list, &dst->list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -040050
Andrew Lunn83c0afa2016-06-04 21:17:07 +020051 kref_init(&dst->refcount);
52
53 return dst;
54}
55
Vivien Didelot65254102017-11-03 19:05:23 -040056static void dsa_tree_free(struct dsa_switch_tree *dst)
57{
58 list_del(&dst->list);
59 kfree(dst);
60}
61
Vivien Didelot9e741042017-11-24 11:36:06 -050062static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
63{
64 if (dst)
65 kref_get(&dst->refcount);
66
67 return dst;
68}
69
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040070static struct dsa_switch_tree *dsa_tree_touch(int index)
71{
72 struct dsa_switch_tree *dst;
73
74 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -050075 if (dst)
76 return dsa_tree_get(dst);
77 else
78 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -040079}
80
81static void dsa_tree_release(struct kref *ref)
82{
83 struct dsa_switch_tree *dst;
84
85 dst = container_of(ref, struct dsa_switch_tree, refcount);
86
87 dsa_tree_free(dst);
88}
89
90static void dsa_tree_put(struct dsa_switch_tree *dst)
91{
Vivien Didelot9e741042017-11-24 11:36:06 -050092 if (dst)
93 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -040094}
95
Florian Fainelli293784a2017-01-26 10:45:52 -080096static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020097{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -040098 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -080099}
100
101static bool dsa_port_is_cpu(struct dsa_port *port)
102{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400103 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200104}
105
Vivien Didelotf0704642017-11-06 16:11:44 -0500106static bool dsa_port_is_user(struct dsa_port *dp)
107{
108 return dp->type == DSA_PORT_TYPE_USER;
109}
110
Vivien Didelotf163da82017-11-06 16:11:49 -0500111static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
112 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200113{
114 struct dsa_switch *ds;
Vivien Didelotf163da82017-11-06 16:11:49 -0500115 struct dsa_port *dp;
116 int device, port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200117
Vivien Didelotf163da82017-11-06 16:11:49 -0500118 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
119 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200120 if (!ds)
121 continue;
122
Vivien Didelotf163da82017-11-06 16:11:49 -0500123 for (port = 0; port < ds->num_ports; port++) {
124 dp = &ds->ports[port];
125
126 if (dp->dn == dn)
127 return dp;
128 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200129 }
130
131 return NULL;
132}
133
Vivien Didelot34c09a82017-11-06 16:11:51 -0500134static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200135{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500136 struct dsa_switch *ds = dp->ds;
137 struct dsa_switch_tree *dst = ds->dst;
138 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500139 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500140 struct dsa_port *link_dp;
Vivien Didelotc5286662017-11-06 16:11:50 -0500141 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200142
Vivien Didelotc5286662017-11-06 16:11:50 -0500143 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
144 link_dp = dsa_tree_find_port_by_node(dst, it.node);
145 if (!link_dp) {
146 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500147 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500148 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200149
Vivien Didelot34c09a82017-11-06 16:11:51 -0500150 ds->rtable[link_dp->ds->index] = dp->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200151 }
152
Vivien Didelot34c09a82017-11-06 16:11:51 -0500153 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200154}
155
Vivien Didelot34c09a82017-11-06 16:11:51 -0500156static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200157{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500158 bool complete = true;
159 struct dsa_port *dp;
160 int i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200161
Vivien Didelot34c09a82017-11-06 16:11:51 -0500162 for (i = 0; i < DSA_MAX_SWITCHES; i++)
163 ds->rtable[i] = DSA_RTABLE_NONE;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200164
Vivien Didelot34c09a82017-11-06 16:11:51 -0500165 for (i = 0; i < ds->num_ports; i++) {
166 dp = &ds->ports[i];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200167
Vivien Didelot34c09a82017-11-06 16:11:51 -0500168 if (dsa_port_is_dsa(dp)) {
169 complete = dsa_port_setup_routing_table(dp);
170 if (!complete)
171 break;
172 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200173 }
174
Vivien Didelot34c09a82017-11-06 16:11:51 -0500175 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200176}
177
Vivien Didelot34c09a82017-11-06 16:11:51 -0500178static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200179{
180 struct dsa_switch *ds;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500181 bool complete = true;
182 int device;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200183
Vivien Didelot34c09a82017-11-06 16:11:51 -0500184 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
185 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200186 if (!ds)
187 continue;
188
Vivien Didelot34c09a82017-11-06 16:11:51 -0500189 complete = dsa_switch_setup_routing_table(ds);
190 if (!complete)
191 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200192 }
193
Vivien Didelot34c09a82017-11-06 16:11:51 -0500194 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200195}
196
Vivien Didelotf0704642017-11-06 16:11:44 -0500197static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
198{
199 struct dsa_switch *ds;
200 struct dsa_port *dp;
201 int device, port;
202
203 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
204 ds = dst->ds[device];
205 if (!ds)
206 continue;
207
208 for (port = 0; port < ds->num_ports; port++) {
209 dp = &ds->ports[port];
210
211 if (dsa_port_is_cpu(dp))
212 return dp;
213 }
214 }
215
216 return NULL;
217}
218
219static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
220{
221 struct dsa_switch *ds;
222 struct dsa_port *dp;
223 int device, port;
224
225 /* DSA currently only supports a single CPU port */
226 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
227 if (!dst->cpu_dp) {
228 pr_warn("Tree has no master device\n");
229 return -EINVAL;
230 }
231
232 /* Assign the default CPU port to all ports of the fabric */
233 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
234 ds = dst->ds[device];
235 if (!ds)
236 continue;
237
238 for (port = 0; port < ds->num_ports; port++) {
239 dp = &ds->ports[port];
240
Vivien Didelot986d7cc2017-12-05 15:34:12 -0500241 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
Vivien Didelotf0704642017-11-06 16:11:44 -0500242 dp->cpu_dp = dst->cpu_dp;
243 }
244 }
245
246 return 0;
247}
248
249static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
250{
251 /* DSA currently only supports a single CPU port */
252 dst->cpu_dp = NULL;
253}
254
Vivien Didelot1d277322017-11-06 16:11:48 -0500255static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200256{
Jiri Pirkod8ba3622019-03-24 11:14:32 +0100257 enum devlink_port_flavour flavour;
Vivien Didelot1d277322017-11-06 16:11:48 -0500258 struct dsa_switch *ds = dp->ds;
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200259 struct dsa_switch_tree *dst = ds->dst;
Jiri Pirkod8ba3622019-03-24 11:14:32 +0100260 int err;
261
262 if (dp->type == DSA_PORT_TYPE_UNUSED)
263 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200264
Vivien Didelot1d277322017-11-06 16:11:48 -0500265 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
Xiaofei Shena2c70232019-03-29 11:04:58 +0530266 dp->mac = of_get_mac_address(dp->dn);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200267
Jiri Pirkod8ba3622019-03-24 11:14:32 +0100268 switch (dp->type) {
269 case DSA_PORT_TYPE_CPU:
270 flavour = DEVLINK_PORT_FLAVOUR_CPU;
271 break;
272 case DSA_PORT_TYPE_DSA:
273 flavour = DEVLINK_PORT_FLAVOUR_DSA;
274 break;
275 case DSA_PORT_TYPE_USER: /* fall-through */
276 default:
277 flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
278 break;
279 }
280
281 /* dp->index is used now as port_number. However
282 * CPU and DSA ports should have separate numbering
283 * independent from front panel port numbers.
284 */
285 devlink_port_attrs_set(&dp->devlink_port, flavour,
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200286 dp->index, false, 0,
287 (const char *) &dst->index, sizeof(dst->index));
Jiri Pirkod8ba3622019-03-24 11:14:32 +0100288 err = devlink_port_register(ds->devlink, &dp->devlink_port,
289 dp->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200290 if (err)
291 return err;
292
Vivien Didelot1d277322017-11-06 16:11:48 -0500293 switch (dp->type) {
294 case DSA_PORT_TYPE_UNUSED:
295 break;
296 case DSA_PORT_TYPE_CPU:
Jiri Pirkoda077392018-05-18 09:29:03 +0200297 err = dsa_port_link_register_of(dp);
298 if (err) {
299 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
300 ds->index, dp->index);
301 return err;
302 }
303 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500304 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100305 err = dsa_port_link_register_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500306 if (err) {
Sebastian Reichel33615362018-01-23 16:03:46 +0100307 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
Vivien Didelot1d277322017-11-06 16:11:48 -0500308 ds->index, dp->index);
309 return err;
310 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500311 break;
312 case DSA_PORT_TYPE_USER:
313 err = dsa_slave_create(dp);
314 if (err)
315 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
316 ds->index, dp->index);
317 else
318 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
319 break;
320 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200321
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200322 return 0;
323}
324
Vivien Didelot1d277322017-11-06 16:11:48 -0500325static void dsa_port_teardown(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200326{
Florian Fainelli5447d782018-05-17 16:55:39 -0700327 if (dp->type != DSA_PORT_TYPE_UNUSED)
328 devlink_port_unregister(&dp->devlink_port);
Vivien Didelot1d277322017-11-06 16:11:48 -0500329
330 switch (dp->type) {
331 case DSA_PORT_TYPE_UNUSED:
332 break;
333 case DSA_PORT_TYPE_CPU:
Andrew Lunn4dad81e2019-04-28 19:37:19 +0200334 dsa_tag_driver_put(dp->tag_ops);
335 /* fall-through */
Vivien Didelot1d277322017-11-06 16:11:48 -0500336 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100337 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500338 break;
339 case DSA_PORT_TYPE_USER:
340 if (dp->slave) {
341 dsa_slave_destroy(dp->slave);
342 dp->slave = NULL;
343 }
344 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200345 }
346}
347
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500348static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200349{
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200350 int err;
351
Florian Fainelli6e830d82016-06-07 16:32:39 -0700352 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400353 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700354 * the slave MDIO bus driver rely on these values for probing PHY
355 * devices or not
356 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400357 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700358
Andrew Lunn96567d52017-03-28 23:45:07 +0200359 /* Add the switch to devlink before calling setup, so that setup can
360 * add dpipe tables
361 */
362 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
363 if (!ds->devlink)
364 return -ENOMEM;
365
366 err = devlink_register(ds->devlink, ds->dev);
367 if (err)
368 return err;
369
Vivien Didelotf515f192017-02-03 13:20:20 -0500370 err = dsa_switch_register_notifier(ds);
371 if (err)
372 return err;
373
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300374 err = ds->ops->setup(ds);
375 if (err < 0)
376 return err;
377
Vivien Didelot9d490b42016-08-23 12:38:56 -0400378 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700379 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
380 if (!ds->slave_mii_bus)
381 return -ENOMEM;
382
383 dsa_slave_mii_bus_init(ds);
384
385 err = mdiobus_register(ds->slave_mii_bus);
386 if (err < 0)
387 return err;
388 }
389
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200390 return 0;
391}
392
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500393static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200394{
Vivien Didelot9d490b42016-08-23 12:38:56 -0400395 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700396 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500397
398 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200399
400 if (ds->devlink) {
401 devlink_unregister(ds->devlink);
402 devlink_free(ds->devlink);
403 ds->devlink = NULL;
404 }
405
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200406}
407
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500408static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
409{
410 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500411 struct dsa_port *dp;
412 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500413 int err;
414
415 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
416 ds = dst->ds[device];
417 if (!ds)
418 continue;
419
420 err = dsa_switch_setup(ds);
421 if (err)
422 return err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500423
424 for (port = 0; port < ds->num_ports; port++) {
425 dp = &ds->ports[port];
426
427 err = dsa_port_setup(dp);
428 if (err)
429 return err;
430 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500431 }
432
433 return 0;
434}
435
436static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
437{
438 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500439 struct dsa_port *dp;
440 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500441
442 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
443 ds = dst->ds[device];
444 if (!ds)
445 continue;
446
Vivien Didelot1d277322017-11-06 16:11:48 -0500447 for (port = 0; port < ds->num_ports; port++) {
448 dp = &ds->ports[port];
449
450 dsa_port_teardown(dp);
451 }
452
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500453 dsa_switch_teardown(ds);
454 }
455}
456
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500457static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
458{
459 struct dsa_port *cpu_dp = dst->cpu_dp;
460 struct net_device *master = cpu_dp->master;
461
462 /* DSA currently supports a single pair of CPU port and master device */
463 return dsa_master_setup(master, cpu_dp);
464}
465
466static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
467{
468 struct dsa_port *cpu_dp = dst->cpu_dp;
469 struct net_device *master = cpu_dp->master;
470
471 return dsa_master_teardown(master);
472}
473
Vivien Didelotec15dd42017-11-06 16:11:46 -0500474static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200475{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500476 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200477 int err;
478
Vivien Didelotec15dd42017-11-06 16:11:46 -0500479 if (dst->setup) {
480 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
481 dst->index);
482 return -EEXIST;
483 }
484
Vivien Didelot34c09a82017-11-06 16:11:51 -0500485 complete = dsa_tree_setup_routing_table(dst);
486 if (!complete)
487 return 0;
488
Vivien Didelotf0704642017-11-06 16:11:44 -0500489 err = dsa_tree_setup_default_cpu(dst);
490 if (err)
491 return err;
492
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500493 err = dsa_tree_setup_switches(dst);
494 if (err)
495 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200496
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500497 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400498 if (err)
499 return err;
500
Vivien Didelotec15dd42017-11-06 16:11:46 -0500501 dst->setup = true;
502
503 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200504
505 return 0;
506}
507
Vivien Didelotec15dd42017-11-06 16:11:46 -0500508static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200509{
Vivien Didelotec15dd42017-11-06 16:11:46 -0500510 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200511 return;
512
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500513 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200514
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500515 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200516
Vivien Didelotf0704642017-11-06 16:11:44 -0500517 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700518
Vivien Didelotec15dd42017-11-06 16:11:46 -0500519 pr_info("DSA: tree %d torn down\n", dst->index);
520
521 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200522}
523
Vivien Didelot6da2a942017-11-03 19:05:25 -0400524static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
525 unsigned int index)
526{
Vivien Didelot30817352017-11-06 16:11:52 -0500527 dsa_tree_teardown(dst);
528
Vivien Didelot6da2a942017-11-03 19:05:25 -0400529 dst->ds[index] = NULL;
530 dsa_tree_put(dst);
531}
532
533static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
534 struct dsa_switch *ds)
535{
536 unsigned int index = ds->index;
Vivien Didelot30817352017-11-06 16:11:52 -0500537 int err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400538
539 if (dst->ds[index])
540 return -EBUSY;
541
542 dsa_tree_get(dst);
543 dst->ds[index] = ds;
544
Vivien Didelot30817352017-11-06 16:11:52 -0500545 err = dsa_tree_setup(dst);
546 if (err)
547 dsa_tree_remove_switch(dst, index);
548
549 return err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400550}
551
Vivien Didelot06e24d02017-11-03 19:05:29 -0400552static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
553{
554 if (!name)
555 name = "eth%d";
556
557 dp->type = DSA_PORT_TYPE_USER;
558 dp->name = name;
559
560 return 0;
561}
562
563static int dsa_port_parse_dsa(struct dsa_port *dp)
564{
565 dp->type = DSA_PORT_TYPE_DSA;
566
567 return 0;
568}
569
570static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
571{
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400572 struct dsa_switch *ds = dp->ds;
573 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400574 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200575 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200576
Florian Fainelli5ed4e3e2017-11-10 15:22:52 -0800577 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200578 tag_ops = dsa_tag_driver_get(tag_protocol);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400579 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700580 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400581 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700582 }
583
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400584 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Olteancc1939e2019-05-05 13:19:23 +0300585 dp->filter = tag_ops->filter;
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400586 dp->rcv = tag_ops->rcv;
587 dp->tag_ops = tag_ops;
588 dp->master = master;
589 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400590
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400591 return 0;
592}
593
Vivien Didelotfd223e22017-10-27 15:55:14 -0400594static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
595{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400596 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400597 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -0400598 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400599
Vivien Didelot06e24d02017-11-03 19:05:29 -0400600 dp->dn = dn;
601
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400602 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400603 struct net_device *master;
604
605 master = of_find_net_device_by_node(ethernet);
606 if (!master)
607 return -EPROBE_DEFER;
608
Vivien Didelot06e24d02017-11-03 19:05:29 -0400609 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400610 }
611
Vivien Didelot06e24d02017-11-03 19:05:29 -0400612 if (link)
613 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400614
Vivien Didelot06e24d02017-11-03 19:05:29 -0400615 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400616}
617
Vivien Didelot975e6e32017-11-03 19:05:27 -0400618static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
619 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200620{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400621 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400622 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +0800623 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200624 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400625
626 ports = of_get_child_by_name(dn, "ports");
627 if (!ports) {
628 dev_err(ds->dev, "no ports child node found\n");
629 return -EINVAL;
630 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200631
632 for_each_available_child_of_node(ports, port) {
633 err = of_property_read_u32(port, "reg", &reg);
634 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800635 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200636
Wen Yang9919a362019-02-25 15:22:19 +0800637 if (reg >= ds->num_ports) {
638 err = -EINVAL;
639 goto out_put_node;
640 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200641
Vivien Didelotfd223e22017-10-27 15:55:14 -0400642 dp = &ds->ports[reg];
643
644 err = dsa_port_parse_of(dp, port);
645 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800646 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200647 }
648
Wen Yang9919a362019-02-25 15:22:19 +0800649out_put_node:
650 of_node_put(ports);
651 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200652}
653
Vivien Didelot975e6e32017-11-03 19:05:27 -0400654static int dsa_switch_parse_member_of(struct dsa_switch *ds,
655 struct device_node *dn)
656{
657 u32 m[2] = { 0, 0 };
658 int sz;
659
660 /* Don't error out if this optional property isn't found */
661 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
662 if (sz < 0 && sz != -EINVAL)
663 return sz;
664
665 ds->index = m[1];
666 if (ds->index >= DSA_MAX_SWITCHES)
667 return -EINVAL;
668
669 ds->dst = dsa_tree_touch(m[0]);
670 if (!ds->dst)
671 return -ENOMEM;
672
673 return 0;
674}
675
676static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
677{
678 int err;
679
680 err = dsa_switch_parse_member_of(ds, dn);
681 if (err)
682 return err;
683
684 return dsa_switch_parse_ports_of(ds, dn);
685}
686
Vivien Didelotfd223e22017-10-27 15:55:14 -0400687static int dsa_port_parse(struct dsa_port *dp, const char *name,
688 struct device *dev)
689{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400690 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400691 struct net_device *master;
692
693 master = dsa_dev_to_net_device(dev);
694 if (!master)
695 return -EPROBE_DEFER;
696
697 dev_put(master);
698
Vivien Didelot06e24d02017-11-03 19:05:29 -0400699 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400700 }
701
Vivien Didelot06e24d02017-11-03 19:05:29 -0400702 if (!strcmp(name, "dsa"))
703 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400704
Vivien Didelot06e24d02017-11-03 19:05:29 -0400705 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400706}
707
Vivien Didelot975e6e32017-11-03 19:05:27 -0400708static int dsa_switch_parse_ports(struct dsa_switch *ds,
709 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800710{
711 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400712 struct dsa_port *dp;
713 struct device *dev;
714 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800715 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400716 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800717
718 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400719 name = cd->port_names[i];
720 dev = cd->netdev[i];
721 dp = &ds->ports[i];
722
723 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800724 continue;
725
Vivien Didelotfd223e22017-10-27 15:55:14 -0400726 err = dsa_port_parse(dp, name, dev);
727 if (err)
728 return err;
729
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800730 valid_name_found = true;
731 }
732
733 if (!valid_name_found && i == DSA_MAX_PORTS)
734 return -EINVAL;
735
736 return 0;
737}
738
Vivien Didelot975e6e32017-11-03 19:05:27 -0400739static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200740{
Vivien Didelot975e6e32017-11-03 19:05:27 -0400741 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200742
Vivien Didelot975e6e32017-11-03 19:05:27 -0400743 /* We don't support interconnected switches nor multiple trees via
744 * platform data, so this is the unique switch of the tree.
745 */
746 ds->index = 0;
747 ds->dst = dsa_tree_touch(0);
748 if (!ds->dst)
749 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200750
Vivien Didelot975e6e32017-11-03 19:05:27 -0400751 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800752}
753
Vivien Didelot30817352017-11-06 16:11:52 -0500754static int dsa_switch_add(struct dsa_switch *ds)
755{
756 struct dsa_switch_tree *dst = ds->dst;
757
758 return dsa_tree_add_switch(dst, ds);
759}
760
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500761static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200762{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400763 struct dsa_chip_data *pdata = ds->dev->platform_data;
764 struct device_node *np = ds->dev->of_node;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500765 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200766
Vivien Didelot975e6e32017-11-03 19:05:27 -0400767 if (np)
768 err = dsa_switch_parse_of(ds, np);
769 else if (pdata)
770 err = dsa_switch_parse(ds, pdata);
771 else
772 err = -ENODEV;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200773
Vivien Didelot975e6e32017-11-03 19:05:27 -0400774 if (err)
775 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200776
Vivien Didelot30817352017-11-06 16:11:52 -0500777 return dsa_switch_add(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200778}
779
Vivien Didelota0c02162017-01-27 15:29:36 -0500780struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
781{
Vivien Didelota0c02162017-01-27 15:29:36 -0500782 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500783 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500784
Gustavo A. R. Silva33b363e2019-02-07 19:16:03 -0600785 ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
Vivien Didelota0c02162017-01-27 15:29:36 -0500786 if (!ds)
787 return NULL;
788
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700789 /* We avoid allocating memory outside dsa_switch
790 * if it is not needed.
791 */
792 if (n <= sizeof(ds->_bitmap) * 8) {
793 ds->bitmap = &ds->_bitmap;
794 } else {
795 ds->bitmap = devm_kcalloc(dev,
796 BITS_TO_LONGS(n),
797 sizeof(unsigned long),
798 GFP_KERNEL);
799 if (unlikely(!ds->bitmap))
800 return NULL;
801 }
802
Vivien Didelota0c02162017-01-27 15:29:36 -0500803 ds->dev = dev;
804 ds->num_ports = n;
805
Vivien Didelot818be842017-01-27 15:29:38 -0500806 for (i = 0; i < ds->num_ports; ++i) {
807 ds->ports[i].index = i;
808 ds->ports[i].ds = ds;
809 }
810
Vivien Didelota0c02162017-01-27 15:29:36 -0500811 return ds;
812}
813EXPORT_SYMBOL_GPL(dsa_switch_alloc);
814
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400815int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200816{
817 int err;
818
819 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500820 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -0500821 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200822 mutex_unlock(&dsa2_mutex);
823
824 return err;
825}
826EXPORT_SYMBOL_GPL(dsa_register_switch);
827
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500828static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200829{
830 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400831 unsigned int index = ds->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200832
Vivien Didelot6da2a942017-11-03 19:05:25 -0400833 dsa_tree_remove_switch(dst, index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200834}
835
836void dsa_unregister_switch(struct dsa_switch *ds)
837{
838 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500839 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200840 mutex_unlock(&dsa2_mutex);
841}
842EXPORT_SYMBOL_GPL(dsa_unregister_switch);