Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 2 | /* |
| 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 Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/list.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 12 | #include <linux/netdevice.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/rtnetlink.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_net.h> |
Jiri Pirko | 402f99e5 | 2019-03-24 11:14:26 +0100 | [diff] [blame] | 17 | #include <net/devlink.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 18 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 19 | #include "dsa_priv.h" |
| 20 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 21 | static DEFINE_MUTEX(dsa2_mutex); |
Vladimir Oltean | bff33f7 | 2020-03-27 21:55:43 +0200 | [diff] [blame] | 22 | LIST_HEAD(dsa_tree_list); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 23 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 24 | /** |
Vladimir Oltean | 886f8e2 | 2021-01-29 03:00:04 +0200 | [diff] [blame] | 25 | * 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 | */ |
| 34 | int 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 | */ |
| 53 | int 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 Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 68 | * 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 | */ |
| 78 | void 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 | */ |
| 109 | void 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 Oltean | 3b7bc1f | 2020-05-10 19:37:42 +0300 | [diff] [blame] | 126 | struct 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 | } |
| 145 | EXPORT_SYMBOL_GPL(dsa_switch_find); |
| 146 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 147 | static struct dsa_switch_tree *dsa_tree_find(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 148 | { |
| 149 | struct dsa_switch_tree *dst; |
| 150 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 151 | list_for_each_entry(dst, &dsa_tree_list, list) |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 152 | if (dst->index == index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 153 | return dst; |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 154 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
| 157 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 158 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 159 | { |
| 160 | struct dsa_switch_tree *dst; |
| 161 | |
| 162 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 163 | if (!dst) |
| 164 | return NULL; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 165 | |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 166 | dst->index = index; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 167 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 168 | INIT_LIST_HEAD(&dst->rtable); |
| 169 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 170 | INIT_LIST_HEAD(&dst->ports); |
| 171 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 172 | INIT_LIST_HEAD(&dst->list); |
Vivien Didelot | 50c7d2ba | 2019-10-18 17:02:46 -0400 | [diff] [blame] | 173 | list_add_tail(&dst->list, &dsa_tree_list); |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 174 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 175 | kref_init(&dst->refcount); |
| 176 | |
| 177 | return dst; |
| 178 | } |
| 179 | |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 180 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
| 181 | { |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 182 | if (dst->tag_ops) |
| 183 | dsa_tag_driver_put(dst->tag_ops); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 184 | list_del(&dst->list); |
| 185 | kfree(dst); |
| 186 | } |
| 187 | |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 188 | static 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 Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 196 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
| 197 | { |
| 198 | struct dsa_switch_tree *dst; |
| 199 | |
| 200 | dst = dsa_tree_find(index); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 201 | if (dst) |
| 202 | return dsa_tree_get(dst); |
| 203 | else |
| 204 | return dsa_tree_alloc(index); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static 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 | |
| 216 | static void dsa_tree_put(struct dsa_switch_tree *dst) |
| 217 | { |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 218 | if (dst) |
| 219 | kref_put(&dst->refcount, dsa_tree_release); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 222 | static bool dsa_port_is_dsa(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 223 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 224 | return port->type == DSA_PORT_TYPE_DSA; |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static bool dsa_port_is_cpu(struct dsa_port *port) |
| 228 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 229 | return port->type == DSA_PORT_TYPE_CPU; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 232 | static bool dsa_port_is_user(struct dsa_port *dp) |
| 233 | { |
| 234 | return dp->type == DSA_PORT_TYPE_USER; |
| 235 | } |
| 236 | |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 237 | static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst, |
| 238 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 239 | { |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 240 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 241 | |
Vivien Didelot | 764b7e6 | 2019-10-21 16:51:21 -0400 | [diff] [blame] | 242 | list_for_each_entry(dp, &dst->ports, list) |
| 243 | if (dp->dn == dn) |
| 244 | return dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 245 | |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Ben Dooks (Codethink) | 4e2ce6e | 2019-12-17 11:20:38 +0000 | [diff] [blame] | 249 | static struct dsa_link *dsa_link_touch(struct dsa_port *dp, |
| 250 | struct dsa_port *link_dp) |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 251 | { |
| 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 Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 275 | static bool dsa_port_setup_routing_table(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 276 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 277 | struct dsa_switch *ds = dp->ds; |
| 278 | struct dsa_switch_tree *dst = ds->dst; |
| 279 | struct device_node *dn = dp->dn; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 280 | struct of_phandle_iterator it; |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 281 | struct dsa_port *link_dp; |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 282 | struct dsa_link *dl; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 283 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 284 | |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 285 | 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 Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 289 | return false; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 290 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 291 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 292 | dl = dsa_link_touch(dp, link_dp); |
| 293 | if (!dl) { |
| 294 | of_node_put(it.node); |
| 295 | return false; |
| 296 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 299 | return true; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 302 | static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 303 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 304 | bool complete = true; |
| 305 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 306 | |
Vivien Didelot | 86bfb2c | 2019-10-21 16:51:20 -0400 | [diff] [blame] | 307 | list_for_each_entry(dp, &dst->ports, list) { |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 308 | if (dsa_port_is_dsa(dp)) { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 309 | complete = dsa_port_setup_routing_table(dp); |
| 310 | if (!complete) |
| 311 | break; |
| 312 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 315 | return complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 318 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
| 319 | { |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 320 | struct dsa_port *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 321 | |
Vivien Didelot | c0b7362 | 2019-10-21 16:51:23 -0400 | [diff] [blame] | 322 | list_for_each_entry(dp, &dst->ports, list) |
| 323 | if (dsa_port_is_cpu(dp)) |
| 324 | return dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 325 | |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) |
| 330 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 331 | struct dsa_port *cpu_dp, *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 332 | |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 333 | 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 Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | /* Assign the default CPU port to all ports of the fabric */ |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 340 | 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 Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) |
| 348 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 349 | 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 Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 356 | static int dsa_port_setup(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 357 | { |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 358 | struct devlink_port *dlp = &dp->devlink_port; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 359 | bool dsa_port_link_registered = false; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 360 | bool dsa_port_enabled = false; |
| 361 | int err = 0; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 362 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 363 | if (dp->setup) |
| 364 | return 0; |
| 365 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 366 | switch (dp->type) { |
| 367 | case DSA_PORT_TYPE_UNUSED: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 368 | dsa_port_disable(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 369 | break; |
| 370 | case DSA_PORT_TYPE_CPU: |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 371 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 372 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 373 | break; |
| 374 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 375 | |
| 376 | err = dsa_port_enable(dp, NULL); |
| 377 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 378 | break; |
| 379 | dsa_port_enabled = true; |
| 380 | |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 381 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 382 | case DSA_PORT_TYPE_DSA: |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 383 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 384 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 385 | break; |
| 386 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 387 | |
| 388 | err = dsa_port_enable(dp, NULL); |
| 389 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 390 | break; |
| 391 | dsa_port_enabled = true; |
| 392 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 393 | break; |
| 394 | case DSA_PORT_TYPE_USER: |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 395 | dp->mac = of_get_mac_address(dp->dn); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 396 | err = dsa_slave_create(dp); |
| 397 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 398 | break; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 399 | |
| 400 | devlink_port_type_eth_set(dlp, dp->slave); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 401 | break; |
| 402 | } |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 403 | |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 404 | 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 Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 408 | if (err) |
| 409 | return err; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 410 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 411 | dp->setup = true; |
| 412 | |
| 413 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 416 | static int dsa_port_devlink_setup(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 417 | { |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 418 | struct devlink_port *dlp = &dp->devlink_port; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 419 | 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 Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 425 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 426 | 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 | |
| 458 | static void dsa_port_teardown(struct dsa_port *dp) |
| 459 | { |
Vladimir Oltean | 91158e1 | 2021-01-12 02:48:31 +0200 | [diff] [blame] | 460 | struct devlink_port *dlp = &dp->devlink_port; |
| 461 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 462 | if (!dp->setup) |
| 463 | return; |
| 464 | |
Vladimir Oltean | 91158e1 | 2021-01-12 02:48:31 +0200 | [diff] [blame] | 465 | devlink_port_type_clear(dlp); |
| 466 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 467 | switch (dp->type) { |
| 468 | case DSA_PORT_TYPE_UNUSED: |
| 469 | break; |
| 470 | case DSA_PORT_TYPE_CPU: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 471 | dsa_port_disable(dp); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 472 | dsa_port_link_unregister_of(dp); |
| 473 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 474 | case DSA_PORT_TYPE_DSA: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 475 | dsa_port_disable(dp); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 476 | dsa_port_link_unregister_of(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 477 | 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 Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 484 | } |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 485 | |
| 486 | dp->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 489 | static 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 Lunn | 0f06b85 | 2020-09-18 21:11:08 +0200 | [diff] [blame] | 498 | static 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 Oltean | 2a6ef76 | 2021-01-15 04:11:13 +0200 | [diff] [blame] | 510 | static 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 | |
| 523 | static 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 | |
| 537 | static 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 | |
| 551 | static 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 | |
| 566 | static int |
| 567 | dsa_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 | |
| 583 | static int |
| 584 | dsa_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 | |
| 602 | static 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 | |
| 613 | static 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 | |
| 624 | static 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 | |
| 639 | static int |
| 640 | dsa_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 Lunn | 0f06b85 | 2020-09-18 21:11:08 +0200 | [diff] [blame] | 657 | static const struct devlink_ops dsa_devlink_ops = { |
Vladimir Oltean | 2a6ef76 | 2021-01-15 04:11:13 +0200 | [diff] [blame] | 658 | .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 Lunn | 0f06b85 | 2020-09-18 21:11:08 +0200 | [diff] [blame] | 669 | }; |
| 670 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 671 | static int dsa_switch_setup(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 672 | { |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 673 | struct dsa_devlink_priv *dl_priv; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 674 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 675 | int err; |
| 676 | |
| 677 | if (ds->setup) |
| 678 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 679 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 680 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 681 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 682 | * the slave MDIO bus driver rely on these values for probing PHY |
| 683 | * devices or not |
| 684 | */ |
Vivien Didelot | 02bc6e5 | 2017-10-26 11:22:56 -0400 | [diff] [blame] | 685 | ds->phys_mii_mask |= dsa_user_ports(ds); |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 686 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 687 | /* Add the switch to devlink before calling setup, so that setup can |
| 688 | * add dpipe tables |
| 689 | */ |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 690 | ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv)); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 691 | if (!ds->devlink) |
| 692 | return -ENOMEM; |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 693 | dl_priv = devlink_priv(ds->devlink); |
| 694 | dl_priv->ds = ds; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 695 | |
| 696 | err = devlink_register(ds->devlink, ds->dev); |
| 697 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 698 | goto free_devlink; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 699 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 700 | /* 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 Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 711 | err = dsa_switch_register_notifier(ds); |
| 712 | if (err) |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 713 | goto unregister_devlink_ports; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 714 | |
Vladimir Oltean | 0ee2af4 | 2021-01-16 01:19:19 +0200 | [diff] [blame] | 715 | ds->configure_vlan_while_not_filtering = true; |
| 716 | |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 717 | err = ds->ops->setup(ds); |
| 718 | if (err < 0) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 719 | goto unregister_notifier; |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 720 | |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 721 | devlink_params_publish(ds->devlink); |
| 722 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 723 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 724 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 725 | if (!ds->slave_mii_bus) { |
| 726 | err = -ENOMEM; |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 727 | goto teardown; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 728 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 729 | |
| 730 | dsa_slave_mii_bus_init(ds); |
| 731 | |
| 732 | err = mdiobus_register(ds->slave_mii_bus); |
| 733 | if (err < 0) |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 734 | goto teardown; |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 737 | ds->setup = true; |
| 738 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 739 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 740 | |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 741 | teardown: |
| 742 | if (ds->ops->teardown) |
| 743 | ds->ops->teardown(ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 744 | unregister_notifier: |
| 745 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 746 | unregister_devlink_ports: |
| 747 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 748 | if (dp->ds == ds) |
| 749 | dsa_port_devlink_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 750 | devlink_unregister(ds->devlink); |
| 751 | free_devlink: |
| 752 | devlink_free(ds->devlink); |
| 753 | ds->devlink = NULL; |
| 754 | |
| 755 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 758 | static void dsa_switch_teardown(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 759 | { |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 760 | struct dsa_port *dp; |
| 761 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 762 | if (!ds->setup) |
| 763 | return; |
| 764 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 765 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 766 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 767 | |
| 768 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 769 | |
Vladimir Oltean | 5e3f847 | 2019-06-08 15:04:28 +0300 | [diff] [blame] | 770 | if (ds->ops->teardown) |
| 771 | ds->ops->teardown(ds); |
| 772 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 773 | if (ds->devlink) { |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 774 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 775 | if (dp->ds == ds) |
| 776 | dsa_port_devlink_teardown(dp); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 777 | devlink_unregister(ds->devlink); |
| 778 | devlink_free(ds->devlink); |
| 779 | ds->devlink = NULL; |
| 780 | } |
| 781 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 782 | ds->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 785 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
| 786 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 787 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 788 | int err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 789 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 790 | list_for_each_entry(dp, &dst->ports, list) { |
| 791 | err = dsa_switch_setup(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 792 | if (err) |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 793 | goto teardown; |
| 794 | } |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 795 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 796 | list_for_each_entry(dp, &dst->ports, list) { |
| 797 | err = dsa_port_setup(dp); |
| 798 | if (err) |
Florian Fainelli | 86f8b1c | 2020-05-03 20:50:57 -0700 | [diff] [blame] | 799 | continue; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 803 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 804 | teardown: |
| 805 | list_for_each_entry(dp, &dst->ports, list) |
| 806 | dsa_port_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 807 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 808 | list_for_each_entry(dp, &dst->ports, list) |
| 809 | dsa_switch_teardown(dp->ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 810 | |
| 811 | return err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) |
| 815 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 816 | struct dsa_port *dp; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 817 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 818 | list_for_each_entry(dp, &dst->ports, list) |
| 819 | dsa_port_teardown(dp); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 820 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 821 | list_for_each_entry(dp, &dst->ports, list) |
| 822 | dsa_switch_teardown(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 823 | } |
| 824 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 825 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
| 826 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 827 | struct dsa_port *dp; |
| 828 | int err; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 829 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 830 | 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 Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) |
| 842 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 843 | struct dsa_port *dp; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 844 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 845 | list_for_each_entry(dp, &dst->ports, list) |
| 846 | if (dsa_port_is_cpu(dp)) |
| 847 | dsa_master_teardown(dp->master); |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 850 | static 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 | |
| 871 | static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst) |
| 872 | { |
| 873 | kfree(dst->lags); |
| 874 | } |
| 875 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 876 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 877 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 878 | bool complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 879 | int err; |
| 880 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 881 | if (dst->setup) { |
| 882 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", |
| 883 | dst->index); |
| 884 | return -EEXIST; |
| 885 | } |
| 886 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 887 | complete = dsa_tree_setup_routing_table(dst); |
| 888 | if (!complete) |
| 889 | return 0; |
| 890 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 891 | err = dsa_tree_setup_default_cpu(dst); |
| 892 | if (err) |
| 893 | return err; |
| 894 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 895 | err = dsa_tree_setup_switches(dst); |
| 896 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 897 | goto teardown_default_cpu; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 898 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 899 | err = dsa_tree_setup_master(dst); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 900 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 901 | goto teardown_switches; |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 902 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 903 | err = dsa_tree_setup_lags(dst); |
| 904 | if (err) |
| 905 | goto teardown_master; |
| 906 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 907 | dst->setup = true; |
| 908 | |
| 909 | pr_info("DSA: tree %d setup\n", dst->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 910 | |
| 911 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 912 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 913 | teardown_master: |
| 914 | dsa_tree_teardown_master(dst); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 915 | teardown_switches: |
| 916 | dsa_tree_teardown_switches(dst); |
| 917 | teardown_default_cpu: |
| 918 | dsa_tree_teardown_default_cpu(dst); |
| 919 | |
| 920 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 923 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 924 | { |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 925 | struct dsa_link *dl, *next; |
| 926 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 927 | if (!dst->setup) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 928 | return; |
| 929 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 930 | dsa_tree_teardown_lags(dst); |
| 931 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 932 | dsa_tree_teardown_master(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 933 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 934 | dsa_tree_teardown_switches(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 935 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 936 | dsa_tree_teardown_default_cpu(dst); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 937 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 938 | list_for_each_entry_safe(dl, next, &dst->rtable, list) { |
| 939 | list_del(&dl->list); |
| 940 | kfree(dl); |
| 941 | } |
| 942 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 943 | pr_info("DSA: tree %d torn down\n", dst->index); |
| 944 | |
| 945 | dst->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 948 | /* 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 | */ |
| 952 | int 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 | |
| 991 | out_unwind_tagger: |
| 992 | info.tag_ops = old_tag_ops; |
| 993 | dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info); |
| 994 | out_unlock: |
| 995 | rtnl_unlock(); |
| 996 | return err; |
| 997 | } |
| 998 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 999 | static 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 Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1004 | 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 Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1011 | |
| 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 Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1021 | static 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 | |
| 1032 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 1033 | { |
| 1034 | dp->type = DSA_PORT_TYPE_DSA; |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1039 | static 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 Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1065 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) |
| 1066 | { |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1067 | struct dsa_switch *ds = dp->ds; |
| 1068 | struct dsa_switch_tree *dst = ds->dst; |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame^] | 1069 | const struct dsa_device_ops *tag_ops; |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 1070 | enum dsa_tag_protocol tag_protocol; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1071 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1072 | tag_protocol = dsa_get_tag_protocol(dp, master); |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1073 | 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 McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame^] | 1084 | tag_ops = dsa_tag_driver_get(tag_protocol); |
| 1085 | if (IS_ERR(tag_ops)) { |
| 1086 | if (PTR_ERR(tag_ops) == -ENOPROTOOPT) |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1087 | return -EPROBE_DEFER; |
| 1088 | dev_warn(ds->dev, "No tagger for this switch\n"); |
| 1089 | dp->master = NULL; |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame^] | 1090 | return PTR_ERR(tag_ops); |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1091 | } |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame^] | 1092 | |
| 1093 | dst->tag_ops = tag_ops; |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1096 | dp->master = master; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1097 | dp->type = DSA_PORT_TYPE_CPU; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1098 | dsa_port_set_tag_protocol(dp, dst->tag_ops); |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1099 | dp->dst = dst; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 1100 | |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1101 | return 0; |
| 1102 | } |
| 1103 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1104 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 1105 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1106 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
Vivien Didelot | 1838fa8 | 2017-10-27 15:55:18 -0400 | [diff] [blame] | 1107 | const char *name = of_get_property(dn, "label", NULL); |
Vivien Didelot | 54df6fa | 2017-11-03 19:05:28 -0400 | [diff] [blame] | 1108 | bool link = of_property_read_bool(dn, "link"); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1109 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1110 | dp->dn = dn; |
| 1111 | |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1112 | if (ethernet) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1113 | struct net_device *master; |
| 1114 | |
| 1115 | master = of_find_net_device_by_node(ethernet); |
| 1116 | if (!master) |
| 1117 | return -EPROBE_DEFER; |
| 1118 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1119 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1120 | } |
| 1121 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1122 | if (link) |
| 1123 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1124 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1125 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1126 | } |
| 1127 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1128 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 1129 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1130 | { |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1131 | struct device_node *ports, *port; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1132 | struct dsa_port *dp; |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1133 | int err = 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1134 | u32 reg; |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1135 | |
| 1136 | ports = of_get_child_by_name(dn, "ports"); |
| 1137 | if (!ports) { |
Kurt Kanzenbach | 85e05d2 | 2020-07-20 14:49:39 +0200 | [diff] [blame] | 1138 | /* 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 Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1144 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1145 | |
| 1146 | for_each_available_child_of_node(ports, port) { |
| 1147 | err = of_property_read_u32(port, "reg", ®); |
| 1148 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1149 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1150 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1151 | if (reg >= ds->num_ports) { |
Rafał Miłecki | 8209f5b | 2021-01-06 10:09:15 +0100 | [diff] [blame] | 1152 | dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n", |
| 1153 | port, reg, ds->num_ports); |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1154 | err = -EINVAL; |
| 1155 | goto out_put_node; |
| 1156 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1157 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1158 | dp = dsa_to_port(ds, reg); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1159 | |
| 1160 | err = dsa_port_parse_of(dp, port); |
| 1161 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1162 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1165 | out_put_node: |
| 1166 | of_node_put(ports); |
| 1167 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1170 | static 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 Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1182 | |
| 1183 | ds->dst = dsa_tree_touch(m[0]); |
| 1184 | if (!ds->dst) |
| 1185 | return -ENOMEM; |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1190 | static 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 Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1204 | static 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 Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1212 | err = dsa_switch_touch_ports(ds); |
| 1213 | if (err) |
| 1214 | return err; |
| 1215 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1216 | return dsa_switch_parse_ports_of(ds, dn); |
| 1217 | } |
| 1218 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1219 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 1220 | struct device *dev) |
| 1221 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1222 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1223 | 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 Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1231 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1234 | if (!strcmp(name, "dsa")) |
| 1235 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1236 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1237 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1240 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 1241 | struct dsa_chip_data *cd) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1242 | { |
| 1243 | bool valid_name_found = false; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1244 | struct dsa_port *dp; |
| 1245 | struct device *dev; |
| 1246 | const char *name; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1247 | unsigned int i; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1248 | int err; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1249 | |
| 1250 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1251 | name = cd->port_names[i]; |
| 1252 | dev = cd->netdev[i]; |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1253 | dp = dsa_to_port(ds, i); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1254 | |
| 1255 | if (!name) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1256 | continue; |
| 1257 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1258 | err = dsa_port_parse(dp, name, dev); |
| 1259 | if (err) |
| 1260 | return err; |
| 1261 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1262 | 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 Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1271 | static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1272 | { |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1273 | int err; |
| 1274 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1275 | ds->cd = cd; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1276 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1277 | /* 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 Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1284 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1285 | err = dsa_switch_touch_ports(ds); |
| 1286 | if (err) |
| 1287 | return err; |
| 1288 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1289 | return dsa_switch_parse_ports(ds, cd); |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1292 | static 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 Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1305 | static int dsa_switch_probe(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1306 | { |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1307 | struct dsa_switch_tree *dst; |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1308 | struct dsa_chip_data *pdata; |
| 1309 | struct device_node *np; |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 1310 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1311 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1312 | if (!ds->dev) |
| 1313 | return -ENODEV; |
| 1314 | |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1315 | pdata = ds->dev->platform_data; |
| 1316 | np = ds->dev->of_node; |
| 1317 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1318 | if (!ds->num_ports) |
| 1319 | return -EINVAL; |
| 1320 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1321 | if (np) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1322 | err = dsa_switch_parse_of(ds, np); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1323 | if (err) |
| 1324 | dsa_switch_release_ports(ds); |
| 1325 | } else if (pdata) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1326 | err = dsa_switch_parse(ds, pdata); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1327 | if (err) |
| 1328 | dsa_switch_release_ports(ds); |
| 1329 | } else { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1330 | err = -ENODEV; |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1331 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1332 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1333 | if (err) |
| 1334 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1335 | |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1336 | dst = ds->dst; |
| 1337 | dsa_tree_get(dst); |
| 1338 | err = dsa_tree_setup(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1339 | if (err) { |
| 1340 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1341 | dsa_tree_put(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1342 | } |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1343 | |
| 1344 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 1347 | int dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1348 | { |
| 1349 | int err; |
| 1350 | |
| 1351 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1352 | err = dsa_switch_probe(ds); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 1353 | dsa_tree_put(ds->dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1354 | mutex_unlock(&dsa2_mutex); |
| 1355 | |
| 1356 | return err; |
| 1357 | } |
| 1358 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 1359 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1360 | static void dsa_switch_remove(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1361 | { |
| 1362 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1363 | |
Florian Fainelli | c058f6d | 2019-11-02 20:13:26 -0700 | [diff] [blame] | 1364 | dsa_tree_teardown(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1365 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1366 | dsa_tree_put(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 1370 | { |
| 1371 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1372 | dsa_switch_remove(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1373 | mutex_unlock(&dsa2_mutex); |
| 1374 | } |
| 1375 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |