blob: 2a339fb09f4ecb5faf281ffb40a26a9f7a1cfe17 [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
Vladimir Olteanf5e165e2021-08-19 20:55:00 +030024/* Track the bridges with forwarding offload enabled */
25static unsigned long dsa_fwd_offloading_bridges;
26
Tobias Waldekranz058102a2021-01-13 09:42:53 +010027/**
Vladimir Oltean886f8e22021-01-29 03:00:04 +020028 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
29 * @dst: collection of struct dsa_switch devices to notify.
30 * @e: event, must be of type DSA_NOTIFIER_*
31 * @v: event-specific value.
32 *
33 * Given a struct dsa_switch_tree, this can be used to run a function once for
34 * each member DSA switch. The other alternative of traversing the tree is only
35 * through its ports list, which does not uniquely list the switches.
36 */
37int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
38{
39 struct raw_notifier_head *nh = &dst->nh;
40 int err;
41
42 err = raw_notifier_call_chain(nh, e, v);
43
44 return notifier_to_errno(err);
45}
46
47/**
48 * dsa_broadcast - Notify all DSA trees in the system.
49 * @e: event, must be of type DSA_NOTIFIER_*
50 * @v: event-specific value.
51 *
52 * Can be used to notify the switching fabric of events such as cross-chip
53 * bridging between disjoint trees (such as islands of tagger-compatible
54 * switches bridged by an incompatible middle switch).
Vladimir Oltean724395f2021-08-11 16:46:06 +030055 *
56 * WARNING: this function is not reliable during probe time, because probing
57 * between trees is asynchronous and not all DSA trees might have probed.
Vladimir Oltean886f8e22021-01-29 03:00:04 +020058 */
59int dsa_broadcast(unsigned long e, void *v)
60{
61 struct dsa_switch_tree *dst;
62 int err = 0;
63
64 list_for_each_entry(dst, &dsa_tree_list, list) {
65 err = dsa_tree_notify(dst, e, v);
66 if (err)
67 break;
68 }
69
70 return err;
71}
72
73/**
Tobias Waldekranz058102a2021-01-13 09:42:53 +010074 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
75 * @dst: Tree in which to record the mapping.
76 * @lag: Netdev that is to be mapped to an ID.
77 *
78 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
79 * two spaces. The size of the mapping space is determined by the
80 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
81 * it unset if it is not needed, in which case these functions become
82 * no-ops.
83 */
84void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
85{
86 unsigned int id;
87
88 if (dsa_lag_id(dst, lag) >= 0)
89 /* Already mapped */
90 return;
91
92 for (id = 0; id < dst->lags_len; id++) {
93 if (!dsa_lag_dev(dst, id)) {
94 dst->lags[id] = lag;
95 return;
96 }
97 }
98
99 /* No IDs left, which is OK. Some drivers do not need it. The
100 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
101 * returns an error for this device when joining the LAG. The
102 * driver can then return -EOPNOTSUPP back to DSA, which will
103 * fall back to a software LAG.
104 */
105}
106
107/**
108 * dsa_lag_unmap() - Remove a LAG ID mapping
109 * @dst: Tree in which the mapping is recorded.
110 * @lag: Netdev that was mapped.
111 *
112 * As there may be multiple users of the mapping, it is only removed
113 * if there are no other references to it.
114 */
115void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
116{
117 struct dsa_port *dp;
118 unsigned int id;
119
120 dsa_lag_foreach_port(dp, dst, lag)
121 /* There are remaining users of this mapping */
122 return;
123
124 dsa_lags_foreach_id(id, dst) {
125 if (dsa_lag_dev(dst, id) == lag) {
126 dst->lags[id] = NULL;
127 break;
128 }
129 }
130}
131
Vladimir Olteanf5e165e2021-08-19 20:55:00 +0300132static int dsa_bridge_num_find(const struct net_device *bridge_dev)
133{
134 struct dsa_switch_tree *dst;
135 struct dsa_port *dp;
136
137 /* When preparing the offload for a port, it will have a valid
138 * dp->bridge_dev pointer but a not yet valid dp->bridge_num.
139 * However there might be other ports having the same dp->bridge_dev
140 * and a valid dp->bridge_num, so just ignore this port.
141 */
142 list_for_each_entry(dst, &dsa_tree_list, list)
143 list_for_each_entry(dp, &dst->ports, list)
144 if (dp->bridge_dev == bridge_dev &&
145 dp->bridge_num != -1)
146 return dp->bridge_num;
147
148 return -1;
149}
150
151int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
152{
153 int bridge_num = dsa_bridge_num_find(bridge_dev);
154
155 if (bridge_num < 0) {
156 /* First port that offloads TX forwarding for this bridge */
157 bridge_num = find_first_zero_bit(&dsa_fwd_offloading_bridges,
158 DSA_MAX_NUM_OFFLOADING_BRIDGES);
159 if (bridge_num >= max)
160 return -1;
161
162 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
163 }
164
165 return bridge_num;
166}
167
168void dsa_bridge_num_put(const struct net_device *bridge_dev, int bridge_num)
169{
170 /* Check if the bridge is still in use, otherwise it is time
171 * to clean it up so we can reuse this bridge_num later.
172 */
Vladimir Oltean1bec0f02021-10-07 19:47:08 +0300173 if (dsa_bridge_num_find(bridge_dev) < 0)
Vladimir Olteanf5e165e2021-08-19 20:55:00 +0300174 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
175}
176
Vladimir Oltean3b7bc1f2020-05-10 19:37:42 +0300177struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
178{
179 struct dsa_switch_tree *dst;
180 struct dsa_port *dp;
181
182 list_for_each_entry(dst, &dsa_tree_list, list) {
183 if (dst->index != tree_index)
184 continue;
185
186 list_for_each_entry(dp, &dst->ports, list) {
187 if (dp->ds->index != sw_index)
188 continue;
189
190 return dp->ds;
191 }
192 }
193
194 return NULL;
195}
196EXPORT_SYMBOL_GPL(dsa_switch_find);
197
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400198static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200199{
200 struct dsa_switch_tree *dst;
201
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400202 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400203 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200204 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400205
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200206 return NULL;
207}
208
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400209static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200210{
211 struct dsa_switch_tree *dst;
212
213 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
214 if (!dst)
215 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400216
Vivien Didelot49463b72017-11-03 19:05:21 -0400217 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400218
Vivien Didelotc5f51762019-10-30 22:09:13 -0400219 INIT_LIST_HEAD(&dst->rtable);
220
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400221 INIT_LIST_HEAD(&dst->ports);
222
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200223 INIT_LIST_HEAD(&dst->list);
Vivien Didelot50c7d2ba2019-10-18 17:02:46 -0400224 list_add_tail(&dst->list, &dsa_tree_list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400225
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200226 kref_init(&dst->refcount);
227
228 return dst;
229}
230
Vivien Didelot65254102017-11-03 19:05:23 -0400231static void dsa_tree_free(struct dsa_switch_tree *dst)
232{
Vladimir Oltean357f2032021-01-29 03:00:05 +0200233 if (dst->tag_ops)
234 dsa_tag_driver_put(dst->tag_ops);
Vivien Didelot65254102017-11-03 19:05:23 -0400235 list_del(&dst->list);
236 kfree(dst);
237}
238
Vivien Didelot9e741042017-11-24 11:36:06 -0500239static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
240{
241 if (dst)
242 kref_get(&dst->refcount);
243
244 return dst;
245}
246
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400247static struct dsa_switch_tree *dsa_tree_touch(int index)
248{
249 struct dsa_switch_tree *dst;
250
251 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -0500252 if (dst)
253 return dsa_tree_get(dst);
254 else
255 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -0400256}
257
258static void dsa_tree_release(struct kref *ref)
259{
260 struct dsa_switch_tree *dst;
261
262 dst = container_of(ref, struct dsa_switch_tree, refcount);
263
264 dsa_tree_free(dst);
265}
266
267static void dsa_tree_put(struct dsa_switch_tree *dst)
268{
Vivien Didelot9e741042017-11-24 11:36:06 -0500269 if (dst)
270 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -0400271}
272
Vivien Didelotf163da82017-11-06 16:11:49 -0500273static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
274 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200275{
Vivien Didelotf163da82017-11-06 16:11:49 -0500276 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200277
Vivien Didelot764b7e62019-10-21 16:51:21 -0400278 list_for_each_entry(dp, &dst->ports, list)
279 if (dp->dn == dn)
280 return dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200281
282 return NULL;
283}
284
Ben Dooks (Codethink)4e2ce6e2019-12-17 11:20:38 +0000285static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
286 struct dsa_port *link_dp)
Vivien Didelotc5f51762019-10-30 22:09:13 -0400287{
288 struct dsa_switch *ds = dp->ds;
289 struct dsa_switch_tree *dst;
290 struct dsa_link *dl;
291
292 dst = ds->dst;
293
294 list_for_each_entry(dl, &dst->rtable, list)
295 if (dl->dp == dp && dl->link_dp == link_dp)
296 return dl;
297
298 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
299 if (!dl)
300 return NULL;
301
302 dl->dp = dp;
303 dl->link_dp = link_dp;
304
305 INIT_LIST_HEAD(&dl->list);
306 list_add_tail(&dl->list, &dst->rtable);
307
308 return dl;
309}
310
Vivien Didelot34c09a82017-11-06 16:11:51 -0500311static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200312{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500313 struct dsa_switch *ds = dp->ds;
314 struct dsa_switch_tree *dst = ds->dst;
315 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500316 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500317 struct dsa_port *link_dp;
Vivien Didelotc5f51762019-10-30 22:09:13 -0400318 struct dsa_link *dl;
Vivien Didelotc5286662017-11-06 16:11:50 -0500319 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200320
Vivien Didelotc5286662017-11-06 16:11:50 -0500321 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
322 link_dp = dsa_tree_find_port_by_node(dst, it.node);
323 if (!link_dp) {
324 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500325 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500326 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200327
Vivien Didelotc5f51762019-10-30 22:09:13 -0400328 dl = dsa_link_touch(dp, link_dp);
329 if (!dl) {
330 of_node_put(it.node);
331 return false;
332 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200333 }
334
Vivien Didelot34c09a82017-11-06 16:11:51 -0500335 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200336}
337
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400338static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200339{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500340 bool complete = true;
341 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200342
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400343 list_for_each_entry(dp, &dst->ports, list) {
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400344 if (dsa_port_is_dsa(dp)) {
Vivien Didelot34c09a82017-11-06 16:11:51 -0500345 complete = dsa_port_setup_routing_table(dp);
346 if (!complete)
347 break;
348 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200349 }
350
Vivien Didelot34c09a82017-11-06 16:11:51 -0500351 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200352}
353
Vivien Didelotf0704642017-11-06 16:11:44 -0500354static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
355{
Vivien Didelotf0704642017-11-06 16:11:44 -0500356 struct dsa_port *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500357
Vivien Didelotc0b73622019-10-21 16:51:23 -0400358 list_for_each_entry(dp, &dst->ports, list)
359 if (dsa_port_is_cpu(dp))
360 return dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500361
362 return NULL;
363}
364
Vladimir Oltean2c0b0322021-08-04 16:54:30 +0300365/* Assign the default CPU port (the first one in the tree) to all ports of the
366 * fabric which don't already have one as part of their own switch.
367 */
Vivien Didelotf0704642017-11-06 16:11:44 -0500368static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
369{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400370 struct dsa_port *cpu_dp, *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500371
Vivien Didelotda4561c2019-10-21 16:51:24 -0400372 cpu_dp = dsa_tree_find_first_cpu(dst);
373 if (!cpu_dp) {
374 pr_err("DSA: tree %d has no CPU port\n", dst->index);
Vivien Didelotf0704642017-11-06 16:11:44 -0500375 return -EINVAL;
376 }
377
Vladimir Oltean2c0b0322021-08-04 16:54:30 +0300378 list_for_each_entry(dp, &dst->ports, list) {
379 if (dp->cpu_dp)
380 continue;
381
Vivien Didelotda4561c2019-10-21 16:51:24 -0400382 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
383 dp->cpu_dp = cpu_dp;
Vladimir Oltean2c0b0322021-08-04 16:54:30 +0300384 }
Vivien Didelotf0704642017-11-06 16:11:44 -0500385
386 return 0;
387}
388
Vladimir Oltean2c0b0322021-08-04 16:54:30 +0300389/* Perform initial assignment of CPU ports to user ports and DSA links in the
390 * fabric, giving preference to CPU ports local to each switch. Default to
391 * using the first CPU port in the switch tree if the port does not have a CPU
392 * port local to this switch.
393 */
394static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
395{
396 struct dsa_port *cpu_dp, *dp;
397
398 list_for_each_entry(cpu_dp, &dst->ports, list) {
399 if (!dsa_port_is_cpu(cpu_dp))
400 continue;
401
Vladimir Oltean65c563a2021-10-20 20:49:51 +0300402 /* Prefer a local CPU port */
403 dsa_switch_for_each_port(dp, cpu_dp->ds) {
Vladimir Oltean2c0b0322021-08-04 16:54:30 +0300404 /* Prefer the first local CPU port found */
405 if (dp->cpu_dp)
406 continue;
407
408 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
409 dp->cpu_dp = cpu_dp;
410 }
411 }
412
413 return dsa_tree_setup_default_cpu(dst);
414}
415
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +0300416static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
Vivien Didelotf0704642017-11-06 16:11:44 -0500417{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400418 struct dsa_port *dp;
419
420 list_for_each_entry(dp, &dst->ports, list)
421 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
422 dp->cpu_dp = NULL;
Vivien Didelotf0704642017-11-06 16:11:44 -0500423}
424
Vivien Didelot1d277322017-11-06 16:11:48 -0500425static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200426{
Vivien Didelot955222c2019-08-19 16:00:48 -0400427 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300428 bool dsa_port_link_registered = false;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300429 struct dsa_switch *ds = dp->ds;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300430 bool dsa_port_enabled = false;
431 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200432
Vivien Didelotfb35c602019-10-21 16:51:19 -0400433 if (dp->setup)
434 return 0;
435
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300436 INIT_LIST_HEAD(&dp->fdbs);
Vladimir Oltean161ca592021-06-29 17:06:50 +0300437 INIT_LIST_HEAD(&dp->mdbs);
438
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300439 if (ds->ops->port_setup) {
440 err = ds->ops->port_setup(ds, dp->index);
441 if (err)
442 return err;
443 }
444
Vivien Didelot1d277322017-11-06 16:11:48 -0500445 switch (dp->type) {
446 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400447 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500448 break;
449 case DSA_PORT_TYPE_CPU:
Jiri Pirkoda077392018-05-18 09:29:03 +0200450 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300451 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300452 break;
453 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400454
455 err = dsa_port_enable(dp, NULL);
456 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300457 break;
458 dsa_port_enabled = true;
459
Jiri Pirkoda077392018-05-18 09:29:03 +0200460 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500461 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100462 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300463 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300464 break;
465 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400466
467 err = dsa_port_enable(dp, NULL);
468 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300469 break;
470 dsa_port_enabled = true;
471
Vivien Didelot1d277322017-11-06 16:11:48 -0500472 break;
473 case DSA_PORT_TYPE_USER:
Michael Walle83216e32021-04-12 19:47:17 +0200474 of_get_mac_address(dp->dn, dp->mac);
Vivien Didelot1d277322017-11-06 16:11:48 -0500475 err = dsa_slave_create(dp);
476 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300477 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400478
479 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500480 break;
481 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200482
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300483 if (err && dsa_port_enabled)
484 dsa_port_disable(dp);
485 if (err && dsa_port_link_registered)
486 dsa_port_link_unregister_of(dp);
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300487 if (err) {
488 if (ds->ops->port_teardown)
489 ds->ops->port_teardown(ds, dp->index);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400490 return err;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300491 }
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300492
Vivien Didelotfb35c602019-10-21 16:51:19 -0400493 dp->setup = true;
494
495 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200496}
497
Andrew Lunn31224332020-10-04 18:12:53 +0200498static int dsa_port_devlink_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200499{
Vivien Didelot955222c2019-08-19 16:00:48 -0400500 struct devlink_port *dlp = &dp->devlink_port;
Andrew Lunn31224332020-10-04 18:12:53 +0200501 struct dsa_switch_tree *dst = dp->ds->dst;
502 struct devlink_port_attrs attrs = {};
503 struct devlink *dl = dp->ds->devlink;
504 const unsigned char *id;
505 unsigned char len;
506 int err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500507
Andrew Lunn31224332020-10-04 18:12:53 +0200508 id = (const unsigned char *)&dst->index;
509 len = sizeof(dst->index);
510
511 attrs.phys.port_number = dp->index;
512 memcpy(attrs.switch_id.id, id, len);
513 attrs.switch_id.id_len = len;
514 memset(dlp, 0, sizeof(*dlp));
515
516 switch (dp->type) {
517 case DSA_PORT_TYPE_UNUSED:
518 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
519 break;
520 case DSA_PORT_TYPE_CPU:
521 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
522 break;
523 case DSA_PORT_TYPE_DSA:
524 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
525 break;
526 case DSA_PORT_TYPE_USER:
527 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
528 break;
529 }
530
531 devlink_port_attrs_set(dlp, &attrs);
532 err = devlink_port_register(dl, dlp, dp->index);
533
534 if (!err)
535 dp->devlink_port_setup = true;
536
537 return err;
538}
539
540static void dsa_port_teardown(struct dsa_port *dp)
541{
Vladimir Oltean91158e12021-01-12 02:48:31 +0200542 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300543 struct dsa_switch *ds = dp->ds;
Vladimir Oltean161ca592021-06-29 17:06:50 +0300544 struct dsa_mac_addr *a, *tmp;
Vladimir Oltean91158e12021-01-12 02:48:31 +0200545
Vivien Didelotfb35c602019-10-21 16:51:19 -0400546 if (!dp->setup)
547 return;
548
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300549 if (ds->ops->port_teardown)
550 ds->ops->port_teardown(ds, dp->index);
551
Vladimir Oltean91158e12021-01-12 02:48:31 +0200552 devlink_port_type_clear(dlp);
553
Vivien Didelot1d277322017-11-06 16:11:48 -0500554 switch (dp->type) {
555 case DSA_PORT_TYPE_UNUSED:
556 break;
557 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400558 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400559 dsa_port_link_unregister_of(dp);
560 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500561 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400562 dsa_port_disable(dp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100563 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500564 break;
565 case DSA_PORT_TYPE_USER:
566 if (dp->slave) {
567 dsa_slave_destroy(dp->slave);
568 dp->slave = NULL;
569 }
570 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200571 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400572
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300573 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
574 list_del(&a->list);
575 kfree(a);
576 }
577
Vladimir Oltean161ca592021-06-29 17:06:50 +0300578 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
579 list_del(&a->list);
580 kfree(a);
581 }
582
Vivien Didelotfb35c602019-10-21 16:51:19 -0400583 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200584}
585
Andrew Lunn31224332020-10-04 18:12:53 +0200586static void dsa_port_devlink_teardown(struct dsa_port *dp)
587{
588 struct devlink_port *dlp = &dp->devlink_port;
589
590 if (dp->devlink_port_setup)
591 devlink_port_unregister(dlp);
592 dp->devlink_port_setup = false;
593}
594
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300595/* Destroy the current devlink port, and create a new one which has the UNUSED
596 * flavour. At this point, any call to ds->ops->port_setup has been already
597 * balanced out by a call to ds->ops->port_teardown, so we know that any
598 * devlink port regions the driver had are now unregistered. We then call its
599 * ds->ops->port_setup again, in order for the driver to re-create them on the
600 * new devlink port.
601 */
602static int dsa_port_reinit_as_unused(struct dsa_port *dp)
603{
604 struct dsa_switch *ds = dp->ds;
605 int err;
606
607 dsa_port_devlink_teardown(dp);
608 dp->type = DSA_PORT_TYPE_UNUSED;
609 err = dsa_port_devlink_setup(dp);
610 if (err)
611 return err;
612
613 if (ds->ops->port_setup) {
614 /* On error, leave the devlink port registered,
615 * dsa_switch_teardown will clean it up later.
616 */
617 err = ds->ops->port_setup(ds, dp->index);
618 if (err)
619 return err;
620 }
621
622 return 0;
623}
624
Andrew Lunn0f06b852020-09-18 21:11:08 +0200625static int dsa_devlink_info_get(struct devlink *dl,
626 struct devlink_info_req *req,
627 struct netlink_ext_ack *extack)
628{
629 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
630
631 if (ds->ops->devlink_info_get)
632 return ds->ops->devlink_info_get(ds, req, extack);
633
634 return -EOPNOTSUPP;
635}
636
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200637static int dsa_devlink_sb_pool_get(struct devlink *dl,
638 unsigned int sb_index, u16 pool_index,
639 struct devlink_sb_pool_info *pool_info)
640{
641 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
642
643 if (!ds->ops->devlink_sb_pool_get)
644 return -EOPNOTSUPP;
645
646 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
647 pool_info);
648}
649
650static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
651 u16 pool_index, u32 size,
652 enum devlink_sb_threshold_type threshold_type,
653 struct netlink_ext_ack *extack)
654{
655 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
656
657 if (!ds->ops->devlink_sb_pool_set)
658 return -EOPNOTSUPP;
659
660 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
661 threshold_type, extack);
662}
663
664static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
665 unsigned int sb_index, u16 pool_index,
666 u32 *p_threshold)
667{
668 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
669 int port = dsa_devlink_port_to_port(dlp);
670
671 if (!ds->ops->devlink_sb_port_pool_get)
672 return -EOPNOTSUPP;
673
674 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
675 pool_index, p_threshold);
676}
677
678static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
679 unsigned int sb_index, u16 pool_index,
680 u32 threshold,
681 struct netlink_ext_ack *extack)
682{
683 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
684 int port = dsa_devlink_port_to_port(dlp);
685
686 if (!ds->ops->devlink_sb_port_pool_set)
687 return -EOPNOTSUPP;
688
689 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
690 pool_index, threshold, extack);
691}
692
693static int
694dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
695 unsigned int sb_index, u16 tc_index,
696 enum devlink_sb_pool_type pool_type,
697 u16 *p_pool_index, u32 *p_threshold)
698{
699 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
700 int port = dsa_devlink_port_to_port(dlp);
701
702 if (!ds->ops->devlink_sb_tc_pool_bind_get)
703 return -EOPNOTSUPP;
704
705 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
706 tc_index, pool_type,
707 p_pool_index, p_threshold);
708}
709
710static int
711dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
712 unsigned int sb_index, u16 tc_index,
713 enum devlink_sb_pool_type pool_type,
714 u16 pool_index, u32 threshold,
715 struct netlink_ext_ack *extack)
716{
717 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
718 int port = dsa_devlink_port_to_port(dlp);
719
720 if (!ds->ops->devlink_sb_tc_pool_bind_set)
721 return -EOPNOTSUPP;
722
723 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
724 tc_index, pool_type,
725 pool_index, threshold,
726 extack);
727}
728
729static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
730 unsigned int sb_index)
731{
732 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
733
734 if (!ds->ops->devlink_sb_occ_snapshot)
735 return -EOPNOTSUPP;
736
737 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
738}
739
740static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
741 unsigned int sb_index)
742{
743 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
744
745 if (!ds->ops->devlink_sb_occ_max_clear)
746 return -EOPNOTSUPP;
747
748 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
749}
750
751static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
752 unsigned int sb_index,
753 u16 pool_index, u32 *p_cur,
754 u32 *p_max)
755{
756 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
757 int port = dsa_devlink_port_to_port(dlp);
758
759 if (!ds->ops->devlink_sb_occ_port_pool_get)
760 return -EOPNOTSUPP;
761
762 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
763 pool_index, p_cur, p_max);
764}
765
766static int
767dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
768 unsigned int sb_index, u16 tc_index,
769 enum devlink_sb_pool_type pool_type,
770 u32 *p_cur, u32 *p_max)
771{
772 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
773 int port = dsa_devlink_port_to_port(dlp);
774
775 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
776 return -EOPNOTSUPP;
777
778 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
779 sb_index, tc_index,
780 pool_type, p_cur,
781 p_max);
782}
783
Andrew Lunn0f06b852020-09-18 21:11:08 +0200784static const struct devlink_ops dsa_devlink_ops = {
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200785 .info_get = dsa_devlink_info_get,
786 .sb_pool_get = dsa_devlink_sb_pool_get,
787 .sb_pool_set = dsa_devlink_sb_pool_set,
788 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
789 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
790 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
791 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
792 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
793 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
794 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
795 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
Andrew Lunn0f06b852020-09-18 21:11:08 +0200796};
797
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200798static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
799{
800 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
801 struct dsa_switch_tree *dst = ds->dst;
Vladimir Olteand0004a02021-10-20 20:49:50 +0300802 struct dsa_port *cpu_dp;
803 int err;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200804
805 if (tag_ops->proto == dst->default_proto)
806 return 0;
807
Vladimir Olteand0004a02021-10-20 20:49:50 +0300808 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
Vladimir Oltean1951b3f2021-10-09 15:26:07 +0300809 rtnl_lock();
Vladimir Olteand0004a02021-10-20 20:49:50 +0300810 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
811 tag_ops->proto);
Vladimir Oltean1951b3f2021-10-09 15:26:07 +0300812 rtnl_unlock();
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200813 if (err) {
814 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
815 tag_ops->name, ERR_PTR(err));
816 return err;
817 }
818 }
819
820 return 0;
821}
822
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500823static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200824{
Andrew Lunn6b297522019-10-25 01:03:51 +0200825 struct dsa_devlink_priv *dl_priv;
Andrew Lunn31224332020-10-04 18:12:53 +0200826 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400827 int err;
828
829 if (ds->setup)
830 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200831
Florian Fainelli6e830d82016-06-07 16:32:39 -0700832 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400833 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700834 * the slave MDIO bus driver rely on these values for probing PHY
835 * devices or not
836 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400837 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700838
Andrew Lunn96567d52017-03-28 23:45:07 +0200839 /* Add the switch to devlink before calling setup, so that setup can
840 * add dpipe tables
841 */
Leon Romanovsky919d13a2021-08-08 21:57:43 +0300842 ds->devlink =
843 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
Andrew Lunn96567d52017-03-28 23:45:07 +0200844 if (!ds->devlink)
845 return -ENOMEM;
Andrew Lunn6b297522019-10-25 01:03:51 +0200846 dl_priv = devlink_priv(ds->devlink);
847 dl_priv->ds = ds;
Andrew Lunn96567d52017-03-28 23:45:07 +0200848
Andrew Lunn31224332020-10-04 18:12:53 +0200849 /* Setup devlink port instances now, so that the switch
850 * setup() can register regions etc, against the ports
851 */
Vladimir Oltean65c563a2021-10-20 20:49:51 +0300852 dsa_switch_for_each_port(dp, ds) {
853 err = dsa_port_devlink_setup(dp);
854 if (err)
855 goto unregister_devlink_ports;
Andrew Lunn31224332020-10-04 18:12:53 +0200856 }
857
Vivien Didelotf515f192017-02-03 13:20:20 -0500858 err = dsa_switch_register_notifier(ds);
859 if (err)
Andrew Lunn31224332020-10-04 18:12:53 +0200860 goto unregister_devlink_ports;
Vivien Didelotf515f192017-02-03 13:20:20 -0500861
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200862 ds->configure_vlan_while_not_filtering = true;
863
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300864 err = ds->ops->setup(ds);
865 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300866 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300867
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200868 err = dsa_switch_setup_tag_protocol(ds);
869 if (err)
870 goto teardown;
871
Vivien Didelot9d490b42016-08-23 12:38:56 -0400872 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Vladimir Oltean5135e962021-09-21 00:42:08 +0300873 ds->slave_mii_bus = mdiobus_alloc();
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300874 if (!ds->slave_mii_bus) {
875 err = -ENOMEM;
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200876 goto teardown;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300877 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700878
879 dsa_slave_mii_bus_init(ds);
880
881 err = mdiobus_register(ds->slave_mii_bus);
882 if (err < 0)
Vladimir Oltean5135e962021-09-21 00:42:08 +0300883 goto free_slave_mii_bus;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700884 }
885
Vivien Didelotfb35c602019-10-21 16:51:19 -0400886 ds->setup = true;
Leon Romanovskybd936bd2021-09-25 14:23:01 +0300887 devlink_register(ds->devlink);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200888 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300889
Vladimir Oltean5135e962021-09-21 00:42:08 +0300890free_slave_mii_bus:
891 if (ds->slave_mii_bus && ds->ops->phy_read)
892 mdiobus_free(ds->slave_mii_bus);
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200893teardown:
894 if (ds->ops->teardown)
895 ds->ops->teardown(ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300896unregister_notifier:
897 dsa_switch_unregister_notifier(ds);
Andrew Lunn31224332020-10-04 18:12:53 +0200898unregister_devlink_ports:
Vladimir Oltean65c563a2021-10-20 20:49:51 +0300899 dsa_switch_for_each_port(dp, ds)
900 dsa_port_devlink_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300901 devlink_free(ds->devlink);
902 ds->devlink = NULL;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300903 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200904}
905
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500906static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200907{
Andrew Lunn31224332020-10-04 18:12:53 +0200908 struct dsa_port *dp;
909
Vivien Didelotfb35c602019-10-21 16:51:19 -0400910 if (!ds->setup)
911 return;
912
Leon Romanovskybd936bd2021-09-25 14:23:01 +0300913 if (ds->devlink)
914 devlink_unregister(ds->devlink);
915
Vladimir Oltean5135e962021-09-21 00:42:08 +0300916 if (ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700917 mdiobus_unregister(ds->slave_mii_bus);
Vladimir Oltean5135e962021-09-21 00:42:08 +0300918 mdiobus_free(ds->slave_mii_bus);
919 ds->slave_mii_bus = NULL;
920 }
Vivien Didelotf515f192017-02-03 13:20:20 -0500921
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300922 if (ds->ops->teardown)
923 ds->ops->teardown(ds);
924
Vladimir Oltean39e222b2021-10-12 15:37:35 +0300925 dsa_switch_unregister_notifier(ds);
926
Andrew Lunn96567d52017-03-28 23:45:07 +0200927 if (ds->devlink) {
Vladimir Oltean65c563a2021-10-20 20:49:51 +0300928 dsa_switch_for_each_port(dp, ds)
929 dsa_port_devlink_teardown(dp);
Andrew Lunn96567d52017-03-28 23:45:07 +0200930 devlink_free(ds->devlink);
931 ds->devlink = NULL;
932 }
933
Vivien Didelotfb35c602019-10-21 16:51:19 -0400934 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200935}
936
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300937/* First tear down the non-shared, then the shared ports. This ensures that
938 * all work items scheduled by our switchdev handlers for user ports have
939 * completed before we destroy the refcounting kept on the shared ports.
940 */
941static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
942{
943 struct dsa_port *dp;
944
945 list_for_each_entry(dp, &dst->ports, list)
946 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
947 dsa_port_teardown(dp);
948
949 dsa_flush_workqueue();
950
951 list_for_each_entry(dp, &dst->ports, list)
952 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
953 dsa_port_teardown(dp);
954}
955
956static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
957{
958 struct dsa_port *dp;
959
960 list_for_each_entry(dp, &dst->ports, list)
961 dsa_switch_teardown(dp->ds);
962}
963
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500964static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
965{
Vivien Didelot1d277322017-11-06 16:11:48 -0500966 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400967 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500968
Vivien Didelotfb35c602019-10-21 16:51:19 -0400969 list_for_each_entry(dp, &dst->ports, list) {
970 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500971 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400972 goto teardown;
973 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500974
Vivien Didelotfb35c602019-10-21 16:51:19 -0400975 list_for_each_entry(dp, &dst->ports, list) {
976 err = dsa_port_setup(dp);
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300977 if (err) {
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300978 err = dsa_port_reinit_as_unused(dp);
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300979 if (err)
980 goto teardown;
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300981 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500982 }
983
984 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300985
Vivien Didelotfb35c602019-10-21 16:51:19 -0400986teardown:
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300987 dsa_tree_teardown_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300988
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300989 dsa_tree_teardown_switches(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300990
991 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500992}
993
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500994static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
995{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400996 struct dsa_port *dp;
997 int err;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500998
Vivien Didelot0cfec582019-10-21 16:51:22 -0400999 list_for_each_entry(dp, &dst->ports, list) {
1000 if (dsa_port_is_cpu(dp)) {
1001 err = dsa_master_setup(dp->master, dp);
1002 if (err)
1003 return err;
1004 }
1005 }
1006
1007 return 0;
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001008}
1009
1010static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1011{
Vivien Didelot0cfec582019-10-21 16:51:22 -04001012 struct dsa_port *dp;
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001013
Vivien Didelot0cfec582019-10-21 16:51:22 -04001014 list_for_each_entry(dp, &dst->ports, list)
1015 if (dsa_port_is_cpu(dp))
1016 dsa_master_teardown(dp->master);
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001017}
1018
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001019static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1020{
1021 unsigned int len = 0;
1022 struct dsa_port *dp;
1023
1024 list_for_each_entry(dp, &dst->ports, list) {
1025 if (dp->ds->num_lag_ids > len)
1026 len = dp->ds->num_lag_ids;
1027 }
1028
1029 if (!len)
1030 return 0;
1031
1032 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1033 if (!dst->lags)
1034 return -ENOMEM;
1035
1036 dst->lags_len = len;
1037 return 0;
1038}
1039
1040static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1041{
1042 kfree(dst->lags);
1043}
1044
Vivien Didelotec15dd42017-11-06 16:11:46 -05001045static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001046{
Vivien Didelot34c09a82017-11-06 16:11:51 -05001047 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001048 int err;
1049
Vivien Didelotec15dd42017-11-06 16:11:46 -05001050 if (dst->setup) {
1051 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1052 dst->index);
1053 return -EEXIST;
1054 }
1055
Vivien Didelot34c09a82017-11-06 16:11:51 -05001056 complete = dsa_tree_setup_routing_table(dst);
1057 if (!complete)
1058 return 0;
1059
Vladimir Oltean2c0b0322021-08-04 16:54:30 +03001060 err = dsa_tree_setup_cpu_ports(dst);
Vivien Didelotf0704642017-11-06 16:11:44 -05001061 if (err)
1062 return err;
1063
Vivien Didelot1f08f9e2017-11-06 16:11:47 -05001064 err = dsa_tree_setup_switches(dst);
1065 if (err)
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001066 goto teardown_cpu_ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001067
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001068 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -04001069 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001070 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -04001071
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001072 err = dsa_tree_setup_lags(dst);
1073 if (err)
1074 goto teardown_master;
1075
Vivien Didelotec15dd42017-11-06 16:11:46 -05001076 dst->setup = true;
1077
1078 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001079
1080 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001081
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001082teardown_master:
1083 dsa_tree_teardown_master(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001084teardown_switches:
Vladimir Olteane5845aa2021-09-21 01:49:18 +03001085 dsa_tree_teardown_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001086 dsa_tree_teardown_switches(dst);
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001087teardown_cpu_ports:
1088 dsa_tree_teardown_cpu_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001089
1090 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001091}
1092
Vivien Didelotec15dd42017-11-06 16:11:46 -05001093static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001094{
Vivien Didelotc5f51762019-10-30 22:09:13 -04001095 struct dsa_link *dl, *next;
1096
Vivien Didelotec15dd42017-11-06 16:11:46 -05001097 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001098 return;
1099
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001100 dsa_tree_teardown_lags(dst);
1101
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001102 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001103
Vladimir Olteana57d8c212021-09-14 16:47:26 +03001104 dsa_tree_teardown_ports(dst);
1105
Vivien Didelot1f08f9e2017-11-06 16:11:47 -05001106 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001107
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001108 dsa_tree_teardown_cpu_ports(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -07001109
Vivien Didelotc5f51762019-10-30 22:09:13 -04001110 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1111 list_del(&dl->list);
1112 kfree(dl);
1113 }
1114
Vivien Didelotec15dd42017-11-06 16:11:46 -05001115 pr_info("DSA: tree %d torn down\n", dst->index);
1116
1117 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001118}
1119
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001120/* Since the dsa/tagging sysfs device attribute is per master, the assumption
1121 * is that all DSA switches within a tree share the same tagger, otherwise
1122 * they would have formed disjoint trees (different "dsa,member" values).
1123 */
1124int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1125 struct net_device *master,
1126 const struct dsa_device_ops *tag_ops,
1127 const struct dsa_device_ops *old_tag_ops)
1128{
1129 struct dsa_notifier_tag_proto_info info;
1130 struct dsa_port *dp;
1131 int err = -EBUSY;
1132
1133 if (!rtnl_trylock())
1134 return restart_syscall();
1135
1136 /* At the moment we don't allow changing the tag protocol under
1137 * traffic. The rtnl_mutex also happens to serialize concurrent
1138 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1139 * restriction, there needs to be another mutex which serializes this.
1140 */
1141 if (master->flags & IFF_UP)
1142 goto out_unlock;
1143
1144 list_for_each_entry(dp, &dst->ports, list) {
Vladimir Olteand0004a02021-10-20 20:49:50 +03001145 if (!dsa_port_is_user(dp))
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001146 continue;
1147
1148 if (dp->slave->flags & IFF_UP)
1149 goto out_unlock;
1150 }
1151
1152 info.tag_ops = tag_ops;
1153 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1154 if (err)
1155 goto out_unwind_tagger;
1156
1157 dst->tag_ops = tag_ops;
1158
1159 rtnl_unlock();
1160
1161 return 0;
1162
1163out_unwind_tagger:
1164 info.tag_ops = old_tag_ops;
1165 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1166out_unlock:
1167 rtnl_unlock();
1168 return err;
1169}
1170
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001171static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1172{
1173 struct dsa_switch_tree *dst = ds->dst;
1174 struct dsa_port *dp;
1175
Vladimir Oltean65c563a2021-10-20 20:49:51 +03001176 dsa_switch_for_each_port(dp, ds)
1177 if (dp->index == index)
Vivien Didelot05f294a2019-10-21 16:51:29 -04001178 return dp;
1179
1180 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1181 if (!dp)
1182 return NULL;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001183
1184 dp->ds = ds;
1185 dp->index = index;
Vladimir Oltean123abc062021-07-22 18:55:40 +03001186 dp->bridge_num = -1;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001187
1188 INIT_LIST_HEAD(&dp->list);
1189 list_add_tail(&dp->list, &dst->ports);
1190
1191 return dp;
1192}
1193
Vivien Didelot06e24d02017-11-03 19:05:29 -04001194static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1195{
1196 if (!name)
1197 name = "eth%d";
1198
1199 dp->type = DSA_PORT_TYPE_USER;
1200 dp->name = name;
1201
1202 return 0;
1203}
1204
1205static int dsa_port_parse_dsa(struct dsa_port *dp)
1206{
1207 dp->type = DSA_PORT_TYPE_DSA;
1208
1209 return 0;
1210}
1211
Florian Fainelli4d776482020-01-07 21:06:05 -08001212static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1213 struct net_device *master)
1214{
1215 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1216 struct dsa_switch *mds, *ds = dp->ds;
1217 unsigned int mdp_upstream;
1218 struct dsa_port *mdp;
1219
1220 /* It is possible to stack DSA switches onto one another when that
1221 * happens the switch driver may want to know if its tagging protocol
1222 * is going to work in such a configuration.
1223 */
1224 if (dsa_slave_dev_check(master)) {
1225 mdp = dsa_slave_to_port(master);
1226 mds = mdp->ds;
1227 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1228 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1229 DSA_TAG_PROTO_NONE);
1230 }
1231
1232 /* If the master device is not itself a DSA slave in a disjoint DSA
1233 * tree, then return immediately.
1234 */
1235 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1236}
1237
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001238static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1239 const char *user_protocol)
Vivien Didelot06e24d02017-11-03 19:05:29 -04001240{
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001241 struct dsa_switch *ds = dp->ds;
1242 struct dsa_switch_tree *dst = ds->dst;
George McCollistere0c755a2021-03-22 15:26:50 -05001243 const struct dsa_device_ops *tag_ops;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001244 enum dsa_tag_protocol default_proto;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001245
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001246 /* Find out which protocol the switch would prefer. */
1247 default_proto = dsa_get_tag_protocol(dp, master);
1248 if (dst->default_proto) {
1249 if (dst->default_proto != default_proto) {
Vladimir Oltean357f2032021-01-29 03:00:05 +02001250 dev_err(ds->dev,
1251 "A DSA switch tree can have only one tagging protocol\n");
1252 return -EINVAL;
1253 }
Vladimir Oltean357f2032021-01-29 03:00:05 +02001254 } else {
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001255 dst->default_proto = default_proto;
1256 }
1257
1258 /* See if the user wants to override that preference. */
1259 if (user_protocol) {
1260 if (!ds->ops->change_tag_protocol) {
1261 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1262 return -EINVAL;
Vladimir Oltean357f2032021-01-29 03:00:05 +02001263 }
George McCollistere0c755a2021-03-22 15:26:50 -05001264
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001265 tag_ops = dsa_find_tagger_by_name(user_protocol);
1266 } else {
1267 tag_ops = dsa_tag_driver_get(default_proto);
1268 }
1269
1270 if (IS_ERR(tag_ops)) {
1271 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1272 return -EPROBE_DEFER;
1273
1274 dev_warn(ds->dev, "No tagger for this switch\n");
1275 return PTR_ERR(tag_ops);
1276 }
1277
1278 if (dst->tag_ops) {
1279 if (dst->tag_ops != tag_ops) {
1280 dev_err(ds->dev,
1281 "A DSA switch tree can have only one tagging protocol\n");
1282
1283 dsa_tag_driver_put(tag_ops);
1284 return -EINVAL;
1285 }
1286
1287 /* In the case of multiple CPU ports per switch, the tagging
1288 * protocol is still reference-counted only per switch tree.
1289 */
1290 dsa_tag_driver_put(tag_ops);
1291 } else {
George McCollistere0c755a2021-03-22 15:26:50 -05001292 dst->tag_ops = tag_ops;
Florian Fainelli9f9e7722017-07-24 10:49:23 -07001293 }
1294
Florian Fainelli4d776482020-01-07 21:06:05 -08001295 dp->master = master;
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001296 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001297 dsa_port_set_tag_protocol(dp, dst->tag_ops);
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001298 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -04001299
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001300 /* At this point, the tree may be configured to use a different
1301 * tagger than the one chosen by the switch driver during
1302 * .setup, in the case when a user selects a custom protocol
1303 * through the DT.
1304 *
1305 * This is resolved by syncing the driver with the tree in
1306 * dsa_switch_setup_tag_protocol once .setup has run and the
1307 * driver is ready to accept calls to .change_tag_protocol. If
1308 * the driver does not support the custom protocol at that
1309 * point, the tree is wholly rejected, thereby ensuring that the
1310 * tree and driver are always in agreement on the protocol to
1311 * use.
1312 */
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001313 return 0;
1314}
1315
Vivien Didelotfd223e22017-10-27 15:55:14 -04001316static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1317{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001318 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -04001319 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -04001320 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001321
Vivien Didelot06e24d02017-11-03 19:05:29 -04001322 dp->dn = dn;
1323
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001324 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001325 struct net_device *master;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001326 const char *user_protocol;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001327
1328 master = of_find_net_device_by_node(ethernet);
1329 if (!master)
1330 return -EPROBE_DEFER;
1331
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001332 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1333 return dsa_port_parse_cpu(dp, master, user_protocol);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001334 }
1335
Vivien Didelot06e24d02017-11-03 19:05:29 -04001336 if (link)
1337 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001338
Vivien Didelot06e24d02017-11-03 19:05:29 -04001339 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001340}
1341
Vivien Didelot975e6e32017-11-03 19:05:27 -04001342static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1343 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001344{
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001345 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001346 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +08001347 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001348 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001349
1350 ports = of_get_child_by_name(dn, "ports");
1351 if (!ports) {
Kurt Kanzenbach85e05d22020-07-20 14:49:39 +02001352 /* The second possibility is "ethernet-ports" */
1353 ports = of_get_child_by_name(dn, "ethernet-ports");
1354 if (!ports) {
1355 dev_err(ds->dev, "no ports child node found\n");
1356 return -EINVAL;
1357 }
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001358 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001359
1360 for_each_available_child_of_node(ports, port) {
1361 err = of_property_read_u32(port, "reg", &reg);
1362 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001363 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001364
Wen Yang9919a362019-02-25 15:22:19 +08001365 if (reg >= ds->num_ports) {
Rafał Miłecki8209f5b2021-01-06 10:09:15 +01001366 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1367 port, reg, ds->num_ports);
Wen Yang9919a362019-02-25 15:22:19 +08001368 err = -EINVAL;
1369 goto out_put_node;
1370 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001371
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001372 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001373
1374 err = dsa_port_parse_of(dp, port);
1375 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001376 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001377 }
1378
Wen Yang9919a362019-02-25 15:22:19 +08001379out_put_node:
1380 of_node_put(ports);
1381 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001382}
1383
Vivien Didelot975e6e32017-11-03 19:05:27 -04001384static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1385 struct device_node *dn)
1386{
1387 u32 m[2] = { 0, 0 };
1388 int sz;
1389
1390 /* Don't error out if this optional property isn't found */
1391 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1392 if (sz < 0 && sz != -EINVAL)
1393 return sz;
1394
1395 ds->index = m[1];
Vivien Didelot975e6e32017-11-03 19:05:27 -04001396
1397 ds->dst = dsa_tree_touch(m[0]);
1398 if (!ds->dst)
1399 return -ENOMEM;
1400
Vladimir Oltean8674f8d2021-06-21 19:42:14 +03001401 if (dsa_switch_find(ds->dst->index, ds->index)) {
1402 dev_err(ds->dev,
1403 "A DSA switch with index %d already exists in tree %d\n",
1404 ds->index, ds->dst->index);
1405 return -EEXIST;
1406 }
1407
Vladimir Oltean5b22d362021-07-22 18:55:39 +03001408 if (ds->dst->last_switch < ds->index)
1409 ds->dst->last_switch = ds->index;
1410
Vivien Didelot975e6e32017-11-03 19:05:27 -04001411 return 0;
1412}
1413
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001414static int dsa_switch_touch_ports(struct dsa_switch *ds)
1415{
1416 struct dsa_port *dp;
1417 int port;
1418
1419 for (port = 0; port < ds->num_ports; port++) {
1420 dp = dsa_port_touch(ds, port);
1421 if (!dp)
1422 return -ENOMEM;
1423 }
1424
1425 return 0;
1426}
1427
Vivien Didelot975e6e32017-11-03 19:05:27 -04001428static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1429{
1430 int err;
1431
1432 err = dsa_switch_parse_member_of(ds, dn);
1433 if (err)
1434 return err;
1435
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001436 err = dsa_switch_touch_ports(ds);
1437 if (err)
1438 return err;
1439
Vivien Didelot975e6e32017-11-03 19:05:27 -04001440 return dsa_switch_parse_ports_of(ds, dn);
1441}
1442
Vivien Didelotfd223e22017-10-27 15:55:14 -04001443static int dsa_port_parse(struct dsa_port *dp, const char *name,
1444 struct device *dev)
1445{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001446 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001447 struct net_device *master;
1448
1449 master = dsa_dev_to_net_device(dev);
1450 if (!master)
1451 return -EPROBE_DEFER;
1452
1453 dev_put(master);
1454
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001455 return dsa_port_parse_cpu(dp, master, NULL);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001456 }
1457
Vivien Didelot06e24d02017-11-03 19:05:29 -04001458 if (!strcmp(name, "dsa"))
1459 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001460
Vivien Didelot06e24d02017-11-03 19:05:29 -04001461 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001462}
1463
Vivien Didelot975e6e32017-11-03 19:05:27 -04001464static int dsa_switch_parse_ports(struct dsa_switch *ds,
1465 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001466{
1467 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001468 struct dsa_port *dp;
1469 struct device *dev;
1470 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001471 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001472 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001473
1474 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -04001475 name = cd->port_names[i];
1476 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001477 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001478
1479 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001480 continue;
1481
Vivien Didelotfd223e22017-10-27 15:55:14 -04001482 err = dsa_port_parse(dp, name, dev);
1483 if (err)
1484 return err;
1485
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001486 valid_name_found = true;
1487 }
1488
1489 if (!valid_name_found && i == DSA_MAX_PORTS)
1490 return -EINVAL;
1491
1492 return 0;
1493}
1494
Vivien Didelot975e6e32017-11-03 19:05:27 -04001495static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001496{
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001497 int err;
1498
Vivien Didelot975e6e32017-11-03 19:05:27 -04001499 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001500
Vivien Didelot975e6e32017-11-03 19:05:27 -04001501 /* We don't support interconnected switches nor multiple trees via
1502 * platform data, so this is the unique switch of the tree.
1503 */
1504 ds->index = 0;
1505 ds->dst = dsa_tree_touch(0);
1506 if (!ds->dst)
1507 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001508
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001509 err = dsa_switch_touch_ports(ds);
1510 if (err)
1511 return err;
1512
Vivien Didelot975e6e32017-11-03 19:05:27 -04001513 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001514}
1515
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001516static void dsa_switch_release_ports(struct dsa_switch *ds)
1517{
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001518 struct dsa_port *dp, *next;
1519
Vladimir Oltean65c563a2021-10-20 20:49:51 +03001520 dsa_switch_for_each_port_safe(dp, next, ds) {
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001521 list_del(&dp->list);
1522 kfree(dp);
1523 }
1524}
1525
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001526static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001527{
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001528 struct dsa_switch_tree *dst;
Colin Ian King556f1242019-10-24 11:32:18 +01001529 struct dsa_chip_data *pdata;
1530 struct device_node *np;
Vivien Didelot34c09a82017-11-06 16:11:51 -05001531 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001532
Vivien Didelot7e99e342019-10-21 16:51:30 -04001533 if (!ds->dev)
1534 return -ENODEV;
1535
Colin Ian King556f1242019-10-24 11:32:18 +01001536 pdata = ds->dev->platform_data;
1537 np = ds->dev->of_node;
1538
Vivien Didelot7e99e342019-10-21 16:51:30 -04001539 if (!ds->num_ports)
1540 return -EINVAL;
1541
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001542 if (np) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001543 err = dsa_switch_parse_of(ds, np);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001544 if (err)
1545 dsa_switch_release_ports(ds);
1546 } else if (pdata) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001547 err = dsa_switch_parse(ds, pdata);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001548 if (err)
1549 dsa_switch_release_ports(ds);
1550 } else {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001551 err = -ENODEV;
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001552 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001553
Vivien Didelot975e6e32017-11-03 19:05:27 -04001554 if (err)
1555 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001556
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001557 dst = ds->dst;
1558 dsa_tree_get(dst);
1559 err = dsa_tree_setup(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001560 if (err) {
1561 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001562 dsa_tree_put(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001563 }
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001564
1565 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001566}
1567
Vivien Didelot23c9ee42017-05-26 18:12:51 -04001568int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001569{
1570 int err;
1571
1572 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001573 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -05001574 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001575 mutex_unlock(&dsa2_mutex);
1576
1577 return err;
1578}
1579EXPORT_SYMBOL_GPL(dsa_register_switch);
1580
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001581static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001582{
1583 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot05f294a2019-10-21 16:51:29 -04001584
Florian Fainellic058f6d2019-11-02 20:13:26 -07001585 dsa_tree_teardown(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001586 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001587 dsa_tree_put(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001588}
1589
1590void dsa_unregister_switch(struct dsa_switch *ds)
1591{
1592 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001593 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001594 mutex_unlock(&dsa2_mutex);
1595}
1596EXPORT_SYMBOL_GPL(dsa_unregister_switch);
Vladimir Oltean0650bf52021-09-17 16:34:33 +03001597
1598/* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1599 * blocking that operation from completion, due to the dev_hold taken inside
1600 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1601 * the DSA master, so that the system can reboot successfully.
1602 */
1603void dsa_switch_shutdown(struct dsa_switch *ds)
1604{
1605 struct net_device *master, *slave_dev;
1606 LIST_HEAD(unregister_list);
1607 struct dsa_port *dp;
1608
1609 mutex_lock(&dsa2_mutex);
1610 rtnl_lock();
1611
Vladimir Oltean65c563a2021-10-20 20:49:51 +03001612 dsa_switch_for_each_user_port(dp, ds) {
Vladimir Oltean0650bf52021-09-17 16:34:33 +03001613 master = dp->cpu_dp->master;
1614 slave_dev = dp->slave;
1615
1616 netdev_upper_dev_unlink(master, slave_dev);
1617 /* Just unlinking ourselves as uppers of the master is not
1618 * sufficient. When the master net device unregisters, that will
1619 * also call dev_close, which we will catch as NETDEV_GOING_DOWN
1620 * and trigger a dev_close on our own devices (dsa_slave_close).
1621 * In turn, that will call dev_mc_unsync on the master's net
1622 * device. If the master is also a DSA switch port, this will
1623 * trigger dsa_slave_set_rx_mode which will call dev_mc_sync on
1624 * its own master. Lockdep will complain about the fact that
1625 * all cascaded masters have the same dsa_master_addr_list_lock_key,
1626 * which it normally would not do if the cascaded masters would
1627 * be in a proper upper/lower relationship, which we've just
1628 * destroyed.
1629 * To suppress the lockdep warnings, let's actually unregister
1630 * the DSA slave interfaces too, to avoid the nonsensical
1631 * multicast address list synchronization on shutdown.
1632 */
1633 unregister_netdevice_queue(slave_dev, &unregister_list);
1634 }
1635 unregister_netdevice_many(&unregister_list);
1636
1637 rtnl_unlock();
1638 mutex_unlock(&dsa2_mutex);
1639}
1640EXPORT_SYMBOL_GPL(dsa_switch_shutdown);