blob: 8b68dc2f570764f215dd8bac6bf7aff8742d15db [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
Vivien Didelot49463b72017-11-03 19:05:21 -040030static struct dsa_switch_tree *dsa_get_dst(unsigned int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020031{
32 struct dsa_switch_tree *dst;
33
34 list_for_each_entry(dst, &dsa_switch_trees, list)
Vivien Didelot49463b72017-11-03 19:05:21 -040035 if (dst->index == index) {
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030036 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
Vivien Didelot49463b72017-11-03 19:05:21 -040056static struct dsa_switch_tree *dsa_add_dst(unsigned int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020057{
58 struct dsa_switch_tree *dst;
59
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61 if (!dst)
62 return NULL;
Vivien Didelot49463b72017-11-03 19:05:21 -040063 dst->index = index;
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;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200260 int err;
261
Vivien Didelot951259aa2017-10-27 15:55:19 -0400262 err = dsa_slave_create(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200263 if (err) {
264 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700265 port->index, err);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400266 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200267 return err;
268 }
269
Florian Fainellie41c1b52017-06-02 12:31:22 -0700270 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
271 err = devlink_port_register(ds->devlink, &port->devlink_port,
272 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200273 if (err)
274 return err;
275
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400276 devlink_port_type_eth_set(&port->devlink_port, port->slave);
Andrew Lunn96567d52017-03-28 23:45:07 +0200277
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200278 return 0;
279}
280
Florian Fainellie41c1b52017-06-02 12:31:22 -0700281static void dsa_user_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200282{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700283 devlink_port_unregister(&port->devlink_port);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400284 if (port->slave) {
285 dsa_slave_destroy(port->slave);
286 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200287 }
288}
289
290static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
291{
Florian Fainelli293784a2017-01-26 10:45:52 -0800292 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200293 u32 index;
294 int err;
295
Florian Fainelli6e830d82016-06-07 16:32:39 -0700296 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400297 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700298 * the slave MDIO bus driver rely on these values for probing PHY
299 * devices or not
300 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400301 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700302
Andrew Lunn96567d52017-03-28 23:45:07 +0200303 /* Add the switch to devlink before calling setup, so that setup can
304 * add dpipe tables
305 */
306 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
307 if (!ds->devlink)
308 return -ENOMEM;
309
310 err = devlink_register(ds->devlink, ds->dev);
311 if (err)
312 return err;
313
Vivien Didelot9d490b42016-08-23 12:38:56 -0400314 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200315 if (err < 0)
316 return err;
317
Vivien Didelotf515f192017-02-03 13:20:20 -0500318 err = dsa_switch_register_notifier(ds);
319 if (err)
320 return err;
321
Vivien Didelot9d490b42016-08-23 12:38:56 -0400322 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700323 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
324 if (!ds->slave_mii_bus)
325 return -ENOMEM;
326
327 dsa_slave_mii_bus_init(ds);
328
329 err = mdiobus_register(ds->slave_mii_bus);
330 if (err < 0)
331 return err;
332 }
333
Vivien Didelot26895e22017-01-27 15:29:37 -0500334 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800335 port = &ds->ports[index];
336 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200337 continue;
338
339 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700340 err = dsa_dsa_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200341 if (err)
342 return err;
343 continue;
344 }
345
346 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700347 err = dsa_cpu_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200348 if (err)
349 return err;
350 continue;
351 }
352
Florian Fainellie41c1b52017-06-02 12:31:22 -0700353 err = dsa_user_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200354 if (err)
355 continue;
356 }
357
358 return 0;
359}
360
361static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
362{
Florian Fainelli293784a2017-01-26 10:45:52 -0800363 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200364 u32 index;
365
Vivien Didelot26895e22017-01-27 15:29:37 -0500366 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800367 port = &ds->ports[index];
368 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200369 continue;
370
371 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700372 dsa_dsa_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200373 continue;
374 }
375
376 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700377 dsa_cpu_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200378 continue;
379 }
380
Florian Fainellie41c1b52017-06-02 12:31:22 -0700381 dsa_user_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200382 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700383
Vivien Didelot9d490b42016-08-23 12:38:56 -0400384 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700385 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500386
387 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200388
389 if (ds->devlink) {
390 devlink_unregister(ds->devlink);
391 devlink_free(ds->devlink);
392 ds->devlink = NULL;
393 }
394
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200395}
396
397static int dsa_dst_apply(struct dsa_switch_tree *dst)
398{
399 struct dsa_switch *ds;
400 u32 index;
401 int err;
402
403 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
404 ds = dst->ds[index];
405 if (!ds)
406 continue;
407
408 err = dsa_ds_apply(dst, ds);
409 if (err)
410 return err;
411 }
412
413 /* If we use a tagging format that doesn't have an ethertype
414 * field, make sure that all packets from this point on get
415 * sent to the tag format's receive function.
416 */
417 wmb();
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400418 dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
Vivien Didelot19435632017-09-19 11:56:59 -0400419
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400420 err = dsa_master_ethtool_setup(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400421 if (err)
422 return err;
423
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200424 dst->applied = true;
425
426 return 0;
427}
428
429static void dsa_dst_unapply(struct dsa_switch_tree *dst)
430{
431 struct dsa_switch *ds;
432 u32 index;
433
434 if (!dst->applied)
435 return;
436
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400437 dsa_master_ethtool_restore(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400438
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400439 dst->cpu_dp->master->dsa_ptr = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200440
441 /* If we used a tagging format that doesn't have an ethertype
442 * field, make sure that all packets from this point get sent
443 * without the tag and go through the regular receive path.
444 */
445 wmb();
446
447 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
448 ds = dst->ds[index];
449 if (!ds)
450 continue;
451
452 dsa_ds_unapply(dst, ds);
453 }
454
Vivien Didelotcd8d7dd2017-09-19 11:56:58 -0400455 dst->cpu_dp = NULL;
Florian Fainelli0c73c522016-06-07 16:32:42 -0700456
Vivien Didelot49463b72017-11-03 19:05:21 -0400457 pr_info("DSA: tree %d unapplied\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200458 dst->applied = false;
459}
460
Florian Fainelli293784a2017-01-26 10:45:52 -0800461static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200462 struct dsa_switch_tree *dst,
463 struct dsa_switch *ds)
464{
Vivien Didelot62fc9582017-09-29 17:19:17 -0400465 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200466 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200467
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400468 if (!dst->cpu_dp)
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400469 dst->cpu_dp = port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200470
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700471 tag_protocol = ds->ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400472 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
473 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700474 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400475 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700476 }
477
Vivien Didelot15240242017-09-29 17:19:18 -0400478 dst->cpu_dp->tag_ops = tag_ops;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400479
480 /* Make a few copies for faster access in master receive hot path */
481 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400482 dst->cpu_dp->dst = dst;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700483
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200484 return 0;
485}
486
487static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
488{
Florian Fainelli293784a2017-01-26 10:45:52 -0800489 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200490 u32 index;
491 int err;
492
Vivien Didelot26895e22017-01-27 15:29:37 -0500493 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800494 port = &ds->ports[index];
Florian Fainelli14be36c2017-06-02 12:31:23 -0700495 if (!dsa_port_is_valid(port) ||
496 dsa_port_is_dsa(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200497 continue;
498
499 if (dsa_port_is_cpu(port)) {
500 err = dsa_cpu_parse(port, index, dst, ds);
501 if (err)
502 return err;
503 }
Florian Fainelli14be36c2017-06-02 12:31:23 -0700504
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200505 }
506
Vivien Didelot49463b72017-11-03 19:05:21 -0400507 pr_info("DSA: switch %d %d parsed\n", dst->index, ds->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200508
509 return 0;
510}
511
512static int dsa_dst_parse(struct dsa_switch_tree *dst)
513{
514 struct dsa_switch *ds;
Vivien Didelote4b77782017-06-15 15:06:54 -0400515 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200516 u32 index;
Vivien Didelote4b77782017-06-15 15:06:54 -0400517 int port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200518 int err;
519
520 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
521 ds = dst->ds[index];
522 if (!ds)
523 continue;
524
525 err = dsa_ds_parse(dst, ds);
526 if (err)
527 return err;
528 }
529
Florian Fainellic7848392017-08-28 17:10:51 -0700530 if (!dst->cpu_dp) {
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200531 pr_warn("Tree has no master device\n");
532 return -EINVAL;
533 }
534
Vivien Didelote4b77782017-06-15 15:06:54 -0400535 /* Assign the default CPU port to all ports of the fabric */
536 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
537 ds = dst->ds[index];
538 if (!ds)
539 continue;
540
541 for (port = 0; port < ds->num_ports; port++) {
542 dp = &ds->ports[port];
543 if (!dsa_port_is_valid(dp) ||
544 dsa_port_is_dsa(dp) ||
545 dsa_port_is_cpu(dp))
546 continue;
547
548 dp->cpu_dp = dst->cpu_dp;
549 }
550 }
551
Vivien Didelot49463b72017-11-03 19:05:21 -0400552 pr_info("DSA: tree %d parsed\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200553
554 return 0;
555}
556
Vivien Didelotfd223e22017-10-27 15:55:14 -0400557static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
558{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400559 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
560 struct device_node *link = of_parse_phandle(dn, "link", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400561 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400562
563 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400564 struct net_device *master;
565
566 master = of_find_net_device_by_node(ethernet);
567 if (!master)
568 return -EPROBE_DEFER;
569
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400570 dp->type = DSA_PORT_TYPE_CPU;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400571 dp->master = master;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400572 } else if (link) {
573 dp->type = DSA_PORT_TYPE_DSA;
574 } else {
Vivien Didelot1838fa82017-10-27 15:55:18 -0400575 if (!name)
576 name = "eth%d";
577
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400578 dp->type = DSA_PORT_TYPE_USER;
Vivien Didelot1838fa82017-10-27 15:55:18 -0400579 dp->name = name;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400580 }
581
Vivien Didelotfd223e22017-10-27 15:55:14 -0400582 dp->dn = dn;
583
584 return 0;
585}
586
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400587static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200588{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400589 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400590 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200591 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400592 int err;
593
594 ports = of_get_child_by_name(dn, "ports");
595 if (!ports) {
596 dev_err(ds->dev, "no ports child node found\n");
597 return -EINVAL;
598 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200599
600 for_each_available_child_of_node(ports, port) {
601 err = of_property_read_u32(port, "reg", &reg);
602 if (err)
603 return err;
604
Vivien Didelot26895e22017-01-27 15:29:37 -0500605 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200606 return -EINVAL;
607
Vivien Didelotfd223e22017-10-27 15:55:14 -0400608 dp = &ds->ports[reg];
609
610 err = dsa_port_parse_of(dp, port);
611 if (err)
612 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200613 }
614
615 return 0;
616}
617
Vivien Didelotfd223e22017-10-27 15:55:14 -0400618static int dsa_port_parse(struct dsa_port *dp, const char *name,
619 struct device *dev)
620{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400621 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400622 struct net_device *master;
623
624 master = dsa_dev_to_net_device(dev);
625 if (!master)
626 return -EPROBE_DEFER;
627
628 dev_put(master);
629
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400630 dp->type = DSA_PORT_TYPE_CPU;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400631 dp->master = master;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400632 } else if (!strcmp(name, "dsa")) {
633 dp->type = DSA_PORT_TYPE_DSA;
634 } else {
635 dp->type = DSA_PORT_TYPE_USER;
636 }
637
Vivien Didelotfd223e22017-10-27 15:55:14 -0400638 dp->name = name;
639
640 return 0;
641}
642
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800643static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
644{
645 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400646 struct dsa_port *dp;
647 struct device *dev;
648 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800649 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400650 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800651
652 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400653 name = cd->port_names[i];
654 dev = cd->netdev[i];
655 dp = &ds->ports[i];
656
657 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800658 continue;
659
Vivien Didelotfd223e22017-10-27 15:55:14 -0400660 err = dsa_port_parse(dp, name, dev);
661 if (err)
662 return err;
663
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800664 valid_name_found = true;
665 }
666
667 if (!valid_name_found && i == DSA_MAX_PORTS)
668 return -EINVAL;
669
670 return 0;
671}
672
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800673static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200674{
675 int err;
676
677 *tree = *index = 0;
678
679 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
680 if (err) {
681 /* Does not exist, but it is optional */
682 if (err == -EINVAL)
683 return 0;
684 return err;
685 }
686
687 err = of_property_read_u32_index(np, "dsa,member", 1, index);
688 if (err)
689 return err;
690
691 if (*index >= DSA_MAX_SWITCHES)
692 return -EINVAL;
693
694 return 0;
695}
696
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800697static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
698{
699 if (!pd)
700 return -ENODEV;
701
702 /* We do not support complex trees with dsa_chip_data */
703 *tree = 0;
704 *index = 0;
705
706 return 0;
707}
708
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400709static int _dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200710{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400711 struct dsa_chip_data *pdata = ds->dev->platform_data;
712 struct device_node *np = ds->dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200713 struct dsa_switch_tree *dst;
714 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400715 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200716
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800717 if (np) {
718 err = dsa_parse_member_dn(np, &tree, &index);
719 if (err)
720 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200721
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400722 err = dsa_parse_ports_of(np, ds);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800723 if (err)
724 return err;
725 } else {
726 err = dsa_parse_member(pdata, &tree, &index);
727 if (err)
728 return err;
729
730 err = dsa_parse_ports(pdata, ds);
731 if (err)
732 return err;
733 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200734
735 dst = dsa_get_dst(tree);
736 if (!dst) {
737 dst = dsa_add_dst(tree);
738 if (!dst)
739 return -ENOMEM;
740 }
741
742 if (dst->ds[index]) {
743 err = -EBUSY;
744 goto out;
745 }
746
747 ds->dst = dst;
748 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800749 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400750
751 /* Initialize the routing table */
752 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
753 ds->rtable[i] = DSA_RTABLE_NONE;
754
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200755 dsa_dst_add_ds(dst, ds, index);
756
757 err = dsa_dst_complete(dst);
758 if (err < 0)
759 goto out_del_dst;
760
761 if (err == 1) {
762 /* Not all switches registered yet */
763 err = 0;
764 goto out;
765 }
766
767 if (dst->applied) {
768 pr_info("DSA: Disjoint trees?\n");
769 return -EINVAL;
770 }
771
772 err = dsa_dst_parse(dst);
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400773 if (err)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200774 goto out_del_dst;
775
776 err = dsa_dst_apply(dst);
777 if (err) {
778 dsa_dst_unapply(dst);
779 goto out_del_dst;
780 }
781
782 dsa_put_dst(dst);
783 return 0;
784
785out_del_dst:
786 dsa_dst_del_ds(dst, ds, ds->index);
787out:
788 dsa_put_dst(dst);
789
790 return err;
791}
792
Vivien Didelota0c02162017-01-27 15:29:36 -0500793struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
794{
795 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
796 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500797 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500798
799 ds = devm_kzalloc(dev, size, GFP_KERNEL);
800 if (!ds)
801 return NULL;
802
803 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 Didelot23c9ee42017-05-26 18:12:51 -0400820 err = _dsa_register_switch(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200821 mutex_unlock(&dsa2_mutex);
822
823 return err;
824}
825EXPORT_SYMBOL_GPL(dsa_register_switch);
826
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000827static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200828{
829 struct dsa_switch_tree *dst = ds->dst;
830
831 dsa_dst_unapply(dst);
832
833 dsa_dst_del_ds(dst, ds, ds->index);
834}
835
836void dsa_unregister_switch(struct dsa_switch *ds)
837{
838 mutex_lock(&dsa2_mutex);
839 _dsa_unregister_switch(ds);
840 mutex_unlock(&dsa2_mutex);
841}
842EXPORT_SYMBOL_GPL(dsa_unregister_switch);