blob: 1c546b6621ee4545d15e68a16b4bcdb518a55eef [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 Fainelli293784a2017-01-26 10:45:52 -080081static bool dsa_port_is_valid(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020082{
Florian Fainelli293784a2017-01-26 10:45:52 -080083 return !!port->dn;
Andrew Lunn83c0afa2016-06-04 21:17:07 +020084}
85
Florian Fainelli293784a2017-01-26 10:45:52 -080086static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020087{
Florian Fainelli293784a2017-01-26 10:45:52 -080088 return !!of_parse_phandle(port->dn, "link", 0);
89}
90
91static bool dsa_port_is_cpu(struct dsa_port *port)
92{
93 return !!of_parse_phandle(port->dn, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020094}
95
Florian Fainelli3512a8e2017-01-26 10:45:53 -080096static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
97 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020098{
99 u32 index;
100
Vivien Didelot26895e22017-01-27 15:29:37 -0500101 for (index = 0; index < ds->num_ports; index++)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200102 if (ds->ports[index].dn == port)
103 return true;
104 return false;
105}
106
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800107static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
108 struct device_node *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200109{
110 struct dsa_switch *ds;
111 u32 index;
112
113 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
114 ds = dst->ds[index];
115 if (!ds)
116 continue;
117
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800118 if (dsa_ds_find_port_dn(ds, port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200119 return ds;
120 }
121
122 return NULL;
123}
124
125static int dsa_port_complete(struct dsa_switch_tree *dst,
126 struct dsa_switch *src_ds,
Florian Fainelli293784a2017-01-26 10:45:52 -0800127 struct dsa_port *port,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200128 u32 src_port)
129{
130 struct device_node *link;
131 int index;
132 struct dsa_switch *dst_ds;
133
134 for (index = 0;; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800135 link = of_parse_phandle(port->dn, "link", index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200136 if (!link)
137 break;
138
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800139 dst_ds = dsa_dst_find_port_dn(dst, link);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200140 of_node_put(link);
141
142 if (!dst_ds)
143 return 1;
144
145 src_ds->rtable[dst_ds->index] = src_port;
146 }
147
148 return 0;
149}
150
151/* A switch is complete if all the DSA ports phandles point to ports
152 * known in the tree. A return value of 1 means the tree is not
153 * complete. This is not an error condition. A value of 0 is
154 * success.
155 */
156static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
157{
Florian Fainelli293784a2017-01-26 10:45:52 -0800158 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200159 u32 index;
160 int err;
161
Vivien Didelot26895e22017-01-27 15:29:37 -0500162 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800163 port = &ds->ports[index];
164 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200165 continue;
166
167 if (!dsa_port_is_dsa(port))
168 continue;
169
170 err = dsa_port_complete(dst, ds, port, index);
171 if (err != 0)
172 return err;
173
174 ds->dsa_port_mask |= BIT(index);
175 }
176
177 return 0;
178}
179
180/* A tree is complete if all the DSA ports phandles point to ports
181 * known in the tree. A return value of 1 means the tree is not
182 * complete. This is not an error condition. A value of 0 is
183 * success.
184 */
185static int dsa_dst_complete(struct dsa_switch_tree *dst)
186{
187 struct dsa_switch *ds;
188 u32 index;
189 int err;
190
191 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
192 ds = dst->ds[index];
193 if (!ds)
194 continue;
195
196 err = dsa_ds_complete(dst, ds);
197 if (err != 0)
198 return err;
199 }
200
201 return 0;
202}
203
Florian Fainelli293784a2017-01-26 10:45:52 -0800204static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200205 struct dsa_switch *ds)
206{
207 int err;
208
209 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
210 if (err) {
211 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
212 index, err);
213 return err;
214 }
215
216 return 0;
217}
218
Florian Fainelli293784a2017-01-26 10:45:52 -0800219static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200220 struct dsa_switch *ds)
221{
222 dsa_cpu_dsa_destroy(port);
223}
224
Florian Fainelli293784a2017-01-26 10:45:52 -0800225static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200226 struct dsa_switch *ds)
227{
228 int err;
229
230 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
231 if (err) {
232 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
233 index, err);
234 return err;
235 }
236
237 ds->cpu_port_mask |= BIT(index);
238
239 return 0;
240}
241
Florian Fainelli293784a2017-01-26 10:45:52 -0800242static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200243 struct dsa_switch *ds)
244{
245 dsa_cpu_dsa_destroy(port);
246 ds->cpu_port_mask &= ~BIT(index);
247
248}
249
Florian Fainelli293784a2017-01-26 10:45:52 -0800250static int dsa_user_port_apply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200251 struct dsa_switch *ds)
252{
253 const char *name;
254 int err;
255
Florian Fainelli293784a2017-01-26 10:45:52 -0800256 name = of_get_property(port->dn, "label", NULL);
Vivien Didelot9f914842017-01-09 18:13:51 -0500257 if (!name)
258 name = "eth%d";
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200259
260 err = dsa_slave_create(ds, ds->dev, index, name);
261 if (err) {
262 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
263 index, err);
264 return err;
265 }
266
267 return 0;
268}
269
Florian Fainelli293784a2017-01-26 10:45:52 -0800270static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200271 struct dsa_switch *ds)
272{
273 if (ds->ports[index].netdev) {
274 dsa_slave_destroy(ds->ports[index].netdev);
275 ds->ports[index].netdev = NULL;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700276 ds->enabled_port_mask &= ~(1 << index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200277 }
278}
279
280static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
281{
Florian Fainelli293784a2017-01-26 10:45:52 -0800282 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200283 u32 index;
284 int err;
285
Florian Fainelli6e830d82016-06-07 16:32:39 -0700286 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400287 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700288 * the slave MDIO bus driver rely on these values for probing PHY
289 * devices or not
290 */
291 ds->phys_mii_mask = ds->enabled_port_mask;
292
Vivien Didelot9d490b42016-08-23 12:38:56 -0400293 err = ds->ops->setup(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200294 if (err < 0)
295 return err;
296
Vivien Didelotf515f192017-02-03 13:20:20 -0500297 err = dsa_switch_register_notifier(ds);
298 if (err)
299 return err;
300
John Crispin092183d2016-09-19 15:28:01 +0200301 if (ds->ops->set_addr) {
302 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
303 if (err < 0)
304 return err;
305 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200306
Vivien Didelot9d490b42016-08-23 12:38:56 -0400307 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700308 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
309 if (!ds->slave_mii_bus)
310 return -ENOMEM;
311
312 dsa_slave_mii_bus_init(ds);
313
314 err = mdiobus_register(ds->slave_mii_bus);
315 if (err < 0)
316 return err;
317 }
318
Vivien Didelot26895e22017-01-27 15:29:37 -0500319 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800320 port = &ds->ports[index];
321 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200322 continue;
323
324 if (dsa_port_is_dsa(port)) {
325 err = dsa_dsa_port_apply(port, index, ds);
326 if (err)
327 return err;
328 continue;
329 }
330
331 if (dsa_port_is_cpu(port)) {
332 err = dsa_cpu_port_apply(port, index, ds);
333 if (err)
334 return err;
335 continue;
336 }
337
338 err = dsa_user_port_apply(port, index, ds);
339 if (err)
340 continue;
341 }
342
343 return 0;
344}
345
346static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
347{
Florian Fainelli293784a2017-01-26 10:45:52 -0800348 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200349 u32 index;
350
Vivien Didelot26895e22017-01-27 15:29:37 -0500351 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800352 port = &ds->ports[index];
353 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200354 continue;
355
356 if (dsa_port_is_dsa(port)) {
357 dsa_dsa_port_unapply(port, index, ds);
358 continue;
359 }
360
361 if (dsa_port_is_cpu(port)) {
362 dsa_cpu_port_unapply(port, index, ds);
363 continue;
364 }
365
366 dsa_user_port_unapply(port, index, ds);
367 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700368
Vivien Didelot9d490b42016-08-23 12:38:56 -0400369 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700370 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500371
372 dsa_switch_unregister_notifier(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200373}
374
375static int dsa_dst_apply(struct dsa_switch_tree *dst)
376{
377 struct dsa_switch *ds;
378 u32 index;
379 int err;
380
381 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
382 ds = dst->ds[index];
383 if (!ds)
384 continue;
385
386 err = dsa_ds_apply(dst, ds);
387 if (err)
388 return err;
389 }
390
Vivien Didelot9520ed82017-01-17 20:41:39 -0500391 if (dst->cpu_switch) {
392 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800393 if (err)
394 return err;
395 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700396
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200397 /* If we use a tagging format that doesn't have an ethertype
398 * field, make sure that all packets from this point on get
399 * sent to the tag format's receive function.
400 */
401 wmb();
402 dst->master_netdev->dsa_ptr = (void *)dst;
403 dst->applied = true;
404
405 return 0;
406}
407
408static void dsa_dst_unapply(struct dsa_switch_tree *dst)
409{
410 struct dsa_switch *ds;
411 u32 index;
412
413 if (!dst->applied)
414 return;
415
416 dst->master_netdev->dsa_ptr = NULL;
417
418 /* If we used a tagging format that doesn't have an ethertype
419 * field, make sure that all packets from this point get sent
420 * without the tag and go through the regular receive path.
421 */
422 wmb();
423
424 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
425 ds = dst->ds[index];
426 if (!ds)
427 continue;
428
429 dsa_ds_unapply(dst, ds);
430 }
431
Vivien Didelot9520ed82017-01-17 20:41:39 -0500432 if (dst->cpu_switch)
433 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700434
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200435 pr_info("DSA: tree %d unapplied\n", dst->tree);
436 dst->applied = false;
437}
438
Florian Fainelli293784a2017-01-26 10:45:52 -0800439static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200440 struct dsa_switch_tree *dst,
441 struct dsa_switch *ds)
442{
Andrew Lunn7b314362016-08-22 16:01:01 +0200443 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200444 struct net_device *ethernet_dev;
445 struct device_node *ethernet;
446
Florian Fainelli293784a2017-01-26 10:45:52 -0800447 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200448 if (!ethernet)
449 return -EINVAL;
450
451 ethernet_dev = of_find_net_device_by_node(ethernet);
452 if (!ethernet_dev)
453 return -EPROBE_DEFER;
454
455 if (!ds->master_netdev)
456 ds->master_netdev = ethernet_dev;
457
458 if (!dst->master_netdev)
459 dst->master_netdev = ethernet_dev;
460
Vivien Didelotb22de492017-01-17 20:41:38 -0500461 if (!dst->cpu_switch) {
462 dst->cpu_switch = ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200463 dst->cpu_port = index;
464 }
465
Vivien Didelot9d490b42016-08-23 12:38:56 -0400466 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200467 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200468 if (IS_ERR(dst->tag_ops)) {
469 dev_warn(ds->dev, "No tagger for this switch\n");
470 return PTR_ERR(dst->tag_ops);
471 }
472
473 dst->rcv = dst->tag_ops->rcv;
474
475 return 0;
476}
477
478static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
479{
Florian Fainelli293784a2017-01-26 10:45:52 -0800480 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200481 u32 index;
482 int err;
483
Vivien Didelot26895e22017-01-27 15:29:37 -0500484 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800485 port = &ds->ports[index];
486 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200487 continue;
488
489 if (dsa_port_is_cpu(port)) {
490 err = dsa_cpu_parse(port, index, dst, ds);
491 if (err)
492 return err;
493 }
494 }
495
496 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
497
498 return 0;
499}
500
501static int dsa_dst_parse(struct dsa_switch_tree *dst)
502{
503 struct dsa_switch *ds;
504 u32 index;
505 int err;
506
507 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
508 ds = dst->ds[index];
509 if (!ds)
510 continue;
511
512 err = dsa_ds_parse(dst, ds);
513 if (err)
514 return err;
515 }
516
517 if (!dst->master_netdev) {
518 pr_warn("Tree has no master device\n");
519 return -EINVAL;
520 }
521
522 pr_info("DSA: tree %d parsed\n", dst->tree);
523
524 return 0;
525}
526
527static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
528{
529 struct device_node *port;
530 int err;
531 u32 reg;
532
533 for_each_available_child_of_node(ports, port) {
534 err = of_property_read_u32(port, "reg", &reg);
535 if (err)
536 return err;
537
Vivien Didelot26895e22017-01-27 15:29:37 -0500538 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200539 return -EINVAL;
540
541 ds->ports[reg].dn = port;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700542
Vivien Didelot9d490b42016-08-23 12:38:56 -0400543 /* Initialize enabled_port_mask now for ops->setup()
Florian Fainelli6e830d82016-06-07 16:32:39 -0700544 * to have access to a correct value, just like what
545 * net/dsa/dsa.c::dsa_switch_setup_one does.
546 */
Florian Fainelli293784a2017-01-26 10:45:52 -0800547 if (!dsa_port_is_cpu(&ds->ports[reg]))
Florian Fainelli6e830d82016-06-07 16:32:39 -0700548 ds->enabled_port_mask |= 1 << reg;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200549 }
550
551 return 0;
552}
553
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800554static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200555{
556 int err;
557
558 *tree = *index = 0;
559
560 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
561 if (err) {
562 /* Does not exist, but it is optional */
563 if (err == -EINVAL)
564 return 0;
565 return err;
566 }
567
568 err = of_property_read_u32_index(np, "dsa,member", 1, index);
569 if (err)
570 return err;
571
572 if (*index >= DSA_MAX_SWITCHES)
573 return -EINVAL;
574
575 return 0;
576}
577
578static struct device_node *dsa_get_ports(struct dsa_switch *ds,
579 struct device_node *np)
580{
581 struct device_node *ports;
582
583 ports = of_get_child_by_name(np, "ports");
584 if (!ports) {
585 dev_err(ds->dev, "no ports child node found\n");
586 return ERR_PTR(-EINVAL);
587 }
588
589 return ports;
590}
591
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800592static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200593{
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800594 struct device_node *np = dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200595 struct dsa_switch_tree *dst;
Florian Fainellibc1727d2017-01-26 10:45:54 -0800596 struct device_node *ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200597 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400598 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200599
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800600 err = dsa_parse_member_dn(np, &tree, &index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200601 if (err)
602 return err;
603
Florian Fainellibc1727d2017-01-26 10:45:54 -0800604 ports = dsa_get_ports(ds, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200605 if (IS_ERR(ports))
606 return PTR_ERR(ports);
607
608 err = dsa_parse_ports_dn(ports, ds);
609 if (err)
610 return err;
611
612 dst = dsa_get_dst(tree);
613 if (!dst) {
614 dst = dsa_add_dst(tree);
615 if (!dst)
616 return -ENOMEM;
617 }
618
619 if (dst->ds[index]) {
620 err = -EBUSY;
621 goto out;
622 }
623
624 ds->dst = dst;
625 ds->index = index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400626
627 /* Initialize the routing table */
628 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
629 ds->rtable[i] = DSA_RTABLE_NONE;
630
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200631 dsa_dst_add_ds(dst, ds, index);
632
633 err = dsa_dst_complete(dst);
634 if (err < 0)
635 goto out_del_dst;
636
637 if (err == 1) {
638 /* Not all switches registered yet */
639 err = 0;
640 goto out;
641 }
642
643 if (dst->applied) {
644 pr_info("DSA: Disjoint trees?\n");
645 return -EINVAL;
646 }
647
648 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100649 if (err) {
650 if (err == -EPROBE_DEFER) {
651 dsa_dst_del_ds(dst, ds, ds->index);
652 return err;
653 }
654
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200655 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100656 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200657
658 err = dsa_dst_apply(dst);
659 if (err) {
660 dsa_dst_unapply(dst);
661 goto out_del_dst;
662 }
663
664 dsa_put_dst(dst);
665 return 0;
666
667out_del_dst:
668 dsa_dst_del_ds(dst, ds, ds->index);
669out:
670 dsa_put_dst(dst);
671
672 return err;
673}
674
Vivien Didelota0c02162017-01-27 15:29:36 -0500675struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
676{
677 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
678 struct dsa_switch *ds;
Vivien Didelot818be842017-01-27 15:29:38 -0500679 int i;
Vivien Didelota0c02162017-01-27 15:29:36 -0500680
681 ds = devm_kzalloc(dev, size, GFP_KERNEL);
682 if (!ds)
683 return NULL;
684
685 ds->dev = dev;
686 ds->num_ports = n;
687
Vivien Didelot818be842017-01-27 15:29:38 -0500688 for (i = 0; i < ds->num_ports; ++i) {
689 ds->ports[i].index = i;
690 ds->ports[i].ds = ds;
691 }
692
Vivien Didelota0c02162017-01-27 15:29:36 -0500693 return ds;
694}
695EXPORT_SYMBOL_GPL(dsa_switch_alloc);
696
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800697int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200698{
699 int err;
700
701 mutex_lock(&dsa2_mutex);
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800702 err = _dsa_register_switch(ds, dev);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200703 mutex_unlock(&dsa2_mutex);
704
705 return err;
706}
707EXPORT_SYMBOL_GPL(dsa_register_switch);
708
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000709static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200710{
711 struct dsa_switch_tree *dst = ds->dst;
712
713 dsa_dst_unapply(dst);
714
715 dsa_dst_del_ds(dst, ds, ds->index);
716}
717
718void dsa_unregister_switch(struct dsa_switch *ds)
719{
720 mutex_lock(&dsa2_mutex);
721 _dsa_unregister_switch(ds);
722 mutex_unlock(&dsa2_mutex);
723}
724EXPORT_SYMBOL_GPL(dsa_unregister_switch);