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