blob: 9a271a58a41dcf8fa352284f2d23477038e269d2 [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
Andrew Lunn83c0afa2016-06-04 21:17:07 +020021static DEFINE_MUTEX(dsa2_mutex);
Vladimir Olteanbff33f72020-03-27 21:55:43 +020022LIST_HEAD(dsa_tree_list);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020023
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 Didelotc5f51762019-10-30 22:09:13 -040048 INIT_LIST_HEAD(&dst->rtable);
49
Vivien Didelotab8ccae2019-10-21 16:51:16 -040050 INIT_LIST_HEAD(&dst->ports);
51
Andrew Lunn83c0afa2016-06-04 21:17:07 +020052 INIT_LIST_HEAD(&dst->list);
Vivien Didelot50c7d2ba2019-10-18 17:02:46 -040053 list_add_tail(&dst->list, &dsa_tree_list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -040054
Andrew Lunn83c0afa2016-06-04 21:17:07 +020055 kref_init(&dst->refcount);
56
57 return dst;
58}
59
Vivien Didelot65254102017-11-03 19:05:23 -040060static void dsa_tree_free(struct dsa_switch_tree *dst)
61{
62 list_del(&dst->list);
63 kfree(dst);
64}
65
Vivien Didelot9e741042017-11-24 11:36:06 -050066static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
67{
68 if (dst)
69 kref_get(&dst->refcount);
70
71 return dst;
72}
73
Vivien Didelot1ca28ec2017-11-03 19:05:24 -040074static struct dsa_switch_tree *dsa_tree_touch(int index)
75{
76 struct dsa_switch_tree *dst;
77
78 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -050079 if (dst)
80 return dsa_tree_get(dst);
81 else
82 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -040083}
84
85static void dsa_tree_release(struct kref *ref)
86{
87 struct dsa_switch_tree *dst;
88
89 dst = container_of(ref, struct dsa_switch_tree, refcount);
90
91 dsa_tree_free(dst);
92}
93
94static void dsa_tree_put(struct dsa_switch_tree *dst)
95{
Vivien Didelot9e741042017-11-24 11:36:06 -050096 if (dst)
97 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -040098}
99
Florian Fainelli293784a2017-01-26 10:45:52 -0800100static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200101{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400102 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800103}
104
105static bool dsa_port_is_cpu(struct dsa_port *port)
106{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400107 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200108}
109
Vivien Didelotf0704642017-11-06 16:11:44 -0500110static bool dsa_port_is_user(struct dsa_port *dp)
111{
112 return dp->type == DSA_PORT_TYPE_USER;
113}
114
Vivien Didelotf163da82017-11-06 16:11:49 -0500115static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
116 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200117{
Vivien Didelotf163da82017-11-06 16:11:49 -0500118 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200119
Vivien Didelot764b7e62019-10-21 16:51:21 -0400120 list_for_each_entry(dp, &dst->ports, list)
121 if (dp->dn == dn)
122 return dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200123
124 return NULL;
125}
126
Ben Dooks (Codethink)4e2ce6e2019-12-17 11:20:38 +0000127static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
128 struct dsa_port *link_dp)
Vivien Didelotc5f51762019-10-30 22:09:13 -0400129{
130 struct dsa_switch *ds = dp->ds;
131 struct dsa_switch_tree *dst;
132 struct dsa_link *dl;
133
134 dst = ds->dst;
135
136 list_for_each_entry(dl, &dst->rtable, list)
137 if (dl->dp == dp && dl->link_dp == link_dp)
138 return dl;
139
140 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
141 if (!dl)
142 return NULL;
143
144 dl->dp = dp;
145 dl->link_dp = link_dp;
146
147 INIT_LIST_HEAD(&dl->list);
148 list_add_tail(&dl->list, &dst->rtable);
149
150 return dl;
151}
152
Vivien Didelot34c09a82017-11-06 16:11:51 -0500153static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200154{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500155 struct dsa_switch *ds = dp->ds;
156 struct dsa_switch_tree *dst = ds->dst;
157 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500158 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500159 struct dsa_port *link_dp;
Vivien Didelotc5f51762019-10-30 22:09:13 -0400160 struct dsa_link *dl;
Vivien Didelotc5286662017-11-06 16:11:50 -0500161 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200162
Vivien Didelotc5286662017-11-06 16:11:50 -0500163 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
164 link_dp = dsa_tree_find_port_by_node(dst, it.node);
165 if (!link_dp) {
166 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500167 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500168 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200169
Vivien Didelotc5f51762019-10-30 22:09:13 -0400170 dl = dsa_link_touch(dp, link_dp);
171 if (!dl) {
172 of_node_put(it.node);
173 return false;
174 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200175 }
176
Vivien Didelot34c09a82017-11-06 16:11:51 -0500177 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200178}
179
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400180static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200181{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500182 bool complete = true;
183 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200184
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400185 list_for_each_entry(dp, &dst->ports, list) {
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400186 if (dsa_port_is_dsa(dp)) {
Vivien Didelot34c09a82017-11-06 16:11:51 -0500187 complete = dsa_port_setup_routing_table(dp);
188 if (!complete)
189 break;
190 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200191 }
192
Vivien Didelot34c09a82017-11-06 16:11:51 -0500193 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200194}
195
Vivien Didelotf0704642017-11-06 16:11:44 -0500196static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
197{
Vivien Didelotf0704642017-11-06 16:11:44 -0500198 struct dsa_port *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500199
Vivien Didelotc0b73622019-10-21 16:51:23 -0400200 list_for_each_entry(dp, &dst->ports, list)
201 if (dsa_port_is_cpu(dp))
202 return dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500203
204 return NULL;
205}
206
207static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
208{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400209 struct dsa_port *cpu_dp, *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500210
Vivien Didelotda4561c2019-10-21 16:51:24 -0400211 cpu_dp = dsa_tree_find_first_cpu(dst);
212 if (!cpu_dp) {
213 pr_err("DSA: tree %d has no CPU port\n", dst->index);
Vivien Didelotf0704642017-11-06 16:11:44 -0500214 return -EINVAL;
215 }
216
217 /* Assign the default CPU port to all ports of the fabric */
Vivien Didelotda4561c2019-10-21 16:51:24 -0400218 list_for_each_entry(dp, &dst->ports, list)
219 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
220 dp->cpu_dp = cpu_dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500221
222 return 0;
223}
224
225static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
226{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400227 struct dsa_port *dp;
228
229 list_for_each_entry(dp, &dst->ports, list)
230 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
231 dp->cpu_dp = NULL;
Vivien Didelotf0704642017-11-06 16:11:44 -0500232}
233
Vivien Didelot1d277322017-11-06 16:11:48 -0500234static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200235{
Vivien Didelot1d277322017-11-06 16:11:48 -0500236 struct dsa_switch *ds = dp->ds;
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200237 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot955222c2019-08-19 16:00:48 -0400238 const unsigned char *id = (const unsigned char *)&dst->index;
239 const unsigned char len = sizeof(dst->index);
240 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300241 bool dsa_port_link_registered = false;
242 bool devlink_port_registered = false;
Vivien Didelot955222c2019-08-19 16:00:48 -0400243 struct devlink *dl = ds->devlink;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300244 bool dsa_port_enabled = false;
245 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200246
Vivien Didelotfb35c602019-10-21 16:51:19 -0400247 if (dp->setup)
248 return 0;
249
Vivien Didelot1d277322017-11-06 16:11:48 -0500250 switch (dp->type) {
251 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400252 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500253 break;
254 case DSA_PORT_TYPE_CPU:
Vivien Didelot955222c2019-08-19 16:00:48 -0400255 memset(dlp, 0, sizeof(*dlp));
256 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_CPU,
257 dp->index, false, 0, id, len);
258 err = devlink_port_register(dl, dlp, dp->index);
259 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300260 break;
261 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400262
Jiri Pirkoda077392018-05-18 09:29:03 +0200263 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300264 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300265 break;
266 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400267
268 err = dsa_port_enable(dp, NULL);
269 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300270 break;
271 dsa_port_enabled = true;
272
Jiri Pirkoda077392018-05-18 09:29:03 +0200273 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500274 case DSA_PORT_TYPE_DSA:
Vivien Didelot955222c2019-08-19 16:00:48 -0400275 memset(dlp, 0, sizeof(*dlp));
276 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_DSA,
277 dp->index, false, 0, id, len);
278 err = devlink_port_register(dl, dlp, dp->index);
279 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300280 break;
281 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400282
Sebastian Reichel33615362018-01-23 16:03:46 +0100283 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300284 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300285 break;
286 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400287
288 err = dsa_port_enable(dp, NULL);
289 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300290 break;
291 dsa_port_enabled = true;
292
Vivien Didelot1d277322017-11-06 16:11:48 -0500293 break;
294 case DSA_PORT_TYPE_USER:
Vivien Didelot955222c2019-08-19 16:00:48 -0400295 memset(dlp, 0, sizeof(*dlp));
296 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_PHYSICAL,
297 dp->index, false, 0, id, len);
298 err = devlink_port_register(dl, dlp, dp->index);
299 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300300 break;
301 devlink_port_registered = true;
Vivien Didelot955222c2019-08-19 16:00:48 -0400302
303 dp->mac = of_get_mac_address(dp->dn);
Vivien Didelot1d277322017-11-06 16:11:48 -0500304 err = dsa_slave_create(dp);
305 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300306 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400307
308 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500309 break;
310 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200311
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300312 if (err && dsa_port_enabled)
313 dsa_port_disable(dp);
314 if (err && dsa_port_link_registered)
315 dsa_port_link_unregister_of(dp);
316 if (err && devlink_port_registered)
317 devlink_port_unregister(dlp);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400318 if (err)
319 return err;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300320
Vivien Didelotfb35c602019-10-21 16:51:19 -0400321 dp->setup = true;
322
323 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200324}
325
Vivien Didelot1d277322017-11-06 16:11:48 -0500326static void dsa_port_teardown(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200327{
Vivien Didelot955222c2019-08-19 16:00:48 -0400328 struct devlink_port *dlp = &dp->devlink_port;
Vivien Didelot1d277322017-11-06 16:11:48 -0500329
Vivien Didelotfb35c602019-10-21 16:51:19 -0400330 if (!dp->setup)
331 return;
332
Vivien Didelot1d277322017-11-06 16:11:48 -0500333 switch (dp->type) {
334 case DSA_PORT_TYPE_UNUSED:
335 break;
336 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400337 dsa_port_disable(dp);
Andrew Lunn4dad81e2019-04-28 19:37:19 +0200338 dsa_tag_driver_put(dp->tag_ops);
Vivien Didelot955222c2019-08-19 16:00:48 -0400339 devlink_port_unregister(dlp);
340 dsa_port_link_unregister_of(dp);
341 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500342 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400343 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400344 devlink_port_unregister(dlp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100345 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500346 break;
347 case DSA_PORT_TYPE_USER:
Vivien Didelot955222c2019-08-19 16:00:48 -0400348 devlink_port_unregister(dlp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500349 if (dp->slave) {
350 dsa_slave_destroy(dp->slave);
351 dp->slave = NULL;
352 }
353 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200354 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400355
356 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200357}
358
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500359static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200360{
Andrew Lunn6b297522019-10-25 01:03:51 +0200361 struct dsa_devlink_priv *dl_priv;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400362 int err;
363
364 if (ds->setup)
365 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200366
Florian Fainelli6e830d82016-06-07 16:32:39 -0700367 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400368 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700369 * the slave MDIO bus driver rely on these values for probing PHY
370 * devices or not
371 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400372 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700373
Andrew Lunn96567d52017-03-28 23:45:07 +0200374 /* Add the switch to devlink before calling setup, so that setup can
375 * add dpipe tables
376 */
Andrew Lunn6b297522019-10-25 01:03:51 +0200377 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
Andrew Lunn96567d52017-03-28 23:45:07 +0200378 if (!ds->devlink)
379 return -ENOMEM;
Andrew Lunn6b297522019-10-25 01:03:51 +0200380 dl_priv = devlink_priv(ds->devlink);
381 dl_priv->ds = ds;
Andrew Lunn96567d52017-03-28 23:45:07 +0200382
383 err = devlink_register(ds->devlink, ds->dev);
384 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300385 goto free_devlink;
Andrew Lunn96567d52017-03-28 23:45:07 +0200386
Vivien Didelotf515f192017-02-03 13:20:20 -0500387 err = dsa_switch_register_notifier(ds);
388 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300389 goto unregister_devlink;
Vivien Didelotf515f192017-02-03 13:20:20 -0500390
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300391 err = ds->ops->setup(ds);
392 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300393 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300394
Andrew Lunn6b297522019-10-25 01:03:51 +0200395 devlink_params_publish(ds->devlink);
396
Vivien Didelot9d490b42016-08-23 12:38:56 -0400397 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700398 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300399 if (!ds->slave_mii_bus) {
400 err = -ENOMEM;
401 goto unregister_notifier;
402 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700403
404 dsa_slave_mii_bus_init(ds);
405
406 err = mdiobus_register(ds->slave_mii_bus);
407 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300408 goto unregister_notifier;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700409 }
410
Vivien Didelotfb35c602019-10-21 16:51:19 -0400411 ds->setup = true;
412
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200413 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300414
415unregister_notifier:
416 dsa_switch_unregister_notifier(ds);
417unregister_devlink:
418 devlink_unregister(ds->devlink);
419free_devlink:
420 devlink_free(ds->devlink);
421 ds->devlink = NULL;
422
423 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200424}
425
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500426static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200427{
Vivien Didelotfb35c602019-10-21 16:51:19 -0400428 if (!ds->setup)
429 return;
430
Vivien Didelot9d490b42016-08-23 12:38:56 -0400431 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700432 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500433
434 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200435
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300436 if (ds->ops->teardown)
437 ds->ops->teardown(ds);
438
Andrew Lunn96567d52017-03-28 23:45:07 +0200439 if (ds->devlink) {
440 devlink_unregister(ds->devlink);
441 devlink_free(ds->devlink);
442 ds->devlink = NULL;
443 }
444
Vivien Didelotfb35c602019-10-21 16:51:19 -0400445 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200446}
447
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500448static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
449{
Vivien Didelot1d277322017-11-06 16:11:48 -0500450 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400451 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500452
Vivien Didelotfb35c602019-10-21 16:51:19 -0400453 list_for_each_entry(dp, &dst->ports, list) {
454 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500455 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400456 goto teardown;
457 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500458
Vivien Didelotfb35c602019-10-21 16:51:19 -0400459 list_for_each_entry(dp, &dst->ports, list) {
460 err = dsa_port_setup(dp);
461 if (err)
462 goto teardown;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500463 }
464
465 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300466
Vivien Didelotfb35c602019-10-21 16:51:19 -0400467teardown:
468 list_for_each_entry(dp, &dst->ports, list)
469 dsa_port_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300470
Vivien Didelotfb35c602019-10-21 16:51:19 -0400471 list_for_each_entry(dp, &dst->ports, list)
472 dsa_switch_teardown(dp->ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300473
474 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500475}
476
477static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
478{
Vivien Didelot1d277322017-11-06 16:11:48 -0500479 struct dsa_port *dp;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500480
Vivien Didelotfb35c602019-10-21 16:51:19 -0400481 list_for_each_entry(dp, &dst->ports, list)
482 dsa_port_teardown(dp);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500483
Vivien Didelotfb35c602019-10-21 16:51:19 -0400484 list_for_each_entry(dp, &dst->ports, list)
485 dsa_switch_teardown(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500486}
487
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500488static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
489{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400490 struct dsa_port *dp;
491 int err;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500492
Vivien Didelot0cfec582019-10-21 16:51:22 -0400493 list_for_each_entry(dp, &dst->ports, list) {
494 if (dsa_port_is_cpu(dp)) {
495 err = dsa_master_setup(dp->master, dp);
496 if (err)
497 return err;
498 }
499 }
500
501 return 0;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500502}
503
504static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
505{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400506 struct dsa_port *dp;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500507
Vivien Didelot0cfec582019-10-21 16:51:22 -0400508 list_for_each_entry(dp, &dst->ports, list)
509 if (dsa_port_is_cpu(dp))
510 dsa_master_teardown(dp->master);
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500511}
512
Vivien Didelotec15dd42017-11-06 16:11:46 -0500513static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200514{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500515 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200516 int err;
517
Vivien Didelotec15dd42017-11-06 16:11:46 -0500518 if (dst->setup) {
519 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
520 dst->index);
521 return -EEXIST;
522 }
523
Vivien Didelot34c09a82017-11-06 16:11:51 -0500524 complete = dsa_tree_setup_routing_table(dst);
525 if (!complete)
526 return 0;
527
Vivien Didelotf0704642017-11-06 16:11:44 -0500528 err = dsa_tree_setup_default_cpu(dst);
529 if (err)
530 return err;
531
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500532 err = dsa_tree_setup_switches(dst);
533 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300534 goto teardown_default_cpu;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200535
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500536 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400537 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300538 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -0400539
Vivien Didelotec15dd42017-11-06 16:11:46 -0500540 dst->setup = true;
541
542 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200543
544 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300545
546teardown_switches:
547 dsa_tree_teardown_switches(dst);
548teardown_default_cpu:
549 dsa_tree_teardown_default_cpu(dst);
550
551 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200552}
553
Vivien Didelotec15dd42017-11-06 16:11:46 -0500554static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200555{
Vivien Didelotc5f51762019-10-30 22:09:13 -0400556 struct dsa_link *dl, *next;
557
Vivien Didelotec15dd42017-11-06 16:11:46 -0500558 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200559 return;
560
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500561 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200562
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500563 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200564
Vivien Didelotf0704642017-11-06 16:11:44 -0500565 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700566
Vivien Didelotc5f51762019-10-30 22:09:13 -0400567 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
568 list_del(&dl->list);
569 kfree(dl);
570 }
571
Vivien Didelotec15dd42017-11-06 16:11:46 -0500572 pr_info("DSA: tree %d torn down\n", dst->index);
573
574 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200575}
576
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400577static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
578{
579 struct dsa_switch_tree *dst = ds->dst;
580 struct dsa_port *dp;
581
Vivien Didelot05f294a2019-10-21 16:51:29 -0400582 list_for_each_entry(dp, &dst->ports, list)
583 if (dp->ds == ds && dp->index == index)
584 return dp;
585
586 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
587 if (!dp)
588 return NULL;
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400589
590 dp->ds = ds;
591 dp->index = index;
592
593 INIT_LIST_HEAD(&dp->list);
594 list_add_tail(&dp->list, &dst->ports);
595
596 return dp;
597}
598
Vivien Didelot06e24d02017-11-03 19:05:29 -0400599static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
600{
601 if (!name)
602 name = "eth%d";
603
604 dp->type = DSA_PORT_TYPE_USER;
605 dp->name = name;
606
607 return 0;
608}
609
610static int dsa_port_parse_dsa(struct dsa_port *dp)
611{
612 dp->type = DSA_PORT_TYPE_DSA;
613
614 return 0;
615}
616
Florian Fainelli4d776482020-01-07 21:06:05 -0800617static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
618 struct net_device *master)
619{
620 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
621 struct dsa_switch *mds, *ds = dp->ds;
622 unsigned int mdp_upstream;
623 struct dsa_port *mdp;
624
625 /* It is possible to stack DSA switches onto one another when that
626 * happens the switch driver may want to know if its tagging protocol
627 * is going to work in such a configuration.
628 */
629 if (dsa_slave_dev_check(master)) {
630 mdp = dsa_slave_to_port(master);
631 mds = mdp->ds;
632 mdp_upstream = dsa_upstream_port(mds, mdp->index);
633 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
634 DSA_TAG_PROTO_NONE);
635 }
636
637 /* If the master device is not itself a DSA slave in a disjoint DSA
638 * tree, then return immediately.
639 */
640 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
641}
642
Vivien Didelot06e24d02017-11-03 19:05:29 -0400643static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
644{
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400645 struct dsa_switch *ds = dp->ds;
646 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400647 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +0200648 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200649
Florian Fainelli4d776482020-01-07 21:06:05 -0800650 tag_protocol = dsa_get_tag_protocol(dp, master);
Andrew Lunnc39e2a12019-04-28 19:37:18 +0200651 tag_ops = dsa_tag_driver_get(tag_protocol);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400652 if (IS_ERR(tag_ops)) {
Andrew Lunn23426a22019-09-12 15:16:45 +0200653 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
654 return -EPROBE_DEFER;
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700655 dev_warn(ds->dev, "No tagger for this switch\n");
Florian Fainelli4d776482020-01-07 21:06:05 -0800656 dp->master = NULL;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400657 return PTR_ERR(tag_ops);
Florian Fainelli9f9e7722017-07-24 10:49:23 -0700658 }
659
Florian Fainelli4d776482020-01-07 21:06:05 -0800660 dp->master = master;
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400661 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Olteancc1939e2019-05-05 13:19:23 +0300662 dp->filter = tag_ops->filter;
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400663 dp->rcv = tag_ops->rcv;
664 dp->tag_ops = tag_ops;
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400665 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -0400666
Vivien Didelot7354fcb2017-11-03 19:05:30 -0400667 return 0;
668}
669
Vivien Didelotfd223e22017-10-27 15:55:14 -0400670static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
671{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400672 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -0400673 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -0400674 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400675
Vivien Didelot06e24d02017-11-03 19:05:29 -0400676 dp->dn = dn;
677
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400678 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400679 struct net_device *master;
680
681 master = of_find_net_device_by_node(ethernet);
682 if (!master)
683 return -EPROBE_DEFER;
684
Vivien Didelot06e24d02017-11-03 19:05:29 -0400685 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400686 }
687
Vivien Didelot06e24d02017-11-03 19:05:29 -0400688 if (link)
689 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400690
Vivien Didelot06e24d02017-11-03 19:05:29 -0400691 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400692}
693
Vivien Didelot975e6e32017-11-03 19:05:27 -0400694static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
695 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200696{
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400697 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400698 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +0800699 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200700 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -0400701
702 ports = of_get_child_by_name(dn, "ports");
703 if (!ports) {
704 dev_err(ds->dev, "no ports child node found\n");
705 return -EINVAL;
706 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200707
708 for_each_available_child_of_node(ports, port) {
709 err = of_property_read_u32(port, "reg", &reg);
710 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800711 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200712
Wen Yang9919a362019-02-25 15:22:19 +0800713 if (reg >= ds->num_ports) {
714 err = -EINVAL;
715 goto out_put_node;
716 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200717
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400718 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400719
720 err = dsa_port_parse_of(dp, port);
721 if (err)
Wen Yang9919a362019-02-25 15:22:19 +0800722 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200723 }
724
Wen Yang9919a362019-02-25 15:22:19 +0800725out_put_node:
726 of_node_put(ports);
727 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200728}
729
Vivien Didelot975e6e32017-11-03 19:05:27 -0400730static int dsa_switch_parse_member_of(struct dsa_switch *ds,
731 struct device_node *dn)
732{
733 u32 m[2] = { 0, 0 };
734 int sz;
735
736 /* Don't error out if this optional property isn't found */
737 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
738 if (sz < 0 && sz != -EINVAL)
739 return sz;
740
741 ds->index = m[1];
Vivien Didelot975e6e32017-11-03 19:05:27 -0400742
743 ds->dst = dsa_tree_touch(m[0]);
744 if (!ds->dst)
745 return -ENOMEM;
746
747 return 0;
748}
749
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400750static int dsa_switch_touch_ports(struct dsa_switch *ds)
751{
752 struct dsa_port *dp;
753 int port;
754
755 for (port = 0; port < ds->num_ports; port++) {
756 dp = dsa_port_touch(ds, port);
757 if (!dp)
758 return -ENOMEM;
759 }
760
761 return 0;
762}
763
Vivien Didelot975e6e32017-11-03 19:05:27 -0400764static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
765{
766 int err;
767
768 err = dsa_switch_parse_member_of(ds, dn);
769 if (err)
770 return err;
771
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400772 err = dsa_switch_touch_ports(ds);
773 if (err)
774 return err;
775
Vivien Didelot975e6e32017-11-03 19:05:27 -0400776 return dsa_switch_parse_ports_of(ds, dn);
777}
778
Vivien Didelotfd223e22017-10-27 15:55:14 -0400779static int dsa_port_parse(struct dsa_port *dp, const char *name,
780 struct device *dev)
781{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400782 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -0400783 struct net_device *master;
784
785 master = dsa_dev_to_net_device(dev);
786 if (!master)
787 return -EPROBE_DEFER;
788
789 dev_put(master);
790
Vivien Didelot06e24d02017-11-03 19:05:29 -0400791 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400792 }
793
Vivien Didelot06e24d02017-11-03 19:05:29 -0400794 if (!strcmp(name, "dsa"))
795 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400796
Vivien Didelot06e24d02017-11-03 19:05:29 -0400797 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400798}
799
Vivien Didelot975e6e32017-11-03 19:05:27 -0400800static int dsa_switch_parse_ports(struct dsa_switch *ds,
801 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800802{
803 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400804 struct dsa_port *dp;
805 struct device *dev;
806 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800807 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -0400808 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800809
810 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -0400811 name = cd->port_names[i];
812 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400813 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -0400814
815 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800816 continue;
817
Vivien Didelotfd223e22017-10-27 15:55:14 -0400818 err = dsa_port_parse(dp, name, dev);
819 if (err)
820 return err;
821
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800822 valid_name_found = true;
823 }
824
825 if (!valid_name_found && i == DSA_MAX_PORTS)
826 return -EINVAL;
827
828 return 0;
829}
830
Vivien Didelot975e6e32017-11-03 19:05:27 -0400831static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200832{
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400833 int err;
834
Vivien Didelot975e6e32017-11-03 19:05:27 -0400835 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200836
Vivien Didelot975e6e32017-11-03 19:05:27 -0400837 /* We don't support interconnected switches nor multiple trees via
838 * platform data, so this is the unique switch of the tree.
839 */
840 ds->index = 0;
841 ds->dst = dsa_tree_touch(0);
842 if (!ds->dst)
843 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200844
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400845 err = dsa_switch_touch_ports(ds);
846 if (err)
847 return err;
848
Vivien Didelot975e6e32017-11-03 19:05:27 -0400849 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -0800850}
851
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200852static void dsa_switch_release_ports(struct dsa_switch *ds)
853{
854 struct dsa_switch_tree *dst = ds->dst;
855 struct dsa_port *dp, *next;
856
857 list_for_each_entry_safe(dp, next, &dst->ports, list) {
858 if (dp->ds != ds)
859 continue;
860 list_del(&dp->list);
861 kfree(dp);
862 }
863}
864
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500865static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200866{
Vivien Didelot8e5cb842019-10-30 22:09:17 -0400867 struct dsa_switch_tree *dst;
Colin Ian King556f1242019-10-24 11:32:18 +0100868 struct dsa_chip_data *pdata;
869 struct device_node *np;
Vivien Didelot34c09a82017-11-06 16:11:51 -0500870 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200871
Vivien Didelot7e99e342019-10-21 16:51:30 -0400872 if (!ds->dev)
873 return -ENODEV;
874
Colin Ian King556f1242019-10-24 11:32:18 +0100875 pdata = ds->dev->platform_data;
876 np = ds->dev->of_node;
877
Vivien Didelot7e99e342019-10-21 16:51:30 -0400878 if (!ds->num_ports)
879 return -EINVAL;
880
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200881 if (np) {
Vivien Didelot975e6e32017-11-03 19:05:27 -0400882 err = dsa_switch_parse_of(ds, np);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200883 if (err)
884 dsa_switch_release_ports(ds);
885 } else if (pdata) {
Vivien Didelot975e6e32017-11-03 19:05:27 -0400886 err = dsa_switch_parse(ds, pdata);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200887 if (err)
888 dsa_switch_release_ports(ds);
889 } else {
Vivien Didelot975e6e32017-11-03 19:05:27 -0400890 err = -ENODEV;
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200891 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200892
Vivien Didelot975e6e32017-11-03 19:05:27 -0400893 if (err)
894 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200895
Vivien Didelot8e5cb842019-10-30 22:09:17 -0400896 dst = ds->dst;
897 dsa_tree_get(dst);
898 err = dsa_tree_setup(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200899 if (err) {
900 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -0400901 dsa_tree_put(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200902 }
Vivien Didelot8e5cb842019-10-30 22:09:17 -0400903
904 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200905}
906
Vivien Didelot23c9ee42017-05-26 18:12:51 -0400907int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200908{
909 int err;
910
911 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500912 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -0500913 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200914 mutex_unlock(&dsa2_mutex);
915
916 return err;
917}
918EXPORT_SYMBOL_GPL(dsa_register_switch);
919
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500920static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200921{
922 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot05f294a2019-10-21 16:51:29 -0400923
Florian Fainellic058f6d2019-11-02 20:13:26 -0700924 dsa_tree_teardown(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +0200925 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -0400926 dsa_tree_put(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200927}
928
929void dsa_unregister_switch(struct dsa_switch *ds)
930{
931 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -0500932 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200933 mutex_unlock(&dsa2_mutex);
934}
935EXPORT_SYMBOL_GPL(dsa_unregister_switch);