blob: 895c38427ef03f72002fd42f7b98ca1ea10319d9 [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 struct net_device *ethernet_dev;
474 struct device_node *ethernet;
475
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800476 if (port->dn) {
477 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
478 if (!ethernet)
479 return -EINVAL;
480 ethernet_dev = of_find_net_device_by_node(ethernet);
Vivien Didelot3eb8fee2017-10-24 16:37:19 -0400481 if (!ethernet_dev)
482 return -EPROBE_DEFER;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800483 } else {
484 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
Vivien Didelot3eb8fee2017-10-24 16:37:19 -0400485 if (!ethernet_dev)
486 return -EPROBE_DEFER;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800487 dev_put(ethernet_dev);
488 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200489
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700490 if (!dst->cpu_dp) {
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400491 dst->cpu_dp = port;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400492 dst->cpu_dp->master = ethernet_dev;
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700493 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200494
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700495 tag_protocol = ds->ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400496 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
497 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700498 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400499 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700500 }
501
Vivien Didelot15240242017-09-29 17:19:18 -0400502 dst->cpu_dp->tag_ops = tag_ops;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400503
504 /* Make a few copies for faster access in master receive hot path */
505 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400506 dst->cpu_dp->dst = dst;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700507
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200508 return 0;
509}
510
511static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
512{
Florian Fainelli293784a2017-01-26 10:45:52 -0800513 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200514 u32 index;
515 int err;
516
Vivien Didelot26895e22017-01-27 15:29:37 -0500517 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800518 port = &ds->ports[index];
Florian Fainelli14be36c2017-06-02 12:31:23 -0700519 if (!dsa_port_is_valid(port) ||
520 dsa_port_is_dsa(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200521 continue;
522
523 if (dsa_port_is_cpu(port)) {
524 err = dsa_cpu_parse(port, index, dst, ds);
525 if (err)
526 return err;
527 }
Florian Fainelli14be36c2017-06-02 12:31:23 -0700528
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200529 }
530
531 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
532
533 return 0;
534}
535
536static int dsa_dst_parse(struct dsa_switch_tree *dst)
537{
538 struct dsa_switch *ds;
Vivien Didelote4b77782017-06-15 15:06:54 -0400539 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200540 u32 index;
Vivien Didelote4b77782017-06-15 15:06:54 -0400541 int port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200542 int err;
543
544 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
545 ds = dst->ds[index];
546 if (!ds)
547 continue;
548
549 err = dsa_ds_parse(dst, ds);
550 if (err)
551 return err;
552 }
553
Florian Fainellic7848392017-08-28 17:10:51 -0700554 if (!dst->cpu_dp) {
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200555 pr_warn("Tree has no master device\n");
556 return -EINVAL;
557 }
558
Vivien Didelote4b77782017-06-15 15:06:54 -0400559 /* Assign the default CPU port to all ports of the fabric */
560 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
561 ds = dst->ds[index];
562 if (!ds)
563 continue;
564
565 for (port = 0; port < ds->num_ports; port++) {
566 dp = &ds->ports[port];
567 if (!dsa_port_is_valid(dp) ||
568 dsa_port_is_dsa(dp) ||
569 dsa_port_is_cpu(dp))
570 continue;
571
572 dp->cpu_dp = dst->cpu_dp;
573 }
574 }
575
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200576 pr_info("DSA: tree %d parsed\n", dst->tree);
577
578 return 0;
579}
580
Vivien Didelotfd223e22017-10-27 15:55:14 -0400581static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
582{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400583 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
584 struct device_node *link = of_parse_phandle(dn, "link", 0);
585
586 if (ethernet) {
587 dp->type = DSA_PORT_TYPE_CPU;
588 } else if (link) {
589 dp->type = DSA_PORT_TYPE_DSA;
590 } else {
591 dp->type = DSA_PORT_TYPE_USER;
592 }
593
Vivien Didelotfd223e22017-10-27 15:55:14 -0400594 dp->dn = dn;
595
596 return 0;
597}
598
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400599static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200600{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400601 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400602 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200603 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400604 int err;
605
606 ports = of_get_child_by_name(dn, "ports");
607 if (!ports) {
608 dev_err(ds->dev, "no ports child node found\n");
609 return -EINVAL;
610 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200611
612 for_each_available_child_of_node(ports, port) {
613 err = of_property_read_u32(port, "reg", &reg);
614 if (err)
615 return err;
616
Vivien Didelot26895e22017-01-27 15:29:37 -0500617 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200618 return -EINVAL;
619
Vivien Didelotfd223e22017-10-27 15:55:14 -0400620 dp = &ds->ports[reg];
621
622 err = dsa_port_parse_of(dp, port);
623 if (err)
624 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200625 }
626
627 return 0;
628}
629
Vivien Didelotfd223e22017-10-27 15:55:14 -0400630static int dsa_port_parse(struct dsa_port *dp, const char *name,
631 struct device *dev)
632{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400633 if (!strcmp(name, "cpu")) {
634 dp->type = DSA_PORT_TYPE_CPU;
635 } else if (!strcmp(name, "dsa")) {
636 dp->type = DSA_PORT_TYPE_DSA;
637 } else {
638 dp->type = DSA_PORT_TYPE_USER;
639 }
640
Vivien Didelotfd223e22017-10-27 15:55:14 -0400641 dp->name = name;
642
643 return 0;
644}
645
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800646static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
647{
648 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400649 struct dsa_port *dp;
650 struct device *dev;
651 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800652 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400653 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800654
655 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400656 name = cd->port_names[i];
657 dev = cd->netdev[i];
658 dp = &ds->ports[i];
659
660 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800661 continue;
662
Vivien Didelotfd223e22017-10-27 15:55:14 -0400663 err = dsa_port_parse(dp, name, dev);
664 if (err)
665 return err;
666
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800667 valid_name_found = true;
668 }
669
670 if (!valid_name_found && i == DSA_MAX_PORTS)
671 return -EINVAL;
672
673 return 0;
674}
675
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800676static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200677{
678 int err;
679
680 *tree = *index = 0;
681
682 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
683 if (err) {
684 /* Does not exist, but it is optional */
685 if (err == -EINVAL)
686 return 0;
687 return err;
688 }
689
690 err = of_property_read_u32_index(np, "dsa,member", 1, index);
691 if (err)
692 return err;
693
694 if (*index >= DSA_MAX_SWITCHES)
695 return -EINVAL;
696
697 return 0;
698}
699
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800700static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
701{
702 if (!pd)
703 return -ENODEV;
704
705 /* We do not support complex trees with dsa_chip_data */
706 *tree = 0;
707 *index = 0;
708
709 return 0;
710}
711
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400712static int _dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200713{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400714 struct dsa_chip_data *pdata = ds->dev->platform_data;
715 struct device_node *np = ds->dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200716 struct dsa_switch_tree *dst;
717 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400718 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200719
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800720 if (np) {
721 err = dsa_parse_member_dn(np, &tree, &index);
722 if (err)
723 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200724
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400725 err = dsa_parse_ports_of(np, ds);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800726 if (err)
727 return err;
728 } else {
729 err = dsa_parse_member(pdata, &tree, &index);
730 if (err)
731 return err;
732
733 err = dsa_parse_ports(pdata, ds);
734 if (err)
735 return err;
736 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200737
738 dst = dsa_get_dst(tree);
739 if (!dst) {
740 dst = dsa_add_dst(tree);
741 if (!dst)
742 return -ENOMEM;
743 }
744
745 if (dst->ds[index]) {
746 err = -EBUSY;
747 goto out;
748 }
749
750 ds->dst = dst;
751 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800752 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400753
754 /* Initialize the routing table */
755 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
756 ds->rtable[i] = DSA_RTABLE_NONE;
757
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200758 dsa_dst_add_ds(dst, ds, index);
759
760 err = dsa_dst_complete(dst);
761 if (err < 0)
762 goto out_del_dst;
763
764 if (err == 1) {
765 /* Not all switches registered yet */
766 err = 0;
767 goto out;
768 }
769
770 if (dst->applied) {
771 pr_info("DSA: Disjoint trees?\n");
772 return -EINVAL;
773 }
774
775 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100776 if (err) {
777 if (err == -EPROBE_DEFER) {
778 dsa_dst_del_ds(dst, ds, ds->index);
779 return err;
780 }
781
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200782 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100783 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200784
785 err = dsa_dst_apply(dst);
786 if (err) {
787 dsa_dst_unapply(dst);
788 goto out_del_dst;
789 }
790
791 dsa_put_dst(dst);
792 return 0;
793
794out_del_dst:
795 dsa_dst_del_ds(dst, ds, ds->index);
796out:
797 dsa_put_dst(dst);
798
799 return err;
800}
801
Vivien Didelota0c02162017-01-27 15:29:36 -0500802struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
803{
804 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
805 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500806 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500807
808 ds = devm_kzalloc(dev, size, GFP_KERNEL);
809 if (!ds)
810 return NULL;
811
812 ds->dev = dev;
813 ds->num_ports = n;
814
Vivien Didelot818be842017-01-27 15:29:38 -0500815 for (i = 0; i < ds->num_ports; ++i) {
816 ds->ports[i].index = i;
817 ds->ports[i].ds = ds;
818 }
819
Vivien Didelota0c02162017-01-27 15:29:36 -0500820 return ds;
821}
822EXPORT_SYMBOL_GPL(dsa_switch_alloc);
823
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400824int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200825{
826 int err;
827
828 mutex_lock(&dsa2_mutex);
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400829 err = _dsa_register_switch(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200830 mutex_unlock(&dsa2_mutex);
831
832 return err;
833}
834EXPORT_SYMBOL_GPL(dsa_register_switch);
835
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000836static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200837{
838 struct dsa_switch_tree *dst = ds->dst;
839
840 dsa_dst_unapply(dst);
841
842 dsa_dst_del_ds(dst, ds, ds->index);
843}
844
845void dsa_unregister_switch(struct dsa_switch *ds)
846{
847 mutex_lock(&dsa2_mutex);
848 _dsa_unregister_switch(ds);
849 mutex_unlock(&dsa2_mutex);
850}
851EXPORT_SYMBOL_GPL(dsa_unregister_switch);