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 | |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 24 | /* Track the bridges with forwarding offload enabled */ |
| 25 | static unsigned long dsa_fwd_offloading_bridges; |
| 26 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 27 | /** |
Vladimir Oltean | 886f8e2 | 2021-01-29 03:00:04 +0200 | [diff] [blame] | 28 | * dsa_tree_notify - Execute code for all switches in a DSA switch tree. |
| 29 | * @dst: collection of struct dsa_switch devices to notify. |
| 30 | * @e: event, must be of type DSA_NOTIFIER_* |
| 31 | * @v: event-specific value. |
| 32 | * |
| 33 | * Given a struct dsa_switch_tree, this can be used to run a function once for |
| 34 | * each member DSA switch. The other alternative of traversing the tree is only |
| 35 | * through its ports list, which does not uniquely list the switches. |
| 36 | */ |
| 37 | int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v) |
| 38 | { |
| 39 | struct raw_notifier_head *nh = &dst->nh; |
| 40 | int err; |
| 41 | |
| 42 | err = raw_notifier_call_chain(nh, e, v); |
| 43 | |
| 44 | return notifier_to_errno(err); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * dsa_broadcast - Notify all DSA trees in the system. |
| 49 | * @e: event, must be of type DSA_NOTIFIER_* |
| 50 | * @v: event-specific value. |
| 51 | * |
| 52 | * Can be used to notify the switching fabric of events such as cross-chip |
| 53 | * bridging between disjoint trees (such as islands of tagger-compatible |
| 54 | * switches bridged by an incompatible middle switch). |
Vladimir Oltean | 724395f | 2021-08-11 16:46:06 +0300 | [diff] [blame] | 55 | * |
| 56 | * WARNING: this function is not reliable during probe time, because probing |
| 57 | * between trees is asynchronous and not all DSA trees might have probed. |
Vladimir Oltean | 886f8e2 | 2021-01-29 03:00:04 +0200 | [diff] [blame] | 58 | */ |
| 59 | int dsa_broadcast(unsigned long e, void *v) |
| 60 | { |
| 61 | struct dsa_switch_tree *dst; |
| 62 | int err = 0; |
| 63 | |
| 64 | list_for_each_entry(dst, &dsa_tree_list, list) { |
| 65 | err = dsa_tree_notify(dst, e, v); |
| 66 | if (err) |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | return err; |
| 71 | } |
| 72 | |
| 73 | /** |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 74 | * dsa_lag_map() - Map LAG netdev to a linear LAG ID |
| 75 | * @dst: Tree in which to record the mapping. |
| 76 | * @lag: Netdev that is to be mapped to an ID. |
| 77 | * |
| 78 | * dsa_lag_id/dsa_lag_dev can then be used to translate between the |
| 79 | * two spaces. The size of the mapping space is determined by the |
| 80 | * driver by setting ds->num_lag_ids. It is perfectly legal to leave |
| 81 | * it unset if it is not needed, in which case these functions become |
| 82 | * no-ops. |
| 83 | */ |
| 84 | void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag) |
| 85 | { |
| 86 | unsigned int id; |
| 87 | |
| 88 | if (dsa_lag_id(dst, lag) >= 0) |
| 89 | /* Already mapped */ |
| 90 | return; |
| 91 | |
| 92 | for (id = 0; id < dst->lags_len; id++) { |
| 93 | if (!dsa_lag_dev(dst, id)) { |
| 94 | dst->lags[id] = lag; |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* No IDs left, which is OK. Some drivers do not need it. The |
| 100 | * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id |
| 101 | * returns an error for this device when joining the LAG. The |
| 102 | * driver can then return -EOPNOTSUPP back to DSA, which will |
| 103 | * fall back to a software LAG. |
| 104 | */ |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * dsa_lag_unmap() - Remove a LAG ID mapping |
| 109 | * @dst: Tree in which the mapping is recorded. |
| 110 | * @lag: Netdev that was mapped. |
| 111 | * |
| 112 | * As there may be multiple users of the mapping, it is only removed |
| 113 | * if there are no other references to it. |
| 114 | */ |
| 115 | void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag) |
| 116 | { |
| 117 | struct dsa_port *dp; |
| 118 | unsigned int id; |
| 119 | |
| 120 | dsa_lag_foreach_port(dp, dst, lag) |
| 121 | /* There are remaining users of this mapping */ |
| 122 | return; |
| 123 | |
| 124 | dsa_lags_foreach_id(id, dst) { |
| 125 | if (dsa_lag_dev(dst, id) == lag) { |
| 126 | dst->lags[id] = NULL; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 132 | struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst, |
| 133 | const struct net_device *br) |
| 134 | { |
| 135 | struct dsa_port *dp; |
| 136 | |
| 137 | list_for_each_entry(dp, &dst->ports, list) |
| 138 | if (dsa_port_bridge_dev_get(dp) == br) |
| 139 | return dp->bridge; |
| 140 | |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 144 | static int dsa_bridge_num_find(const struct net_device *bridge_dev) |
| 145 | { |
| 146 | struct dsa_switch_tree *dst; |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 147 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 148 | list_for_each_entry(dst, &dsa_tree_list, list) { |
| 149 | struct dsa_bridge *bridge; |
| 150 | |
| 151 | bridge = dsa_tree_bridge_find(dst, bridge_dev); |
| 152 | if (bridge) |
| 153 | return bridge->num; |
| 154 | } |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 155 | |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 156 | return 0; |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 157 | } |
| 158 | |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 159 | unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max) |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 160 | { |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 161 | unsigned int bridge_num = dsa_bridge_num_find(bridge_dev); |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 162 | |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 163 | /* Switches without FDB isolation support don't get unique |
| 164 | * bridge numbering |
| 165 | */ |
| 166 | if (!max) |
| 167 | return 0; |
| 168 | |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 169 | if (!bridge_num) { |
Vladimir Oltean | 947c874 | 2021-12-06 18:57:48 +0200 | [diff] [blame] | 170 | /* First port that requests FDB isolation or TX forwarding |
| 171 | * offload for this bridge |
| 172 | */ |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 173 | bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges, |
| 174 | DSA_MAX_NUM_OFFLOADING_BRIDGES, |
| 175 | 1); |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 176 | if (bridge_num >= max) |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 177 | return 0; |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 178 | |
| 179 | set_bit(bridge_num, &dsa_fwd_offloading_bridges); |
| 180 | } |
| 181 | |
| 182 | return bridge_num; |
| 183 | } |
| 184 | |
Vladimir Oltean | 3f9bb03 | 2021-12-06 18:57:47 +0200 | [diff] [blame] | 185 | void dsa_bridge_num_put(const struct net_device *bridge_dev, |
| 186 | unsigned int bridge_num) |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 187 | { |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 188 | /* Since we refcount bridges, we know that when we call this function |
| 189 | * it is no longer in use, so we can just go ahead and remove it from |
| 190 | * the bit mask. |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 191 | */ |
Vladimir Oltean | d3eed0e | 2021-12-06 18:57:56 +0200 | [diff] [blame] | 192 | clear_bit(bridge_num, &dsa_fwd_offloading_bridges); |
Vladimir Oltean | f5e165e | 2021-08-19 20:55:00 +0300 | [diff] [blame] | 193 | } |
| 194 | |
Vladimir Oltean | 3b7bc1f | 2020-05-10 19:37:42 +0300 | [diff] [blame] | 195 | struct dsa_switch *dsa_switch_find(int tree_index, int sw_index) |
| 196 | { |
| 197 | struct dsa_switch_tree *dst; |
| 198 | struct dsa_port *dp; |
| 199 | |
| 200 | list_for_each_entry(dst, &dsa_tree_list, list) { |
| 201 | if (dst->index != tree_index) |
| 202 | continue; |
| 203 | |
| 204 | list_for_each_entry(dp, &dst->ports, list) { |
| 205 | if (dp->ds->index != sw_index) |
| 206 | continue; |
| 207 | |
| 208 | return dp->ds; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return NULL; |
| 213 | } |
| 214 | EXPORT_SYMBOL_GPL(dsa_switch_find); |
| 215 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 216 | static struct dsa_switch_tree *dsa_tree_find(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 217 | { |
| 218 | struct dsa_switch_tree *dst; |
| 219 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 220 | list_for_each_entry(dst, &dsa_tree_list, list) |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 221 | if (dst->index == index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 222 | return dst; |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 223 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
| 226 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 227 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 228 | { |
| 229 | struct dsa_switch_tree *dst; |
| 230 | |
| 231 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 232 | if (!dst) |
| 233 | return NULL; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 234 | |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 235 | dst->index = index; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 236 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 237 | INIT_LIST_HEAD(&dst->rtable); |
| 238 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 239 | INIT_LIST_HEAD(&dst->ports); |
| 240 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 241 | INIT_LIST_HEAD(&dst->list); |
Vivien Didelot | 50c7d2ba | 2019-10-18 17:02:46 -0400 | [diff] [blame] | 242 | list_add_tail(&dst->list, &dsa_tree_list); |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 243 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 244 | kref_init(&dst->refcount); |
| 245 | |
| 246 | return dst; |
| 247 | } |
| 248 | |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 249 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
| 250 | { |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 251 | if (dst->tag_ops) |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 252 | dsa_tag_driver_put(dst->tag_ops); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 253 | list_del(&dst->list); |
| 254 | kfree(dst); |
| 255 | } |
| 256 | |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 257 | static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst) |
| 258 | { |
| 259 | if (dst) |
| 260 | kref_get(&dst->refcount); |
| 261 | |
| 262 | return dst; |
| 263 | } |
| 264 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 265 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
| 266 | { |
| 267 | struct dsa_switch_tree *dst; |
| 268 | |
| 269 | dst = dsa_tree_find(index); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 270 | if (dst) |
| 271 | return dsa_tree_get(dst); |
| 272 | else |
| 273 | return dsa_tree_alloc(index); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void dsa_tree_release(struct kref *ref) |
| 277 | { |
| 278 | struct dsa_switch_tree *dst; |
| 279 | |
| 280 | dst = container_of(ref, struct dsa_switch_tree, refcount); |
| 281 | |
| 282 | dsa_tree_free(dst); |
| 283 | } |
| 284 | |
| 285 | static void dsa_tree_put(struct dsa_switch_tree *dst) |
| 286 | { |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 287 | if (dst) |
| 288 | kref_put(&dst->refcount, dsa_tree_release); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 291 | static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst, |
| 292 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 293 | { |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 294 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 295 | |
Vivien Didelot | 764b7e6 | 2019-10-21 16:51:21 -0400 | [diff] [blame] | 296 | list_for_each_entry(dp, &dst->ports, list) |
| 297 | if (dp->dn == dn) |
| 298 | return dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Ben Dooks (Codethink) | 4e2ce6e | 2019-12-17 11:20:38 +0000 | [diff] [blame] | 303 | static struct dsa_link *dsa_link_touch(struct dsa_port *dp, |
| 304 | struct dsa_port *link_dp) |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 305 | { |
| 306 | struct dsa_switch *ds = dp->ds; |
| 307 | struct dsa_switch_tree *dst; |
| 308 | struct dsa_link *dl; |
| 309 | |
| 310 | dst = ds->dst; |
| 311 | |
| 312 | list_for_each_entry(dl, &dst->rtable, list) |
| 313 | if (dl->dp == dp && dl->link_dp == link_dp) |
| 314 | return dl; |
| 315 | |
| 316 | dl = kzalloc(sizeof(*dl), GFP_KERNEL); |
| 317 | if (!dl) |
| 318 | return NULL; |
| 319 | |
| 320 | dl->dp = dp; |
| 321 | dl->link_dp = link_dp; |
| 322 | |
| 323 | INIT_LIST_HEAD(&dl->list); |
| 324 | list_add_tail(&dl->list, &dst->rtable); |
| 325 | |
| 326 | return dl; |
| 327 | } |
| 328 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 329 | static bool dsa_port_setup_routing_table(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 330 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 331 | struct dsa_switch *ds = dp->ds; |
| 332 | struct dsa_switch_tree *dst = ds->dst; |
| 333 | struct device_node *dn = dp->dn; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 334 | struct of_phandle_iterator it; |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 335 | struct dsa_port *link_dp; |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 336 | struct dsa_link *dl; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 337 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 338 | |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 339 | of_for_each_phandle(&it, err, dn, "link", NULL, 0) { |
| 340 | link_dp = dsa_tree_find_port_by_node(dst, it.node); |
| 341 | if (!link_dp) { |
| 342 | of_node_put(it.node); |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 343 | return false; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 344 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 345 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 346 | dl = dsa_link_touch(dp, link_dp); |
| 347 | if (!dl) { |
| 348 | of_node_put(it.node); |
| 349 | return false; |
| 350 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 353 | return true; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 356 | static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 357 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 358 | bool complete = true; |
| 359 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 360 | |
Vivien Didelot | 86bfb2c | 2019-10-21 16:51:20 -0400 | [diff] [blame] | 361 | list_for_each_entry(dp, &dst->ports, list) { |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 362 | if (dsa_port_is_dsa(dp)) { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 363 | complete = dsa_port_setup_routing_table(dp); |
| 364 | if (!complete) |
| 365 | break; |
| 366 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 369 | return complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 372 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
| 373 | { |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 374 | struct dsa_port *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 375 | |
Vivien Didelot | c0b7362 | 2019-10-21 16:51:23 -0400 | [diff] [blame] | 376 | list_for_each_entry(dp, &dst->ports, list) |
| 377 | if (dsa_port_is_cpu(dp)) |
| 378 | return dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 379 | |
| 380 | return NULL; |
| 381 | } |
| 382 | |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 383 | /* Assign the default CPU port (the first one in the tree) to all ports of the |
| 384 | * fabric which don't already have one as part of their own switch. |
| 385 | */ |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 386 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) |
| 387 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 388 | struct dsa_port *cpu_dp, *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 389 | |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 390 | cpu_dp = dsa_tree_find_first_cpu(dst); |
| 391 | if (!cpu_dp) { |
| 392 | pr_err("DSA: tree %d has no CPU port\n", dst->index); |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 393 | return -EINVAL; |
| 394 | } |
| 395 | |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 396 | list_for_each_entry(dp, &dst->ports, list) { |
| 397 | if (dp->cpu_dp) |
| 398 | continue; |
| 399 | |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 400 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 401 | dp->cpu_dp = cpu_dp; |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 402 | } |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 407 | /* Perform initial assignment of CPU ports to user ports and DSA links in the |
| 408 | * fabric, giving preference to CPU ports local to each switch. Default to |
| 409 | * using the first CPU port in the switch tree if the port does not have a CPU |
| 410 | * port local to this switch. |
| 411 | */ |
| 412 | static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst) |
| 413 | { |
| 414 | struct dsa_port *cpu_dp, *dp; |
| 415 | |
| 416 | list_for_each_entry(cpu_dp, &dst->ports, list) { |
| 417 | if (!dsa_port_is_cpu(cpu_dp)) |
| 418 | continue; |
| 419 | |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 420 | /* Prefer a local CPU port */ |
| 421 | dsa_switch_for_each_port(dp, cpu_dp->ds) { |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 422 | /* Prefer the first local CPU port found */ |
| 423 | if (dp->cpu_dp) |
| 424 | continue; |
| 425 | |
| 426 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 427 | dp->cpu_dp = cpu_dp; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return dsa_tree_setup_default_cpu(dst); |
| 432 | } |
| 433 | |
Vladimir Oltean | 0e8eb9a | 2021-08-04 16:54:29 +0300 | [diff] [blame] | 434 | static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst) |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 435 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 436 | struct dsa_port *dp; |
| 437 | |
| 438 | list_for_each_entry(dp, &dst->ports, list) |
| 439 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 440 | dp->cpu_dp = NULL; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 443 | static int dsa_port_setup(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 444 | { |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 445 | struct devlink_port *dlp = &dp->devlink_port; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 446 | bool dsa_port_link_registered = false; |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 447 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 448 | bool dsa_port_enabled = false; |
| 449 | int err = 0; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 450 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 451 | if (dp->setup) |
| 452 | return 0; |
| 453 | |
Vladimir Oltean | 338a3a47 | 2021-10-24 20:17:54 +0300 | [diff] [blame] | 454 | mutex_init(&dp->addr_lists_lock); |
Vladimir Oltean | 3f6e32f | 2021-06-29 17:06:52 +0300 | [diff] [blame] | 455 | INIT_LIST_HEAD(&dp->fdbs); |
Vladimir Oltean | 161ca59 | 2021-06-29 17:06:50 +0300 | [diff] [blame] | 456 | INIT_LIST_HEAD(&dp->mdbs); |
| 457 | |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 458 | if (ds->ops->port_setup) { |
| 459 | err = ds->ops->port_setup(ds, dp->index); |
| 460 | if (err) |
| 461 | return err; |
| 462 | } |
| 463 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 464 | switch (dp->type) { |
| 465 | case DSA_PORT_TYPE_UNUSED: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 466 | dsa_port_disable(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 467 | break; |
| 468 | case DSA_PORT_TYPE_CPU: |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 469 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 470 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 471 | break; |
| 472 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 473 | |
| 474 | err = dsa_port_enable(dp, NULL); |
| 475 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 476 | break; |
| 477 | dsa_port_enabled = true; |
| 478 | |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 479 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 480 | case DSA_PORT_TYPE_DSA: |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 481 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 482 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 483 | break; |
| 484 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 485 | |
| 486 | err = dsa_port_enable(dp, NULL); |
| 487 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 488 | break; |
| 489 | dsa_port_enabled = true; |
| 490 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 491 | break; |
| 492 | case DSA_PORT_TYPE_USER: |
Michael Walle | 83216e3 | 2021-04-12 19:47:17 +0200 | [diff] [blame] | 493 | of_get_mac_address(dp->dn, dp->mac); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 494 | err = dsa_slave_create(dp); |
| 495 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 496 | break; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 497 | |
| 498 | devlink_port_type_eth_set(dlp, dp->slave); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 499 | break; |
| 500 | } |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 501 | |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 502 | if (err && dsa_port_enabled) |
| 503 | dsa_port_disable(dp); |
| 504 | if (err && dsa_port_link_registered) |
| 505 | dsa_port_link_unregister_of(dp); |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 506 | if (err) { |
| 507 | if (ds->ops->port_teardown) |
| 508 | ds->ops->port_teardown(ds, dp->index); |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 509 | return err; |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 510 | } |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 511 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 512 | dp->setup = true; |
| 513 | |
| 514 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 517 | static int dsa_port_devlink_setup(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 518 | { |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 519 | struct devlink_port *dlp = &dp->devlink_port; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 520 | struct dsa_switch_tree *dst = dp->ds->dst; |
| 521 | struct devlink_port_attrs attrs = {}; |
| 522 | struct devlink *dl = dp->ds->devlink; |
| 523 | const unsigned char *id; |
| 524 | unsigned char len; |
| 525 | int err; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 526 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 527 | id = (const unsigned char *)&dst->index; |
| 528 | len = sizeof(dst->index); |
| 529 | |
| 530 | attrs.phys.port_number = dp->index; |
| 531 | memcpy(attrs.switch_id.id, id, len); |
| 532 | attrs.switch_id.id_len = len; |
| 533 | memset(dlp, 0, sizeof(*dlp)); |
| 534 | |
| 535 | switch (dp->type) { |
| 536 | case DSA_PORT_TYPE_UNUSED: |
| 537 | attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; |
| 538 | break; |
| 539 | case DSA_PORT_TYPE_CPU: |
| 540 | attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU; |
| 541 | break; |
| 542 | case DSA_PORT_TYPE_DSA: |
| 543 | attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA; |
| 544 | break; |
| 545 | case DSA_PORT_TYPE_USER: |
| 546 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | devlink_port_attrs_set(dlp, &attrs); |
| 551 | err = devlink_port_register(dl, dlp, dp->index); |
| 552 | |
| 553 | if (!err) |
| 554 | dp->devlink_port_setup = true; |
| 555 | |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | static void dsa_port_teardown(struct dsa_port *dp) |
| 560 | { |
Vladimir Oltean | 91158e1 | 2021-01-12 02:48:31 +0200 | [diff] [blame] | 561 | struct devlink_port *dlp = &dp->devlink_port; |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 562 | struct dsa_switch *ds = dp->ds; |
Vladimir Oltean | 161ca59 | 2021-06-29 17:06:50 +0300 | [diff] [blame] | 563 | struct dsa_mac_addr *a, *tmp; |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 564 | struct net_device *slave; |
Vladimir Oltean | 91158e1 | 2021-01-12 02:48:31 +0200 | [diff] [blame] | 565 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 566 | if (!dp->setup) |
| 567 | return; |
| 568 | |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 569 | if (ds->ops->port_teardown) |
| 570 | ds->ops->port_teardown(ds, dp->index); |
| 571 | |
Vladimir Oltean | 91158e1 | 2021-01-12 02:48:31 +0200 | [diff] [blame] | 572 | devlink_port_type_clear(dlp); |
| 573 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 574 | switch (dp->type) { |
| 575 | case DSA_PORT_TYPE_UNUSED: |
| 576 | break; |
| 577 | case DSA_PORT_TYPE_CPU: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 578 | dsa_port_disable(dp); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 579 | dsa_port_link_unregister_of(dp); |
| 580 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 581 | case DSA_PORT_TYPE_DSA: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 582 | dsa_port_disable(dp); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 583 | dsa_port_link_unregister_of(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 584 | break; |
| 585 | case DSA_PORT_TYPE_USER: |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 586 | slave = dp->slave; |
| 587 | |
| 588 | if (slave) { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 589 | dp->slave = NULL; |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 590 | dsa_slave_destroy(slave); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 591 | } |
| 592 | break; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 593 | } |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 594 | |
Vladimir Oltean | 3f6e32f | 2021-06-29 17:06:52 +0300 | [diff] [blame] | 595 | list_for_each_entry_safe(a, tmp, &dp->fdbs, list) { |
| 596 | list_del(&a->list); |
| 597 | kfree(a); |
| 598 | } |
| 599 | |
Vladimir Oltean | 161ca59 | 2021-06-29 17:06:50 +0300 | [diff] [blame] | 600 | list_for_each_entry_safe(a, tmp, &dp->mdbs, list) { |
| 601 | list_del(&a->list); |
| 602 | kfree(a); |
| 603 | } |
| 604 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 605 | dp->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 608 | static void dsa_port_devlink_teardown(struct dsa_port *dp) |
| 609 | { |
| 610 | struct devlink_port *dlp = &dp->devlink_port; |
| 611 | |
| 612 | if (dp->devlink_port_setup) |
| 613 | devlink_port_unregister(dlp); |
| 614 | dp->devlink_port_setup = false; |
| 615 | } |
| 616 | |
Vladimir Oltean | fd292c1 | 2021-09-17 17:29:16 +0300 | [diff] [blame] | 617 | /* Destroy the current devlink port, and create a new one which has the UNUSED |
| 618 | * flavour. At this point, any call to ds->ops->port_setup has been already |
| 619 | * balanced out by a call to ds->ops->port_teardown, so we know that any |
| 620 | * devlink port regions the driver had are now unregistered. We then call its |
| 621 | * ds->ops->port_setup again, in order for the driver to re-create them on the |
| 622 | * new devlink port. |
| 623 | */ |
| 624 | static int dsa_port_reinit_as_unused(struct dsa_port *dp) |
| 625 | { |
| 626 | struct dsa_switch *ds = dp->ds; |
| 627 | int err; |
| 628 | |
| 629 | dsa_port_devlink_teardown(dp); |
| 630 | dp->type = DSA_PORT_TYPE_UNUSED; |
| 631 | err = dsa_port_devlink_setup(dp); |
| 632 | if (err) |
| 633 | return err; |
| 634 | |
| 635 | if (ds->ops->port_setup) { |
| 636 | /* On error, leave the devlink port registered, |
| 637 | * dsa_switch_teardown will clean it up later. |
| 638 | */ |
| 639 | err = ds->ops->port_setup(ds, dp->index); |
| 640 | if (err) |
| 641 | return err; |
| 642 | } |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Andrew Lunn | 0f06b85 | 2020-09-18 21:11:08 +0200 | [diff] [blame] | 647 | static int dsa_devlink_info_get(struct devlink *dl, |
| 648 | struct devlink_info_req *req, |
| 649 | struct netlink_ext_ack *extack) |
| 650 | { |
| 651 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 652 | |
| 653 | if (ds->ops->devlink_info_get) |
| 654 | return ds->ops->devlink_info_get(ds, req, extack); |
| 655 | |
| 656 | return -EOPNOTSUPP; |
| 657 | } |
| 658 | |
Vladimir Oltean | 2a6ef76 | 2021-01-15 04:11:13 +0200 | [diff] [blame] | 659 | static int dsa_devlink_sb_pool_get(struct devlink *dl, |
| 660 | unsigned int sb_index, u16 pool_index, |
| 661 | struct devlink_sb_pool_info *pool_info) |
| 662 | { |
| 663 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 664 | |
| 665 | if (!ds->ops->devlink_sb_pool_get) |
| 666 | return -EOPNOTSUPP; |
| 667 | |
| 668 | return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index, |
| 669 | pool_info); |
| 670 | } |
| 671 | |
| 672 | static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index, |
| 673 | u16 pool_index, u32 size, |
| 674 | enum devlink_sb_threshold_type threshold_type, |
| 675 | struct netlink_ext_ack *extack) |
| 676 | { |
| 677 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 678 | |
| 679 | if (!ds->ops->devlink_sb_pool_set) |
| 680 | return -EOPNOTSUPP; |
| 681 | |
| 682 | return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size, |
| 683 | threshold_type, extack); |
| 684 | } |
| 685 | |
| 686 | static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp, |
| 687 | unsigned int sb_index, u16 pool_index, |
| 688 | u32 *p_threshold) |
| 689 | { |
| 690 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 691 | int port = dsa_devlink_port_to_port(dlp); |
| 692 | |
| 693 | if (!ds->ops->devlink_sb_port_pool_get) |
| 694 | return -EOPNOTSUPP; |
| 695 | |
| 696 | return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index, |
| 697 | pool_index, p_threshold); |
| 698 | } |
| 699 | |
| 700 | static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp, |
| 701 | unsigned int sb_index, u16 pool_index, |
| 702 | u32 threshold, |
| 703 | struct netlink_ext_ack *extack) |
| 704 | { |
| 705 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 706 | int port = dsa_devlink_port_to_port(dlp); |
| 707 | |
| 708 | if (!ds->ops->devlink_sb_port_pool_set) |
| 709 | return -EOPNOTSUPP; |
| 710 | |
| 711 | return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index, |
| 712 | pool_index, threshold, extack); |
| 713 | } |
| 714 | |
| 715 | static int |
| 716 | dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp, |
| 717 | unsigned int sb_index, u16 tc_index, |
| 718 | enum devlink_sb_pool_type pool_type, |
| 719 | u16 *p_pool_index, u32 *p_threshold) |
| 720 | { |
| 721 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 722 | int port = dsa_devlink_port_to_port(dlp); |
| 723 | |
| 724 | if (!ds->ops->devlink_sb_tc_pool_bind_get) |
| 725 | return -EOPNOTSUPP; |
| 726 | |
| 727 | return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index, |
| 728 | tc_index, pool_type, |
| 729 | p_pool_index, p_threshold); |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp, |
| 734 | unsigned int sb_index, u16 tc_index, |
| 735 | enum devlink_sb_pool_type pool_type, |
| 736 | u16 pool_index, u32 threshold, |
| 737 | struct netlink_ext_ack *extack) |
| 738 | { |
| 739 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 740 | int port = dsa_devlink_port_to_port(dlp); |
| 741 | |
| 742 | if (!ds->ops->devlink_sb_tc_pool_bind_set) |
| 743 | return -EOPNOTSUPP; |
| 744 | |
| 745 | return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index, |
| 746 | tc_index, pool_type, |
| 747 | pool_index, threshold, |
| 748 | extack); |
| 749 | } |
| 750 | |
| 751 | static int dsa_devlink_sb_occ_snapshot(struct devlink *dl, |
| 752 | unsigned int sb_index) |
| 753 | { |
| 754 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 755 | |
| 756 | if (!ds->ops->devlink_sb_occ_snapshot) |
| 757 | return -EOPNOTSUPP; |
| 758 | |
| 759 | return ds->ops->devlink_sb_occ_snapshot(ds, sb_index); |
| 760 | } |
| 761 | |
| 762 | static int dsa_devlink_sb_occ_max_clear(struct devlink *dl, |
| 763 | unsigned int sb_index) |
| 764 | { |
| 765 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 766 | |
| 767 | if (!ds->ops->devlink_sb_occ_max_clear) |
| 768 | return -EOPNOTSUPP; |
| 769 | |
| 770 | return ds->ops->devlink_sb_occ_max_clear(ds, sb_index); |
| 771 | } |
| 772 | |
| 773 | static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp, |
| 774 | unsigned int sb_index, |
| 775 | u16 pool_index, u32 *p_cur, |
| 776 | u32 *p_max) |
| 777 | { |
| 778 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 779 | int port = dsa_devlink_port_to_port(dlp); |
| 780 | |
| 781 | if (!ds->ops->devlink_sb_occ_port_pool_get) |
| 782 | return -EOPNOTSUPP; |
| 783 | |
| 784 | return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index, |
| 785 | pool_index, p_cur, p_max); |
| 786 | } |
| 787 | |
| 788 | static int |
| 789 | dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp, |
| 790 | unsigned int sb_index, u16 tc_index, |
| 791 | enum devlink_sb_pool_type pool_type, |
| 792 | u32 *p_cur, u32 *p_max) |
| 793 | { |
| 794 | struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp); |
| 795 | int port = dsa_devlink_port_to_port(dlp); |
| 796 | |
| 797 | if (!ds->ops->devlink_sb_occ_tc_port_bind_get) |
| 798 | return -EOPNOTSUPP; |
| 799 | |
| 800 | return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port, |
| 801 | sb_index, tc_index, |
| 802 | pool_type, p_cur, |
| 803 | p_max); |
| 804 | } |
| 805 | |
Andrew Lunn | 0f06b85 | 2020-09-18 21:11:08 +0200 | [diff] [blame] | 806 | static const struct devlink_ops dsa_devlink_ops = { |
Vladimir Oltean | 2a6ef76 | 2021-01-15 04:11:13 +0200 | [diff] [blame] | 807 | .info_get = dsa_devlink_info_get, |
| 808 | .sb_pool_get = dsa_devlink_sb_pool_get, |
| 809 | .sb_pool_set = dsa_devlink_sb_pool_set, |
| 810 | .sb_port_pool_get = dsa_devlink_sb_port_pool_get, |
| 811 | .sb_port_pool_set = dsa_devlink_sb_port_pool_set, |
| 812 | .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get, |
| 813 | .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set, |
| 814 | .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot, |
| 815 | .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear, |
| 816 | .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get, |
| 817 | .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] | 818 | }; |
| 819 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 820 | static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds) |
| 821 | { |
| 822 | const struct dsa_device_ops *tag_ops = ds->dst->tag_ops; |
| 823 | struct dsa_switch_tree *dst = ds->dst; |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 824 | struct dsa_port *cpu_dp; |
| 825 | int err; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 826 | |
| 827 | if (tag_ops->proto == dst->default_proto) |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 828 | goto connect; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 829 | |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 830 | dsa_switch_for_each_cpu_port(cpu_dp, ds) { |
Vladimir Oltean | 1951b3f | 2021-10-09 15:26:07 +0300 | [diff] [blame] | 831 | rtnl_lock(); |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 832 | err = ds->ops->change_tag_protocol(ds, cpu_dp->index, |
| 833 | tag_ops->proto); |
Vladimir Oltean | 1951b3f | 2021-10-09 15:26:07 +0300 | [diff] [blame] | 834 | rtnl_unlock(); |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 835 | if (err) { |
| 836 | dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n", |
| 837 | tag_ops->name, ERR_PTR(err)); |
| 838 | return err; |
| 839 | } |
| 840 | } |
| 841 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 842 | connect: |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 843 | if (tag_ops->connect) { |
| 844 | err = tag_ops->connect(ds); |
| 845 | if (err) |
| 846 | return err; |
| 847 | } |
| 848 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 849 | if (ds->ops->connect_tag_protocol) { |
| 850 | err = ds->ops->connect_tag_protocol(ds, tag_ops->proto); |
| 851 | if (err) { |
| 852 | dev_err(ds->dev, |
| 853 | "Unable to connect to tag protocol \"%s\": %pe\n", |
| 854 | tag_ops->name, ERR_PTR(err)); |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 855 | goto disconnect; |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 859 | return 0; |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 860 | |
| 861 | disconnect: |
| 862 | if (tag_ops->disconnect) |
| 863 | tag_ops->disconnect(ds); |
| 864 | |
| 865 | return err; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 868 | static int dsa_switch_setup(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 869 | { |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 870 | struct dsa_devlink_priv *dl_priv; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 871 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 872 | int err; |
| 873 | |
| 874 | if (ds->setup) |
| 875 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 876 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 877 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 878 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 879 | * the slave MDIO bus driver rely on these values for probing PHY |
| 880 | * devices or not |
| 881 | */ |
Vivien Didelot | 02bc6e5 | 2017-10-26 11:22:56 -0400 | [diff] [blame] | 882 | ds->phys_mii_mask |= dsa_user_ports(ds); |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 883 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 884 | /* Add the switch to devlink before calling setup, so that setup can |
| 885 | * add dpipe tables |
| 886 | */ |
Leon Romanovsky | 919d13a | 2021-08-08 21:57:43 +0300 | [diff] [blame] | 887 | ds->devlink = |
| 888 | devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 889 | if (!ds->devlink) |
| 890 | return -ENOMEM; |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 891 | dl_priv = devlink_priv(ds->devlink); |
| 892 | dl_priv->ds = ds; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 893 | |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 894 | /* Setup devlink port instances now, so that the switch |
| 895 | * setup() can register regions etc, against the ports |
| 896 | */ |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 897 | dsa_switch_for_each_port(dp, ds) { |
| 898 | err = dsa_port_devlink_setup(dp); |
| 899 | if (err) |
| 900 | goto unregister_devlink_ports; |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 903 | err = dsa_switch_register_notifier(ds); |
| 904 | if (err) |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 905 | goto unregister_devlink_ports; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 906 | |
Vladimir Oltean | 0ee2af4 | 2021-01-16 01:19:19 +0200 | [diff] [blame] | 907 | ds->configure_vlan_while_not_filtering = true; |
| 908 | |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 909 | err = ds->ops->setup(ds); |
| 910 | if (err < 0) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 911 | goto unregister_notifier; |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 912 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 913 | err = dsa_switch_setup_tag_protocol(ds); |
| 914 | if (err) |
| 915 | goto teardown; |
| 916 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 917 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Vladimir Oltean | 5135e96 | 2021-09-21 00:42:08 +0300 | [diff] [blame] | 918 | ds->slave_mii_bus = mdiobus_alloc(); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 919 | if (!ds->slave_mii_bus) { |
| 920 | err = -ENOMEM; |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 921 | goto teardown; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 922 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 923 | |
| 924 | dsa_slave_mii_bus_init(ds); |
| 925 | |
| 926 | err = mdiobus_register(ds->slave_mii_bus); |
| 927 | if (err < 0) |
Vladimir Oltean | 5135e96 | 2021-09-21 00:42:08 +0300 | [diff] [blame] | 928 | goto free_slave_mii_bus; |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 931 | ds->setup = true; |
Leon Romanovsky | bd936bd | 2021-09-25 14:23:01 +0300 | [diff] [blame] | 932 | devlink_register(ds->devlink); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 933 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 934 | |
Vladimir Oltean | 5135e96 | 2021-09-21 00:42:08 +0300 | [diff] [blame] | 935 | free_slave_mii_bus: |
| 936 | if (ds->slave_mii_bus && ds->ops->phy_read) |
| 937 | mdiobus_free(ds->slave_mii_bus); |
Vladimir Oltean | 8fd54a7 | 2021-02-04 18:33:51 +0200 | [diff] [blame] | 938 | teardown: |
| 939 | if (ds->ops->teardown) |
| 940 | ds->ops->teardown(ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 941 | unregister_notifier: |
| 942 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 943 | unregister_devlink_ports: |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 944 | dsa_switch_for_each_port(dp, ds) |
| 945 | dsa_port_devlink_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 946 | devlink_free(ds->devlink); |
| 947 | ds->devlink = NULL; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 948 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 951 | static void dsa_switch_teardown(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 952 | { |
Andrew Lunn | 3122433 | 2020-10-04 18:12:53 +0200 | [diff] [blame] | 953 | struct dsa_port *dp; |
| 954 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 955 | if (!ds->setup) |
| 956 | return; |
| 957 | |
Leon Romanovsky | bd936bd | 2021-09-25 14:23:01 +0300 | [diff] [blame] | 958 | if (ds->devlink) |
| 959 | devlink_unregister(ds->devlink); |
| 960 | |
Vladimir Oltean | 5135e96 | 2021-09-21 00:42:08 +0300 | [diff] [blame] | 961 | if (ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 962 | mdiobus_unregister(ds->slave_mii_bus); |
Vladimir Oltean | 5135e96 | 2021-09-21 00:42:08 +0300 | [diff] [blame] | 963 | mdiobus_free(ds->slave_mii_bus); |
| 964 | ds->slave_mii_bus = NULL; |
| 965 | } |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 966 | |
Vladimir Oltean | 5e3f847 | 2019-06-08 15:04:28 +0300 | [diff] [blame] | 967 | if (ds->ops->teardown) |
| 968 | ds->ops->teardown(ds); |
| 969 | |
Vladimir Oltean | 39e222b | 2021-10-12 15:37:35 +0300 | [diff] [blame] | 970 | dsa_switch_unregister_notifier(ds); |
| 971 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 972 | if (ds->devlink) { |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 973 | dsa_switch_for_each_port(dp, ds) |
| 974 | dsa_port_devlink_teardown(dp); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 975 | devlink_free(ds->devlink); |
| 976 | ds->devlink = NULL; |
| 977 | } |
| 978 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 979 | ds->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Vladimir Oltean | a57d8c21 | 2021-09-14 16:47:26 +0300 | [diff] [blame] | 982 | /* First tear down the non-shared, then the shared ports. This ensures that |
| 983 | * all work items scheduled by our switchdev handlers for user ports have |
| 984 | * completed before we destroy the refcounting kept on the shared ports. |
| 985 | */ |
| 986 | static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst) |
| 987 | { |
| 988 | struct dsa_port *dp; |
| 989 | |
| 990 | list_for_each_entry(dp, &dst->ports, list) |
| 991 | if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) |
| 992 | dsa_port_teardown(dp); |
| 993 | |
| 994 | dsa_flush_workqueue(); |
| 995 | |
| 996 | list_for_each_entry(dp, &dst->ports, list) |
| 997 | if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) |
| 998 | dsa_port_teardown(dp); |
| 999 | } |
| 1000 | |
| 1001 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) |
| 1002 | { |
| 1003 | struct dsa_port *dp; |
| 1004 | |
| 1005 | list_for_each_entry(dp, &dst->ports, list) |
| 1006 | dsa_switch_teardown(dp->ds); |
| 1007 | } |
| 1008 | |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1009 | /* Bring shared ports up first, then non-shared ports */ |
| 1010 | static int dsa_tree_setup_ports(struct dsa_switch_tree *dst) |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1011 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 1012 | struct dsa_port *dp; |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1013 | int err = 0; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1014 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 1015 | list_for_each_entry(dp, &dst->ports, list) { |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1016 | if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) { |
| 1017 | err = dsa_port_setup(dp); |
| 1018 | if (err) |
| 1019 | goto teardown; |
| 1020 | } |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 1021 | } |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 1022 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 1023 | list_for_each_entry(dp, &dst->ports, list) { |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1024 | if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) { |
| 1025 | err = dsa_port_setup(dp); |
| 1026 | if (err) { |
| 1027 | err = dsa_port_reinit_as_unused(dp); |
| 1028 | if (err) |
| 1029 | goto teardown; |
| 1030 | } |
Maxim Kochetkov | fb6ec87 | 2021-03-29 18:30:16 +0300 | [diff] [blame] | 1031 | } |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1035 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 1036 | teardown: |
Vladimir Oltean | a57d8c21 | 2021-09-14 16:47:26 +0300 | [diff] [blame] | 1037 | dsa_tree_teardown_ports(dst); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1038 | |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1039 | return err; |
| 1040 | } |
| 1041 | |
| 1042 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
| 1043 | { |
| 1044 | struct dsa_port *dp; |
| 1045 | int err = 0; |
| 1046 | |
| 1047 | list_for_each_entry(dp, &dst->ports, list) { |
| 1048 | err = dsa_switch_setup(dp->ds); |
| 1049 | if (err) { |
| 1050 | dsa_tree_teardown_switches(dst); |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1054 | |
| 1055 | return err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1056 | } |
| 1057 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 1058 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
| 1059 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 1060 | struct dsa_port *dp; |
| 1061 | int err; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 1062 | |
Vladimir Oltean | c146f9b | 2022-01-06 01:11:15 +0200 | [diff] [blame] | 1063 | rtnl_lock(); |
| 1064 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 1065 | list_for_each_entry(dp, &dst->ports, list) { |
| 1066 | if (dsa_port_is_cpu(dp)) { |
| 1067 | err = dsa_master_setup(dp->master, dp); |
| 1068 | if (err) |
| 1069 | return err; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Vladimir Oltean | c146f9b | 2022-01-06 01:11:15 +0200 | [diff] [blame] | 1073 | rtnl_unlock(); |
| 1074 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 1075 | return 0; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) |
| 1079 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 1080 | struct dsa_port *dp; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 1081 | |
Vladimir Oltean | c146f9b | 2022-01-06 01:11:15 +0200 | [diff] [blame] | 1082 | rtnl_lock(); |
| 1083 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 1084 | list_for_each_entry(dp, &dst->ports, list) |
| 1085 | if (dsa_port_is_cpu(dp)) |
| 1086 | dsa_master_teardown(dp->master); |
Vladimir Oltean | c146f9b | 2022-01-06 01:11:15 +0200 | [diff] [blame] | 1087 | |
| 1088 | rtnl_unlock(); |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 1091 | static int dsa_tree_setup_lags(struct dsa_switch_tree *dst) |
| 1092 | { |
| 1093 | unsigned int len = 0; |
| 1094 | struct dsa_port *dp; |
| 1095 | |
| 1096 | list_for_each_entry(dp, &dst->ports, list) { |
| 1097 | if (dp->ds->num_lag_ids > len) |
| 1098 | len = dp->ds->num_lag_ids; |
| 1099 | } |
| 1100 | |
| 1101 | if (!len) |
| 1102 | return 0; |
| 1103 | |
| 1104 | dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL); |
| 1105 | if (!dst->lags) |
| 1106 | return -ENOMEM; |
| 1107 | |
| 1108 | dst->lags_len = len; |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst) |
| 1113 | { |
| 1114 | kfree(dst->lags); |
| 1115 | } |
| 1116 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1117 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1118 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 1119 | bool complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1120 | int err; |
| 1121 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1122 | if (dst->setup) { |
| 1123 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", |
| 1124 | dst->index); |
| 1125 | return -EEXIST; |
| 1126 | } |
| 1127 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 1128 | complete = dsa_tree_setup_routing_table(dst); |
| 1129 | if (!complete) |
| 1130 | return 0; |
| 1131 | |
Vladimir Oltean | 2c0b032 | 2021-08-04 16:54:30 +0300 | [diff] [blame] | 1132 | err = dsa_tree_setup_cpu_ports(dst); |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 1133 | if (err) |
| 1134 | return err; |
| 1135 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1136 | err = dsa_tree_setup_switches(dst); |
| 1137 | if (err) |
Vladimir Oltean | 0e8eb9a | 2021-08-04 16:54:29 +0300 | [diff] [blame] | 1138 | goto teardown_cpu_ports; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1139 | |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1140 | err = dsa_tree_setup_master(dst); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 1141 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1142 | goto teardown_switches; |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 1143 | |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1144 | err = dsa_tree_setup_ports(dst); |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1145 | if (err) |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1146 | goto teardown_master; |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1147 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 1148 | err = dsa_tree_setup_lags(dst); |
| 1149 | if (err) |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1150 | goto teardown_ports; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 1151 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1152 | dst->setup = true; |
| 1153 | |
| 1154 | pr_info("DSA: tree %d setup\n", dst->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1155 | |
| 1156 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1157 | |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1158 | teardown_ports: |
Vladimir Oltean | e5845aa | 2021-09-21 01:49:18 +0300 | [diff] [blame] | 1159 | dsa_tree_teardown_ports(dst); |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1160 | teardown_master: |
| 1161 | dsa_tree_teardown_master(dst); |
Vladimir Oltean | 1e3f407 | 2022-01-06 01:11:16 +0200 | [diff] [blame] | 1162 | teardown_switches: |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1163 | dsa_tree_teardown_switches(dst); |
Vladimir Oltean | 0e8eb9a | 2021-08-04 16:54:29 +0300 | [diff] [blame] | 1164 | teardown_cpu_ports: |
| 1165 | dsa_tree_teardown_cpu_ports(dst); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 1166 | |
| 1167 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1170 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1171 | { |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 1172 | struct dsa_link *dl, *next; |
| 1173 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1174 | if (!dst->setup) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1175 | return; |
| 1176 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 1177 | dsa_tree_teardown_lags(dst); |
| 1178 | |
Vladimir Oltean | a57d8c21 | 2021-09-14 16:47:26 +0300 | [diff] [blame] | 1179 | dsa_tree_teardown_ports(dst); |
| 1180 | |
Vladimir Oltean | 11fd667 | 2022-01-06 01:11:17 +0200 | [diff] [blame] | 1181 | dsa_tree_teardown_master(dst); |
| 1182 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 1183 | dsa_tree_teardown_switches(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1184 | |
Vladimir Oltean | 0e8eb9a | 2021-08-04 16:54:29 +0300 | [diff] [blame] | 1185 | dsa_tree_teardown_cpu_ports(dst); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 1186 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 1187 | list_for_each_entry_safe(dl, next, &dst->rtable, list) { |
| 1188 | list_del(&dl->list); |
| 1189 | kfree(dl); |
| 1190 | } |
| 1191 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 1192 | pr_info("DSA: tree %d torn down\n", dst->index); |
| 1193 | |
| 1194 | dst->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1197 | static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst, |
| 1198 | const struct dsa_device_ops *tag_ops) |
| 1199 | { |
| 1200 | const struct dsa_device_ops *old_tag_ops = dst->tag_ops; |
| 1201 | struct dsa_notifier_tag_proto_info info; |
| 1202 | int err; |
| 1203 | |
| 1204 | dst->tag_ops = tag_ops; |
| 1205 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1206 | /* Notify the switches from this tree about the connection |
| 1207 | * to the new tagger |
| 1208 | */ |
| 1209 | info.tag_ops = tag_ops; |
| 1210 | err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info); |
| 1211 | if (err && err != -EOPNOTSUPP) |
| 1212 | goto out_disconnect; |
| 1213 | |
| 1214 | /* Notify the old tagger about the disconnection from this tree */ |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 1215 | info.tag_ops = old_tag_ops; |
| 1216 | dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info); |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1217 | |
| 1218 | return 0; |
| 1219 | |
| 1220 | out_disconnect: |
Vladimir Oltean | 7f29731 | 2021-12-14 03:45:36 +0200 | [diff] [blame] | 1221 | info.tag_ops = tag_ops; |
| 1222 | dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info); |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1223 | dst->tag_ops = old_tag_ops; |
| 1224 | |
| 1225 | return err; |
| 1226 | } |
| 1227 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1228 | /* Since the dsa/tagging sysfs device attribute is per master, the assumption |
| 1229 | * is that all DSA switches within a tree share the same tagger, otherwise |
| 1230 | * they would have formed disjoint trees (different "dsa,member" values). |
| 1231 | */ |
| 1232 | int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst, |
| 1233 | struct net_device *master, |
| 1234 | const struct dsa_device_ops *tag_ops, |
| 1235 | const struct dsa_device_ops *old_tag_ops) |
| 1236 | { |
| 1237 | struct dsa_notifier_tag_proto_info info; |
| 1238 | struct dsa_port *dp; |
| 1239 | int err = -EBUSY; |
| 1240 | |
| 1241 | if (!rtnl_trylock()) |
| 1242 | return restart_syscall(); |
| 1243 | |
| 1244 | /* At the moment we don't allow changing the tag protocol under |
| 1245 | * traffic. The rtnl_mutex also happens to serialize concurrent |
| 1246 | * attempts to change the tagging protocol. If we ever lift the IFF_UP |
| 1247 | * restriction, there needs to be another mutex which serializes this. |
| 1248 | */ |
| 1249 | if (master->flags & IFF_UP) |
| 1250 | goto out_unlock; |
| 1251 | |
| 1252 | list_for_each_entry(dp, &dst->ports, list) { |
Vladimir Oltean | d0004a0 | 2021-10-20 20:49:50 +0300 | [diff] [blame] | 1253 | if (!dsa_port_is_user(dp)) |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1254 | continue; |
| 1255 | |
| 1256 | if (dp->slave->flags & IFF_UP) |
| 1257 | goto out_unlock; |
| 1258 | } |
| 1259 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1260 | /* Notify the tag protocol change */ |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1261 | info.tag_ops = tag_ops; |
| 1262 | err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info); |
| 1263 | if (err) |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1264 | return err; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1265 | |
Vladimir Oltean | dc452a4 | 2021-12-10 01:34:37 +0200 | [diff] [blame] | 1266 | err = dsa_tree_bind_tag_proto(dst, tag_ops); |
| 1267 | if (err) |
| 1268 | goto out_unwind_tagger; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1269 | |
| 1270 | rtnl_unlock(); |
| 1271 | |
| 1272 | return 0; |
| 1273 | |
| 1274 | out_unwind_tagger: |
| 1275 | info.tag_ops = old_tag_ops; |
| 1276 | dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info); |
| 1277 | out_unlock: |
| 1278 | rtnl_unlock(); |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1282 | static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) |
| 1283 | { |
| 1284 | struct dsa_switch_tree *dst = ds->dst; |
| 1285 | struct dsa_port *dp; |
| 1286 | |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 1287 | dsa_switch_for_each_port(dp, ds) |
| 1288 | if (dp->index == index) |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1289 | return dp; |
| 1290 | |
| 1291 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 1292 | if (!dp) |
| 1293 | return NULL; |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1294 | |
| 1295 | dp->ds = ds; |
| 1296 | dp->index = index; |
| 1297 | |
| 1298 | INIT_LIST_HEAD(&dp->list); |
| 1299 | list_add_tail(&dp->list, &dst->ports); |
| 1300 | |
| 1301 | return dp; |
| 1302 | } |
| 1303 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1304 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
| 1305 | { |
| 1306 | if (!name) |
| 1307 | name = "eth%d"; |
| 1308 | |
| 1309 | dp->type = DSA_PORT_TYPE_USER; |
| 1310 | dp->name = name; |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 1316 | { |
| 1317 | dp->type = DSA_PORT_TYPE_DSA; |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1322 | static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp, |
| 1323 | struct net_device *master) |
| 1324 | { |
| 1325 | enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE; |
| 1326 | struct dsa_switch *mds, *ds = dp->ds; |
| 1327 | unsigned int mdp_upstream; |
| 1328 | struct dsa_port *mdp; |
| 1329 | |
| 1330 | /* It is possible to stack DSA switches onto one another when that |
| 1331 | * happens the switch driver may want to know if its tagging protocol |
| 1332 | * is going to work in such a configuration. |
| 1333 | */ |
| 1334 | if (dsa_slave_dev_check(master)) { |
| 1335 | mdp = dsa_slave_to_port(master); |
| 1336 | mds = mdp->ds; |
| 1337 | mdp_upstream = dsa_upstream_port(mds, mdp->index); |
| 1338 | tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream, |
| 1339 | DSA_TAG_PROTO_NONE); |
| 1340 | } |
| 1341 | |
| 1342 | /* If the master device is not itself a DSA slave in a disjoint DSA |
| 1343 | * tree, then return immediately. |
| 1344 | */ |
| 1345 | return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol); |
| 1346 | } |
| 1347 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1348 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master, |
| 1349 | const char *user_protocol) |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1350 | { |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1351 | struct dsa_switch *ds = dp->ds; |
| 1352 | struct dsa_switch_tree *dst = ds->dst; |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1353 | const struct dsa_device_ops *tag_ops; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1354 | enum dsa_tag_protocol default_proto; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1355 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1356 | /* Find out which protocol the switch would prefer. */ |
| 1357 | default_proto = dsa_get_tag_protocol(dp, master); |
| 1358 | if (dst->default_proto) { |
| 1359 | if (dst->default_proto != default_proto) { |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1360 | dev_err(ds->dev, |
| 1361 | "A DSA switch tree can have only one tagging protocol\n"); |
| 1362 | return -EINVAL; |
| 1363 | } |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1364 | } else { |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1365 | dst->default_proto = default_proto; |
| 1366 | } |
| 1367 | |
| 1368 | /* See if the user wants to override that preference. */ |
| 1369 | if (user_protocol) { |
| 1370 | if (!ds->ops->change_tag_protocol) { |
| 1371 | dev_err(ds->dev, "Tag protocol cannot be modified\n"); |
| 1372 | return -EINVAL; |
Vladimir Oltean | 357f203 | 2021-01-29 03:00:05 +0200 | [diff] [blame] | 1373 | } |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1374 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1375 | tag_ops = dsa_find_tagger_by_name(user_protocol); |
| 1376 | } else { |
| 1377 | tag_ops = dsa_tag_driver_get(default_proto); |
| 1378 | } |
| 1379 | |
| 1380 | if (IS_ERR(tag_ops)) { |
| 1381 | if (PTR_ERR(tag_ops) == -ENOPROTOOPT) |
| 1382 | return -EPROBE_DEFER; |
| 1383 | |
| 1384 | dev_warn(ds->dev, "No tagger for this switch\n"); |
| 1385 | return PTR_ERR(tag_ops); |
| 1386 | } |
| 1387 | |
| 1388 | if (dst->tag_ops) { |
| 1389 | if (dst->tag_ops != tag_ops) { |
| 1390 | dev_err(ds->dev, |
| 1391 | "A DSA switch tree can have only one tagging protocol\n"); |
| 1392 | |
| 1393 | dsa_tag_driver_put(tag_ops); |
| 1394 | return -EINVAL; |
| 1395 | } |
| 1396 | |
| 1397 | /* In the case of multiple CPU ports per switch, the tagging |
| 1398 | * protocol is still reference-counted only per switch tree. |
| 1399 | */ |
| 1400 | dsa_tag_driver_put(tag_ops); |
| 1401 | } else { |
George McCollister | e0c755a | 2021-03-22 15:26:50 -0500 | [diff] [blame] | 1402 | dst->tag_ops = tag_ops; |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 1405 | dp->master = master; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1406 | dp->type = DSA_PORT_TYPE_CPU; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 1407 | dsa_port_set_tag_protocol(dp, dst->tag_ops); |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1408 | dp->dst = dst; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 1409 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1410 | /* At this point, the tree may be configured to use a different |
| 1411 | * tagger than the one chosen by the switch driver during |
| 1412 | * .setup, in the case when a user selects a custom protocol |
| 1413 | * through the DT. |
| 1414 | * |
| 1415 | * This is resolved by syncing the driver with the tree in |
| 1416 | * dsa_switch_setup_tag_protocol once .setup has run and the |
| 1417 | * driver is ready to accept calls to .change_tag_protocol. If |
| 1418 | * the driver does not support the custom protocol at that |
| 1419 | * point, the tree is wholly rejected, thereby ensuring that the |
| 1420 | * tree and driver are always in agreement on the protocol to |
| 1421 | * use. |
| 1422 | */ |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1426 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 1427 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1428 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
Vivien Didelot | 1838fa8 | 2017-10-27 15:55:18 -0400 | [diff] [blame] | 1429 | const char *name = of_get_property(dn, "label", NULL); |
Vivien Didelot | 54df6fa | 2017-11-03 19:05:28 -0400 | [diff] [blame] | 1430 | bool link = of_property_read_bool(dn, "link"); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1431 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1432 | dp->dn = dn; |
| 1433 | |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1434 | if (ethernet) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1435 | struct net_device *master; |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1436 | const char *user_protocol; |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1437 | |
| 1438 | master = of_find_net_device_by_node(ethernet); |
| 1439 | if (!master) |
| 1440 | return -EPROBE_DEFER; |
| 1441 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1442 | user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL); |
| 1443 | return dsa_port_parse_cpu(dp, master, user_protocol); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1446 | if (link) |
| 1447 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1448 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1449 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1452 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 1453 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1454 | { |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1455 | struct device_node *ports, *port; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1456 | struct dsa_port *dp; |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1457 | int err = 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1458 | u32 reg; |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1459 | |
| 1460 | ports = of_get_child_by_name(dn, "ports"); |
| 1461 | if (!ports) { |
Kurt Kanzenbach | 85e05d2 | 2020-07-20 14:49:39 +0200 | [diff] [blame] | 1462 | /* The second possibility is "ethernet-ports" */ |
| 1463 | ports = of_get_child_by_name(dn, "ethernet-ports"); |
| 1464 | if (!ports) { |
| 1465 | dev_err(ds->dev, "no ports child node found\n"); |
| 1466 | return -EINVAL; |
| 1467 | } |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 1468 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1469 | |
| 1470 | for_each_available_child_of_node(ports, port) { |
| 1471 | err = of_property_read_u32(port, "reg", ®); |
Christophe JAILLET | ba69fd9 | 2021-10-18 21:59:00 +0200 | [diff] [blame] | 1472 | if (err) { |
| 1473 | of_node_put(port); |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1474 | goto out_put_node; |
Christophe JAILLET | ba69fd9 | 2021-10-18 21:59:00 +0200 | [diff] [blame] | 1475 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1476 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1477 | if (reg >= ds->num_ports) { |
Vladimir Oltean | 258030a | 2022-01-05 15:21:39 +0200 | [diff] [blame] | 1478 | dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n", |
Rafał Miłecki | 8209f5b | 2021-01-06 10:09:15 +0100 | [diff] [blame] | 1479 | port, reg, ds->num_ports); |
Christophe JAILLET | ba69fd9 | 2021-10-18 21:59:00 +0200 | [diff] [blame] | 1480 | of_node_put(port); |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1481 | err = -EINVAL; |
| 1482 | goto out_put_node; |
| 1483 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1484 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1485 | dp = dsa_to_port(ds, reg); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1486 | |
| 1487 | err = dsa_port_parse_of(dp, port); |
Christophe JAILLET | ba69fd9 | 2021-10-18 21:59:00 +0200 | [diff] [blame] | 1488 | if (err) { |
| 1489 | of_node_put(port); |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1490 | goto out_put_node; |
Christophe JAILLET | ba69fd9 | 2021-10-18 21:59:00 +0200 | [diff] [blame] | 1491 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 1494 | out_put_node: |
| 1495 | of_node_put(ports); |
| 1496 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1499 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
| 1500 | struct device_node *dn) |
| 1501 | { |
| 1502 | u32 m[2] = { 0, 0 }; |
| 1503 | int sz; |
| 1504 | |
| 1505 | /* Don't error out if this optional property isn't found */ |
| 1506 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); |
| 1507 | if (sz < 0 && sz != -EINVAL) |
| 1508 | return sz; |
| 1509 | |
| 1510 | ds->index = m[1]; |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1511 | |
| 1512 | ds->dst = dsa_tree_touch(m[0]); |
| 1513 | if (!ds->dst) |
| 1514 | return -ENOMEM; |
| 1515 | |
Vladimir Oltean | 8674f8d | 2021-06-21 19:42:14 +0300 | [diff] [blame] | 1516 | if (dsa_switch_find(ds->dst->index, ds->index)) { |
| 1517 | dev_err(ds->dev, |
| 1518 | "A DSA switch with index %d already exists in tree %d\n", |
| 1519 | ds->index, ds->dst->index); |
| 1520 | return -EEXIST; |
| 1521 | } |
| 1522 | |
Vladimir Oltean | 5b22d36 | 2021-07-22 18:55:39 +0300 | [diff] [blame] | 1523 | if (ds->dst->last_switch < ds->index) |
| 1524 | ds->dst->last_switch = ds->index; |
| 1525 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1526 | return 0; |
| 1527 | } |
| 1528 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1529 | static int dsa_switch_touch_ports(struct dsa_switch *ds) |
| 1530 | { |
| 1531 | struct dsa_port *dp; |
| 1532 | int port; |
| 1533 | |
| 1534 | for (port = 0; port < ds->num_ports; port++) { |
| 1535 | dp = dsa_port_touch(ds, port); |
| 1536 | if (!dp) |
| 1537 | return -ENOMEM; |
| 1538 | } |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1543 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) |
| 1544 | { |
| 1545 | int err; |
| 1546 | |
| 1547 | err = dsa_switch_parse_member_of(ds, dn); |
| 1548 | if (err) |
| 1549 | return err; |
| 1550 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1551 | err = dsa_switch_touch_ports(ds); |
| 1552 | if (err) |
| 1553 | return err; |
| 1554 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1555 | return dsa_switch_parse_ports_of(ds, dn); |
| 1556 | } |
| 1557 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1558 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 1559 | struct device *dev) |
| 1560 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1561 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 1562 | struct net_device *master; |
| 1563 | |
| 1564 | master = dsa_dev_to_net_device(dev); |
| 1565 | if (!master) |
| 1566 | return -EPROBE_DEFER; |
| 1567 | |
| 1568 | dev_put(master); |
| 1569 | |
Tobias Waldekranz | deff710 | 2021-04-20 20:53:10 +0200 | [diff] [blame] | 1570 | return dsa_port_parse_cpu(dp, master, NULL); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 1571 | } |
| 1572 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1573 | if (!strcmp(name, "dsa")) |
| 1574 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1575 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 1576 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1577 | } |
| 1578 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1579 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 1580 | struct dsa_chip_data *cd) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1581 | { |
| 1582 | bool valid_name_found = false; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1583 | struct dsa_port *dp; |
| 1584 | struct device *dev; |
| 1585 | const char *name; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1586 | unsigned int i; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1587 | int err; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1588 | |
| 1589 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1590 | name = cd->port_names[i]; |
| 1591 | dev = cd->netdev[i]; |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1592 | dp = dsa_to_port(ds, i); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1593 | |
| 1594 | if (!name) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1595 | continue; |
| 1596 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 1597 | err = dsa_port_parse(dp, name, dev); |
| 1598 | if (err) |
| 1599 | return err; |
| 1600 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1601 | valid_name_found = true; |
| 1602 | } |
| 1603 | |
| 1604 | if (!valid_name_found && i == DSA_MAX_PORTS) |
| 1605 | return -EINVAL; |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1610 | 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] | 1611 | { |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1612 | int err; |
| 1613 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1614 | ds->cd = cd; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1615 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1616 | /* We don't support interconnected switches nor multiple trees via |
| 1617 | * platform data, so this is the unique switch of the tree. |
| 1618 | */ |
| 1619 | ds->index = 0; |
| 1620 | ds->dst = dsa_tree_touch(0); |
| 1621 | if (!ds->dst) |
| 1622 | return -ENOMEM; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1623 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 1624 | err = dsa_switch_touch_ports(ds); |
| 1625 | if (err) |
| 1626 | return err; |
| 1627 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1628 | return dsa_switch_parse_ports(ds, cd); |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1631 | static void dsa_switch_release_ports(struct dsa_switch *ds) |
| 1632 | { |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1633 | struct dsa_port *dp, *next; |
| 1634 | |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 1635 | dsa_switch_for_each_port_safe(dp, next, ds) { |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1636 | list_del(&dp->list); |
| 1637 | kfree(dp); |
| 1638 | } |
| 1639 | } |
| 1640 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1641 | static int dsa_switch_probe(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1642 | { |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1643 | struct dsa_switch_tree *dst; |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1644 | struct dsa_chip_data *pdata; |
| 1645 | struct device_node *np; |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 1646 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1647 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1648 | if (!ds->dev) |
| 1649 | return -ENODEV; |
| 1650 | |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 1651 | pdata = ds->dev->platform_data; |
| 1652 | np = ds->dev->of_node; |
| 1653 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 1654 | if (!ds->num_ports) |
| 1655 | return -EINVAL; |
| 1656 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1657 | if (np) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1658 | err = dsa_switch_parse_of(ds, np); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1659 | if (err) |
| 1660 | dsa_switch_release_ports(ds); |
| 1661 | } else if (pdata) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1662 | err = dsa_switch_parse(ds, pdata); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1663 | if (err) |
| 1664 | dsa_switch_release_ports(ds); |
| 1665 | } else { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1666 | err = -ENODEV; |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1667 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1668 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 1669 | if (err) |
| 1670 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1671 | |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1672 | dst = ds->dst; |
| 1673 | dsa_tree_get(dst); |
| 1674 | err = dsa_tree_setup(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1675 | if (err) { |
| 1676 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1677 | dsa_tree_put(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1678 | } |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1679 | |
| 1680 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 1683 | int dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1684 | { |
| 1685 | int err; |
| 1686 | |
| 1687 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1688 | err = dsa_switch_probe(ds); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 1689 | dsa_tree_put(ds->dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1690 | mutex_unlock(&dsa2_mutex); |
| 1691 | |
| 1692 | return err; |
| 1693 | } |
| 1694 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 1695 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1696 | static void dsa_switch_remove(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1697 | { |
| 1698 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 1699 | |
Florian Fainelli | c058f6d | 2019-11-02 20:13:26 -0700 | [diff] [blame] | 1700 | dsa_tree_teardown(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 1701 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 1702 | dsa_tree_put(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 1706 | { |
| 1707 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 1708 | dsa_switch_remove(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1709 | mutex_unlock(&dsa2_mutex); |
| 1710 | } |
| 1711 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |
Vladimir Oltean | 0650bf5 | 2021-09-17 16:34:33 +0300 | [diff] [blame] | 1712 | |
| 1713 | /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is |
| 1714 | * blocking that operation from completion, due to the dev_hold taken inside |
| 1715 | * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of |
| 1716 | * the DSA master, so that the system can reboot successfully. |
| 1717 | */ |
| 1718 | void dsa_switch_shutdown(struct dsa_switch *ds) |
| 1719 | { |
| 1720 | struct net_device *master, *slave_dev; |
Vladimir Oltean | 0650bf5 | 2021-09-17 16:34:33 +0300 | [diff] [blame] | 1721 | struct dsa_port *dp; |
| 1722 | |
| 1723 | mutex_lock(&dsa2_mutex); |
| 1724 | rtnl_lock(); |
| 1725 | |
Vladimir Oltean | 65c563a | 2021-10-20 20:49:51 +0300 | [diff] [blame] | 1726 | dsa_switch_for_each_user_port(dp, ds) { |
Vladimir Oltean | 0650bf5 | 2021-09-17 16:34:33 +0300 | [diff] [blame] | 1727 | master = dp->cpu_dp->master; |
| 1728 | slave_dev = dp->slave; |
| 1729 | |
| 1730 | netdev_upper_dev_unlink(master, slave_dev); |
Vladimir Oltean | 0650bf5 | 2021-09-17 16:34:33 +0300 | [diff] [blame] | 1731 | } |
Vladimir Oltean | ee53437 | 2022-02-09 14:04:33 +0200 | [diff] [blame^] | 1732 | |
| 1733 | /* Disconnect from further netdevice notifiers on the master, |
| 1734 | * since netdev_uses_dsa() will now return false. |
| 1735 | */ |
| 1736 | dsa_switch_for_each_cpu_port(dp, ds) |
| 1737 | dp->master->dsa_ptr = NULL; |
Vladimir Oltean | 0650bf5 | 2021-09-17 16:34:33 +0300 | [diff] [blame] | 1738 | |
| 1739 | rtnl_unlock(); |
| 1740 | mutex_unlock(&dsa2_mutex); |
| 1741 | } |
| 1742 | EXPORT_SYMBOL_GPL(dsa_switch_shutdown); |