blob: 6e7b3e88b77846c2bb007a5c3da69c192e9237d5 [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
John Crispin092183d2016-09-19 15:28:01 +0200297 if (ds->ops->set_addr) {
298 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
299 if (err < 0)
300 return err;
301 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200302
Vivien Didelot9d490b42016-08-23 12:38:56 -0400303 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700304 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
305 if (!ds->slave_mii_bus)
306 return -ENOMEM;
307
308 dsa_slave_mii_bus_init(ds);
309
310 err = mdiobus_register(ds->slave_mii_bus);
311 if (err < 0)
312 return err;
313 }
314
Vivien Didelot26895e22017-01-27 15:29:37 -0500315 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800316 port = &ds->ports[index];
317 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200318 continue;
319
320 if (dsa_port_is_dsa(port)) {
321 err = dsa_dsa_port_apply(port, index, ds);
322 if (err)
323 return err;
324 continue;
325 }
326
327 if (dsa_port_is_cpu(port)) {
328 err = dsa_cpu_port_apply(port, index, ds);
329 if (err)
330 return err;
331 continue;
332 }
333
334 err = dsa_user_port_apply(port, index, ds);
335 if (err)
336 continue;
337 }
338
339 return 0;
340}
341
342static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
343{
Florian Fainelli293784a2017-01-26 10:45:52 -0800344 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200345 u32 index;
346
Vivien Didelot26895e22017-01-27 15:29:37 -0500347 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800348 port = &ds->ports[index];
349 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200350 continue;
351
352 if (dsa_port_is_dsa(port)) {
353 dsa_dsa_port_unapply(port, index, ds);
354 continue;
355 }
356
357 if (dsa_port_is_cpu(port)) {
358 dsa_cpu_port_unapply(port, index, ds);
359 continue;
360 }
361
362 dsa_user_port_unapply(port, index, ds);
363 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700364
Vivien Didelot9d490b42016-08-23 12:38:56 -0400365 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700366 mdiobus_unregister(ds->slave_mii_bus);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200367}
368
369static int dsa_dst_apply(struct dsa_switch_tree *dst)
370{
371 struct dsa_switch *ds;
372 u32 index;
373 int err;
374
375 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
376 ds = dst->ds[index];
377 if (!ds)
378 continue;
379
380 err = dsa_ds_apply(dst, ds);
381 if (err)
382 return err;
383 }
384
Vivien Didelot9520ed82017-01-17 20:41:39 -0500385 if (dst->cpu_switch) {
386 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
Florian Fainellifaf3a932017-01-09 11:58:34 -0800387 if (err)
388 return err;
389 }
Florian Fainelli0c73c522016-06-07 16:32:42 -0700390
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200391 /* If we use a tagging format that doesn't have an ethertype
392 * field, make sure that all packets from this point on get
393 * sent to the tag format's receive function.
394 */
395 wmb();
396 dst->master_netdev->dsa_ptr = (void *)dst;
397 dst->applied = true;
398
399 return 0;
400}
401
402static void dsa_dst_unapply(struct dsa_switch_tree *dst)
403{
404 struct dsa_switch *ds;
405 u32 index;
406
407 if (!dst->applied)
408 return;
409
410 dst->master_netdev->dsa_ptr = NULL;
411
412 /* If we used a tagging format that doesn't have an ethertype
413 * field, make sure that all packets from this point get sent
414 * without the tag and go through the regular receive path.
415 */
416 wmb();
417
418 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
419 ds = dst->ds[index];
420 if (!ds)
421 continue;
422
423 dsa_ds_unapply(dst, ds);
424 }
425
Vivien Didelot9520ed82017-01-17 20:41:39 -0500426 if (dst->cpu_switch)
427 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700428
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200429 pr_info("DSA: tree %d unapplied\n", dst->tree);
430 dst->applied = false;
431}
432
Florian Fainelli293784a2017-01-26 10:45:52 -0800433static int dsa_cpu_parse(struct dsa_port *port, u32 index,
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200434 struct dsa_switch_tree *dst,
435 struct dsa_switch *ds)
436{
Andrew Lunn7b314362016-08-22 16:01:01 +0200437 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200438 struct net_device *ethernet_dev;
439 struct device_node *ethernet;
440
Florian Fainelli293784a2017-01-26 10:45:52 -0800441 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200442 if (!ethernet)
443 return -EINVAL;
444
445 ethernet_dev = of_find_net_device_by_node(ethernet);
446 if (!ethernet_dev)
447 return -EPROBE_DEFER;
448
449 if (!ds->master_netdev)
450 ds->master_netdev = ethernet_dev;
451
452 if (!dst->master_netdev)
453 dst->master_netdev = ethernet_dev;
454
Vivien Didelotb22de492017-01-17 20:41:38 -0500455 if (!dst->cpu_switch) {
456 dst->cpu_switch = ds;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200457 dst->cpu_port = index;
458 }
459
Vivien Didelot9d490b42016-08-23 12:38:56 -0400460 tag_protocol = ds->ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200461 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200462 if (IS_ERR(dst->tag_ops)) {
463 dev_warn(ds->dev, "No tagger for this switch\n");
464 return PTR_ERR(dst->tag_ops);
465 }
466
467 dst->rcv = dst->tag_ops->rcv;
468
469 return 0;
470}
471
472static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
473{
Florian Fainelli293784a2017-01-26 10:45:52 -0800474 struct dsa_port *port;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200475 u32 index;
476 int err;
477
Vivien Didelot26895e22017-01-27 15:29:37 -0500478 for (index = 0; index < ds->num_ports; index++) {
Florian Fainelli293784a2017-01-26 10:45:52 -0800479 port = &ds->ports[index];
480 if (!dsa_port_is_valid(port))
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200481 continue;
482
483 if (dsa_port_is_cpu(port)) {
484 err = dsa_cpu_parse(port, index, dst, ds);
485 if (err)
486 return err;
487 }
488 }
489
490 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
491
492 return 0;
493}
494
495static int dsa_dst_parse(struct dsa_switch_tree *dst)
496{
497 struct dsa_switch *ds;
498 u32 index;
499 int err;
500
501 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
502 ds = dst->ds[index];
503 if (!ds)
504 continue;
505
506 err = dsa_ds_parse(dst, ds);
507 if (err)
508 return err;
509 }
510
511 if (!dst->master_netdev) {
512 pr_warn("Tree has no master device\n");
513 return -EINVAL;
514 }
515
516 pr_info("DSA: tree %d parsed\n", dst->tree);
517
518 return 0;
519}
520
521static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
522{
523 struct device_node *port;
524 int err;
525 u32 reg;
526
527 for_each_available_child_of_node(ports, port) {
528 err = of_property_read_u32(port, "reg", &reg);
529 if (err)
530 return err;
531
Vivien Didelot26895e22017-01-27 15:29:37 -0500532 if (reg >= ds->num_ports)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200533 return -EINVAL;
534
535 ds->ports[reg].dn = port;
Florian Fainelli6e830d82016-06-07 16:32:39 -0700536
Vivien Didelot9d490b42016-08-23 12:38:56 -0400537 /* Initialize enabled_port_mask now for ops->setup()
Florian Fainelli6e830d82016-06-07 16:32:39 -0700538 * to have access to a correct value, just like what
539 * net/dsa/dsa.c::dsa_switch_setup_one does.
540 */
Florian Fainelli293784a2017-01-26 10:45:52 -0800541 if (!dsa_port_is_cpu(&ds->ports[reg]))
Florian Fainelli6e830d82016-06-07 16:32:39 -0700542 ds->enabled_port_mask |= 1 << reg;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200543 }
544
545 return 0;
546}
547
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800548static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200549{
550 int err;
551
552 *tree = *index = 0;
553
554 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
555 if (err) {
556 /* Does not exist, but it is optional */
557 if (err == -EINVAL)
558 return 0;
559 return err;
560 }
561
562 err = of_property_read_u32_index(np, "dsa,member", 1, index);
563 if (err)
564 return err;
565
566 if (*index >= DSA_MAX_SWITCHES)
567 return -EINVAL;
568
569 return 0;
570}
571
572static struct device_node *dsa_get_ports(struct dsa_switch *ds,
573 struct device_node *np)
574{
575 struct device_node *ports;
576
577 ports = of_get_child_by_name(np, "ports");
578 if (!ports) {
579 dev_err(ds->dev, "no ports child node found\n");
580 return ERR_PTR(-EINVAL);
581 }
582
583 return ports;
584}
585
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800586static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200587{
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800588 struct device_node *np = dev->of_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200589 struct dsa_switch_tree *dst;
Florian Fainellibc1727d2017-01-26 10:45:54 -0800590 struct device_node *ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200591 u32 tree, index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400592 int i, err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200593
Florian Fainelli3512a8e2017-01-26 10:45:53 -0800594 err = dsa_parse_member_dn(np, &tree, &index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200595 if (err)
596 return err;
597
Florian Fainellibc1727d2017-01-26 10:45:54 -0800598 ports = dsa_get_ports(ds, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200599 if (IS_ERR(ports))
600 return PTR_ERR(ports);
601
602 err = dsa_parse_ports_dn(ports, ds);
603 if (err)
604 return err;
605
606 dst = dsa_get_dst(tree);
607 if (!dst) {
608 dst = dsa_add_dst(tree);
609 if (!dst)
610 return -ENOMEM;
611 }
612
613 if (dst->ds[index]) {
614 err = -EBUSY;
615 goto out;
616 }
617
618 ds->dst = dst;
619 ds->index = index;
Vivien Didelotd3902382016-07-06 20:03:54 -0400620
621 /* Initialize the routing table */
622 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
623 ds->rtable[i] = DSA_RTABLE_NONE;
624
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200625 dsa_dst_add_ds(dst, ds, index);
626
627 err = dsa_dst_complete(dst);
628 if (err < 0)
629 goto out_del_dst;
630
631 if (err == 1) {
632 /* Not all switches registered yet */
633 err = 0;
634 goto out;
635 }
636
637 if (dst->applied) {
638 pr_info("DSA: Disjoint trees?\n");
639 return -EINVAL;
640 }
641
642 err = dsa_dst_parse(dst);
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100643 if (err) {
644 if (err == -EPROBE_DEFER) {
645 dsa_dst_del_ds(dst, ds, ds->index);
646 return err;
647 }
648
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200649 goto out_del_dst;
Volodymyr Bendiuga5e6eb452017-01-05 11:10:13 +0100650 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200651
652 err = dsa_dst_apply(dst);
653 if (err) {
654 dsa_dst_unapply(dst);
655 goto out_del_dst;
656 }
657
658 dsa_put_dst(dst);
659 return 0;
660
661out_del_dst:
662 dsa_dst_del_ds(dst, ds, ds->index);
663out:
664 dsa_put_dst(dst);
665
666 return err;
667}
668
Vivien Didelota0c02162017-01-27 15:29:36 -0500669struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
670{
671 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
672 struct dsa_switch *ds;
673
674 ds = devm_kzalloc(dev, size, GFP_KERNEL);
675 if (!ds)
676 return NULL;
677
678 ds->dev = dev;
679 ds->num_ports = n;
680
681 return ds;
682}
683EXPORT_SYMBOL_GPL(dsa_switch_alloc);
684
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800685int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200686{
687 int err;
688
689 mutex_lock(&dsa2_mutex);
Florian Fainelli55ed0ce2017-01-26 10:45:51 -0800690 err = _dsa_register_switch(ds, dev);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200691 mutex_unlock(&dsa2_mutex);
692
693 return err;
694}
695EXPORT_SYMBOL_GPL(dsa_register_switch);
696
Wei Yongjun85c22ba2016-07-12 15:24:10 +0000697static void _dsa_unregister_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200698{
699 struct dsa_switch_tree *dst = ds->dst;
700
701 dsa_dst_unapply(dst);
702
703 dsa_dst_del_ds(dst, ds, ds->index);
704}
705
706void dsa_unregister_switch(struct dsa_switch *ds)
707{
708 mutex_lock(&dsa2_mutex);
709 _dsa_unregister_switch(ds);
710 mutex_unlock(&dsa2_mutex);
711}
712EXPORT_SYMBOL_GPL(dsa_unregister_switch);