Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/dsa2.c - Hardware switch handling, binding version 2 |
| 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
| 5 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/rtnetlink.h> |
| 18 | #include <net/dsa.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_net.h> |
| 21 | #include "dsa_priv.h" |
| 22 | |
| 23 | static LIST_HEAD(dsa_switch_trees); |
| 24 | static DEFINE_MUTEX(dsa2_mutex); |
| 25 | |
| 26 | static struct dsa_switch_tree *dsa_get_dst(u32 tree) |
| 27 | { |
| 28 | struct dsa_switch_tree *dst; |
| 29 | |
| 30 | list_for_each_entry(dst, &dsa_switch_trees, list) |
Nikita Yushchenko | 7a99cd6 | 2016-11-28 09:48:48 +0300 | [diff] [blame] | 31 | if (dst->tree == tree) { |
| 32 | kref_get(&dst->refcount); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 33 | return dst; |
Nikita Yushchenko | 7a99cd6 | 2016-11-28 09:48:48 +0300 | [diff] [blame] | 34 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | static void dsa_free_dst(struct kref *ref) |
| 39 | { |
| 40 | struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree, |
| 41 | refcount); |
| 42 | |
| 43 | list_del(&dst->list); |
| 44 | kfree(dst); |
| 45 | } |
| 46 | |
| 47 | static void dsa_put_dst(struct dsa_switch_tree *dst) |
| 48 | { |
| 49 | kref_put(&dst->refcount, dsa_free_dst); |
| 50 | } |
| 51 | |
| 52 | static struct dsa_switch_tree *dsa_add_dst(u32 tree) |
| 53 | { |
| 54 | struct dsa_switch_tree *dst; |
| 55 | |
| 56 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 57 | if (!dst) |
| 58 | return NULL; |
| 59 | dst->tree = tree; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 60 | INIT_LIST_HEAD(&dst->list); |
| 61 | list_add_tail(&dsa_switch_trees, &dst->list); |
| 62 | kref_init(&dst->refcount); |
| 63 | |
| 64 | return dst; |
| 65 | } |
| 66 | |
| 67 | static void dsa_dst_add_ds(struct dsa_switch_tree *dst, |
| 68 | struct dsa_switch *ds, u32 index) |
| 69 | { |
| 70 | kref_get(&dst->refcount); |
| 71 | dst->ds[index] = ds; |
| 72 | } |
| 73 | |
| 74 | static void dsa_dst_del_ds(struct dsa_switch_tree *dst, |
| 75 | struct dsa_switch *ds, u32 index) |
| 76 | { |
| 77 | dst->ds[index] = NULL; |
| 78 | kref_put(&dst->refcount, dsa_free_dst); |
| 79 | } |
| 80 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 81 | static bool dsa_port_is_valid(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 82 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 83 | return !!port->dn; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 86 | static bool dsa_port_is_dsa(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 87 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 88 | return !!of_parse_phandle(port->dn, "link", 0); |
| 89 | } |
| 90 | |
| 91 | static bool dsa_port_is_cpu(struct dsa_port *port) |
| 92 | { |
| 93 | return !!of_parse_phandle(port->dn, "ethernet", 0); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 96 | static bool dsa_ds_find_port_dn(struct dsa_switch *ds, |
| 97 | struct device_node *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 98 | { |
| 99 | u32 index; |
| 100 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 101 | for (index = 0; index < ds->num_ports; index++) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 102 | if (ds->ports[index].dn == port) |
| 103 | return true; |
| 104 | return false; |
| 105 | } |
| 106 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 107 | static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst, |
| 108 | struct device_node *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 109 | { |
| 110 | struct dsa_switch *ds; |
| 111 | u32 index; |
| 112 | |
| 113 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 114 | ds = dst->ds[index]; |
| 115 | if (!ds) |
| 116 | continue; |
| 117 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 118 | if (dsa_ds_find_port_dn(ds, port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 119 | return ds; |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | static int dsa_port_complete(struct dsa_switch_tree *dst, |
| 126 | struct dsa_switch *src_ds, |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 127 | struct dsa_port *port, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 128 | u32 src_port) |
| 129 | { |
| 130 | struct device_node *link; |
| 131 | int index; |
| 132 | struct dsa_switch *dst_ds; |
| 133 | |
| 134 | for (index = 0;; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 135 | link = of_parse_phandle(port->dn, "link", index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 136 | if (!link) |
| 137 | break; |
| 138 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 139 | dst_ds = dsa_dst_find_port_dn(dst, link); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 140 | of_node_put(link); |
| 141 | |
| 142 | if (!dst_ds) |
| 143 | return 1; |
| 144 | |
| 145 | src_ds->rtable[dst_ds->index] = src_port; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* A switch is complete if all the DSA ports phandles point to ports |
| 152 | * known in the tree. A return value of 1 means the tree is not |
| 153 | * complete. This is not an error condition. A value of 0 is |
| 154 | * success. |
| 155 | */ |
| 156 | static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 157 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 158 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 159 | u32 index; |
| 160 | int err; |
| 161 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 162 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 163 | port = &ds->ports[index]; |
| 164 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 165 | continue; |
| 166 | |
| 167 | if (!dsa_port_is_dsa(port)) |
| 168 | continue; |
| 169 | |
| 170 | err = dsa_port_complete(dst, ds, port, index); |
| 171 | if (err != 0) |
| 172 | return err; |
| 173 | |
| 174 | ds->dsa_port_mask |= BIT(index); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* A tree is complete if all the DSA ports phandles point to ports |
| 181 | * known in the tree. A return value of 1 means the tree is not |
| 182 | * complete. This is not an error condition. A value of 0 is |
| 183 | * success. |
| 184 | */ |
| 185 | static int dsa_dst_complete(struct dsa_switch_tree *dst) |
| 186 | { |
| 187 | struct dsa_switch *ds; |
| 188 | u32 index; |
| 189 | int err; |
| 190 | |
| 191 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 192 | ds = dst->ds[index]; |
| 193 | if (!ds) |
| 194 | continue; |
| 195 | |
| 196 | err = dsa_ds_complete(dst, ds); |
| 197 | if (err != 0) |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 204 | static int dsa_dsa_port_apply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 205 | struct dsa_switch *ds) |
| 206 | { |
| 207 | int err; |
| 208 | |
| 209 | err = dsa_cpu_dsa_setup(ds, ds->dev, port, index); |
| 210 | if (err) { |
| 211 | dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n", |
| 212 | index, err); |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 219 | static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 220 | struct dsa_switch *ds) |
| 221 | { |
| 222 | dsa_cpu_dsa_destroy(port); |
| 223 | } |
| 224 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 225 | static int dsa_cpu_port_apply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 226 | struct dsa_switch *ds) |
| 227 | { |
| 228 | int err; |
| 229 | |
| 230 | err = dsa_cpu_dsa_setup(ds, ds->dev, port, index); |
| 231 | if (err) { |
| 232 | dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n", |
| 233 | index, err); |
| 234 | return err; |
| 235 | } |
| 236 | |
| 237 | ds->cpu_port_mask |= BIT(index); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 242 | static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 243 | struct dsa_switch *ds) |
| 244 | { |
| 245 | dsa_cpu_dsa_destroy(port); |
| 246 | ds->cpu_port_mask &= ~BIT(index); |
| 247 | |
| 248 | } |
| 249 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 250 | static int dsa_user_port_apply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 251 | struct dsa_switch *ds) |
| 252 | { |
| 253 | const char *name; |
| 254 | int err; |
| 255 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 256 | name = of_get_property(port->dn, "label", NULL); |
Vivien Didelot | 9f91484 | 2017-01-09 18:13:51 -0500 | [diff] [blame] | 257 | if (!name) |
| 258 | name = "eth%d"; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 259 | |
| 260 | err = dsa_slave_create(ds, ds->dev, index, name); |
| 261 | if (err) { |
| 262 | dev_warn(ds->dev, "Failed to create slave %d: %d\n", |
| 263 | index, err); |
| 264 | return err; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 270 | static void dsa_user_port_unapply(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 271 | struct dsa_switch *ds) |
| 272 | { |
| 273 | if (ds->ports[index].netdev) { |
| 274 | dsa_slave_destroy(ds->ports[index].netdev); |
| 275 | ds->ports[index].netdev = NULL; |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 276 | ds->enabled_port_mask &= ~(1 << index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 281 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 282 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 283 | u32 index; |
| 284 | int err; |
| 285 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 286 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 287 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 288 | * the slave MDIO bus driver rely on these values for probing PHY |
| 289 | * devices or not |
| 290 | */ |
| 291 | ds->phys_mii_mask = ds->enabled_port_mask; |
| 292 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 293 | err = ds->ops->setup(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 294 | if (err < 0) |
| 295 | return err; |
| 296 | |
John Crispin | 092183d | 2016-09-19 15:28:01 +0200 | [diff] [blame] | 297 | if (ds->ops->set_addr) { |
| 298 | err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr); |
| 299 | if (err < 0) |
| 300 | return err; |
| 301 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 302 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 303 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 304 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
| 305 | if (!ds->slave_mii_bus) |
| 306 | return -ENOMEM; |
| 307 | |
| 308 | dsa_slave_mii_bus_init(ds); |
| 309 | |
| 310 | err = mdiobus_register(ds->slave_mii_bus); |
| 311 | if (err < 0) |
| 312 | return err; |
| 313 | } |
| 314 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 315 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 316 | port = &ds->ports[index]; |
| 317 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 318 | continue; |
| 319 | |
| 320 | if (dsa_port_is_dsa(port)) { |
| 321 | err = dsa_dsa_port_apply(port, index, ds); |
| 322 | if (err) |
| 323 | return err; |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | if (dsa_port_is_cpu(port)) { |
| 328 | err = dsa_cpu_port_apply(port, index, ds); |
| 329 | if (err) |
| 330 | return err; |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | err = dsa_user_port_apply(port, index, ds); |
| 335 | if (err) |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 343 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 344 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 345 | u32 index; |
| 346 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 347 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 348 | port = &ds->ports[index]; |
| 349 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 350 | continue; |
| 351 | |
| 352 | if (dsa_port_is_dsa(port)) { |
| 353 | dsa_dsa_port_unapply(port, index, ds); |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | if (dsa_port_is_cpu(port)) { |
| 358 | dsa_cpu_port_unapply(port, index, ds); |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | dsa_user_port_unapply(port, index, ds); |
| 363 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 364 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 365 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 366 | mdiobus_unregister(ds->slave_mii_bus); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int dsa_dst_apply(struct dsa_switch_tree *dst) |
| 370 | { |
| 371 | struct dsa_switch *ds; |
| 372 | u32 index; |
| 373 | int err; |
| 374 | |
| 375 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 376 | ds = dst->ds[index]; |
| 377 | if (!ds) |
| 378 | continue; |
| 379 | |
| 380 | err = dsa_ds_apply(dst, ds); |
| 381 | if (err) |
| 382 | return err; |
| 383 | } |
| 384 | |
Vivien Didelot | 9520ed8 | 2017-01-17 20:41:39 -0500 | [diff] [blame] | 385 | if (dst->cpu_switch) { |
| 386 | err = dsa_cpu_port_ethtool_setup(dst->cpu_switch); |
Florian Fainelli | faf3a93 | 2017-01-09 11:58:34 -0800 | [diff] [blame] | 387 | if (err) |
| 388 | return err; |
| 389 | } |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 390 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 391 | /* If we use a tagging format that doesn't have an ethertype |
| 392 | * field, make sure that all packets from this point on get |
| 393 | * sent to the tag format's receive function. |
| 394 | */ |
| 395 | wmb(); |
| 396 | dst->master_netdev->dsa_ptr = (void *)dst; |
| 397 | dst->applied = true; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void dsa_dst_unapply(struct dsa_switch_tree *dst) |
| 403 | { |
| 404 | struct dsa_switch *ds; |
| 405 | u32 index; |
| 406 | |
| 407 | if (!dst->applied) |
| 408 | return; |
| 409 | |
| 410 | dst->master_netdev->dsa_ptr = NULL; |
| 411 | |
| 412 | /* If we used a tagging format that doesn't have an ethertype |
| 413 | * field, make sure that all packets from this point get sent |
| 414 | * without the tag and go through the regular receive path. |
| 415 | */ |
| 416 | wmb(); |
| 417 | |
| 418 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 419 | ds = dst->ds[index]; |
| 420 | if (!ds) |
| 421 | continue; |
| 422 | |
| 423 | dsa_ds_unapply(dst, ds); |
| 424 | } |
| 425 | |
Vivien Didelot | 9520ed8 | 2017-01-17 20:41:39 -0500 | [diff] [blame] | 426 | if (dst->cpu_switch) |
| 427 | dsa_cpu_port_ethtool_restore(dst->cpu_switch); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 428 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 429 | pr_info("DSA: tree %d unapplied\n", dst->tree); |
| 430 | dst->applied = false; |
| 431 | } |
| 432 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 433 | static int dsa_cpu_parse(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 434 | struct dsa_switch_tree *dst, |
| 435 | struct dsa_switch *ds) |
| 436 | { |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 437 | enum dsa_tag_protocol tag_protocol; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 438 | struct net_device *ethernet_dev; |
| 439 | struct device_node *ethernet; |
| 440 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 441 | ethernet = of_parse_phandle(port->dn, "ethernet", 0); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 442 | if (!ethernet) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | ethernet_dev = of_find_net_device_by_node(ethernet); |
| 446 | if (!ethernet_dev) |
| 447 | return -EPROBE_DEFER; |
| 448 | |
| 449 | if (!ds->master_netdev) |
| 450 | ds->master_netdev = ethernet_dev; |
| 451 | |
| 452 | if (!dst->master_netdev) |
| 453 | dst->master_netdev = ethernet_dev; |
| 454 | |
Vivien Didelot | b22de49 | 2017-01-17 20:41:38 -0500 | [diff] [blame] | 455 | if (!dst->cpu_switch) { |
| 456 | dst->cpu_switch = ds; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 457 | dst->cpu_port = index; |
| 458 | } |
| 459 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 460 | tag_protocol = ds->ops->get_tag_protocol(ds); |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 461 | dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 462 | if (IS_ERR(dst->tag_ops)) { |
| 463 | dev_warn(ds->dev, "No tagger for this switch\n"); |
| 464 | return PTR_ERR(dst->tag_ops); |
| 465 | } |
| 466 | |
| 467 | dst->rcv = dst->tag_ops->rcv; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 473 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 474 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 475 | u32 index; |
| 476 | int err; |
| 477 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 478 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 479 | port = &ds->ports[index]; |
| 480 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 481 | continue; |
| 482 | |
| 483 | if (dsa_port_is_cpu(port)) { |
| 484 | err = dsa_cpu_parse(port, index, dst, ds); |
| 485 | if (err) |
| 486 | return err; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int dsa_dst_parse(struct dsa_switch_tree *dst) |
| 496 | { |
| 497 | struct dsa_switch *ds; |
| 498 | u32 index; |
| 499 | int err; |
| 500 | |
| 501 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 502 | ds = dst->ds[index]; |
| 503 | if (!ds) |
| 504 | continue; |
| 505 | |
| 506 | err = dsa_ds_parse(dst, ds); |
| 507 | if (err) |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | if (!dst->master_netdev) { |
| 512 | pr_warn("Tree has no master device\n"); |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | |
| 516 | pr_info("DSA: tree %d parsed\n", dst->tree); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds) |
| 522 | { |
| 523 | struct device_node *port; |
| 524 | int err; |
| 525 | u32 reg; |
| 526 | |
| 527 | for_each_available_child_of_node(ports, port) { |
| 528 | err = of_property_read_u32(port, "reg", ®); |
| 529 | if (err) |
| 530 | return err; |
| 531 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 532 | if (reg >= ds->num_ports) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 533 | return -EINVAL; |
| 534 | |
| 535 | ds->ports[reg].dn = port; |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 536 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 537 | /* Initialize enabled_port_mask now for ops->setup() |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 538 | * to have access to a correct value, just like what |
| 539 | * net/dsa/dsa.c::dsa_switch_setup_one does. |
| 540 | */ |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 541 | if (!dsa_port_is_cpu(&ds->ports[reg])) |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 542 | ds->enabled_port_mask |= 1 << reg; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 548 | static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 549 | { |
| 550 | int err; |
| 551 | |
| 552 | *tree = *index = 0; |
| 553 | |
| 554 | err = of_property_read_u32_index(np, "dsa,member", 0, tree); |
| 555 | if (err) { |
| 556 | /* Does not exist, but it is optional */ |
| 557 | if (err == -EINVAL) |
| 558 | return 0; |
| 559 | return err; |
| 560 | } |
| 561 | |
| 562 | err = of_property_read_u32_index(np, "dsa,member", 1, index); |
| 563 | if (err) |
| 564 | return err; |
| 565 | |
| 566 | if (*index >= DSA_MAX_SWITCHES) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static struct device_node *dsa_get_ports(struct dsa_switch *ds, |
| 573 | struct device_node *np) |
| 574 | { |
| 575 | struct device_node *ports; |
| 576 | |
| 577 | ports = of_get_child_by_name(np, "ports"); |
| 578 | if (!ports) { |
| 579 | dev_err(ds->dev, "no ports child node found\n"); |
| 580 | return ERR_PTR(-EINVAL); |
| 581 | } |
| 582 | |
| 583 | return ports; |
| 584 | } |
| 585 | |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 586 | static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 587 | { |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 588 | struct device_node *np = dev->of_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 589 | struct dsa_switch_tree *dst; |
Florian Fainelli | bc1727d | 2017-01-26 10:45:54 -0800 | [diff] [blame] | 590 | struct device_node *ports; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 591 | u32 tree, index; |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 592 | int i, err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 593 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 594 | err = dsa_parse_member_dn(np, &tree, &index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 595 | if (err) |
| 596 | return err; |
| 597 | |
Florian Fainelli | bc1727d | 2017-01-26 10:45:54 -0800 | [diff] [blame] | 598 | ports = dsa_get_ports(ds, np); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 599 | if (IS_ERR(ports)) |
| 600 | return PTR_ERR(ports); |
| 601 | |
| 602 | err = dsa_parse_ports_dn(ports, ds); |
| 603 | if (err) |
| 604 | return err; |
| 605 | |
| 606 | dst = dsa_get_dst(tree); |
| 607 | if (!dst) { |
| 608 | dst = dsa_add_dst(tree); |
| 609 | if (!dst) |
| 610 | return -ENOMEM; |
| 611 | } |
| 612 | |
| 613 | if (dst->ds[index]) { |
| 614 | err = -EBUSY; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | ds->dst = dst; |
| 619 | ds->index = index; |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 620 | |
| 621 | /* Initialize the routing table */ |
| 622 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) |
| 623 | ds->rtable[i] = DSA_RTABLE_NONE; |
| 624 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 625 | dsa_dst_add_ds(dst, ds, index); |
| 626 | |
| 627 | err = dsa_dst_complete(dst); |
| 628 | if (err < 0) |
| 629 | goto out_del_dst; |
| 630 | |
| 631 | if (err == 1) { |
| 632 | /* Not all switches registered yet */ |
| 633 | err = 0; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | if (dst->applied) { |
| 638 | pr_info("DSA: Disjoint trees?\n"); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
| 642 | err = dsa_dst_parse(dst); |
Volodymyr Bendiuga | 5e6eb45 | 2017-01-05 11:10:13 +0100 | [diff] [blame] | 643 | if (err) { |
| 644 | if (err == -EPROBE_DEFER) { |
| 645 | dsa_dst_del_ds(dst, ds, ds->index); |
| 646 | return err; |
| 647 | } |
| 648 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 649 | goto out_del_dst; |
Volodymyr Bendiuga | 5e6eb45 | 2017-01-05 11:10:13 +0100 | [diff] [blame] | 650 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 651 | |
| 652 | err = dsa_dst_apply(dst); |
| 653 | if (err) { |
| 654 | dsa_dst_unapply(dst); |
| 655 | goto out_del_dst; |
| 656 | } |
| 657 | |
| 658 | dsa_put_dst(dst); |
| 659 | return 0; |
| 660 | |
| 661 | out_del_dst: |
| 662 | dsa_dst_del_ds(dst, ds, ds->index); |
| 663 | out: |
| 664 | dsa_put_dst(dst); |
| 665 | |
| 666 | return err; |
| 667 | } |
| 668 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 669 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
| 670 | { |
| 671 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); |
| 672 | struct dsa_switch *ds; |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame^] | 673 | int i; |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 674 | |
| 675 | ds = devm_kzalloc(dev, size, GFP_KERNEL); |
| 676 | if (!ds) |
| 677 | return NULL; |
| 678 | |
| 679 | ds->dev = dev; |
| 680 | ds->num_ports = n; |
| 681 | |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame^] | 682 | for (i = 0; i < ds->num_ports; ++i) { |
| 683 | ds->ports[i].index = i; |
| 684 | ds->ports[i].ds = ds; |
| 685 | } |
| 686 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 687 | return ds; |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); |
| 690 | |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 691 | int dsa_register_switch(struct dsa_switch *ds, struct device *dev) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 692 | { |
| 693 | int err; |
| 694 | |
| 695 | mutex_lock(&dsa2_mutex); |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 696 | err = _dsa_register_switch(ds, dev); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 697 | mutex_unlock(&dsa2_mutex); |
| 698 | |
| 699 | return err; |
| 700 | } |
| 701 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 702 | |
Wei Yongjun | 85c22ba | 2016-07-12 15:24:10 +0000 | [diff] [blame] | 703 | static void _dsa_unregister_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 704 | { |
| 705 | struct dsa_switch_tree *dst = ds->dst; |
| 706 | |
| 707 | dsa_dst_unapply(dst); |
| 708 | |
| 709 | dsa_dst_del_ds(dst, ds, ds->index); |
| 710 | } |
| 711 | |
| 712 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 713 | { |
| 714 | mutex_lock(&dsa2_mutex); |
| 715 | _dsa_unregister_switch(ds); |
| 716 | mutex_unlock(&dsa2_mutex); |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |