blob: 6f5f0a2ad256df3202202337f2a4e9f104d43c75 [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>
16#include <linux/slab.h>
17#include <linux/rtnetlink.h>
18#include <net/dsa.h>
19#include <linux/of.h>
20#include <linux/of_net.h>
21#include "dsa_priv.h"
22
23static LIST_HEAD(dsa_switch_trees);
24static DEFINE_MUTEX(dsa2_mutex);
25
26static struct dsa_switch_tree *dsa_get_dst(u32 tree)
27{
28 struct dsa_switch_tree *dst;
29
30 list_for_each_entry(dst, &dsa_switch_trees, list)
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030031 if (dst->tree == tree) {
32 kref_get(&dst->refcount);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020033 return dst;
Nikita Yushchenko7a99cd62016-11-28 09:48:48 +030034 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +020035 return NULL;
36}
37
38static void dsa_free_dst(struct kref *ref)
39{
40 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
41 refcount);
42
43 list_del(&dst->list);
44 kfree(dst);
45}
46
47static void dsa_put_dst(struct dsa_switch_tree *dst)
48{
49 kref_put(&dst->refcount, dsa_free_dst);
50}
51
52static struct dsa_switch_tree *dsa_add_dst(u32 tree)
53{
54 struct dsa_switch_tree *dst;
55
56 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
57 if (!dst)
58 return NULL;
59 dst->tree = tree;
Andrew Lunn83c0afa2016-06-04 21:17:07 +020060 INIT_LIST_HEAD(&dst->list);
61 list_add_tail(&dsa_switch_trees, &dst->list);
62 kref_init(&dst->refcount);
63
64 return dst;
65}
66
67static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
68 struct dsa_switch *ds, u32 index)
69{
70 kref_get(&dst->refcount);
71 dst->ds[index] = ds;
72}
73
74static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
75 struct dsa_switch *ds, u32 index)
76{
77 dst->ds[index] = NULL;
78 kref_put(&dst->refcount, dsa_free_dst);
79}
80
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080081/* For platform data configurations, we need to have a valid name argument to
82 * differentiate a disabled port from an enabled one
83 */
Florian Fainelli293784a2017-01-26 10:45:52 -080084static bool dsa_port_is_valid(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020085{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080086 return !!(port->dn || port->name);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020087}
88
Florian Fainelli293784a2017-01-26 10:45:52 -080089static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020090{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080091 if (port->name && !strcmp(port->name, "dsa"))
92 return true;
93 else
94 return !!of_parse_phandle(port->dn, "link", 0);
Florian Fainelli293784a2017-01-26 10:45:52 -080095}
96
97static bool dsa_port_is_cpu(struct dsa_port *port)
98{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -080099 if (port->name && !strcmp(port->name, "cpu"))
100 return true;
101 else
102 return !!of_parse_phandle(port->dn, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200103}
104
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800105static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
106 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200107{
108 u32 index;
109
Vivien Didelot26895e22017-01-27 15:29:37 -0500110 for (index = 0; index < ds->num_ports; index++)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200111 if (ds->ports[index].dn == port)
112 return true;
113 return false;
114}
115
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800116static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
117 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200118{
119 struct dsa_switch *ds;
120 u32 index;
121
122 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
123 ds = dst->ds[index];
124 if (!ds)
125 continue;
126
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800127 if (dsa_ds_find_port_dn(ds, port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200128 return ds;
129 }
130
131 return NULL;
132}
133
134static int dsa_port_complete(struct dsa_switch_tree *dst,
135 struct dsa_switch *src_ds,
Florian Fainelli293784a2017-01-26 10:45:52 -0800136 struct dsa_port *port,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200137 u32 src_port)
138{
139 struct device_node *link;
140 int index;
141 struct dsa_switch *dst_ds;
142
143 for (index = 0;; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800144 link = of_parse_phandle(port->dn, "link", index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200145 if (!link)
146 break;
147
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800148 dst_ds = dsa_dst_find_port_dn(dst, link);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200149 of_node_put(link);
150
151 if (!dst_ds)
152 return 1;
153
154 src_ds->rtable[dst_ds->index] = src_port;
155 }
156
157 return 0;
158}
159
160/* A switch is complete if all the DSA ports phandles point to ports
161 * known in the tree. A return value of 1 means the tree is not
162 * complete. This is not an error condition. A value of 0 is
163 * success.
164 */
165static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
166{
Florian Fainelli293784a2017-01-26 10:45:52 -0800167 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200168 u32 index;
169 int err;
170
Vivien Didelot26895e22017-01-27 15:29:37 -0500171 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800172 port = &ds->ports[index];
173 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200174 continue;
175
176 if (!dsa_port_is_dsa(port))
177 continue;
178
179 err = dsa_port_complete(dst, ds, port, index);
180 if (err != 0)
181 return err;
182
183 ds->dsa_port_mask |= BIT(index);
184 }
185
186 return 0;
187}
188
189/* A tree is complete if all the DSA ports phandles point to ports
190 * known in the tree. A return value of 1 means the tree is not
191 * complete. This is not an error condition. A value of 0 is
192 * success.
193 */
194static int dsa_dst_complete(struct dsa_switch_tree *dst)
195{
196 struct dsa_switch *ds;
197 u32 index;
198 int err;
199
200 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
201 ds = dst->ds[index];
202 if (!ds)
203 continue;
204
205 err = dsa_ds_complete(dst, ds);
206 if (err != 0)
207 return err;
208 }
209
210 return 0;
211}
212
Florian Fainelli293784a2017-01-26 10:45:52 -0800213static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200214 struct dsa_switch *ds)
215{
216 int err;
217
218 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
219 if (err) {
220 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
221 index, err);
222 return err;
223 }
224
225 return 0;
226}
227
Florian Fainelli293784a2017-01-26 10:45:52 -0800228static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200229 struct dsa_switch *ds)
230{
231 dsa_cpu_dsa_destroy(port);
232}
233
Florian Fainelli293784a2017-01-26 10:45:52 -0800234static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200235 struct dsa_switch *ds)
236{
237 int err;
238
239 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
240 if (err) {
241 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
242 index, err);
243 return err;
244 }
245
246 ds->cpu_port_mask |= BIT(index);
247
248 return 0;
249}
250
Florian Fainelli293784a2017-01-26 10:45:52 -0800251static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200252 struct dsa_switch *ds)
253{
254 dsa_cpu_dsa_destroy(port);
255 ds->cpu_port_mask &= ~BIT(index);
256
257}
258
Florian Fainelli293784a2017-01-26 10:45:52 -0800259static int dsa_user_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200260 struct dsa_switch *ds)
261{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800262 const char *name = port->name;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200263 int err;
264
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800265 if (port->dn)
266 name = of_get_property(port->dn, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500267 if (!name)
268 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200269
270 err = dsa_slave_create(ds, ds->dev, index, name);
271 if (err) {
272 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
273 index, err);
274 return err;
275 }
276
277 return 0;
278}
279
Florian Fainelli293784a2017-01-26 10:45:52 -0800280static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200281 struct dsa_switch *ds)
282{
283 if (ds->ports[index].netdev) {
284 dsa_slave_destroy(ds->ports[index].netdev);
285 ds->ports[index].netdev = NULL;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700286 ds->enabled_port_mask &= ~(1 << index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200287 }
288}
289
290static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
291{
Florian Fainelli293784a2017-01-26 10:45:52 -0800292 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200293 u32 index;
294 int err;
295
Florian Fainelli6e830d82016-06-07 16:32:39 -0700296 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400297 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700298 * the slave MDIO bus driver rely on these values for probing PHY
299 * devices or not
300 */
301 ds->phys_mii_mask = ds->enabled_port_mask;
302
Vivien Didelot9d490b42016-08-23 12:38:56 -0400303 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200304 if (err < 0)
305 return err;
306
Vivien Didelotf515f192017-02-03 13:20:20 -0500307 err = dsa_switch_register_notifier(ds);
308 if (err)
309 return err;
310
John Crispin092183d2016-09-19 15:28:01 +0200311 if (ds->ops->set_addr) {
312 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
313 if (err < 0)
314 return err;
315 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200316
Vivien Didelot9d490b42016-08-23 12:38:56 -0400317 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700318 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
319 if (!ds->slave_mii_bus)
320 return -ENOMEM;
321
322 dsa_slave_mii_bus_init(ds);
323
324 err = mdiobus_register(ds->slave_mii_bus);
325 if (err < 0)
326 return err;
327 }
328
Vivien Didelot26895e22017-01-27 15:29:37 -0500329 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800330 port = &ds->ports[index];
331 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200332 continue;
333
334 if (dsa_port_is_dsa(port)) {
335 err = dsa_dsa_port_apply(port, index, ds);
336 if (err)
337 return err;
338 continue;
339 }
340
341 if (dsa_port_is_cpu(port)) {
342 err = dsa_cpu_port_apply(port, index, ds);
343 if (err)
344 return err;
345 continue;
346 }
347
348 err = dsa_user_port_apply(port, index, ds);
349 if (err)
350 continue;
351 }
352
353 return 0;
354}
355
356static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
357{
Florian Fainelli293784a2017-01-26 10:45:52 -0800358 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200359 u32 index;
360
Vivien Didelot26895e22017-01-27 15:29:37 -0500361 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800362 port = &ds->ports[index];
363 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200364 continue;
365
366 if (dsa_port_is_dsa(port)) {
367 dsa_dsa_port_unapply(port, index, ds);
368 continue;
369 }
370
371 if (dsa_port_is_cpu(port)) {
372 dsa_cpu_port_unapply(port, index, ds);
373 continue;
374 }
375
376 dsa_user_port_unapply(port, index, ds);
377 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700378
Vivien Didelot9d490b42016-08-23 12:38:56 -0400379 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700380 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500381
382 dsa_switch_unregister_notifier(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200383}
384
385static int dsa_dst_apply(struct dsa_switch_tree *dst)
386{
387 struct dsa_switch *ds;
388 u32 index;
389 int err;
390
391 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
392 ds = dst->ds[index];
393 if (!ds)
394 continue;
395
396 err = dsa_ds_apply(dst, ds);
397 if (err)
398 return err;
399 }
400
Vivien Didelot9520ed82017-01-17 20:41:39 -0500401 if (dst->cpu_switch) {
402 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800403 if (err)
404 return err;
405 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700406
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200407 /* If we use a tagging format that doesn't have an ethertype
408 * field, make sure that all packets from this point on get
409 * sent to the tag format's receive function.
410 */
411 wmb();
412 dst->master_netdev->dsa_ptr = (void *)dst;
413 dst->applied = true;
414
415 return 0;
416}
417
418static void dsa_dst_unapply(struct dsa_switch_tree *dst)
419{
420 struct dsa_switch *ds;
421 u32 index;
422
423 if (!dst->applied)
424 return;
425
426 dst->master_netdev->dsa_ptr = NULL;
427
428 /* If we used a tagging format that doesn't have an ethertype
429 * field, make sure that all packets from this point get sent
430 * without the tag and go through the regular receive path.
431 */
432 wmb();
433
434 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
435 ds = dst->ds[index];
436 if (!ds)
437 continue;
438
439 dsa_ds_unapply(dst, ds);
440 }
441
Vivien Didelot9520ed82017-01-17 20:41:39 -0500442 if (dst->cpu_switch)
443 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700444
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200445 pr_info("DSA: tree %d unapplied\n", dst->tree);
446 dst->applied = false;
447}
448
Florian Fainelli293784a2017-01-26 10:45:52 -0800449static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200450 struct dsa_switch_tree *dst,
451 struct dsa_switch *ds)
452{
Andrew Lunn7b314362016-08-22 16:01:01 +0200453 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200454 struct net_device *ethernet_dev;
455 struct device_node *ethernet;
456
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800457 if (port->dn) {
458 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
459 if (!ethernet)
460 return -EINVAL;
461 ethernet_dev = of_find_net_device_by_node(ethernet);
462 } else {
463 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
464 dev_put(ethernet_dev);
465 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200466
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200467 if (!ethernet_dev)
468 return -EPROBE_DEFER;
469
470 if (!ds->master_netdev)
471 ds->master_netdev = ethernet_dev;
472
473 if (!dst->master_netdev)
474 dst->master_netdev = ethernet_dev;
475
Vivien Didelotb22de492017-01-17 20:41:38 -0500476 if (!dst->cpu_switch) {
477 dst->cpu_switch = ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200478 dst->cpu_port = index;
479 }
480
Vivien Didelot9d490b42016-08-23 12:38:56 -0400481 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200482 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200483 if (IS_ERR(dst->tag_ops)) {
484 dev_warn(ds->dev, "No tagger for this switch\n");
485 return PTR_ERR(dst->tag_ops);
486 }
487
488 dst->rcv = dst->tag_ops->rcv;
489
490 return 0;
491}
492
493static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
494{
Florian Fainelli293784a2017-01-26 10:45:52 -0800495 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200496 u32 index;
497 int err;
498
Vivien Didelot26895e22017-01-27 15:29:37 -0500499 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800500 port = &ds->ports[index];
501 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200502 continue;
503
504 if (dsa_port_is_cpu(port)) {
505 err = dsa_cpu_parse(port, index, dst, ds);
506 if (err)
507 return err;
508 }
509 }
510
511 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
512
513 return 0;
514}
515
516static int dsa_dst_parse(struct dsa_switch_tree *dst)
517{
518 struct dsa_switch *ds;
519 u32 index;
520 int err;
521
522 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
523 ds = dst->ds[index];
524 if (!ds)
525 continue;
526
527 err = dsa_ds_parse(dst, ds);
528 if (err)
529 return err;
530 }
531
532 if (!dst->master_netdev) {
533 pr_warn("Tree has no master device\n");
534 return -EINVAL;
535 }
536
537 pr_info("DSA: tree %d parsed\n", dst->tree);
538
539 return 0;
540}
541
542static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
543{
544 struct device_node *port;
545 int err;
546 u32 reg;
547
548 for_each_available_child_of_node(ports, port) {
549 err = of_property_read_u32(port, "reg", &reg);
550 if (err)
551 return err;
552
Vivien Didelot26895e22017-01-27 15:29:37 -0500553 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200554 return -EINVAL;
555
556 ds->ports[reg].dn = port;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700557
Vivien Didelot9d490b42016-08-23 12:38:56 -0400558 /* Initialize enabled_port_mask now for ops->setup()
Florian Fainelli6e830d82016-06-07 16:32:39 -0700559 * to have access to a correct value, just like what
560 * net/dsa/dsa.c::dsa_switch_setup_one does.
561 */
Florian Fainelli293784a2017-01-26 10:45:52 -0800562 if (!dsa_port_is_cpu(&ds->ports[reg]))
Florian Fainelli6e830d82016-06-07 16:32:39 -0700563 ds->enabled_port_mask |= 1 << reg;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200564 }
565
566 return 0;
567}
568
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800569static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
570{
571 bool valid_name_found = false;
572 unsigned int i;
573
574 for (i = 0; i < DSA_MAX_PORTS; i++) {
575 if (!cd->port_names[i])
576 continue;
577
578 ds->ports[i].name = cd->port_names[i];
579
580 /* Initialize enabled_port_mask now for drv->setup()
581 * to have access to a correct value, just like what
582 * net/dsa/dsa.c::dsa_switch_setup_one does.
583 */
584 if (!dsa_port_is_cpu(&ds->ports[i]))
585 ds->enabled_port_mask |= 1 << i;
586
587 valid_name_found = true;
588 }
589
590 if (!valid_name_found && i == DSA_MAX_PORTS)
591 return -EINVAL;
592
593 return 0;
594}
595
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800596static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200597{
598 int err;
599
600 *tree = *index = 0;
601
602 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
603 if (err) {
604 /* Does not exist, but it is optional */
605 if (err == -EINVAL)
606 return 0;
607 return err;
608 }
609
610 err = of_property_read_u32_index(np, "dsa,member", 1, index);
611 if (err)
612 return err;
613
614 if (*index >= DSA_MAX_SWITCHES)
615 return -EINVAL;
616
617 return 0;
618}
619
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800620static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
621{
622 if (!pd)
623 return -ENODEV;
624
625 /* We do not support complex trees with dsa_chip_data */
626 *tree = 0;
627 *index = 0;
628
629 return 0;
630}
631
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200632static struct device_node *dsa_get_ports(struct dsa_switch *ds,
633 struct device_node *np)
634{
635 struct device_node *ports;
636
637 ports = of_get_child_by_name(np, "ports");
638 if (!ports) {
639 dev_err(ds->dev, "no ports child node found\n");
640 return ERR_PTR(-EINVAL);
641 }
642
643 return ports;
644}
645
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800646static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200647{
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800648 struct dsa_chip_data *pdata = dev->platform_data;
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800649 struct device_node *np = dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200650 struct dsa_switch_tree *dst;
Florian Fainellibc1727d2017-01-26 10:45:54 -0800651 struct device_node *ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200652 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400653 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200654
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800655 if (np) {
656 err = dsa_parse_member_dn(np, &tree, &index);
657 if (err)
658 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200659
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800660 ports = dsa_get_ports(ds, np);
661 if (IS_ERR(ports))
662 return PTR_ERR(ports);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200663
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800664 err = dsa_parse_ports_dn(ports, ds);
665 if (err)
666 return err;
667 } else {
668 err = dsa_parse_member(pdata, &tree, &index);
669 if (err)
670 return err;
671
672 err = dsa_parse_ports(pdata, ds);
673 if (err)
674 return err;
675 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200676
677 dst = dsa_get_dst(tree);
678 if (!dst) {
679 dst = dsa_add_dst(tree);
680 if (!dst)
681 return -ENOMEM;
682 }
683
684 if (dst->ds[index]) {
685 err = -EBUSY;
686 goto out;
687 }
688
689 ds->dst = dst;
690 ds->index = index;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800691 ds->cd = pdata;
Vivien Didelotd3902382016-07-06 20:03:54 -0400692
693 /* Initialize the routing table */
694 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
695 ds->rtable[i] = DSA_RTABLE_NONE;
696
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200697 dsa_dst_add_ds(dst, ds, index);
698
699 err = dsa_dst_complete(dst);
700 if (err < 0)
701 goto out_del_dst;
702
703 if (err == 1) {
704 /* Not all switches registered yet */
705 err = 0;
706 goto out;
707 }
708
709 if (dst->applied) {
710 pr_info("DSA: Disjoint trees?\n");
711 return -EINVAL;
712 }
713
714 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100715 if (err) {
716 if (err == -EPROBE_DEFER) {
717 dsa_dst_del_ds(dst, ds, ds->index);
718 return err;
719 }
720
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200721 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100722 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200723
724 err = dsa_dst_apply(dst);
725 if (err) {
726 dsa_dst_unapply(dst);
727 goto out_del_dst;
728 }
729
730 dsa_put_dst(dst);
731 return 0;
732
733out_del_dst:
734 dsa_dst_del_ds(dst, ds, ds->index);
735out:
736 dsa_put_dst(dst);
737
738 return err;
739}
740
Vivien Didelota0c02162017-01-27 15:29:36 -0500741struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
742{
743 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
744 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500745 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500746
747 ds = devm_kzalloc(dev, size, GFP_KERNEL);
748 if (!ds)
749 return NULL;
750
751 ds->dev = dev;
752 ds->num_ports = n;
753
Vivien Didelot818be842017-01-27 15:29:38 -0500754 for (i = 0; i < ds->num_ports; ++i) {
755 ds->ports[i].index = i;
756 ds->ports[i].ds = ds;
757 }
758
Vivien Didelota0c02162017-01-27 15:29:36 -0500759 return ds;
760}
761EXPORT_SYMBOL_GPL(dsa_switch_alloc);
762
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800763int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200764{
765 int err;
766
767 mutex_lock(&dsa2_mutex);
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800768 err = _dsa_register_switch(ds, dev);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200769 mutex_unlock(&dsa2_mutex);
770
771 return err;
772}
773EXPORT_SYMBOL_GPL(dsa_register_switch);
774
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000775static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200776{
777 struct dsa_switch_tree *dst = ds->dst;
778
779 dsa_dst_unapply(dst);
780
781 dsa_dst_del_ds(dst, ds, ds->index);
782}
783
784void dsa_unregister_switch(struct dsa_switch *ds)
785{
786 mutex_lock(&dsa2_mutex);
787 _dsa_unregister_switch(ds);
788 mutex_unlock(&dsa2_mutex);
789}
790EXPORT_SYMBOL_GPL(dsa_unregister_switch);