blob: a4de7ff8b19bb43bd860dcd9b12164091db8154a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Lunn83c0afa2016-06-04 21:17:07 +02002/*
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
Andrew Lunn83c0afa2016-06-04 21:17:07 +02007 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/list.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020012#include <linux/netdevice.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020013#include <linux/slab.h>
14#include <linux/rtnetlink.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020015#include <linux/of.h>
16#include <linux/of_net.h>
Jiri Pirko402f99e52019-03-24 11:14:26 +010017#include <net/devlink.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040018
Andrew Lunn83c0afa2016-06-04 21:17:07 +020019#include "dsa_priv.h"
20
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040021static LIST_HEAD(dsa_tree_list);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020022static DEFINE_MUTEX(dsa2_mutex);
23
Andrew Lunn96567d52017-03-28 23:45:07 +020024static const struct devlink_ops dsa_devlink_ops = {
25};
26
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040027static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020028{
29 struct dsa_switch_tree *dst;
30
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040031 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -040032 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020033 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -040034
Andrew Lunn83c0afa2016-06-04 21:17:07 +020035 return NULL;
36}
37
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040038static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020039{
40 struct dsa_switch_tree *dst;
41
42 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
43 if (!dst)
44 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040045
Vivien Didelot49463b72017-11-03 19:05:21 -040046 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040047
Vivien Didelotab8ccae2019-10-21 16:51:16 -040048 INIT_LIST_HEAD(&dst->ports);
49
Andrew Lunn83c0afa2016-06-04 21:17:07 +020050 INIT_LIST_HEAD(&dst->list);
Vivien Didelot50c7d2ba2019-10-18 17:02:46 -040051 list_add_tail(&dst->list, &dsa_tree_list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -040052
Andrew Lunn83c0afa2016-06-04 21:17:07 +020053 kref_init(&dst->refcount);
54
55 return dst;
56}
57
Vivien Didelot65254102017-11-03 19:05:23 -040058static void dsa_tree_free(struct dsa_switch_tree *dst)
59{
60 list_del(&dst->list);
61 kfree(dst);
62}
63
Vivien Didelot9e741042017-11-24 11:36:06 -050064static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
65{
66 if (dst)
67 kref_get(&dst->refcount);
68
69 return dst;
70}
71
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040072static struct dsa_switch_tree *dsa_tree_touch(int index)
73{
74 struct dsa_switch_tree *dst;
75
76 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -050077 if (dst)
78 return dsa_tree_get(dst);
79 else
80 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -040081}
82
83static void dsa_tree_release(struct kref *ref)
84{
85 struct dsa_switch_tree *dst;
86
87 dst = container_of(ref, struct dsa_switch_tree, refcount);
88
89 dsa_tree_free(dst);
90}
91
92static void dsa_tree_put(struct dsa_switch_tree *dst)
93{
Vivien Didelot9e741042017-11-24 11:36:06 -050094 if (dst)
95 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -040096}
97
Florian Fainelli293784a2017-01-26 10:45:52 -080098static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +020099{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400100 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800101}
102
103static bool dsa_port_is_cpu(struct dsa_port *port)
104{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400105 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200106}
107
Vivien Didelotf0704642017-11-06 16:11:44 -0500108static bool dsa_port_is_user(struct dsa_port *dp)
109{
110 return dp->type == DSA_PORT_TYPE_USER;
111}
112
Vivien Didelotf163da82017-11-06 16:11:49 -0500113static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
114 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200115{
Vivien Didelotf163da82017-11-06 16:11:49 -0500116 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200117
Vivien Didelot764b7e62019-10-21 16:51:21 -0400118 list_for_each_entry(dp, &dst->ports, list)
119 if (dp->dn == dn)
120 return dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200121
122 return NULL;
123}
124
Vivien Didelot34c09a82017-11-06 16:11:51 -0500125static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200126{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500127 struct dsa_switch *ds = dp->ds;
128 struct dsa_switch_tree *dst = ds->dst;
129 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500130 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500131 struct dsa_port *link_dp;
Vivien Didelotc5286662017-11-06 16:11:50 -0500132 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200133
Vivien Didelotc5286662017-11-06 16:11:50 -0500134 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
135 link_dp = dsa_tree_find_port_by_node(dst, it.node);
136 if (!link_dp) {
137 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500138 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500139 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200140
Vivien Didelot34c09a82017-11-06 16:11:51 -0500141 ds->rtable[link_dp->ds->index] = dp->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200142 }
143
Vivien Didelot34c09a82017-11-06 16:11:51 -0500144 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200145}
146
Vivien Didelot34c09a82017-11-06 16:11:51 -0500147static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200148{
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400149 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500150 bool complete = true;
151 struct dsa_port *dp;
152 int i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200153
Vivien Didelot34c09a82017-11-06 16:11:51 -0500154 for (i = 0; i < DSA_MAX_SWITCHES; i++)
155 ds->rtable[i] = DSA_RTABLE_NONE;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200156
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400157 list_for_each_entry(dp, &dst->ports, list) {
158 if (dp->ds == ds && dsa_port_is_dsa(dp)) {
Vivien Didelot34c09a82017-11-06 16:11:51 -0500159 complete = dsa_port_setup_routing_table(dp);
160 if (!complete)
161 break;
162 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200163 }
164
Vivien Didelot34c09a82017-11-06 16:11:51 -0500165 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200166}
167
Vivien Didelot34c09a82017-11-06 16:11:51 -0500168static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200169{
170 struct dsa_switch *ds;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500171 bool complete = true;
172 int device;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200173
Vivien Didelot34c09a82017-11-06 16:11:51 -0500174 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
175 ds = dst->ds[device];
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200176 if (!ds)
177 continue;
178
Vivien Didelot34c09a82017-11-06 16:11:51 -0500179 complete = dsa_switch_setup_routing_table(ds);
180 if (!complete)
181 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200182 }
183
Vivien Didelot34c09a82017-11-06 16:11:51 -0500184 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200185}
186
Vivien Didelotf0704642017-11-06 16:11:44 -0500187static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
188{
189 struct dsa_switch *ds;
190 struct dsa_port *dp;
191 int device, port;
192
193 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
194 ds = dst->ds[device];
195 if (!ds)
196 continue;
197
198 for (port = 0; port < ds->num_ports; port++) {
199 dp = &ds->ports[port];
200
201 if (dsa_port_is_cpu(dp))
202 return dp;
203 }
204 }
205
206 return NULL;
207}
208
209static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
210{
211 struct dsa_switch *ds;
212 struct dsa_port *dp;
213 int device, port;
214
215 /* DSA currently only supports a single CPU port */
216 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
217 if (!dst->cpu_dp) {
218 pr_warn("Tree has no master device\n");
219 return -EINVAL;
220 }
221
222 /* Assign the default CPU port to all ports of the fabric */
223 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
224 ds = dst->ds[device];
225 if (!ds)
226 continue;
227
228 for (port = 0; port < ds->num_ports; port++) {
229 dp = &ds->ports[port];
230
Vivien Didelot986d7cc2017-12-05 15:34:12 -0500231 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
Vivien Didelotf0704642017-11-06 16:11:44 -0500232 dp->cpu_dp = dst->cpu_dp;
233 }
234 }
235
236 return 0;
237}
238
239static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
240{
241 /* DSA currently only supports a single CPU port */
242 dst->cpu_dp = NULL;
243}
244
Vivien Didelot1d277322017-11-06 16:11:48 -0500245static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200246{
Vivien Didelot1d277322017-11-06 16:11:48 -0500247 struct dsa_switch *ds = dp->ds;
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200248 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot955222c2019-08-19 16:00:48 -0400249 const unsigned char *id = (const unsigned char *)&dst->index;
250 const unsigned char len = sizeof(dst->index);
251 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300252 bool dsa_port_link_registered = false;
253 bool devlink_port_registered = false;
Vivien Didelot955222c2019-08-19 16:00:48 -0400254 struct devlink *dl = ds->devlink;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300255 bool dsa_port_enabled = false;
256 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200257
Vivien Didelotfb35c602019-10-21 16:51:19 -0400258 if (dp->setup)
259 return 0;
260
Vivien Didelot1d277322017-11-06 16:11:48 -0500261 switch (dp->type) {
262 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400263 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500264 break;
265 case DSA_PORT_TYPE_CPU:
Vivien Didelot955222c2019-08-19 16:00:48 -0400266 memset(dlp, 0, sizeof(*dlp));
267 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_CPU,
268 dp->index, false, 0, id, len);
269 err = devlink_port_register(dl, dlp, dp->index);
270 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300271 break;
272 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400273
Jiri Pirkoda077392018-05-18 09:29:03 +0200274 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300275 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300276 break;
277 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400278
279 err = dsa_port_enable(dp, NULL);
280 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300281 break;
282 dsa_port_enabled = true;
283
Jiri Pirkoda077392018-05-18 09:29:03 +0200284 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500285 case DSA_PORT_TYPE_DSA:
Vivien Didelot955222c2019-08-19 16:00:48 -0400286 memset(dlp, 0, sizeof(*dlp));
287 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_DSA,
288 dp->index, false, 0, id, len);
289 err = devlink_port_register(dl, dlp, dp->index);
290 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300291 break;
292 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400293
Sebastian Reichel33615362018-01-23 16:03:46 +0100294 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300295 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300296 break;
297 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400298
299 err = dsa_port_enable(dp, NULL);
300 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300301 break;
302 dsa_port_enabled = true;
303
Vivien Didelot1d277322017-11-06 16:11:48 -0500304 break;
305 case DSA_PORT_TYPE_USER:
Vivien Didelot955222c2019-08-19 16:00:48 -0400306 memset(dlp, 0, sizeof(*dlp));
307 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_PHYSICAL,
308 dp->index, false, 0, id, len);
309 err = devlink_port_register(dl, dlp, dp->index);
310 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300311 break;
312 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400313
314 dp->mac = of_get_mac_address(dp->dn);
Vivien Didelot1d277322017-11-06 16:11:48 -0500315 err = dsa_slave_create(dp);
316 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300317 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400318
319 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500320 break;
321 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200322
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300323 if (err && dsa_port_enabled)
324 dsa_port_disable(dp);
325 if (err && dsa_port_link_registered)
326 dsa_port_link_unregister_of(dp);
327 if (err && devlink_port_registered)
328 devlink_port_unregister(dlp);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400329 if (err)
330 return err;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300331
Vivien Didelotfb35c602019-10-21 16:51:19 -0400332 dp->setup = true;
333
334 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200335}
336
Vivien Didelot1d277322017-11-06 16:11:48 -0500337static void dsa_port_teardown(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200338{
Vivien Didelot955222c2019-08-19 16:00:48 -0400339 struct devlink_port *dlp = &dp->devlink_port;
Vivien Didelot1d277322017-11-06 16:11:48 -0500340
Vivien Didelotfb35c602019-10-21 16:51:19 -0400341 if (!dp->setup)
342 return;
343
Vivien Didelot1d277322017-11-06 16:11:48 -0500344 switch (dp->type) {
345 case DSA_PORT_TYPE_UNUSED:
346 break;
347 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400348 dsa_port_disable(dp);
Andrew Lunn4dad81e2019-04-28 19:37:19 +0200349 dsa_tag_driver_put(dp->tag_ops);
Vivien Didelot955222c2019-08-19 16:00:48 -0400350 devlink_port_unregister(dlp);
351 dsa_port_link_unregister_of(dp);
352 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500353 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400354 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400355 devlink_port_unregister(dlp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100356 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500357 break;
358 case DSA_PORT_TYPE_USER:
Vivien Didelot955222c2019-08-19 16:00:48 -0400359 devlink_port_unregister(dlp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500360 if (dp->slave) {
361 dsa_slave_destroy(dp->slave);
362 dp->slave = NULL;
363 }
364 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200365 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400366
367 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200368}
369
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500370static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200371{
Vivien Didelotfb35c602019-10-21 16:51:19 -0400372 int err;
373
374 if (ds->setup)
375 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200376
Florian Fainelli6e830d82016-06-07 16:32:39 -0700377 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400378 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700379 * the slave MDIO bus driver rely on these values for probing PHY
380 * devices or not
381 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400382 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700383
Andrew Lunn96567d52017-03-28 23:45:07 +0200384 /* Add the switch to devlink before calling setup, so that setup can
385 * add dpipe tables
386 */
387 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
388 if (!ds->devlink)
389 return -ENOMEM;
390
391 err = devlink_register(ds->devlink, ds->dev);
392 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300393 goto free_devlink;
Andrew Lunn96567d52017-03-28 23:45:07 +0200394
Vivien Didelotf515f192017-02-03 13:20:20 -0500395 err = dsa_switch_register_notifier(ds);
396 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300397 goto unregister_devlink;
Vivien Didelotf515f192017-02-03 13:20:20 -0500398
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300399 err = ds->ops->setup(ds);
400 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300401 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300402
Vivien Didelot9d490b42016-08-23 12:38:56 -0400403 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700404 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300405 if (!ds->slave_mii_bus) {
406 err = -ENOMEM;
407 goto unregister_notifier;
408 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700409
410 dsa_slave_mii_bus_init(ds);
411
412 err = mdiobus_register(ds->slave_mii_bus);
413 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300414 goto unregister_notifier;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700415 }
416
Vivien Didelotfb35c602019-10-21 16:51:19 -0400417 ds->setup = true;
418
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200419 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300420
421unregister_notifier:
422 dsa_switch_unregister_notifier(ds);
423unregister_devlink:
424 devlink_unregister(ds->devlink);
425free_devlink:
426 devlink_free(ds->devlink);
427 ds->devlink = NULL;
428
429 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200430}
431
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500432static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200433{
Vivien Didelotfb35c602019-10-21 16:51:19 -0400434 if (!ds->setup)
435 return;
436
Vivien Didelot9d490b42016-08-23 12:38:56 -0400437 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700438 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500439
440 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200441
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300442 if (ds->ops->teardown)
443 ds->ops->teardown(ds);
444
Andrew Lunn96567d52017-03-28 23:45:07 +0200445 if (ds->devlink) {
446 devlink_unregister(ds->devlink);
447 devlink_free(ds->devlink);
448 ds->devlink = NULL;
449 }
450
Vivien Didelotfb35c602019-10-21 16:51:19 -0400451 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200452}
453
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500454static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
455{
Vivien Didelot1d277322017-11-06 16:11:48 -0500456 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400457 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500458
Vivien Didelotfb35c602019-10-21 16:51:19 -0400459 list_for_each_entry(dp, &dst->ports, list) {
460 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500461 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400462 goto teardown;
463 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500464
Vivien Didelotfb35c602019-10-21 16:51:19 -0400465 list_for_each_entry(dp, &dst->ports, list) {
466 err = dsa_port_setup(dp);
467 if (err)
468 goto teardown;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500469 }
470
471 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300472
Vivien Didelotfb35c602019-10-21 16:51:19 -0400473teardown:
474 list_for_each_entry(dp, &dst->ports, list)
475 dsa_port_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300476
Vivien Didelotfb35c602019-10-21 16:51:19 -0400477 list_for_each_entry(dp, &dst->ports, list)
478 dsa_switch_teardown(dp->ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300479
480 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500481}
482
483static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
484{
Vivien Didelot1d277322017-11-06 16:11:48 -0500485 struct dsa_port *dp;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500486
Vivien Didelotfb35c602019-10-21 16:51:19 -0400487 list_for_each_entry(dp, &dst->ports, list)
488 dsa_port_teardown(dp);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500489
Vivien Didelotfb35c602019-10-21 16:51:19 -0400490 list_for_each_entry(dp, &dst->ports, list)
491 dsa_switch_teardown(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500492}
493
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500494static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
495{
496 struct dsa_port *cpu_dp = dst->cpu_dp;
497 struct net_device *master = cpu_dp->master;
498
499 /* DSA currently supports a single pair of CPU port and master device */
500 return dsa_master_setup(master, cpu_dp);
501}
502
503static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
504{
505 struct dsa_port *cpu_dp = dst->cpu_dp;
506 struct net_device *master = cpu_dp->master;
507
508 return dsa_master_teardown(master);
509}
510
Vivien Didelotec15dd42017-11-06 16:11:46 -0500511static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200512{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500513 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200514 int err;
515
Vivien Didelotec15dd42017-11-06 16:11:46 -0500516 if (dst->setup) {
517 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
518 dst->index);
519 return -EEXIST;
520 }
521
Vivien Didelot34c09a82017-11-06 16:11:51 -0500522 complete = dsa_tree_setup_routing_table(dst);
523 if (!complete)
524 return 0;
525
Vivien Didelotf0704642017-11-06 16:11:44 -0500526 err = dsa_tree_setup_default_cpu(dst);
527 if (err)
528 return err;
529
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500530 err = dsa_tree_setup_switches(dst);
531 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300532 goto teardown_default_cpu;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200533
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500534 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400535 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300536 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -0400537
Vivien Didelotec15dd42017-11-06 16:11:46 -0500538 dst->setup = true;
539
540 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200541
542 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300543
544teardown_switches:
545 dsa_tree_teardown_switches(dst);
546teardown_default_cpu:
547 dsa_tree_teardown_default_cpu(dst);
548
549 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200550}
551
Vivien Didelotec15dd42017-11-06 16:11:46 -0500552static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200553{
Vivien Didelotec15dd42017-11-06 16:11:46 -0500554 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200555 return;
556
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500557 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200558
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500559 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200560
Vivien Didelotf0704642017-11-06 16:11:44 -0500561 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700562
Vivien Didelotec15dd42017-11-06 16:11:46 -0500563 pr_info("DSA: tree %d torn down\n", dst->index);
564
565 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200566}
567
Vivien Didelot6da2a942017-11-03 19:05:25 -0400568static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
569 unsigned int index)
570{
Vivien Didelot30817352017-11-06 16:11:52 -0500571 dsa_tree_teardown(dst);
572
Vivien Didelot6da2a942017-11-03 19:05:25 -0400573 dst->ds[index] = NULL;
574 dsa_tree_put(dst);
575}
576
577static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
578 struct dsa_switch *ds)
579{
580 unsigned int index = ds->index;
Vivien Didelot30817352017-11-06 16:11:52 -0500581 int err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400582
583 if (dst->ds[index])
584 return -EBUSY;
585
586 dsa_tree_get(dst);
587 dst->ds[index] = ds;
588
Vivien Didelot30817352017-11-06 16:11:52 -0500589 err = dsa_tree_setup(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300590 if (err) {
591 dst->ds[index] = NULL;
592 dsa_tree_put(dst);
593 }
Vivien Didelot30817352017-11-06 16:11:52 -0500594
595 return err;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400596}
597
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400598static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
599{
600 struct dsa_switch_tree *dst = ds->dst;
601 struct dsa_port *dp;
602
603 dp = &ds->ports[index];
604
605 dp->ds = ds;
606 dp->index = index;
607
608 INIT_LIST_HEAD(&dp->list);
609 list_add_tail(&dp->list, &dst->ports);
610
611 return dp;
612}
613
Vivien Didelot06e24d02017-11-03 19:05:29 -0400614static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
615{
616 if (!name)
617 name = "eth%d";
618
619 dp->type = DSA_PORT_TYPE_USER;
620 dp->name = name;
621
622 return 0;
623}
624
625static int dsa_port_parse_dsa(struct dsa_port *dp)
626{
627 dp->type = DSA_PORT_TYPE_DSA;
628
629 return 0;
630}
631
632static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
633{
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400634 struct dsa_switch *ds = dp->ds;
635 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400636 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200637 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200638
Florian Fainelli5ed4e3e2017-11-10 15:22:52 -0800639 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200640 tag_ops = dsa_tag_driver_get(tag_protocol);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400641 if (IS_ERR(tag_ops)) {
Andrew Lunn23426a22019-09-12 15:16:45 +0200642 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
643 return -EPROBE_DEFER;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700644 dev_warn(ds->dev, "No tagger for this switch\n");
Vivien Didelot62fc9582017-09-29 17:19:17 -0400645 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700646 }
647
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400648 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Olteancc1939e2019-05-05 13:19:23 +0300649 dp->filter = tag_ops->filter;
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400650 dp->rcv = tag_ops->rcv;
651 dp->tag_ops = tag_ops;
652 dp->master = master;
653 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400654
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400655 return 0;
656}
657
Vivien Didelotfd223e22017-10-27 15:55:14 -0400658static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
659{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400660 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400661 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -0400662 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400663
Vivien Didelot06e24d02017-11-03 19:05:29 -0400664 dp->dn = dn;
665
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400666 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400667 struct net_device *master;
668
669 master = of_find_net_device_by_node(ethernet);
670 if (!master)
671 return -EPROBE_DEFER;
672
Vivien Didelot06e24d02017-11-03 19:05:29 -0400673 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400674 }
675
Vivien Didelot06e24d02017-11-03 19:05:29 -0400676 if (link)
677 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400678
Vivien Didelot06e24d02017-11-03 19:05:29 -0400679 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400680}
681
Vivien Didelot975e6e32017-11-03 19:05:27 -0400682static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
683 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200684{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400685 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400686 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +0800687 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200688 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400689
690 ports = of_get_child_by_name(dn, "ports");
691 if (!ports) {
692 dev_err(ds->dev, "no ports child node found\n");
693 return -EINVAL;
694 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200695
696 for_each_available_child_of_node(ports, port) {
697 err = of_property_read_u32(port, "reg", &reg);
698 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800699 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200700
Wen Yang9919a362019-02-25 15:22:19 +0800701 if (reg >= ds->num_ports) {
702 err = -EINVAL;
703 goto out_put_node;
704 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200705
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400706 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400707
708 err = dsa_port_parse_of(dp, port);
709 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800710 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200711 }
712
Wen Yang9919a362019-02-25 15:22:19 +0800713out_put_node:
714 of_node_put(ports);
715 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200716}
717
Vivien Didelot975e6e32017-11-03 19:05:27 -0400718static int dsa_switch_parse_member_of(struct dsa_switch *ds,
719 struct device_node *dn)
720{
721 u32 m[2] = { 0, 0 };
722 int sz;
723
724 /* Don't error out if this optional property isn't found */
725 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
726 if (sz < 0 && sz != -EINVAL)
727 return sz;
728
729 ds->index = m[1];
730 if (ds->index >= DSA_MAX_SWITCHES)
731 return -EINVAL;
732
733 ds->dst = dsa_tree_touch(m[0]);
734 if (!ds->dst)
735 return -ENOMEM;
736
737 return 0;
738}
739
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400740static int dsa_switch_touch_ports(struct dsa_switch *ds)
741{
742 struct dsa_port *dp;
743 int port;
744
745 for (port = 0; port < ds->num_ports; port++) {
746 dp = dsa_port_touch(ds, port);
747 if (!dp)
748 return -ENOMEM;
749 }
750
751 return 0;
752}
753
Vivien Didelot975e6e32017-11-03 19:05:27 -0400754static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
755{
756 int err;
757
758 err = dsa_switch_parse_member_of(ds, dn);
759 if (err)
760 return err;
761
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400762 err = dsa_switch_touch_ports(ds);
763 if (err)
764 return err;
765
Vivien Didelot975e6e32017-11-03 19:05:27 -0400766 return dsa_switch_parse_ports_of(ds, dn);
767}
768
Vivien Didelotfd223e22017-10-27 15:55:14 -0400769static int dsa_port_parse(struct dsa_port *dp, const char *name,
770 struct device *dev)
771{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400772 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400773 struct net_device *master;
774
775 master = dsa_dev_to_net_device(dev);
776 if (!master)
777 return -EPROBE_DEFER;
778
779 dev_put(master);
780
Vivien Didelot06e24d02017-11-03 19:05:29 -0400781 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400782 }
783
Vivien Didelot06e24d02017-11-03 19:05:29 -0400784 if (!strcmp(name, "dsa"))
785 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400786
Vivien Didelot06e24d02017-11-03 19:05:29 -0400787 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400788}
789
Vivien Didelot975e6e32017-11-03 19:05:27 -0400790static int dsa_switch_parse_ports(struct dsa_switch *ds,
791 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800792{
793 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400794 struct dsa_port *dp;
795 struct device *dev;
796 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800797 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400798 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800799
800 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400801 name = cd->port_names[i];
802 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400803 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400804
805 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800806 continue;
807
Vivien Didelotfd223e22017-10-27 15:55:14 -0400808 err = dsa_port_parse(dp, name, dev);
809 if (err)
810 return err;
811
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800812 valid_name_found = true;
813 }
814
815 if (!valid_name_found && i == DSA_MAX_PORTS)
816 return -EINVAL;
817
818 return 0;
819}
820
Vivien Didelot975e6e32017-11-03 19:05:27 -0400821static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200822{
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400823 int err;
824
Vivien Didelot975e6e32017-11-03 19:05:27 -0400825 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200826
Vivien Didelot975e6e32017-11-03 19:05:27 -0400827 /* We don't support interconnected switches nor multiple trees via
828 * platform data, so this is the unique switch of the tree.
829 */
830 ds->index = 0;
831 ds->dst = dsa_tree_touch(0);
832 if (!ds->dst)
833 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200834
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400835 err = dsa_switch_touch_ports(ds);
836 if (err)
837 return err;
838
Vivien Didelot975e6e32017-11-03 19:05:27 -0400839 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800840}
841
Vivien Didelot30817352017-11-06 16:11:52 -0500842static int dsa_switch_add(struct dsa_switch *ds)
843{
844 struct dsa_switch_tree *dst = ds->dst;
845
846 return dsa_tree_add_switch(dst, ds);
847}
848
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500849static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200850{
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400851 struct dsa_chip_data *pdata = ds->dev->platform_data;
852 struct device_node *np = ds->dev->of_node;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500853 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200854
Vivien Didelot975e6e32017-11-03 19:05:27 -0400855 if (np)
856 err = dsa_switch_parse_of(ds, np);
857 else if (pdata)
858 err = dsa_switch_parse(ds, pdata);
859 else
860 err = -ENODEV;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200861
Vivien Didelot975e6e32017-11-03 19:05:27 -0400862 if (err)
863 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200864
Vivien Didelot30817352017-11-06 16:11:52 -0500865 return dsa_switch_add(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200866}
867
Vivien Didelota0c02162017-01-27 15:29:36 -0500868struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
869{
Vivien Didelota0c02162017-01-27 15:29:36 -0500870 struct dsa_switch *ds;
871
Gustavo A. R. Silva33b363e2019-02-07 19:16:03 -0600872 ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
Vivien Didelota0c02162017-01-27 15:29:36 -0500873 if (!ds)
874 return NULL;
875
876 ds->dev = dev;
877 ds->num_ports = n;
878
879 return ds;
880}
881EXPORT_SYMBOL_GPL(dsa_switch_alloc);
882
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400883int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200884{
885 int err;
886
887 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500888 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -0500889 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200890 mutex_unlock(&dsa2_mutex);
891
892 return err;
893}
894EXPORT_SYMBOL_GPL(dsa_register_switch);
895
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500896static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200897{
898 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot6da2a942017-11-03 19:05:25 -0400899 unsigned int index = ds->index;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200900
Vivien Didelot6da2a942017-11-03 19:05:25 -0400901 dsa_tree_remove_switch(dst, index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200902}
903
904void dsa_unregister_switch(struct dsa_switch *ds)
905{
906 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500907 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200908 mutex_unlock(&dsa2_mutex);
909}
910EXPORT_SYMBOL_GPL(dsa_unregister_switch);