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: |
Michael Walle | 83216e3 | 2021-04-12 19:47:17 +0200 | [diff] [blame] | 395 | of_get_mac_address(dp->dn, dp->mac); |
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 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 671 | static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds) |
| 672 | { |
| 673 | const struct dsa_device_ops *tag_ops = ds->dst->tag_ops; |
| 674 | struct dsa_switch_tree *dst = ds->dst; |
| 675 | int port, err; |
| 676 | |
| 677 | if (tag_ops->proto == dst->default_proto) |
| 678 | return 0; |
| 679 | |
| 680 | for (port = 0; port < ds->num_ports; port++) { |
| 681 | if (!dsa_is_cpu_port(ds, port)) |
| 682 | continue; |
| 683 | |
| 684 | err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto); |
| 685 | if (err) { |
| 686 | dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n", |
| 687 | tag_ops->name, ERR_PTR(err)); |
| 688 | return err; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 695 | static int dsa_switch_setup(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 696 | { |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 697 | struct dsa_devlink_priv *dl_priv; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 698 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 699 | int err; |
| 700 | |
| 701 | if (ds->setup) |
| 702 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 703 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 704 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 705 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 706 | * the slave MDIO bus driver rely on these values for probing PHY |
| 707 | * devices or not |
| 708 | */ |
Vivien Didelot | 02bc6e5 | 2017-10-26 11:22:56 -0400 | [diff] [blame] | 709 | ds->phys_mii_mask |= dsa_user_ports(ds); |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 710 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 711 | /* Add the switch to devlink before calling setup, so that setup can |
| 712 | * add dpipe tables |
| 713 | */ |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 714 | ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv)); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 715 | if (!ds->devlink) |
| 716 | return -ENOMEM; |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 717 | dl_priv = devlink_priv(ds->devlink); |
| 718 | dl_priv->ds = ds; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 719 | |
| 720 | err = devlink_register(ds->devlink, ds->dev); |
| 721 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 722 | goto free_devlink; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 723 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 724 | /* Setup devlink port instances now, so that the switch |
| 725 | * setup() can register regions etc, against the ports |
| 726 | */ |
| 727 | list_for_each_entry(dp, &ds->dst->ports, list) { |
| 728 | if (dp->ds == ds) { |
| 729 | err = dsa_port_devlink_setup(dp); |
| 730 | if (err) |
| 731 | goto unregister_devlink_ports; |
| 732 | } |
| 733 | } |
| 734 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 735 | err = dsa_switch_register_notifier(ds); |
| 736 | if (err) |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 737 | goto unregister_devlink_ports; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 738 | |
Vladimir Oltean | 0ee2af4 | 2021-01-16 01:19:19 +0200 | [diff] [blame] | 739 | ds->configure_vlan_while_not_filtering = true; |
| 740 | |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 741 | err = ds->ops->setup(ds); |
| 742 | if (err < 0) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 743 | goto unregister_notifier; |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 744 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 745 | err = dsa_switch_setup_tag_protocol(ds); |
| 746 | if (err) |
| 747 | goto teardown; |
| 748 | |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 749 | devlink_params_publish(ds->devlink); |
| 750 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 751 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 752 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 753 | if (!ds->slave_mii_bus) { |
| 754 | err = -ENOMEM; |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 755 | goto teardown; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 756 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 757 | |
| 758 | dsa_slave_mii_bus_init(ds); |
| 759 | |
| 760 | err = mdiobus_register(ds->slave_mii_bus); |
| 761 | if (err < 0) |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 762 | goto teardown; |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 765 | ds->setup = true; |
| 766 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 767 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 768 | |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 769 | teardown: |
| 770 | if (ds->ops->teardown) |
| 771 | ds->ops->teardown(ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 772 | unregister_notifier: |
| 773 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 774 | unregister_devlink_ports: |
| 775 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 776 | if (dp->ds == ds) |
| 777 | dsa_port_devlink_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 778 | devlink_unregister(ds->devlink); |
| 779 | free_devlink: |
| 780 | devlink_free(ds->devlink); |
| 781 | ds->devlink = NULL; |
| 782 | |
| 783 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 786 | static void dsa_switch_teardown(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 787 | { |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 788 | struct dsa_port *dp; |
| 789 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 790 | if (!ds->setup) |
| 791 | return; |
| 792 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 793 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 794 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 795 | |
| 796 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 797 | |
Vladimir Oltean | 5e3f847 | 2019-06-08 15:04:28 +0300 | [diff] [blame] | 798 | if (ds->ops->teardown) |
| 799 | ds->ops->teardown(ds); |
| 800 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 801 | if (ds->devlink) { |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 802 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 803 | if (dp->ds == ds) |
| 804 | dsa_port_devlink_teardown(dp); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 805 | devlink_unregister(ds->devlink); |
| 806 | devlink_free(ds->devlink); |
| 807 | ds->devlink = NULL; |
| 808 | } |
| 809 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 810 | ds->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 813 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
| 814 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 815 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 816 | int err; |
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 | err = dsa_switch_setup(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 820 | if (err) |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 821 | goto teardown; |
| 822 | } |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 823 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 824 | list_for_each_entry(dp, &dst->ports, list) { |
| 825 | err = dsa_port_setup(dp); |
Maxim Kochetkov | fb6ec87 | 2021-03-29 18:30:16 +0300 | [diff] [blame] | 826 | if (err) { |
| 827 | dsa_port_devlink_teardown(dp); |
| 828 | dp->type = DSA_PORT_TYPE_UNUSED; |
| 829 | err = dsa_port_devlink_setup(dp); |
| 830 | if (err) |
| 831 | goto teardown; |
Florian Fainelli | 86f8b1c | 2020-05-03 20:50:57 -0700 | [diff] [blame] | 832 | continue; |
Maxim Kochetkov | fb6ec87 | 2021-03-29 18:30:16 +0300 | [diff] [blame] | 833 | } |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 837 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 838 | teardown: |
| 839 | list_for_each_entry(dp, &dst->ports, list) |
| 840 | dsa_port_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 841 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 842 | list_for_each_entry(dp, &dst->ports, list) |
| 843 | dsa_switch_teardown(dp->ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 844 | |
| 845 | return err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) |
| 849 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 850 | struct dsa_port *dp; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 851 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 852 | list_for_each_entry(dp, &dst->ports, list) |
| 853 | dsa_port_teardown(dp); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 854 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 855 | list_for_each_entry(dp, &dst->ports, list) |
| 856 | dsa_switch_teardown(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 857 | } |
| 858 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 859 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
| 860 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 861 | struct dsa_port *dp; |
| 862 | int err; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 863 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 864 | list_for_each_entry(dp, &dst->ports, list) { |
| 865 | if (dsa_port_is_cpu(dp)) { |
| 866 | err = dsa_master_setup(dp->master, dp); |
| 867 | if (err) |
| 868 | return err; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | return 0; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) |
| 876 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 877 | struct dsa_port *dp; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 878 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 879 | list_for_each_entry(dp, &dst->ports, list) |
| 880 | if (dsa_port_is_cpu(dp)) |
| 881 | dsa_master_teardown(dp->master); |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 882 | } |
| 883 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 884 | static int dsa_tree_setup_lags(struct dsa_switch_tree *dst) |
| 885 | { |
| 886 | unsigned int len = 0; |
| 887 | struct dsa_port *dp; |
| 888 | |
| 889 | list_for_each_entry(dp, &dst->ports, list) { |
| 890 | if (dp->ds->num_lag_ids > len) |
| 891 | len = dp->ds->num_lag_ids; |
| 892 | } |
| 893 | |
| 894 | if (!len) |
| 895 | return 0; |
| 896 | |
| 897 | dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL); |
| 898 | if (!dst->lags) |
| 899 | return -ENOMEM; |
| 900 | |
| 901 | dst->lags_len = len; |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst) |
| 906 | { |
| 907 | kfree(dst->lags); |
| 908 | } |
| 909 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 910 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 911 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 912 | bool complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 913 | int err; |
| 914 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 915 | if (dst->setup) { |
| 916 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", |
| 917 | dst->index); |
| 918 | return -EEXIST; |
| 919 | } |
| 920 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 921 | complete = dsa_tree_setup_routing_table(dst); |
| 922 | if (!complete) |
| 923 | return 0; |
| 924 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 925 | err = dsa_tree_setup_default_cpu(dst); |
| 926 | if (err) |
| 927 | return err; |
| 928 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 929 | err = dsa_tree_setup_switches(dst); |
| 930 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 931 | goto teardown_default_cpu; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 932 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 933 | err = dsa_tree_setup_master(dst); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 934 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 935 | goto teardown_switches; |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 936 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 937 | err = dsa_tree_setup_lags(dst); |
| 938 | if (err) |
| 939 | goto teardown_master; |
| 940 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 941 | dst->setup = true; |
| 942 | |
| 943 | pr_info("DSA: tree %d setup\n", dst->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 944 | |
| 945 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 946 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 947 | teardown_master: |
| 948 | dsa_tree_teardown_master(dst); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 949 | teardown_switches: |
| 950 | dsa_tree_teardown_switches(dst); |
| 951 | teardown_default_cpu: |
| 952 | dsa_tree_teardown_default_cpu(dst); |
| 953 | |
| 954 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 957 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 958 | { |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 959 | struct dsa_link *dl, *next; |
| 960 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 961 | if (!dst->setup) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 962 | return; |
| 963 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 964 | dsa_tree_teardown_lags(dst); |
| 965 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 966 | dsa_tree_teardown_master(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 967 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 968 | dsa_tree_teardown_switches(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 969 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 970 | dsa_tree_teardown_default_cpu(dst); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 971 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 972 | list_for_each_entry_safe(dl, next, &dst->rtable, list) { |
| 973 | list_del(&dl->list); |
| 974 | kfree(dl); |
| 975 | } |
| 976 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 977 | pr_info("DSA: tree %d torn down\n", dst->index); |
| 978 | |
| 979 | dst->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 982 | /* Since the dsa/tagging sysfs device attribute is per master, the assumption |
| 983 | * is that all DSA switches within a tree share the same tagger, otherwise |
| 984 | * they would have formed disjoint trees (different "dsa,member" values). |
| 985 | */ |
| 986 | int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst, |
| 987 | struct net_device *master, |
| 988 | const struct dsa_device_ops *tag_ops, |
| 989 | const struct dsa_device_ops *old_tag_ops) |
| 990 | { |
| 991 | struct dsa_notifier_tag_proto_info info; |
| 992 | struct dsa_port *dp; |
| 993 | int err = -EBUSY; |
| 994 | |
| 995 | if (!rtnl_trylock()) |
| 996 | return restart_syscall(); |
| 997 | |
| 998 | /* At the moment we don't allow changing the tag protocol under |
| 999 | * traffic. The rtnl_mutex also happens to serialize concurrent |
| 1000 | * attempts to change the tagging protocol. If we ever lift the IFF_UP |
| 1001 | * restriction, there needs to be another mutex which serializes this. |
| 1002 | */ |
| 1003 | if (master->flags & IFF_UP) |
| 1004 | goto out_unlock; |
| 1005 | |
| 1006 | list_for_each_entry(dp, &dst->ports, list) { |
| 1007 | if (!dsa_is_user_port(dp->ds, dp->index)) |
| 1008 | continue; |
| 1009 | |
| 1010 | if (dp->slave->flags & IFF_UP) |
| 1011 | goto out_unlock; |
| 1012 | } |
| 1013 | |
| 1014 | info.tag_ops = tag_ops; |
| 1015 | err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info); |
| 1016 | if (err) |
| 1017 | goto out_unwind_tagger; |
| 1018 | |
| 1019 | dst->tag_ops = tag_ops; |
| 1020 | |
| 1021 | rtnl_unlock(); |
| 1022 | |
| 1023 | return 0; |
| 1024 | |
| 1025 | out_unwind_tagger: |
| 1026 | info.tag_ops = old_tag_ops; |
| 1027 | dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info); |
| 1028 | out_unlock: |
| 1029 | rtnl_unlock(); |
| 1030 | return err; |
| 1031 | } |
| 1032 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1033 | static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) |
| 1034 | { |
| 1035 | struct dsa_switch_tree *dst = ds->dst; |
| 1036 | struct dsa_port *dp; |
| 1037 | |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1038 | list_for_each_entry(dp, &dst->ports, list) |
| 1039 | if (dp->ds == ds && dp->index == index) |
| 1040 | return dp; |
| 1041 | |
| 1042 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 1043 | if (!dp) |
| 1044 | return NULL; |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1045 | |
| 1046 | dp->ds = ds; |
| 1047 | dp->index = index; |
| 1048 | |
| 1049 | INIT_LIST_HEAD(&dp->list); |
| 1050 | list_add_tail(&dp->list, &dst->ports); |
| 1051 | |
| 1052 | return dp; |
| 1053 | } |
| 1054 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1055 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
| 1056 | { |
| 1057 | if (!name) |
| 1058 | name = "eth%d"; |
| 1059 | |
| 1060 | dp->type = DSA_PORT_TYPE_USER; |
| 1061 | dp->name = name; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 1067 | { |
| 1068 | dp->type = DSA_PORT_TYPE_DSA; |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1073 | static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp, |
| 1074 | struct net_device *master) |
| 1075 | { |
| 1076 | enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE; |
| 1077 | struct dsa_switch *mds, *ds = dp->ds; |
| 1078 | unsigned int mdp_upstream; |
| 1079 | struct dsa_port *mdp; |
| 1080 | |
| 1081 | /* It is possible to stack DSA switches onto one another when that |
| 1082 | * happens the switch driver may want to know if its tagging protocol |
| 1083 | * is going to work in such a configuration. |
| 1084 | */ |
| 1085 | if (dsa_slave_dev_check(master)) { |
| 1086 | mdp = dsa_slave_to_port(master); |
| 1087 | mds = mdp->ds; |
| 1088 | mdp_upstream = dsa_upstream_port(mds, mdp->index); |
| 1089 | tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream, |
| 1090 | DSA_TAG_PROTO_NONE); |
| 1091 | } |
| 1092 | |
| 1093 | /* If the master device is not itself a DSA slave in a disjoint DSA |
| 1094 | * tree, then return immediately. |
| 1095 | */ |
| 1096 | return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol); |
| 1097 | } |
| 1098 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1099 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master, |
| 1100 | const char *user_protocol) |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1101 | { |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1102 | struct dsa_switch *ds = dp->ds; |
| 1103 | struct dsa_switch_tree *dst = ds->dst; |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1104 | const struct dsa_device_ops *tag_ops; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1105 | enum dsa_tag_protocol default_proto; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1106 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1107 | /* Find out which protocol the switch would prefer. */ |
| 1108 | default_proto = dsa_get_tag_protocol(dp, master); |
| 1109 | if (dst->default_proto) { |
| 1110 | if (dst->default_proto != default_proto) { |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1111 | dev_err(ds->dev, |
| 1112 | "A DSA switch tree can have only one tagging protocol\n"); |
| 1113 | return -EINVAL; |
| 1114 | } |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1115 | } else { |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1116 | dst->default_proto = default_proto; |
| 1117 | } |
| 1118 | |
| 1119 | /* See if the user wants to override that preference. */ |
| 1120 | if (user_protocol) { |
| 1121 | if (!ds->ops->change_tag_protocol) { |
| 1122 | dev_err(ds->dev, "Tag protocol cannot be modified\n"); |
| 1123 | return -EINVAL; |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1124 | } |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1125 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1126 | tag_ops = dsa_find_tagger_by_name(user_protocol); |
| 1127 | } else { |
| 1128 | tag_ops = dsa_tag_driver_get(default_proto); |
| 1129 | } |
| 1130 | |
| 1131 | if (IS_ERR(tag_ops)) { |
| 1132 | if (PTR_ERR(tag_ops) == -ENOPROTOOPT) |
| 1133 | return -EPROBE_DEFER; |
| 1134 | |
| 1135 | dev_warn(ds->dev, "No tagger for this switch\n"); |
| 1136 | return PTR_ERR(tag_ops); |
| 1137 | } |
| 1138 | |
| 1139 | if (dst->tag_ops) { |
| 1140 | if (dst->tag_ops != tag_ops) { |
| 1141 | dev_err(ds->dev, |
| 1142 | "A DSA switch tree can have only one tagging protocol\n"); |
| 1143 | |
| 1144 | dsa_tag_driver_put(tag_ops); |
| 1145 | return -EINVAL; |
| 1146 | } |
| 1147 | |
| 1148 | /* In the case of multiple CPU ports per switch, the tagging |
| 1149 | * protocol is still reference-counted only per switch tree. |
| 1150 | */ |
| 1151 | dsa_tag_driver_put(tag_ops); |
| 1152 | } else { |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1153 | dst->tag_ops = tag_ops; |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1156 | dp->master = master; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1157 | dp->type = DSA_PORT_TYPE_CPU; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1158 | dsa_port_set_tag_protocol(dp, dst->tag_ops); |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1159 | dp->dst = dst; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 1160 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1161 | /* At this point, the tree may be configured to use a different |
| 1162 | * tagger than the one chosen by the switch driver during |
| 1163 | * .setup, in the case when a user selects a custom protocol |
| 1164 | * through the DT. |
| 1165 | * |
| 1166 | * This is resolved by syncing the driver with the tree in |
| 1167 | * dsa_switch_setup_tag_protocol once .setup has run and the |
| 1168 | * driver is ready to accept calls to .change_tag_protocol. If |
| 1169 | * the driver does not support the custom protocol at that |
| 1170 | * point, the tree is wholly rejected, thereby ensuring that the |
| 1171 | * tree and driver are always in agreement on the protocol to |
| 1172 | * use. |
| 1173 | */ |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1174 | return 0; |
| 1175 | } |
| 1176 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1177 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 1178 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1179 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
Vivien Didelot | 1838fa8 | 2017-10-27 15:55:18 -0400 | [diff] [blame] | 1180 | const char *name = of_get_property(dn, "label", NULL); |
Vivien Didelot | 54df6fa | 2017-11-03 19:05:28 -0400 | [diff] [blame] | 1181 | bool link = of_property_read_bool(dn, "link"); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1182 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1183 | dp->dn = dn; |
| 1184 | |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1185 | if (ethernet) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1186 | struct net_device *master; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1187 | const char *user_protocol; |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1188 | |
| 1189 | master = of_find_net_device_by_node(ethernet); |
| 1190 | if (!master) |
| 1191 | return -EPROBE_DEFER; |
| 1192 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1193 | user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL); |
| 1194 | return dsa_port_parse_cpu(dp, master, user_protocol); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1197 | if (link) |
| 1198 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1199 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1200 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1203 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 1204 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1205 | { |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1206 | struct device_node *ports, *port; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1207 | struct dsa_port *dp; |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1208 | int err = 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1209 | u32 reg; |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1210 | |
| 1211 | ports = of_get_child_by_name(dn, "ports"); |
| 1212 | if (!ports) { |
Kurt Kanzenbach | 85e05d2 | 2020-07-20 14:49:39 +0200 | [diff] [blame] | 1213 | /* The second possibility is "ethernet-ports" */ |
| 1214 | ports = of_get_child_by_name(dn, "ethernet-ports"); |
| 1215 | if (!ports) { |
| 1216 | dev_err(ds->dev, "no ports child node found\n"); |
| 1217 | return -EINVAL; |
| 1218 | } |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1219 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1220 | |
| 1221 | for_each_available_child_of_node(ports, port) { |
| 1222 | err = of_property_read_u32(port, "reg", ®); |
| 1223 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1224 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1225 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1226 | if (reg >= ds->num_ports) { |
Rafał Miłecki | 8209f5b | 2021-01-06 10:09:15 +0100 | [diff] [blame] | 1227 | dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n", |
| 1228 | port, reg, ds->num_ports); |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1229 | err = -EINVAL; |
| 1230 | goto out_put_node; |
| 1231 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1232 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1233 | dp = dsa_to_port(ds, reg); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1234 | |
| 1235 | err = dsa_port_parse_of(dp, port); |
| 1236 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1237 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1240 | out_put_node: |
| 1241 | of_node_put(ports); |
| 1242 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1245 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
| 1246 | struct device_node *dn) |
| 1247 | { |
| 1248 | u32 m[2] = { 0, 0 }; |
| 1249 | int sz; |
| 1250 | |
| 1251 | /* Don't error out if this optional property isn't found */ |
| 1252 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); |
| 1253 | if (sz < 0 && sz != -EINVAL) |
| 1254 | return sz; |
| 1255 | |
| 1256 | ds->index = m[1]; |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1257 | |
| 1258 | ds->dst = dsa_tree_touch(m[0]); |
| 1259 | if (!ds->dst) |
| 1260 | return -ENOMEM; |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1265 | static int dsa_switch_touch_ports(struct dsa_switch *ds) |
| 1266 | { |
| 1267 | struct dsa_port *dp; |
| 1268 | int port; |
| 1269 | |
| 1270 | for (port = 0; port < ds->num_ports; port++) { |
| 1271 | dp = dsa_port_touch(ds, port); |
| 1272 | if (!dp) |
| 1273 | return -ENOMEM; |
| 1274 | } |
| 1275 | |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1279 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) |
| 1280 | { |
| 1281 | int err; |
| 1282 | |
| 1283 | err = dsa_switch_parse_member_of(ds, dn); |
| 1284 | if (err) |
| 1285 | return err; |
| 1286 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1287 | err = dsa_switch_touch_ports(ds); |
| 1288 | if (err) |
| 1289 | return err; |
| 1290 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1291 | return dsa_switch_parse_ports_of(ds, dn); |
| 1292 | } |
| 1293 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1294 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 1295 | struct device *dev) |
| 1296 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1297 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1298 | struct net_device *master; |
| 1299 | |
| 1300 | master = dsa_dev_to_net_device(dev); |
| 1301 | if (!master) |
| 1302 | return -EPROBE_DEFER; |
| 1303 | |
| 1304 | dev_put(master); |
| 1305 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame^] | 1306 | return dsa_port_parse_cpu(dp, master, NULL); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1307 | } |
| 1308 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1309 | if (!strcmp(name, "dsa")) |
| 1310 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1311 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1312 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1313 | } |
| 1314 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1315 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 1316 | struct dsa_chip_data *cd) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1317 | { |
| 1318 | bool valid_name_found = false; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1319 | struct dsa_port *dp; |
| 1320 | struct device *dev; |
| 1321 | const char *name; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1322 | unsigned int i; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1323 | int err; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1324 | |
| 1325 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1326 | name = cd->port_names[i]; |
| 1327 | dev = cd->netdev[i]; |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1328 | dp = dsa_to_port(ds, i); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1329 | |
| 1330 | if (!name) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1331 | continue; |
| 1332 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1333 | err = dsa_port_parse(dp, name, dev); |
| 1334 | if (err) |
| 1335 | return err; |
| 1336 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1337 | valid_name_found = true; |
| 1338 | } |
| 1339 | |
| 1340 | if (!valid_name_found && i == DSA_MAX_PORTS) |
| 1341 | return -EINVAL; |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1346 | 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] | 1347 | { |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1348 | int err; |
| 1349 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1350 | ds->cd = cd; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1351 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1352 | /* We don't support interconnected switches nor multiple trees via |
| 1353 | * platform data, so this is the unique switch of the tree. |
| 1354 | */ |
| 1355 | ds->index = 0; |
| 1356 | ds->dst = dsa_tree_touch(0); |
| 1357 | if (!ds->dst) |
| 1358 | return -ENOMEM; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1359 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1360 | err = dsa_switch_touch_ports(ds); |
| 1361 | if (err) |
| 1362 | return err; |
| 1363 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1364 | return dsa_switch_parse_ports(ds, cd); |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1367 | static void dsa_switch_release_ports(struct dsa_switch *ds) |
| 1368 | { |
| 1369 | struct dsa_switch_tree *dst = ds->dst; |
| 1370 | struct dsa_port *dp, *next; |
| 1371 | |
| 1372 | list_for_each_entry_safe(dp, next, &dst->ports, list) { |
| 1373 | if (dp->ds != ds) |
| 1374 | continue; |
| 1375 | list_del(&dp->list); |
| 1376 | kfree(dp); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1380 | static int dsa_switch_probe(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1381 | { |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1382 | struct dsa_switch_tree *dst; |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1383 | struct dsa_chip_data *pdata; |
| 1384 | struct device_node *np; |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 1385 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1386 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1387 | if (!ds->dev) |
| 1388 | return -ENODEV; |
| 1389 | |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1390 | pdata = ds->dev->platform_data; |
| 1391 | np = ds->dev->of_node; |
| 1392 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1393 | if (!ds->num_ports) |
| 1394 | return -EINVAL; |
| 1395 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1396 | if (np) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1397 | err = dsa_switch_parse_of(ds, np); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1398 | if (err) |
| 1399 | dsa_switch_release_ports(ds); |
| 1400 | } else if (pdata) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1401 | err = dsa_switch_parse(ds, pdata); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1402 | if (err) |
| 1403 | dsa_switch_release_ports(ds); |
| 1404 | } else { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1405 | err = -ENODEV; |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1406 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1407 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1408 | if (err) |
| 1409 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1410 | |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1411 | dst = ds->dst; |
| 1412 | dsa_tree_get(dst); |
| 1413 | err = dsa_tree_setup(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1414 | if (err) { |
| 1415 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1416 | dsa_tree_put(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1417 | } |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1418 | |
| 1419 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 1422 | int dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1423 | { |
| 1424 | int err; |
| 1425 | |
| 1426 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1427 | err = dsa_switch_probe(ds); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 1428 | dsa_tree_put(ds->dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1429 | mutex_unlock(&dsa2_mutex); |
| 1430 | |
| 1431 | return err; |
| 1432 | } |
| 1433 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 1434 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1435 | static void dsa_switch_remove(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1436 | { |
| 1437 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1438 | |
Florian Fainelli | c058f6d | 2019-11-02 20:13:26 -0700 | [diff] [blame] | 1439 | dsa_tree_teardown(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1440 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1441 | dsa_tree_put(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 1445 | { |
| 1446 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1447 | dsa_switch_remove(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1448 | mutex_unlock(&dsa2_mutex); |
| 1449 | } |
| 1450 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |