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 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame^] | 297 | err = dsa_switch_register_notifier(ds); |
| 298 | if (err) |
| 299 | return err; |
| 300 | |
John Crispin | 092183d | 2016-09-19 15:28:01 +0200 | [diff] [blame] | 301 | if (ds->ops->set_addr) { |
| 302 | err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr); |
| 303 | if (err < 0) |
| 304 | return err; |
| 305 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 306 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 307 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 308 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
| 309 | if (!ds->slave_mii_bus) |
| 310 | return -ENOMEM; |
| 311 | |
| 312 | dsa_slave_mii_bus_init(ds); |
| 313 | |
| 314 | err = mdiobus_register(ds->slave_mii_bus); |
| 315 | if (err < 0) |
| 316 | return err; |
| 317 | } |
| 318 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 319 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 320 | port = &ds->ports[index]; |
| 321 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 322 | continue; |
| 323 | |
| 324 | if (dsa_port_is_dsa(port)) { |
| 325 | err = dsa_dsa_port_apply(port, index, ds); |
| 326 | if (err) |
| 327 | return err; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if (dsa_port_is_cpu(port)) { |
| 332 | err = dsa_cpu_port_apply(port, index, ds); |
| 333 | if (err) |
| 334 | return err; |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | err = dsa_user_port_apply(port, index, ds); |
| 339 | if (err) |
| 340 | continue; |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 347 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 348 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 349 | u32 index; |
| 350 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 351 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 352 | port = &ds->ports[index]; |
| 353 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 354 | continue; |
| 355 | |
| 356 | if (dsa_port_is_dsa(port)) { |
| 357 | dsa_dsa_port_unapply(port, index, ds); |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | if (dsa_port_is_cpu(port)) { |
| 362 | dsa_cpu_port_unapply(port, index, ds); |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | dsa_user_port_unapply(port, index, ds); |
| 367 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 368 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 369 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 370 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame^] | 371 | |
| 372 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int dsa_dst_apply(struct dsa_switch_tree *dst) |
| 376 | { |
| 377 | struct dsa_switch *ds; |
| 378 | u32 index; |
| 379 | int err; |
| 380 | |
| 381 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 382 | ds = dst->ds[index]; |
| 383 | if (!ds) |
| 384 | continue; |
| 385 | |
| 386 | err = dsa_ds_apply(dst, ds); |
| 387 | if (err) |
| 388 | return err; |
| 389 | } |
| 390 | |
Vivien Didelot | 9520ed8 | 2017-01-17 20:41:39 -0500 | [diff] [blame] | 391 | if (dst->cpu_switch) { |
| 392 | err = dsa_cpu_port_ethtool_setup(dst->cpu_switch); |
Florian Fainelli | faf3a93 | 2017-01-09 11:58:34 -0800 | [diff] [blame] | 393 | if (err) |
| 394 | return err; |
| 395 | } |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 396 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 397 | /* If we use a tagging format that doesn't have an ethertype |
| 398 | * field, make sure that all packets from this point on get |
| 399 | * sent to the tag format's receive function. |
| 400 | */ |
| 401 | wmb(); |
| 402 | dst->master_netdev->dsa_ptr = (void *)dst; |
| 403 | dst->applied = true; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static void dsa_dst_unapply(struct dsa_switch_tree *dst) |
| 409 | { |
| 410 | struct dsa_switch *ds; |
| 411 | u32 index; |
| 412 | |
| 413 | if (!dst->applied) |
| 414 | return; |
| 415 | |
| 416 | dst->master_netdev->dsa_ptr = NULL; |
| 417 | |
| 418 | /* If we used a tagging format that doesn't have an ethertype |
| 419 | * field, make sure that all packets from this point get sent |
| 420 | * without the tag and go through the regular receive path. |
| 421 | */ |
| 422 | wmb(); |
| 423 | |
| 424 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 425 | ds = dst->ds[index]; |
| 426 | if (!ds) |
| 427 | continue; |
| 428 | |
| 429 | dsa_ds_unapply(dst, ds); |
| 430 | } |
| 431 | |
Vivien Didelot | 9520ed8 | 2017-01-17 20:41:39 -0500 | [diff] [blame] | 432 | if (dst->cpu_switch) |
| 433 | dsa_cpu_port_ethtool_restore(dst->cpu_switch); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 434 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 435 | pr_info("DSA: tree %d unapplied\n", dst->tree); |
| 436 | dst->applied = false; |
| 437 | } |
| 438 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 439 | static int dsa_cpu_parse(struct dsa_port *port, u32 index, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 440 | struct dsa_switch_tree *dst, |
| 441 | struct dsa_switch *ds) |
| 442 | { |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 443 | enum dsa_tag_protocol tag_protocol; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 444 | struct net_device *ethernet_dev; |
| 445 | struct device_node *ethernet; |
| 446 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 447 | ethernet = of_parse_phandle(port->dn, "ethernet", 0); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 448 | if (!ethernet) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | ethernet_dev = of_find_net_device_by_node(ethernet); |
| 452 | if (!ethernet_dev) |
| 453 | return -EPROBE_DEFER; |
| 454 | |
| 455 | if (!ds->master_netdev) |
| 456 | ds->master_netdev = ethernet_dev; |
| 457 | |
| 458 | if (!dst->master_netdev) |
| 459 | dst->master_netdev = ethernet_dev; |
| 460 | |
Vivien Didelot | b22de49 | 2017-01-17 20:41:38 -0500 | [diff] [blame] | 461 | if (!dst->cpu_switch) { |
| 462 | dst->cpu_switch = ds; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 463 | dst->cpu_port = index; |
| 464 | } |
| 465 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 466 | tag_protocol = ds->ops->get_tag_protocol(ds); |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 467 | dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 468 | if (IS_ERR(dst->tag_ops)) { |
| 469 | dev_warn(ds->dev, "No tagger for this switch\n"); |
| 470 | return PTR_ERR(dst->tag_ops); |
| 471 | } |
| 472 | |
| 473 | dst->rcv = dst->tag_ops->rcv; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 479 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 480 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 481 | u32 index; |
| 482 | int err; |
| 483 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 484 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 485 | port = &ds->ports[index]; |
| 486 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 487 | continue; |
| 488 | |
| 489 | if (dsa_port_is_cpu(port)) { |
| 490 | err = dsa_cpu_parse(port, index, dst, ds); |
| 491 | if (err) |
| 492 | return err; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int dsa_dst_parse(struct dsa_switch_tree *dst) |
| 502 | { |
| 503 | struct dsa_switch *ds; |
| 504 | u32 index; |
| 505 | int err; |
| 506 | |
| 507 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 508 | ds = dst->ds[index]; |
| 509 | if (!ds) |
| 510 | continue; |
| 511 | |
| 512 | err = dsa_ds_parse(dst, ds); |
| 513 | if (err) |
| 514 | return err; |
| 515 | } |
| 516 | |
| 517 | if (!dst->master_netdev) { |
| 518 | pr_warn("Tree has no master device\n"); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | |
| 522 | pr_info("DSA: tree %d parsed\n", dst->tree); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds) |
| 528 | { |
| 529 | struct device_node *port; |
| 530 | int err; |
| 531 | u32 reg; |
| 532 | |
| 533 | for_each_available_child_of_node(ports, port) { |
| 534 | err = of_property_read_u32(port, "reg", ®); |
| 535 | if (err) |
| 536 | return err; |
| 537 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 538 | if (reg >= ds->num_ports) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | |
| 541 | ds->ports[reg].dn = port; |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 542 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 543 | /* Initialize enabled_port_mask now for ops->setup() |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 544 | * to have access to a correct value, just like what |
| 545 | * net/dsa/dsa.c::dsa_switch_setup_one does. |
| 546 | */ |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 547 | if (!dsa_port_is_cpu(&ds->ports[reg])) |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 548 | ds->enabled_port_mask |= 1 << reg; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 554 | 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] | 555 | { |
| 556 | int err; |
| 557 | |
| 558 | *tree = *index = 0; |
| 559 | |
| 560 | err = of_property_read_u32_index(np, "dsa,member", 0, tree); |
| 561 | if (err) { |
| 562 | /* Does not exist, but it is optional */ |
| 563 | if (err == -EINVAL) |
| 564 | return 0; |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | err = of_property_read_u32_index(np, "dsa,member", 1, index); |
| 569 | if (err) |
| 570 | return err; |
| 571 | |
| 572 | if (*index >= DSA_MAX_SWITCHES) |
| 573 | return -EINVAL; |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static struct device_node *dsa_get_ports(struct dsa_switch *ds, |
| 579 | struct device_node *np) |
| 580 | { |
| 581 | struct device_node *ports; |
| 582 | |
| 583 | ports = of_get_child_by_name(np, "ports"); |
| 584 | if (!ports) { |
| 585 | dev_err(ds->dev, "no ports child node found\n"); |
| 586 | return ERR_PTR(-EINVAL); |
| 587 | } |
| 588 | |
| 589 | return ports; |
| 590 | } |
| 591 | |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 592 | static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 593 | { |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 594 | struct device_node *np = dev->of_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 595 | struct dsa_switch_tree *dst; |
Florian Fainelli | bc1727d | 2017-01-26 10:45:54 -0800 | [diff] [blame] | 596 | struct device_node *ports; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 597 | u32 tree, index; |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 598 | int i, err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 599 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 600 | err = dsa_parse_member_dn(np, &tree, &index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 601 | if (err) |
| 602 | return err; |
| 603 | |
Florian Fainelli | bc1727d | 2017-01-26 10:45:54 -0800 | [diff] [blame] | 604 | ports = dsa_get_ports(ds, np); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 605 | if (IS_ERR(ports)) |
| 606 | return PTR_ERR(ports); |
| 607 | |
| 608 | err = dsa_parse_ports_dn(ports, ds); |
| 609 | if (err) |
| 610 | return err; |
| 611 | |
| 612 | dst = dsa_get_dst(tree); |
| 613 | if (!dst) { |
| 614 | dst = dsa_add_dst(tree); |
| 615 | if (!dst) |
| 616 | return -ENOMEM; |
| 617 | } |
| 618 | |
| 619 | if (dst->ds[index]) { |
| 620 | err = -EBUSY; |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | ds->dst = dst; |
| 625 | ds->index = index; |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 626 | |
| 627 | /* Initialize the routing table */ |
| 628 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) |
| 629 | ds->rtable[i] = DSA_RTABLE_NONE; |
| 630 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 631 | dsa_dst_add_ds(dst, ds, index); |
| 632 | |
| 633 | err = dsa_dst_complete(dst); |
| 634 | if (err < 0) |
| 635 | goto out_del_dst; |
| 636 | |
| 637 | if (err == 1) { |
| 638 | /* Not all switches registered yet */ |
| 639 | err = 0; |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | if (dst->applied) { |
| 644 | pr_info("DSA: Disjoint trees?\n"); |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | err = dsa_dst_parse(dst); |
Volodymyr Bendiuga | 5e6eb45 | 2017-01-05 11:10:13 +0100 | [diff] [blame] | 649 | if (err) { |
| 650 | if (err == -EPROBE_DEFER) { |
| 651 | dsa_dst_del_ds(dst, ds, ds->index); |
| 652 | return err; |
| 653 | } |
| 654 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 655 | goto out_del_dst; |
Volodymyr Bendiuga | 5e6eb45 | 2017-01-05 11:10:13 +0100 | [diff] [blame] | 656 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 657 | |
| 658 | err = dsa_dst_apply(dst); |
| 659 | if (err) { |
| 660 | dsa_dst_unapply(dst); |
| 661 | goto out_del_dst; |
| 662 | } |
| 663 | |
| 664 | dsa_put_dst(dst); |
| 665 | return 0; |
| 666 | |
| 667 | out_del_dst: |
| 668 | dsa_dst_del_ds(dst, ds, ds->index); |
| 669 | out: |
| 670 | dsa_put_dst(dst); |
| 671 | |
| 672 | return err; |
| 673 | } |
| 674 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 675 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
| 676 | { |
| 677 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); |
| 678 | struct dsa_switch *ds; |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame] | 679 | int i; |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 680 | |
| 681 | ds = devm_kzalloc(dev, size, GFP_KERNEL); |
| 682 | if (!ds) |
| 683 | return NULL; |
| 684 | |
| 685 | ds->dev = dev; |
| 686 | ds->num_ports = n; |
| 687 | |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame] | 688 | for (i = 0; i < ds->num_ports; ++i) { |
| 689 | ds->ports[i].index = i; |
| 690 | ds->ports[i].ds = ds; |
| 691 | } |
| 692 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 693 | return ds; |
| 694 | } |
| 695 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); |
| 696 | |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 697 | int dsa_register_switch(struct dsa_switch *ds, struct device *dev) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 698 | { |
| 699 | int err; |
| 700 | |
| 701 | mutex_lock(&dsa2_mutex); |
Florian Fainelli | 55ed0ce | 2017-01-26 10:45:51 -0800 | [diff] [blame] | 702 | err = _dsa_register_switch(ds, dev); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 703 | mutex_unlock(&dsa2_mutex); |
| 704 | |
| 705 | return err; |
| 706 | } |
| 707 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 708 | |
Wei Yongjun | 85c22ba | 2016-07-12 15:24:10 +0000 | [diff] [blame] | 709 | static void _dsa_unregister_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 710 | { |
| 711 | struct dsa_switch_tree *dst = ds->dst; |
| 712 | |
| 713 | dsa_dst_unapply(dst); |
| 714 | |
| 715 | dsa_dst_del_ds(dst, ds, ds->index); |
| 716 | } |
| 717 | |
| 718 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 719 | { |
| 720 | mutex_lock(&dsa2_mutex); |
| 721 | _dsa_unregister_switch(ds); |
| 722 | mutex_unlock(&dsa2_mutex); |
| 723 | } |
| 724 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |