blob: 3c134ff26863f56e7515b0fb9705fa236bbd1ff6 [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
Vivien Didelot4cfbf092017-08-05 16:20:19 -0400263 err = dsa_slave_create(port, name);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200264 if (err) {
265 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700266 port->index, err);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400267 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200268 return err;
269 }
270
Florian Fainellie41c1b52017-06-02 12:31:22 -0700271 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
272 err = devlink_port_register(ds->devlink, &port->devlink_port,
273 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200274 if (err)
275 return err;
276
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400277 devlink_port_type_eth_set(&port->devlink_port, port->slave);
Andrew Lunn96567d52017-03-28 23:45:07 +0200278
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200279 return 0;
280}
281
Florian Fainellie41c1b52017-06-02 12:31:22 -0700282static void dsa_user_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200283{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700284 devlink_port_unregister(&port->devlink_port);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400285 if (port->slave) {
286 dsa_slave_destroy(port->slave);
287 port->slave = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200288 }
289}
290
291static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
292{
Florian Fainelli293784a2017-01-26 10:45:52 -0800293 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200294 u32 index;
295 int err;
296
Florian Fainelli6e830d82016-06-07 16:32:39 -0700297 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400298 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700299 * the slave MDIO bus driver rely on these values for probing PHY
300 * devices or not
301 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400302 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700303
Andrew Lunn96567d52017-03-28 23:45:07 +0200304 /* Add the switch to devlink before calling setup, so that setup can
305 * add dpipe tables
306 */
307 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
308 if (!ds->devlink)
309 return -ENOMEM;
310
311 err = devlink_register(ds->devlink, ds->dev);
312 if (err)
313 return err;
314
Vivien Didelot9d490b42016-08-23 12:38:56 -0400315 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200316 if (err < 0)
317 return err;
318
Vivien Didelotf515f192017-02-03 13:20:20 -0500319 err = dsa_switch_register_notifier(ds);
320 if (err)
321 return err;
322
Vivien Didelot9d490b42016-08-23 12:38:56 -0400323 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700324 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
325 if (!ds->slave_mii_bus)
326 return -ENOMEM;
327
328 dsa_slave_mii_bus_init(ds);
329
330 err = mdiobus_register(ds->slave_mii_bus);
331 if (err < 0)
332 return err;
333 }
334
Vivien Didelot26895e22017-01-27 15:29:37 -0500335 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800336 port = &ds->ports[index];
337 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200338 continue;
339
340 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700341 err = dsa_dsa_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200342 if (err)
343 return err;
344 continue;
345 }
346
347 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700348 err = dsa_cpu_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200349 if (err)
350 return err;
351 continue;
352 }
353
Florian Fainellie41c1b52017-06-02 12:31:22 -0700354 err = dsa_user_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200355 if (err)
356 continue;
357 }
358
359 return 0;
360}
361
362static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
363{
Florian Fainelli293784a2017-01-26 10:45:52 -0800364 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200365 u32 index;
366
Vivien Didelot26895e22017-01-27 15:29:37 -0500367 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800368 port = &ds->ports[index];
369 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200370 continue;
371
372 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700373 dsa_dsa_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200374 continue;
375 }
376
377 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700378 dsa_cpu_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200379 continue;
380 }
381
Florian Fainellie41c1b52017-06-02 12:31:22 -0700382 dsa_user_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200383 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700384
Vivien Didelot9d490b42016-08-23 12:38:56 -0400385 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700386 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500387
388 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200389
390 if (ds->devlink) {
391 devlink_unregister(ds->devlink);
392 devlink_free(ds->devlink);
393 ds->devlink = NULL;
394 }
395
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200396}
397
398static int dsa_dst_apply(struct dsa_switch_tree *dst)
399{
400 struct dsa_switch *ds;
401 u32 index;
402 int err;
403
404 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
405 ds = dst->ds[index];
406 if (!ds)
407 continue;
408
409 err = dsa_ds_apply(dst, ds);
410 if (err)
411 return err;
412 }
413
414 /* If we use a tagging format that doesn't have an ethertype
415 * field, make sure that all packets from this point on get
416 * sent to the tag format's receive function.
417 */
418 wmb();
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400419 dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
Vivien Didelot19435632017-09-19 11:56:59 -0400420
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400421 err = dsa_master_ethtool_setup(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400422 if (err)
423 return err;
424
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200425 dst->applied = true;
426
427 return 0;
428}
429
430static void dsa_dst_unapply(struct dsa_switch_tree *dst)
431{
432 struct dsa_switch *ds;
433 u32 index;
434
435 if (!dst->applied)
436 return;
437
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400438 dsa_master_ethtool_restore(dst->cpu_dp->master);
Vivien Didelot19435632017-09-19 11:56:59 -0400439
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400440 dst->cpu_dp->master->dsa_ptr = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200441
442 /* If we used a tagging format that doesn't have an ethertype
443 * field, make sure that all packets from this point get sent
444 * without the tag and go through the regular receive path.
445 */
446 wmb();
447
448 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
449 ds = dst->ds[index];
450 if (!ds)
451 continue;
452
453 dsa_ds_unapply(dst, ds);
454 }
455
Vivien Didelotcd8d7dd2017-09-19 11:56:58 -0400456 dst->cpu_dp = NULL;
Florian Fainelli0c73c522016-06-07 16:32:42 -0700457
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200458 pr_info("DSA: tree %d unapplied\n", dst->tree);
459 dst->applied = false;
460}
461
Florian Fainelli293784a2017-01-26 10:45:52 -0800462static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200463 struct dsa_switch_tree *dst,
464 struct dsa_switch *ds)
465{
Vivien Didelot62fc9582017-09-29 17:19:17 -0400466 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200467 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200468
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400469 if (!dst->cpu_dp)
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400470 dst->cpu_dp = port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200471
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700472 tag_protocol = ds->ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400473 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
474 if (IS_ERR(tag_ops)) {
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700475 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400476 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700477 }
478
Vivien Didelot15240242017-09-29 17:19:18 -0400479 dst->cpu_dp->tag_ops = tag_ops;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400480
481 /* Make a few copies for faster access in master receive hot path */
482 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400483 dst->cpu_dp->dst = dst;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700484
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200485 return 0;
486}
487
488static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
489{
Florian Fainelli293784a2017-01-26 10:45:52 -0800490 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200491 u32 index;
492 int err;
493
Vivien Didelot26895e22017-01-27 15:29:37 -0500494 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800495 port = &ds->ports[index];
Florian Fainelli14be36c2017-06-02 12:31:23 -0700496 if (!dsa_port_is_valid(port) ||
497 dsa_port_is_dsa(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200498 continue;
499
500 if (dsa_port_is_cpu(port)) {
501 err = dsa_cpu_parse(port, index, dst, ds);
502 if (err)
503 return err;
504 }
Florian Fainelli14be36c2017-06-02 12:31:23 -0700505
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200506 }
507
508 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
509
510 return 0;
511}
512
513static int dsa_dst_parse(struct dsa_switch_tree *dst)
514{
515 struct dsa_switch *ds;
Vivien Didelote4b77782017-06-15 15:06:54 -0400516 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200517 u32 index;
Vivien Didelote4b77782017-06-15 15:06:54 -0400518 int port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200519 int err;
520
521 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
522 ds = dst->ds[index];
523 if (!ds)
524 continue;
525
526 err = dsa_ds_parse(dst, ds);
527 if (err)
528 return err;
529 }
530
Florian Fainellic7848392017-08-28 17:10:51 -0700531 if (!dst->cpu_dp) {
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200532 pr_warn("Tree has no master device\n");
533 return -EINVAL;
534 }
535
Vivien Didelote4b77782017-06-15 15:06:54 -0400536 /* Assign the default CPU port to all ports of the fabric */
537 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
538 ds = dst->ds[index];
539 if (!ds)
540 continue;
541
542 for (port = 0; port < ds->num_ports; port++) {
543 dp = &ds->ports[port];
544 if (!dsa_port_is_valid(dp) ||
545 dsa_port_is_dsa(dp) ||
546 dsa_port_is_cpu(dp))
547 continue;
548
549 dp->cpu_dp = dst->cpu_dp;
550 }
551 }
552
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200553 pr_info("DSA: tree %d parsed\n", dst->tree);
554
555 return 0;
556}
557
Vivien Didelotfd223e22017-10-27 15:55:14 -0400558static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
559{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400560 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
561 struct device_node *link = of_parse_phandle(dn, "link", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400562 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400563
564 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400565 struct net_device *master;
566
567 master = of_find_net_device_by_node(ethernet);
568 if (!master)
569 return -EPROBE_DEFER;
570
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400571 dp->type = DSA_PORT_TYPE_CPU;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400572 dp->master = master;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400573 } else if (link) {
574 dp->type = DSA_PORT_TYPE_DSA;
575 } else {
Vivien Didelot1838fa82017-10-27 15:55:18 -0400576 if (!name)
577 name = "eth%d";
578
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400579 dp->type = DSA_PORT_TYPE_USER;
Vivien Didelot1838fa82017-10-27 15:55:18 -0400580 dp->name = name;
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400581 }
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);