Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.13-rc2 771 lines 17 kB view raw
1/* 2 * net/dsa/legacy.c - Hardware switch handling 3 * Copyright (c) 2008-2009 Marvell Semiconductor 4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12#include <linux/device.h> 13#include <linux/list.h> 14#include <linux/platform_device.h> 15#include <linux/slab.h> 16#include <linux/module.h> 17#include <linux/of.h> 18#include <linux/of_mdio.h> 19#include <linux/of_platform.h> 20#include <linux/of_net.h> 21#include <linux/netdevice.h> 22#include <linux/sysfs.h> 23#include <linux/phy_fixed.h> 24#include <linux/etherdevice.h> 25 26#include "dsa_priv.h" 27 28/* switch driver registration ***********************************************/ 29static DEFINE_MUTEX(dsa_switch_drivers_mutex); 30static LIST_HEAD(dsa_switch_drivers); 31 32void register_switch_driver(struct dsa_switch_driver *drv) 33{ 34 mutex_lock(&dsa_switch_drivers_mutex); 35 list_add_tail(&drv->list, &dsa_switch_drivers); 36 mutex_unlock(&dsa_switch_drivers_mutex); 37} 38EXPORT_SYMBOL_GPL(register_switch_driver); 39 40void unregister_switch_driver(struct dsa_switch_driver *drv) 41{ 42 mutex_lock(&dsa_switch_drivers_mutex); 43 list_del_init(&drv->list); 44 mutex_unlock(&dsa_switch_drivers_mutex); 45} 46EXPORT_SYMBOL_GPL(unregister_switch_driver); 47 48static const struct dsa_switch_ops * 49dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr, 50 const char **_name, void **priv) 51{ 52 const struct dsa_switch_ops *ret; 53 struct list_head *list; 54 const char *name; 55 56 ret = NULL; 57 name = NULL; 58 59 mutex_lock(&dsa_switch_drivers_mutex); 60 list_for_each(list, &dsa_switch_drivers) { 61 const struct dsa_switch_ops *ops; 62 struct dsa_switch_driver *drv; 63 64 drv = list_entry(list, struct dsa_switch_driver, list); 65 ops = drv->ops; 66 67 name = ops->probe(parent, host_dev, sw_addr, priv); 68 if (name != NULL) { 69 ret = ops; 70 break; 71 } 72 } 73 mutex_unlock(&dsa_switch_drivers_mutex); 74 75 *_name = name; 76 77 return ret; 78} 79 80/* basic switch operations **************************************************/ 81static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev) 82{ 83 struct dsa_port *dport; 84 int ret, port; 85 86 for (port = 0; port < ds->num_ports; port++) { 87 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) 88 continue; 89 90 dport = &ds->ports[port]; 91 ret = dsa_cpu_dsa_setup(ds, dev, dport, port); 92 if (ret) 93 return ret; 94 } 95 return 0; 96} 97 98static int dsa_switch_setup_one(struct dsa_switch *ds, struct net_device *master, 99 struct device *parent) 100{ 101 const struct dsa_switch_ops *ops = ds->ops; 102 struct dsa_switch_tree *dst = ds->dst; 103 struct dsa_chip_data *cd = ds->cd; 104 bool valid_name_found = false; 105 int index = ds->index; 106 int i, ret; 107 108 /* 109 * Validate supplied switch configuration. 110 */ 111 for (i = 0; i < ds->num_ports; i++) { 112 char *name; 113 114 name = cd->port_names[i]; 115 if (name == NULL) 116 continue; 117 118 if (!strcmp(name, "cpu")) { 119 if (dst->cpu_dp) { 120 netdev_err(master, 121 "multiple cpu ports?!\n"); 122 return -EINVAL; 123 } 124 dst->cpu_dp = &ds->ports[i]; 125 dst->cpu_dp->netdev = master; 126 ds->cpu_port_mask |= 1 << i; 127 } else if (!strcmp(name, "dsa")) { 128 ds->dsa_port_mask |= 1 << i; 129 } else { 130 ds->enabled_port_mask |= 1 << i; 131 } 132 valid_name_found = true; 133 } 134 135 if (!valid_name_found && i == ds->num_ports) 136 return -EINVAL; 137 138 /* Make the built-in MII bus mask match the number of ports, 139 * switch drivers can override this later 140 */ 141 ds->phys_mii_mask = ds->enabled_port_mask; 142 143 /* 144 * If the CPU connects to this switch, set the switch tree 145 * tagging protocol to the preferred tagging format of this 146 * switch. 147 */ 148 if (dst->cpu_dp->ds == ds) { 149 enum dsa_tag_protocol tag_protocol; 150 151 tag_protocol = ops->get_tag_protocol(ds); 152 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); 153 if (IS_ERR(dst->tag_ops)) 154 return PTR_ERR(dst->tag_ops); 155 156 dst->rcv = dst->tag_ops->rcv; 157 } 158 159 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable)); 160 161 /* 162 * Do basic register setup. 163 */ 164 ret = ops->setup(ds); 165 if (ret < 0) 166 return ret; 167 168 ret = dsa_switch_register_notifier(ds); 169 if (ret) 170 return ret; 171 172 if (ops->set_addr) { 173 ret = ops->set_addr(ds, master->dev_addr); 174 if (ret < 0) 175 return ret; 176 } 177 178 if (!ds->slave_mii_bus && ops->phy_read) { 179 ds->slave_mii_bus = devm_mdiobus_alloc(parent); 180 if (!ds->slave_mii_bus) 181 return -ENOMEM; 182 dsa_slave_mii_bus_init(ds); 183 184 ret = mdiobus_register(ds->slave_mii_bus); 185 if (ret < 0) 186 return ret; 187 } 188 189 /* 190 * Create network devices for physical switch ports. 191 */ 192 for (i = 0; i < ds->num_ports; i++) { 193 ds->ports[i].dn = cd->port_dn[i]; 194 ds->ports[i].cpu_dp = dst->cpu_dp; 195 196 if (!(ds->enabled_port_mask & (1 << i))) 197 continue; 198 199 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]); 200 if (ret < 0) 201 netdev_err(master, "[%d]: can't create dsa slave device for port %d(%s): %d\n", 202 index, i, cd->port_names[i], ret); 203 } 204 205 /* Perform configuration of the CPU and DSA ports */ 206 ret = dsa_cpu_dsa_setups(ds, parent); 207 if (ret < 0) 208 netdev_err(master, "[%d] : can't configure CPU and DSA ports\n", 209 index); 210 211 ret = dsa_cpu_port_ethtool_setup(ds->dst->cpu_dp); 212 if (ret) 213 return ret; 214 215 return 0; 216} 217 218static struct dsa_switch * 219dsa_switch_setup(struct dsa_switch_tree *dst, struct net_device *master, 220 int index, struct device *parent, struct device *host_dev) 221{ 222 struct dsa_chip_data *cd = dst->pd->chip + index; 223 const struct dsa_switch_ops *ops; 224 struct dsa_switch *ds; 225 int ret; 226 const char *name; 227 void *priv; 228 229 /* 230 * Probe for switch model. 231 */ 232 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv); 233 if (!ops) { 234 netdev_err(master, "[%d]: could not detect attached switch\n", 235 index); 236 return ERR_PTR(-EINVAL); 237 } 238 netdev_info(master, "[%d]: detected a %s switch\n", 239 index, name); 240 241 242 /* 243 * Allocate and initialise switch state. 244 */ 245 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS); 246 if (!ds) 247 return ERR_PTR(-ENOMEM); 248 249 ds->dst = dst; 250 ds->index = index; 251 ds->cd = cd; 252 ds->ops = ops; 253 ds->priv = priv; 254 255 ret = dsa_switch_setup_one(ds, master, parent); 256 if (ret) 257 return ERR_PTR(ret); 258 259 return ds; 260} 261 262static void dsa_switch_destroy(struct dsa_switch *ds) 263{ 264 int port; 265 266 /* Destroy network devices for physical switch ports. */ 267 for (port = 0; port < ds->num_ports; port++) { 268 if (!(ds->enabled_port_mask & (1 << port))) 269 continue; 270 271 if (!ds->ports[port].netdev) 272 continue; 273 274 dsa_slave_destroy(ds->ports[port].netdev); 275 } 276 277 /* Disable configuration of the CPU and DSA ports */ 278 for (port = 0; port < ds->num_ports; port++) { 279 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) 280 continue; 281 dsa_cpu_dsa_destroy(&ds->ports[port]); 282 283 /* Clearing a bit which is not set does no harm */ 284 ds->cpu_port_mask |= ~(1 << port); 285 ds->dsa_port_mask |= ~(1 << port); 286 } 287 288 if (ds->slave_mii_bus && ds->ops->phy_read) 289 mdiobus_unregister(ds->slave_mii_bus); 290 291 dsa_switch_unregister_notifier(ds); 292} 293 294/* platform driver init and cleanup *****************************************/ 295static int dev_is_class(struct device *dev, void *class) 296{ 297 if (dev->class != NULL && !strcmp(dev->class->name, class)) 298 return 1; 299 300 return 0; 301} 302 303static struct device *dev_find_class(struct device *parent, char *class) 304{ 305 if (dev_is_class(parent, class)) { 306 get_device(parent); 307 return parent; 308 } 309 310 return device_find_child(parent, class, dev_is_class); 311} 312 313struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) 314{ 315 struct device *d; 316 317 d = dev_find_class(dev, "mdio_bus"); 318 if (d != NULL) { 319 struct mii_bus *bus; 320 321 bus = to_mii_bus(d); 322 put_device(d); 323 324 return bus; 325 } 326 327 return NULL; 328} 329EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); 330 331#ifdef CONFIG_OF 332static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, 333 struct dsa_chip_data *cd, 334 int chip_index, int port_index, 335 struct device_node *link) 336{ 337 const __be32 *reg; 338 int link_sw_addr; 339 struct device_node *parent_sw; 340 int len; 341 342 parent_sw = of_get_parent(link); 343 if (!parent_sw) 344 return -EINVAL; 345 346 reg = of_get_property(parent_sw, "reg", &len); 347 if (!reg || (len != sizeof(*reg) * 2)) 348 return -EINVAL; 349 350 /* 351 * Get the destination switch number from the second field of its 'reg' 352 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'. 353 */ 354 link_sw_addr = be32_to_cpup(reg + 1); 355 356 if (link_sw_addr >= pd->nr_chips) 357 return -EINVAL; 358 359 cd->rtable[link_sw_addr] = port_index; 360 361 return 0; 362} 363 364static int dsa_of_probe_links(struct dsa_platform_data *pd, 365 struct dsa_chip_data *cd, 366 int chip_index, int port_index, 367 struct device_node *port, 368 const char *port_name) 369{ 370 struct device_node *link; 371 int link_index; 372 int ret; 373 374 for (link_index = 0;; link_index++) { 375 link = of_parse_phandle(port, "link", link_index); 376 if (!link) 377 break; 378 379 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) { 380 ret = dsa_of_setup_routing_table(pd, cd, chip_index, 381 port_index, link); 382 if (ret) 383 return ret; 384 } 385 } 386 return 0; 387} 388 389static void dsa_of_free_platform_data(struct dsa_platform_data *pd) 390{ 391 int i; 392 int port_index; 393 394 for (i = 0; i < pd->nr_chips; i++) { 395 port_index = 0; 396 while (port_index < DSA_MAX_PORTS) { 397 kfree(pd->chip[i].port_names[port_index]); 398 port_index++; 399 } 400 401 /* Drop our reference to the MDIO bus device */ 402 if (pd->chip[i].host_dev) 403 put_device(pd->chip[i].host_dev); 404 } 405 kfree(pd->chip); 406} 407 408static int dsa_of_probe(struct device *dev) 409{ 410 struct device_node *np = dev->of_node; 411 struct device_node *child, *mdio, *ethernet, *port; 412 struct mii_bus *mdio_bus, *mdio_bus_switch; 413 struct net_device *ethernet_dev; 414 struct dsa_platform_data *pd; 415 struct dsa_chip_data *cd; 416 const char *port_name; 417 int chip_index, port_index; 418 const unsigned int *sw_addr, *port_reg; 419 u32 eeprom_len; 420 int ret; 421 422 mdio = of_parse_phandle(np, "dsa,mii-bus", 0); 423 if (!mdio) 424 return -EINVAL; 425 426 mdio_bus = of_mdio_find_bus(mdio); 427 if (!mdio_bus) 428 return -EPROBE_DEFER; 429 430 ethernet = of_parse_phandle(np, "dsa,ethernet", 0); 431 if (!ethernet) { 432 ret = -EINVAL; 433 goto out_put_mdio; 434 } 435 436 ethernet_dev = of_find_net_device_by_node(ethernet); 437 if (!ethernet_dev) { 438 ret = -EPROBE_DEFER; 439 goto out_put_mdio; 440 } 441 442 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 443 if (!pd) { 444 ret = -ENOMEM; 445 goto out_put_ethernet; 446 } 447 448 dev->platform_data = pd; 449 pd->of_netdev = ethernet_dev; 450 pd->nr_chips = of_get_available_child_count(np); 451 if (pd->nr_chips > DSA_MAX_SWITCHES) 452 pd->nr_chips = DSA_MAX_SWITCHES; 453 454 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), 455 GFP_KERNEL); 456 if (!pd->chip) { 457 ret = -ENOMEM; 458 goto out_free; 459 } 460 461 chip_index = -1; 462 for_each_available_child_of_node(np, child) { 463 int i; 464 465 chip_index++; 466 cd = &pd->chip[chip_index]; 467 468 cd->of_node = child; 469 470 /* Initialize the routing table */ 471 for (i = 0; i < DSA_MAX_SWITCHES; ++i) 472 cd->rtable[i] = DSA_RTABLE_NONE; 473 474 /* When assigning the host device, increment its refcount */ 475 cd->host_dev = get_device(&mdio_bus->dev); 476 477 sw_addr = of_get_property(child, "reg", NULL); 478 if (!sw_addr) 479 continue; 480 481 cd->sw_addr = be32_to_cpup(sw_addr); 482 if (cd->sw_addr >= PHY_MAX_ADDR) 483 continue; 484 485 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len)) 486 cd->eeprom_len = eeprom_len; 487 488 mdio = of_parse_phandle(child, "mii-bus", 0); 489 if (mdio) { 490 mdio_bus_switch = of_mdio_find_bus(mdio); 491 if (!mdio_bus_switch) { 492 ret = -EPROBE_DEFER; 493 goto out_free_chip; 494 } 495 496 /* Drop the mdio_bus device ref, replacing the host 497 * device with the mdio_bus_switch device, keeping 498 * the refcount from of_mdio_find_bus() above. 499 */ 500 put_device(cd->host_dev); 501 cd->host_dev = &mdio_bus_switch->dev; 502 } 503 504 for_each_available_child_of_node(child, port) { 505 port_reg = of_get_property(port, "reg", NULL); 506 if (!port_reg) 507 continue; 508 509 port_index = be32_to_cpup(port_reg); 510 if (port_index >= DSA_MAX_PORTS) 511 break; 512 513 port_name = of_get_property(port, "label", NULL); 514 if (!port_name) 515 continue; 516 517 cd->port_dn[port_index] = port; 518 519 cd->port_names[port_index] = kstrdup(port_name, 520 GFP_KERNEL); 521 if (!cd->port_names[port_index]) { 522 ret = -ENOMEM; 523 goto out_free_chip; 524 } 525 526 ret = dsa_of_probe_links(pd, cd, chip_index, 527 port_index, port, port_name); 528 if (ret) 529 goto out_free_chip; 530 531 } 532 } 533 534 /* The individual chips hold their own refcount on the mdio bus, 535 * so drop ours */ 536 put_device(&mdio_bus->dev); 537 538 return 0; 539 540out_free_chip: 541 dsa_of_free_platform_data(pd); 542out_free: 543 kfree(pd); 544 dev->platform_data = NULL; 545out_put_ethernet: 546 put_device(&ethernet_dev->dev); 547out_put_mdio: 548 put_device(&mdio_bus->dev); 549 return ret; 550} 551 552static void dsa_of_remove(struct device *dev) 553{ 554 struct dsa_platform_data *pd = dev->platform_data; 555 556 if (!dev->of_node) 557 return; 558 559 dsa_of_free_platform_data(pd); 560 put_device(&pd->of_netdev->dev); 561 kfree(pd); 562} 563#else 564static inline int dsa_of_probe(struct device *dev) 565{ 566 return 0; 567} 568 569static inline void dsa_of_remove(struct device *dev) 570{ 571} 572#endif 573 574static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev, 575 struct device *parent, struct dsa_platform_data *pd) 576{ 577 int i; 578 unsigned configured = 0; 579 580 dst->pd = pd; 581 582 for (i = 0; i < pd->nr_chips; i++) { 583 struct dsa_switch *ds; 584 585 ds = dsa_switch_setup(dst, dev, i, parent, pd->chip[i].host_dev); 586 if (IS_ERR(ds)) { 587 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", 588 i, PTR_ERR(ds)); 589 continue; 590 } 591 592 dst->ds[i] = ds; 593 594 ++configured; 595 } 596 597 /* 598 * If no switch was found, exit cleanly 599 */ 600 if (!configured) 601 return -EPROBE_DEFER; 602 603 /* 604 * If we use a tagging format that doesn't have an ethertype 605 * field, make sure that all packets from this point on get 606 * sent to the tag format's receive function. 607 */ 608 wmb(); 609 dev->dsa_ptr = dst; 610 611 return 0; 612} 613 614static int dsa_probe(struct platform_device *pdev) 615{ 616 struct dsa_platform_data *pd = pdev->dev.platform_data; 617 struct net_device *dev; 618 struct dsa_switch_tree *dst; 619 int ret; 620 621 if (pdev->dev.of_node) { 622 ret = dsa_of_probe(&pdev->dev); 623 if (ret) 624 return ret; 625 626 pd = pdev->dev.platform_data; 627 } 628 629 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL)) 630 return -EINVAL; 631 632 if (pd->of_netdev) { 633 dev = pd->of_netdev; 634 dev_hold(dev); 635 } else { 636 dev = dsa_dev_to_net_device(pd->netdev); 637 } 638 if (dev == NULL) { 639 ret = -EPROBE_DEFER; 640 goto out; 641 } 642 643 if (dev->dsa_ptr != NULL) { 644 dev_put(dev); 645 ret = -EEXIST; 646 goto out; 647 } 648 649 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL); 650 if (dst == NULL) { 651 dev_put(dev); 652 ret = -ENOMEM; 653 goto out; 654 } 655 656 platform_set_drvdata(pdev, dst); 657 658 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd); 659 if (ret) { 660 dev_put(dev); 661 goto out; 662 } 663 664 return 0; 665 666out: 667 dsa_of_remove(&pdev->dev); 668 669 return ret; 670} 671 672static void dsa_remove_dst(struct dsa_switch_tree *dst) 673{ 674 int i; 675 676 dst->cpu_dp->netdev->dsa_ptr = NULL; 677 678 /* If we used a tagging format that doesn't have an ethertype 679 * field, make sure that all packets from this point get sent 680 * without the tag and go through the regular receive path. 681 */ 682 wmb(); 683 684 for (i = 0; i < dst->pd->nr_chips; i++) { 685 struct dsa_switch *ds = dst->ds[i]; 686 687 if (ds) 688 dsa_switch_destroy(ds); 689 } 690 691 dsa_cpu_port_ethtool_restore(dst->cpu_dp); 692 693 dev_put(dst->cpu_dp->netdev); 694} 695 696static int dsa_remove(struct platform_device *pdev) 697{ 698 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 699 700 dsa_remove_dst(dst); 701 dsa_of_remove(&pdev->dev); 702 703 return 0; 704} 705 706static void dsa_shutdown(struct platform_device *pdev) 707{ 708} 709 710#ifdef CONFIG_PM_SLEEP 711static int dsa_suspend(struct device *d) 712{ 713 struct platform_device *pdev = to_platform_device(d); 714 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 715 int i, ret = 0; 716 717 for (i = 0; i < dst->pd->nr_chips; i++) { 718 struct dsa_switch *ds = dst->ds[i]; 719 720 if (ds != NULL) 721 ret = dsa_switch_suspend(ds); 722 } 723 724 return ret; 725} 726 727static int dsa_resume(struct device *d) 728{ 729 struct platform_device *pdev = to_platform_device(d); 730 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 731 int i, ret = 0; 732 733 for (i = 0; i < dst->pd->nr_chips; i++) { 734 struct dsa_switch *ds = dst->ds[i]; 735 736 if (ds != NULL) 737 ret = dsa_switch_resume(ds); 738 } 739 740 return ret; 741} 742#endif 743 744static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); 745 746static const struct of_device_id dsa_of_match_table[] = { 747 { .compatible = "marvell,dsa", }, 748 {} 749}; 750MODULE_DEVICE_TABLE(of, dsa_of_match_table); 751 752static struct platform_driver dsa_driver = { 753 .probe = dsa_probe, 754 .remove = dsa_remove, 755 .shutdown = dsa_shutdown, 756 .driver = { 757 .name = "dsa", 758 .of_match_table = dsa_of_match_table, 759 .pm = &dsa_pm_ops, 760 }, 761}; 762 763int dsa_legacy_register(void) 764{ 765 return platform_driver_register(&dsa_driver); 766} 767 768void dsa_legacy_unregister(void) 769{ 770 platform_driver_unregister(&dsa_driver); 771}