blob: cd13bb54a30c10040f196ae3b6f5d88a565b4153 [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{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080090 return !!(port->dn || port->name);
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{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080095 if (port->name && !strcmp(port->name, "dsa"))
96 return true;
97 else
98 return !!of_parse_phandle(port->dn, "link", 0);
Florian Fainelli293784a2017-01-26 10:45:52 -080099}
100
101static bool dsa_port_is_cpu(struct dsa_port *port)
102{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800103 if (port->name && !strcmp(port->name, "cpu"))
104 return true;
105 else
106 return !!of_parse_phandle(port->dn, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200107}
108
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800109static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
110 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200111{
112 u32 index;
113
Vivien Didelot26895e22017-01-27 15:29:37 -0500114 for (index = 0; index < ds->num_ports; index++)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200115 if (ds->ports[index].dn == port)
116 return true;
117 return false;
118}
119
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800120static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
121 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200122{
123 struct dsa_switch *ds;
124 u32 index;
125
126 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
127 ds = dst->ds[index];
128 if (!ds)
129 continue;
130
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800131 if (dsa_ds_find_port_dn(ds, port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200132 return ds;
133 }
134
135 return NULL;
136}
137
138static int dsa_port_complete(struct dsa_switch_tree *dst,
139 struct dsa_switch *src_ds,
Florian Fainelli293784a2017-01-26 10:45:52 -0800140 struct dsa_port *port,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200141 u32 src_port)
142{
143 struct device_node *link;
144 int index;
145 struct dsa_switch *dst_ds;
146
147 for (index = 0;; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800148 link = of_parse_phandle(port->dn, "link", index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200149 if (!link)
150 break;
151
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800152 dst_ds = dsa_dst_find_port_dn(dst, link);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200153 of_node_put(link);
154
155 if (!dst_ds)
156 return 1;
157
158 src_ds->rtable[dst_ds->index] = src_port;
159 }
160
161 return 0;
162}
163
164/* A switch is complete if all the DSA ports phandles point to ports
165 * known in the tree. A return value of 1 means the tree is not
166 * complete. This is not an error condition. A value of 0 is
167 * success.
168 */
169static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
170{
Florian Fainelli293784a2017-01-26 10:45:52 -0800171 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200172 u32 index;
173 int err;
174
Vivien Didelot26895e22017-01-27 15:29:37 -0500175 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800176 port = &ds->ports[index];
177 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200178 continue;
179
180 if (!dsa_port_is_dsa(port))
181 continue;
182
183 err = dsa_port_complete(dst, ds, port, index);
184 if (err != 0)
185 return err;
186
187 ds->dsa_port_mask |= BIT(index);
188 }
189
190 return 0;
191}
192
193/* A tree is complete if all the DSA ports phandles point to ports
194 * known in the tree. A return value of 1 means the tree is not
195 * complete. This is not an error condition. A value of 0 is
196 * success.
197 */
198static int dsa_dst_complete(struct dsa_switch_tree *dst)
199{
200 struct dsa_switch *ds;
201 u32 index;
202 int err;
203
204 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
205 ds = dst->ds[index];
206 if (!ds)
207 continue;
208
209 err = dsa_ds_complete(dst, ds);
210 if (err != 0)
211 return err;
212 }
213
214 return 0;
215}
216
Florian Fainellie41c1b52017-06-02 12:31:22 -0700217static int dsa_dsa_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200218{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700219 struct dsa_switch *ds = port->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200220 int err;
221
Florian Fainellie41c1b52017-06-02 12:31:22 -0700222 err = dsa_cpu_dsa_setup(ds, ds->dev, port, port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200223 if (err) {
224 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700225 port->index, err);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200226 return err;
227 }
228
Florian Fainellie41c1b52017-06-02 12:31:22 -0700229 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
Andrew Lunn96567d52017-03-28 23:45:07 +0200230
Florian Fainellie41c1b52017-06-02 12:31:22 -0700231 return devlink_port_register(ds->devlink, &port->devlink_port,
232 port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200233}
234
Florian Fainellie41c1b52017-06-02 12:31:22 -0700235static void dsa_dsa_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200236{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700237 devlink_port_unregister(&port->devlink_port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200238 dsa_cpu_dsa_destroy(port);
239}
240
Florian Fainellie41c1b52017-06-02 12:31:22 -0700241static int dsa_cpu_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200242{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700243 struct dsa_switch *ds = port->ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200244 int err;
245
Florian Fainellie41c1b52017-06-02 12:31:22 -0700246 err = dsa_cpu_dsa_setup(ds, ds->dev, port, port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200247 if (err) {
248 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700249 port->index, err);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200250 return err;
251 }
252
Florian Fainellie41c1b52017-06-02 12:31:22 -0700253 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
254 err = devlink_port_register(ds->devlink, &port->devlink_port,
255 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200256 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200257}
258
Florian Fainellie41c1b52017-06-02 12:31:22 -0700259static void dsa_cpu_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200260{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700261 devlink_port_unregister(&port->devlink_port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200262 dsa_cpu_dsa_destroy(port);
Florian Fainellie41c1b52017-06-02 12:31:22 -0700263 port->ds->cpu_port_mask &= ~BIT(port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200264
265}
266
Florian Fainellie41c1b52017-06-02 12:31:22 -0700267static int dsa_user_port_apply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200268{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700269 struct dsa_switch *ds = port->ds;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800270 const char *name = port->name;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200271 int err;
272
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800273 if (port->dn)
274 name = of_get_property(port->dn, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500275 if (!name)
276 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200277
Florian Fainellie41c1b52017-06-02 12:31:22 -0700278 err = dsa_slave_create(ds, ds->dev, port->index, name);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200279 if (err) {
280 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
Florian Fainellie41c1b52017-06-02 12:31:22 -0700281 port->index, err);
282 port->netdev = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200283 return err;
284 }
285
Florian Fainellie41c1b52017-06-02 12:31:22 -0700286 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
287 err = devlink_port_register(ds->devlink, &port->devlink_port,
288 port->index);
Andrew Lunn96567d52017-03-28 23:45:07 +0200289 if (err)
290 return err;
291
Florian Fainellie41c1b52017-06-02 12:31:22 -0700292 devlink_port_type_eth_set(&port->devlink_port, port->netdev);
Andrew Lunn96567d52017-03-28 23:45:07 +0200293
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200294 return 0;
295}
296
Florian Fainellie41c1b52017-06-02 12:31:22 -0700297static void dsa_user_port_unapply(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200298{
Florian Fainellie41c1b52017-06-02 12:31:22 -0700299 devlink_port_unregister(&port->devlink_port);
300 if (port->netdev) {
301 dsa_slave_destroy(port->netdev);
302 port->netdev = NULL;
303 port->ds->enabled_port_mask &= ~(1 << port->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200304 }
305}
306
307static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
308{
Florian Fainelli293784a2017-01-26 10:45:52 -0800309 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200310 u32 index;
311 int err;
312
Florian Fainelli6e830d82016-06-07 16:32:39 -0700313 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400314 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700315 * the slave MDIO bus driver rely on these values for probing PHY
316 * devices or not
317 */
318 ds->phys_mii_mask = ds->enabled_port_mask;
319
Andrew Lunn96567d52017-03-28 23:45:07 +0200320 /* Add the switch to devlink before calling setup, so that setup can
321 * add dpipe tables
322 */
323 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
324 if (!ds->devlink)
325 return -ENOMEM;
326
327 err = devlink_register(ds->devlink, ds->dev);
328 if (err)
329 return err;
330
Vivien Didelot9d490b42016-08-23 12:38:56 -0400331 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200332 if (err < 0)
333 return err;
334
Vivien Didelotf515f192017-02-03 13:20:20 -0500335 err = dsa_switch_register_notifier(ds);
336 if (err)
337 return err;
338
John Crispin092183d2016-09-19 15:28:01 +0200339 if (ds->ops->set_addr) {
340 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
341 if (err < 0)
342 return err;
343 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200344
Vivien Didelot9d490b42016-08-23 12:38:56 -0400345 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700346 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
347 if (!ds->slave_mii_bus)
348 return -ENOMEM;
349
350 dsa_slave_mii_bus_init(ds);
351
352 err = mdiobus_register(ds->slave_mii_bus);
353 if (err < 0)
354 return err;
355 }
356
Vivien Didelot26895e22017-01-27 15:29:37 -0500357 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800358 port = &ds->ports[index];
359 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200360 continue;
361
362 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700363 err = dsa_dsa_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200364 if (err)
365 return err;
366 continue;
367 }
368
369 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700370 err = dsa_cpu_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200371 if (err)
372 return err;
373 continue;
374 }
375
Florian Fainellie41c1b52017-06-02 12:31:22 -0700376 err = dsa_user_port_apply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200377 if (err)
378 continue;
379 }
380
381 return 0;
382}
383
384static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
385{
Florian Fainelli293784a2017-01-26 10:45:52 -0800386 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200387 u32 index;
388
Vivien Didelot26895e22017-01-27 15:29:37 -0500389 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800390 port = &ds->ports[index];
391 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200392 continue;
393
394 if (dsa_port_is_dsa(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700395 dsa_dsa_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200396 continue;
397 }
398
399 if (dsa_port_is_cpu(port)) {
Florian Fainellie41c1b52017-06-02 12:31:22 -0700400 dsa_cpu_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200401 continue;
402 }
403
Florian Fainellie41c1b52017-06-02 12:31:22 -0700404 dsa_user_port_unapply(port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200405 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700406
Vivien Didelot9d490b42016-08-23 12:38:56 -0400407 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700408 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500409
410 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200411
412 if (ds->devlink) {
413 devlink_unregister(ds->devlink);
414 devlink_free(ds->devlink);
415 ds->devlink = NULL;
416 }
417
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200418}
419
420static int dsa_dst_apply(struct dsa_switch_tree *dst)
421{
422 struct dsa_switch *ds;
423 u32 index;
424 int err;
425
426 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
427 ds = dst->ds[index];
428 if (!ds)
429 continue;
430
431 err = dsa_ds_apply(dst, ds);
432 if (err)
433 return err;
434 }
435
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400436 if (dst->cpu_dp) {
Florian Fainelli937c7df2017-06-02 12:31:21 -0700437 err = dsa_cpu_port_ethtool_setup(dst->cpu_dp);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800438 if (err)
439 return err;
440 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700441
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200442 /* If we use a tagging format that doesn't have an ethertype
443 * field, make sure that all packets from this point on get
444 * sent to the tag format's receive function.
445 */
446 wmb();
Vivien Didelot02f840c2017-06-01 16:07:12 -0400447 dst->master_netdev->dsa_ptr = dst;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200448 dst->applied = true;
449
450 return 0;
451}
452
453static void dsa_dst_unapply(struct dsa_switch_tree *dst)
454{
455 struct dsa_switch *ds;
456 u32 index;
457
458 if (!dst->applied)
459 return;
460
461 dst->master_netdev->dsa_ptr = NULL;
462
463 /* If we used a tagging format that doesn't have an ethertype
464 * field, make sure that all packets from this point get sent
465 * without the tag and go through the regular receive path.
466 */
467 wmb();
468
469 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
470 ds = dst->ds[index];
471 if (!ds)
472 continue;
473
474 dsa_ds_unapply(dst, ds);
475 }
476
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400477 if (dst->cpu_dp)
Florian Fainelli937c7df2017-06-02 12:31:21 -0700478 dsa_cpu_port_ethtool_restore(dst->cpu_dp);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700479
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200480 pr_info("DSA: tree %d unapplied\n", dst->tree);
481 dst->applied = false;
482}
483
Florian Fainelli293784a2017-01-26 10:45:52 -0800484static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200485 struct dsa_switch_tree *dst,
486 struct dsa_switch *ds)
487{
Andrew Lunn7b314362016-08-22 16:01:01 +0200488 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200489 struct net_device *ethernet_dev;
490 struct device_node *ethernet;
491
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800492 if (port->dn) {
493 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
494 if (!ethernet)
495 return -EINVAL;
496 ethernet_dev = of_find_net_device_by_node(ethernet);
497 } else {
498 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
499 dev_put(ethernet_dev);
500 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200501
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200502 if (!ethernet_dev)
503 return -EPROBE_DEFER;
504
505 if (!ds->master_netdev)
506 ds->master_netdev = ethernet_dev;
507
508 if (!dst->master_netdev)
509 dst->master_netdev = ethernet_dev;
510
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400511 if (!dst->cpu_dp)
512 dst->cpu_dp = port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200513
Vivien Didelot9d490b42016-08-23 12:38:56 -0400514 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200515 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200516 if (IS_ERR(dst->tag_ops)) {
517 dev_warn(ds->dev, "No tagger for this switch\n");
518 return PTR_ERR(dst->tag_ops);
519 }
520
521 dst->rcv = dst->tag_ops->rcv;
522
Florian Fainelli14be36c2017-06-02 12:31:23 -0700523 /* Initialize cpu_port_mask now for drv->setup()
524 * to have access to a correct value, just like what
525 * net/dsa/dsa.c::dsa_switch_setup_one does.
526 */
527 ds->cpu_port_mask |= BIT(index);
528
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200529 return 0;
530}
531
532static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
533{
Florian Fainelli293784a2017-01-26 10:45:52 -0800534 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200535 u32 index;
536 int err;
537
Vivien Didelot26895e22017-01-27 15:29:37 -0500538 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800539 port = &ds->ports[index];
Florian Fainelli14be36c2017-06-02 12:31:23 -0700540 if (!dsa_port_is_valid(port) ||
541 dsa_port_is_dsa(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200542 continue;
543
544 if (dsa_port_is_cpu(port)) {
545 err = dsa_cpu_parse(port, index, dst, ds);
546 if (err)
547 return err;
Florian Fainelli14be36c2017-06-02 12:31:23 -0700548 } else {
549 /* Initialize enabled_port_mask now for drv->setup()
550 * to have access to a correct value, just like what
551 * net/dsa/dsa.c::dsa_switch_setup_one does.
552 */
553 ds->enabled_port_mask |= BIT(index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200554 }
Florian Fainelli14be36c2017-06-02 12:31:23 -0700555
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200556 }
557
558 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
559
560 return 0;
561}
562
563static int dsa_dst_parse(struct dsa_switch_tree *dst)
564{
565 struct dsa_switch *ds;
566 u32 index;
567 int err;
568
569 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
570 ds = dst->ds[index];
571 if (!ds)
572 continue;
573
574 err = dsa_ds_parse(dst, ds);
575 if (err)
576 return err;
577 }
578
579 if (!dst->master_netdev) {
580 pr_warn("Tree has no master device\n");
581 return -EINVAL;
582 }
583
584 pr_info("DSA: tree %d parsed\n", dst->tree);
585
586 return 0;
587}
588
589static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
590{
591 struct device_node *port;
592 int err;
593 u32 reg;
594
595 for_each_available_child_of_node(ports, port) {
596 err = of_property_read_u32(port, "reg", &reg);
597 if (err)
598 return err;
599
Vivien Didelot26895e22017-01-27 15:29:37 -0500600 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200601 return -EINVAL;
602
603 ds->ports[reg].dn = port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200604 }
605
606 return 0;
607}
608
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800609static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
610{
611 bool valid_name_found = false;
612 unsigned int i;
613
614 for (i = 0; i < DSA_MAX_PORTS; i++) {
615 if (!cd->port_names[i])
616 continue;
617
618 ds->ports[i].name = cd->port_names[i];
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800619 valid_name_found = true;
620 }
621
622 if (!valid_name_found && i == DSA_MAX_PORTS)
623 return -EINVAL;
624
625 return 0;
626}
627
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800628static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200629{
630 int err;
631
632 *tree = *index = 0;
633
634 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
635 if (err) {
636 /* Does not exist, but it is optional */
637 if (err == -EINVAL)
638 return 0;
639 return err;
640 }
641
642 err = of_property_read_u32_index(np, "dsa,member", 1, index);
643 if (err)
644 return err;
645
646 if (*index >= DSA_MAX_SWITCHES)
647 return -EINVAL;
648
649 return 0;
650}
651
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800652static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
653{
654 if (!pd)
655 return -ENODEV;
656
657 /* We do not support complex trees with dsa_chip_data */
658 *tree = 0;
659 *index = 0;
660
661 return 0;
662}
663
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200664static struct device_node *dsa_get_ports(struct dsa_switch *ds,
665 struct device_node *np)
666{
667 struct device_node *ports;
668
669 ports = of_get_child_by_name(np, "ports");
670 if (!ports) {
671 dev_err(ds->dev, "no ports child node found\n");
672 return ERR_PTR(-EINVAL);
673 }
674
675 return ports;
676}
677
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400678static int _dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200679{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400680 struct dsa_chip_data *pdata = ds->dev->platform_data;
681 struct device_node *np = ds->dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200682 struct dsa_switch_tree *dst;
Florian Fainellibc1727d2017-01-26 10:45:54 -0800683 struct device_node *ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200684 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400685 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200686
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800687 if (np) {
688 err = dsa_parse_member_dn(np, &tree, &index);
689 if (err)
690 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200691
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800692 ports = dsa_get_ports(ds, np);
693 if (IS_ERR(ports))
694 return PTR_ERR(ports);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200695
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800696 err = dsa_parse_ports_dn(ports, ds);
697 if (err)
698 return err;
699 } else {
700 err = dsa_parse_member(pdata, &tree, &index);
701 if (err)
702 return err;
703
704 err = dsa_parse_ports(pdata, ds);
705 if (err)
706 return err;
707 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200708
709 dst = dsa_get_dst(tree);
710 if (!dst) {
711 dst = dsa_add_dst(tree);
712 if (!dst)
713 return -ENOMEM;
714 }
715
716 if (dst->ds[index]) {
717 err = -EBUSY;
718 goto out;
719 }
720
721 ds->dst = dst;
722 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800723 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400724
725 /* Initialize the routing table */
726 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
727 ds->rtable[i] = DSA_RTABLE_NONE;
728
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200729 dsa_dst_add_ds(dst, ds, index);
730
731 err = dsa_dst_complete(dst);
732 if (err < 0)
733 goto out_del_dst;
734
735 if (err == 1) {
736 /* Not all switches registered yet */
737 err = 0;
738 goto out;
739 }
740
741 if (dst->applied) {
742 pr_info("DSA: Disjoint trees?\n");
743 return -EINVAL;
744 }
745
746 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100747 if (err) {
748 if (err == -EPROBE_DEFER) {
749 dsa_dst_del_ds(dst, ds, ds->index);
750 return err;
751 }
752
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200753 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100754 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200755
756 err = dsa_dst_apply(dst);
757 if (err) {
758 dsa_dst_unapply(dst);
759 goto out_del_dst;
760 }
761
762 dsa_put_dst(dst);
763 return 0;
764
765out_del_dst:
766 dsa_dst_del_ds(dst, ds, ds->index);
767out:
768 dsa_put_dst(dst);
769
770 return err;
771}
772
Vivien Didelota0c02162017-01-27 15:29:36 -0500773struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
774{
775 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
776 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500777 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500778
779 ds = devm_kzalloc(dev, size, GFP_KERNEL);
780 if (!ds)
781 return NULL;
782
783 ds->dev = dev;
784 ds->num_ports = n;
785
Vivien Didelot818be842017-01-27 15:29:38 -0500786 for (i = 0; i < ds->num_ports; ++i) {
787 ds->ports[i].index = i;
788 ds->ports[i].ds = ds;
789 }
790
Vivien Didelota0c02162017-01-27 15:29:36 -0500791 return ds;
792}
793EXPORT_SYMBOL_GPL(dsa_switch_alloc);
794
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400795int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200796{
797 int err;
798
799 mutex_lock(&dsa2_mutex);
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400800 err = _dsa_register_switch(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200801 mutex_unlock(&dsa2_mutex);
802
803 return err;
804}
805EXPORT_SYMBOL_GPL(dsa_register_switch);
806
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000807static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200808{
809 struct dsa_switch_tree *dst = ds->dst;
810
811 dsa_dst_unapply(dst);
812
813 dsa_dst_del_ds(dst, ds, ds->index);
814}
815
816void dsa_unregister_switch(struct dsa_switch *ds)
817{
818 mutex_lock(&dsa2_mutex);
819 _dsa_unregister_switch(ds);
820 mutex_unlock(&dsa2_mutex);
821}
822EXPORT_SYMBOL_GPL(dsa_unregister_switch);