blob: f54639a3a8220a8acac07179b4f1032e88f01e73 [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 */
173 if (!dsa_bridge_num_find(bridge_dev))
174 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
402 list_for_each_entry(dp, &dst->ports, list) {
403 /* Prefer a local CPU port */
404 if (dp->ds != cpu_dp->ds)
405 continue;
406
407 /* Prefer the first local CPU port found */
408 if (dp->cpu_dp)
409 continue;
410
411 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
412 dp->cpu_dp = cpu_dp;
413 }
414 }
415
416 return dsa_tree_setup_default_cpu(dst);
417}
418
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +0300419static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
Vivien Didelotf0704642017-11-06 16:11:44 -0500420{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400421 struct dsa_port *dp;
422
423 list_for_each_entry(dp, &dst->ports, list)
424 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
425 dp->cpu_dp = NULL;
Vivien Didelotf0704642017-11-06 16:11:44 -0500426}
427
Vivien Didelot1d277322017-11-06 16:11:48 -0500428static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200429{
Vivien Didelot955222c2019-08-19 16:00:48 -0400430 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300431 bool dsa_port_link_registered = false;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300432 struct dsa_switch *ds = dp->ds;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300433 bool dsa_port_enabled = false;
434 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200435
Vivien Didelotfb35c602019-10-21 16:51:19 -0400436 if (dp->setup)
437 return 0;
438
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300439 INIT_LIST_HEAD(&dp->fdbs);
Vladimir Oltean161ca592021-06-29 17:06:50 +0300440 INIT_LIST_HEAD(&dp->mdbs);
441
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300442 if (ds->ops->port_setup) {
443 err = ds->ops->port_setup(ds, dp->index);
444 if (err)
445 return err;
446 }
447
Vivien Didelot1d277322017-11-06 16:11:48 -0500448 switch (dp->type) {
449 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400450 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500451 break;
452 case DSA_PORT_TYPE_CPU:
Jiri Pirkoda077392018-05-18 09:29:03 +0200453 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300454 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300455 break;
456 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400457
458 err = dsa_port_enable(dp, NULL);
459 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300460 break;
461 dsa_port_enabled = true;
462
Jiri Pirkoda077392018-05-18 09:29:03 +0200463 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500464 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100465 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300466 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300467 break;
468 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400469
470 err = dsa_port_enable(dp, NULL);
471 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300472 break;
473 dsa_port_enabled = true;
474
Vivien Didelot1d277322017-11-06 16:11:48 -0500475 break;
476 case DSA_PORT_TYPE_USER:
Michael Walle83216e32021-04-12 19:47:17 +0200477 of_get_mac_address(dp->dn, dp->mac);
Vivien Didelot1d277322017-11-06 16:11:48 -0500478 err = dsa_slave_create(dp);
479 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300480 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400481
482 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500483 break;
484 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200485
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300486 if (err && dsa_port_enabled)
487 dsa_port_disable(dp);
488 if (err && dsa_port_link_registered)
489 dsa_port_link_unregister_of(dp);
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300490 if (err) {
491 if (ds->ops->port_teardown)
492 ds->ops->port_teardown(ds, dp->index);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400493 return err;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300494 }
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300495
Vivien Didelotfb35c602019-10-21 16:51:19 -0400496 dp->setup = true;
497
498 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200499}
500
Andrew Lunn31224332020-10-04 18:12:53 +0200501static int dsa_port_devlink_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200502{
Vivien Didelot955222c2019-08-19 16:00:48 -0400503 struct devlink_port *dlp = &dp->devlink_port;
Andrew Lunn31224332020-10-04 18:12:53 +0200504 struct dsa_switch_tree *dst = dp->ds->dst;
505 struct devlink_port_attrs attrs = {};
506 struct devlink *dl = dp->ds->devlink;
507 const unsigned char *id;
508 unsigned char len;
509 int err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500510
Andrew Lunn31224332020-10-04 18:12:53 +0200511 id = (const unsigned char *)&dst->index;
512 len = sizeof(dst->index);
513
514 attrs.phys.port_number = dp->index;
515 memcpy(attrs.switch_id.id, id, len);
516 attrs.switch_id.id_len = len;
517 memset(dlp, 0, sizeof(*dlp));
518
519 switch (dp->type) {
520 case DSA_PORT_TYPE_UNUSED:
521 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
522 break;
523 case DSA_PORT_TYPE_CPU:
524 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
525 break;
526 case DSA_PORT_TYPE_DSA:
527 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
528 break;
529 case DSA_PORT_TYPE_USER:
530 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
531 break;
532 }
533
534 devlink_port_attrs_set(dlp, &attrs);
535 err = devlink_port_register(dl, dlp, dp->index);
536
537 if (!err)
538 dp->devlink_port_setup = true;
539
540 return err;
541}
542
543static void dsa_port_teardown(struct dsa_port *dp)
544{
Vladimir Oltean91158e12021-01-12 02:48:31 +0200545 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300546 struct dsa_switch *ds = dp->ds;
Vladimir Oltean161ca592021-06-29 17:06:50 +0300547 struct dsa_mac_addr *a, *tmp;
Vladimir Oltean91158e12021-01-12 02:48:31 +0200548
Vivien Didelotfb35c602019-10-21 16:51:19 -0400549 if (!dp->setup)
550 return;
551
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300552 if (ds->ops->port_teardown)
553 ds->ops->port_teardown(ds, dp->index);
554
Vladimir Oltean91158e12021-01-12 02:48:31 +0200555 devlink_port_type_clear(dlp);
556
Vivien Didelot1d277322017-11-06 16:11:48 -0500557 switch (dp->type) {
558 case DSA_PORT_TYPE_UNUSED:
559 break;
560 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400561 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400562 dsa_port_link_unregister_of(dp);
563 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500564 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400565 dsa_port_disable(dp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100566 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500567 break;
568 case DSA_PORT_TYPE_USER:
569 if (dp->slave) {
570 dsa_slave_destroy(dp->slave);
571 dp->slave = NULL;
572 }
573 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200574 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400575
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300576 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
577 list_del(&a->list);
578 kfree(a);
579 }
580
Vladimir Oltean161ca592021-06-29 17:06:50 +0300581 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
582 list_del(&a->list);
583 kfree(a);
584 }
585
Vivien Didelotfb35c602019-10-21 16:51:19 -0400586 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200587}
588
Andrew Lunn31224332020-10-04 18:12:53 +0200589static void dsa_port_devlink_teardown(struct dsa_port *dp)
590{
591 struct devlink_port *dlp = &dp->devlink_port;
592
593 if (dp->devlink_port_setup)
594 devlink_port_unregister(dlp);
595 dp->devlink_port_setup = false;
596}
597
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300598/* Destroy the current devlink port, and create a new one which has the UNUSED
599 * flavour. At this point, any call to ds->ops->port_setup has been already
600 * balanced out by a call to ds->ops->port_teardown, so we know that any
601 * devlink port regions the driver had are now unregistered. We then call its
602 * ds->ops->port_setup again, in order for the driver to re-create them on the
603 * new devlink port.
604 */
605static int dsa_port_reinit_as_unused(struct dsa_port *dp)
606{
607 struct dsa_switch *ds = dp->ds;
608 int err;
609
610 dsa_port_devlink_teardown(dp);
611 dp->type = DSA_PORT_TYPE_UNUSED;
612 err = dsa_port_devlink_setup(dp);
613 if (err)
614 return err;
615
616 if (ds->ops->port_setup) {
617 /* On error, leave the devlink port registered,
618 * dsa_switch_teardown will clean it up later.
619 */
620 err = ds->ops->port_setup(ds, dp->index);
621 if (err)
622 return err;
623 }
624
625 return 0;
626}
627
Andrew Lunn0f06b852020-09-18 21:11:08 +0200628static int dsa_devlink_info_get(struct devlink *dl,
629 struct devlink_info_req *req,
630 struct netlink_ext_ack *extack)
631{
632 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
633
634 if (ds->ops->devlink_info_get)
635 return ds->ops->devlink_info_get(ds, req, extack);
636
637 return -EOPNOTSUPP;
638}
639
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200640static int dsa_devlink_sb_pool_get(struct devlink *dl,
641 unsigned int sb_index, u16 pool_index,
642 struct devlink_sb_pool_info *pool_info)
643{
644 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
645
646 if (!ds->ops->devlink_sb_pool_get)
647 return -EOPNOTSUPP;
648
649 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
650 pool_info);
651}
652
653static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
654 u16 pool_index, u32 size,
655 enum devlink_sb_threshold_type threshold_type,
656 struct netlink_ext_ack *extack)
657{
658 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
659
660 if (!ds->ops->devlink_sb_pool_set)
661 return -EOPNOTSUPP;
662
663 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
664 threshold_type, extack);
665}
666
667static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
668 unsigned int sb_index, u16 pool_index,
669 u32 *p_threshold)
670{
671 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
672 int port = dsa_devlink_port_to_port(dlp);
673
674 if (!ds->ops->devlink_sb_port_pool_get)
675 return -EOPNOTSUPP;
676
677 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
678 pool_index, p_threshold);
679}
680
681static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
682 unsigned int sb_index, u16 pool_index,
683 u32 threshold,
684 struct netlink_ext_ack *extack)
685{
686 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
687 int port = dsa_devlink_port_to_port(dlp);
688
689 if (!ds->ops->devlink_sb_port_pool_set)
690 return -EOPNOTSUPP;
691
692 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
693 pool_index, threshold, extack);
694}
695
696static int
697dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
698 unsigned int sb_index, u16 tc_index,
699 enum devlink_sb_pool_type pool_type,
700 u16 *p_pool_index, u32 *p_threshold)
701{
702 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
703 int port = dsa_devlink_port_to_port(dlp);
704
705 if (!ds->ops->devlink_sb_tc_pool_bind_get)
706 return -EOPNOTSUPP;
707
708 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
709 tc_index, pool_type,
710 p_pool_index, p_threshold);
711}
712
713static int
714dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
715 unsigned int sb_index, u16 tc_index,
716 enum devlink_sb_pool_type pool_type,
717 u16 pool_index, u32 threshold,
718 struct netlink_ext_ack *extack)
719{
720 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
721 int port = dsa_devlink_port_to_port(dlp);
722
723 if (!ds->ops->devlink_sb_tc_pool_bind_set)
724 return -EOPNOTSUPP;
725
726 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
727 tc_index, pool_type,
728 pool_index, threshold,
729 extack);
730}
731
732static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
733 unsigned int sb_index)
734{
735 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
736
737 if (!ds->ops->devlink_sb_occ_snapshot)
738 return -EOPNOTSUPP;
739
740 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
741}
742
743static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
744 unsigned int sb_index)
745{
746 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
747
748 if (!ds->ops->devlink_sb_occ_max_clear)
749 return -EOPNOTSUPP;
750
751 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
752}
753
754static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
755 unsigned int sb_index,
756 u16 pool_index, u32 *p_cur,
757 u32 *p_max)
758{
759 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
760 int port = dsa_devlink_port_to_port(dlp);
761
762 if (!ds->ops->devlink_sb_occ_port_pool_get)
763 return -EOPNOTSUPP;
764
765 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
766 pool_index, p_cur, p_max);
767}
768
769static int
770dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
771 unsigned int sb_index, u16 tc_index,
772 enum devlink_sb_pool_type pool_type,
773 u32 *p_cur, u32 *p_max)
774{
775 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
776 int port = dsa_devlink_port_to_port(dlp);
777
778 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
779 return -EOPNOTSUPP;
780
781 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
782 sb_index, tc_index,
783 pool_type, p_cur,
784 p_max);
785}
786
Andrew Lunn0f06b852020-09-18 21:11:08 +0200787static const struct devlink_ops dsa_devlink_ops = {
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200788 .info_get = dsa_devlink_info_get,
789 .sb_pool_get = dsa_devlink_sb_pool_get,
790 .sb_pool_set = dsa_devlink_sb_pool_set,
791 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
792 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
793 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
794 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
795 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
796 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
797 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
798 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
Andrew Lunn0f06b852020-09-18 21:11:08 +0200799};
800
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200801static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
802{
803 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
804 struct dsa_switch_tree *dst = ds->dst;
805 int port, err;
806
807 if (tag_ops->proto == dst->default_proto)
808 return 0;
809
810 for (port = 0; port < ds->num_ports; port++) {
811 if (!dsa_is_cpu_port(ds, port))
812 continue;
813
814 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
815 if (err) {
816 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
817 tag_ops->name, ERR_PTR(err));
818 return err;
819 }
820 }
821
822 return 0;
823}
824
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500825static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200826{
Andrew Lunn6b297522019-10-25 01:03:51 +0200827 struct dsa_devlink_priv *dl_priv;
Andrew Lunn31224332020-10-04 18:12:53 +0200828 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400829 int err;
830
831 if (ds->setup)
832 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200833
Florian Fainelli6e830d82016-06-07 16:32:39 -0700834 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400835 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700836 * the slave MDIO bus driver rely on these values for probing PHY
837 * devices or not
838 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400839 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700840
Andrew Lunn96567d52017-03-28 23:45:07 +0200841 /* Add the switch to devlink before calling setup, so that setup can
842 * add dpipe tables
843 */
Leon Romanovsky919d13a2021-08-08 21:57:43 +0300844 ds->devlink =
845 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
Andrew Lunn96567d52017-03-28 23:45:07 +0200846 if (!ds->devlink)
847 return -ENOMEM;
Andrew Lunn6b297522019-10-25 01:03:51 +0200848 dl_priv = devlink_priv(ds->devlink);
849 dl_priv->ds = ds;
Andrew Lunn96567d52017-03-28 23:45:07 +0200850
Leon Romanovsky919d13a2021-08-08 21:57:43 +0300851 err = devlink_register(ds->devlink);
Andrew Lunn96567d52017-03-28 23:45:07 +0200852 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300853 goto free_devlink;
Andrew Lunn96567d52017-03-28 23:45:07 +0200854
Andrew Lunn31224332020-10-04 18:12:53 +0200855 /* Setup devlink port instances now, so that the switch
856 * setup() can register regions etc, against the ports
857 */
858 list_for_each_entry(dp, &ds->dst->ports, list) {
859 if (dp->ds == ds) {
860 err = dsa_port_devlink_setup(dp);
861 if (err)
862 goto unregister_devlink_ports;
863 }
864 }
865
Vivien Didelotf515f192017-02-03 13:20:20 -0500866 err = dsa_switch_register_notifier(ds);
867 if (err)
Andrew Lunn31224332020-10-04 18:12:53 +0200868 goto unregister_devlink_ports;
Vivien Didelotf515f192017-02-03 13:20:20 -0500869
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200870 ds->configure_vlan_while_not_filtering = true;
871
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300872 err = ds->ops->setup(ds);
873 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300874 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300875
Tobias Waldekranzdeff7102021-04-20 20:53:10 +0200876 err = dsa_switch_setup_tag_protocol(ds);
877 if (err)
878 goto teardown;
879
Andrew Lunn6b297522019-10-25 01:03:51 +0200880 devlink_params_publish(ds->devlink);
881
Vivien Didelot9d490b42016-08-23 12:38:56 -0400882 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700883 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300884 if (!ds->slave_mii_bus) {
885 err = -ENOMEM;
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200886 goto teardown;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300887 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700888
889 dsa_slave_mii_bus_init(ds);
890
891 err = mdiobus_register(ds->slave_mii_bus);
892 if (err < 0)
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200893 goto teardown;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700894 }
895
Vivien Didelotfb35c602019-10-21 16:51:19 -0400896 ds->setup = true;
897
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200898 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300899
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200900teardown:
901 if (ds->ops->teardown)
902 ds->ops->teardown(ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300903unregister_notifier:
904 dsa_switch_unregister_notifier(ds);
Andrew Lunn31224332020-10-04 18:12:53 +0200905unregister_devlink_ports:
906 list_for_each_entry(dp, &ds->dst->ports, list)
907 if (dp->ds == ds)
908 dsa_port_devlink_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300909 devlink_unregister(ds->devlink);
910free_devlink:
911 devlink_free(ds->devlink);
912 ds->devlink = NULL;
913
914 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200915}
916
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500917static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200918{
Andrew Lunn31224332020-10-04 18:12:53 +0200919 struct dsa_port *dp;
920
Vivien Didelotfb35c602019-10-21 16:51:19 -0400921 if (!ds->setup)
922 return;
923
Vivien Didelot9d490b42016-08-23 12:38:56 -0400924 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700925 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500926
927 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200928
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300929 if (ds->ops->teardown)
930 ds->ops->teardown(ds);
931
Andrew Lunn96567d52017-03-28 23:45:07 +0200932 if (ds->devlink) {
Andrew Lunn31224332020-10-04 18:12:53 +0200933 list_for_each_entry(dp, &ds->dst->ports, list)
934 if (dp->ds == ds)
935 dsa_port_devlink_teardown(dp);
Andrew Lunn96567d52017-03-28 23:45:07 +0200936 devlink_unregister(ds->devlink);
937 devlink_free(ds->devlink);
938 ds->devlink = NULL;
939 }
940
Vivien Didelotfb35c602019-10-21 16:51:19 -0400941 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200942}
943
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300944/* First tear down the non-shared, then the shared ports. This ensures that
945 * all work items scheduled by our switchdev handlers for user ports have
946 * completed before we destroy the refcounting kept on the shared ports.
947 */
948static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
949{
950 struct dsa_port *dp;
951
952 list_for_each_entry(dp, &dst->ports, list)
953 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
954 dsa_port_teardown(dp);
955
956 dsa_flush_workqueue();
957
958 list_for_each_entry(dp, &dst->ports, list)
959 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
960 dsa_port_teardown(dp);
961}
962
963static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
964{
965 struct dsa_port *dp;
966
967 list_for_each_entry(dp, &dst->ports, list)
968 dsa_switch_teardown(dp->ds);
969}
970
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500971static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
972{
Vivien Didelot1d277322017-11-06 16:11:48 -0500973 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400974 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500975
Vivien Didelotfb35c602019-10-21 16:51:19 -0400976 list_for_each_entry(dp, &dst->ports, list) {
977 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500978 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400979 goto teardown;
980 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500981
Vivien Didelotfb35c602019-10-21 16:51:19 -0400982 list_for_each_entry(dp, &dst->ports, list) {
983 err = dsa_port_setup(dp);
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300984 if (err) {
Vladimir Olteanfd292c12021-09-17 17:29:16 +0300985 err = dsa_port_reinit_as_unused(dp);
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300986 if (err)
987 goto teardown;
Maxim Kochetkovfb6ec872021-03-29 18:30:16 +0300988 }
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500989 }
990
991 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300992
Vivien Didelotfb35c602019-10-21 16:51:19 -0400993teardown:
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300994 dsa_tree_teardown_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300995
Vladimir Olteana57d8c212021-09-14 16:47:26 +0300996 dsa_tree_teardown_switches(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300997
998 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500999}
1000
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001001static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1002{
Vivien Didelot0cfec582019-10-21 16:51:22 -04001003 struct dsa_port *dp;
1004 int err;
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001005
Vivien Didelot0cfec582019-10-21 16:51:22 -04001006 list_for_each_entry(dp, &dst->ports, list) {
1007 if (dsa_port_is_cpu(dp)) {
1008 err = dsa_master_setup(dp->master, dp);
1009 if (err)
1010 return err;
1011 }
1012 }
1013
1014 return 0;
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001015}
1016
1017static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1018{
Vivien Didelot0cfec582019-10-21 16:51:22 -04001019 struct dsa_port *dp;
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001020
Vivien Didelot0cfec582019-10-21 16:51:22 -04001021 list_for_each_entry(dp, &dst->ports, list)
1022 if (dsa_port_is_cpu(dp))
1023 dsa_master_teardown(dp->master);
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001024}
1025
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001026static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1027{
1028 unsigned int len = 0;
1029 struct dsa_port *dp;
1030
1031 list_for_each_entry(dp, &dst->ports, list) {
1032 if (dp->ds->num_lag_ids > len)
1033 len = dp->ds->num_lag_ids;
1034 }
1035
1036 if (!len)
1037 return 0;
1038
1039 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1040 if (!dst->lags)
1041 return -ENOMEM;
1042
1043 dst->lags_len = len;
1044 return 0;
1045}
1046
1047static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1048{
1049 kfree(dst->lags);
1050}
1051
Vivien Didelotec15dd42017-11-06 16:11:46 -05001052static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001053{
Vivien Didelot34c09a82017-11-06 16:11:51 -05001054 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001055 int err;
1056
Vivien Didelotec15dd42017-11-06 16:11:46 -05001057 if (dst->setup) {
1058 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1059 dst->index);
1060 return -EEXIST;
1061 }
1062
Vivien Didelot34c09a82017-11-06 16:11:51 -05001063 complete = dsa_tree_setup_routing_table(dst);
1064 if (!complete)
1065 return 0;
1066
Vladimir Oltean2c0b0322021-08-04 16:54:30 +03001067 err = dsa_tree_setup_cpu_ports(dst);
Vivien Didelotf0704642017-11-06 16:11:44 -05001068 if (err)
1069 return err;
1070
Vivien Didelot1f08f9e2017-11-06 16:11:47 -05001071 err = dsa_tree_setup_switches(dst);
1072 if (err)
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001073 goto teardown_cpu_ports;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001074
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001075 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -04001076 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001077 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -04001078
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001079 err = dsa_tree_setup_lags(dst);
1080 if (err)
1081 goto teardown_master;
1082
Vivien Didelotec15dd42017-11-06 16:11:46 -05001083 dst->setup = true;
1084
1085 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001086
1087 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001088
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001089teardown_master:
1090 dsa_tree_teardown_master(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001091teardown_switches:
Vladimir Olteane5845aa2021-09-21 01:49:18 +03001092 dsa_tree_teardown_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001093 dsa_tree_teardown_switches(dst);
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001094teardown_cpu_ports:
1095 dsa_tree_teardown_cpu_ports(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +03001096
1097 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001098}
1099
Vivien Didelotec15dd42017-11-06 16:11:46 -05001100static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001101{
Vivien Didelotc5f51762019-10-30 22:09:13 -04001102 struct dsa_link *dl, *next;
1103
Vivien Didelotec15dd42017-11-06 16:11:46 -05001104 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001105 return;
1106
Tobias Waldekranz058102a2021-01-13 09:42:53 +01001107 dsa_tree_teardown_lags(dst);
1108
Vivien Didelot17a22fc2017-11-06 16:11:45 -05001109 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001110
Vladimir Olteana57d8c212021-09-14 16:47:26 +03001111 dsa_tree_teardown_ports(dst);
1112
Vivien Didelot1f08f9e2017-11-06 16:11:47 -05001113 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001114
Vladimir Oltean0e8eb9a2021-08-04 16:54:29 +03001115 dsa_tree_teardown_cpu_ports(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -07001116
Vivien Didelotc5f51762019-10-30 22:09:13 -04001117 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1118 list_del(&dl->list);
1119 kfree(dl);
1120 }
1121
Vivien Didelotec15dd42017-11-06 16:11:46 -05001122 pr_info("DSA: tree %d torn down\n", dst->index);
1123
1124 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001125}
1126
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001127/* Since the dsa/tagging sysfs device attribute is per master, the assumption
1128 * is that all DSA switches within a tree share the same tagger, otherwise
1129 * they would have formed disjoint trees (different "dsa,member" values).
1130 */
1131int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1132 struct net_device *master,
1133 const struct dsa_device_ops *tag_ops,
1134 const struct dsa_device_ops *old_tag_ops)
1135{
1136 struct dsa_notifier_tag_proto_info info;
1137 struct dsa_port *dp;
1138 int err = -EBUSY;
1139
1140 if (!rtnl_trylock())
1141 return restart_syscall();
1142
1143 /* At the moment we don't allow changing the tag protocol under
1144 * traffic. The rtnl_mutex also happens to serialize concurrent
1145 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1146 * restriction, there needs to be another mutex which serializes this.
1147 */
1148 if (master->flags & IFF_UP)
1149 goto out_unlock;
1150
1151 list_for_each_entry(dp, &dst->ports, list) {
1152 if (!dsa_is_user_port(dp->ds, dp->index))
1153 continue;
1154
1155 if (dp->slave->flags & IFF_UP)
1156 goto out_unlock;
1157 }
1158
1159 info.tag_ops = tag_ops;
1160 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1161 if (err)
1162 goto out_unwind_tagger;
1163
1164 dst->tag_ops = tag_ops;
1165
1166 rtnl_unlock();
1167
1168 return 0;
1169
1170out_unwind_tagger:
1171 info.tag_ops = old_tag_ops;
1172 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1173out_unlock:
1174 rtnl_unlock();
1175 return err;
1176}
1177
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001178static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1179{
1180 struct dsa_switch_tree *dst = ds->dst;
1181 struct dsa_port *dp;
1182
Vivien Didelot05f294a2019-10-21 16:51:29 -04001183 list_for_each_entry(dp, &dst->ports, list)
1184 if (dp->ds == ds && dp->index == index)
1185 return dp;
1186
1187 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1188 if (!dp)
1189 return NULL;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001190
1191 dp->ds = ds;
1192 dp->index = index;
Vladimir Oltean123abc062021-07-22 18:55:40 +03001193 dp->bridge_num = -1;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001194
1195 INIT_LIST_HEAD(&dp->list);
1196 list_add_tail(&dp->list, &dst->ports);
1197
1198 return dp;
1199}
1200
Vivien Didelot06e24d02017-11-03 19:05:29 -04001201static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1202{
1203 if (!name)
1204 name = "eth%d";
1205
1206 dp->type = DSA_PORT_TYPE_USER;
1207 dp->name = name;
1208
1209 return 0;
1210}
1211
1212static int dsa_port_parse_dsa(struct dsa_port *dp)
1213{
1214 dp->type = DSA_PORT_TYPE_DSA;
1215
1216 return 0;
1217}
1218
Florian Fainelli4d776482020-01-07 21:06:05 -08001219static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1220 struct net_device *master)
1221{
1222 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1223 struct dsa_switch *mds, *ds = dp->ds;
1224 unsigned int mdp_upstream;
1225 struct dsa_port *mdp;
1226
1227 /* It is possible to stack DSA switches onto one another when that
1228 * happens the switch driver may want to know if its tagging protocol
1229 * is going to work in such a configuration.
1230 */
1231 if (dsa_slave_dev_check(master)) {
1232 mdp = dsa_slave_to_port(master);
1233 mds = mdp->ds;
1234 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1235 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1236 DSA_TAG_PROTO_NONE);
1237 }
1238
1239 /* If the master device is not itself a DSA slave in a disjoint DSA
1240 * tree, then return immediately.
1241 */
1242 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1243}
1244
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001245static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1246 const char *user_protocol)
Vivien Didelot06e24d02017-11-03 19:05:29 -04001247{
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001248 struct dsa_switch *ds = dp->ds;
1249 struct dsa_switch_tree *dst = ds->dst;
George McCollistere0c755a2021-03-22 15:26:50 -05001250 const struct dsa_device_ops *tag_ops;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001251 enum dsa_tag_protocol default_proto;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001252
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001253 /* Find out which protocol the switch would prefer. */
1254 default_proto = dsa_get_tag_protocol(dp, master);
1255 if (dst->default_proto) {
1256 if (dst->default_proto != default_proto) {
Vladimir Oltean357f2032021-01-29 03:00:05 +02001257 dev_err(ds->dev,
1258 "A DSA switch tree can have only one tagging protocol\n");
1259 return -EINVAL;
1260 }
Vladimir Oltean357f2032021-01-29 03:00:05 +02001261 } else {
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001262 dst->default_proto = default_proto;
1263 }
1264
1265 /* See if the user wants to override that preference. */
1266 if (user_protocol) {
1267 if (!ds->ops->change_tag_protocol) {
1268 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1269 return -EINVAL;
Vladimir Oltean357f2032021-01-29 03:00:05 +02001270 }
George McCollistere0c755a2021-03-22 15:26:50 -05001271
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001272 tag_ops = dsa_find_tagger_by_name(user_protocol);
1273 } else {
1274 tag_ops = dsa_tag_driver_get(default_proto);
1275 }
1276
1277 if (IS_ERR(tag_ops)) {
1278 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1279 return -EPROBE_DEFER;
1280
1281 dev_warn(ds->dev, "No tagger for this switch\n");
1282 return PTR_ERR(tag_ops);
1283 }
1284
1285 if (dst->tag_ops) {
1286 if (dst->tag_ops != tag_ops) {
1287 dev_err(ds->dev,
1288 "A DSA switch tree can have only one tagging protocol\n");
1289
1290 dsa_tag_driver_put(tag_ops);
1291 return -EINVAL;
1292 }
1293
1294 /* In the case of multiple CPU ports per switch, the tagging
1295 * protocol is still reference-counted only per switch tree.
1296 */
1297 dsa_tag_driver_put(tag_ops);
1298 } else {
George McCollistere0c755a2021-03-22 15:26:50 -05001299 dst->tag_ops = tag_ops;
Florian Fainelli9f9e7722017-07-24 10:49:23 -07001300 }
1301
Florian Fainelli4d776482020-01-07 21:06:05 -08001302 dp->master = master;
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001303 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001304 dsa_port_set_tag_protocol(dp, dst->tag_ops);
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001305 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -04001306
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001307 /* At this point, the tree may be configured to use a different
1308 * tagger than the one chosen by the switch driver during
1309 * .setup, in the case when a user selects a custom protocol
1310 * through the DT.
1311 *
1312 * This is resolved by syncing the driver with the tree in
1313 * dsa_switch_setup_tag_protocol once .setup has run and the
1314 * driver is ready to accept calls to .change_tag_protocol. If
1315 * the driver does not support the custom protocol at that
1316 * point, the tree is wholly rejected, thereby ensuring that the
1317 * tree and driver are always in agreement on the protocol to
1318 * use.
1319 */
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001320 return 0;
1321}
1322
Vivien Didelotfd223e22017-10-27 15:55:14 -04001323static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1324{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001325 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -04001326 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -04001327 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001328
Vivien Didelot06e24d02017-11-03 19:05:29 -04001329 dp->dn = dn;
1330
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001331 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001332 struct net_device *master;
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001333 const char *user_protocol;
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001334
1335 master = of_find_net_device_by_node(ethernet);
1336 if (!master)
1337 return -EPROBE_DEFER;
1338
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001339 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1340 return dsa_port_parse_cpu(dp, master, user_protocol);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001341 }
1342
Vivien Didelot06e24d02017-11-03 19:05:29 -04001343 if (link)
1344 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001345
Vivien Didelot06e24d02017-11-03 19:05:29 -04001346 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001347}
1348
Vivien Didelot975e6e32017-11-03 19:05:27 -04001349static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1350 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001351{
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001352 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001353 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +08001354 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001355 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001356
1357 ports = of_get_child_by_name(dn, "ports");
1358 if (!ports) {
Kurt Kanzenbach85e05d22020-07-20 14:49:39 +02001359 /* The second possibility is "ethernet-ports" */
1360 ports = of_get_child_by_name(dn, "ethernet-ports");
1361 if (!ports) {
1362 dev_err(ds->dev, "no ports child node found\n");
1363 return -EINVAL;
1364 }
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001365 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001366
1367 for_each_available_child_of_node(ports, port) {
1368 err = of_property_read_u32(port, "reg", &reg);
1369 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001370 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001371
Wen Yang9919a362019-02-25 15:22:19 +08001372 if (reg >= ds->num_ports) {
Rafał Miłecki8209f5b2021-01-06 10:09:15 +01001373 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1374 port, reg, ds->num_ports);
Wen Yang9919a362019-02-25 15:22:19 +08001375 err = -EINVAL;
1376 goto out_put_node;
1377 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001378
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001379 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001380
1381 err = dsa_port_parse_of(dp, port);
1382 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001383 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001384 }
1385
Wen Yang9919a362019-02-25 15:22:19 +08001386out_put_node:
1387 of_node_put(ports);
1388 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001389}
1390
Vivien Didelot975e6e32017-11-03 19:05:27 -04001391static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1392 struct device_node *dn)
1393{
1394 u32 m[2] = { 0, 0 };
1395 int sz;
1396
1397 /* Don't error out if this optional property isn't found */
1398 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1399 if (sz < 0 && sz != -EINVAL)
1400 return sz;
1401
1402 ds->index = m[1];
Vivien Didelot975e6e32017-11-03 19:05:27 -04001403
1404 ds->dst = dsa_tree_touch(m[0]);
1405 if (!ds->dst)
1406 return -ENOMEM;
1407
Vladimir Oltean8674f8d2021-06-21 19:42:14 +03001408 if (dsa_switch_find(ds->dst->index, ds->index)) {
1409 dev_err(ds->dev,
1410 "A DSA switch with index %d already exists in tree %d\n",
1411 ds->index, ds->dst->index);
1412 return -EEXIST;
1413 }
1414
Vladimir Oltean5b22d362021-07-22 18:55:39 +03001415 if (ds->dst->last_switch < ds->index)
1416 ds->dst->last_switch = ds->index;
1417
Vivien Didelot975e6e32017-11-03 19:05:27 -04001418 return 0;
1419}
1420
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001421static int dsa_switch_touch_ports(struct dsa_switch *ds)
1422{
1423 struct dsa_port *dp;
1424 int port;
1425
1426 for (port = 0; port < ds->num_ports; port++) {
1427 dp = dsa_port_touch(ds, port);
1428 if (!dp)
1429 return -ENOMEM;
1430 }
1431
1432 return 0;
1433}
1434
Vivien Didelot975e6e32017-11-03 19:05:27 -04001435static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1436{
1437 int err;
1438
1439 err = dsa_switch_parse_member_of(ds, dn);
1440 if (err)
1441 return err;
1442
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001443 err = dsa_switch_touch_ports(ds);
1444 if (err)
1445 return err;
1446
Vivien Didelot975e6e32017-11-03 19:05:27 -04001447 return dsa_switch_parse_ports_of(ds, dn);
1448}
1449
Vivien Didelotfd223e22017-10-27 15:55:14 -04001450static int dsa_port_parse(struct dsa_port *dp, const char *name,
1451 struct device *dev)
1452{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001453 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001454 struct net_device *master;
1455
1456 master = dsa_dev_to_net_device(dev);
1457 if (!master)
1458 return -EPROBE_DEFER;
1459
1460 dev_put(master);
1461
Tobias Waldekranzdeff7102021-04-20 20:53:10 +02001462 return dsa_port_parse_cpu(dp, master, NULL);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001463 }
1464
Vivien Didelot06e24d02017-11-03 19:05:29 -04001465 if (!strcmp(name, "dsa"))
1466 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001467
Vivien Didelot06e24d02017-11-03 19:05:29 -04001468 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001469}
1470
Vivien Didelot975e6e32017-11-03 19:05:27 -04001471static int dsa_switch_parse_ports(struct dsa_switch *ds,
1472 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001473{
1474 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001475 struct dsa_port *dp;
1476 struct device *dev;
1477 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001478 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001479 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001480
1481 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -04001482 name = cd->port_names[i];
1483 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001484 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001485
1486 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001487 continue;
1488
Vivien Didelotfd223e22017-10-27 15:55:14 -04001489 err = dsa_port_parse(dp, name, dev);
1490 if (err)
1491 return err;
1492
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001493 valid_name_found = true;
1494 }
1495
1496 if (!valid_name_found && i == DSA_MAX_PORTS)
1497 return -EINVAL;
1498
1499 return 0;
1500}
1501
Vivien Didelot975e6e32017-11-03 19:05:27 -04001502static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001503{
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001504 int err;
1505
Vivien Didelot975e6e32017-11-03 19:05:27 -04001506 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001507
Vivien Didelot975e6e32017-11-03 19:05:27 -04001508 /* We don't support interconnected switches nor multiple trees via
1509 * platform data, so this is the unique switch of the tree.
1510 */
1511 ds->index = 0;
1512 ds->dst = dsa_tree_touch(0);
1513 if (!ds->dst)
1514 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001515
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001516 err = dsa_switch_touch_ports(ds);
1517 if (err)
1518 return err;
1519
Vivien Didelot975e6e32017-11-03 19:05:27 -04001520 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001521}
1522
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001523static void dsa_switch_release_ports(struct dsa_switch *ds)
1524{
1525 struct dsa_switch_tree *dst = ds->dst;
1526 struct dsa_port *dp, *next;
1527
1528 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1529 if (dp->ds != ds)
1530 continue;
1531 list_del(&dp->list);
1532 kfree(dp);
1533 }
1534}
1535
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001536static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001537{
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001538 struct dsa_switch_tree *dst;
Colin Ian King556f1242019-10-24 11:32:18 +01001539 struct dsa_chip_data *pdata;
1540 struct device_node *np;
Vivien Didelot34c09a82017-11-06 16:11:51 -05001541 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001542
Vivien Didelot7e99e342019-10-21 16:51:30 -04001543 if (!ds->dev)
1544 return -ENODEV;
1545
Colin Ian King556f1242019-10-24 11:32:18 +01001546 pdata = ds->dev->platform_data;
1547 np = ds->dev->of_node;
1548
Vivien Didelot7e99e342019-10-21 16:51:30 -04001549 if (!ds->num_ports)
1550 return -EINVAL;
1551
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001552 if (np) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001553 err = dsa_switch_parse_of(ds, np);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001554 if (err)
1555 dsa_switch_release_ports(ds);
1556 } else if (pdata) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001557 err = dsa_switch_parse(ds, pdata);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001558 if (err)
1559 dsa_switch_release_ports(ds);
1560 } else {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001561 err = -ENODEV;
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001562 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001563
Vivien Didelot975e6e32017-11-03 19:05:27 -04001564 if (err)
1565 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001566
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001567 dst = ds->dst;
1568 dsa_tree_get(dst);
1569 err = dsa_tree_setup(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001570 if (err) {
1571 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001572 dsa_tree_put(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001573 }
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001574
1575 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001576}
1577
Vivien Didelot23c9ee42017-05-26 18:12:51 -04001578int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001579{
1580 int err;
1581
1582 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001583 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -05001584 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001585 mutex_unlock(&dsa2_mutex);
1586
1587 return err;
1588}
1589EXPORT_SYMBOL_GPL(dsa_register_switch);
1590
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001591static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001592{
1593 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot05f294a2019-10-21 16:51:29 -04001594
Florian Fainellic058f6d2019-11-02 20:13:26 -07001595 dsa_tree_teardown(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001596 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001597 dsa_tree_put(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001598}
1599
1600void dsa_unregister_switch(struct dsa_switch *ds)
1601{
1602 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001603 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001604 mutex_unlock(&dsa2_mutex);
1605}
1606EXPORT_SYMBOL_GPL(dsa_unregister_switch);
Vladimir Oltean0650bf52021-09-17 16:34:33 +03001607
1608/* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1609 * blocking that operation from completion, due to the dev_hold taken inside
1610 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1611 * the DSA master, so that the system can reboot successfully.
1612 */
1613void dsa_switch_shutdown(struct dsa_switch *ds)
1614{
1615 struct net_device *master, *slave_dev;
1616 LIST_HEAD(unregister_list);
1617 struct dsa_port *dp;
1618
1619 mutex_lock(&dsa2_mutex);
1620 rtnl_lock();
1621
1622 list_for_each_entry(dp, &ds->dst->ports, list) {
1623 if (dp->ds != ds)
1624 continue;
1625
1626 if (!dsa_port_is_user(dp))
1627 continue;
1628
1629 master = dp->cpu_dp->master;
1630 slave_dev = dp->slave;
1631
1632 netdev_upper_dev_unlink(master, slave_dev);
1633 /* Just unlinking ourselves as uppers of the master is not
1634 * sufficient. When the master net device unregisters, that will
1635 * also call dev_close, which we will catch as NETDEV_GOING_DOWN
1636 * and trigger a dev_close on our own devices (dsa_slave_close).
1637 * In turn, that will call dev_mc_unsync on the master's net
1638 * device. If the master is also a DSA switch port, this will
1639 * trigger dsa_slave_set_rx_mode which will call dev_mc_sync on
1640 * its own master. Lockdep will complain about the fact that
1641 * all cascaded masters have the same dsa_master_addr_list_lock_key,
1642 * which it normally would not do if the cascaded masters would
1643 * be in a proper upper/lower relationship, which we've just
1644 * destroyed.
1645 * To suppress the lockdep warnings, let's actually unregister
1646 * the DSA slave interfaces too, to avoid the nonsensical
1647 * multicast address list synchronization on shutdown.
1648 */
1649 unregister_netdevice_queue(slave_dev, &unregister_list);
1650 }
1651 unregister_netdevice_many(&unregister_list);
1652
1653 rtnl_unlock();
1654 mutex_unlock(&dsa2_mutex);
1655}
1656EXPORT_SYMBOL_GPL(dsa_switch_shutdown);