blob: 21f9bed11988937313f096a58c8b10340e24d052 [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
Andrew Lunn83c0afa2016-06-04 21:17:07 +020054 kref_init(&dst->refcount);
55
56 return dst;
57}
58
Vivien Didelot65254102017-11-03 19:05:23 -040059static void dsa_tree_free(struct dsa_switch_tree *dst)
60{
61 list_del(&dst->list);
62 kfree(dst);
63}
64
Vivien Didelot9e741042017-11-24 11:36:06 -050065static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
66{
67 if (dst)
68 kref_get(&dst->refcount);
69
70 return dst;
71}
72
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040073static struct dsa_switch_tree *dsa_tree_touch(int index)
74{
75 struct dsa_switch_tree *dst;
76
77 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -050078 if (dst)
79 return dsa_tree_get(dst);
80 else
81 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -040082}
83
84static void dsa_tree_release(struct kref *ref)
85{
86 struct dsa_switch_tree *dst;
87
88 dst = container_of(ref, struct dsa_switch_tree, refcount);
89
90 dsa_tree_free(dst);
91}
92
93static void dsa_tree_put(struct dsa_switch_tree *dst)
94{
Vivien Didelot9e741042017-11-24 11:36:06 -050095 if (dst)
96 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -040097}
98
Florian Fainelli293784a2017-01-26 10:45:52 -080099static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200100{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400101 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800102}
103
104static bool dsa_port_is_cpu(struct dsa_port *port)
105{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400106 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200107}
108
Vivien Didelotf0704642017-11-06 16:11:44 -0500109static bool dsa_port_is_user(struct dsa_port *dp)
110{
111 return dp->type == DSA_PORT_TYPE_USER;
112}
113
Vivien Didelotf163da82017-11-06 16:11:49 -0500114static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
115 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200116{
117 struct dsa_switch *ds;
Vivien Didelotf163da82017-11-06 16:11:49 -0500118 struct dsa_port *dp;
119 int device, port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200120
Vivien Didelotf163da82017-11-06 16:11:49 -0500121 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
122 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200123 if (!ds)
124 continue;
125
Vivien Didelotf163da82017-11-06 16:11:49 -0500126 for (port = 0; port < ds->num_ports; port++) {
127 dp = &ds->ports[port];
128
129 if (dp->dn == dn)
130 return dp;
131 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200132 }
133
134 return NULL;
135}
136
Vivien Didelot34c09a82017-11-06 16:11:51 -0500137static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200138{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500139 struct dsa_switch *ds = dp->ds;
140 struct dsa_switch_tree *dst = ds->dst;
141 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500142 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500143 struct dsa_port *link_dp;
Vivien Didelotc5286662017-11-06 16:11:50 -0500144 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200145
Vivien Didelotc5286662017-11-06 16:11:50 -0500146 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
147 link_dp = dsa_tree_find_port_by_node(dst, it.node);
148 if (!link_dp) {
149 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500150 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500151 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200152
Vivien Didelot34c09a82017-11-06 16:11:51 -0500153 ds->rtable[link_dp->ds->index] = dp->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200154 }
155
Vivien Didelot34c09a82017-11-06 16:11:51 -0500156 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200157}
158
Vivien Didelot34c09a82017-11-06 16:11:51 -0500159static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200160{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500161 bool complete = true;
162 struct dsa_port *dp;
163 int i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200164
Vivien Didelot34c09a82017-11-06 16:11:51 -0500165 for (i = 0; i < DSA_MAX_SWITCHES; i++)
166 ds->rtable[i] = DSA_RTABLE_NONE;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200167
Vivien Didelot34c09a82017-11-06 16:11:51 -0500168 for (i = 0; i < ds->num_ports; i++) {
169 dp = &ds->ports[i];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200170
Vivien Didelot34c09a82017-11-06 16:11:51 -0500171 if (dsa_port_is_dsa(dp)) {
172 complete = dsa_port_setup_routing_table(dp);
173 if (!complete)
174 break;
175 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200176 }
177
Vivien Didelot34c09a82017-11-06 16:11:51 -0500178 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200179}
180
Vivien Didelot34c09a82017-11-06 16:11:51 -0500181static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200182{
183 struct dsa_switch *ds;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500184 bool complete = true;
185 int device;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200186
Vivien Didelot34c09a82017-11-06 16:11:51 -0500187 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
188 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200189 if (!ds)
190 continue;
191
Vivien Didelot34c09a82017-11-06 16:11:51 -0500192 complete = dsa_switch_setup_routing_table(ds);
193 if (!complete)
194 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200195 }
196
Vivien Didelot34c09a82017-11-06 16:11:51 -0500197 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200198}
199
Vivien Didelotf0704642017-11-06 16:11:44 -0500200static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
201{
202 struct dsa_switch *ds;
203 struct dsa_port *dp;
204 int device, port;
205
206 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
207 ds = dst->ds[device];
208 if (!ds)
209 continue;
210
211 for (port = 0; port < ds->num_ports; port++) {
212 dp = &ds->ports[port];
213
214 if (dsa_port_is_cpu(dp))
215 return dp;
216 }
217 }
218
219 return NULL;
220}
221
222static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
223{
224 struct dsa_switch *ds;
225 struct dsa_port *dp;
226 int device, port;
227
228 /* DSA currently only supports a single CPU port */
229 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
230 if (!dst->cpu_dp) {
231 pr_warn("Tree has no master device\n");
232 return -EINVAL;
233 }
234
235 /* Assign the default CPU port to all ports of the fabric */
236 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
237 ds = dst->ds[device];
238 if (!ds)
239 continue;
240
241 for (port = 0; port < ds->num_ports; port++) {
242 dp = &ds->ports[port];
243
Vivien Didelot986d7cc2017-12-05 15:34:12 -0500244 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
Vivien Didelotf0704642017-11-06 16:11:44 -0500245 dp->cpu_dp = dst->cpu_dp;
246 }
247 }
248
249 return 0;
250}
251
252static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
253{
254 /* DSA currently only supports a single CPU port */
255 dst->cpu_dp = NULL;
256}
257
Vivien Didelot1d277322017-11-06 16:11:48 -0500258static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200259{
Vivien Didelot1d277322017-11-06 16:11:48 -0500260 struct dsa_switch *ds = dp->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200261 int err;
262
Vivien Didelot1d277322017-11-06 16:11:48 -0500263 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200264
Vivien Didelot1d277322017-11-06 16:11:48 -0500265 err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200266 if (err)
267 return err;
268
Vivien Didelot1d277322017-11-06 16:11:48 -0500269 switch (dp->type) {
270 case DSA_PORT_TYPE_UNUSED:
271 break;
272 case DSA_PORT_TYPE_CPU:
273 case DSA_PORT_TYPE_DSA:
274 err = dsa_port_fixed_link_register_of(dp);
275 if (err) {
276 dev_err(ds->dev, "failed to register fixed link for port %d.%d\n",
277 ds->index, dp->index);
278 return err;
279 }
280
281 break;
282 case DSA_PORT_TYPE_USER:
283 err = dsa_slave_create(dp);
284 if (err)
285 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
286 ds->index, dp->index);
287 else
288 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
289 break;
290 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200291
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200292 return 0;
293}
294
Vivien Didelot1d277322017-11-06 16:11:48 -0500295static void dsa_port_teardown(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200296{
Vivien Didelot1d277322017-11-06 16:11:48 -0500297 devlink_port_unregister(&dp->devlink_port);
298
299 switch (dp->type) {
300 case DSA_PORT_TYPE_UNUSED:
301 break;
302 case DSA_PORT_TYPE_CPU:
303 case DSA_PORT_TYPE_DSA:
304 dsa_port_fixed_link_unregister_of(dp);
305 break;
306 case DSA_PORT_TYPE_USER:
307 if (dp->slave) {
308 dsa_slave_destroy(dp->slave);
309 dp->slave = NULL;
310 }
311 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200312 }
313}
314
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500315static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200316{
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200317 int err;
318
Florian Fainelli6e830d82016-06-07 16:32:39 -0700319 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400320 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700321 * the slave MDIO bus driver rely on these values for probing PHY
322 * devices or not
323 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400324 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700325
Andrew Lunn96567d52017-03-28 23:45:07 +0200326 /* Add the switch to devlink before calling setup, so that setup can
327 * add dpipe tables
328 */
329 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
330 if (!ds->devlink)
331 return -ENOMEM;
332
333 err = devlink_register(ds->devlink, ds->dev);
334 if (err)
335 return err;
336
Vivien Didelot9d490b42016-08-23 12:38:56 -0400337 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200338 if (err < 0)
339 return err;
340
Vivien Didelotf515f192017-02-03 13:20:20 -0500341 err = dsa_switch_register_notifier(ds);
342 if (err)
343 return err;
344
Vivien Didelot9d490b42016-08-23 12:38:56 -0400345 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700346 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
347 if (!ds->slave_mii_bus)
348 return -ENOMEM;
349
350 dsa_slave_mii_bus_init(ds);
351
352 err = mdiobus_register(ds->slave_mii_bus);
353 if (err < 0)
354 return err;
355 }
356
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200357 return 0;
358}
359
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500360static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200361{
Vivien Didelot9d490b42016-08-23 12:38:56 -0400362 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700363 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500364
365 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200366
367 if (ds->devlink) {
368 devlink_unregister(ds->devlink);
369 devlink_free(ds->devlink);
370 ds->devlink = NULL;
371 }
372
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200373}
374
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500375static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
376{
377 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500378 struct dsa_port *dp;
379 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500380 int err;
381
382 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
383 ds = dst->ds[device];
384 if (!ds)
385 continue;
386
387 err = dsa_switch_setup(ds);
388 if (err)
389 return err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500390
391 for (port = 0; port < ds->num_ports; port++) {
392 dp = &ds->ports[port];
393
394 err = dsa_port_setup(dp);
395 if (err)
396 return err;
397 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500398 }
399
400 return 0;
401}
402
403static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
404{
405 struct dsa_switch *ds;
Vivien Didelot1d277322017-11-06 16:11:48 -0500406 struct dsa_port *dp;
407 int device, port;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500408
409 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
410 ds = dst->ds[device];
411 if (!ds)
412 continue;
413
Vivien Didelot1d277322017-11-06 16:11:48 -0500414 for (port = 0; port < ds->num_ports; port++) {
415 dp = &ds->ports[port];
416
417 dsa_port_teardown(dp);
418 }
419
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500420 dsa_switch_teardown(ds);
421 }
422}
423
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500424static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
425{
426 struct dsa_port *cpu_dp = dst->cpu_dp;
427 struct net_device *master = cpu_dp->master;
428
429 /* DSA currently supports a single pair of CPU port and master device */
430 return dsa_master_setup(master, cpu_dp);
431}
432
433static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
434{
435 struct dsa_port *cpu_dp = dst->cpu_dp;
436 struct net_device *master = cpu_dp->master;
437
438 return dsa_master_teardown(master);
439}
440
Vivien Didelotec15dd42017-11-06 16:11:46 -0500441static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200442{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500443 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200444 int err;
445
Vivien Didelotec15dd42017-11-06 16:11:46 -0500446 if (dst->setup) {
447 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
448 dst->index);
449 return -EEXIST;
450 }
451
Vivien Didelot34c09a82017-11-06 16:11:51 -0500452 complete = dsa_tree_setup_routing_table(dst);
453 if (!complete)
454 return 0;
455
Vivien Didelotf0704642017-11-06 16:11:44 -0500456 err = dsa_tree_setup_default_cpu(dst);
457 if (err)
458 return err;
459
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500460 err = dsa_tree_setup_switches(dst);
461 if (err)
462 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200463
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500464 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400465 if (err)
466 return err;
467
Vivien Didelotec15dd42017-11-06 16:11:46 -0500468 dst->setup = true;
469
470 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200471
472 return 0;
473}
474
Vivien Didelotec15dd42017-11-06 16:11:46 -0500475static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200476{
Vivien Didelotec15dd42017-11-06 16:11:46 -0500477 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200478 return;
479
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500480 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200481
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500482 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200483
Vivien Didelotf0704642017-11-06 16:11:44 -0500484 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700485
Vivien Didelotec15dd42017-11-06 16:11:46 -0500486 pr_info("DSA: tree %d torn down\n", dst->index);
487
488 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200489}
490
Vivien Didelot6da2a942017-11-03 19:05:25 -0400491static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
492 unsigned int index)
493{
Vivien Didelot30817352017-11-06 16:11:52 -0500494 dsa_tree_teardown(dst);
495
Vivien Didelot6da2a942017-11-03 19:05:25 -0400496 dst->ds[index] = NULL;
497 dsa_tree_put(dst);
498}
499
500static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
501 struct dsa_switch *ds)
502{
503 unsigned int index = ds->index;
Vivien Didelot30817352017-11-06 16:11:52 -0500504 int err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400505
506 if (dst->ds[index])
507 return -EBUSY;
508
509 dsa_tree_get(dst);
510 dst->ds[index] = ds;
511
Vivien Didelot30817352017-11-06 16:11:52 -0500512 err = dsa_tree_setup(dst);
513 if (err)
514 dsa_tree_remove_switch(dst, index);
515
516 return err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400517}
518
Vivien Didelot06e24d02017-11-03 19:05:29 -0400519static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
520{
521 if (!name)
522 name = "eth%d";
523
524 dp->type = DSA_PORT_TYPE_USER;
525 dp->name = name;
526
527 return 0;
528}
529
530static int dsa_port_parse_dsa(struct dsa_port *dp)
531{
532 dp->type = DSA_PORT_TYPE_DSA;
533
534 return 0;
535}
536
537static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
538{
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400539 struct dsa_switch *ds = dp->ds;
540 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400541 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200542 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200543
Florian Fainelli5ed4e3e2017-11-10 15:22:52 -0800544 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400545 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
546 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700547 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400548 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700549 }
550
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400551 dp->type = DSA_PORT_TYPE_CPU;
552 dp->rcv = tag_ops->rcv;
553 dp->tag_ops = tag_ops;
554 dp->master = master;
555 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400556
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400557 return 0;
558}
559
Vivien Didelotfd223e22017-10-27 15:55:14 -0400560static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
561{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400562 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400563 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -0400564 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400565
Vivien Didelot06e24d02017-11-03 19:05:29 -0400566 dp->dn = dn;
567
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400568 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400569 struct net_device *master;
570
571 master = of_find_net_device_by_node(ethernet);
572 if (!master)
573 return -EPROBE_DEFER;
574
Vivien Didelot06e24d02017-11-03 19:05:29 -0400575 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400576 }
577
Vivien Didelot06e24d02017-11-03 19:05:29 -0400578 if (link)
579 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400580
Vivien Didelot06e24d02017-11-03 19:05:29 -0400581 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400582}
583
Vivien Didelot975e6e32017-11-03 19:05:27 -0400584static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
585 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200586{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400587 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400588 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200589 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400590 int err;
591
592 ports = of_get_child_by_name(dn, "ports");
593 if (!ports) {
594 dev_err(ds->dev, "no ports child node found\n");
595 return -EINVAL;
596 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200597
598 for_each_available_child_of_node(ports, port) {
599 err = of_property_read_u32(port, "reg", &reg);
600 if (err)
601 return err;
602
Vivien Didelot26895e22017-01-27 15:29:37 -0500603 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200604 return -EINVAL;
605
Vivien Didelotfd223e22017-10-27 15:55:14 -0400606 dp = &ds->ports[reg];
607
608 err = dsa_port_parse_of(dp, port);
609 if (err)
610 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200611 }
612
613 return 0;
614}
615
Vivien Didelot975e6e32017-11-03 19:05:27 -0400616static int dsa_switch_parse_member_of(struct dsa_switch *ds,
617 struct device_node *dn)
618{
619 u32 m[2] = { 0, 0 };
620 int sz;
621
622 /* Don't error out if this optional property isn't found */
623 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
624 if (sz < 0 && sz != -EINVAL)
625 return sz;
626
627 ds->index = m[1];
628 if (ds->index >= DSA_MAX_SWITCHES)
629 return -EINVAL;
630
631 ds->dst = dsa_tree_touch(m[0]);
632 if (!ds->dst)
633 return -ENOMEM;
634
635 return 0;
636}
637
638static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
639{
640 int err;
641
642 err = dsa_switch_parse_member_of(ds, dn);
643 if (err)
644 return err;
645
646 return dsa_switch_parse_ports_of(ds, dn);
647}
648
Vivien Didelotfd223e22017-10-27 15:55:14 -0400649static int dsa_port_parse(struct dsa_port *dp, const char *name,
650 struct device *dev)
651{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400652 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400653 struct net_device *master;
654
655 master = dsa_dev_to_net_device(dev);
656 if (!master)
657 return -EPROBE_DEFER;
658
659 dev_put(master);
660
Vivien Didelot06e24d02017-11-03 19:05:29 -0400661 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400662 }
663
Vivien Didelot06e24d02017-11-03 19:05:29 -0400664 if (!strcmp(name, "dsa"))
665 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400666
Vivien Didelot06e24d02017-11-03 19:05:29 -0400667 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400668}
669
Vivien Didelot975e6e32017-11-03 19:05:27 -0400670static int dsa_switch_parse_ports(struct dsa_switch *ds,
671 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800672{
673 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400674 struct dsa_port *dp;
675 struct device *dev;
676 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800677 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400678 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800679
680 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400681 name = cd->port_names[i];
682 dev = cd->netdev[i];
683 dp = &ds->ports[i];
684
685 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800686 continue;
687
Vivien Didelotfd223e22017-10-27 15:55:14 -0400688 err = dsa_port_parse(dp, name, dev);
689 if (err)
690 return err;
691
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800692 valid_name_found = true;
693 }
694
695 if (!valid_name_found && i == DSA_MAX_PORTS)
696 return -EINVAL;
697
698 return 0;
699}
700
Vivien Didelot975e6e32017-11-03 19:05:27 -0400701static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200702{
Vivien Didelot975e6e32017-11-03 19:05:27 -0400703 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200704
Vivien Didelot975e6e32017-11-03 19:05:27 -0400705 /* We don't support interconnected switches nor multiple trees via
706 * platform data, so this is the unique switch of the tree.
707 */
708 ds->index = 0;
709 ds->dst = dsa_tree_touch(0);
710 if (!ds->dst)
711 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200712
Vivien Didelot975e6e32017-11-03 19:05:27 -0400713 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800714}
715
Vivien Didelot30817352017-11-06 16:11:52 -0500716static int dsa_switch_add(struct dsa_switch *ds)
717{
718 struct dsa_switch_tree *dst = ds->dst;
719
720 return dsa_tree_add_switch(dst, ds);
721}
722
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500723static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200724{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400725 struct dsa_chip_data *pdata = ds->dev->platform_data;
726 struct device_node *np = ds->dev->of_node;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500727 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200728
Vivien Didelot975e6e32017-11-03 19:05:27 -0400729 if (np)
730 err = dsa_switch_parse_of(ds, np);
731 else if (pdata)
732 err = dsa_switch_parse(ds, pdata);
733 else
734 err = -ENODEV;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200735
Vivien Didelot975e6e32017-11-03 19:05:27 -0400736 if (err)
737 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200738
Vivien Didelot30817352017-11-06 16:11:52 -0500739 return dsa_switch_add(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200740}
741
Vivien Didelota0c02162017-01-27 15:29:36 -0500742struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
743{
744 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
745 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500746 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500747
748 ds = devm_kzalloc(dev, size, GFP_KERNEL);
749 if (!ds)
750 return NULL;
751
752 ds->dev = dev;
753 ds->num_ports = n;
754
Vivien Didelot818be842017-01-27 15:29:38 -0500755 for (i = 0; i < ds->num_ports; ++i) {
756 ds->ports[i].index = i;
757 ds->ports[i].ds = ds;
758 }
759
Vivien Didelota0c02162017-01-27 15:29:36 -0500760 return ds;
761}
762EXPORT_SYMBOL_GPL(dsa_switch_alloc);
763
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400764int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200765{
766 int err;
767
768 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500769 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -0500770 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200771 mutex_unlock(&dsa2_mutex);
772
773 return err;
774}
775EXPORT_SYMBOL_GPL(dsa_register_switch);
776
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500777static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200778{
779 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400780 unsigned int index = ds->index;
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);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500788 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200789 mutex_unlock(&dsa2_mutex);
790}
791EXPORT_SYMBOL_GPL(dsa_unregister_switch);