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