blob: d142eb2b288b3d9a28934b5a10f43ae5c363d961 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Lunn83c0afa2016-06-04 21:17:07 +02002/*
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
Andrew Lunn83c0afa2016-06-04 21:17:07 +02007 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/list.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020012#include <linux/netdevice.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020013#include <linux/slab.h>
14#include <linux/rtnetlink.h>
Andrew Lunn83c0afa2016-06-04 21:17:07 +020015#include <linux/of.h>
16#include <linux/of_net.h>
Jiri Pirko402f99e52019-03-24 11:14:26 +010017#include <net/devlink.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040018
Andrew Lunn83c0afa2016-06-04 21:17:07 +020019#include "dsa_priv.h"
20
Andrew Lunn83c0afa2016-06-04 21:17:07 +020021static DEFINE_MUTEX(dsa2_mutex);
Vladimir Olteanbff33f72020-03-27 21:55:43 +020022LIST_HEAD(dsa_tree_list);
Andrew Lunn83c0afa2016-06-04 21:17:07 +020023
Tobias Waldekranz058102a2021-01-13 09:42:53 +010024/**
Vladimir Oltean886f8e22021-01-29 03:00:04 +020025 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
26 * @dst: collection of struct dsa_switch devices to notify.
27 * @e: event, must be of type DSA_NOTIFIER_*
28 * @v: event-specific value.
29 *
30 * Given a struct dsa_switch_tree, this can be used to run a function once for
31 * each member DSA switch. The other alternative of traversing the tree is only
32 * through its ports list, which does not uniquely list the switches.
33 */
34int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
35{
36 struct raw_notifier_head *nh = &dst->nh;
37 int err;
38
39 err = raw_notifier_call_chain(nh, e, v);
40
41 return notifier_to_errno(err);
42}
43
44/**
45 * dsa_broadcast - Notify all DSA trees in the system.
46 * @e: event, must be of type DSA_NOTIFIER_*
47 * @v: event-specific value.
48 *
49 * Can be used to notify the switching fabric of events such as cross-chip
50 * bridging between disjoint trees (such as islands of tagger-compatible
51 * switches bridged by an incompatible middle switch).
52 */
53int dsa_broadcast(unsigned long e, void *v)
54{
55 struct dsa_switch_tree *dst;
56 int err = 0;
57
58 list_for_each_entry(dst, &dsa_tree_list, list) {
59 err = dsa_tree_notify(dst, e, v);
60 if (err)
61 break;
62 }
63
64 return err;
65}
66
67/**
Tobias Waldekranz058102a2021-01-13 09:42:53 +010068 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
69 * @dst: Tree in which to record the mapping.
70 * @lag: Netdev that is to be mapped to an ID.
71 *
72 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
73 * two spaces. The size of the mapping space is determined by the
74 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
75 * it unset if it is not needed, in which case these functions become
76 * no-ops.
77 */
78void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
79{
80 unsigned int id;
81
82 if (dsa_lag_id(dst, lag) >= 0)
83 /* Already mapped */
84 return;
85
86 for (id = 0; id < dst->lags_len; id++) {
87 if (!dsa_lag_dev(dst, id)) {
88 dst->lags[id] = lag;
89 return;
90 }
91 }
92
93 /* No IDs left, which is OK. Some drivers do not need it. The
94 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
95 * returns an error for this device when joining the LAG. The
96 * driver can then return -EOPNOTSUPP back to DSA, which will
97 * fall back to a software LAG.
98 */
99}
100
101/**
102 * dsa_lag_unmap() - Remove a LAG ID mapping
103 * @dst: Tree in which the mapping is recorded.
104 * @lag: Netdev that was mapped.
105 *
106 * As there may be multiple users of the mapping, it is only removed
107 * if there are no other references to it.
108 */
109void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
110{
111 struct dsa_port *dp;
112 unsigned int id;
113
114 dsa_lag_foreach_port(dp, dst, lag)
115 /* There are remaining users of this mapping */
116 return;
117
118 dsa_lags_foreach_id(id, dst) {
119 if (dsa_lag_dev(dst, id) == lag) {
120 dst->lags[id] = NULL;
121 break;
122 }
123 }
124}
125
Vladimir Oltean3b7bc1f2020-05-10 19:37:42 +0300126struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
127{
128 struct dsa_switch_tree *dst;
129 struct dsa_port *dp;
130
131 list_for_each_entry(dst, &dsa_tree_list, list) {
132 if (dst->index != tree_index)
133 continue;
134
135 list_for_each_entry(dp, &dst->ports, list) {
136 if (dp->ds->index != sw_index)
137 continue;
138
139 return dp->ds;
140 }
141 }
142
143 return NULL;
144}
145EXPORT_SYMBOL_GPL(dsa_switch_find);
146
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400147static struct dsa_switch_tree *dsa_tree_find(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200148{
149 struct dsa_switch_tree *dst;
150
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400151 list_for_each_entry(dst, &dsa_tree_list, list)
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400152 if (dst->index == index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200153 return dst;
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400154
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200155 return NULL;
156}
157
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400158static struct dsa_switch_tree *dsa_tree_alloc(int index)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200159{
160 struct dsa_switch_tree *dst;
161
162 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
163 if (!dst)
164 return NULL;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400165
Vivien Didelot49463b72017-11-03 19:05:21 -0400166 dst->index = index;
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400167
Vivien Didelotc5f51762019-10-30 22:09:13 -0400168 INIT_LIST_HEAD(&dst->rtable);
169
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400170 INIT_LIST_HEAD(&dst->ports);
171
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200172 INIT_LIST_HEAD(&dst->list);
Vivien Didelot50c7d2ba2019-10-18 17:02:46 -0400173 list_add_tail(&dst->list, &dsa_tree_list);
Vivien Didelot8e5bf972017-11-03 19:05:22 -0400174
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200175 kref_init(&dst->refcount);
176
177 return dst;
178}
179
Vivien Didelot65254102017-11-03 19:05:23 -0400180static void dsa_tree_free(struct dsa_switch_tree *dst)
181{
Vladimir Oltean357f2032021-01-29 03:00:05 +0200182 if (dst->tag_ops)
183 dsa_tag_driver_put(dst->tag_ops);
Vivien Didelot65254102017-11-03 19:05:23 -0400184 list_del(&dst->list);
185 kfree(dst);
186}
187
Vivien Didelot9e741042017-11-24 11:36:06 -0500188static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
189{
190 if (dst)
191 kref_get(&dst->refcount);
192
193 return dst;
194}
195
Vivien Didelot1ca28ec2017-11-03 19:05:24 -0400196static struct dsa_switch_tree *dsa_tree_touch(int index)
197{
198 struct dsa_switch_tree *dst;
199
200 dst = dsa_tree_find(index);
Vivien Didelot9e741042017-11-24 11:36:06 -0500201 if (dst)
202 return dsa_tree_get(dst);
203 else
204 return dsa_tree_alloc(index);
Vivien Didelot65254102017-11-03 19:05:23 -0400205}
206
207static void dsa_tree_release(struct kref *ref)
208{
209 struct dsa_switch_tree *dst;
210
211 dst = container_of(ref, struct dsa_switch_tree, refcount);
212
213 dsa_tree_free(dst);
214}
215
216static void dsa_tree_put(struct dsa_switch_tree *dst)
217{
Vivien Didelot9e741042017-11-24 11:36:06 -0500218 if (dst)
219 kref_put(&dst->refcount, dsa_tree_release);
Vivien Didelot65254102017-11-03 19:05:23 -0400220}
221
Florian Fainelli293784a2017-01-26 10:45:52 -0800222static bool dsa_port_is_dsa(struct dsa_port *port)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200223{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400224 return port->type == DSA_PORT_TYPE_DSA;
Florian Fainelli293784a2017-01-26 10:45:52 -0800225}
226
227static bool dsa_port_is_cpu(struct dsa_port *port)
228{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -0400229 return port->type == DSA_PORT_TYPE_CPU;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200230}
231
Vivien Didelotf0704642017-11-06 16:11:44 -0500232static bool dsa_port_is_user(struct dsa_port *dp)
233{
234 return dp->type == DSA_PORT_TYPE_USER;
235}
236
Vivien Didelotf163da82017-11-06 16:11:49 -0500237static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
238 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200239{
Vivien Didelotf163da82017-11-06 16:11:49 -0500240 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200241
Vivien Didelot764b7e62019-10-21 16:51:21 -0400242 list_for_each_entry(dp, &dst->ports, list)
243 if (dp->dn == dn)
244 return dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200245
246 return NULL;
247}
248
Ben Dooks (Codethink)4e2ce6e2019-12-17 11:20:38 +0000249static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
250 struct dsa_port *link_dp)
Vivien Didelotc5f51762019-10-30 22:09:13 -0400251{
252 struct dsa_switch *ds = dp->ds;
253 struct dsa_switch_tree *dst;
254 struct dsa_link *dl;
255
256 dst = ds->dst;
257
258 list_for_each_entry(dl, &dst->rtable, list)
259 if (dl->dp == dp && dl->link_dp == link_dp)
260 return dl;
261
262 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
263 if (!dl)
264 return NULL;
265
266 dl->dp = dp;
267 dl->link_dp = link_dp;
268
269 INIT_LIST_HEAD(&dl->list);
270 list_add_tail(&dl->list, &dst->rtable);
271
272 return dl;
273}
274
Vivien Didelot34c09a82017-11-06 16:11:51 -0500275static bool dsa_port_setup_routing_table(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200276{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500277 struct dsa_switch *ds = dp->ds;
278 struct dsa_switch_tree *dst = ds->dst;
279 struct device_node *dn = dp->dn;
Vivien Didelotc5286662017-11-06 16:11:50 -0500280 struct of_phandle_iterator it;
Vivien Didelotf163da82017-11-06 16:11:49 -0500281 struct dsa_port *link_dp;
Vivien Didelotc5f51762019-10-30 22:09:13 -0400282 struct dsa_link *dl;
Vivien Didelotc5286662017-11-06 16:11:50 -0500283 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200284
Vivien Didelotc5286662017-11-06 16:11:50 -0500285 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
286 link_dp = dsa_tree_find_port_by_node(dst, it.node);
287 if (!link_dp) {
288 of_node_put(it.node);
Vivien Didelot34c09a82017-11-06 16:11:51 -0500289 return false;
Vivien Didelotc5286662017-11-06 16:11:50 -0500290 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200291
Vivien Didelotc5f51762019-10-30 22:09:13 -0400292 dl = dsa_link_touch(dp, link_dp);
293 if (!dl) {
294 of_node_put(it.node);
295 return false;
296 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200297 }
298
Vivien Didelot34c09a82017-11-06 16:11:51 -0500299 return true;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200300}
301
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400302static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200303{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500304 bool complete = true;
305 struct dsa_port *dp;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200306
Vivien Didelot86bfb2c2019-10-21 16:51:20 -0400307 list_for_each_entry(dp, &dst->ports, list) {
Vivien Didelot3774ecd2019-10-30 22:09:15 -0400308 if (dsa_port_is_dsa(dp)) {
Vivien Didelot34c09a82017-11-06 16:11:51 -0500309 complete = dsa_port_setup_routing_table(dp);
310 if (!complete)
311 break;
312 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200313 }
314
Vivien Didelot34c09a82017-11-06 16:11:51 -0500315 return complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200316}
317
Vivien Didelotf0704642017-11-06 16:11:44 -0500318static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
319{
Vivien Didelotf0704642017-11-06 16:11:44 -0500320 struct dsa_port *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500321
Vivien Didelotc0b73622019-10-21 16:51:23 -0400322 list_for_each_entry(dp, &dst->ports, list)
323 if (dsa_port_is_cpu(dp))
324 return dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500325
326 return NULL;
327}
328
329static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
330{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400331 struct dsa_port *cpu_dp, *dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500332
Vivien Didelotda4561c2019-10-21 16:51:24 -0400333 cpu_dp = dsa_tree_find_first_cpu(dst);
334 if (!cpu_dp) {
335 pr_err("DSA: tree %d has no CPU port\n", dst->index);
Vivien Didelotf0704642017-11-06 16:11:44 -0500336 return -EINVAL;
337 }
338
339 /* Assign the default CPU port to all ports of the fabric */
Vivien Didelotda4561c2019-10-21 16:51:24 -0400340 list_for_each_entry(dp, &dst->ports, list)
341 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
342 dp->cpu_dp = cpu_dp;
Vivien Didelotf0704642017-11-06 16:11:44 -0500343
344 return 0;
345}
346
347static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
348{
Vivien Didelotda4561c2019-10-21 16:51:24 -0400349 struct dsa_port *dp;
350
351 list_for_each_entry(dp, &dst->ports, list)
352 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
353 dp->cpu_dp = NULL;
Vivien Didelotf0704642017-11-06 16:11:44 -0500354}
355
Vivien Didelot1d277322017-11-06 16:11:48 -0500356static int dsa_port_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200357{
Vivien Didelot955222c2019-08-19 16:00:48 -0400358 struct devlink_port *dlp = &dp->devlink_port;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300359 bool dsa_port_link_registered = false;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300360 bool dsa_port_enabled = false;
361 int err = 0;
Andrew Lunn96567d52017-03-28 23:45:07 +0200362
Vivien Didelotfb35c602019-10-21 16:51:19 -0400363 if (dp->setup)
364 return 0;
365
Vivien Didelot1d277322017-11-06 16:11:48 -0500366 switch (dp->type) {
367 case DSA_PORT_TYPE_UNUSED:
Vivien Didelot0394a632019-08-19 16:00:50 -0400368 dsa_port_disable(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500369 break;
370 case DSA_PORT_TYPE_CPU:
Jiri Pirkoda077392018-05-18 09:29:03 +0200371 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300372 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300373 break;
374 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400375
376 err = dsa_port_enable(dp, NULL);
377 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300378 break;
379 dsa_port_enabled = true;
380
Jiri Pirkoda077392018-05-18 09:29:03 +0200381 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500382 case DSA_PORT_TYPE_DSA:
Sebastian Reichel33615362018-01-23 16:03:46 +0100383 err = dsa_port_link_register_of(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300384 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300385 break;
386 dsa_port_link_registered = true;
Vivien Didelot0394a632019-08-19 16:00:50 -0400387
388 err = dsa_port_enable(dp, NULL);
389 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300390 break;
391 dsa_port_enabled = true;
392
Vivien Didelot1d277322017-11-06 16:11:48 -0500393 break;
394 case DSA_PORT_TYPE_USER:
Vivien Didelot955222c2019-08-19 16:00:48 -0400395 dp->mac = of_get_mac_address(dp->dn);
Vivien Didelot1d277322017-11-06 16:11:48 -0500396 err = dsa_slave_create(dp);
397 if (err)
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300398 break;
Vivien Didelot955222c2019-08-19 16:00:48 -0400399
400 devlink_port_type_eth_set(dlp, dp->slave);
Vivien Didelot1d277322017-11-06 16:11:48 -0500401 break;
402 }
Andrew Lunn96567d52017-03-28 23:45:07 +0200403
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300404 if (err && dsa_port_enabled)
405 dsa_port_disable(dp);
406 if (err && dsa_port_link_registered)
407 dsa_port_link_unregister_of(dp);
Vivien Didelotfb35c602019-10-21 16:51:19 -0400408 if (err)
409 return err;
Vladimir Oltean4ba0ebb2019-08-31 15:46:19 +0300410
Vivien Didelotfb35c602019-10-21 16:51:19 -0400411 dp->setup = true;
412
413 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200414}
415
Andrew Lunn31224332020-10-04 18:12:53 +0200416static int dsa_port_devlink_setup(struct dsa_port *dp)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200417{
Vivien Didelot955222c2019-08-19 16:00:48 -0400418 struct devlink_port *dlp = &dp->devlink_port;
Andrew Lunn31224332020-10-04 18:12:53 +0200419 struct dsa_switch_tree *dst = dp->ds->dst;
420 struct devlink_port_attrs attrs = {};
421 struct devlink *dl = dp->ds->devlink;
422 const unsigned char *id;
423 unsigned char len;
424 int err;
Vivien Didelot1d277322017-11-06 16:11:48 -0500425
Andrew Lunn31224332020-10-04 18:12:53 +0200426 id = (const unsigned char *)&dst->index;
427 len = sizeof(dst->index);
428
429 attrs.phys.port_number = dp->index;
430 memcpy(attrs.switch_id.id, id, len);
431 attrs.switch_id.id_len = len;
432 memset(dlp, 0, sizeof(*dlp));
433
434 switch (dp->type) {
435 case DSA_PORT_TYPE_UNUSED:
436 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
437 break;
438 case DSA_PORT_TYPE_CPU:
439 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
440 break;
441 case DSA_PORT_TYPE_DSA:
442 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
443 break;
444 case DSA_PORT_TYPE_USER:
445 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
446 break;
447 }
448
449 devlink_port_attrs_set(dlp, &attrs);
450 err = devlink_port_register(dl, dlp, dp->index);
451
452 if (!err)
453 dp->devlink_port_setup = true;
454
455 return err;
456}
457
458static void dsa_port_teardown(struct dsa_port *dp)
459{
Vladimir Oltean91158e12021-01-12 02:48:31 +0200460 struct devlink_port *dlp = &dp->devlink_port;
461
Vivien Didelotfb35c602019-10-21 16:51:19 -0400462 if (!dp->setup)
463 return;
464
Vladimir Oltean91158e12021-01-12 02:48:31 +0200465 devlink_port_type_clear(dlp);
466
Vivien Didelot1d277322017-11-06 16:11:48 -0500467 switch (dp->type) {
468 case DSA_PORT_TYPE_UNUSED:
469 break;
470 case DSA_PORT_TYPE_CPU:
Vivien Didelot0394a632019-08-19 16:00:50 -0400471 dsa_port_disable(dp);
Vivien Didelot955222c2019-08-19 16:00:48 -0400472 dsa_port_link_unregister_of(dp);
473 break;
Vivien Didelot1d277322017-11-06 16:11:48 -0500474 case DSA_PORT_TYPE_DSA:
Vivien Didelot0394a632019-08-19 16:00:50 -0400475 dsa_port_disable(dp);
Sebastian Reichel33615362018-01-23 16:03:46 +0100476 dsa_port_link_unregister_of(dp);
Vivien Didelot1d277322017-11-06 16:11:48 -0500477 break;
478 case DSA_PORT_TYPE_USER:
479 if (dp->slave) {
480 dsa_slave_destroy(dp->slave);
481 dp->slave = NULL;
482 }
483 break;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200484 }
Vivien Didelotfb35c602019-10-21 16:51:19 -0400485
486 dp->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200487}
488
Andrew Lunn31224332020-10-04 18:12:53 +0200489static void dsa_port_devlink_teardown(struct dsa_port *dp)
490{
491 struct devlink_port *dlp = &dp->devlink_port;
492
493 if (dp->devlink_port_setup)
494 devlink_port_unregister(dlp);
495 dp->devlink_port_setup = false;
496}
497
Andrew Lunn0f06b852020-09-18 21:11:08 +0200498static int dsa_devlink_info_get(struct devlink *dl,
499 struct devlink_info_req *req,
500 struct netlink_ext_ack *extack)
501{
502 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
503
504 if (ds->ops->devlink_info_get)
505 return ds->ops->devlink_info_get(ds, req, extack);
506
507 return -EOPNOTSUPP;
508}
509
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200510static int dsa_devlink_sb_pool_get(struct devlink *dl,
511 unsigned int sb_index, u16 pool_index,
512 struct devlink_sb_pool_info *pool_info)
513{
514 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
515
516 if (!ds->ops->devlink_sb_pool_get)
517 return -EOPNOTSUPP;
518
519 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
520 pool_info);
521}
522
523static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
524 u16 pool_index, u32 size,
525 enum devlink_sb_threshold_type threshold_type,
526 struct netlink_ext_ack *extack)
527{
528 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
529
530 if (!ds->ops->devlink_sb_pool_set)
531 return -EOPNOTSUPP;
532
533 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
534 threshold_type, extack);
535}
536
537static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
538 unsigned int sb_index, u16 pool_index,
539 u32 *p_threshold)
540{
541 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
542 int port = dsa_devlink_port_to_port(dlp);
543
544 if (!ds->ops->devlink_sb_port_pool_get)
545 return -EOPNOTSUPP;
546
547 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
548 pool_index, p_threshold);
549}
550
551static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
552 unsigned int sb_index, u16 pool_index,
553 u32 threshold,
554 struct netlink_ext_ack *extack)
555{
556 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
557 int port = dsa_devlink_port_to_port(dlp);
558
559 if (!ds->ops->devlink_sb_port_pool_set)
560 return -EOPNOTSUPP;
561
562 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
563 pool_index, threshold, extack);
564}
565
566static int
567dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
568 unsigned int sb_index, u16 tc_index,
569 enum devlink_sb_pool_type pool_type,
570 u16 *p_pool_index, u32 *p_threshold)
571{
572 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
573 int port = dsa_devlink_port_to_port(dlp);
574
575 if (!ds->ops->devlink_sb_tc_pool_bind_get)
576 return -EOPNOTSUPP;
577
578 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
579 tc_index, pool_type,
580 p_pool_index, p_threshold);
581}
582
583static int
584dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
585 unsigned int sb_index, u16 tc_index,
586 enum devlink_sb_pool_type pool_type,
587 u16 pool_index, u32 threshold,
588 struct netlink_ext_ack *extack)
589{
590 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
591 int port = dsa_devlink_port_to_port(dlp);
592
593 if (!ds->ops->devlink_sb_tc_pool_bind_set)
594 return -EOPNOTSUPP;
595
596 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
597 tc_index, pool_type,
598 pool_index, threshold,
599 extack);
600}
601
602static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
603 unsigned int sb_index)
604{
605 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
606
607 if (!ds->ops->devlink_sb_occ_snapshot)
608 return -EOPNOTSUPP;
609
610 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
611}
612
613static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
614 unsigned int sb_index)
615{
616 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
617
618 if (!ds->ops->devlink_sb_occ_max_clear)
619 return -EOPNOTSUPP;
620
621 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
622}
623
624static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
625 unsigned int sb_index,
626 u16 pool_index, u32 *p_cur,
627 u32 *p_max)
628{
629 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
630 int port = dsa_devlink_port_to_port(dlp);
631
632 if (!ds->ops->devlink_sb_occ_port_pool_get)
633 return -EOPNOTSUPP;
634
635 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
636 pool_index, p_cur, p_max);
637}
638
639static int
640dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
641 unsigned int sb_index, u16 tc_index,
642 enum devlink_sb_pool_type pool_type,
643 u32 *p_cur, u32 *p_max)
644{
645 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
646 int port = dsa_devlink_port_to_port(dlp);
647
648 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
649 return -EOPNOTSUPP;
650
651 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
652 sb_index, tc_index,
653 pool_type, p_cur,
654 p_max);
655}
656
Andrew Lunn0f06b852020-09-18 21:11:08 +0200657static const struct devlink_ops dsa_devlink_ops = {
Vladimir Oltean2a6ef762021-01-15 04:11:13 +0200658 .info_get = dsa_devlink_info_get,
659 .sb_pool_get = dsa_devlink_sb_pool_get,
660 .sb_pool_set = dsa_devlink_sb_pool_set,
661 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
662 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
663 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
664 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
665 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
666 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
667 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
668 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
Andrew Lunn0f06b852020-09-18 21:11:08 +0200669};
670
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500671static int dsa_switch_setup(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200672{
Andrew Lunn6b297522019-10-25 01:03:51 +0200673 struct dsa_devlink_priv *dl_priv;
Andrew Lunn31224332020-10-04 18:12:53 +0200674 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400675 int err;
676
677 if (ds->setup)
678 return 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200679
Florian Fainelli6e830d82016-06-07 16:32:39 -0700680 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
Vivien Didelot9d490b42016-08-23 12:38:56 -0400681 * driver and before ops->setup() has run, since the switch drivers and
Florian Fainelli6e830d82016-06-07 16:32:39 -0700682 * the slave MDIO bus driver rely on these values for probing PHY
683 * devices or not
684 */
Vivien Didelot02bc6e52017-10-26 11:22:56 -0400685 ds->phys_mii_mask |= dsa_user_ports(ds);
Florian Fainelli6e830d82016-06-07 16:32:39 -0700686
Andrew Lunn96567d52017-03-28 23:45:07 +0200687 /* Add the switch to devlink before calling setup, so that setup can
688 * add dpipe tables
689 */
Andrew Lunn6b297522019-10-25 01:03:51 +0200690 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
Andrew Lunn96567d52017-03-28 23:45:07 +0200691 if (!ds->devlink)
692 return -ENOMEM;
Andrew Lunn6b297522019-10-25 01:03:51 +0200693 dl_priv = devlink_priv(ds->devlink);
694 dl_priv->ds = ds;
Andrew Lunn96567d52017-03-28 23:45:07 +0200695
696 err = devlink_register(ds->devlink, ds->dev);
697 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300698 goto free_devlink;
Andrew Lunn96567d52017-03-28 23:45:07 +0200699
Andrew Lunn31224332020-10-04 18:12:53 +0200700 /* Setup devlink port instances now, so that the switch
701 * setup() can register regions etc, against the ports
702 */
703 list_for_each_entry(dp, &ds->dst->ports, list) {
704 if (dp->ds == ds) {
705 err = dsa_port_devlink_setup(dp);
706 if (err)
707 goto unregister_devlink_ports;
708 }
709 }
710
Vivien Didelotf515f192017-02-03 13:20:20 -0500711 err = dsa_switch_register_notifier(ds);
712 if (err)
Andrew Lunn31224332020-10-04 18:12:53 +0200713 goto unregister_devlink_ports;
Vivien Didelotf515f192017-02-03 13:20:20 -0500714
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200715 ds->configure_vlan_while_not_filtering = true;
716
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300717 err = ds->ops->setup(ds);
718 if (err < 0)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300719 goto unregister_notifier;
Vladimir Olteanb2243b32019-05-05 13:19:20 +0300720
Andrew Lunn6b297522019-10-25 01:03:51 +0200721 devlink_params_publish(ds->devlink);
722
Vivien Didelot9d490b42016-08-23 12:38:56 -0400723 if (!ds->slave_mii_bus && ds->ops->phy_read) {
Florian Fainelli1eb59442016-06-07 16:32:40 -0700724 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300725 if (!ds->slave_mii_bus) {
726 err = -ENOMEM;
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200727 goto teardown;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300728 }
Florian Fainelli1eb59442016-06-07 16:32:40 -0700729
730 dsa_slave_mii_bus_init(ds);
731
732 err = mdiobus_register(ds->slave_mii_bus);
733 if (err < 0)
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200734 goto teardown;
Florian Fainelli1eb59442016-06-07 16:32:40 -0700735 }
736
Vivien Didelotfb35c602019-10-21 16:51:19 -0400737 ds->setup = true;
738
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200739 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300740
Vladimir Oltean8fd54a72021-02-04 18:33:51 +0200741teardown:
742 if (ds->ops->teardown)
743 ds->ops->teardown(ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300744unregister_notifier:
745 dsa_switch_unregister_notifier(ds);
Andrew Lunn31224332020-10-04 18:12:53 +0200746unregister_devlink_ports:
747 list_for_each_entry(dp, &ds->dst->ports, list)
748 if (dp->ds == ds)
749 dsa_port_devlink_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300750 devlink_unregister(ds->devlink);
751free_devlink:
752 devlink_free(ds->devlink);
753 ds->devlink = NULL;
754
755 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200756}
757
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500758static void dsa_switch_teardown(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200759{
Andrew Lunn31224332020-10-04 18:12:53 +0200760 struct dsa_port *dp;
761
Vivien Didelotfb35c602019-10-21 16:51:19 -0400762 if (!ds->setup)
763 return;
764
Vivien Didelot9d490b42016-08-23 12:38:56 -0400765 if (ds->slave_mii_bus && ds->ops->phy_read)
Florian Fainelli1eb59442016-06-07 16:32:40 -0700766 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500767
768 dsa_switch_unregister_notifier(ds);
Andrew Lunn96567d52017-03-28 23:45:07 +0200769
Vladimir Oltean5e3f8472019-06-08 15:04:28 +0300770 if (ds->ops->teardown)
771 ds->ops->teardown(ds);
772
Andrew Lunn96567d52017-03-28 23:45:07 +0200773 if (ds->devlink) {
Andrew Lunn31224332020-10-04 18:12:53 +0200774 list_for_each_entry(dp, &ds->dst->ports, list)
775 if (dp->ds == ds)
776 dsa_port_devlink_teardown(dp);
Andrew Lunn96567d52017-03-28 23:45:07 +0200777 devlink_unregister(ds->devlink);
778 devlink_free(ds->devlink);
779 ds->devlink = NULL;
780 }
781
Vivien Didelotfb35c602019-10-21 16:51:19 -0400782 ds->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200783}
784
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500785static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
786{
Vivien Didelot1d277322017-11-06 16:11:48 -0500787 struct dsa_port *dp;
Vivien Didelotfb35c602019-10-21 16:51:19 -0400788 int err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500789
Vivien Didelotfb35c602019-10-21 16:51:19 -0400790 list_for_each_entry(dp, &dst->ports, list) {
791 err = dsa_switch_setup(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500792 if (err)
Vivien Didelotfb35c602019-10-21 16:51:19 -0400793 goto teardown;
794 }
Vivien Didelot1d277322017-11-06 16:11:48 -0500795
Vivien Didelotfb35c602019-10-21 16:51:19 -0400796 list_for_each_entry(dp, &dst->ports, list) {
797 err = dsa_port_setup(dp);
798 if (err)
Florian Fainelli86f8b1c2020-05-03 20:50:57 -0700799 continue;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500800 }
801
802 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300803
Vivien Didelotfb35c602019-10-21 16:51:19 -0400804teardown:
805 list_for_each_entry(dp, &dst->ports, list)
806 dsa_port_teardown(dp);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300807
Vivien Didelotfb35c602019-10-21 16:51:19 -0400808 list_for_each_entry(dp, &dst->ports, list)
809 dsa_switch_teardown(dp->ds);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300810
811 return err;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500812}
813
814static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
815{
Vivien Didelot1d277322017-11-06 16:11:48 -0500816 struct dsa_port *dp;
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500817
Vivien Didelotfb35c602019-10-21 16:51:19 -0400818 list_for_each_entry(dp, &dst->ports, list)
819 dsa_port_teardown(dp);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500820
Vivien Didelotfb35c602019-10-21 16:51:19 -0400821 list_for_each_entry(dp, &dst->ports, list)
822 dsa_switch_teardown(dp->ds);
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500823}
824
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500825static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
826{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400827 struct dsa_port *dp;
828 int err;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500829
Vivien Didelot0cfec582019-10-21 16:51:22 -0400830 list_for_each_entry(dp, &dst->ports, list) {
831 if (dsa_port_is_cpu(dp)) {
832 err = dsa_master_setup(dp->master, dp);
833 if (err)
834 return err;
835 }
836 }
837
838 return 0;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500839}
840
841static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
842{
Vivien Didelot0cfec582019-10-21 16:51:22 -0400843 struct dsa_port *dp;
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500844
Vivien Didelot0cfec582019-10-21 16:51:22 -0400845 list_for_each_entry(dp, &dst->ports, list)
846 if (dsa_port_is_cpu(dp))
847 dsa_master_teardown(dp->master);
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500848}
849
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100850static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
851{
852 unsigned int len = 0;
853 struct dsa_port *dp;
854
855 list_for_each_entry(dp, &dst->ports, list) {
856 if (dp->ds->num_lag_ids > len)
857 len = dp->ds->num_lag_ids;
858 }
859
860 if (!len)
861 return 0;
862
863 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
864 if (!dst->lags)
865 return -ENOMEM;
866
867 dst->lags_len = len;
868 return 0;
869}
870
871static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
872{
873 kfree(dst->lags);
874}
875
Vivien Didelotec15dd42017-11-06 16:11:46 -0500876static int dsa_tree_setup(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200877{
Vivien Didelot34c09a82017-11-06 16:11:51 -0500878 bool complete;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200879 int err;
880
Vivien Didelotec15dd42017-11-06 16:11:46 -0500881 if (dst->setup) {
882 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
883 dst->index);
884 return -EEXIST;
885 }
886
Vivien Didelot34c09a82017-11-06 16:11:51 -0500887 complete = dsa_tree_setup_routing_table(dst);
888 if (!complete)
889 return 0;
890
Vivien Didelotf0704642017-11-06 16:11:44 -0500891 err = dsa_tree_setup_default_cpu(dst);
892 if (err)
893 return err;
894
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500895 err = dsa_tree_setup_switches(dst);
896 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300897 goto teardown_default_cpu;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200898
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500899 err = dsa_tree_setup_master(dst);
Vivien Didelot19435632017-09-19 11:56:59 -0400900 if (err)
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300901 goto teardown_switches;
Vivien Didelot19435632017-09-19 11:56:59 -0400902
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100903 err = dsa_tree_setup_lags(dst);
904 if (err)
905 goto teardown_master;
906
Vivien Didelotec15dd42017-11-06 16:11:46 -0500907 dst->setup = true;
908
909 pr_info("DSA: tree %d setup\n", dst->index);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200910
911 return 0;
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300912
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100913teardown_master:
914 dsa_tree_teardown_master(dst);
Ioana Ciorneie70c7aa2019-05-30 09:09:07 +0300915teardown_switches:
916 dsa_tree_teardown_switches(dst);
917teardown_default_cpu:
918 dsa_tree_teardown_default_cpu(dst);
919
920 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200921}
922
Vivien Didelotec15dd42017-11-06 16:11:46 -0500923static void dsa_tree_teardown(struct dsa_switch_tree *dst)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200924{
Vivien Didelotc5f51762019-10-30 22:09:13 -0400925 struct dsa_link *dl, *next;
926
Vivien Didelotec15dd42017-11-06 16:11:46 -0500927 if (!dst->setup)
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200928 return;
929
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100930 dsa_tree_teardown_lags(dst);
931
Vivien Didelot17a22fc2017-11-06 16:11:45 -0500932 dsa_tree_teardown_master(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200933
Vivien Didelot1f08f9e2017-11-06 16:11:47 -0500934 dsa_tree_teardown_switches(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200935
Vivien Didelotf0704642017-11-06 16:11:44 -0500936 dsa_tree_teardown_default_cpu(dst);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700937
Vivien Didelotc5f51762019-10-30 22:09:13 -0400938 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
939 list_del(&dl->list);
940 kfree(dl);
941 }
942
Vivien Didelotec15dd42017-11-06 16:11:46 -0500943 pr_info("DSA: tree %d torn down\n", dst->index);
944
945 dst->setup = false;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200946}
947
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200948/* Since the dsa/tagging sysfs device attribute is per master, the assumption
949 * is that all DSA switches within a tree share the same tagger, otherwise
950 * they would have formed disjoint trees (different "dsa,member" values).
951 */
952int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
953 struct net_device *master,
954 const struct dsa_device_ops *tag_ops,
955 const struct dsa_device_ops *old_tag_ops)
956{
957 struct dsa_notifier_tag_proto_info info;
958 struct dsa_port *dp;
959 int err = -EBUSY;
960
961 if (!rtnl_trylock())
962 return restart_syscall();
963
964 /* At the moment we don't allow changing the tag protocol under
965 * traffic. The rtnl_mutex also happens to serialize concurrent
966 * attempts to change the tagging protocol. If we ever lift the IFF_UP
967 * restriction, there needs to be another mutex which serializes this.
968 */
969 if (master->flags & IFF_UP)
970 goto out_unlock;
971
972 list_for_each_entry(dp, &dst->ports, list) {
973 if (!dsa_is_user_port(dp->ds, dp->index))
974 continue;
975
976 if (dp->slave->flags & IFF_UP)
977 goto out_unlock;
978 }
979
980 info.tag_ops = tag_ops;
981 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
982 if (err)
983 goto out_unwind_tagger;
984
985 dst->tag_ops = tag_ops;
986
987 rtnl_unlock();
988
989 return 0;
990
991out_unwind_tagger:
992 info.tag_ops = old_tag_ops;
993 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
994out_unlock:
995 rtnl_unlock();
996 return err;
997}
998
Vivien Didelotab8ccae2019-10-21 16:51:16 -0400999static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1000{
1001 struct dsa_switch_tree *dst = ds->dst;
1002 struct dsa_port *dp;
1003
Vivien Didelot05f294a2019-10-21 16:51:29 -04001004 list_for_each_entry(dp, &dst->ports, list)
1005 if (dp->ds == ds && dp->index == index)
1006 return dp;
1007
1008 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1009 if (!dp)
1010 return NULL;
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001011
1012 dp->ds = ds;
1013 dp->index = index;
1014
1015 INIT_LIST_HEAD(&dp->list);
1016 list_add_tail(&dp->list, &dst->ports);
1017
1018 return dp;
1019}
1020
Vivien Didelot06e24d02017-11-03 19:05:29 -04001021static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1022{
1023 if (!name)
1024 name = "eth%d";
1025
1026 dp->type = DSA_PORT_TYPE_USER;
1027 dp->name = name;
1028
1029 return 0;
1030}
1031
1032static int dsa_port_parse_dsa(struct dsa_port *dp)
1033{
1034 dp->type = DSA_PORT_TYPE_DSA;
1035
1036 return 0;
1037}
1038
Florian Fainelli4d776482020-01-07 21:06:05 -08001039static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1040 struct net_device *master)
1041{
1042 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1043 struct dsa_switch *mds, *ds = dp->ds;
1044 unsigned int mdp_upstream;
1045 struct dsa_port *mdp;
1046
1047 /* It is possible to stack DSA switches onto one another when that
1048 * happens the switch driver may want to know if its tagging protocol
1049 * is going to work in such a configuration.
1050 */
1051 if (dsa_slave_dev_check(master)) {
1052 mdp = dsa_slave_to_port(master);
1053 mds = mdp->ds;
1054 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1055 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1056 DSA_TAG_PROTO_NONE);
1057 }
1058
1059 /* If the master device is not itself a DSA slave in a disjoint DSA
1060 * tree, then return immediately.
1061 */
1062 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1063}
1064
Vivien Didelot06e24d02017-11-03 19:05:29 -04001065static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
1066{
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001067 struct dsa_switch *ds = dp->ds;
1068 struct dsa_switch_tree *dst = ds->dst;
George McCollistere0c755a2021-03-22 15:26:50 -05001069 const struct dsa_device_ops *tag_ops;
Andrew Lunn7b314362016-08-22 16:01:01 +02001070 enum dsa_tag_protocol tag_protocol;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001071
Florian Fainelli4d776482020-01-07 21:06:05 -08001072 tag_protocol = dsa_get_tag_protocol(dp, master);
Vladimir Oltean357f2032021-01-29 03:00:05 +02001073 if (dst->tag_ops) {
1074 if (dst->tag_ops->proto != tag_protocol) {
1075 dev_err(ds->dev,
1076 "A DSA switch tree can have only one tagging protocol\n");
1077 return -EINVAL;
1078 }
1079 /* In the case of multiple CPU ports per switch, the tagging
1080 * protocol is still reference-counted only per switch tree, so
1081 * nothing to do here.
1082 */
1083 } else {
George McCollistere0c755a2021-03-22 15:26:50 -05001084 tag_ops = dsa_tag_driver_get(tag_protocol);
1085 if (IS_ERR(tag_ops)) {
1086 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
Vladimir Oltean357f2032021-01-29 03:00:05 +02001087 return -EPROBE_DEFER;
1088 dev_warn(ds->dev, "No tagger for this switch\n");
1089 dp->master = NULL;
George McCollistere0c755a2021-03-22 15:26:50 -05001090 return PTR_ERR(tag_ops);
Vladimir Oltean357f2032021-01-29 03:00:05 +02001091 }
George McCollistere0c755a2021-03-22 15:26:50 -05001092
1093 dst->tag_ops = tag_ops;
Florian Fainelli9f9e7722017-07-24 10:49:23 -07001094 }
1095
Florian Fainelli4d776482020-01-07 21:06:05 -08001096 dp->master = master;
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001097 dp->type = DSA_PORT_TYPE_CPU;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +02001098 dsa_port_set_tag_protocol(dp, dst->tag_ops);
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001099 dp->dst = dst;
Vivien Didelot3e41f932017-09-29 17:19:19 -04001100
Vivien Didelot7354fcb2017-11-03 19:05:30 -04001101 return 0;
1102}
1103
Vivien Didelotfd223e22017-10-27 15:55:14 -04001104static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1105{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001106 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
Vivien Didelot1838fa82017-10-27 15:55:18 -04001107 const char *name = of_get_property(dn, "label", NULL);
Vivien Didelot54df6fa2017-11-03 19:05:28 -04001108 bool link = of_property_read_bool(dn, "link");
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001109
Vivien Didelot06e24d02017-11-03 19:05:29 -04001110 dp->dn = dn;
1111
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001112 if (ethernet) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001113 struct net_device *master;
1114
1115 master = of_find_net_device_by_node(ethernet);
1116 if (!master)
1117 return -EPROBE_DEFER;
1118
Vivien Didelot06e24d02017-11-03 19:05:29 -04001119 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001120 }
1121
Vivien Didelot06e24d02017-11-03 19:05:29 -04001122 if (link)
1123 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001124
Vivien Didelot06e24d02017-11-03 19:05:29 -04001125 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001126}
1127
Vivien Didelot975e6e32017-11-03 19:05:27 -04001128static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1129 struct device_node *dn)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001130{
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001131 struct device_node *ports, *port;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001132 struct dsa_port *dp;
Wen Yang9919a362019-02-25 15:22:19 +08001133 int err = 0;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001134 u32 reg;
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001135
1136 ports = of_get_child_by_name(dn, "ports");
1137 if (!ports) {
Kurt Kanzenbach85e05d22020-07-20 14:49:39 +02001138 /* The second possibility is "ethernet-ports" */
1139 ports = of_get_child_by_name(dn, "ethernet-ports");
1140 if (!ports) {
1141 dev_err(ds->dev, "no ports child node found\n");
1142 return -EINVAL;
1143 }
Vivien Didelot5b32fe02017-10-27 15:55:13 -04001144 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001145
1146 for_each_available_child_of_node(ports, port) {
1147 err = of_property_read_u32(port, "reg", &reg);
1148 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001149 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001150
Wen Yang9919a362019-02-25 15:22:19 +08001151 if (reg >= ds->num_ports) {
Rafał Miłecki8209f5b2021-01-06 10:09:15 +01001152 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1153 port, reg, ds->num_ports);
Wen Yang9919a362019-02-25 15:22:19 +08001154 err = -EINVAL;
1155 goto out_put_node;
1156 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001157
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001158 dp = dsa_to_port(ds, reg);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001159
1160 err = dsa_port_parse_of(dp, port);
1161 if (err)
Wen Yang9919a362019-02-25 15:22:19 +08001162 goto out_put_node;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001163 }
1164
Wen Yang9919a362019-02-25 15:22:19 +08001165out_put_node:
1166 of_node_put(ports);
1167 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001168}
1169
Vivien Didelot975e6e32017-11-03 19:05:27 -04001170static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1171 struct device_node *dn)
1172{
1173 u32 m[2] = { 0, 0 };
1174 int sz;
1175
1176 /* Don't error out if this optional property isn't found */
1177 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1178 if (sz < 0 && sz != -EINVAL)
1179 return sz;
1180
1181 ds->index = m[1];
Vivien Didelot975e6e32017-11-03 19:05:27 -04001182
1183 ds->dst = dsa_tree_touch(m[0]);
1184 if (!ds->dst)
1185 return -ENOMEM;
1186
1187 return 0;
1188}
1189
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001190static int dsa_switch_touch_ports(struct dsa_switch *ds)
1191{
1192 struct dsa_port *dp;
1193 int port;
1194
1195 for (port = 0; port < ds->num_ports; port++) {
1196 dp = dsa_port_touch(ds, port);
1197 if (!dp)
1198 return -ENOMEM;
1199 }
1200
1201 return 0;
1202}
1203
Vivien Didelot975e6e32017-11-03 19:05:27 -04001204static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1205{
1206 int err;
1207
1208 err = dsa_switch_parse_member_of(ds, dn);
1209 if (err)
1210 return err;
1211
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001212 err = dsa_switch_touch_ports(ds);
1213 if (err)
1214 return err;
1215
Vivien Didelot975e6e32017-11-03 19:05:27 -04001216 return dsa_switch_parse_ports_of(ds, dn);
1217}
1218
Vivien Didelotfd223e22017-10-27 15:55:14 -04001219static int dsa_port_parse(struct dsa_port *dp, const char *name,
1220 struct device *dev)
1221{
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001222 if (!strcmp(name, "cpu")) {
Vivien Didelotcbabb0a2017-10-27 15:55:17 -04001223 struct net_device *master;
1224
1225 master = dsa_dev_to_net_device(dev);
1226 if (!master)
1227 return -EPROBE_DEFER;
1228
1229 dev_put(master);
1230
Vivien Didelot06e24d02017-11-03 19:05:29 -04001231 return dsa_port_parse_cpu(dp, master);
Vivien Didelot6d4e5c52017-10-27 15:55:15 -04001232 }
1233
Vivien Didelot06e24d02017-11-03 19:05:29 -04001234 if (!strcmp(name, "dsa"))
1235 return dsa_port_parse_dsa(dp);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001236
Vivien Didelot06e24d02017-11-03 19:05:29 -04001237 return dsa_port_parse_user(dp, name);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001238}
1239
Vivien Didelot975e6e32017-11-03 19:05:27 -04001240static int dsa_switch_parse_ports(struct dsa_switch *ds,
1241 struct dsa_chip_data *cd)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001242{
1243 bool valid_name_found = false;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001244 struct dsa_port *dp;
1245 struct device *dev;
1246 const char *name;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001247 unsigned int i;
Vivien Didelotfd223e22017-10-27 15:55:14 -04001248 int err;
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001249
1250 for (i = 0; i < DSA_MAX_PORTS; i++) {
Vivien Didelotfd223e22017-10-27 15:55:14 -04001251 name = cd->port_names[i];
1252 dev = cd->netdev[i];
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001253 dp = dsa_to_port(ds, i);
Vivien Didelotfd223e22017-10-27 15:55:14 -04001254
1255 if (!name)
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001256 continue;
1257
Vivien Didelotfd223e22017-10-27 15:55:14 -04001258 err = dsa_port_parse(dp, name, dev);
1259 if (err)
1260 return err;
1261
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001262 valid_name_found = true;
1263 }
1264
1265 if (!valid_name_found && i == DSA_MAX_PORTS)
1266 return -EINVAL;
1267
1268 return 0;
1269}
1270
Vivien Didelot975e6e32017-11-03 19:05:27 -04001271static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001272{
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001273 int err;
1274
Vivien Didelot975e6e32017-11-03 19:05:27 -04001275 ds->cd = cd;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001276
Vivien Didelot975e6e32017-11-03 19:05:27 -04001277 /* We don't support interconnected switches nor multiple trees via
1278 * platform data, so this is the unique switch of the tree.
1279 */
1280 ds->index = 0;
1281 ds->dst = dsa_tree_touch(0);
1282 if (!ds->dst)
1283 return -ENOMEM;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001284
Vivien Didelotab8ccae2019-10-21 16:51:16 -04001285 err = dsa_switch_touch_ports(ds);
1286 if (err)
1287 return err;
1288
Vivien Didelot975e6e32017-11-03 19:05:27 -04001289 return dsa_switch_parse_ports(ds, cd);
Florian Fainelli71e0bbd2017-02-04 13:02:43 -08001290}
1291
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001292static void dsa_switch_release_ports(struct dsa_switch *ds)
1293{
1294 struct dsa_switch_tree *dst = ds->dst;
1295 struct dsa_port *dp, *next;
1296
1297 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1298 if (dp->ds != ds)
1299 continue;
1300 list_del(&dp->list);
1301 kfree(dp);
1302 }
1303}
1304
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001305static int dsa_switch_probe(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001306{
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001307 struct dsa_switch_tree *dst;
Colin Ian King556f1242019-10-24 11:32:18 +01001308 struct dsa_chip_data *pdata;
1309 struct device_node *np;
Vivien Didelot34c09a82017-11-06 16:11:51 -05001310 int err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001311
Vivien Didelot7e99e342019-10-21 16:51:30 -04001312 if (!ds->dev)
1313 return -ENODEV;
1314
Colin Ian King556f1242019-10-24 11:32:18 +01001315 pdata = ds->dev->platform_data;
1316 np = ds->dev->of_node;
1317
Vivien Didelot7e99e342019-10-21 16:51:30 -04001318 if (!ds->num_ports)
1319 return -EINVAL;
1320
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001321 if (np) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001322 err = dsa_switch_parse_of(ds, np);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001323 if (err)
1324 dsa_switch_release_ports(ds);
1325 } else if (pdata) {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001326 err = dsa_switch_parse(ds, pdata);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001327 if (err)
1328 dsa_switch_release_ports(ds);
1329 } else {
Vivien Didelot975e6e32017-11-03 19:05:27 -04001330 err = -ENODEV;
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001331 }
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001332
Vivien Didelot975e6e32017-11-03 19:05:27 -04001333 if (err)
1334 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001335
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001336 dst = ds->dst;
1337 dsa_tree_get(dst);
1338 err = dsa_tree_setup(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001339 if (err) {
1340 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001341 dsa_tree_put(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001342 }
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001343
1344 return err;
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001345}
1346
Vivien Didelot23c9ee42017-05-26 18:12:51 -04001347int dsa_register_switch(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001348{
1349 int err;
1350
1351 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001352 err = dsa_switch_probe(ds);
Vivien Didelot9e741042017-11-24 11:36:06 -05001353 dsa_tree_put(ds->dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001354 mutex_unlock(&dsa2_mutex);
1355
1356 return err;
1357}
1358EXPORT_SYMBOL_GPL(dsa_register_switch);
1359
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001360static void dsa_switch_remove(struct dsa_switch *ds)
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001361{
1362 struct dsa_switch_tree *dst = ds->dst;
Vivien Didelot05f294a2019-10-21 16:51:29 -04001363
Florian Fainellic058f6d2019-11-02 20:13:26 -07001364 dsa_tree_teardown(dst);
Vladimir Oltean6dc43cd2020-01-25 23:01:11 +02001365 dsa_switch_release_ports(ds);
Vivien Didelot8e5cb842019-10-30 22:09:17 -04001366 dsa_tree_put(dst);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001367}
1368
1369void dsa_unregister_switch(struct dsa_switch *ds)
1370{
1371 mutex_lock(&dsa2_mutex);
Vivien Didelotb4fbb342017-11-06 16:11:53 -05001372 dsa_switch_remove(ds);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02001373 mutex_unlock(&dsa2_mutex);
1374}
1375EXPORT_SYMBOL_GPL(dsa_unregister_switch);