1 /* 2 * net/dsa/slave.c - Slave device handling 3 * Copyright (c) 2008-2009 Marvell Semiconductor 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 */ 10 11 #include <linux/list.h> 12 #include <linux/etherdevice.h> 13 #include <linux/netdevice.h> 14 #include <linux/phy.h> 15 #include <linux/phy_fixed.h> 16 #include <linux/of_net.h> 17 #include <linux/of_mdio.h> 18 #include <net/rtnetlink.h> 19 #include <net/switchdev.h> 20 #include <linux/if_bridge.h> 21 #include <linux/netpoll.h> 22 #include "dsa_priv.h" 23 24 /* slave mii_bus handling ***************************************************/ 25 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 26 { 27 struct dsa_switch *ds = bus->priv; 28 29 if (ds->phys_mii_mask & (1 << addr)) 30 return ds->drv->phy_read(ds, addr, reg); 31 32 return 0xffff; 33 } 34 35 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 36 { 37 struct dsa_switch *ds = bus->priv; 38 39 if (ds->phys_mii_mask & (1 << addr)) 40 return ds->drv->phy_write(ds, addr, reg, val); 41 42 return 0; 43 } 44 45 void dsa_slave_mii_bus_init(struct dsa_switch *ds) 46 { 47 ds->slave_mii_bus->priv = (void *)ds; 48 ds->slave_mii_bus->name = "dsa slave smi"; 49 ds->slave_mii_bus->read = dsa_slave_phy_read; 50 ds->slave_mii_bus->write = dsa_slave_phy_write; 51 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", 52 ds->index, ds->pd->sw_addr); 53 ds->slave_mii_bus->parent = ds->master_dev; 54 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 55 } 56 57 58 /* slave device handling ****************************************************/ 59 static int dsa_slave_get_iflink(const struct net_device *dev) 60 { 61 struct dsa_slave_priv *p = netdev_priv(dev); 62 63 return p->parent->dst->master_netdev->ifindex; 64 } 65 66 static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p) 67 { 68 return !!p->bridge_dev; 69 } 70 71 static int dsa_slave_open(struct net_device *dev) 72 { 73 struct dsa_slave_priv *p = netdev_priv(dev); 74 struct net_device *master = p->parent->dst->master_netdev; 75 struct dsa_switch *ds = p->parent; 76 u8 stp_state = dsa_port_is_bridged(p) ? 77 BR_STATE_BLOCKING : BR_STATE_FORWARDING; 78 int err; 79 80 if (!(master->flags & IFF_UP)) 81 return -ENETDOWN; 82 83 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 84 err = dev_uc_add(master, dev->dev_addr); 85 if (err < 0) 86 goto out; 87 } 88 89 if (dev->flags & IFF_ALLMULTI) { 90 err = dev_set_allmulti(master, 1); 91 if (err < 0) 92 goto del_unicast; 93 } 94 if (dev->flags & IFF_PROMISC) { 95 err = dev_set_promiscuity(master, 1); 96 if (err < 0) 97 goto clear_allmulti; 98 } 99 100 if (ds->drv->port_enable) { 101 err = ds->drv->port_enable(ds, p->port, p->phy); 102 if (err) 103 goto clear_promisc; 104 } 105 106 if (ds->drv->port_stp_update) 107 ds->drv->port_stp_update(ds, p->port, stp_state); 108 109 if (p->phy) 110 phy_start(p->phy); 111 112 return 0; 113 114 clear_promisc: 115 if (dev->flags & IFF_PROMISC) 116 dev_set_promiscuity(master, -1); 117 clear_allmulti: 118 if (dev->flags & IFF_ALLMULTI) 119 dev_set_allmulti(master, -1); 120 del_unicast: 121 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 122 dev_uc_del(master, dev->dev_addr); 123 out: 124 return err; 125 } 126 127 static int dsa_slave_close(struct net_device *dev) 128 { 129 struct dsa_slave_priv *p = netdev_priv(dev); 130 struct net_device *master = p->parent->dst->master_netdev; 131 struct dsa_switch *ds = p->parent; 132 133 if (p->phy) 134 phy_stop(p->phy); 135 136 dev_mc_unsync(master, dev); 137 dev_uc_unsync(master, dev); 138 if (dev->flags & IFF_ALLMULTI) 139 dev_set_allmulti(master, -1); 140 if (dev->flags & IFF_PROMISC) 141 dev_set_promiscuity(master, -1); 142 143 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 144 dev_uc_del(master, dev->dev_addr); 145 146 if (ds->drv->port_disable) 147 ds->drv->port_disable(ds, p->port, p->phy); 148 149 if (ds->drv->port_stp_update) 150 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED); 151 152 return 0; 153 } 154 155 static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 156 { 157 struct dsa_slave_priv *p = netdev_priv(dev); 158 struct net_device *master = p->parent->dst->master_netdev; 159 160 if (change & IFF_ALLMULTI) 161 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 162 if (change & IFF_PROMISC) 163 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 164 } 165 166 static void dsa_slave_set_rx_mode(struct net_device *dev) 167 { 168 struct dsa_slave_priv *p = netdev_priv(dev); 169 struct net_device *master = p->parent->dst->master_netdev; 170 171 dev_mc_sync(master, dev); 172 dev_uc_sync(master, dev); 173 } 174 175 static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 176 { 177 struct dsa_slave_priv *p = netdev_priv(dev); 178 struct net_device *master = p->parent->dst->master_netdev; 179 struct sockaddr *addr = a; 180 int err; 181 182 if (!is_valid_ether_addr(addr->sa_data)) 183 return -EADDRNOTAVAIL; 184 185 if (!(dev->flags & IFF_UP)) 186 goto out; 187 188 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 189 err = dev_uc_add(master, addr->sa_data); 190 if (err < 0) 191 return err; 192 } 193 194 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 195 dev_uc_del(master, dev->dev_addr); 196 197 out: 198 ether_addr_copy(dev->dev_addr, addr->sa_data); 199 200 return 0; 201 } 202 203 static int dsa_bridge_check_vlan_range(struct dsa_switch *ds, 204 const struct net_device *bridge, 205 u16 vid_begin, u16 vid_end) 206 { 207 struct dsa_slave_priv *p; 208 struct net_device *dev, *vlan_br; 209 DECLARE_BITMAP(members, DSA_MAX_PORTS); 210 DECLARE_BITMAP(untagged, DSA_MAX_PORTS); 211 u16 vid; 212 int member, err; 213 214 if (!ds->drv->vlan_getnext || !vid_begin) 215 return -EOPNOTSUPP; 216 217 vid = vid_begin - 1; 218 219 do { 220 err = ds->drv->vlan_getnext(ds, &vid, members, untagged); 221 if (err) 222 break; 223 224 if (vid > vid_end) 225 break; 226 227 member = find_first_bit(members, DSA_MAX_PORTS); 228 if (member == DSA_MAX_PORTS) 229 continue; 230 231 dev = ds->ports[member]; 232 p = netdev_priv(dev); 233 vlan_br = p->bridge_dev; 234 if (vlan_br == bridge) 235 continue; 236 237 netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid); 238 return -EOPNOTSUPP; 239 } while (vid < vid_end); 240 241 return err == -ENOENT ? 0 : err; 242 } 243 244 static int dsa_slave_port_vlan_add(struct net_device *dev, 245 struct switchdev_obj *obj) 246 { 247 struct switchdev_obj_vlan *vlan = &obj->u.vlan; 248 struct dsa_slave_priv *p = netdev_priv(dev); 249 struct dsa_switch *ds = p->parent; 250 u16 vid; 251 int err; 252 253 switch (obj->trans) { 254 case SWITCHDEV_TRANS_PREPARE: 255 if (!ds->drv->port_vlan_add || !ds->drv->port_pvid_set) 256 return -EOPNOTSUPP; 257 258 /* If the requested port doesn't belong to the same bridge as 259 * the VLAN members, fallback to software VLAN (hopefully). 260 */ 261 err = dsa_bridge_check_vlan_range(ds, p->bridge_dev, 262 vlan->vid_begin, 263 vlan->vid_end); 264 if (err) 265 return err; 266 break; 267 case SWITCHDEV_TRANS_COMMIT: 268 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { 269 err = ds->drv->port_vlan_add(ds, p->port, vid, 270 vlan->flags & 271 BRIDGE_VLAN_INFO_UNTAGGED); 272 if (!err && vlan->flags & BRIDGE_VLAN_INFO_PVID) 273 err = ds->drv->port_pvid_set(ds, p->port, vid); 274 if (err) 275 return err; 276 } 277 break; 278 default: 279 return -EOPNOTSUPP; 280 } 281 282 return 0; 283 } 284 285 static int dsa_slave_port_vlan_del(struct net_device *dev, 286 struct switchdev_obj *obj) 287 { 288 struct switchdev_obj_vlan *vlan = &obj->u.vlan; 289 struct dsa_slave_priv *p = netdev_priv(dev); 290 struct dsa_switch *ds = p->parent; 291 u16 vid; 292 int err; 293 294 if (!ds->drv->port_vlan_del) 295 return -EOPNOTSUPP; 296 297 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { 298 err = ds->drv->port_vlan_del(ds, p->port, vid); 299 if (err) 300 return err; 301 } 302 303 return 0; 304 } 305 306 static int dsa_slave_port_vlan_dump(struct net_device *dev, 307 struct switchdev_obj *obj) 308 { 309 struct switchdev_obj_vlan *vlan = &obj->u.vlan; 310 struct dsa_slave_priv *p = netdev_priv(dev); 311 struct dsa_switch *ds = p->parent; 312 DECLARE_BITMAP(members, DSA_MAX_PORTS); 313 DECLARE_BITMAP(untagged, DSA_MAX_PORTS); 314 u16 pvid, vid = 0; 315 int err; 316 317 if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get) 318 return -EOPNOTSUPP; 319 320 err = ds->drv->port_pvid_get(ds, p->port, &pvid); 321 if (err) 322 return err; 323 324 for (;;) { 325 err = ds->drv->vlan_getnext(ds, &vid, members, untagged); 326 if (err) 327 break; 328 329 if (!test_bit(p->port, members)) 330 continue; 331 332 memset(vlan, 0, sizeof(*vlan)); 333 vlan->vid_begin = vlan->vid_end = vid; 334 335 if (vid == pvid) 336 vlan->flags |= BRIDGE_VLAN_INFO_PVID; 337 338 if (test_bit(p->port, untagged)) 339 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; 340 341 err = obj->cb(dev, obj); 342 if (err) 343 break; 344 } 345 346 return err == -ENOENT ? 0 : err; 347 } 348 349 static int dsa_slave_port_fdb_add(struct net_device *dev, 350 struct switchdev_obj *obj) 351 { 352 struct switchdev_obj_fdb *fdb = &obj->u.fdb; 353 struct dsa_slave_priv *p = netdev_priv(dev); 354 struct dsa_switch *ds = p->parent; 355 int ret = -EOPNOTSUPP; 356 357 if (obj->trans == SWITCHDEV_TRANS_PREPARE) 358 ret = ds->drv->port_fdb_add ? 0 : -EOPNOTSUPP; 359 else if (obj->trans == SWITCHDEV_TRANS_COMMIT) 360 ret = ds->drv->port_fdb_add(ds, p->port, fdb->addr, fdb->vid); 361 362 return ret; 363 } 364 365 static int dsa_slave_port_fdb_del(struct net_device *dev, 366 struct switchdev_obj *obj) 367 { 368 struct switchdev_obj_fdb *fdb = &obj->u.fdb; 369 struct dsa_slave_priv *p = netdev_priv(dev); 370 struct dsa_switch *ds = p->parent; 371 int ret = -EOPNOTSUPP; 372 373 if (ds->drv->port_fdb_del) 374 ret = ds->drv->port_fdb_del(ds, p->port, fdb->addr, fdb->vid); 375 376 return ret; 377 } 378 379 static int dsa_slave_port_fdb_dump(struct net_device *dev, 380 struct switchdev_obj *obj) 381 { 382 struct dsa_slave_priv *p = netdev_priv(dev); 383 struct dsa_switch *ds = p->parent; 384 unsigned char addr[ETH_ALEN] = { 0 }; 385 u16 vid = 0; 386 int ret; 387 388 if (!ds->drv->port_fdb_getnext) 389 return -EOPNOTSUPP; 390 391 for (;;) { 392 bool is_static; 393 394 ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid, 395 &is_static); 396 if (ret < 0) 397 break; 398 399 obj->u.fdb.addr = addr; 400 obj->u.fdb.vid = vid; 401 obj->u.fdb.ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 402 403 ret = obj->cb(dev, obj); 404 if (ret < 0) 405 break; 406 } 407 408 return ret == -ENOENT ? 0 : ret; 409 } 410 411 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 412 { 413 struct dsa_slave_priv *p = netdev_priv(dev); 414 415 if (p->phy != NULL) 416 return phy_mii_ioctl(p->phy, ifr, cmd); 417 418 return -EOPNOTSUPP; 419 } 420 421 /* Return a bitmask of all ports being currently bridged within a given bridge 422 * device. Note that on leave, the mask will still return the bitmask of ports 423 * currently bridged, prior to port removal, and this is exactly what we want. 424 */ 425 static u32 dsa_slave_br_port_mask(struct dsa_switch *ds, 426 struct net_device *bridge) 427 { 428 struct dsa_slave_priv *p; 429 unsigned int port; 430 u32 mask = 0; 431 432 for (port = 0; port < DSA_MAX_PORTS; port++) { 433 if (!dsa_is_port_initialized(ds, port)) 434 continue; 435 436 p = netdev_priv(ds->ports[port]); 437 438 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT && 439 p->bridge_dev == bridge) 440 mask |= 1 << port; 441 } 442 443 return mask; 444 } 445 446 static int dsa_slave_stp_update(struct net_device *dev, u8 state) 447 { 448 struct dsa_slave_priv *p = netdev_priv(dev); 449 struct dsa_switch *ds = p->parent; 450 int ret = -EOPNOTSUPP; 451 452 if (ds->drv->port_stp_update) 453 ret = ds->drv->port_stp_update(ds, p->port, state); 454 455 return ret; 456 } 457 458 static int dsa_slave_port_attr_set(struct net_device *dev, 459 struct switchdev_attr *attr) 460 { 461 struct dsa_slave_priv *p = netdev_priv(dev); 462 struct dsa_switch *ds = p->parent; 463 int ret; 464 465 switch (attr->id) { 466 case SWITCHDEV_ATTR_PORT_STP_STATE: 467 if (attr->trans == SWITCHDEV_TRANS_PREPARE) 468 ret = ds->drv->port_stp_update ? 0 : -EOPNOTSUPP; 469 else 470 ret = ds->drv->port_stp_update(ds, p->port, 471 attr->u.stp_state); 472 break; 473 default: 474 ret = -EOPNOTSUPP; 475 break; 476 } 477 478 return ret; 479 } 480 481 static int dsa_slave_port_obj_add(struct net_device *dev, 482 struct switchdev_obj *obj) 483 { 484 int err; 485 486 /* For the prepare phase, ensure the full set of changes is feasable in 487 * one go in order to signal a failure properly. If an operation is not 488 * supported, return -EOPNOTSUPP. 489 */ 490 491 switch (obj->id) { 492 case SWITCHDEV_OBJ_PORT_FDB: 493 err = dsa_slave_port_fdb_add(dev, obj); 494 break; 495 case SWITCHDEV_OBJ_PORT_VLAN: 496 err = dsa_slave_port_vlan_add(dev, obj); 497 break; 498 default: 499 err = -EOPNOTSUPP; 500 break; 501 } 502 503 return err; 504 } 505 506 static int dsa_slave_port_obj_del(struct net_device *dev, 507 struct switchdev_obj *obj) 508 { 509 int err; 510 511 switch (obj->id) { 512 case SWITCHDEV_OBJ_PORT_FDB: 513 err = dsa_slave_port_fdb_del(dev, obj); 514 break; 515 case SWITCHDEV_OBJ_PORT_VLAN: 516 err = dsa_slave_port_vlan_del(dev, obj); 517 break; 518 default: 519 err = -EOPNOTSUPP; 520 break; 521 } 522 523 return err; 524 } 525 526 static int dsa_slave_port_obj_dump(struct net_device *dev, 527 struct switchdev_obj *obj) 528 { 529 int err; 530 531 switch (obj->id) { 532 case SWITCHDEV_OBJ_PORT_FDB: 533 err = dsa_slave_port_fdb_dump(dev, obj); 534 break; 535 case SWITCHDEV_OBJ_PORT_VLAN: 536 err = dsa_slave_port_vlan_dump(dev, obj); 537 break; 538 default: 539 err = -EOPNOTSUPP; 540 break; 541 } 542 543 return err; 544 } 545 546 static int dsa_slave_bridge_port_join(struct net_device *dev, 547 struct net_device *br) 548 { 549 struct dsa_slave_priv *p = netdev_priv(dev); 550 struct dsa_switch *ds = p->parent; 551 int ret = -EOPNOTSUPP; 552 553 p->bridge_dev = br; 554 555 if (ds->drv->port_join_bridge) 556 ret = ds->drv->port_join_bridge(ds, p->port, 557 dsa_slave_br_port_mask(ds, br)); 558 559 return ret; 560 } 561 562 static int dsa_slave_bridge_port_leave(struct net_device *dev) 563 { 564 struct dsa_slave_priv *p = netdev_priv(dev); 565 struct dsa_switch *ds = p->parent; 566 int ret = -EOPNOTSUPP; 567 568 569 if (ds->drv->port_leave_bridge) 570 ret = ds->drv->port_leave_bridge(ds, p->port, 571 dsa_slave_br_port_mask(ds, p->bridge_dev)); 572 573 p->bridge_dev = NULL; 574 575 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, 576 * so allow it to be in BR_STATE_FORWARDING to be kept functional 577 */ 578 dsa_slave_stp_update(dev, BR_STATE_FORWARDING); 579 580 return ret; 581 } 582 583 static int dsa_slave_port_attr_get(struct net_device *dev, 584 struct switchdev_attr *attr) 585 { 586 struct dsa_slave_priv *p = netdev_priv(dev); 587 struct dsa_switch *ds = p->parent; 588 589 switch (attr->id) { 590 case SWITCHDEV_ATTR_PORT_PARENT_ID: 591 attr->u.ppid.id_len = sizeof(ds->index); 592 memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); 593 break; 594 default: 595 return -EOPNOTSUPP; 596 } 597 598 return 0; 599 } 600 601 static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, 602 struct sk_buff *skb) 603 { 604 #ifdef CONFIG_NET_POLL_CONTROLLER 605 if (p->netpoll) 606 netpoll_send_skb(p->netpoll, skb); 607 #else 608 BUG(); 609 #endif 610 return NETDEV_TX_OK; 611 } 612 613 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 614 { 615 struct dsa_slave_priv *p = netdev_priv(dev); 616 struct sk_buff *nskb; 617 618 dev->stats.tx_packets++; 619 dev->stats.tx_bytes += skb->len; 620 621 /* Transmit function may have to reallocate the original SKB */ 622 nskb = p->xmit(skb, dev); 623 if (!nskb) 624 return NETDEV_TX_OK; 625 626 /* SKB for netpoll still need to be mangled with the protocol-specific 627 * tag to be successfully transmitted 628 */ 629 if (unlikely(netpoll_tx_running(dev))) 630 return dsa_netpoll_send_skb(p, nskb); 631 632 /* Queue the SKB for transmission on the parent interface, but 633 * do not modify its EtherType 634 */ 635 nskb->dev = p->parent->dst->master_netdev; 636 dev_queue_xmit(nskb); 637 638 return NETDEV_TX_OK; 639 } 640 641 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, 642 struct net_device *dev) 643 { 644 /* Just return the original SKB */ 645 return skb; 646 } 647 648 649 /* ethtool operations *******************************************************/ 650 static int 651 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 652 { 653 struct dsa_slave_priv *p = netdev_priv(dev); 654 int err; 655 656 err = -EOPNOTSUPP; 657 if (p->phy != NULL) { 658 err = phy_read_status(p->phy); 659 if (err == 0) 660 err = phy_ethtool_gset(p->phy, cmd); 661 } 662 663 return err; 664 } 665 666 static int 667 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 668 { 669 struct dsa_slave_priv *p = netdev_priv(dev); 670 671 if (p->phy != NULL) 672 return phy_ethtool_sset(p->phy, cmd); 673 674 return -EOPNOTSUPP; 675 } 676 677 static void dsa_slave_get_drvinfo(struct net_device *dev, 678 struct ethtool_drvinfo *drvinfo) 679 { 680 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 681 strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); 682 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 683 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 684 } 685 686 static int dsa_slave_get_regs_len(struct net_device *dev) 687 { 688 struct dsa_slave_priv *p = netdev_priv(dev); 689 struct dsa_switch *ds = p->parent; 690 691 if (ds->drv->get_regs_len) 692 return ds->drv->get_regs_len(ds, p->port); 693 694 return -EOPNOTSUPP; 695 } 696 697 static void 698 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 699 { 700 struct dsa_slave_priv *p = netdev_priv(dev); 701 struct dsa_switch *ds = p->parent; 702 703 if (ds->drv->get_regs) 704 ds->drv->get_regs(ds, p->port, regs, _p); 705 } 706 707 static int dsa_slave_nway_reset(struct net_device *dev) 708 { 709 struct dsa_slave_priv *p = netdev_priv(dev); 710 711 if (p->phy != NULL) 712 return genphy_restart_aneg(p->phy); 713 714 return -EOPNOTSUPP; 715 } 716 717 static u32 dsa_slave_get_link(struct net_device *dev) 718 { 719 struct dsa_slave_priv *p = netdev_priv(dev); 720 721 if (p->phy != NULL) { 722 genphy_update_link(p->phy); 723 return p->phy->link; 724 } 725 726 return -EOPNOTSUPP; 727 } 728 729 static int dsa_slave_get_eeprom_len(struct net_device *dev) 730 { 731 struct dsa_slave_priv *p = netdev_priv(dev); 732 struct dsa_switch *ds = p->parent; 733 734 if (ds->pd->eeprom_len) 735 return ds->pd->eeprom_len; 736 737 if (ds->drv->get_eeprom_len) 738 return ds->drv->get_eeprom_len(ds); 739 740 return 0; 741 } 742 743 static int dsa_slave_get_eeprom(struct net_device *dev, 744 struct ethtool_eeprom *eeprom, u8 *data) 745 { 746 struct dsa_slave_priv *p = netdev_priv(dev); 747 struct dsa_switch *ds = p->parent; 748 749 if (ds->drv->get_eeprom) 750 return ds->drv->get_eeprom(ds, eeprom, data); 751 752 return -EOPNOTSUPP; 753 } 754 755 static int dsa_slave_set_eeprom(struct net_device *dev, 756 struct ethtool_eeprom *eeprom, u8 *data) 757 { 758 struct dsa_slave_priv *p = netdev_priv(dev); 759 struct dsa_switch *ds = p->parent; 760 761 if (ds->drv->set_eeprom) 762 return ds->drv->set_eeprom(ds, eeprom, data); 763 764 return -EOPNOTSUPP; 765 } 766 767 static void dsa_slave_get_strings(struct net_device *dev, 768 uint32_t stringset, uint8_t *data) 769 { 770 struct dsa_slave_priv *p = netdev_priv(dev); 771 struct dsa_switch *ds = p->parent; 772 773 if (stringset == ETH_SS_STATS) { 774 int len = ETH_GSTRING_LEN; 775 776 strncpy(data, "tx_packets", len); 777 strncpy(data + len, "tx_bytes", len); 778 strncpy(data + 2 * len, "rx_packets", len); 779 strncpy(data + 3 * len, "rx_bytes", len); 780 if (ds->drv->get_strings != NULL) 781 ds->drv->get_strings(ds, p->port, data + 4 * len); 782 } 783 } 784 785 static void dsa_slave_get_ethtool_stats(struct net_device *dev, 786 struct ethtool_stats *stats, 787 uint64_t *data) 788 { 789 struct dsa_slave_priv *p = netdev_priv(dev); 790 struct dsa_switch *ds = p->parent; 791 792 data[0] = p->dev->stats.tx_packets; 793 data[1] = p->dev->stats.tx_bytes; 794 data[2] = p->dev->stats.rx_packets; 795 data[3] = p->dev->stats.rx_bytes; 796 if (ds->drv->get_ethtool_stats != NULL) 797 ds->drv->get_ethtool_stats(ds, p->port, data + 4); 798 } 799 800 static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 801 { 802 struct dsa_slave_priv *p = netdev_priv(dev); 803 struct dsa_switch *ds = p->parent; 804 805 if (sset == ETH_SS_STATS) { 806 int count; 807 808 count = 4; 809 if (ds->drv->get_sset_count != NULL) 810 count += ds->drv->get_sset_count(ds); 811 812 return count; 813 } 814 815 return -EOPNOTSUPP; 816 } 817 818 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 819 { 820 struct dsa_slave_priv *p = netdev_priv(dev); 821 struct dsa_switch *ds = p->parent; 822 823 if (ds->drv->get_wol) 824 ds->drv->get_wol(ds, p->port, w); 825 } 826 827 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 828 { 829 struct dsa_slave_priv *p = netdev_priv(dev); 830 struct dsa_switch *ds = p->parent; 831 int ret = -EOPNOTSUPP; 832 833 if (ds->drv->set_wol) 834 ret = ds->drv->set_wol(ds, p->port, w); 835 836 return ret; 837 } 838 839 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 840 { 841 struct dsa_slave_priv *p = netdev_priv(dev); 842 struct dsa_switch *ds = p->parent; 843 int ret; 844 845 if (!ds->drv->set_eee) 846 return -EOPNOTSUPP; 847 848 ret = ds->drv->set_eee(ds, p->port, p->phy, e); 849 if (ret) 850 return ret; 851 852 if (p->phy) 853 ret = phy_ethtool_set_eee(p->phy, e); 854 855 return ret; 856 } 857 858 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 859 { 860 struct dsa_slave_priv *p = netdev_priv(dev); 861 struct dsa_switch *ds = p->parent; 862 int ret; 863 864 if (!ds->drv->get_eee) 865 return -EOPNOTSUPP; 866 867 ret = ds->drv->get_eee(ds, p->port, e); 868 if (ret) 869 return ret; 870 871 if (p->phy) 872 ret = phy_ethtool_get_eee(p->phy, e); 873 874 return ret; 875 } 876 877 #ifdef CONFIG_NET_POLL_CONTROLLER 878 static int dsa_slave_netpoll_setup(struct net_device *dev, 879 struct netpoll_info *ni) 880 { 881 struct dsa_slave_priv *p = netdev_priv(dev); 882 struct dsa_switch *ds = p->parent; 883 struct net_device *master = ds->dst->master_netdev; 884 struct netpoll *netpoll; 885 int err = 0; 886 887 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 888 if (!netpoll) 889 return -ENOMEM; 890 891 err = __netpoll_setup(netpoll, master); 892 if (err) { 893 kfree(netpoll); 894 goto out; 895 } 896 897 p->netpoll = netpoll; 898 out: 899 return err; 900 } 901 902 static void dsa_slave_netpoll_cleanup(struct net_device *dev) 903 { 904 struct dsa_slave_priv *p = netdev_priv(dev); 905 struct netpoll *netpoll = p->netpoll; 906 907 if (!netpoll) 908 return; 909 910 p->netpoll = NULL; 911 912 __netpoll_free_async(netpoll); 913 } 914 915 static void dsa_slave_poll_controller(struct net_device *dev) 916 { 917 } 918 #endif 919 920 static const struct ethtool_ops dsa_slave_ethtool_ops = { 921 .get_settings = dsa_slave_get_settings, 922 .set_settings = dsa_slave_set_settings, 923 .get_drvinfo = dsa_slave_get_drvinfo, 924 .get_regs_len = dsa_slave_get_regs_len, 925 .get_regs = dsa_slave_get_regs, 926 .nway_reset = dsa_slave_nway_reset, 927 .get_link = dsa_slave_get_link, 928 .get_eeprom_len = dsa_slave_get_eeprom_len, 929 .get_eeprom = dsa_slave_get_eeprom, 930 .set_eeprom = dsa_slave_set_eeprom, 931 .get_strings = dsa_slave_get_strings, 932 .get_ethtool_stats = dsa_slave_get_ethtool_stats, 933 .get_sset_count = dsa_slave_get_sset_count, 934 .set_wol = dsa_slave_set_wol, 935 .get_wol = dsa_slave_get_wol, 936 .set_eee = dsa_slave_set_eee, 937 .get_eee = dsa_slave_get_eee, 938 }; 939 940 static const struct net_device_ops dsa_slave_netdev_ops = { 941 .ndo_open = dsa_slave_open, 942 .ndo_stop = dsa_slave_close, 943 .ndo_start_xmit = dsa_slave_xmit, 944 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 945 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 946 .ndo_set_mac_address = dsa_slave_set_mac_address, 947 .ndo_fdb_add = switchdev_port_fdb_add, 948 .ndo_fdb_del = switchdev_port_fdb_del, 949 .ndo_fdb_dump = switchdev_port_fdb_dump, 950 .ndo_do_ioctl = dsa_slave_ioctl, 951 .ndo_get_iflink = dsa_slave_get_iflink, 952 #ifdef CONFIG_NET_POLL_CONTROLLER 953 .ndo_netpoll_setup = dsa_slave_netpoll_setup, 954 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 955 .ndo_poll_controller = dsa_slave_poll_controller, 956 #endif 957 .ndo_bridge_getlink = switchdev_port_bridge_getlink, 958 .ndo_bridge_setlink = switchdev_port_bridge_setlink, 959 .ndo_bridge_dellink = switchdev_port_bridge_dellink, 960 }; 961 962 static const struct switchdev_ops dsa_slave_switchdev_ops = { 963 .switchdev_port_attr_get = dsa_slave_port_attr_get, 964 .switchdev_port_attr_set = dsa_slave_port_attr_set, 965 .switchdev_port_obj_add = dsa_slave_port_obj_add, 966 .switchdev_port_obj_del = dsa_slave_port_obj_del, 967 .switchdev_port_obj_dump = dsa_slave_port_obj_dump, 968 }; 969 970 static void dsa_slave_adjust_link(struct net_device *dev) 971 { 972 struct dsa_slave_priv *p = netdev_priv(dev); 973 struct dsa_switch *ds = p->parent; 974 unsigned int status_changed = 0; 975 976 if (p->old_link != p->phy->link) { 977 status_changed = 1; 978 p->old_link = p->phy->link; 979 } 980 981 if (p->old_duplex != p->phy->duplex) { 982 status_changed = 1; 983 p->old_duplex = p->phy->duplex; 984 } 985 986 if (p->old_pause != p->phy->pause) { 987 status_changed = 1; 988 p->old_pause = p->phy->pause; 989 } 990 991 if (ds->drv->adjust_link && status_changed) 992 ds->drv->adjust_link(ds, p->port, p->phy); 993 994 if (status_changed) 995 phy_print_status(p->phy); 996 } 997 998 static int dsa_slave_fixed_link_update(struct net_device *dev, 999 struct fixed_phy_status *status) 1000 { 1001 struct dsa_slave_priv *p = netdev_priv(dev); 1002 struct dsa_switch *ds = p->parent; 1003 1004 if (ds->drv->fixed_link_update) 1005 ds->drv->fixed_link_update(ds, p->port, status); 1006 1007 return 0; 1008 } 1009 1010 /* slave device setup *******************************************************/ 1011 static int dsa_slave_phy_connect(struct dsa_slave_priv *p, 1012 struct net_device *slave_dev, 1013 int addr) 1014 { 1015 struct dsa_switch *ds = p->parent; 1016 1017 p->phy = ds->slave_mii_bus->phy_map[addr]; 1018 if (!p->phy) 1019 return -ENODEV; 1020 1021 /* Use already configured phy mode */ 1022 if (p->phy_interface == PHY_INTERFACE_MODE_NA) 1023 p->phy_interface = p->phy->interface; 1024 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 1025 p->phy_interface); 1026 1027 return 0; 1028 } 1029 1030 static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 1031 struct net_device *slave_dev) 1032 { 1033 struct dsa_switch *ds = p->parent; 1034 struct dsa_chip_data *cd = ds->pd; 1035 struct device_node *phy_dn, *port_dn; 1036 bool phy_is_fixed = false; 1037 u32 phy_flags = 0; 1038 int mode, ret; 1039 1040 port_dn = cd->port_dn[p->port]; 1041 mode = of_get_phy_mode(port_dn); 1042 if (mode < 0) 1043 mode = PHY_INTERFACE_MODE_NA; 1044 p->phy_interface = mode; 1045 1046 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 1047 if (of_phy_is_fixed_link(port_dn)) { 1048 /* In the case of a fixed PHY, the DT node associated 1049 * to the fixed PHY is the Port DT node 1050 */ 1051 ret = of_phy_register_fixed_link(port_dn); 1052 if (ret) { 1053 netdev_err(slave_dev, "failed to register fixed PHY\n"); 1054 return ret; 1055 } 1056 phy_is_fixed = true; 1057 phy_dn = port_dn; 1058 } 1059 1060 if (ds->drv->get_phy_flags) 1061 phy_flags = ds->drv->get_phy_flags(ds, p->port); 1062 1063 if (phy_dn) { 1064 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn); 1065 /* If this PHY address is part of phys_mii_mask, which means 1066 * that we need to divert reads and writes to/from it, then we 1067 * want to bind this device using the slave MII bus created by 1068 * DSA to make that happen. 1069 */ 1070 if (!phy_is_fixed && ret >= 0 && 1071 (ds->phys_mii_mask & (1 << ret))) { 1072 ret = dsa_slave_phy_connect(p, slave_dev, ret); 1073 if (ret) 1074 return ret; 1075 } else { 1076 p->phy = of_phy_connect(slave_dev, phy_dn, 1077 dsa_slave_adjust_link, 1078 phy_flags, 1079 p->phy_interface); 1080 } 1081 } 1082 1083 if (p->phy && phy_is_fixed) 1084 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 1085 1086 /* We could not connect to a designated PHY, so use the switch internal 1087 * MDIO bus instead 1088 */ 1089 if (!p->phy) { 1090 ret = dsa_slave_phy_connect(p, slave_dev, p->port); 1091 if (ret) 1092 return ret; 1093 } else { 1094 netdev_info(slave_dev, "attached PHY at address %d [%s]\n", 1095 p->phy->addr, p->phy->drv->name); 1096 } 1097 1098 return 0; 1099 } 1100 1101 static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1102 static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1103 struct netdev_queue *txq, 1104 void *_unused) 1105 { 1106 lockdep_set_class(&txq->_xmit_lock, 1107 &dsa_slave_netdev_xmit_lock_key); 1108 } 1109 1110 int dsa_slave_suspend(struct net_device *slave_dev) 1111 { 1112 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1113 1114 if (p->phy) { 1115 phy_stop(p->phy); 1116 p->old_pause = -1; 1117 p->old_link = -1; 1118 p->old_duplex = -1; 1119 phy_suspend(p->phy); 1120 } 1121 1122 return 0; 1123 } 1124 1125 int dsa_slave_resume(struct net_device *slave_dev) 1126 { 1127 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1128 1129 netif_device_attach(slave_dev); 1130 1131 if (p->phy) { 1132 phy_resume(p->phy); 1133 phy_start(p->phy); 1134 } 1135 1136 return 0; 1137 } 1138 1139 int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 1140 int port, char *name) 1141 { 1142 struct net_device *master = ds->dst->master_netdev; 1143 struct net_device *slave_dev; 1144 struct dsa_slave_priv *p; 1145 int ret; 1146 1147 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 1148 NET_NAME_UNKNOWN, ether_setup); 1149 if (slave_dev == NULL) 1150 return -ENOMEM; 1151 1152 slave_dev->features = master->vlan_features; 1153 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 1154 eth_hw_addr_inherit(slave_dev, master); 1155 slave_dev->priv_flags |= IFF_NO_QUEUE; 1156 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 1157 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 1158 1159 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1160 NULL); 1161 1162 SET_NETDEV_DEV(slave_dev, parent); 1163 slave_dev->dev.of_node = ds->pd->port_dn[port]; 1164 slave_dev->vlan_features = master->vlan_features; 1165 1166 p = netdev_priv(slave_dev); 1167 p->dev = slave_dev; 1168 p->parent = ds; 1169 p->port = port; 1170 1171 switch (ds->dst->tag_protocol) { 1172 #ifdef CONFIG_NET_DSA_TAG_DSA 1173 case DSA_TAG_PROTO_DSA: 1174 p->xmit = dsa_netdev_ops.xmit; 1175 break; 1176 #endif 1177 #ifdef CONFIG_NET_DSA_TAG_EDSA 1178 case DSA_TAG_PROTO_EDSA: 1179 p->xmit = edsa_netdev_ops.xmit; 1180 break; 1181 #endif 1182 #ifdef CONFIG_NET_DSA_TAG_TRAILER 1183 case DSA_TAG_PROTO_TRAILER: 1184 p->xmit = trailer_netdev_ops.xmit; 1185 break; 1186 #endif 1187 #ifdef CONFIG_NET_DSA_TAG_BRCM 1188 case DSA_TAG_PROTO_BRCM: 1189 p->xmit = brcm_netdev_ops.xmit; 1190 break; 1191 #endif 1192 default: 1193 p->xmit = dsa_slave_notag_xmit; 1194 break; 1195 } 1196 1197 p->old_pause = -1; 1198 p->old_link = -1; 1199 p->old_duplex = -1; 1200 1201 ret = dsa_slave_phy_setup(p, slave_dev); 1202 if (ret) { 1203 free_netdev(slave_dev); 1204 return ret; 1205 } 1206 1207 ds->ports[port] = slave_dev; 1208 ret = register_netdev(slave_dev); 1209 if (ret) { 1210 netdev_err(master, "error %d registering interface %s\n", 1211 ret, slave_dev->name); 1212 phy_disconnect(p->phy); 1213 ds->ports[port] = NULL; 1214 free_netdev(slave_dev); 1215 return ret; 1216 } 1217 1218 netif_carrier_off(slave_dev); 1219 1220 return 0; 1221 } 1222 1223 static bool dsa_slave_dev_check(struct net_device *dev) 1224 { 1225 return dev->netdev_ops == &dsa_slave_netdev_ops; 1226 } 1227 1228 static int dsa_slave_master_changed(struct net_device *dev) 1229 { 1230 struct net_device *master = netdev_master_upper_dev_get(dev); 1231 struct dsa_slave_priv *p = netdev_priv(dev); 1232 int err = 0; 1233 1234 if (master && master->rtnl_link_ops && 1235 !strcmp(master->rtnl_link_ops->kind, "bridge")) 1236 err = dsa_slave_bridge_port_join(dev, master); 1237 else if (dsa_port_is_bridged(p)) 1238 err = dsa_slave_bridge_port_leave(dev); 1239 1240 return err; 1241 } 1242 1243 int dsa_slave_netdevice_event(struct notifier_block *unused, 1244 unsigned long event, void *ptr) 1245 { 1246 struct net_device *dev; 1247 int err = 0; 1248 1249 switch (event) { 1250 case NETDEV_CHANGEUPPER: 1251 dev = netdev_notifier_info_to_dev(ptr); 1252 if (!dsa_slave_dev_check(dev)) 1253 goto out; 1254 1255 err = dsa_slave_master_changed(dev); 1256 if (err) 1257 netdev_warn(dev, "failed to reflect master change\n"); 1258 1259 break; 1260 } 1261 1262 out: 1263 return NOTIFY_DONE; 1264 } 1265