blob: 033b3bfb63dc1887b15b3e08f00a00f70b706ec4 [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>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020021#include <net/dsa.h>
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 Fainelli293784a2017-01-26 10:45:52 -0800217static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200218 struct dsa_switch *ds)
219{
220 int err;
221
222 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
223 if (err) {
224 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
225 index, err);
226 return err;
227 }
228
Andrew Lunn96567d52017-03-28 23:45:07 +0200229 memset(&ds->ports[index].devlink_port, 0,
230 sizeof(ds->ports[index].devlink_port));
231
232 return devlink_port_register(ds->devlink,
233 &ds->ports[index].devlink_port,
234 index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200235}
236
Florian Fainelli293784a2017-01-26 10:45:52 -0800237static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200238 struct dsa_switch *ds)
239{
Andrew Lunn96567d52017-03-28 23:45:07 +0200240 devlink_port_unregister(&ds->ports[index].devlink_port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200241 dsa_cpu_dsa_destroy(port);
242}
243
Florian Fainelli293784a2017-01-26 10:45:52 -0800244static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200245 struct dsa_switch *ds)
246{
247 int err;
248
249 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
250 if (err) {
251 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
252 index, err);
253 return err;
254 }
255
256 ds->cpu_port_mask |= BIT(index);
257
Andrew Lunn96567d52017-03-28 23:45:07 +0200258 memset(&ds->ports[index].devlink_port, 0,
259 sizeof(ds->ports[index].devlink_port));
260 err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
261 index);
262 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200263}
264
Florian Fainelli293784a2017-01-26 10:45:52 -0800265static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200266 struct dsa_switch *ds)
267{
Andrew Lunn96567d52017-03-28 23:45:07 +0200268 devlink_port_unregister(&ds->ports[index].devlink_port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200269 dsa_cpu_dsa_destroy(port);
270 ds->cpu_port_mask &= ~BIT(index);
271
272}
273
Florian Fainelli293784a2017-01-26 10:45:52 -0800274static int dsa_user_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200275 struct dsa_switch *ds)
276{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800277 const char *name = port->name;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200278 int err;
279
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800280 if (port->dn)
281 name = of_get_property(port->dn, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500282 if (!name)
283 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200284
285 err = dsa_slave_create(ds, ds->dev, index, name);
286 if (err) {
287 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
288 index, err);
Florian Fainelli382e1ee2017-02-07 23:10:13 -0800289 ds->ports[index].netdev = NULL;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200290 return err;
291 }
292
Andrew Lunn96567d52017-03-28 23:45:07 +0200293 memset(&ds->ports[index].devlink_port, 0,
294 sizeof(ds->ports[index].devlink_port));
295 err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
296 index);
297 if (err)
298 return err;
299
300 devlink_port_type_eth_set(&ds->ports[index].devlink_port,
301 ds->ports[index].netdev);
302
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200303 return 0;
304}
305
Florian Fainelli293784a2017-01-26 10:45:52 -0800306static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200307 struct dsa_switch *ds)
308{
Andrew Lunn96567d52017-03-28 23:45:07 +0200309 devlink_port_unregister(&ds->ports[index].devlink_port);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200310 if (ds->ports[index].netdev) {
311 dsa_slave_destroy(ds->ports[index].netdev);
312 ds->ports[index].netdev = NULL;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700313 ds->enabled_port_mask &= ~(1 << index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200314 }
315}
316
317static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
318{
Florian Fainelli293784a2017-01-26 10:45:52 -0800319 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200320 u32 index;
321 int err;
322
Florian Fainelli6e830d82016-06-07 16:32:39 -0700323 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400324 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700325 * the slave MDIO bus driver rely on these values for probing PHY
326 * devices or not
327 */
328 ds->phys_mii_mask = ds->enabled_port_mask;
329
Andrew Lunn96567d52017-03-28 23:45:07 +0200330 /* Add the switch to devlink before calling setup, so that setup can
331 * add dpipe tables
332 */
333 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
334 if (!ds->devlink)
335 return -ENOMEM;
336
337 err = devlink_register(ds->devlink, ds->dev);
338 if (err)
339 return err;
340
Vivien Didelot9d490b42016-08-23 12:38:56 -0400341 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200342 if (err < 0)
343 return err;
344
Vivien Didelotf515f192017-02-03 13:20:20 -0500345 err = dsa_switch_register_notifier(ds);
346 if (err)
347 return err;
348
John Crispin092183d2016-09-19 15:28:01 +0200349 if (ds->ops->set_addr) {
350 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
351 if (err < 0)
352 return err;
353 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200354
Vivien Didelot9d490b42016-08-23 12:38:56 -0400355 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700356 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
357 if (!ds->slave_mii_bus)
358 return -ENOMEM;
359
360 dsa_slave_mii_bus_init(ds);
361
362 err = mdiobus_register(ds->slave_mii_bus);
363 if (err < 0)
364 return err;
365 }
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)) {
373 err = dsa_dsa_port_apply(port, index, ds);
374 if (err)
375 return err;
376 continue;
377 }
378
379 if (dsa_port_is_cpu(port)) {
380 err = dsa_cpu_port_apply(port, index, ds);
381 if (err)
382 return err;
383 continue;
384 }
385
386 err = dsa_user_port_apply(port, index, ds);
387 if (err)
388 continue;
389 }
390
391 return 0;
392}
393
394static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
395{
Florian Fainelli293784a2017-01-26 10:45:52 -0800396 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200397 u32 index;
398
Vivien Didelot26895e22017-01-27 15:29:37 -0500399 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800400 port = &ds->ports[index];
401 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200402 continue;
403
404 if (dsa_port_is_dsa(port)) {
405 dsa_dsa_port_unapply(port, index, ds);
406 continue;
407 }
408
409 if (dsa_port_is_cpu(port)) {
410 dsa_cpu_port_unapply(port, index, ds);
411 continue;
412 }
413
414 dsa_user_port_unapply(port, index, ds);
415 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700416
Vivien Didelot9d490b42016-08-23 12:38:56 -0400417 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700418 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500419
420 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200421
422 if (ds->devlink) {
423 devlink_unregister(ds->devlink);
424 devlink_free(ds->devlink);
425 ds->devlink = NULL;
426 }
427
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200428}
429
430static int dsa_dst_apply(struct dsa_switch_tree *dst)
431{
432 struct dsa_switch *ds;
433 u32 index;
434 int err;
435
436 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
437 ds = dst->ds[index];
438 if (!ds)
439 continue;
440
441 err = dsa_ds_apply(dst, ds);
442 if (err)
443 return err;
444 }
445
Vivien Didelot9520ed82017-01-17 20:41:39 -0500446 if (dst->cpu_switch) {
447 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800448 if (err)
449 return err;
450 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700451
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200452 /* If we use a tagging format that doesn't have an ethertype
453 * field, make sure that all packets from this point on get
454 * sent to the tag format's receive function.
455 */
456 wmb();
457 dst->master_netdev->dsa_ptr = (void *)dst;
458 dst->applied = true;
459
460 return 0;
461}
462
463static void dsa_dst_unapply(struct dsa_switch_tree *dst)
464{
465 struct dsa_switch *ds;
466 u32 index;
467
468 if (!dst->applied)
469 return;
470
471 dst->master_netdev->dsa_ptr = NULL;
472
473 /* If we used a tagging format that doesn't have an ethertype
474 * field, make sure that all packets from this point get sent
475 * without the tag and go through the regular receive path.
476 */
477 wmb();
478
479 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
480 ds = dst->ds[index];
481 if (!ds)
482 continue;
483
484 dsa_ds_unapply(dst, ds);
485 }
486
Vivien Didelot9520ed82017-01-17 20:41:39 -0500487 if (dst->cpu_switch)
488 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700489
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200490 pr_info("DSA: tree %d unapplied\n", dst->tree);
491 dst->applied = false;
492}
493
Florian Fainelli293784a2017-01-26 10:45:52 -0800494static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200495 struct dsa_switch_tree *dst,
496 struct dsa_switch *ds)
497{
Andrew Lunn7b314362016-08-22 16:01:01 +0200498 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200499 struct net_device *ethernet_dev;
500 struct device_node *ethernet;
501
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800502 if (port->dn) {
503 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
504 if (!ethernet)
505 return -EINVAL;
506 ethernet_dev = of_find_net_device_by_node(ethernet);
507 } else {
508 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
509 dev_put(ethernet_dev);
510 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200511
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200512 if (!ethernet_dev)
513 return -EPROBE_DEFER;
514
515 if (!ds->master_netdev)
516 ds->master_netdev = ethernet_dev;
517
518 if (!dst->master_netdev)
519 dst->master_netdev = ethernet_dev;
520
Vivien Didelotb22de492017-01-17 20:41:38 -0500521 if (!dst->cpu_switch) {
522 dst->cpu_switch = ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200523 dst->cpu_port = index;
524 }
525
Vivien Didelot9d490b42016-08-23 12:38:56 -0400526 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200527 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200528 if (IS_ERR(dst->tag_ops)) {
529 dev_warn(ds->dev, "No tagger for this switch\n");
530 return PTR_ERR(dst->tag_ops);
531 }
532
533 dst->rcv = dst->tag_ops->rcv;
534
535 return 0;
536}
537
538static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
539{
Florian Fainelli293784a2017-01-26 10:45:52 -0800540 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200541 u32 index;
542 int err;
543
Vivien Didelot26895e22017-01-27 15:29:37 -0500544 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800545 port = &ds->ports[index];
546 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200547 continue;
548
549 if (dsa_port_is_cpu(port)) {
550 err = dsa_cpu_parse(port, index, dst, ds);
551 if (err)
552 return err;
553 }
554 }
555
556 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
557
558 return 0;
559}
560
561static int dsa_dst_parse(struct dsa_switch_tree *dst)
562{
563 struct dsa_switch *ds;
564 u32 index;
565 int err;
566
567 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
568 ds = dst->ds[index];
569 if (!ds)
570 continue;
571
572 err = dsa_ds_parse(dst, ds);
573 if (err)
574 return err;
575 }
576
577 if (!dst->master_netdev) {
578 pr_warn("Tree has no master device\n");
579 return -EINVAL;
580 }
581
582 pr_info("DSA: tree %d parsed\n", dst->tree);
583
584 return 0;
585}
586
587static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
588{
589 struct device_node *port;
590 int err;
591 u32 reg;
592
593 for_each_available_child_of_node(ports, port) {
594 err = of_property_read_u32(port, "reg", &reg);
595 if (err)
596 return err;
597
Vivien Didelot26895e22017-01-27 15:29:37 -0500598 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200599 return -EINVAL;
600
601 ds->ports[reg].dn = port;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700602
Vivien Didelot9d490b42016-08-23 12:38:56 -0400603 /* Initialize enabled_port_mask now for ops->setup()
Florian Fainelli6e830d82016-06-07 16:32:39 -0700604 * to have access to a correct value, just like what
605 * net/dsa/dsa.c::dsa_switch_setup_one does.
606 */
Florian Fainelli293784a2017-01-26 10:45:52 -0800607 if (!dsa_port_is_cpu(&ds->ports[reg]))
Florian Fainelli6e830d82016-06-07 16:32:39 -0700608 ds->enabled_port_mask |= 1 << reg;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200609 }
610
611 return 0;
612}
613
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800614static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
615{
616 bool valid_name_found = false;
617 unsigned int i;
618
619 for (i = 0; i < DSA_MAX_PORTS; i++) {
620 if (!cd->port_names[i])
621 continue;
622
623 ds->ports[i].name = cd->port_names[i];
624
625 /* Initialize enabled_port_mask now for drv->setup()
626 * to have access to a correct value, just like what
627 * net/dsa/dsa.c::dsa_switch_setup_one does.
628 */
629 if (!dsa_port_is_cpu(&ds->ports[i]))
630 ds->enabled_port_mask |= 1 << i;
631
632 valid_name_found = true;
633 }
634
635 if (!valid_name_found && i == DSA_MAX_PORTS)
636 return -EINVAL;
637
638 return 0;
639}
640
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800641static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200642{
643 int err;
644
645 *tree = *index = 0;
646
647 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
648 if (err) {
649 /* Does not exist, but it is optional */
650 if (err == -EINVAL)
651 return 0;
652 return err;
653 }
654
655 err = of_property_read_u32_index(np, "dsa,member", 1, index);
656 if (err)
657 return err;
658
659 if (*index >= DSA_MAX_SWITCHES)
660 return -EINVAL;
661
662 return 0;
663}
664
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800665static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
666{
667 if (!pd)
668 return -ENODEV;
669
670 /* We do not support complex trees with dsa_chip_data */
671 *tree = 0;
672 *index = 0;
673
674 return 0;
675}
676
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200677static struct device_node *dsa_get_ports(struct dsa_switch *ds,
678 struct device_node *np)
679{
680 struct device_node *ports;
681
682 ports = of_get_child_by_name(np, "ports");
683 if (!ports) {
684 dev_err(ds->dev, "no ports child node found\n");
685 return ERR_PTR(-EINVAL);
686 }
687
688 return ports;
689}
690
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800691static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200692{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800693 struct dsa_chip_data *pdata = dev->platform_data;
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800694 struct device_node *np = dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200695 struct dsa_switch_tree *dst;
Florian Fainellibc1727d2017-01-26 10:45:54 -0800696 struct device_node *ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200697 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400698 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200699
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800700 if (np) {
701 err = dsa_parse_member_dn(np, &tree, &index);
702 if (err)
703 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200704
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800705 ports = dsa_get_ports(ds, np);
706 if (IS_ERR(ports))
707 return PTR_ERR(ports);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200708
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800709 err = dsa_parse_ports_dn(ports, ds);
710 if (err)
711 return err;
712 } else {
713 err = dsa_parse_member(pdata, &tree, &index);
714 if (err)
715 return err;
716
717 err = dsa_parse_ports(pdata, ds);
718 if (err)
719 return err;
720 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200721
722 dst = dsa_get_dst(tree);
723 if (!dst) {
724 dst = dsa_add_dst(tree);
725 if (!dst)
726 return -ENOMEM;
727 }
728
729 if (dst->ds[index]) {
730 err = -EBUSY;
731 goto out;
732 }
733
734 ds->dst = dst;
735 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800736 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400737
738 /* Initialize the routing table */
739 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
740 ds->rtable[i] = DSA_RTABLE_NONE;
741
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200742 dsa_dst_add_ds(dst, ds, index);
743
744 err = dsa_dst_complete(dst);
745 if (err < 0)
746 goto out_del_dst;
747
748 if (err == 1) {
749 /* Not all switches registered yet */
750 err = 0;
751 goto out;
752 }
753
754 if (dst->applied) {
755 pr_info("DSA: Disjoint trees?\n");
756 return -EINVAL;
757 }
758
759 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100760 if (err) {
761 if (err == -EPROBE_DEFER) {
762 dsa_dst_del_ds(dst, ds, ds->index);
763 return err;
764 }
765
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200766 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100767 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200768
769 err = dsa_dst_apply(dst);
770 if (err) {
771 dsa_dst_unapply(dst);
772 goto out_del_dst;
773 }
774
775 dsa_put_dst(dst);
776 return 0;
777
778out_del_dst:
779 dsa_dst_del_ds(dst, ds, ds->index);
780out:
781 dsa_put_dst(dst);
782
783 return err;
784}
785
Vivien Didelota0c02162017-01-27 15:29:36 -0500786struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
787{
788 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
789 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500790 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500791
792 ds = devm_kzalloc(dev, size, GFP_KERNEL);
793 if (!ds)
794 return NULL;
795
796 ds->dev = dev;
797 ds->num_ports = n;
798
Vivien Didelot818be842017-01-27 15:29:38 -0500799 for (i = 0; i < ds->num_ports; ++i) {
800 ds->ports[i].index = i;
801 ds->ports[i].ds = ds;
802 }
803
Vivien Didelota0c02162017-01-27 15:29:36 -0500804 return ds;
805}
806EXPORT_SYMBOL_GPL(dsa_switch_alloc);
807
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800808int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200809{
810 int err;
811
812 mutex_lock(&dsa2_mutex);
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800813 err = _dsa_register_switch(ds, dev);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200814 mutex_unlock(&dsa2_mutex);
815
816 return err;
817}
818EXPORT_SYMBOL_GPL(dsa_register_switch);
819
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000820static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200821{
822 struct dsa_switch_tree *dst = ds->dst;
823
824 dsa_dst_unapply(dst);
825
826 dsa_dst_del_ds(dst, ds, ds->index);
827}
828
829void dsa_unregister_switch(struct dsa_switch *ds)
830{
831 mutex_lock(&dsa2_mutex);
832 _dsa_unregister_switch(ds);
833 mutex_unlock(&dsa2_mutex);
834}
835EXPORT_SYMBOL_GPL(dsa_unregister_switch);