1 /* 2 * net/dsa/dsa.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/ctype.h> 13 #include <linux/device.h> 14 #include <linux/hwmon.h> 15 #include <linux/list.h> 16 #include <linux/platform_device.h> 17 #include <linux/slab.h> 18 #include <linux/module.h> 19 #include <net/dsa.h> 20 #include <linux/of.h> 21 #include <linux/of_mdio.h> 22 #include <linux/of_platform.h> 23 #include <linux/sysfs.h> 24 #include "dsa_priv.h" 25 26 char dsa_driver_version[] = "0.1"; 27 28 29 /* switch driver registration ***********************************************/ 30 static DEFINE_MUTEX(dsa_switch_drivers_mutex); 31 static LIST_HEAD(dsa_switch_drivers); 32 33 void register_switch_driver(struct dsa_switch_driver *drv) 34 { 35 mutex_lock(&dsa_switch_drivers_mutex); 36 list_add_tail(&drv->list, &dsa_switch_drivers); 37 mutex_unlock(&dsa_switch_drivers_mutex); 38 } 39 EXPORT_SYMBOL_GPL(register_switch_driver); 40 41 void unregister_switch_driver(struct dsa_switch_driver *drv) 42 { 43 mutex_lock(&dsa_switch_drivers_mutex); 44 list_del_init(&drv->list); 45 mutex_unlock(&dsa_switch_drivers_mutex); 46 } 47 EXPORT_SYMBOL_GPL(unregister_switch_driver); 48 49 static struct dsa_switch_driver * 50 dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name) 51 { 52 struct dsa_switch_driver *ret; 53 struct list_head *list; 54 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 struct dsa_switch_driver *drv; 62 63 drv = list_entry(list, struct dsa_switch_driver, list); 64 65 name = drv->probe(host_dev, sw_addr); 66 if (name != NULL) { 67 ret = drv; 68 break; 69 } 70 } 71 mutex_unlock(&dsa_switch_drivers_mutex); 72 73 *_name = name; 74 75 return ret; 76 } 77 78 /* hwmon support ************************************************************/ 79 80 #ifdef CONFIG_NET_DSA_HWMON 81 82 static ssize_t temp1_input_show(struct device *dev, 83 struct device_attribute *attr, char *buf) 84 { 85 struct dsa_switch *ds = dev_get_drvdata(dev); 86 int temp, ret; 87 88 ret = ds->drv->get_temp(ds, &temp); 89 if (ret < 0) 90 return ret; 91 92 return sprintf(buf, "%d\n", temp * 1000); 93 } 94 static DEVICE_ATTR_RO(temp1_input); 95 96 static ssize_t temp1_max_show(struct device *dev, 97 struct device_attribute *attr, char *buf) 98 { 99 struct dsa_switch *ds = dev_get_drvdata(dev); 100 int temp, ret; 101 102 ret = ds->drv->get_temp_limit(ds, &temp); 103 if (ret < 0) 104 return ret; 105 106 return sprintf(buf, "%d\n", temp * 1000); 107 } 108 109 static ssize_t temp1_max_store(struct device *dev, 110 struct device_attribute *attr, const char *buf, 111 size_t count) 112 { 113 struct dsa_switch *ds = dev_get_drvdata(dev); 114 int temp, ret; 115 116 ret = kstrtoint(buf, 0, &temp); 117 if (ret < 0) 118 return ret; 119 120 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000)); 121 if (ret < 0) 122 return ret; 123 124 return count; 125 } 126 static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store); 127 128 static ssize_t temp1_max_alarm_show(struct device *dev, 129 struct device_attribute *attr, char *buf) 130 { 131 struct dsa_switch *ds = dev_get_drvdata(dev); 132 bool alarm; 133 int ret; 134 135 ret = ds->drv->get_temp_alarm(ds, &alarm); 136 if (ret < 0) 137 return ret; 138 139 return sprintf(buf, "%d\n", alarm); 140 } 141 static DEVICE_ATTR_RO(temp1_max_alarm); 142 143 static struct attribute *dsa_hwmon_attrs[] = { 144 &dev_attr_temp1_input.attr, /* 0 */ 145 &dev_attr_temp1_max.attr, /* 1 */ 146 &dev_attr_temp1_max_alarm.attr, /* 2 */ 147 NULL 148 }; 149 150 static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj, 151 struct attribute *attr, int index) 152 { 153 struct device *dev = container_of(kobj, struct device, kobj); 154 struct dsa_switch *ds = dev_get_drvdata(dev); 155 struct dsa_switch_driver *drv = ds->drv; 156 umode_t mode = attr->mode; 157 158 if (index == 1) { 159 if (!drv->get_temp_limit) 160 mode = 0; 161 else if (drv->set_temp_limit) 162 mode |= S_IWUSR; 163 } else if (index == 2 && !drv->get_temp_alarm) { 164 mode = 0; 165 } 166 return mode; 167 } 168 169 static const struct attribute_group dsa_hwmon_group = { 170 .attrs = dsa_hwmon_attrs, 171 .is_visible = dsa_hwmon_attrs_visible, 172 }; 173 __ATTRIBUTE_GROUPS(dsa_hwmon); 174 175 #endif /* CONFIG_NET_DSA_HWMON */ 176 177 /* basic switch operations **************************************************/ 178 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent) 179 { 180 struct dsa_switch_driver *drv = ds->drv; 181 struct dsa_switch_tree *dst = ds->dst; 182 struct dsa_chip_data *pd = ds->pd; 183 bool valid_name_found = false; 184 int index = ds->index; 185 int i, ret; 186 187 /* 188 * Validate supplied switch configuration. 189 */ 190 for (i = 0; i < DSA_MAX_PORTS; i++) { 191 char *name; 192 193 name = pd->port_names[i]; 194 if (name == NULL) 195 continue; 196 197 if (!strcmp(name, "cpu")) { 198 if (dst->cpu_switch != -1) { 199 netdev_err(dst->master_netdev, 200 "multiple cpu ports?!\n"); 201 ret = -EINVAL; 202 goto out; 203 } 204 dst->cpu_switch = index; 205 dst->cpu_port = i; 206 } else if (!strcmp(name, "dsa")) { 207 ds->dsa_port_mask |= 1 << i; 208 } else { 209 ds->phys_port_mask |= 1 << i; 210 } 211 valid_name_found = true; 212 } 213 214 if (!valid_name_found && i == DSA_MAX_PORTS) { 215 ret = -EINVAL; 216 goto out; 217 } 218 219 /* Make the built-in MII bus mask match the number of ports, 220 * switch drivers can override this later 221 */ 222 ds->phys_mii_mask = ds->phys_port_mask; 223 224 /* 225 * If the CPU connects to this switch, set the switch tree 226 * tagging protocol to the preferred tagging format of this 227 * switch. 228 */ 229 if (dst->cpu_switch == index) { 230 switch (ds->tag_protocol) { 231 #ifdef CONFIG_NET_DSA_TAG_DSA 232 case DSA_TAG_PROTO_DSA: 233 dst->rcv = dsa_netdev_ops.rcv; 234 break; 235 #endif 236 #ifdef CONFIG_NET_DSA_TAG_EDSA 237 case DSA_TAG_PROTO_EDSA: 238 dst->rcv = edsa_netdev_ops.rcv; 239 break; 240 #endif 241 #ifdef CONFIG_NET_DSA_TAG_TRAILER 242 case DSA_TAG_PROTO_TRAILER: 243 dst->rcv = trailer_netdev_ops.rcv; 244 break; 245 #endif 246 #ifdef CONFIG_NET_DSA_TAG_BRCM 247 case DSA_TAG_PROTO_BRCM: 248 dst->rcv = brcm_netdev_ops.rcv; 249 break; 250 #endif 251 case DSA_TAG_PROTO_NONE: 252 break; 253 default: 254 ret = -ENOPROTOOPT; 255 goto out; 256 } 257 258 dst->tag_protocol = ds->tag_protocol; 259 } 260 261 /* 262 * Do basic register setup. 263 */ 264 ret = drv->setup(ds); 265 if (ret < 0) 266 goto out; 267 268 ret = drv->set_addr(ds, dst->master_netdev->dev_addr); 269 if (ret < 0) 270 goto out; 271 272 ds->slave_mii_bus = mdiobus_alloc(); 273 if (ds->slave_mii_bus == NULL) { 274 ret = -ENOMEM; 275 goto out; 276 } 277 dsa_slave_mii_bus_init(ds); 278 279 ret = mdiobus_register(ds->slave_mii_bus); 280 if (ret < 0) 281 goto out_free; 282 283 284 /* 285 * Create network devices for physical switch ports. 286 */ 287 for (i = 0; i < DSA_MAX_PORTS; i++) { 288 if (!(ds->phys_port_mask & (1 << i))) 289 continue; 290 291 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]); 292 if (ret < 0) { 293 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n", 294 index, i, pd->port_names[i]); 295 ret = 0; 296 } 297 } 298 299 #ifdef CONFIG_NET_DSA_HWMON 300 /* If the switch provides a temperature sensor, 301 * register with hardware monitoring subsystem. 302 * Treat registration error as non-fatal and ignore it. 303 */ 304 if (drv->get_temp) { 305 const char *netname = netdev_name(dst->master_netdev); 306 char hname[IFNAMSIZ + 1]; 307 int i, j; 308 309 /* Create valid hwmon 'name' attribute */ 310 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) { 311 if (isalnum(netname[i])) 312 hname[j++] = netname[i]; 313 } 314 hname[j] = '\0'; 315 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d", 316 hname, index); 317 ds->hwmon_dev = hwmon_device_register_with_groups(NULL, 318 ds->hwmon_name, ds, dsa_hwmon_groups); 319 if (IS_ERR(ds->hwmon_dev)) 320 ds->hwmon_dev = NULL; 321 } 322 #endif /* CONFIG_NET_DSA_HWMON */ 323 324 return ret; 325 326 out_free: 327 mdiobus_free(ds->slave_mii_bus); 328 out: 329 kfree(ds); 330 return ret; 331 } 332 333 static struct dsa_switch * 334 dsa_switch_setup(struct dsa_switch_tree *dst, int index, 335 struct device *parent, struct device *host_dev) 336 { 337 struct dsa_chip_data *pd = dst->pd->chip + index; 338 struct dsa_switch_driver *drv; 339 struct dsa_switch *ds; 340 int ret; 341 char *name; 342 343 /* 344 * Probe for switch model. 345 */ 346 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); 347 if (drv == NULL) { 348 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", 349 index); 350 return ERR_PTR(-EINVAL); 351 } 352 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", 353 index, name); 354 355 356 /* 357 * Allocate and initialise switch state. 358 */ 359 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL); 360 if (ds == NULL) 361 return NULL; 362 363 ds->dst = dst; 364 ds->index = index; 365 ds->pd = pd; 366 ds->drv = drv; 367 ds->tag_protocol = drv->tag_protocol; 368 ds->master_dev = host_dev; 369 370 ret = dsa_switch_setup_one(ds, parent); 371 if (ret) 372 return NULL; 373 374 return ds; 375 } 376 377 static void dsa_switch_destroy(struct dsa_switch *ds) 378 { 379 #ifdef CONFIG_NET_DSA_HWMON 380 if (ds->hwmon_dev) 381 hwmon_device_unregister(ds->hwmon_dev); 382 #endif 383 } 384 385 #ifdef CONFIG_PM_SLEEP 386 static int dsa_switch_suspend(struct dsa_switch *ds) 387 { 388 int i, ret = 0; 389 390 /* Suspend slave network devices */ 391 for (i = 0; i < DSA_MAX_PORTS; i++) { 392 if (!dsa_is_port_initialized(ds, i)) 393 continue; 394 395 ret = dsa_slave_suspend(ds->ports[i]); 396 if (ret) 397 return ret; 398 } 399 400 if (ds->drv->suspend) 401 ret = ds->drv->suspend(ds); 402 403 return ret; 404 } 405 406 static int dsa_switch_resume(struct dsa_switch *ds) 407 { 408 int i, ret = 0; 409 410 if (ds->drv->resume) 411 ret = ds->drv->resume(ds); 412 413 if (ret) 414 return ret; 415 416 /* Resume slave network devices */ 417 for (i = 0; i < DSA_MAX_PORTS; i++) { 418 if (!dsa_is_port_initialized(ds, i)) 419 continue; 420 421 ret = dsa_slave_resume(ds->ports[i]); 422 if (ret) 423 return ret; 424 } 425 426 return 0; 427 } 428 #endif 429 430 431 /* link polling *************************************************************/ 432 static void dsa_link_poll_work(struct work_struct *ugly) 433 { 434 struct dsa_switch_tree *dst; 435 int i; 436 437 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); 438 439 for (i = 0; i < dst->pd->nr_chips; i++) { 440 struct dsa_switch *ds = dst->ds[i]; 441 442 if (ds != NULL && ds->drv->poll_link != NULL) 443 ds->drv->poll_link(ds); 444 } 445 446 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ)); 447 } 448 449 static void dsa_link_poll_timer(unsigned long _dst) 450 { 451 struct dsa_switch_tree *dst = (void *)_dst; 452 453 schedule_work(&dst->link_poll_work); 454 } 455 456 457 /* platform driver init and cleanup *****************************************/ 458 static int dev_is_class(struct device *dev, void *class) 459 { 460 if (dev->class != NULL && !strcmp(dev->class->name, class)) 461 return 1; 462 463 return 0; 464 } 465 466 static struct device *dev_find_class(struct device *parent, char *class) 467 { 468 if (dev_is_class(parent, class)) { 469 get_device(parent); 470 return parent; 471 } 472 473 return device_find_child(parent, class, dev_is_class); 474 } 475 476 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) 477 { 478 struct device *d; 479 480 d = dev_find_class(dev, "mdio_bus"); 481 if (d != NULL) { 482 struct mii_bus *bus; 483 484 bus = to_mii_bus(d); 485 put_device(d); 486 487 return bus; 488 } 489 490 return NULL; 491 } 492 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); 493 494 static struct net_device *dev_to_net_device(struct device *dev) 495 { 496 struct device *d; 497 498 d = dev_find_class(dev, "net"); 499 if (d != NULL) { 500 struct net_device *nd; 501 502 nd = to_net_dev(d); 503 dev_hold(nd); 504 put_device(d); 505 506 return nd; 507 } 508 509 return NULL; 510 } 511 512 #ifdef CONFIG_OF 513 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, 514 struct dsa_chip_data *cd, 515 int chip_index, 516 struct device_node *link) 517 { 518 int ret; 519 const __be32 *reg; 520 int link_port_addr; 521 int link_sw_addr; 522 struct device_node *parent_sw; 523 int len; 524 525 parent_sw = of_get_parent(link); 526 if (!parent_sw) 527 return -EINVAL; 528 529 reg = of_get_property(parent_sw, "reg", &len); 530 if (!reg || (len != sizeof(*reg) * 2)) 531 return -EINVAL; 532 533 link_sw_addr = be32_to_cpup(reg + 1); 534 535 if (link_sw_addr >= pd->nr_chips) 536 return -EINVAL; 537 538 /* First time routing table allocation */ 539 if (!cd->rtable) { 540 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8), 541 GFP_KERNEL); 542 if (!cd->rtable) 543 return -ENOMEM; 544 545 /* default to no valid uplink/downlink */ 546 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8)); 547 } 548 549 reg = of_get_property(link, "reg", NULL); 550 if (!reg) { 551 ret = -EINVAL; 552 goto out; 553 } 554 555 link_port_addr = be32_to_cpup(reg); 556 557 cd->rtable[link_sw_addr] = link_port_addr; 558 559 return 0; 560 out: 561 kfree(cd->rtable); 562 return ret; 563 } 564 565 static void dsa_of_free_platform_data(struct dsa_platform_data *pd) 566 { 567 int i; 568 int port_index; 569 570 for (i = 0; i < pd->nr_chips; i++) { 571 port_index = 0; 572 while (port_index < DSA_MAX_PORTS) { 573 kfree(pd->chip[i].port_names[port_index]); 574 port_index++; 575 } 576 kfree(pd->chip[i].rtable); 577 } 578 kfree(pd->chip); 579 } 580 581 static int dsa_of_probe(struct device *dev) 582 { 583 struct device_node *np = dev->of_node; 584 struct device_node *child, *mdio, *ethernet, *port, *link; 585 struct mii_bus *mdio_bus; 586 struct platform_device *ethernet_dev; 587 struct dsa_platform_data *pd; 588 struct dsa_chip_data *cd; 589 const char *port_name; 590 int chip_index, port_index; 591 const unsigned int *sw_addr, *port_reg; 592 u32 eeprom_len; 593 int ret; 594 595 mdio = of_parse_phandle(np, "dsa,mii-bus", 0); 596 if (!mdio) 597 return -EINVAL; 598 599 mdio_bus = of_mdio_find_bus(mdio); 600 if (!mdio_bus) 601 return -EPROBE_DEFER; 602 603 ethernet = of_parse_phandle(np, "dsa,ethernet", 0); 604 if (!ethernet) 605 return -EINVAL; 606 607 ethernet_dev = of_find_device_by_node(ethernet); 608 if (!ethernet_dev) 609 return -EPROBE_DEFER; 610 611 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 612 if (!pd) 613 return -ENOMEM; 614 615 dev->platform_data = pd; 616 pd->netdev = ðernet_dev->dev; 617 pd->nr_chips = of_get_available_child_count(np); 618 if (pd->nr_chips > DSA_MAX_SWITCHES) 619 pd->nr_chips = DSA_MAX_SWITCHES; 620 621 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), 622 GFP_KERNEL); 623 if (!pd->chip) { 624 ret = -ENOMEM; 625 goto out_free; 626 } 627 628 chip_index = -1; 629 for_each_available_child_of_node(np, child) { 630 chip_index++; 631 cd = &pd->chip[chip_index]; 632 633 cd->of_node = child; 634 cd->host_dev = &mdio_bus->dev; 635 636 sw_addr = of_get_property(child, "reg", NULL); 637 if (!sw_addr) 638 continue; 639 640 cd->sw_addr = be32_to_cpup(sw_addr); 641 if (cd->sw_addr > PHY_MAX_ADDR) 642 continue; 643 644 if (!of_property_read_u32(np, "eeprom-length", &eeprom_len)) 645 cd->eeprom_len = eeprom_len; 646 647 for_each_available_child_of_node(child, port) { 648 port_reg = of_get_property(port, "reg", NULL); 649 if (!port_reg) 650 continue; 651 652 port_index = be32_to_cpup(port_reg); 653 654 port_name = of_get_property(port, "label", NULL); 655 if (!port_name) 656 continue; 657 658 cd->port_dn[port_index] = port; 659 660 cd->port_names[port_index] = kstrdup(port_name, 661 GFP_KERNEL); 662 if (!cd->port_names[port_index]) { 663 ret = -ENOMEM; 664 goto out_free_chip; 665 } 666 667 link = of_parse_phandle(port, "link", 0); 668 669 if (!strcmp(port_name, "dsa") && link && 670 pd->nr_chips > 1) { 671 ret = dsa_of_setup_routing_table(pd, cd, 672 chip_index, link); 673 if (ret) 674 goto out_free_chip; 675 } 676 677 if (port_index == DSA_MAX_PORTS) 678 break; 679 } 680 } 681 682 return 0; 683 684 out_free_chip: 685 dsa_of_free_platform_data(pd); 686 out_free: 687 kfree(pd); 688 dev->platform_data = NULL; 689 return ret; 690 } 691 692 static void dsa_of_remove(struct device *dev) 693 { 694 struct dsa_platform_data *pd = dev->platform_data; 695 696 if (!dev->of_node) 697 return; 698 699 dsa_of_free_platform_data(pd); 700 kfree(pd); 701 } 702 #else 703 static inline int dsa_of_probe(struct device *dev) 704 { 705 return 0; 706 } 707 708 static inline void dsa_of_remove(struct device *dev) 709 { 710 } 711 #endif 712 713 static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev, 714 struct device *parent, struct dsa_platform_data *pd) 715 { 716 int i; 717 718 dst->pd = pd; 719 dst->master_netdev = dev; 720 dst->cpu_switch = -1; 721 dst->cpu_port = -1; 722 723 for (i = 0; i < pd->nr_chips; i++) { 724 struct dsa_switch *ds; 725 726 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev); 727 if (IS_ERR(ds)) { 728 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", 729 i, PTR_ERR(ds)); 730 continue; 731 } 732 733 dst->ds[i] = ds; 734 if (ds->drv->poll_link != NULL) 735 dst->link_poll_needed = 1; 736 } 737 738 /* 739 * If we use a tagging format that doesn't have an ethertype 740 * field, make sure that all packets from this point on get 741 * sent to the tag format's receive function. 742 */ 743 wmb(); 744 dev->dsa_ptr = (void *)dst; 745 746 if (dst->link_poll_needed) { 747 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work); 748 init_timer(&dst->link_poll_timer); 749 dst->link_poll_timer.data = (unsigned long)dst; 750 dst->link_poll_timer.function = dsa_link_poll_timer; 751 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ); 752 add_timer(&dst->link_poll_timer); 753 } 754 } 755 756 static int dsa_probe(struct platform_device *pdev) 757 { 758 struct dsa_platform_data *pd = pdev->dev.platform_data; 759 struct net_device *dev; 760 struct dsa_switch_tree *dst; 761 int ret; 762 763 pr_notice_once("Distributed Switch Architecture driver version %s\n", 764 dsa_driver_version); 765 766 if (pdev->dev.of_node) { 767 ret = dsa_of_probe(&pdev->dev); 768 if (ret) 769 return ret; 770 771 pd = pdev->dev.platform_data; 772 } 773 774 if (pd == NULL || pd->netdev == NULL) 775 return -EINVAL; 776 777 dev = dev_to_net_device(pd->netdev); 778 if (dev == NULL) { 779 ret = -EPROBE_DEFER; 780 goto out; 781 } 782 783 if (dev->dsa_ptr != NULL) { 784 dev_put(dev); 785 ret = -EEXIST; 786 goto out; 787 } 788 789 dst = kzalloc(sizeof(*dst), GFP_KERNEL); 790 if (dst == NULL) { 791 dev_put(dev); 792 ret = -ENOMEM; 793 goto out; 794 } 795 796 platform_set_drvdata(pdev, dst); 797 798 dsa_setup_dst(dst, dev, &pdev->dev, pd); 799 800 return 0; 801 802 out: 803 dsa_of_remove(&pdev->dev); 804 805 return ret; 806 } 807 808 static void dsa_remove_dst(struct dsa_switch_tree *dst) 809 { 810 int i; 811 812 if (dst->link_poll_needed) 813 del_timer_sync(&dst->link_poll_timer); 814 815 flush_work(&dst->link_poll_work); 816 817 for (i = 0; i < dst->pd->nr_chips; i++) { 818 struct dsa_switch *ds = dst->ds[i]; 819 820 if (ds != NULL) 821 dsa_switch_destroy(ds); 822 } 823 } 824 825 static int dsa_remove(struct platform_device *pdev) 826 { 827 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 828 829 dsa_remove_dst(dst); 830 dsa_of_remove(&pdev->dev); 831 832 return 0; 833 } 834 835 static void dsa_shutdown(struct platform_device *pdev) 836 { 837 } 838 839 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, 840 struct packet_type *pt, struct net_device *orig_dev) 841 { 842 struct dsa_switch_tree *dst = dev->dsa_ptr; 843 844 if (unlikely(dst == NULL)) { 845 kfree_skb(skb); 846 return 0; 847 } 848 849 return dst->rcv(skb, dev, pt, orig_dev); 850 } 851 852 static struct packet_type dsa_pack_type __read_mostly = { 853 .type = cpu_to_be16(ETH_P_XDSA), 854 .func = dsa_switch_rcv, 855 }; 856 857 static struct notifier_block dsa_netdevice_nb __read_mostly = { 858 .notifier_call = dsa_slave_netdevice_event, 859 }; 860 861 #ifdef CONFIG_PM_SLEEP 862 static int dsa_suspend(struct device *d) 863 { 864 struct platform_device *pdev = to_platform_device(d); 865 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 866 int i, ret = 0; 867 868 for (i = 0; i < dst->pd->nr_chips; i++) { 869 struct dsa_switch *ds = dst->ds[i]; 870 871 if (ds != NULL) 872 ret = dsa_switch_suspend(ds); 873 } 874 875 return ret; 876 } 877 878 static int dsa_resume(struct device *d) 879 { 880 struct platform_device *pdev = to_platform_device(d); 881 struct dsa_switch_tree *dst = platform_get_drvdata(pdev); 882 int i, ret = 0; 883 884 for (i = 0; i < dst->pd->nr_chips; i++) { 885 struct dsa_switch *ds = dst->ds[i]; 886 887 if (ds != NULL) 888 ret = dsa_switch_resume(ds); 889 } 890 891 return ret; 892 } 893 #endif 894 895 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); 896 897 static const struct of_device_id dsa_of_match_table[] = { 898 { .compatible = "brcm,bcm7445-switch-v4.0" }, 899 { .compatible = "marvell,dsa", }, 900 {} 901 }; 902 MODULE_DEVICE_TABLE(of, dsa_of_match_table); 903 904 static struct platform_driver dsa_driver = { 905 .probe = dsa_probe, 906 .remove = dsa_remove, 907 .shutdown = dsa_shutdown, 908 .driver = { 909 .name = "dsa", 910 .of_match_table = dsa_of_match_table, 911 .pm = &dsa_pm_ops, 912 }, 913 }; 914 915 static int __init dsa_init_module(void) 916 { 917 int rc; 918 919 register_netdevice_notifier(&dsa_netdevice_nb); 920 921 rc = platform_driver_register(&dsa_driver); 922 if (rc) 923 return rc; 924 925 dev_add_pack(&dsa_pack_type); 926 927 return 0; 928 } 929 module_init(dsa_init_module); 930 931 static void __exit dsa_cleanup_module(void) 932 { 933 unregister_netdevice_notifier(&dsa_netdevice_nb); 934 dev_remove_pack(&dsa_pack_type); 935 platform_driver_unregister(&dsa_driver); 936 } 937 module_exit(dsa_cleanup_module); 938 939 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); 940 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); 941 MODULE_LICENSE("GPL"); 942 MODULE_ALIAS("platform:dsa"); 943