blob: 44d26b5977cd75eedf73e8ce6fe3b9d2e4db7457 [file] [log] [blame]
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001/*
2 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
5 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/list.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020016#include <linux/netdevice.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020017#include <linux/slab.h>
18#include <linux/rtnetlink.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020019#include <linux/of.h>
20#include <linux/of_net.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040021
Andrew Lunn83c0afa2016-06-04 21:17:07 +020022#include "dsa_priv.h"
23
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040024static LIST_HEAD(dsa_tree_list);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020025static DEFINE_MUTEX(dsa2_mutex);
26
Andrew Lunn96567d52017-03-28 23:45:07 +020027static const struct devlink_ops dsa_devlink_ops = {
28};
29
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040030static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020031{
32 struct dsa_switch_tree *dst;
33
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040034 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -040035 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020036 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -040037
Andrew Lunn83c0afa2016-06-04 21:17:07 +020038 return NULL;
39}
40
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040041static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020042{
43 struct dsa_switch_tree *dst;
44
45 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
46 if (!dst)
47 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040048
Vivien Didelot49463b72017-11-03 19:05:21 -040049 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040050
Andrew Lunn83c0afa2016-06-04 21:17:07 +020051 INIT_LIST_HEAD(&dst->list);
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040052 list_add_tail(&dsa_tree_list, &dst->list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -040053
54 /* Initialize the reference counter to the number of switches, not 1 */
Andrew Lunn83c0afa2016-06-04 21:17:07 +020055 kref_init(&dst->refcount);
Vivien Didelot8e5bf972017-11-03 19:05:22 -040056 refcount_set(&dst->refcount.refcount, 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020057
58 return dst;
59}
60
Vivien Didelot65254102017-11-03 19:05:23 -040061static void dsa_tree_free(struct dsa_switch_tree *dst)
62{
63 list_del(&dst->list);
64 kfree(dst);
65}
66
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040067static struct dsa_switch_tree *dsa_tree_touch(int index)
68{
69 struct dsa_switch_tree *dst;
70
71 dst = dsa_tree_find(index);
72 if (!dst)
73 dst = dsa_tree_alloc(index);
74
75 return dst;
76}
77
Vivien Didelot65254102017-11-03 19:05:23 -040078static void dsa_tree_get(struct dsa_switch_tree *dst)
79{
80 kref_get(&dst->refcount);
81}
82
83static void dsa_tree_release(struct kref *ref)
84{
85 struct dsa_switch_tree *dst;
86
87 dst = container_of(ref, struct dsa_switch_tree, refcount);
88
89 dsa_tree_free(dst);
90}
91
92static void dsa_tree_put(struct dsa_switch_tree *dst)
93{
94 kref_put(&dst->refcount, dsa_tree_release);
95}
96
Florian Fainelli293784a2017-01-26 10:45:52 -080097static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020098{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -040099 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800100}
101
102static bool dsa_port_is_cpu(struct dsa_port *port)
103{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400104 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200105}
106
Vivien Didelotf0704642017-11-06 16:11:44 -0500107static bool dsa_port_is_user(struct dsa_port *dp)
108{
109 return dp->type == DSA_PORT_TYPE_USER;
110}
111
Vivien Didelotf163da82017-11-06 16:11:49 -0500112static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
113 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200114{
115 struct dsa_switch *ds;
Vivien Didelotf163da82017-11-06 16:11:49 -0500116 struct dsa_port *dp;
117 int device, port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200118
Vivien Didelotf163da82017-11-06 16:11:49 -0500119 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
120 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200121 if (!ds)
122 continue;
123
Vivien Didelotf163da82017-11-06 16:11:49 -0500124 for (port = 0; port < ds->num_ports; port++) {
125 dp = &ds->ports[port];
126
127 if (dp->dn == dn)
128 return dp;
129 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200130 }
131
132 return NULL;
133}
134
Vivien Didelot34c09a82017-11-06 16:11:51 -0500135static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200136{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500137 struct dsa_switch *ds = dp->ds;
138 struct dsa_switch_tree *dst = ds->dst;
139 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500140 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500141 struct dsa_port *link_dp;
Vivien Didelotc5286662017-11-06 16:11:50 -0500142 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200143
Vivien Didelotc5286662017-11-06 16:11:50 -0500144 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
145 link_dp = dsa_tree_find_port_by_node(dst, it.node);
146 if (!link_dp) {
147 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500148 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500149 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200150
Vivien Didelot34c09a82017-11-06 16:11:51 -0500151 ds->rtable[link_dp->ds->index] = dp->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200152 }
153
Vivien Didelot34c09a82017-11-06 16:11:51 -0500154 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200155}
156
Vivien Didelot34c09a82017-11-06 16:11:51 -0500157static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200158{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500159 bool complete = true;
160 struct dsa_port *dp;
161 int i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200162
Vivien Didelot34c09a82017-11-06 16:11:51 -0500163 for (i = 0; i < DSA_MAX_SWITCHES; i++)
164 ds->rtable[i] = DSA_RTABLE_NONE;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200165
Vivien Didelot34c09a82017-11-06 16:11:51 -0500166 for (i = 0; i < ds->num_ports; i++) {
167 dp = &ds->ports[i];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200168
Vivien Didelot34c09a82017-11-06 16:11:51 -0500169 if (dsa_port_is_dsa(dp)) {
170 complete = dsa_port_setup_routing_table(dp);
171 if (!complete)
172 break;
173 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200174 }
175
Vivien Didelot34c09a82017-11-06 16:11:51 -0500176 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200177}
178
Vivien Didelot34c09a82017-11-06 16:11:51 -0500179static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200180{
181 struct dsa_switch *ds;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500182 bool complete = true;
183 int device;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200184
Vivien Didelot34c09a82017-11-06 16:11:51 -0500185 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
186 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200187 if (!ds)
188 continue;
189
Vivien Didelot34c09a82017-11-06 16:11:51 -0500190 complete = dsa_switch_setup_routing_table(ds);
191 if (!complete)
192 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200193 }
194
Vivien Didelot34c09a82017-11-06 16:11:51 -0500195 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200196}
197
Vivien Didelotf0704642017-11-06 16:11:44 -0500198static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199{
200 struct dsa_switch *ds;
201 struct dsa_port *dp;
202 int device, port;
203
204 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
205 ds = dst->ds[device];
206 if (!ds)
207 continue;
208
209 for (port = 0; port < ds->num_ports; port++) {
210 dp = &ds->ports[port];
211
212 if (dsa_port_is_cpu(dp))
213 return dp;
214 }
215 }
216
217 return NULL;
218}
219
220static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
221{
222 struct dsa_switch *ds;
223 struct dsa_port *dp;
224 int device, port;
225
226 /* DSA currently only supports a single CPU port */
227 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
228 if (!dst->cpu_dp) {
229 pr_warn("Tree has no master device\n");
230 return -EINVAL;
231 }
232
233 /* Assign the default CPU port to all ports of the fabric */
234 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
235 ds = dst->ds[device];
236 if (!ds)
237 continue;
238
239 for (port = 0; port < ds->num_ports; port++) {
240 dp = &ds->ports[port];
241
242 if (dsa_port_is_user(dp))
243 dp->cpu_dp = dst->cpu_dp;
244 }
245 }
246
247 return 0;
248}
249
250static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
251{
252 /* DSA currently only supports a single CPU port */
253 dst->cpu_dp = NULL;
254}
255
Vivien Didelot1d277322017-11-06 16:11:48 -0500256static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200257{
Vivien Didelot1d277322017-11-06 16:11:48 -0500258 struct dsa_switch *ds = dp->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200259 int err;
260
Vivien Didelot1d277322017-11-06 16:11:48 -0500261 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200262
Vivien Didelot1d277322017-11-06 16:11:48 -0500263 err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200264 if (err)
265 return err;
266
Vivien Didelot1d277322017-11-06 16:11:48 -0500267 switch (dp->type) {
268 case DSA_PORT_TYPE_UNUSED:
269 break;
270 case DSA_PORT_TYPE_CPU:
271 case DSA_PORT_TYPE_DSA:
272 err = dsa_port_fixed_link_register_of(dp);
273 if (err) {
274 dev_err(ds->dev, "failed to register fixed link for port %d.%d\n",
275 ds->index, dp->index);
276 return err;
277 }
278
279 break;
280 case DSA_PORT_TYPE_USER:
281 err = dsa_slave_create(dp);
282 if (err)
283 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
284 ds->index, dp->index);
285 else
286 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
287 break;
288 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200289
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200290 return 0;
291}
292
Vivien Didelot1d277322017-11-06 16:11:48 -0500293static void dsa_port_teardown(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200294{
Vivien Didelot1d277322017-11-06 16:11:48 -0500295 devlink_port_unregister(&dp->devlink_port);
296
297 switch (dp->type) {
298 case DSA_PORT_TYPE_UNUSED:
299 break;
300 case DSA_PORT_TYPE_CPU:
301 case DSA_PORT_TYPE_DSA:
302 dsa_port_fixed_link_unregister_of(dp);
303 break;
304 case DSA_PORT_TYPE_USER:
305 if (dp->slave) {
306 dsa_slave_destroy(dp->slave);
307 dp->slave = NULL;
308 }
309 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200310 }
311}
312
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500313static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200314{
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200315 int err;
316
Florian Fainelli6e830d82016-06-07 16:32:39 -0700317 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400318 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700319 * the slave MDIO bus driver rely on these values for probing PHY
320 * devices or not
321 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400322 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700323
Andrew Lunn96567d52017-03-28 23:45:07 +0200324 /* Add the switch to devlink before calling setup, so that setup can
325 * add dpipe tables
326 */
327 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
328 if (!ds->devlink)
329 return -ENOMEM;
330
331 err = devlink_register(ds->devlink, ds->dev);
332 if (err)
333 return err;
334
Vivien Didelot9d490b42016-08-23 12:38:56 -0400335 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200336 if (err < 0)
337 return err;
338
Vivien Didelotf515f192017-02-03 13:20:20 -0500339 err = dsa_switch_register_notifier(ds);
340 if (err)
341 return err;
342
Vivien Didelot9d490b42016-08-23 12:38:56 -0400343 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700344 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
345 if (!ds->slave_mii_bus)
346 return -ENOMEM;
347
348 dsa_slave_mii_bus_init(ds);
349
350 err = mdiobus_register(ds->slave_mii_bus);
351 if (err < 0)
352 return err;
353 }
354
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200355 return 0;
356}
357
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500358static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200359{
Vivien Didelot9d490b42016-08-23 12:38:56 -0400360 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700361 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500362
363 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200364
365 if (ds->devlink) {
366 devlink_unregister(ds->devlink);
367 devlink_free(ds->devlink);
368 ds->devlink = NULL;
369 }
370
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200371}
372
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500373static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
374{
375 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500376 struct dsa_port *dp;
377 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500378 int err;
379
380 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
381 ds = dst->ds[device];
382 if (!ds)
383 continue;
384
385 err = dsa_switch_setup(ds);
386 if (err)
387 return err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500388
389 for (port = 0; port < ds->num_ports; port++) {
390 dp = &ds->ports[port];
391
392 err = dsa_port_setup(dp);
393 if (err)
394 return err;
395 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500396 }
397
398 return 0;
399}
400
401static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
402{
403 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500404 struct dsa_port *dp;
405 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500406
407 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
408 ds = dst->ds[device];
409 if (!ds)
410 continue;
411
Vivien Didelot1d277322017-11-06 16:11:48 -0500412 for (port = 0; port < ds->num_ports; port++) {
413 dp = &ds->ports[port];
414
415 dsa_port_teardown(dp);
416 }
417
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500418 dsa_switch_teardown(ds);
419 }
420}
421
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500422static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
423{
424 struct dsa_port *cpu_dp = dst->cpu_dp;
425 struct net_device *master = cpu_dp->master;
426
427 /* DSA currently supports a single pair of CPU port and master device */
428 return dsa_master_setup(master, cpu_dp);
429}
430
431static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
432{
433 struct dsa_port *cpu_dp = dst->cpu_dp;
434 struct net_device *master = cpu_dp->master;
435
436 return dsa_master_teardown(master);
437}
438
Vivien Didelotec15dd42017-11-06 16:11:46 -0500439static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200440{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500441 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200442 int err;
443
Vivien Didelotec15dd42017-11-06 16:11:46 -0500444 if (dst->setup) {
445 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
446 dst->index);
447 return -EEXIST;
448 }
449
Vivien Didelot34c09a82017-11-06 16:11:51 -0500450 complete = dsa_tree_setup_routing_table(dst);
451 if (!complete)
452 return 0;
453
Vivien Didelotf0704642017-11-06 16:11:44 -0500454 err = dsa_tree_setup_default_cpu(dst);
455 if (err)
456 return err;
457
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500458 err = dsa_tree_setup_switches(dst);
459 if (err)
460 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200461
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500462 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400463 if (err)
464 return err;
465
Vivien Didelotec15dd42017-11-06 16:11:46 -0500466 dst->setup = true;
467
468 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200469
470 return 0;
471}
472
Vivien Didelotec15dd42017-11-06 16:11:46 -0500473static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200474{
Vivien Didelotec15dd42017-11-06 16:11:46 -0500475 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200476 return;
477
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500478 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200479
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500480 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200481
Vivien Didelotf0704642017-11-06 16:11:44 -0500482 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700483
Vivien Didelotec15dd42017-11-06 16:11:46 -0500484 pr_info("DSA: tree %d torn down\n", dst->index);
485
486 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200487}
488
Vivien Didelot6da2a942017-11-03 19:05:25 -0400489static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
490 unsigned int index)
491{
492 dst->ds[index] = NULL;
493 dsa_tree_put(dst);
494}
495
496static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
497 struct dsa_switch *ds)
498{
499 unsigned int index = ds->index;
500
501 if (dst->ds[index])
502 return -EBUSY;
503
504 dsa_tree_get(dst);
505 dst->ds[index] = ds;
506
507 return 0;
508}
509
Vivien Didelot06e24d02017-11-03 19:05:29 -0400510static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
511{
512 if (!name)
513 name = "eth%d";
514
515 dp->type = DSA_PORT_TYPE_USER;
516 dp->name = name;
517
518 return 0;
519}
520
521static int dsa_port_parse_dsa(struct dsa_port *dp)
522{
523 dp->type = DSA_PORT_TYPE_DSA;
524
525 return 0;
526}
527
528static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
529{
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400530 struct dsa_switch *ds = dp->ds;
531 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400532 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200533 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200534
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700535 tag_protocol = ds->ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400536 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
537 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700538 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400539 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700540 }
541
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400542 dp->type = DSA_PORT_TYPE_CPU;
543 dp->rcv = tag_ops->rcv;
544 dp->tag_ops = tag_ops;
545 dp->master = master;
546 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400547
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400548 return 0;
549}
550
Vivien Didelotfd223e22017-10-27 15:55:14 -0400551static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
552{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400553 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400554 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -0400555 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400556
Vivien Didelot06e24d02017-11-03 19:05:29 -0400557 dp->dn = dn;
558
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400559 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400560 struct net_device *master;
561
562 master = of_find_net_device_by_node(ethernet);
563 if (!master)
564 return -EPROBE_DEFER;
565
Vivien Didelot06e24d02017-11-03 19:05:29 -0400566 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400567 }
568
Vivien Didelot06e24d02017-11-03 19:05:29 -0400569 if (link)
570 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400571
Vivien Didelot06e24d02017-11-03 19:05:29 -0400572 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400573}
574
Vivien Didelot975e6e32017-11-03 19:05:27 -0400575static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
576 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200577{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400578 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400579 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200580 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400581 int err;
582
583 ports = of_get_child_by_name(dn, "ports");
584 if (!ports) {
585 dev_err(ds->dev, "no ports child node found\n");
586 return -EINVAL;
587 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200588
589 for_each_available_child_of_node(ports, port) {
590 err = of_property_read_u32(port, "reg", &reg);
591 if (err)
592 return err;
593
Vivien Didelot26895e22017-01-27 15:29:37 -0500594 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200595 return -EINVAL;
596
Vivien Didelotfd223e22017-10-27 15:55:14 -0400597 dp = &ds->ports[reg];
598
599 err = dsa_port_parse_of(dp, port);
600 if (err)
601 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200602 }
603
604 return 0;
605}
606
Vivien Didelot975e6e32017-11-03 19:05:27 -0400607static int dsa_switch_parse_member_of(struct dsa_switch *ds,
608 struct device_node *dn)
609{
610 u32 m[2] = { 0, 0 };
611 int sz;
612
613 /* Don't error out if this optional property isn't found */
614 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
615 if (sz < 0 && sz != -EINVAL)
616 return sz;
617
618 ds->index = m[1];
619 if (ds->index >= DSA_MAX_SWITCHES)
620 return -EINVAL;
621
622 ds->dst = dsa_tree_touch(m[0]);
623 if (!ds->dst)
624 return -ENOMEM;
625
626 return 0;
627}
628
629static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
630{
631 int err;
632
633 err = dsa_switch_parse_member_of(ds, dn);
634 if (err)
635 return err;
636
637 return dsa_switch_parse_ports_of(ds, dn);
638}
639
Vivien Didelotfd223e22017-10-27 15:55:14 -0400640static int dsa_port_parse(struct dsa_port *dp, const char *name,
641 struct device *dev)
642{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400643 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400644 struct net_device *master;
645
646 master = dsa_dev_to_net_device(dev);
647 if (!master)
648 return -EPROBE_DEFER;
649
650 dev_put(master);
651
Vivien Didelot06e24d02017-11-03 19:05:29 -0400652 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400653 }
654
Vivien Didelot06e24d02017-11-03 19:05:29 -0400655 if (!strcmp(name, "dsa"))
656 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400657
Vivien Didelot06e24d02017-11-03 19:05:29 -0400658 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400659}
660
Vivien Didelot975e6e32017-11-03 19:05:27 -0400661static int dsa_switch_parse_ports(struct dsa_switch *ds,
662 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800663{
664 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400665 struct dsa_port *dp;
666 struct device *dev;
667 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800668 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400669 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800670
671 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400672 name = cd->port_names[i];
673 dev = cd->netdev[i];
674 dp = &ds->ports[i];
675
676 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800677 continue;
678
Vivien Didelotfd223e22017-10-27 15:55:14 -0400679 err = dsa_port_parse(dp, name, dev);
680 if (err)
681 return err;
682
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800683 valid_name_found = true;
684 }
685
686 if (!valid_name_found && i == DSA_MAX_PORTS)
687 return -EINVAL;
688
689 return 0;
690}
691
Vivien Didelot975e6e32017-11-03 19:05:27 -0400692static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200693{
Vivien Didelot975e6e32017-11-03 19:05:27 -0400694 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200695
Vivien Didelot975e6e32017-11-03 19:05:27 -0400696 /* We don't support interconnected switches nor multiple trees via
697 * platform data, so this is the unique switch of the tree.
698 */
699 ds->index = 0;
700 ds->dst = dsa_tree_touch(0);
701 if (!ds->dst)
702 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200703
Vivien Didelot975e6e32017-11-03 19:05:27 -0400704 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800705}
706
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400707static int _dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200708{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400709 struct dsa_chip_data *pdata = ds->dev->platform_data;
710 struct device_node *np = ds->dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200711 struct dsa_switch_tree *dst;
Vivien Didelot975e6e32017-11-03 19:05:27 -0400712 unsigned int index;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500713 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200714
Vivien Didelot975e6e32017-11-03 19:05:27 -0400715 if (np)
716 err = dsa_switch_parse_of(ds, np);
717 else if (pdata)
718 err = dsa_switch_parse(ds, pdata);
719 else
720 err = -ENODEV;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200721
Vivien Didelot975e6e32017-11-03 19:05:27 -0400722 if (err)
723 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200724
Vivien Didelot975e6e32017-11-03 19:05:27 -0400725 index = ds->index;
726 dst = ds->dst;
Vivien Didelot0eefe2c2017-11-03 19:05:26 -0400727
Vivien Didelot6da2a942017-11-03 19:05:25 -0400728 err = dsa_tree_add_switch(dst, ds);
729 if (err)
730 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200731
Vivien Didelotec15dd42017-11-06 16:11:46 -0500732 err = dsa_tree_setup(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200733 if (err) {
Vivien Didelotec15dd42017-11-06 16:11:46 -0500734 dsa_tree_teardown(dst);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500735 dsa_tree_remove_switch(dst, index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200736 }
737
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200738 return err;
739}
740
Vivien Didelota0c02162017-01-27 15:29:36 -0500741struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
742{
743 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
744 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500745 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500746
747 ds = devm_kzalloc(dev, size, GFP_KERNEL);
748 if (!ds)
749 return NULL;
750
751 ds->dev = dev;
752 ds->num_ports = n;
753
Vivien Didelot818be842017-01-27 15:29:38 -0500754 for (i = 0; i < ds->num_ports; ++i) {
755 ds->ports[i].index = i;
756 ds->ports[i].ds = ds;
757 }
758
Vivien Didelota0c02162017-01-27 15:29:36 -0500759 return ds;
760}
761EXPORT_SYMBOL_GPL(dsa_switch_alloc);
762
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400763int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200764{
765 int err;
766
767 mutex_lock(&dsa2_mutex);
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400768 err = _dsa_register_switch(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200769 mutex_unlock(&dsa2_mutex);
770
771 return err;
772}
773EXPORT_SYMBOL_GPL(dsa_register_switch);
774
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000775static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200776{
777 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400778 unsigned int index = ds->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200779
Vivien Didelotec15dd42017-11-06 16:11:46 -0500780 dsa_tree_teardown(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200781
Vivien Didelot6da2a942017-11-03 19:05:25 -0400782 dsa_tree_remove_switch(dst, index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200783}
784
785void dsa_unregister_switch(struct dsa_switch *ds)
786{
787 mutex_lock(&dsa2_mutex);
788 _dsa_unregister_switch(ds);
789 mutex_unlock(&dsa2_mutex);
790}
791EXPORT_SYMBOL_GPL(dsa_unregister_switch);