David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [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 Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/rtnetlink.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_net.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 17 | #include <net/devlink.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | |
| 19 | #include "dsa_priv.h" |
| 20 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | static DEFINE_MUTEX(dsa2_mutex); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 22 | LIST_HEAD(dsa_tree_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 24 | struct dsa_switch *dsa_switch_find(int tree_index, int sw_index) |
| 25 | { |
| 26 | struct dsa_switch_tree *dst; |
| 27 | struct dsa_port *dp; |
| 28 | |
| 29 | list_for_each_entry(dst, &dsa_tree_list, list) { |
| 30 | if (dst->index != tree_index) |
| 31 | continue; |
| 32 | |
| 33 | list_for_each_entry(dp, &dst->ports, list) { |
| 34 | if (dp->ds->index != sw_index) |
| 35 | continue; |
| 36 | |
| 37 | return dp->ds; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(dsa_switch_find); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 44 | |
| 45 | static struct dsa_switch_tree *dsa_tree_find(int index) |
| 46 | { |
| 47 | struct dsa_switch_tree *dst; |
| 48 | |
| 49 | list_for_each_entry(dst, &dsa_tree_list, list) |
| 50 | if (dst->index == index) |
| 51 | return dst; |
| 52 | |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
| 57 | { |
| 58 | struct dsa_switch_tree *dst; |
| 59 | |
| 60 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 61 | if (!dst) |
| 62 | return NULL; |
| 63 | |
| 64 | dst->index = index; |
| 65 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 66 | INIT_LIST_HEAD(&dst->rtable); |
| 67 | |
| 68 | INIT_LIST_HEAD(&dst->ports); |
| 69 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 70 | INIT_LIST_HEAD(&dst->list); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 71 | list_add_tail(&dst->list, &dsa_tree_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 72 | |
| 73 | kref_init(&dst->refcount); |
| 74 | |
| 75 | return dst; |
| 76 | } |
| 77 | |
| 78 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
| 79 | { |
| 80 | list_del(&dst->list); |
| 81 | kfree(dst); |
| 82 | } |
| 83 | |
| 84 | static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst) |
| 85 | { |
| 86 | if (dst) |
| 87 | kref_get(&dst->refcount); |
| 88 | |
| 89 | return dst; |
| 90 | } |
| 91 | |
| 92 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
| 93 | { |
| 94 | struct dsa_switch_tree *dst; |
| 95 | |
| 96 | dst = dsa_tree_find(index); |
| 97 | if (dst) |
| 98 | return dsa_tree_get(dst); |
| 99 | else |
| 100 | return dsa_tree_alloc(index); |
| 101 | } |
| 102 | |
| 103 | static void dsa_tree_release(struct kref *ref) |
| 104 | { |
| 105 | struct dsa_switch_tree *dst; |
| 106 | |
| 107 | dst = container_of(ref, struct dsa_switch_tree, refcount); |
| 108 | |
| 109 | dsa_tree_free(dst); |
| 110 | } |
| 111 | |
| 112 | static void dsa_tree_put(struct dsa_switch_tree *dst) |
| 113 | { |
| 114 | if (dst) |
| 115 | kref_put(&dst->refcount, dsa_tree_release); |
| 116 | } |
| 117 | |
| 118 | static bool dsa_port_is_dsa(struct dsa_port *port) |
| 119 | { |
| 120 | return port->type == DSA_PORT_TYPE_DSA; |
| 121 | } |
| 122 | |
| 123 | static bool dsa_port_is_cpu(struct dsa_port *port) |
| 124 | { |
| 125 | return port->type == DSA_PORT_TYPE_CPU; |
| 126 | } |
| 127 | |
| 128 | static bool dsa_port_is_user(struct dsa_port *dp) |
| 129 | { |
| 130 | return dp->type == DSA_PORT_TYPE_USER; |
| 131 | } |
| 132 | |
| 133 | static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst, |
| 134 | struct device_node *dn) |
| 135 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 136 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 138 | list_for_each_entry(dp, &dst->ports, list) |
| 139 | if (dp->dn == dn) |
| 140 | return dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 145 | static struct dsa_link *dsa_link_touch(struct dsa_port *dp, |
| 146 | struct dsa_port *link_dp) |
| 147 | { |
| 148 | struct dsa_switch *ds = dp->ds; |
| 149 | struct dsa_switch_tree *dst; |
| 150 | struct dsa_link *dl; |
| 151 | |
| 152 | dst = ds->dst; |
| 153 | |
| 154 | list_for_each_entry(dl, &dst->rtable, list) |
| 155 | if (dl->dp == dp && dl->link_dp == link_dp) |
| 156 | return dl; |
| 157 | |
| 158 | dl = kzalloc(sizeof(*dl), GFP_KERNEL); |
| 159 | if (!dl) |
| 160 | return NULL; |
| 161 | |
| 162 | dl->dp = dp; |
| 163 | dl->link_dp = link_dp; |
| 164 | |
| 165 | INIT_LIST_HEAD(&dl->list); |
| 166 | list_add_tail(&dl->list, &dst->rtable); |
| 167 | |
| 168 | return dl; |
| 169 | } |
| 170 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 171 | static bool dsa_port_setup_routing_table(struct dsa_port *dp) |
| 172 | { |
| 173 | struct dsa_switch *ds = dp->ds; |
| 174 | struct dsa_switch_tree *dst = ds->dst; |
| 175 | struct device_node *dn = dp->dn; |
| 176 | struct of_phandle_iterator it; |
| 177 | struct dsa_port *link_dp; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 178 | struct dsa_link *dl; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 179 | int err; |
| 180 | |
| 181 | of_for_each_phandle(&it, err, dn, "link", NULL, 0) { |
| 182 | link_dp = dsa_tree_find_port_by_node(dst, it.node); |
| 183 | if (!link_dp) { |
| 184 | of_node_put(it.node); |
| 185 | return false; |
| 186 | } |
| 187 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 188 | dl = dsa_link_touch(dp, link_dp); |
| 189 | if (!dl) { |
| 190 | of_node_put(it.node); |
| 191 | return false; |
| 192 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 198 | static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 199 | { |
| 200 | bool complete = true; |
| 201 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 202 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 203 | list_for_each_entry(dp, &dst->ports, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | if (dsa_port_is_dsa(dp)) { |
| 205 | complete = dsa_port_setup_routing_table(dp); |
| 206 | if (!complete) |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return complete; |
| 212 | } |
| 213 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 214 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
| 215 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 216 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 217 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 218 | list_for_each_entry(dp, &dst->ports, list) |
| 219 | if (dsa_port_is_cpu(dp)) |
| 220 | return dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) |
| 226 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 227 | struct dsa_port *cpu_dp, *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 228 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 229 | cpu_dp = dsa_tree_find_first_cpu(dst); |
| 230 | if (!cpu_dp) { |
| 231 | pr_err("DSA: tree %d has no CPU port\n", dst->index); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | /* Assign the default CPU port to all ports of the fabric */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 236 | list_for_each_entry(dp, &dst->ports, list) |
| 237 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 238 | dp->cpu_dp = cpu_dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) |
| 244 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 245 | struct dsa_port *dp; |
| 246 | |
| 247 | list_for_each_entry(dp, &dst->ports, list) |
| 248 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 249 | dp->cpu_dp = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int dsa_port_setup(struct dsa_port *dp) |
| 253 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 254 | struct devlink_port *dlp = &dp->devlink_port; |
| 255 | bool dsa_port_link_registered = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 256 | bool dsa_port_enabled = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 257 | int err = 0; |
| 258 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 259 | if (dp->setup) |
| 260 | return 0; |
| 261 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 262 | switch (dp->type) { |
| 263 | case DSA_PORT_TYPE_UNUSED: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 264 | dsa_port_disable(dp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 265 | break; |
| 266 | case DSA_PORT_TYPE_CPU: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 267 | err = dsa_port_link_register_of(dp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 268 | if (err) |
| 269 | break; |
| 270 | dsa_port_link_registered = true; |
| 271 | |
| 272 | err = dsa_port_enable(dp, NULL); |
| 273 | if (err) |
| 274 | break; |
| 275 | dsa_port_enabled = true; |
| 276 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 277 | break; |
| 278 | case DSA_PORT_TYPE_DSA: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 279 | err = dsa_port_link_register_of(dp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 280 | if (err) |
| 281 | break; |
| 282 | dsa_port_link_registered = true; |
| 283 | |
| 284 | err = dsa_port_enable(dp, NULL); |
| 285 | if (err) |
| 286 | break; |
| 287 | dsa_port_enabled = true; |
| 288 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | break; |
| 290 | case DSA_PORT_TYPE_USER: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 291 | dp->mac = of_get_mac_address(dp->dn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 292 | err = dsa_slave_create(dp); |
| 293 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 294 | break; |
| 295 | |
| 296 | devlink_port_type_eth_set(dlp, dp->slave); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 300 | if (err && dsa_port_enabled) |
| 301 | dsa_port_disable(dp); |
| 302 | if (err && dsa_port_link_registered) |
| 303 | dsa_port_link_unregister_of(dp); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 304 | if (err) |
| 305 | return err; |
| 306 | |
| 307 | dp->setup = true; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int dsa_port_devlink_setup(struct dsa_port *dp) |
| 313 | { |
| 314 | struct devlink_port *dlp = &dp->devlink_port; |
| 315 | struct dsa_switch_tree *dst = dp->ds->dst; |
| 316 | struct devlink_port_attrs attrs = {}; |
| 317 | struct devlink *dl = dp->ds->devlink; |
| 318 | const unsigned char *id; |
| 319 | unsigned char len; |
| 320 | int err; |
| 321 | |
| 322 | id = (const unsigned char *)&dst->index; |
| 323 | len = sizeof(dst->index); |
| 324 | |
| 325 | attrs.phys.port_number = dp->index; |
| 326 | memcpy(attrs.switch_id.id, id, len); |
| 327 | attrs.switch_id.id_len = len; |
| 328 | memset(dlp, 0, sizeof(*dlp)); |
| 329 | |
| 330 | switch (dp->type) { |
| 331 | case DSA_PORT_TYPE_UNUSED: |
| 332 | attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; |
| 333 | break; |
| 334 | case DSA_PORT_TYPE_CPU: |
| 335 | attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU; |
| 336 | break; |
| 337 | case DSA_PORT_TYPE_DSA: |
| 338 | attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA; |
| 339 | break; |
| 340 | case DSA_PORT_TYPE_USER: |
| 341 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | devlink_port_attrs_set(dlp, &attrs); |
| 346 | err = devlink_port_register(dl, dlp, dp->index); |
| 347 | |
| 348 | if (!err) |
| 349 | dp->devlink_port_setup = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 350 | |
| 351 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void dsa_port_teardown(struct dsa_port *dp) |
| 355 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 356 | struct devlink_port *dlp = &dp->devlink_port; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 357 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 358 | if (!dp->setup) |
| 359 | return; |
| 360 | |
| 361 | devlink_port_type_clear(dlp); |
| 362 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 363 | switch (dp->type) { |
| 364 | case DSA_PORT_TYPE_UNUSED: |
| 365 | break; |
| 366 | case DSA_PORT_TYPE_CPU: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 367 | dsa_port_disable(dp); |
| 368 | dsa_tag_driver_put(dp->tag_ops); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 369 | dsa_port_link_unregister_of(dp); |
| 370 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 371 | case DSA_PORT_TYPE_DSA: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 372 | dsa_port_disable(dp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 373 | dsa_port_link_unregister_of(dp); |
| 374 | break; |
| 375 | case DSA_PORT_TYPE_USER: |
| 376 | if (dp->slave) { |
| 377 | dsa_slave_destroy(dp->slave); |
| 378 | dp->slave = NULL; |
| 379 | } |
| 380 | break; |
| 381 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 382 | |
| 383 | dp->setup = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 386 | static void dsa_port_devlink_teardown(struct dsa_port *dp) |
| 387 | { |
| 388 | struct devlink_port *dlp = &dp->devlink_port; |
| 389 | |
| 390 | if (dp->devlink_port_setup) |
| 391 | devlink_port_unregister(dlp); |
| 392 | dp->devlink_port_setup = false; |
| 393 | } |
| 394 | |
| 395 | static int dsa_devlink_info_get(struct devlink *dl, |
| 396 | struct devlink_info_req *req, |
| 397 | struct netlink_ext_ack *extack) |
| 398 | { |
| 399 | struct dsa_switch *ds = dsa_devlink_to_ds(dl); |
| 400 | |
| 401 | if (ds->ops->devlink_info_get) |
| 402 | return ds->ops->devlink_info_get(ds, req, extack); |
| 403 | |
| 404 | return -EOPNOTSUPP; |
| 405 | } |
| 406 | |
| 407 | static const struct devlink_ops dsa_devlink_ops = { |
| 408 | .info_get = dsa_devlink_info_get, |
| 409 | }; |
| 410 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 411 | static int dsa_switch_setup(struct dsa_switch *ds) |
| 412 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 413 | struct dsa_devlink_priv *dl_priv; |
| 414 | struct dsa_port *dp; |
| 415 | int err; |
| 416 | |
| 417 | if (ds->setup) |
| 418 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 419 | |
| 420 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
| 421 | * driver and before ops->setup() has run, since the switch drivers and |
| 422 | * the slave MDIO bus driver rely on these values for probing PHY |
| 423 | * devices or not |
| 424 | */ |
| 425 | ds->phys_mii_mask |= dsa_user_ports(ds); |
| 426 | |
| 427 | /* Add the switch to devlink before calling setup, so that setup can |
| 428 | * add dpipe tables |
| 429 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 430 | ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 431 | if (!ds->devlink) |
| 432 | return -ENOMEM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 433 | dl_priv = devlink_priv(ds->devlink); |
| 434 | dl_priv->ds = ds; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 435 | |
| 436 | err = devlink_register(ds->devlink, ds->dev); |
| 437 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 438 | goto free_devlink; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 439 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 440 | /* Setup devlink port instances now, so that the switch |
| 441 | * setup() can register regions etc, against the ports |
| 442 | */ |
| 443 | list_for_each_entry(dp, &ds->dst->ports, list) { |
| 444 | if (dp->ds == ds) { |
| 445 | err = dsa_port_devlink_setup(dp); |
| 446 | if (err) |
| 447 | goto unregister_devlink_ports; |
| 448 | } |
| 449 | } |
| 450 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 451 | err = dsa_switch_register_notifier(ds); |
| 452 | if (err) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 453 | goto unregister_devlink_ports; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 454 | |
| 455 | err = ds->ops->setup(ds); |
| 456 | if (err < 0) |
| 457 | goto unregister_notifier; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 458 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 459 | devlink_params_publish(ds->devlink); |
| 460 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 461 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 462 | ds->slave_mii_bus = mdiobus_alloc(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 463 | if (!ds->slave_mii_bus) { |
| 464 | err = -ENOMEM; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 465 | goto teardown; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 466 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 467 | |
| 468 | dsa_slave_mii_bus_init(ds); |
| 469 | |
| 470 | err = mdiobus_register(ds->slave_mii_bus); |
| 471 | if (err < 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 472 | goto free_slave_mii_bus; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 475 | ds->setup = true; |
| 476 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 477 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 478 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 479 | free_slave_mii_bus: |
| 480 | if (ds->slave_mii_bus && ds->ops->phy_read) |
| 481 | mdiobus_free(ds->slave_mii_bus); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 482 | teardown: |
| 483 | if (ds->ops->teardown) |
| 484 | ds->ops->teardown(ds); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 485 | unregister_notifier: |
| 486 | dsa_switch_unregister_notifier(ds); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 487 | unregister_devlink_ports: |
| 488 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 489 | if (dp->ds == ds) |
| 490 | dsa_port_devlink_teardown(dp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 491 | devlink_unregister(ds->devlink); |
| 492 | free_devlink: |
| 493 | devlink_free(ds->devlink); |
| 494 | ds->devlink = NULL; |
| 495 | |
| 496 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static void dsa_switch_teardown(struct dsa_switch *ds) |
| 500 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 501 | struct dsa_port *dp; |
| 502 | |
| 503 | if (!ds->setup) |
| 504 | return; |
| 505 | |
| 506 | if (ds->slave_mii_bus && ds->ops->phy_read) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 507 | mdiobus_unregister(ds->slave_mii_bus); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 508 | mdiobus_free(ds->slave_mii_bus); |
| 509 | ds->slave_mii_bus = NULL; |
| 510 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 511 | |
| 512 | dsa_switch_unregister_notifier(ds); |
| 513 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 514 | if (ds->ops->teardown) |
| 515 | ds->ops->teardown(ds); |
| 516 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 517 | if (ds->devlink) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 518 | list_for_each_entry(dp, &ds->dst->ports, list) |
| 519 | if (dp->ds == ds) |
| 520 | dsa_port_devlink_teardown(dp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 521 | devlink_unregister(ds->devlink); |
| 522 | devlink_free(ds->devlink); |
| 523 | ds->devlink = NULL; |
| 524 | } |
| 525 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 526 | ds->setup = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
| 530 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 531 | struct dsa_port *dp; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 532 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 533 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 534 | list_for_each_entry(dp, &dst->ports, list) { |
| 535 | err = dsa_switch_setup(dp->ds); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | if (err) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 537 | goto teardown; |
| 538 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 539 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 540 | list_for_each_entry(dp, &dst->ports, list) { |
| 541 | err = dsa_port_setup(dp); |
| 542 | if (err) { |
| 543 | dsa_port_devlink_teardown(dp); |
| 544 | dp->type = DSA_PORT_TYPE_UNUSED; |
| 545 | err = dsa_port_devlink_setup(dp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 546 | if (err) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 547 | goto teardown; |
| 548 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 553 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 554 | teardown: |
| 555 | list_for_each_entry(dp, &dst->ports, list) |
| 556 | dsa_port_teardown(dp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 557 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 558 | list_for_each_entry(dp, &dst->ports, list) |
| 559 | dsa_switch_teardown(dp->ds); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 560 | |
| 561 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) |
| 565 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 566 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 567 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 568 | list_for_each_entry(dp, &dst->ports, list) |
| 569 | dsa_port_teardown(dp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 570 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 571 | list_for_each_entry(dp, &dst->ports, list) |
| 572 | dsa_switch_teardown(dp->ds); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
| 576 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 577 | struct dsa_port *dp; |
| 578 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 579 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 580 | list_for_each_entry(dp, &dst->ports, list) { |
| 581 | if (dsa_port_is_cpu(dp)) { |
| 582 | err = dsa_master_setup(dp->master, dp); |
| 583 | if (err) |
| 584 | return err; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) |
| 592 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 593 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 594 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 595 | list_for_each_entry(dp, &dst->ports, list) |
| 596 | if (dsa_port_is_cpu(dp)) |
| 597 | dsa_master_teardown(dp->master); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
| 601 | { |
| 602 | bool complete; |
| 603 | int err; |
| 604 | |
| 605 | if (dst->setup) { |
| 606 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", |
| 607 | dst->index); |
| 608 | return -EEXIST; |
| 609 | } |
| 610 | |
| 611 | complete = dsa_tree_setup_routing_table(dst); |
| 612 | if (!complete) |
| 613 | return 0; |
| 614 | |
| 615 | err = dsa_tree_setup_default_cpu(dst); |
| 616 | if (err) |
| 617 | return err; |
| 618 | |
| 619 | err = dsa_tree_setup_switches(dst); |
| 620 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 621 | goto teardown_default_cpu; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 622 | |
| 623 | err = dsa_tree_setup_master(dst); |
| 624 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 625 | goto teardown_switches; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | |
| 627 | dst->setup = true; |
| 628 | |
| 629 | pr_info("DSA: tree %d setup\n", dst->index); |
| 630 | |
| 631 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 632 | |
| 633 | teardown_switches: |
| 634 | dsa_tree_teardown_switches(dst); |
| 635 | teardown_default_cpu: |
| 636 | dsa_tree_teardown_default_cpu(dst); |
| 637 | |
| 638 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
| 642 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 643 | struct dsa_link *dl, *next; |
| 644 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 645 | if (!dst->setup) |
| 646 | return; |
| 647 | |
| 648 | dsa_tree_teardown_master(dst); |
| 649 | |
| 650 | dsa_tree_teardown_switches(dst); |
| 651 | |
| 652 | dsa_tree_teardown_default_cpu(dst); |
| 653 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 654 | list_for_each_entry_safe(dl, next, &dst->rtable, list) { |
| 655 | list_del(&dl->list); |
| 656 | kfree(dl); |
| 657 | } |
| 658 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 659 | pr_info("DSA: tree %d torn down\n", dst->index); |
| 660 | |
| 661 | dst->setup = false; |
| 662 | } |
| 663 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 664 | static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 665 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 666 | struct dsa_switch_tree *dst = ds->dst; |
| 667 | struct dsa_port *dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 668 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 669 | list_for_each_entry(dp, &dst->ports, list) |
| 670 | if (dp->ds == ds && dp->index == index) |
| 671 | return dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 672 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 673 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 674 | if (!dp) |
| 675 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 676 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 677 | dp->ds = ds; |
| 678 | dp->index = index; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 679 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 680 | INIT_LIST_HEAD(&dp->list); |
| 681 | list_add_tail(&dp->list, &dst->ports); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 682 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 683 | return dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
| 687 | { |
| 688 | if (!name) |
| 689 | name = "eth%d"; |
| 690 | |
| 691 | dp->type = DSA_PORT_TYPE_USER; |
| 692 | dp->name = name; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 698 | { |
| 699 | dp->type = DSA_PORT_TYPE_DSA; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 704 | static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp, |
| 705 | struct net_device *master) |
| 706 | { |
| 707 | enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE; |
| 708 | struct dsa_switch *mds, *ds = dp->ds; |
| 709 | unsigned int mdp_upstream; |
| 710 | struct dsa_port *mdp; |
| 711 | |
| 712 | /* It is possible to stack DSA switches onto one another when that |
| 713 | * happens the switch driver may want to know if its tagging protocol |
| 714 | * is going to work in such a configuration. |
| 715 | */ |
| 716 | if (dsa_slave_dev_check(master)) { |
| 717 | mdp = dsa_slave_to_port(master); |
| 718 | mds = mdp->ds; |
| 719 | mdp_upstream = dsa_upstream_port(mds, mdp->index); |
| 720 | tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream, |
| 721 | DSA_TAG_PROTO_NONE); |
| 722 | } |
| 723 | |
| 724 | /* If the master device is not itself a DSA slave in a disjoint DSA |
| 725 | * tree, then return immediately. |
| 726 | */ |
| 727 | return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol); |
| 728 | } |
| 729 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 730 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) |
| 731 | { |
| 732 | struct dsa_switch *ds = dp->ds; |
| 733 | struct dsa_switch_tree *dst = ds->dst; |
| 734 | const struct dsa_device_ops *tag_ops; |
| 735 | enum dsa_tag_protocol tag_protocol; |
| 736 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 737 | tag_protocol = dsa_get_tag_protocol(dp, master); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 738 | tag_ops = dsa_tag_driver_get(tag_protocol); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 739 | if (IS_ERR(tag_ops)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 740 | if (PTR_ERR(tag_ops) == -ENOPROTOOPT) |
| 741 | return -EPROBE_DEFER; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 742 | dev_warn(ds->dev, "No tagger for this switch\n"); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 743 | dp->master = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 744 | return PTR_ERR(tag_ops); |
| 745 | } |
| 746 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 747 | dp->master = master; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 748 | dp->type = DSA_PORT_TYPE_CPU; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 749 | dp->filter = tag_ops->filter; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 750 | dp->rcv = tag_ops->rcv; |
| 751 | dp->tag_ops = tag_ops; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 752 | dp->dst = dst; |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 758 | { |
| 759 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
| 760 | const char *name = of_get_property(dn, "label", NULL); |
| 761 | bool link = of_property_read_bool(dn, "link"); |
| 762 | |
| 763 | dp->dn = dn; |
| 764 | |
| 765 | if (ethernet) { |
| 766 | struct net_device *master; |
| 767 | |
| 768 | master = of_find_net_device_by_node(ethernet); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 769 | of_node_put(ethernet); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 770 | if (!master) |
| 771 | return -EPROBE_DEFER; |
| 772 | |
| 773 | return dsa_port_parse_cpu(dp, master); |
| 774 | } |
| 775 | |
| 776 | if (link) |
| 777 | return dsa_port_parse_dsa(dp); |
| 778 | |
| 779 | return dsa_port_parse_user(dp, name); |
| 780 | } |
| 781 | |
| 782 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 783 | struct device_node *dn) |
| 784 | { |
| 785 | struct device_node *ports, *port; |
| 786 | struct dsa_port *dp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 787 | int err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 788 | u32 reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 789 | |
| 790 | ports = of_get_child_by_name(dn, "ports"); |
| 791 | if (!ports) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 792 | /* The second possibility is "ethernet-ports" */ |
| 793 | ports = of_get_child_by_name(dn, "ethernet-ports"); |
| 794 | if (!ports) { |
| 795 | dev_err(ds->dev, "no ports child node found\n"); |
| 796 | return -EINVAL; |
| 797 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | for_each_available_child_of_node(ports, port) { |
| 801 | err = of_property_read_u32(port, "reg", ®); |
| 802 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 803 | goto out_put_node; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 804 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 805 | if (reg >= ds->num_ports) { |
| 806 | err = -EINVAL; |
| 807 | goto out_put_node; |
| 808 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 809 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 810 | dp = dsa_to_port(ds, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 811 | |
| 812 | err = dsa_port_parse_of(dp, port); |
| 813 | if (err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 814 | goto out_put_node; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 817 | out_put_node: |
| 818 | of_node_put(ports); |
| 819 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
| 823 | struct device_node *dn) |
| 824 | { |
| 825 | u32 m[2] = { 0, 0 }; |
| 826 | int sz; |
| 827 | |
| 828 | /* Don't error out if this optional property isn't found */ |
| 829 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); |
| 830 | if (sz < 0 && sz != -EINVAL) |
| 831 | return sz; |
| 832 | |
| 833 | ds->index = m[1]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 834 | |
| 835 | ds->dst = dsa_tree_touch(m[0]); |
| 836 | if (!ds->dst) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 842 | static int dsa_switch_touch_ports(struct dsa_switch *ds) |
| 843 | { |
| 844 | struct dsa_port *dp; |
| 845 | int port; |
| 846 | |
| 847 | for (port = 0; port < ds->num_ports; port++) { |
| 848 | dp = dsa_port_touch(ds, port); |
| 849 | if (!dp) |
| 850 | return -ENOMEM; |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 856 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) |
| 857 | { |
| 858 | int err; |
| 859 | |
| 860 | err = dsa_switch_parse_member_of(ds, dn); |
| 861 | if (err) |
| 862 | return err; |
| 863 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 864 | err = dsa_switch_touch_ports(ds); |
| 865 | if (err) |
| 866 | return err; |
| 867 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 868 | return dsa_switch_parse_ports_of(ds, dn); |
| 869 | } |
| 870 | |
| 871 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 872 | struct device *dev) |
| 873 | { |
| 874 | if (!strcmp(name, "cpu")) { |
| 875 | struct net_device *master; |
| 876 | |
| 877 | master = dsa_dev_to_net_device(dev); |
| 878 | if (!master) |
| 879 | return -EPROBE_DEFER; |
| 880 | |
| 881 | dev_put(master); |
| 882 | |
| 883 | return dsa_port_parse_cpu(dp, master); |
| 884 | } |
| 885 | |
| 886 | if (!strcmp(name, "dsa")) |
| 887 | return dsa_port_parse_dsa(dp); |
| 888 | |
| 889 | return dsa_port_parse_user(dp, name); |
| 890 | } |
| 891 | |
| 892 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 893 | struct dsa_chip_data *cd) |
| 894 | { |
| 895 | bool valid_name_found = false; |
| 896 | struct dsa_port *dp; |
| 897 | struct device *dev; |
| 898 | const char *name; |
| 899 | unsigned int i; |
| 900 | int err; |
| 901 | |
| 902 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 903 | name = cd->port_names[i]; |
| 904 | dev = cd->netdev[i]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 905 | dp = dsa_to_port(ds, i); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 906 | |
| 907 | if (!name) |
| 908 | continue; |
| 909 | |
| 910 | err = dsa_port_parse(dp, name, dev); |
| 911 | if (err) |
| 912 | return err; |
| 913 | |
| 914 | valid_name_found = true; |
| 915 | } |
| 916 | |
| 917 | if (!valid_name_found && i == DSA_MAX_PORTS) |
| 918 | return -EINVAL; |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd) |
| 924 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 925 | int err; |
| 926 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 927 | ds->cd = cd; |
| 928 | |
| 929 | /* We don't support interconnected switches nor multiple trees via |
| 930 | * platform data, so this is the unique switch of the tree. |
| 931 | */ |
| 932 | ds->index = 0; |
| 933 | ds->dst = dsa_tree_touch(0); |
| 934 | if (!ds->dst) |
| 935 | return -ENOMEM; |
| 936 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 937 | err = dsa_switch_touch_ports(ds); |
| 938 | if (err) |
| 939 | return err; |
| 940 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 941 | return dsa_switch_parse_ports(ds, cd); |
| 942 | } |
| 943 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 944 | static void dsa_switch_release_ports(struct dsa_switch *ds) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 945 | { |
| 946 | struct dsa_switch_tree *dst = ds->dst; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 947 | struct dsa_port *dp, *next; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 948 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 949 | list_for_each_entry_safe(dp, next, &dst->ports, list) { |
| 950 | if (dp->ds != ds) |
| 951 | continue; |
| 952 | list_del(&dp->list); |
| 953 | kfree(dp); |
| 954 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | static int dsa_switch_probe(struct dsa_switch *ds) |
| 958 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 959 | struct dsa_switch_tree *dst; |
| 960 | struct dsa_chip_data *pdata; |
| 961 | struct device_node *np; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 962 | int err; |
| 963 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 964 | if (!ds->dev) |
| 965 | return -ENODEV; |
| 966 | |
| 967 | pdata = ds->dev->platform_data; |
| 968 | np = ds->dev->of_node; |
| 969 | |
| 970 | if (!ds->num_ports) |
| 971 | return -EINVAL; |
| 972 | |
| 973 | if (np) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 974 | err = dsa_switch_parse_of(ds, np); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 975 | if (err) |
| 976 | dsa_switch_release_ports(ds); |
| 977 | } else if (pdata) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 978 | err = dsa_switch_parse(ds, pdata); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 979 | if (err) |
| 980 | dsa_switch_release_ports(ds); |
| 981 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 982 | err = -ENODEV; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 983 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 984 | |
| 985 | if (err) |
| 986 | return err; |
| 987 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 988 | dst = ds->dst; |
| 989 | dsa_tree_get(dst); |
| 990 | err = dsa_tree_setup(dst); |
| 991 | if (err) { |
| 992 | dsa_switch_release_ports(ds); |
| 993 | dsa_tree_put(dst); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 996 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 997 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 998 | |
| 999 | int dsa_register_switch(struct dsa_switch *ds) |
| 1000 | { |
| 1001 | int err; |
| 1002 | |
| 1003 | mutex_lock(&dsa2_mutex); |
| 1004 | err = dsa_switch_probe(ds); |
| 1005 | dsa_tree_put(ds->dst); |
| 1006 | mutex_unlock(&dsa2_mutex); |
| 1007 | |
| 1008 | return err; |
| 1009 | } |
| 1010 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 1011 | |
| 1012 | static void dsa_switch_remove(struct dsa_switch *ds) |
| 1013 | { |
| 1014 | struct dsa_switch_tree *dst = ds->dst; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1016 | dsa_tree_teardown(dst); |
| 1017 | dsa_switch_release_ports(ds); |
| 1018 | dsa_tree_put(dst); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 1022 | { |
| 1023 | mutex_lock(&dsa2_mutex); |
| 1024 | dsa_switch_remove(ds); |
| 1025 | mutex_unlock(&dsa2_mutex); |
| 1026 | } |
| 1027 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |