blob: 8cd84c1b3dc020298e4291f8ce2e67056e9754b2 [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
24static LIST_HEAD(dsa_switch_trees);
25static DEFINE_MUTEX(dsa2_mutex);
26
Andrew Lunn96567d52017-03-28 23:45:07 +020027static const struct devlink_ops dsa_devlink_ops = {
28};
29
Andrew Lunn83c0afa2016-06-04 21:17:07 +020030static struct dsa_switch_tree *dsa_get_dst(u32 tree)
31{
32 struct dsa_switch_tree *dst;
33
34 list_for_each_entry(dst, &dsa_switch_trees, list)
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030035 if (dst->tree == tree) {
36 kref_get(&dst->refcount);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020037 return dst;
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030038 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +020039 return NULL;
40}
41
42static void dsa_free_dst(struct kref *ref)
43{
44 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
45 refcount);
46
47 list_del(&dst->list);
48 kfree(dst);
49}
50
51static void dsa_put_dst(struct dsa_switch_tree *dst)
52{
53 kref_put(&dst->refcount, dsa_free_dst);
54}
55
56static struct dsa_switch_tree *dsa_add_dst(u32 tree)
57{
58 struct dsa_switch_tree *dst;
59
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61 if (!dst)
62 return NULL;
63 dst->tree = tree;
Andrew Lunn83c0afa2016-06-04 21:17:07 +020064 INIT_LIST_HEAD(&dst->list);
65 list_add_tail(&dsa_switch_trees, &dst->list);
66 kref_init(&dst->refcount);
67
68 return dst;
69}
70
71static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
72 struct dsa_switch *ds, u32 index)
73{
74 kref_get(&dst->refcount);
75 dst->ds[index] = ds;
76}
77
78static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
79 struct dsa_switch *ds, u32 index)
80{
81 dst->ds[index] = NULL;
82 kref_put(&dst->refcount, dsa_free_dst);
83}
84
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080085/* For platform data configurations, we need to have a valid name argument to
86 * differentiate a disabled port from an enabled one
87 */
Florian Fainelli293784a2017-01-26 10:45:52 -080088static bool dsa_port_is_valid(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020089{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -040090 return port->type != DSA_PORT_TYPE_UNUSED;
Andrew Lunn83c0afa2016-06-04 21:17:07 +020091}
92
Florian Fainelli293784a2017-01-26 10:45:52 -080093static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020094{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -040095 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -080096}
97
98static bool dsa_port_is_cpu(struct dsa_port *port)
99{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400100 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200101}
102
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800103static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
104 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200105{
106 u32 index;
107
Vivien Didelot26895e22017-01-27 15:29:37 -0500108 for (index = 0; index < ds->num_ports; index++)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200109 if (ds->ports[index].dn == port)
110 return true;
111 return false;
112}
113
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800114static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
115 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200116{
117 struct dsa_switch *ds;
118 u32 index;
119
120 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
121 ds = dst->ds[index];
122 if (!ds)
123 continue;
124
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800125 if (dsa_ds_find_port_dn(ds, port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200126 return ds;
127 }
128
129 return NULL;
130}
131
132static int dsa_port_complete(struct dsa_switch_tree *dst,
133 struct dsa_switch *src_ds,
Florian Fainelli293784a2017-01-26 10:45:52 -0800134 struct dsa_port *port,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200135 u32 src_port)
136{
137 struct device_node *link;
138 int index;
139 struct dsa_switch *dst_ds;
140
141 for (index = 0;; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800142 link = of_parse_phandle(port->dn, "link", index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200143 if (!link)
144 break;
145
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800146 dst_ds = dsa_dst_find_port_dn(dst, link);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200147 of_node_put(link);
148
149 if (!dst_ds)
150 return 1;
151
152 src_ds->rtable[dst_ds->index] = src_port;
153 }
154
155 return 0;
156}
157
158/* A switch is complete if all the DSA ports phandles point to ports
159 * known in the tree. A return value of 1 means the tree is not
160 * complete. This is not an error condition. A value of 0 is
161 * success.
162 */
163static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
164{
Florian Fainelli293784a2017-01-26 10:45:52 -0800165 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200166 u32 index;
167 int err;
168
Vivien Didelot26895e22017-01-27 15:29:37 -0500169 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800170 port = &ds->ports[index];
171 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200172 continue;
173
174 if (!dsa_port_is_dsa(port))
175 continue;
176
177 err = dsa_port_complete(dst, ds, port, index);
178 if (err != 0)
179 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200180 }
181
182 return 0;
183}
184
185/* A tree is complete if all the DSA ports phandles point to ports
186 * known in the tree. A return value of 1 means the tree is not
187 * complete. This is not an error condition. A value of 0 is
188 * success.
189 */
190static int dsa_dst_complete(struct dsa_switch_tree *dst)
191{
192 struct dsa_switch *ds;
193 u32 index;
194 int err;
195
196 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
197 ds = dst->ds[index];
198 if (!ds)
199 continue;
200
201 err = dsa_ds_complete(dst, ds);
202 if (err != 0)
203 return err;
204 }
205
206 return 0;
207}
208
Florian Fainellie41c1b52017-06-02 12:31:22 -0700209static int dsa_dsa_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200210{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700211 struct dsa_switch *ds = port->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200212 int err;
213
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400214 err = dsa_port_fixed_link_register_of(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200215 if (err) {
216 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700217 port->index, err);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200218 return err;
219 }
220
Florian Fainellie41c1b52017-06-02 12:31:22 -0700221 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
Andrew Lunn96567d52017-03-28 23:45:07 +0200222
Florian Fainellie41c1b52017-06-02 12:31:22 -0700223 return devlink_port_register(ds->devlink, &port->devlink_port,
224 port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200225}
226
Florian Fainellie41c1b52017-06-02 12:31:22 -0700227static void dsa_dsa_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200228{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700229 devlink_port_unregister(&port->devlink_port);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400230 dsa_port_fixed_link_unregister_of(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200231}
232
Florian Fainellie41c1b52017-06-02 12:31:22 -0700233static int dsa_cpu_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200234{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700235 struct dsa_switch *ds = port->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200236 int err;
237
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400238 err = dsa_port_fixed_link_register_of(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200239 if (err) {
240 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700241 port->index, err);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200242 return err;
243 }
244
Florian Fainellie41c1b52017-06-02 12:31:22 -0700245 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
246 err = devlink_port_register(ds->devlink, &port->devlink_port,
247 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200248 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200249}
250
Florian Fainellie41c1b52017-06-02 12:31:22 -0700251static void dsa_cpu_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200252{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700253 devlink_port_unregister(&port->devlink_port);
Vivien Didelot57ab1ca2017-10-26 10:50:07 -0400254 dsa_port_fixed_link_unregister_of(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200255}
256
Florian Fainellie41c1b52017-06-02 12:31:22 -0700257static int dsa_user_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200258{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700259 struct dsa_switch *ds = port->ds;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800260 const char *name = port->name;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200261 int err;
262
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800263 if (port->dn)
264 name = of_get_property(port->dn, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500265 if (!name)
266 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200267
Vivien Didelot4cfbf092017-08-05 16:20:19 -0400268 err = dsa_slave_create(port, name);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200269 if (err) {
270 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700271 port->index, err);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400272 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200273 return err;
274 }
275
Florian Fainellie41c1b52017-06-02 12:31:22 -0700276 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
277 err = devlink_port_register(ds->devlink, &port->devlink_port,
278 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200279 if (err)
280 return err;
281
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400282 devlink_port_type_eth_set(&port->devlink_port, port->slave);
Andrew Lunn96567d52017-03-28 23:45:07 +0200283
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200284 return 0;
285}
286
Florian Fainellie41c1b52017-06-02 12:31:22 -0700287static void dsa_user_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200288{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700289 devlink_port_unregister(&port->devlink_port);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400290 if (port->slave) {
291 dsa_slave_destroy(port->slave);
292 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200293 }
294}
295
296static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
297{
Florian Fainelli293784a2017-01-26 10:45:52 -0800298 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200299 u32 index;
300 int err;
301
Florian Fainelli6e830d82016-06-07 16:32:39 -0700302 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400303 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700304 * the slave MDIO bus driver rely on these values for probing PHY
305 * devices or not
306 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400307 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700308
Andrew Lunn96567d52017-03-28 23:45:07 +0200309 /* Add the switch to devlink before calling setup, so that setup can
310 * add dpipe tables
311 */
312 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
313 if (!ds->devlink)
314 return -ENOMEM;
315
316 err = devlink_register(ds->devlink, ds->dev);
317 if (err)
318 return err;
319
Vivien Didelot9d490b42016-08-23 12:38:56 -0400320 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200321 if (err < 0)
322 return err;
323
Vivien Didelotf515f192017-02-03 13:20:20 -0500324 err = dsa_switch_register_notifier(ds);
325 if (err)
326 return err;
327
Vivien Didelot9d490b42016-08-23 12:38:56 -0400328 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700329 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
330 if (!ds->slave_mii_bus)
331 return -ENOMEM;
332
333 dsa_slave_mii_bus_init(ds);
334
335 err = mdiobus_register(ds->slave_mii_bus);
336 if (err < 0)
337 return err;
338 }
339
Vivien Didelot26895e22017-01-27 15:29:37 -0500340 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800341 port = &ds->ports[index];
342 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200343 continue;
344
345 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700346 err = dsa_dsa_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200347 if (err)
348 return err;
349 continue;
350 }
351
352 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700353 err = dsa_cpu_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200354 if (err)
355 return err;
356 continue;
357 }
358
Florian Fainellie41c1b52017-06-02 12:31:22 -0700359 err = dsa_user_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200360 if (err)
361 continue;
362 }
363
364 return 0;
365}
366
367static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
368{
Florian Fainelli293784a2017-01-26 10:45:52 -0800369 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200370 u32 index;
371
Vivien Didelot26895e22017-01-27 15:29:37 -0500372 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800373 port = &ds->ports[index];
374 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200375 continue;
376
377 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700378 dsa_dsa_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200379 continue;
380 }
381
382 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700383 dsa_cpu_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200384 continue;
385 }
386
Florian Fainellie41c1b52017-06-02 12:31:22 -0700387 dsa_user_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200388 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700389
Vivien Didelot9d490b42016-08-23 12:38:56 -0400390 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700391 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500392
393 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200394
395 if (ds->devlink) {
396 devlink_unregister(ds->devlink);
397 devlink_free(ds->devlink);
398 ds->devlink = NULL;
399 }
400
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200401}
402
403static int dsa_dst_apply(struct dsa_switch_tree *dst)
404{
405 struct dsa_switch *ds;
406 u32 index;
407 int err;
408
409 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
410 ds = dst->ds[index];
411 if (!ds)
412 continue;
413
414 err = dsa_ds_apply(dst, ds);
415 if (err)
416 return err;
417 }
418
419 /* If we use a tagging format that doesn't have an ethertype
420 * field, make sure that all packets from this point on get
421 * sent to the tag format's receive function.
422 */
423 wmb();
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400424 dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
Vivien Didelot19435632017-09-19 11:56:59 -0400425
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400426 err = dsa_master_ethtool_setup(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400427 if (err)
428 return err;
429
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200430 dst->applied = true;
431
432 return 0;
433}
434
435static void dsa_dst_unapply(struct dsa_switch_tree *dst)
436{
437 struct dsa_switch *ds;
438 u32 index;
439
440 if (!dst->applied)
441 return;
442
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400443 dsa_master_ethtool_restore(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400444
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400445 dst->cpu_dp->master->dsa_ptr = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200446
447 /* If we used a tagging format that doesn't have an ethertype
448 * field, make sure that all packets from this point get sent
449 * without the tag and go through the regular receive path.
450 */
451 wmb();
452
453 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
454 ds = dst->ds[index];
455 if (!ds)
456 continue;
457
458 dsa_ds_unapply(dst, ds);
459 }
460
Vivien Didelotcd8d7dd2017-09-19 11:56:58 -0400461 dst->cpu_dp = NULL;
Florian Fainelli0c73c522016-06-07 16:32:42 -0700462
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200463 pr_info("DSA: tree %d unapplied\n", dst->tree);
464 dst->applied = false;
465}
466
Florian Fainelli293784a2017-01-26 10:45:52 -0800467static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200468 struct dsa_switch_tree *dst,
469 struct dsa_switch *ds)
470{
Vivien Didelot62fc9582017-09-29 17:19:17 -0400471 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200472 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200473
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400474 if (!dst->cpu_dp)
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400475 dst->cpu_dp = port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200476
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700477 tag_protocol = ds->ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400478 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
479 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700480 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400481 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700482 }
483
Vivien Didelot15240242017-09-29 17:19:18 -0400484 dst->cpu_dp->tag_ops = tag_ops;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400485
486 /* Make a few copies for faster access in master receive hot path */
487 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400488 dst->cpu_dp->dst = dst;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700489
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200490 return 0;
491}
492
493static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
494{
Florian Fainelli293784a2017-01-26 10:45:52 -0800495 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200496 u32 index;
497 int err;
498
Vivien Didelot26895e22017-01-27 15:29:37 -0500499 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800500 port = &ds->ports[index];
Florian Fainelli14be36c2017-06-02 12:31:23 -0700501 if (!dsa_port_is_valid(port) ||
502 dsa_port_is_dsa(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200503 continue;
504
505 if (dsa_port_is_cpu(port)) {
506 err = dsa_cpu_parse(port, index, dst, ds);
507 if (err)
508 return err;
509 }
Florian Fainelli14be36c2017-06-02 12:31:23 -0700510
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200511 }
512
513 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
514
515 return 0;
516}
517
518static int dsa_dst_parse(struct dsa_switch_tree *dst)
519{
520 struct dsa_switch *ds;
Vivien Didelote4b77782017-06-15 15:06:54 -0400521 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200522 u32 index;
Vivien Didelote4b77782017-06-15 15:06:54 -0400523 int port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200524 int err;
525
526 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
527 ds = dst->ds[index];
528 if (!ds)
529 continue;
530
531 err = dsa_ds_parse(dst, ds);
532 if (err)
533 return err;
534 }
535
Florian Fainellic7848392017-08-28 17:10:51 -0700536 if (!dst->cpu_dp) {
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200537 pr_warn("Tree has no master device\n");
538 return -EINVAL;
539 }
540
Vivien Didelote4b77782017-06-15 15:06:54 -0400541 /* Assign the default CPU port to all ports of the fabric */
542 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
543 ds = dst->ds[index];
544 if (!ds)
545 continue;
546
547 for (port = 0; port < ds->num_ports; port++) {
548 dp = &ds->ports[port];
549 if (!dsa_port_is_valid(dp) ||
550 dsa_port_is_dsa(dp) ||
551 dsa_port_is_cpu(dp))
552 continue;
553
554 dp->cpu_dp = dst->cpu_dp;
555 }
556 }
557
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200558 pr_info("DSA: tree %d parsed\n", dst->tree);
559
560 return 0;
561}
562
Vivien Didelotfd223e22017-10-27 15:55:14 -0400563static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
564{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400565 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
566 struct device_node *link = of_parse_phandle(dn, "link", 0);
567
568 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 Didelot6d4e5c52017-10-27 15:55:15 -0400575 dp->type = DSA_PORT_TYPE_CPU;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400576 dp->master = master;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400577 } else if (link) {
578 dp->type = DSA_PORT_TYPE_DSA;
579 } else {
580 dp->type = DSA_PORT_TYPE_USER;
581 }
582
Vivien Didelotfd223e22017-10-27 15:55:14 -0400583 dp->dn = dn;
584
585 return 0;
586}
587
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400588static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200589{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400590 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400591 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200592 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400593 int err;
594
595 ports = of_get_child_by_name(dn, "ports");
596 if (!ports) {
597 dev_err(ds->dev, "no ports child node found\n");
598 return -EINVAL;
599 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200600
601 for_each_available_child_of_node(ports, port) {
602 err = of_property_read_u32(port, "reg", &reg);
603 if (err)
604 return err;
605
Vivien Didelot26895e22017-01-27 15:29:37 -0500606 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200607 return -EINVAL;
608
Vivien Didelotfd223e22017-10-27 15:55:14 -0400609 dp = &ds->ports[reg];
610
611 err = dsa_port_parse_of(dp, port);
612 if (err)
613 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200614 }
615
616 return 0;
617}
618
Vivien Didelotfd223e22017-10-27 15:55:14 -0400619static int dsa_port_parse(struct dsa_port *dp, const char *name,
620 struct device *dev)
621{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400622 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400623 struct net_device *master;
624
625 master = dsa_dev_to_net_device(dev);
626 if (!master)
627 return -EPROBE_DEFER;
628
629 dev_put(master);
630
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400631 dp->type = DSA_PORT_TYPE_CPU;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400632 dp->master = master;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400633 } else if (!strcmp(name, "dsa")) {
634 dp->type = DSA_PORT_TYPE_DSA;
635 } else {
636 dp->type = DSA_PORT_TYPE_USER;
637 }
638
Vivien Didelotfd223e22017-10-27 15:55:14 -0400639 dp->name = name;
640
641 return 0;
642}
643
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800644static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
645{
646 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400647 struct dsa_port *dp;
648 struct device *dev;
649 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800650 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400651 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800652
653 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400654 name = cd->port_names[i];
655 dev = cd->netdev[i];
656 dp = &ds->ports[i];
657
658 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800659 continue;
660
Vivien Didelotfd223e22017-10-27 15:55:14 -0400661 err = dsa_port_parse(dp, name, dev);
662 if (err)
663 return err;
664
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800665 valid_name_found = true;
666 }
667
668 if (!valid_name_found && i == DSA_MAX_PORTS)
669 return -EINVAL;
670
671 return 0;
672}
673
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800674static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200675{
676 int err;
677
678 *tree = *index = 0;
679
680 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
681 if (err) {
682 /* Does not exist, but it is optional */
683 if (err == -EINVAL)
684 return 0;
685 return err;
686 }
687
688 err = of_property_read_u32_index(np, "dsa,member", 1, index);
689 if (err)
690 return err;
691
692 if (*index >= DSA_MAX_SWITCHES)
693 return -EINVAL;
694
695 return 0;
696}
697
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800698static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
699{
700 if (!pd)
701 return -ENODEV;
702
703 /* We do not support complex trees with dsa_chip_data */
704 *tree = 0;
705 *index = 0;
706
707 return 0;
708}
709
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400710static int _dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200711{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400712 struct dsa_chip_data *pdata = ds->dev->platform_data;
713 struct device_node *np = ds->dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200714 struct dsa_switch_tree *dst;
715 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400716 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200717
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800718 if (np) {
719 err = dsa_parse_member_dn(np, &tree, &index);
720 if (err)
721 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200722
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400723 err = dsa_parse_ports_of(np, ds);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800724 if (err)
725 return err;
726 } else {
727 err = dsa_parse_member(pdata, &tree, &index);
728 if (err)
729 return err;
730
731 err = dsa_parse_ports(pdata, ds);
732 if (err)
733 return err;
734 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200735
736 dst = dsa_get_dst(tree);
737 if (!dst) {
738 dst = dsa_add_dst(tree);
739 if (!dst)
740 return -ENOMEM;
741 }
742
743 if (dst->ds[index]) {
744 err = -EBUSY;
745 goto out;
746 }
747
748 ds->dst = dst;
749 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800750 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400751
752 /* Initialize the routing table */
753 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
754 ds->rtable[i] = DSA_RTABLE_NONE;
755
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200756 dsa_dst_add_ds(dst, ds, index);
757
758 err = dsa_dst_complete(dst);
759 if (err < 0)
760 goto out_del_dst;
761
762 if (err == 1) {
763 /* Not all switches registered yet */
764 err = 0;
765 goto out;
766 }
767
768 if (dst->applied) {
769 pr_info("DSA: Disjoint trees?\n");
770 return -EINVAL;
771 }
772
773 err = dsa_dst_parse(dst);
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400774 if (err)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200775 goto out_del_dst;
776
777 err = dsa_dst_apply(dst);
778 if (err) {
779 dsa_dst_unapply(dst);
780 goto out_del_dst;
781 }
782
783 dsa_put_dst(dst);
784 return 0;
785
786out_del_dst:
787 dsa_dst_del_ds(dst, ds, ds->index);
788out:
789 dsa_put_dst(dst);
790
791 return err;
792}
793
Vivien Didelota0c02162017-01-27 15:29:36 -0500794struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
795{
796 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
797 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500798 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500799
800 ds = devm_kzalloc(dev, size, GFP_KERNEL);
801 if (!ds)
802 return NULL;
803
804 ds->dev = dev;
805 ds->num_ports = n;
806
Vivien Didelot818be842017-01-27 15:29:38 -0500807 for (i = 0; i < ds->num_ports; ++i) {
808 ds->ports[i].index = i;
809 ds->ports[i].ds = ds;
810 }
811
Vivien Didelota0c02162017-01-27 15:29:36 -0500812 return ds;
813}
814EXPORT_SYMBOL_GPL(dsa_switch_alloc);
815
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400816int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200817{
818 int err;
819
820 mutex_lock(&dsa2_mutex);
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400821 err = _dsa_register_switch(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200822 mutex_unlock(&dsa2_mutex);
823
824 return err;
825}
826EXPORT_SYMBOL_GPL(dsa_register_switch);
827
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000828static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200829{
830 struct dsa_switch_tree *dst = ds->dst;
831
832 dsa_dst_unapply(dst);
833
834 dsa_dst_del_ds(dst, ds, ds->index);
835}
836
837void dsa_unregister_switch(struct dsa_switch *ds)
838{
839 mutex_lock(&dsa2_mutex);
840 _dsa_unregister_switch(ds);
841 mutex_unlock(&dsa2_mutex);
842}
843EXPORT_SYMBOL_GPL(dsa_unregister_switch);