blob: b71e87909f0e3987281ddfe5305b0a72fb71cdaf [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
Tobias Waldekranz058102a2021-01-13 09:42:53 +010024/**
Vladimir Oltean886f8e22021-01-29 03:00:04 +020025 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
26 * @dst: collection of struct dsa_switch devices to notify.
27 * @e: event, must be of type DSA_NOTIFIER_*
28 * @v: event-specific value.
29 *
30 * Given a struct dsa_switch_tree, this can be used to run a function once for
31 * each member DSA switch. The other alternative of traversing the tree is only
32 * through its ports list, which does not uniquely list the switches.
33 */
34int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
35{
36 struct raw_notifier_head *nh = &dst->nh;
37 int err;
38
39 err = raw_notifier_call_chain(nh, e, v);
40
41 return notifier_to_errno(err);
42}
43
44/**
45 * dsa_broadcast - Notify all DSA trees in the system.
46 * @e: event, must be of type DSA_NOTIFIER_*
47 * @v: event-specific value.
48 *
49 * Can be used to notify the switching fabric of events such as cross-chip
50 * bridging between disjoint trees (such as islands of tagger-compatible
51 * switches bridged by an incompatible middle switch).
52 */
53int dsa_broadcast(unsigned long e, void *v)
54{
55 struct dsa_switch_tree *dst;
56 int err = 0;
57
58 list_for_each_entry(dst, &dsa_tree_list, list) {
59 err = dsa_tree_notify(dst, e, v);
60 if (err)
61 break;
62 }
63
64 return err;
65}
66
67/**
Tobias Waldekranz058102a2021-01-13 09:42:53 +010068 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
69 * @dst: Tree in which to record the mapping.
70 * @lag: Netdev that is to be mapped to an ID.
71 *
72 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
73 * two spaces. The size of the mapping space is determined by the
74 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
75 * it unset if it is not needed, in which case these functions become
76 * no-ops.
77 */
78void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
79{
80 unsigned int id;
81
82 if (dsa_lag_id(dst, lag) >= 0)
83 /* Already mapped */
84 return;
85
86 for (id = 0; id < dst->lags_len; id++) {
87 if (!dsa_lag_dev(dst, id)) {
88 dst->lags[id] = lag;
89 return;
90 }
91 }
92
93 /* No IDs left, which is OK. Some drivers do not need it. The
94 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
95 * returns an error for this device when joining the LAG. The
96 * driver can then return -EOPNOTSUPP back to DSA, which will
97 * fall back to a software LAG.
98 */
99}
100
101/**
102 * dsa_lag_unmap() - Remove a LAG ID mapping
103 * @dst: Tree in which the mapping is recorded.
104 * @lag: Netdev that was mapped.
105 *
106 * As there may be multiple users of the mapping, it is only removed
107 * if there are no other references to it.
108 */
109void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
110{
111 struct dsa_port *dp;
112 unsigned int id;
113
114 dsa_lag_foreach_port(dp, dst, lag)
115 /* There are remaining users of this mapping */
116 return;
117
118 dsa_lags_foreach_id(id, dst) {
119 if (dsa_lag_dev(dst, id) == lag) {
120 dst->lags[id] = NULL;
121 break;
122 }
123 }
124}
125
Vladimir Oltean3b7bc1f2020-05-10 19:37:42 +0300126struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
127{
128 struct dsa_switch_tree *dst;
129 struct dsa_port *dp;
130
131 list_for_each_entry(dst, &dsa_tree_list, list) {
132 if (dst->index != tree_index)
133 continue;
134
135 list_for_each_entry(dp, &dst->ports, list) {
136 if (dp->ds->index != sw_index)
137 continue;
138
139 return dp->ds;
140 }
141 }
142
143 return NULL;
144}
145EXPORT_SYMBOL_GPL(dsa_switch_find);
146
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400147static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200148{
149 struct dsa_switch_tree *dst;
150
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400151 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400152 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200153 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400154
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200155 return NULL;
156}
157
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400158static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200159{
160 struct dsa_switch_tree *dst;
161
162 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
163 if (!dst)
164 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400165
Vivien Didelot49463b72017-11-03 19:05:21 -0400166 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400167
Vivien Didelotc5f51762019-10-30 22:09:13 -0400168 INIT_LIST_HEAD(&dst->rtable);
169
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400170 INIT_LIST_HEAD(&dst->ports);
171
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200172 INIT_LIST_HEAD(&dst->list);
Vivien Didelot50c7d2ba2019-10-18 17:02:46 -0400173 list_add_tail(&dst->list, &dsa_tree_list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400174
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200175 kref_init(&dst->refcount);
176
177 return dst;
178}
179
Vivien Didelot65254102017-11-03 19:05:23 -0400180static void dsa_tree_free(struct dsa_switch_tree *dst)
181{
Vladimir Oltean357f2032021-01-29 03:00:05 +0200182 if (dst->tag_ops)
183 dsa_tag_driver_put(dst->tag_ops);
Vivien Didelot65254102017-11-03 19:05:23 -0400184 list_del(&dst->list);
185 kfree(dst);
186}
187
Vivien Didelot9e741042017-11-24 11:36:06 -0500188static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
189{
190 if (dst)
191 kref_get(&dst->refcount);
192
193 return dst;
194}
195
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400196static struct dsa_switch_tree *dsa_tree_touch(int index)
197{
198 struct dsa_switch_tree *dst;
199
200 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -0500201 if (dst)
202 return dsa_tree_get(dst);
203 else
204 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -0400205}
206
207static void dsa_tree_release(struct kref *ref)
208{
209 struct dsa_switch_tree *dst;
210
211 dst = container_of(ref, struct dsa_switch_tree, refcount);
212
213 dsa_tree_free(dst);
214}
215
216static void dsa_tree_put(struct dsa_switch_tree *dst)
217{
Vivien Didelot9e741042017-11-24 11:36:06 -0500218 if (dst)
219 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -0400220}
221
Florian Fainelli293784a2017-01-26 10:45:52 -0800222static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200223{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400224 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800225}
226
227static bool dsa_port_is_cpu(struct dsa_port *port)
228{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400229 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200230}
231
Vivien Didelotf0704642017-11-06 16:11:44 -0500232static bool dsa_port_is_user(struct dsa_port *dp)
233{
234 return dp->type == DSA_PORT_TYPE_USER;
235}
236
Vivien Didelotf163da82017-11-06 16:11:49 -0500237static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
238 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200239{
Vivien Didelotf163da82017-11-06 16:11:49 -0500240 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200241
Vivien Didelot764b7e62019-10-21 16:51:21 -0400242 list_for_each_entry(dp, &dst->ports, list)
243 if (dp->dn == dn)
244 return dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200245
246 return NULL;
247}
248
Ben Dooks (Codethink)4e2ce6e2019-12-17 11:20:38 +0000249static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
250 struct dsa_port *link_dp)
Vivien Didelotc5f51762019-10-30 22:09:13 -0400251{
252 struct dsa_switch *ds = dp->ds;
253 struct dsa_switch_tree *dst;
254 struct dsa_link *dl;
255
256 dst = ds->dst;
257
258 list_for_each_entry(dl, &dst->rtable, list)
259 if (dl->dp == dp && dl->link_dp == link_dp)
260 return dl;
261
262 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
263 if (!dl)
264 return NULL;
265
266 dl->dp = dp;
267 dl->link_dp = link_dp;
268
269 INIT_LIST_HEAD(&dl->list);
270 list_add_tail(&dl->list, &dst->rtable);
271
272 return dl;
273}
274
Vivien Didelot34c09a82017-11-06 16:11:51 -0500275static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200276{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500277 struct dsa_switch *ds = dp->ds;
278 struct dsa_switch_tree *dst = ds->dst;
279 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500280 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500281 struct dsa_port *link_dp;
Vivien Didelotc5f51762019-10-30 22:09:13 -0400282 struct dsa_link *dl;
Vivien Didelotc5286662017-11-06 16:11:50 -0500283 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200284
Vivien Didelotc5286662017-11-06 16:11:50 -0500285 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
286 link_dp = dsa_tree_find_port_by_node(dst, it.node);
287 if (!link_dp) {
288 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500289 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500290 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200291
Vivien Didelotc5f51762019-10-30 22:09:13 -0400292 dl = dsa_link_touch(dp, link_dp);
293 if (!dl) {
294 of_node_put(it.node);
295 return false;
296 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200297 }
298
Vivien Didelot34c09a82017-11-06 16:11:51 -0500299 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200300}
301
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400302static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200303{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500304 bool complete = true;
305 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200306
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400307 list_for_each_entry(dp, &dst->ports, list) {
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400308 if (dsa_port_is_dsa(dp)) {
Vivien Didelot34c09a82017-11-06 16:11:51 -0500309 complete = dsa_port_setup_routing_table(dp);
310 if (!complete)
311 break;
312 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200313 }
314
Vivien Didelot34c09a82017-11-06 16:11:51 -0500315 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200316}
317
Vivien Didelotf0704642017-11-06 16:11:44 -0500318static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
319{
Vivien Didelotf0704642017-11-06 16:11:44 -0500320 struct dsa_port *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500321
Vivien Didelotc0b73622019-10-21 16:51:23 -0400322 list_for_each_entry(dp, &dst->ports, list)
323 if (dsa_port_is_cpu(dp))
324 return dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500325
326 return NULL;
327}
328
329static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
330{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400331 struct dsa_port *cpu_dp, *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500332
Vivien Didelotda4561c2019-10-21 16:51:24 -0400333 cpu_dp = dsa_tree_find_first_cpu(dst);
334 if (!cpu_dp) {
335 pr_err("DSA: tree %d has no CPU port\n", dst->index);
Vivien Didelotf0704642017-11-06 16:11:44 -0500336 return -EINVAL;
337 }
338
339 /* Assign the default CPU port to all ports of the fabric */
Vivien Didelotda4561c2019-10-21 16:51:24 -0400340 list_for_each_entry(dp, &dst->ports, list)
341 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
342 dp->cpu_dp = cpu_dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500343
344 return 0;
345}
346
347static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
348{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400349 struct dsa_port *dp;
350
351 list_for_each_entry(dp, &dst->ports, list)
352 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
353 dp->cpu_dp = NULL;
Vivien Didelotf0704642017-11-06 16:11:44 -0500354}
355
Vivien Didelot1d277322017-11-06 16:11:48 -0500356static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200357{
Vivien Didelot955222c2019-08-19 16:00:48 -0400358 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300359 bool dsa_port_link_registered = false;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300360 bool dsa_port_enabled = false;
361 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200362
Vivien Didelotfb35c602019-10-21 16:51:19 -0400363 if (dp->setup)
364 return 0;
365
Vivien Didelot1d277322017-11-06 16:11:48 -0500366 switch (dp->type) {
367 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400368 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500369 break;
370 case DSA_PORT_TYPE_CPU:
Jiri Pirkoda077392018-05-18 09:29:03 +0200371 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300372 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300373 break;
374 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400375
376 err = dsa_port_enable(dp, NULL);
377 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300378 break;
379 dsa_port_enabled = true;
380
Jiri Pirkoda077392018-05-18 09:29:03 +0200381 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500382 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100383 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300384 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300385 break;
386 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400387
388 err = dsa_port_enable(dp, NULL);
389 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300390 break;
391 dsa_port_enabled = true;
392
Vivien Didelot1d277322017-11-06 16:11:48 -0500393 break;
394 case DSA_PORT_TYPE_USER:
Michael Walle83216e32021-04-12 19:47:17 +0200395 of_get_mac_address(dp->dn, dp->mac);
Vivien Didelot1d277322017-11-06 16:11:48 -0500396 err = dsa_slave_create(dp);
397 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300398 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400399
400 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500401 break;
402 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200403
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300404 if (err && dsa_port_enabled)
405 dsa_port_disable(dp);
406 if (err && dsa_port_link_registered)
407 dsa_port_link_unregister_of(dp);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400408 if (err)
409 return err;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300410
Vivien Didelotfb35c602019-10-21 16:51:19 -0400411 dp->setup = true;
412
413 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200414}
415
Andrew Lunn31224332020-10-04 18:12:53 +0200416static int dsa_port_devlink_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200417{
Vivien Didelot955222c2019-08-19 16:00:48 -0400418 struct devlink_port *dlp = &dp->devlink_port;
Andrew Lunn31224332020-10-04 18:12:53 +0200419 struct dsa_switch_tree *dst = dp->ds->dst;
420 struct devlink_port_attrs attrs = {};
421 struct devlink *dl = dp->ds->devlink;
422 const unsigned char *id;
423 unsigned char len;
424 int err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500425
Andrew Lunn31224332020-10-04 18:12:53 +0200426 id = (const unsigned char *)&dst->index;
427 len = sizeof(dst->index);
428
429 attrs.phys.port_number = dp->index;
430 memcpy(attrs.switch_id.id, id, len);
431 attrs.switch_id.id_len = len;
432 memset(dlp, 0, sizeof(*dlp));
433
434 switch (dp->type) {
435 case DSA_PORT_TYPE_UNUSED:
436 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
437 break;
438 case DSA_PORT_TYPE_CPU:
439 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
440 break;
441 case DSA_PORT_TYPE_DSA:
442 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
443 break;
444 case DSA_PORT_TYPE_USER:
445 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
446 break;
447 }
448
449 devlink_port_attrs_set(dlp, &attrs);
450 err = devlink_port_register(dl, dlp, dp->index);
451
452 if (!err)
453 dp->devlink_port_setup = true;
454
455 return err;
456}
457
458static void dsa_port_teardown(struct dsa_port *dp)
459{
Vladimir Oltean91158e12021-01-12 02:48:31 +0200460 struct devlink_port *dlp = &dp->devlink_port;
461
Vivien Didelotfb35c602019-10-21 16:51:19 -0400462 if (!dp->setup)
463 return;
464
Vladimir Oltean91158e12021-01-12 02:48:31 +0200465 devlink_port_type_clear(dlp);
466
Vivien Didelot1d277322017-11-06 16:11:48 -0500467 switch (dp->type) {
468 case DSA_PORT_TYPE_UNUSED:
469 break;
470 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400471 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400472 dsa_port_link_unregister_of(dp);
473 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500474 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400475 dsa_port_disable(dp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100476 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500477 break;
478 case DSA_PORT_TYPE_USER:
479 if (dp->slave) {
480 dsa_slave_destroy(dp->slave);
481 dp->slave = NULL;
482 }
483 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200484 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400485
486 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200487}
488
Andrew Lunn31224332020-10-04 18:12:53 +0200489static void dsa_port_devlink_teardown(struct dsa_port *dp)
490{
491 struct devlink_port *dlp = &dp->devlink_port;
492
493 if (dp->devlink_port_setup)
494 devlink_port_unregister(dlp);
495 dp->devlink_port_setup = false;
496}
497
Andrew Lunn0f06b852020-09-18 21:11:08 +0200498static int dsa_devlink_info_get(struct devlink *dl,
499 struct devlink_info_req *req,
500 struct netlink_ext_ack *extack)
501{
502 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
503
504 if (ds->ops->devlink_info_get)
505 return ds->ops->devlink_info_get(ds, req, extack);
506
507 return -EOPNOTSUPP;
508}
509
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200510static int dsa_devlink_sb_pool_get(struct devlink *dl,
511 unsigned int sb_index, u16 pool_index,
512 struct devlink_sb_pool_info *pool_info)
513{
514 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
515
516 if (!ds->ops->devlink_sb_pool_get)
517 return -EOPNOTSUPP;
518
519 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
520 pool_info);
521}
522
523static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
524 u16 pool_index, u32 size,
525 enum devlink_sb_threshold_type threshold_type,
526 struct netlink_ext_ack *extack)
527{
528 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
529
530 if (!ds->ops->devlink_sb_pool_set)
531 return -EOPNOTSUPP;
532
533 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
534 threshold_type, extack);
535}
536
537static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
538 unsigned int sb_index, u16 pool_index,
539 u32 *p_threshold)
540{
541 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
542 int port = dsa_devlink_port_to_port(dlp);
543
544 if (!ds->ops->devlink_sb_port_pool_get)
545 return -EOPNOTSUPP;
546
547 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
548 pool_index, p_threshold);
549}
550
551static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
552 unsigned int sb_index, u16 pool_index,
553 u32 threshold,
554 struct netlink_ext_ack *extack)
555{
556 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
557 int port = dsa_devlink_port_to_port(dlp);
558
559 if (!ds->ops->devlink_sb_port_pool_set)
560 return -EOPNOTSUPP;
561
562 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
563 pool_index, threshold, extack);
564}
565
566static int
567dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
568 unsigned int sb_index, u16 tc_index,
569 enum devlink_sb_pool_type pool_type,
570 u16 *p_pool_index, u32 *p_threshold)
571{
572 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
573 int port = dsa_devlink_port_to_port(dlp);
574
575 if (!ds->ops->devlink_sb_tc_pool_bind_get)
576 return -EOPNOTSUPP;
577
578 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
579 tc_index, pool_type,
580 p_pool_index, p_threshold);
581}
582
583static int
584dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
585 unsigned int sb_index, u16 tc_index,
586 enum devlink_sb_pool_type pool_type,
587 u16 pool_index, u32 threshold,
588 struct netlink_ext_ack *extack)
589{
590 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
591 int port = dsa_devlink_port_to_port(dlp);
592
593 if (!ds->ops->devlink_sb_tc_pool_bind_set)
594 return -EOPNOTSUPP;
595
596 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
597 tc_index, pool_type,
598 pool_index, threshold,
599 extack);
600}
601
602static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
603 unsigned int sb_index)
604{
605 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
606
607 if (!ds->ops->devlink_sb_occ_snapshot)
608 return -EOPNOTSUPP;
609
610 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
611}
612
613static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
614 unsigned int sb_index)
615{
616 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
617
618 if (!ds->ops->devlink_sb_occ_max_clear)
619 return -EOPNOTSUPP;
620
621 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
622}
623
624static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
625 unsigned int sb_index,
626 u16 pool_index, u32 *p_cur,
627 u32 *p_max)
628{
629 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
630 int port = dsa_devlink_port_to_port(dlp);
631
632 if (!ds->ops->devlink_sb_occ_port_pool_get)
633 return -EOPNOTSUPP;
634
635 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
636 pool_index, p_cur, p_max);
637}
638
639static int
640dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
641 unsigned int sb_index, u16 tc_index,
642 enum devlink_sb_pool_type pool_type,
643 u32 *p_cur, u32 *p_max)
644{
645 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
646 int port = dsa_devlink_port_to_port(dlp);
647
648 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
649 return -EOPNOTSUPP;
650
651 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
652 sb_index, tc_index,
653 pool_type, p_cur,
654 p_max);
655}
656
Andrew Lunn0f06b852020-09-18 21:11:08 +0200657static const struct devlink_ops dsa_devlink_ops = {
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200658 .info_get = dsa_devlink_info_get,
659 .sb_pool_get = dsa_devlink_sb_pool_get,
660 .sb_pool_set = dsa_devlink_sb_pool_set,
661 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
662 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
663 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
664 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
665 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
666 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
667 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
668 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
Andrew Lunn0f06b852020-09-18 21:11:08 +0200669};
670
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200671static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
672{
673 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
674 struct dsa_switch_tree *dst = ds->dst;
675 int port, err;
676
677 if (tag_ops->proto == dst->default_proto)
678 return 0;
679
680 for (port = 0; port < ds->num_ports; port++) {
681 if (!dsa_is_cpu_port(ds, port))
682 continue;
683
684 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
685 if (err) {
686 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
687 tag_ops->name, ERR_PTR(err));
688 return err;
689 }
690 }
691
692 return 0;
693}
694
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500695static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200696{
Andrew Lunn6b297522019-10-25 01:03:51 +0200697 struct dsa_devlink_priv *dl_priv;
Andrew Lunn31224332020-10-04 18:12:53 +0200698 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400699 int err;
700
701 if (ds->setup)
702 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200703
Florian Fainelli6e830d82016-06-07 16:32:39 -0700704 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400705 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700706 * the slave MDIO bus driver rely on these values for probing PHY
707 * devices or not
708 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400709 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700710
Andrew Lunn96567d52017-03-28 23:45:07 +0200711 /* Add the switch to devlink before calling setup, so that setup can
712 * add dpipe tables
713 */
Andrew Lunn6b297522019-10-25 01:03:51 +0200714 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
Andrew Lunn96567d52017-03-28 23:45:07 +0200715 if (!ds->devlink)
716 return -ENOMEM;
Andrew Lunn6b297522019-10-25 01:03:51 +0200717 dl_priv = devlink_priv(ds->devlink);
718 dl_priv->ds = ds;
Andrew Lunn96567d52017-03-28 23:45:07 +0200719
720 err = devlink_register(ds->devlink, ds->dev);
721 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300722 goto free_devlink;
Andrew Lunn96567d52017-03-28 23:45:07 +0200723
Andrew Lunn31224332020-10-04 18:12:53 +0200724 /* Setup devlink port instances now, so that the switch
725 * setup() can register regions etc, against the ports
726 */
727 list_for_each_entry(dp, &ds->dst->ports, list) {
728 if (dp->ds == ds) {
729 err = dsa_port_devlink_setup(dp);
730 if (err)
731 goto unregister_devlink_ports;
732 }
733 }
734
Vivien Didelotf515f192017-02-03 13:20:20 -0500735 err = dsa_switch_register_notifier(ds);
736 if (err)
Andrew Lunn31224332020-10-04 18:12:53 +0200737 goto unregister_devlink_ports;
Vivien Didelotf515f192017-02-03 13:20:20 -0500738
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200739 ds->configure_vlan_while_not_filtering = true;
740
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300741 err = ds->ops->setup(ds);
742 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300743 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300744
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200745 err = dsa_switch_setup_tag_protocol(ds);
746 if (err)
747 goto teardown;
748
Andrew Lunn6b297522019-10-25 01:03:51 +0200749 devlink_params_publish(ds->devlink);
750
Vivien Didelot9d490b42016-08-23 12:38:56 -0400751 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700752 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300753 if (!ds->slave_mii_bus) {
754 err = -ENOMEM;
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200755 goto teardown;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300756 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700757
758 dsa_slave_mii_bus_init(ds);
759
760 err = mdiobus_register(ds->slave_mii_bus);
761 if (err < 0)
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200762 goto teardown;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700763 }
764
Vivien Didelotfb35c602019-10-21 16:51:19 -0400765 ds->setup = true;
766
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200767 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300768
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200769teardown:
770 if (ds->ops->teardown)
771 ds->ops->teardown(ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300772unregister_notifier:
773 dsa_switch_unregister_notifier(ds);
Andrew Lunn31224332020-10-04 18:12:53 +0200774unregister_devlink_ports:
775 list_for_each_entry(dp, &ds->dst->ports, list)
776 if (dp->ds == ds)
777 dsa_port_devlink_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300778 devlink_unregister(ds->devlink);
779free_devlink:
780 devlink_free(ds->devlink);
781 ds->devlink = NULL;
782
783 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200784}
785
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500786static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200787{
Andrew Lunn31224332020-10-04 18:12:53 +0200788 struct dsa_port *dp;
789
Vivien Didelotfb35c602019-10-21 16:51:19 -0400790 if (!ds->setup)
791 return;
792
Vivien Didelot9d490b42016-08-23 12:38:56 -0400793 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700794 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500795
796 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200797
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300798 if (ds->ops->teardown)
799 ds->ops->teardown(ds);
800
Andrew Lunn96567d52017-03-28 23:45:07 +0200801 if (ds->devlink) {
Andrew Lunn31224332020-10-04 18:12:53 +0200802 list_for_each_entry(dp, &ds->dst->ports, list)
803 if (dp->ds == ds)
804 dsa_port_devlink_teardown(dp);
Andrew Lunn96567d52017-03-28 23:45:07 +0200805 devlink_unregister(ds->devlink);
806 devlink_free(ds->devlink);
807 ds->devlink = NULL;
808 }
809
Vivien Didelotfb35c602019-10-21 16:51:19 -0400810 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200811}
812
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500813static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
814{
Vivien Didelot1d277322017-11-06 16:11:48 -0500815 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400816 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500817
Vivien Didelotfb35c602019-10-21 16:51:19 -0400818 list_for_each_entry(dp, &dst->ports, list) {
819 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500820 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400821 goto teardown;
822 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500823
Vivien Didelotfb35c602019-10-21 16:51:19 -0400824 list_for_each_entry(dp, &dst->ports, list) {
825 err = dsa_port_setup(dp);
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300826 if (err) {
827 dsa_port_devlink_teardown(dp);
828 dp->type = DSA_PORT_TYPE_UNUSED;
829 err = dsa_port_devlink_setup(dp);
830 if (err)
831 goto teardown;
Florian Fainelli86f8b1c2020-05-03 20:50:57 -0700832 continue;
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300833 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500834 }
835
836 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300837
Vivien Didelotfb35c602019-10-21 16:51:19 -0400838teardown:
839 list_for_each_entry(dp, &dst->ports, list)
840 dsa_port_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300841
Vivien Didelotfb35c602019-10-21 16:51:19 -0400842 list_for_each_entry(dp, &dst->ports, list)
843 dsa_switch_teardown(dp->ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300844
845 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500846}
847
848static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
849{
Vivien Didelot1d277322017-11-06 16:11:48 -0500850 struct dsa_port *dp;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500851
Vivien Didelotfb35c602019-10-21 16:51:19 -0400852 list_for_each_entry(dp, &dst->ports, list)
853 dsa_port_teardown(dp);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500854
Vivien Didelotfb35c602019-10-21 16:51:19 -0400855 list_for_each_entry(dp, &dst->ports, list)
856 dsa_switch_teardown(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500857}
858
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500859static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
860{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400861 struct dsa_port *dp;
862 int err;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500863
Vivien Didelot0cfec582019-10-21 16:51:22 -0400864 list_for_each_entry(dp, &dst->ports, list) {
865 if (dsa_port_is_cpu(dp)) {
866 err = dsa_master_setup(dp->master, dp);
867 if (err)
868 return err;
869 }
870 }
871
872 return 0;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500873}
874
875static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
876{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400877 struct dsa_port *dp;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500878
Vivien Didelot0cfec582019-10-21 16:51:22 -0400879 list_for_each_entry(dp, &dst->ports, list)
880 if (dsa_port_is_cpu(dp))
881 dsa_master_teardown(dp->master);
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500882}
883
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100884static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
885{
886 unsigned int len = 0;
887 struct dsa_port *dp;
888
889 list_for_each_entry(dp, &dst->ports, list) {
890 if (dp->ds->num_lag_ids > len)
891 len = dp->ds->num_lag_ids;
892 }
893
894 if (!len)
895 return 0;
896
897 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
898 if (!dst->lags)
899 return -ENOMEM;
900
901 dst->lags_len = len;
902 return 0;
903}
904
905static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
906{
907 kfree(dst->lags);
908}
909
Vivien Didelotec15dd42017-11-06 16:11:46 -0500910static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200911{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500912 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200913 int err;
914
Vivien Didelotec15dd42017-11-06 16:11:46 -0500915 if (dst->setup) {
916 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
917 dst->index);
918 return -EEXIST;
919 }
920
Vivien Didelot34c09a82017-11-06 16:11:51 -0500921 complete = dsa_tree_setup_routing_table(dst);
922 if (!complete)
923 return 0;
924
Vivien Didelotf0704642017-11-06 16:11:44 -0500925 err = dsa_tree_setup_default_cpu(dst);
926 if (err)
927 return err;
928
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500929 err = dsa_tree_setup_switches(dst);
930 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300931 goto teardown_default_cpu;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200932
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500933 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400934 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300935 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -0400936
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100937 err = dsa_tree_setup_lags(dst);
938 if (err)
939 goto teardown_master;
940
Vivien Didelotec15dd42017-11-06 16:11:46 -0500941 dst->setup = true;
942
943 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200944
945 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300946
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100947teardown_master:
948 dsa_tree_teardown_master(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300949teardown_switches:
950 dsa_tree_teardown_switches(dst);
951teardown_default_cpu:
952 dsa_tree_teardown_default_cpu(dst);
953
954 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200955}
956
Vivien Didelotec15dd42017-11-06 16:11:46 -0500957static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200958{
Vivien Didelotc5f51762019-10-30 22:09:13 -0400959 struct dsa_link *dl, *next;
960
Vivien Didelotec15dd42017-11-06 16:11:46 -0500961 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200962 return;
963
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100964 dsa_tree_teardown_lags(dst);
965
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500966 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200967
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500968 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200969
Vivien Didelotf0704642017-11-06 16:11:44 -0500970 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700971
Vivien Didelotc5f51762019-10-30 22:09:13 -0400972 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
973 list_del(&dl->list);
974 kfree(dl);
975 }
976
Vivien Didelotec15dd42017-11-06 16:11:46 -0500977 pr_info("DSA: tree %d torn down\n", dst->index);
978
979 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200980}
981
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200982/* Since the dsa/tagging sysfs device attribute is per master, the assumption
983 * is that all DSA switches within a tree share the same tagger, otherwise
984 * they would have formed disjoint trees (different "dsa,member" values).
985 */
986int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
987 struct net_device *master,
988 const struct dsa_device_ops *tag_ops,
989 const struct dsa_device_ops *old_tag_ops)
990{
991 struct dsa_notifier_tag_proto_info info;
992 struct dsa_port *dp;
993 int err = -EBUSY;
994
995 if (!rtnl_trylock())
996 return restart_syscall();
997
998 /* At the moment we don't allow changing the tag protocol under
999 * traffic. The rtnl_mutex also happens to serialize concurrent
1000 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1001 * restriction, there needs to be another mutex which serializes this.
1002 */
1003 if (master->flags & IFF_UP)
1004 goto out_unlock;
1005
1006 list_for_each_entry(dp, &dst->ports, list) {
1007 if (!dsa_is_user_port(dp->ds, dp->index))
1008 continue;
1009
1010 if (dp->slave->flags & IFF_UP)
1011 goto out_unlock;
1012 }
1013
1014 info.tag_ops = tag_ops;
1015 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1016 if (err)
1017 goto out_unwind_tagger;
1018
1019 dst->tag_ops = tag_ops;
1020
1021 rtnl_unlock();
1022
1023 return 0;
1024
1025out_unwind_tagger:
1026 info.tag_ops = old_tag_ops;
1027 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1028out_unlock:
1029 rtnl_unlock();
1030 return err;
1031}
1032
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001033static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1034{
1035 struct dsa_switch_tree *dst = ds->dst;
1036 struct dsa_port *dp;
1037
Vivien Didelot05f294a2019-10-21 16:51:29 -04001038 list_for_each_entry(dp, &dst->ports, list)
1039 if (dp->ds == ds && dp->index == index)
1040 return dp;
1041
1042 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1043 if (!dp)
1044 return NULL;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001045
1046 dp->ds = ds;
1047 dp->index = index;
1048
1049 INIT_LIST_HEAD(&dp->list);
1050 list_add_tail(&dp->list, &dst->ports);
1051
1052 return dp;
1053}
1054
Vivien Didelot06e24d02017-11-03 19:05:29 -04001055static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1056{
1057 if (!name)
1058 name = "eth%d";
1059
1060 dp->type = DSA_PORT_TYPE_USER;
1061 dp->name = name;
1062
1063 return 0;
1064}
1065
1066static int dsa_port_parse_dsa(struct dsa_port *dp)
1067{
1068 dp->type = DSA_PORT_TYPE_DSA;
1069
1070 return 0;
1071}
1072
Florian Fainelli4d776482020-01-07 21:06:05 -08001073static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1074 struct net_device *master)
1075{
1076 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1077 struct dsa_switch *mds, *ds = dp->ds;
1078 unsigned int mdp_upstream;
1079 struct dsa_port *mdp;
1080
1081 /* It is possible to stack DSA switches onto one another when that
1082 * happens the switch driver may want to know if its tagging protocol
1083 * is going to work in such a configuration.
1084 */
1085 if (dsa_slave_dev_check(master)) {
1086 mdp = dsa_slave_to_port(master);
1087 mds = mdp->ds;
1088 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1089 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1090 DSA_TAG_PROTO_NONE);
1091 }
1092
1093 /* If the master device is not itself a DSA slave in a disjoint DSA
1094 * tree, then return immediately.
1095 */
1096 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1097}
1098
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001099static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1100 const char *user_protocol)
Vivien Didelot06e24d02017-11-03 19:05:29 -04001101{
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001102 struct dsa_switch *ds = dp->ds;
1103 struct dsa_switch_tree *dst = ds->dst;
George McCollistere0c755a2021-03-22 15:26:50 -05001104 const struct dsa_device_ops *tag_ops;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001105 enum dsa_tag_protocol default_proto;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001106
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001107 /* Find out which protocol the switch would prefer. */
1108 default_proto = dsa_get_tag_protocol(dp, master);
1109 if (dst->default_proto) {
1110 if (dst->default_proto != default_proto) {
Vladimir Oltean357f2032021-01-29 03:00:05 +02001111 dev_err(ds->dev,
1112 "A DSA switch tree can have only one tagging protocol\n");
1113 return -EINVAL;
1114 }
Vladimir Oltean357f2032021-01-29 03:00:05 +02001115 } else {
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001116 dst->default_proto = default_proto;
1117 }
1118
1119 /* See if the user wants to override that preference. */
1120 if (user_protocol) {
1121 if (!ds->ops->change_tag_protocol) {
1122 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1123 return -EINVAL;
Vladimir Oltean357f2032021-01-29 03:00:05 +02001124 }
George McCollistere0c755a2021-03-22 15:26:50 -05001125
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001126 tag_ops = dsa_find_tagger_by_name(user_protocol);
1127 } else {
1128 tag_ops = dsa_tag_driver_get(default_proto);
1129 }
1130
1131 if (IS_ERR(tag_ops)) {
1132 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1133 return -EPROBE_DEFER;
1134
1135 dev_warn(ds->dev, "No tagger for this switch\n");
1136 return PTR_ERR(tag_ops);
1137 }
1138
1139 if (dst->tag_ops) {
1140 if (dst->tag_ops != tag_ops) {
1141 dev_err(ds->dev,
1142 "A DSA switch tree can have only one tagging protocol\n");
1143
1144 dsa_tag_driver_put(tag_ops);
1145 return -EINVAL;
1146 }
1147
1148 /* In the case of multiple CPU ports per switch, the tagging
1149 * protocol is still reference-counted only per switch tree.
1150 */
1151 dsa_tag_driver_put(tag_ops);
1152 } else {
George McCollistere0c755a2021-03-22 15:26:50 -05001153 dst->tag_ops = tag_ops;
Florian Fainelli9f9e7722017-07-24 10:49:23 -07001154 }
1155
Florian Fainelli4d776482020-01-07 21:06:05 -08001156 dp->master = master;
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001157 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001158 dsa_port_set_tag_protocol(dp, dst->tag_ops);
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001159 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -04001160
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001161 /* At this point, the tree may be configured to use a different
1162 * tagger than the one chosen by the switch driver during
1163 * .setup, in the case when a user selects a custom protocol
1164 * through the DT.
1165 *
1166 * This is resolved by syncing the driver with the tree in
1167 * dsa_switch_setup_tag_protocol once .setup has run and the
1168 * driver is ready to accept calls to .change_tag_protocol. If
1169 * the driver does not support the custom protocol at that
1170 * point, the tree is wholly rejected, thereby ensuring that the
1171 * tree and driver are always in agreement on the protocol to
1172 * use.
1173 */
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001174 return 0;
1175}
1176
Vivien Didelotfd223e22017-10-27 15:55:14 -04001177static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1178{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001179 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -04001180 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -04001181 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001182
Vivien Didelot06e24d02017-11-03 19:05:29 -04001183 dp->dn = dn;
1184
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001185 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001186 struct net_device *master;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001187 const char *user_protocol;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001188
1189 master = of_find_net_device_by_node(ethernet);
1190 if (!master)
1191 return -EPROBE_DEFER;
1192
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001193 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1194 return dsa_port_parse_cpu(dp, master, user_protocol);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001195 }
1196
Vivien Didelot06e24d02017-11-03 19:05:29 -04001197 if (link)
1198 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001199
Vivien Didelot06e24d02017-11-03 19:05:29 -04001200 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001201}
1202
Vivien Didelot975e6e32017-11-03 19:05:27 -04001203static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1204 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001205{
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001206 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001207 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +08001208 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001209 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001210
1211 ports = of_get_child_by_name(dn, "ports");
1212 if (!ports) {
Kurt Kanzenbach85e05d22020-07-20 14:49:39 +02001213 /* The second possibility is "ethernet-ports" */
1214 ports = of_get_child_by_name(dn, "ethernet-ports");
1215 if (!ports) {
1216 dev_err(ds->dev, "no ports child node found\n");
1217 return -EINVAL;
1218 }
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001219 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001220
1221 for_each_available_child_of_node(ports, port) {
1222 err = of_property_read_u32(port, "reg", &reg);
1223 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001224 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001225
Wen Yang9919a362019-02-25 15:22:19 +08001226 if (reg >= ds->num_ports) {
Rafał Miłecki8209f5b2021-01-06 10:09:15 +01001227 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1228 port, reg, ds->num_ports);
Wen Yang9919a362019-02-25 15:22:19 +08001229 err = -EINVAL;
1230 goto out_put_node;
1231 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001232
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001233 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001234
1235 err = dsa_port_parse_of(dp, port);
1236 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001237 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001238 }
1239
Wen Yang9919a362019-02-25 15:22:19 +08001240out_put_node:
1241 of_node_put(ports);
1242 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001243}
1244
Vivien Didelot975e6e32017-11-03 19:05:27 -04001245static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1246 struct device_node *dn)
1247{
1248 u32 m[2] = { 0, 0 };
1249 int sz;
1250
1251 /* Don't error out if this optional property isn't found */
1252 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1253 if (sz < 0 && sz != -EINVAL)
1254 return sz;
1255
1256 ds->index = m[1];
Vivien Didelot975e6e32017-11-03 19:05:27 -04001257
1258 ds->dst = dsa_tree_touch(m[0]);
1259 if (!ds->dst)
1260 return -ENOMEM;
1261
1262 return 0;
1263}
1264
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001265static int dsa_switch_touch_ports(struct dsa_switch *ds)
1266{
1267 struct dsa_port *dp;
1268 int port;
1269
1270 for (port = 0; port < ds->num_ports; port++) {
1271 dp = dsa_port_touch(ds, port);
1272 if (!dp)
1273 return -ENOMEM;
1274 }
1275
1276 return 0;
1277}
1278
Vivien Didelot975e6e32017-11-03 19:05:27 -04001279static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1280{
1281 int err;
1282
1283 err = dsa_switch_parse_member_of(ds, dn);
1284 if (err)
1285 return err;
1286
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001287 err = dsa_switch_touch_ports(ds);
1288 if (err)
1289 return err;
1290
Vivien Didelot975e6e32017-11-03 19:05:27 -04001291 return dsa_switch_parse_ports_of(ds, dn);
1292}
1293
Vivien Didelotfd223e22017-10-27 15:55:14 -04001294static int dsa_port_parse(struct dsa_port *dp, const char *name,
1295 struct device *dev)
1296{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001297 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001298 struct net_device *master;
1299
1300 master = dsa_dev_to_net_device(dev);
1301 if (!master)
1302 return -EPROBE_DEFER;
1303
1304 dev_put(master);
1305
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001306 return dsa_port_parse_cpu(dp, master, NULL);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001307 }
1308
Vivien Didelot06e24d02017-11-03 19:05:29 -04001309 if (!strcmp(name, "dsa"))
1310 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001311
Vivien Didelot06e24d02017-11-03 19:05:29 -04001312 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001313}
1314
Vivien Didelot975e6e32017-11-03 19:05:27 -04001315static int dsa_switch_parse_ports(struct dsa_switch *ds,
1316 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001317{
1318 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001319 struct dsa_port *dp;
1320 struct device *dev;
1321 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001322 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001323 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001324
1325 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -04001326 name = cd->port_names[i];
1327 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001328 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001329
1330 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001331 continue;
1332
Vivien Didelotfd223e22017-10-27 15:55:14 -04001333 err = dsa_port_parse(dp, name, dev);
1334 if (err)
1335 return err;
1336
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001337 valid_name_found = true;
1338 }
1339
1340 if (!valid_name_found && i == DSA_MAX_PORTS)
1341 return -EINVAL;
1342
1343 return 0;
1344}
1345
Vivien Didelot975e6e32017-11-03 19:05:27 -04001346static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001347{
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001348 int err;
1349
Vivien Didelot975e6e32017-11-03 19:05:27 -04001350 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001351
Vivien Didelot975e6e32017-11-03 19:05:27 -04001352 /* We don't support interconnected switches nor multiple trees via
1353 * platform data, so this is the unique switch of the tree.
1354 */
1355 ds->index = 0;
1356 ds->dst = dsa_tree_touch(0);
1357 if (!ds->dst)
1358 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001359
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001360 err = dsa_switch_touch_ports(ds);
1361 if (err)
1362 return err;
1363
Vivien Didelot975e6e32017-11-03 19:05:27 -04001364 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001365}
1366
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001367static void dsa_switch_release_ports(struct dsa_switch *ds)
1368{
1369 struct dsa_switch_tree *dst = ds->dst;
1370 struct dsa_port *dp, *next;
1371
1372 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1373 if (dp->ds != ds)
1374 continue;
1375 list_del(&dp->list);
1376 kfree(dp);
1377 }
1378}
1379
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001380static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001381{
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001382 struct dsa_switch_tree *dst;
Colin Ian King556f1242019-10-24 11:32:18 +01001383 struct dsa_chip_data *pdata;
1384 struct device_node *np;
Vivien Didelot34c09a82017-11-06 16:11:51 -05001385 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001386
Vivien Didelot7e99e342019-10-21 16:51:30 -04001387 if (!ds->dev)
1388 return -ENODEV;
1389
Colin Ian King556f1242019-10-24 11:32:18 +01001390 pdata = ds->dev->platform_data;
1391 np = ds->dev->of_node;
1392
Vivien Didelot7e99e342019-10-21 16:51:30 -04001393 if (!ds->num_ports)
1394 return -EINVAL;
1395
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001396 if (np) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001397 err = dsa_switch_parse_of(ds, np);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001398 if (err)
1399 dsa_switch_release_ports(ds);
1400 } else if (pdata) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001401 err = dsa_switch_parse(ds, pdata);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001402 if (err)
1403 dsa_switch_release_ports(ds);
1404 } else {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001405 err = -ENODEV;
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001406 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001407
Vivien Didelot975e6e32017-11-03 19:05:27 -04001408 if (err)
1409 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001410
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001411 dst = ds->dst;
1412 dsa_tree_get(dst);
1413 err = dsa_tree_setup(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001414 if (err) {
1415 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001416 dsa_tree_put(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001417 }
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001418
1419 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001420}
1421
Vivien Didelot23c9ee42017-05-26 18:12:51 -04001422int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001423{
1424 int err;
1425
1426 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001427 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -05001428 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001429 mutex_unlock(&dsa2_mutex);
1430
1431 return err;
1432}
1433EXPORT_SYMBOL_GPL(dsa_register_switch);
1434
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001435static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001436{
1437 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot05f294a2019-10-21 16:51:29 -04001438
Florian Fainellic058f6d2019-11-02 20:13:26 -07001439 dsa_tree_teardown(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001440 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001441 dsa_tree_put(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001442}
1443
1444void dsa_unregister_switch(struct dsa_switch *ds)
1445{
1446 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001447 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001448 mutex_unlock(&dsa2_mutex);
1449}
1450EXPORT_SYMBOL_GPL(dsa_unregister_switch);