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 <linux/mdio.h> 19 #include <net/rtnetlink.h> 20 #include <net/pkt_cls.h> 21 #include <net/tc_act/tc_mirred.h> 22 #include <linux/if_bridge.h> 23 #include <linux/netpoll.h> 24 #include <linux/ptp_classify.h> 25 26 #include "dsa_priv.h" 27 28 static bool dsa_slave_dev_check(struct net_device *dev); 29 30 /* slave mii_bus handling ***************************************************/ 31 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 32 { 33 struct dsa_switch *ds = bus->priv; 34 35 if (ds->phys_mii_mask & (1 << addr)) 36 return ds->ops->phy_read(ds, addr, reg); 37 38 return 0xffff; 39 } 40 41 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 42 { 43 struct dsa_switch *ds = bus->priv; 44 45 if (ds->phys_mii_mask & (1 << addr)) 46 return ds->ops->phy_write(ds, addr, reg, val); 47 48 return 0; 49 } 50 51 void dsa_slave_mii_bus_init(struct dsa_switch *ds) 52 { 53 ds->slave_mii_bus->priv = (void *)ds; 54 ds->slave_mii_bus->name = "dsa slave smi"; 55 ds->slave_mii_bus->read = dsa_slave_phy_read; 56 ds->slave_mii_bus->write = dsa_slave_phy_write; 57 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 58 ds->dst->index, ds->index); 59 ds->slave_mii_bus->parent = ds->dev; 60 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 61 } 62 63 64 /* slave device handling ****************************************************/ 65 static int dsa_slave_get_iflink(const struct net_device *dev) 66 { 67 return dsa_slave_to_master(dev)->ifindex; 68 } 69 70 static int dsa_slave_open(struct net_device *dev) 71 { 72 struct net_device *master = dsa_slave_to_master(dev); 73 struct dsa_port *dp = dsa_slave_to_port(dev); 74 int err; 75 76 if (!(master->flags & IFF_UP)) 77 return -ENETDOWN; 78 79 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 80 err = dev_uc_add(master, dev->dev_addr); 81 if (err < 0) 82 goto out; 83 } 84 85 if (dev->flags & IFF_ALLMULTI) { 86 err = dev_set_allmulti(master, 1); 87 if (err < 0) 88 goto del_unicast; 89 } 90 if (dev->flags & IFF_PROMISC) { 91 err = dev_set_promiscuity(master, 1); 92 if (err < 0) 93 goto clear_allmulti; 94 } 95 96 err = dsa_port_enable(dp, dev->phydev); 97 if (err) 98 goto clear_promisc; 99 100 if (dev->phydev) 101 phy_start(dev->phydev); 102 103 return 0; 104 105 clear_promisc: 106 if (dev->flags & IFF_PROMISC) 107 dev_set_promiscuity(master, -1); 108 clear_allmulti: 109 if (dev->flags & IFF_ALLMULTI) 110 dev_set_allmulti(master, -1); 111 del_unicast: 112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 113 dev_uc_del(master, dev->dev_addr); 114 out: 115 return err; 116 } 117 118 static int dsa_slave_close(struct net_device *dev) 119 { 120 struct net_device *master = dsa_slave_to_master(dev); 121 struct dsa_port *dp = dsa_slave_to_port(dev); 122 123 if (dev->phydev) 124 phy_stop(dev->phydev); 125 126 dsa_port_disable(dp, dev->phydev); 127 128 dev_mc_unsync(master, dev); 129 dev_uc_unsync(master, dev); 130 if (dev->flags & IFF_ALLMULTI) 131 dev_set_allmulti(master, -1); 132 if (dev->flags & IFF_PROMISC) 133 dev_set_promiscuity(master, -1); 134 135 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 136 dev_uc_del(master, dev->dev_addr); 137 138 return 0; 139 } 140 141 static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 142 { 143 struct net_device *master = dsa_slave_to_master(dev); 144 145 if (change & IFF_ALLMULTI) 146 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 147 if (change & IFF_PROMISC) 148 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 149 } 150 151 static void dsa_slave_set_rx_mode(struct net_device *dev) 152 { 153 struct net_device *master = dsa_slave_to_master(dev); 154 155 dev_mc_sync(master, dev); 156 dev_uc_sync(master, dev); 157 } 158 159 static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 160 { 161 struct net_device *master = dsa_slave_to_master(dev); 162 struct sockaddr *addr = a; 163 int err; 164 165 if (!is_valid_ether_addr(addr->sa_data)) 166 return -EADDRNOTAVAIL; 167 168 if (!(dev->flags & IFF_UP)) 169 goto out; 170 171 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 172 err = dev_uc_add(master, addr->sa_data); 173 if (err < 0) 174 return err; 175 } 176 177 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 178 dev_uc_del(master, dev->dev_addr); 179 180 out: 181 ether_addr_copy(dev->dev_addr, addr->sa_data); 182 183 return 0; 184 } 185 186 struct dsa_slave_dump_ctx { 187 struct net_device *dev; 188 struct sk_buff *skb; 189 struct netlink_callback *cb; 190 int idx; 191 }; 192 193 static int 194 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 195 bool is_static, void *data) 196 { 197 struct dsa_slave_dump_ctx *dump = data; 198 u32 portid = NETLINK_CB(dump->cb->skb).portid; 199 u32 seq = dump->cb->nlh->nlmsg_seq; 200 struct nlmsghdr *nlh; 201 struct ndmsg *ndm; 202 203 if (dump->idx < dump->cb->args[2]) 204 goto skip; 205 206 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 207 sizeof(*ndm), NLM_F_MULTI); 208 if (!nlh) 209 return -EMSGSIZE; 210 211 ndm = nlmsg_data(nlh); 212 ndm->ndm_family = AF_BRIDGE; 213 ndm->ndm_pad1 = 0; 214 ndm->ndm_pad2 = 0; 215 ndm->ndm_flags = NTF_SELF; 216 ndm->ndm_type = 0; 217 ndm->ndm_ifindex = dump->dev->ifindex; 218 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 219 220 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 221 goto nla_put_failure; 222 223 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 224 goto nla_put_failure; 225 226 nlmsg_end(dump->skb, nlh); 227 228 skip: 229 dump->idx++; 230 return 0; 231 232 nla_put_failure: 233 nlmsg_cancel(dump->skb, nlh); 234 return -EMSGSIZE; 235 } 236 237 static int 238 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 239 struct net_device *dev, struct net_device *filter_dev, 240 int *idx) 241 { 242 struct dsa_port *dp = dsa_slave_to_port(dev); 243 struct dsa_slave_dump_ctx dump = { 244 .dev = dev, 245 .skb = skb, 246 .cb = cb, 247 .idx = *idx, 248 }; 249 int err; 250 251 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 252 *idx = dump.idx; 253 254 return err; 255 } 256 257 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 258 { 259 struct dsa_slave_priv *p = netdev_priv(dev); 260 struct dsa_switch *ds = p->dp->ds; 261 int port = p->dp->index; 262 263 /* Pass through to switch driver if it supports timestamping */ 264 switch (cmd) { 265 case SIOCGHWTSTAMP: 266 if (ds->ops->port_hwtstamp_get) 267 return ds->ops->port_hwtstamp_get(ds, port, ifr); 268 break; 269 case SIOCSHWTSTAMP: 270 if (ds->ops->port_hwtstamp_set) 271 return ds->ops->port_hwtstamp_set(ds, port, ifr); 272 break; 273 } 274 275 if (!dev->phydev) 276 return -ENODEV; 277 278 return phy_mii_ioctl(dev->phydev, ifr, cmd); 279 } 280 281 static int dsa_slave_port_attr_set(struct net_device *dev, 282 const struct switchdev_attr *attr, 283 struct switchdev_trans *trans) 284 { 285 struct dsa_port *dp = dsa_slave_to_port(dev); 286 int ret; 287 288 switch (attr->id) { 289 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 290 ret = dsa_port_set_state(dp, attr->u.stp_state, trans); 291 break; 292 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 293 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 294 trans); 295 break; 296 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 297 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); 298 break; 299 default: 300 ret = -EOPNOTSUPP; 301 break; 302 } 303 304 return ret; 305 } 306 307 static int dsa_slave_port_obj_add(struct net_device *dev, 308 const struct switchdev_obj *obj, 309 struct switchdev_trans *trans) 310 { 311 struct dsa_port *dp = dsa_slave_to_port(dev); 312 int err; 313 314 /* For the prepare phase, ensure the full set of changes is feasable in 315 * one go in order to signal a failure properly. If an operation is not 316 * supported, return -EOPNOTSUPP. 317 */ 318 319 switch (obj->id) { 320 case SWITCHDEV_OBJ_ID_PORT_MDB: 321 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); 322 break; 323 case SWITCHDEV_OBJ_ID_HOST_MDB: 324 /* DSA can directly translate this to a normal MDB add, 325 * but on the CPU port. 326 */ 327 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj), 328 trans); 329 break; 330 case SWITCHDEV_OBJ_ID_PORT_VLAN: 331 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), 332 trans); 333 break; 334 default: 335 err = -EOPNOTSUPP; 336 break; 337 } 338 339 return err; 340 } 341 342 static int dsa_slave_port_obj_del(struct net_device *dev, 343 const struct switchdev_obj *obj) 344 { 345 struct dsa_port *dp = dsa_slave_to_port(dev); 346 int err; 347 348 switch (obj->id) { 349 case SWITCHDEV_OBJ_ID_PORT_MDB: 350 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 351 break; 352 case SWITCHDEV_OBJ_ID_HOST_MDB: 353 /* DSA can directly translate this to a normal MDB add, 354 * but on the CPU port. 355 */ 356 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 357 break; 358 case SWITCHDEV_OBJ_ID_PORT_VLAN: 359 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); 360 break; 361 default: 362 err = -EOPNOTSUPP; 363 break; 364 } 365 366 return err; 367 } 368 369 static int dsa_slave_port_attr_get(struct net_device *dev, 370 struct switchdev_attr *attr) 371 { 372 struct dsa_port *dp = dsa_slave_to_port(dev); 373 struct dsa_switch *ds = dp->ds; 374 struct dsa_switch_tree *dst = ds->dst; 375 376 switch (attr->id) { 377 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 378 attr->u.ppid.id_len = sizeof(dst->index); 379 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len); 380 break; 381 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT: 382 attr->u.brport_flags_support = 0; 383 break; 384 default: 385 return -EOPNOTSUPP; 386 } 387 388 return 0; 389 } 390 391 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 392 struct sk_buff *skb) 393 { 394 #ifdef CONFIG_NET_POLL_CONTROLLER 395 struct dsa_slave_priv *p = netdev_priv(dev); 396 397 if (p->netpoll) 398 netpoll_send_skb(p->netpoll, skb); 399 #else 400 BUG(); 401 #endif 402 return NETDEV_TX_OK; 403 } 404 405 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 406 struct sk_buff *skb) 407 { 408 struct dsa_switch *ds = p->dp->ds; 409 struct sk_buff *clone; 410 unsigned int type; 411 412 type = ptp_classify_raw(skb); 413 if (type == PTP_CLASS_NONE) 414 return; 415 416 if (!ds->ops->port_txtstamp) 417 return; 418 419 clone = skb_clone_sk(skb); 420 if (!clone) 421 return; 422 423 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) 424 return; 425 426 kfree_skb(clone); 427 } 428 429 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 430 { 431 struct dsa_slave_priv *p = netdev_priv(dev); 432 struct pcpu_sw_netstats *s; 433 struct sk_buff *nskb; 434 435 s = this_cpu_ptr(p->stats64); 436 u64_stats_update_begin(&s->syncp); 437 s->tx_packets++; 438 s->tx_bytes += skb->len; 439 u64_stats_update_end(&s->syncp); 440 441 /* Identify PTP protocol packets, clone them, and pass them to the 442 * switch driver 443 */ 444 dsa_skb_tx_timestamp(p, skb); 445 446 /* Transmit function may have to reallocate the original SKB, 447 * in which case it must have freed it. Only free it here on error. 448 */ 449 nskb = p->xmit(skb, dev); 450 if (!nskb) { 451 kfree_skb(skb); 452 return NETDEV_TX_OK; 453 } 454 455 /* SKB for netpoll still need to be mangled with the protocol-specific 456 * tag to be successfully transmitted 457 */ 458 if (unlikely(netpoll_tx_running(dev))) 459 return dsa_slave_netpoll_send_skb(dev, nskb); 460 461 /* Queue the SKB for transmission on the parent interface, but 462 * do not modify its EtherType 463 */ 464 nskb->dev = dsa_slave_to_master(dev); 465 dev_queue_xmit(nskb); 466 467 return NETDEV_TX_OK; 468 } 469 470 /* ethtool operations *******************************************************/ 471 472 static void dsa_slave_get_drvinfo(struct net_device *dev, 473 struct ethtool_drvinfo *drvinfo) 474 { 475 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 476 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 477 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 478 } 479 480 static int dsa_slave_get_regs_len(struct net_device *dev) 481 { 482 struct dsa_port *dp = dsa_slave_to_port(dev); 483 struct dsa_switch *ds = dp->ds; 484 485 if (ds->ops->get_regs_len) 486 return ds->ops->get_regs_len(ds, dp->index); 487 488 return -EOPNOTSUPP; 489 } 490 491 static void 492 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 493 { 494 struct dsa_port *dp = dsa_slave_to_port(dev); 495 struct dsa_switch *ds = dp->ds; 496 497 if (ds->ops->get_regs) 498 ds->ops->get_regs(ds, dp->index, regs, _p); 499 } 500 501 static u32 dsa_slave_get_link(struct net_device *dev) 502 { 503 if (!dev->phydev) 504 return -ENODEV; 505 506 genphy_update_link(dev->phydev); 507 508 return dev->phydev->link; 509 } 510 511 static int dsa_slave_get_eeprom_len(struct net_device *dev) 512 { 513 struct dsa_port *dp = dsa_slave_to_port(dev); 514 struct dsa_switch *ds = dp->ds; 515 516 if (ds->cd && ds->cd->eeprom_len) 517 return ds->cd->eeprom_len; 518 519 if (ds->ops->get_eeprom_len) 520 return ds->ops->get_eeprom_len(ds); 521 522 return 0; 523 } 524 525 static int dsa_slave_get_eeprom(struct net_device *dev, 526 struct ethtool_eeprom *eeprom, u8 *data) 527 { 528 struct dsa_port *dp = dsa_slave_to_port(dev); 529 struct dsa_switch *ds = dp->ds; 530 531 if (ds->ops->get_eeprom) 532 return ds->ops->get_eeprom(ds, eeprom, data); 533 534 return -EOPNOTSUPP; 535 } 536 537 static int dsa_slave_set_eeprom(struct net_device *dev, 538 struct ethtool_eeprom *eeprom, u8 *data) 539 { 540 struct dsa_port *dp = dsa_slave_to_port(dev); 541 struct dsa_switch *ds = dp->ds; 542 543 if (ds->ops->set_eeprom) 544 return ds->ops->set_eeprom(ds, eeprom, data); 545 546 return -EOPNOTSUPP; 547 } 548 549 static void dsa_slave_get_strings(struct net_device *dev, 550 uint32_t stringset, uint8_t *data) 551 { 552 struct dsa_port *dp = dsa_slave_to_port(dev); 553 struct dsa_switch *ds = dp->ds; 554 555 if (stringset == ETH_SS_STATS) { 556 int len = ETH_GSTRING_LEN; 557 558 strncpy(data, "tx_packets", len); 559 strncpy(data + len, "tx_bytes", len); 560 strncpy(data + 2 * len, "rx_packets", len); 561 strncpy(data + 3 * len, "rx_bytes", len); 562 if (ds->ops->get_strings) 563 ds->ops->get_strings(ds, dp->index, data + 4 * len); 564 } 565 } 566 567 static void dsa_slave_get_ethtool_stats(struct net_device *dev, 568 struct ethtool_stats *stats, 569 uint64_t *data) 570 { 571 struct dsa_port *dp = dsa_slave_to_port(dev); 572 struct dsa_slave_priv *p = netdev_priv(dev); 573 struct dsa_switch *ds = dp->ds; 574 struct pcpu_sw_netstats *s; 575 unsigned int start; 576 int i; 577 578 for_each_possible_cpu(i) { 579 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 580 581 s = per_cpu_ptr(p->stats64, i); 582 do { 583 start = u64_stats_fetch_begin_irq(&s->syncp); 584 tx_packets = s->tx_packets; 585 tx_bytes = s->tx_bytes; 586 rx_packets = s->rx_packets; 587 rx_bytes = s->rx_bytes; 588 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 589 data[0] += tx_packets; 590 data[1] += tx_bytes; 591 data[2] += rx_packets; 592 data[3] += rx_bytes; 593 } 594 if (ds->ops->get_ethtool_stats) 595 ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 596 } 597 598 static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 599 { 600 struct dsa_port *dp = dsa_slave_to_port(dev); 601 struct dsa_switch *ds = dp->ds; 602 603 if (sset == ETH_SS_STATS) { 604 int count; 605 606 count = 4; 607 if (ds->ops->get_sset_count) 608 count += ds->ops->get_sset_count(ds, dp->index); 609 610 return count; 611 } 612 613 return -EOPNOTSUPP; 614 } 615 616 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 617 { 618 struct dsa_port *dp = dsa_slave_to_port(dev); 619 struct dsa_switch *ds = dp->ds; 620 621 if (ds->ops->get_wol) 622 ds->ops->get_wol(ds, dp->index, w); 623 } 624 625 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 626 { 627 struct dsa_port *dp = dsa_slave_to_port(dev); 628 struct dsa_switch *ds = dp->ds; 629 int ret = -EOPNOTSUPP; 630 631 if (ds->ops->set_wol) 632 ret = ds->ops->set_wol(ds, dp->index, w); 633 634 return ret; 635 } 636 637 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 638 { 639 struct dsa_port *dp = dsa_slave_to_port(dev); 640 struct dsa_switch *ds = dp->ds; 641 int ret; 642 643 /* Port's PHY and MAC both need to be EEE capable */ 644 if (!dev->phydev) 645 return -ENODEV; 646 647 if (!ds->ops->set_mac_eee) 648 return -EOPNOTSUPP; 649 650 ret = ds->ops->set_mac_eee(ds, dp->index, e); 651 if (ret) 652 return ret; 653 654 if (e->eee_enabled) { 655 ret = phy_init_eee(dev->phydev, 0); 656 if (ret) 657 return ret; 658 } 659 660 return phy_ethtool_set_eee(dev->phydev, e); 661 } 662 663 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 664 { 665 struct dsa_port *dp = dsa_slave_to_port(dev); 666 struct dsa_switch *ds = dp->ds; 667 int ret; 668 669 /* Port's PHY and MAC both need to be EEE capable */ 670 if (!dev->phydev) 671 return -ENODEV; 672 673 if (!ds->ops->get_mac_eee) 674 return -EOPNOTSUPP; 675 676 ret = ds->ops->get_mac_eee(ds, dp->index, e); 677 if (ret) 678 return ret; 679 680 return phy_ethtool_get_eee(dev->phydev, e); 681 } 682 683 #ifdef CONFIG_NET_POLL_CONTROLLER 684 static int dsa_slave_netpoll_setup(struct net_device *dev, 685 struct netpoll_info *ni) 686 { 687 struct net_device *master = dsa_slave_to_master(dev); 688 struct dsa_slave_priv *p = netdev_priv(dev); 689 struct netpoll *netpoll; 690 int err = 0; 691 692 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 693 if (!netpoll) 694 return -ENOMEM; 695 696 err = __netpoll_setup(netpoll, master); 697 if (err) { 698 kfree(netpoll); 699 goto out; 700 } 701 702 p->netpoll = netpoll; 703 out: 704 return err; 705 } 706 707 static void dsa_slave_netpoll_cleanup(struct net_device *dev) 708 { 709 struct dsa_slave_priv *p = netdev_priv(dev); 710 struct netpoll *netpoll = p->netpoll; 711 712 if (!netpoll) 713 return; 714 715 p->netpoll = NULL; 716 717 __netpoll_free_async(netpoll); 718 } 719 720 static void dsa_slave_poll_controller(struct net_device *dev) 721 { 722 } 723 #endif 724 725 static int dsa_slave_get_phys_port_name(struct net_device *dev, 726 char *name, size_t len) 727 { 728 struct dsa_port *dp = dsa_slave_to_port(dev); 729 730 if (snprintf(name, len, "p%d", dp->index) >= len) 731 return -EINVAL; 732 733 return 0; 734 } 735 736 static struct dsa_mall_tc_entry * 737 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 738 { 739 struct dsa_slave_priv *p = netdev_priv(dev); 740 struct dsa_mall_tc_entry *mall_tc_entry; 741 742 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 743 if (mall_tc_entry->cookie == cookie) 744 return mall_tc_entry; 745 746 return NULL; 747 } 748 749 static int dsa_slave_add_cls_matchall(struct net_device *dev, 750 struct tc_cls_matchall_offload *cls, 751 bool ingress) 752 { 753 struct dsa_port *dp = dsa_slave_to_port(dev); 754 struct dsa_slave_priv *p = netdev_priv(dev); 755 struct dsa_mall_tc_entry *mall_tc_entry; 756 __be16 protocol = cls->common.protocol; 757 struct dsa_switch *ds = dp->ds; 758 struct net_device *to_dev; 759 const struct tc_action *a; 760 struct dsa_port *to_dp; 761 int err = -EOPNOTSUPP; 762 LIST_HEAD(actions); 763 764 if (!ds->ops->port_mirror_add) 765 return err; 766 767 if (!tcf_exts_has_one_action(cls->exts)) 768 return err; 769 770 tcf_exts_to_list(cls->exts, &actions); 771 a = list_first_entry(&actions, struct tc_action, list); 772 773 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { 774 struct dsa_mall_mirror_tc_entry *mirror; 775 776 to_dev = tcf_mirred_dev(a); 777 if (!to_dev) 778 return -EINVAL; 779 780 if (!dsa_slave_dev_check(to_dev)) 781 return -EOPNOTSUPP; 782 783 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 784 if (!mall_tc_entry) 785 return -ENOMEM; 786 787 mall_tc_entry->cookie = cls->cookie; 788 mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 789 mirror = &mall_tc_entry->mirror; 790 791 to_dp = dsa_slave_to_port(to_dev); 792 793 mirror->to_local_port = to_dp->index; 794 mirror->ingress = ingress; 795 796 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 797 if (err) { 798 kfree(mall_tc_entry); 799 return err; 800 } 801 802 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 803 } 804 805 return 0; 806 } 807 808 static void dsa_slave_del_cls_matchall(struct net_device *dev, 809 struct tc_cls_matchall_offload *cls) 810 { 811 struct dsa_port *dp = dsa_slave_to_port(dev); 812 struct dsa_mall_tc_entry *mall_tc_entry; 813 struct dsa_switch *ds = dp->ds; 814 815 if (!ds->ops->port_mirror_del) 816 return; 817 818 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 819 if (!mall_tc_entry) 820 return; 821 822 list_del(&mall_tc_entry->list); 823 824 switch (mall_tc_entry->type) { 825 case DSA_PORT_MALL_MIRROR: 826 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror); 827 break; 828 default: 829 WARN_ON(1); 830 } 831 832 kfree(mall_tc_entry); 833 } 834 835 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 836 struct tc_cls_matchall_offload *cls, 837 bool ingress) 838 { 839 if (cls->common.chain_index) 840 return -EOPNOTSUPP; 841 842 switch (cls->command) { 843 case TC_CLSMATCHALL_REPLACE: 844 return dsa_slave_add_cls_matchall(dev, cls, ingress); 845 case TC_CLSMATCHALL_DESTROY: 846 dsa_slave_del_cls_matchall(dev, cls); 847 return 0; 848 default: 849 return -EOPNOTSUPP; 850 } 851 } 852 853 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 854 void *cb_priv, bool ingress) 855 { 856 struct net_device *dev = cb_priv; 857 858 if (!tc_can_offload(dev)) 859 return -EOPNOTSUPP; 860 861 switch (type) { 862 case TC_SETUP_CLSMATCHALL: 863 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 864 default: 865 return -EOPNOTSUPP; 866 } 867 } 868 869 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 870 void *type_data, void *cb_priv) 871 { 872 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 873 } 874 875 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 876 void *type_data, void *cb_priv) 877 { 878 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 879 } 880 881 static int dsa_slave_setup_tc_block(struct net_device *dev, 882 struct tc_block_offload *f) 883 { 884 tc_setup_cb_t *cb; 885 886 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 887 cb = dsa_slave_setup_tc_block_cb_ig; 888 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 889 cb = dsa_slave_setup_tc_block_cb_eg; 890 else 891 return -EOPNOTSUPP; 892 893 switch (f->command) { 894 case TC_BLOCK_BIND: 895 return tcf_block_cb_register(f->block, cb, dev, dev); 896 case TC_BLOCK_UNBIND: 897 tcf_block_cb_unregister(f->block, cb, dev); 898 return 0; 899 default: 900 return -EOPNOTSUPP; 901 } 902 } 903 904 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 905 void *type_data) 906 { 907 switch (type) { 908 case TC_SETUP_BLOCK: 909 return dsa_slave_setup_tc_block(dev, type_data); 910 default: 911 return -EOPNOTSUPP; 912 } 913 } 914 915 static void dsa_slave_get_stats64(struct net_device *dev, 916 struct rtnl_link_stats64 *stats) 917 { 918 struct dsa_slave_priv *p = netdev_priv(dev); 919 struct pcpu_sw_netstats *s; 920 unsigned int start; 921 int i; 922 923 netdev_stats_to_stats64(stats, &dev->stats); 924 for_each_possible_cpu(i) { 925 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 926 927 s = per_cpu_ptr(p->stats64, i); 928 do { 929 start = u64_stats_fetch_begin_irq(&s->syncp); 930 tx_packets = s->tx_packets; 931 tx_bytes = s->tx_bytes; 932 rx_packets = s->rx_packets; 933 rx_bytes = s->rx_bytes; 934 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 935 936 stats->tx_packets += tx_packets; 937 stats->tx_bytes += tx_bytes; 938 stats->rx_packets += rx_packets; 939 stats->rx_bytes += rx_bytes; 940 } 941 } 942 943 static int dsa_slave_get_rxnfc(struct net_device *dev, 944 struct ethtool_rxnfc *nfc, u32 *rule_locs) 945 { 946 struct dsa_port *dp = dsa_slave_to_port(dev); 947 struct dsa_switch *ds = dp->ds; 948 949 if (!ds->ops->get_rxnfc) 950 return -EOPNOTSUPP; 951 952 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 953 } 954 955 static int dsa_slave_set_rxnfc(struct net_device *dev, 956 struct ethtool_rxnfc *nfc) 957 { 958 struct dsa_port *dp = dsa_slave_to_port(dev); 959 struct dsa_switch *ds = dp->ds; 960 961 if (!ds->ops->set_rxnfc) 962 return -EOPNOTSUPP; 963 964 return ds->ops->set_rxnfc(ds, dp->index, nfc); 965 } 966 967 static int dsa_slave_get_ts_info(struct net_device *dev, 968 struct ethtool_ts_info *ts) 969 { 970 struct dsa_slave_priv *p = netdev_priv(dev); 971 struct dsa_switch *ds = p->dp->ds; 972 973 if (!ds->ops->get_ts_info) 974 return -EOPNOTSUPP; 975 976 return ds->ops->get_ts_info(ds, p->dp->index, ts); 977 } 978 979 static const struct ethtool_ops dsa_slave_ethtool_ops = { 980 .get_drvinfo = dsa_slave_get_drvinfo, 981 .get_regs_len = dsa_slave_get_regs_len, 982 .get_regs = dsa_slave_get_regs, 983 .nway_reset = phy_ethtool_nway_reset, 984 .get_link = dsa_slave_get_link, 985 .get_eeprom_len = dsa_slave_get_eeprom_len, 986 .get_eeprom = dsa_slave_get_eeprom, 987 .set_eeprom = dsa_slave_set_eeprom, 988 .get_strings = dsa_slave_get_strings, 989 .get_ethtool_stats = dsa_slave_get_ethtool_stats, 990 .get_sset_count = dsa_slave_get_sset_count, 991 .set_wol = dsa_slave_set_wol, 992 .get_wol = dsa_slave_get_wol, 993 .set_eee = dsa_slave_set_eee, 994 .get_eee = dsa_slave_get_eee, 995 .get_link_ksettings = phy_ethtool_get_link_ksettings, 996 .set_link_ksettings = phy_ethtool_set_link_ksettings, 997 .get_rxnfc = dsa_slave_get_rxnfc, 998 .set_rxnfc = dsa_slave_set_rxnfc, 999 .get_ts_info = dsa_slave_get_ts_info, 1000 }; 1001 1002 /* legacy way, bypassing the bridge *****************************************/ 1003 int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 1004 struct net_device *dev, 1005 const unsigned char *addr, u16 vid, 1006 u16 flags) 1007 { 1008 struct dsa_port *dp = dsa_slave_to_port(dev); 1009 1010 return dsa_port_fdb_add(dp, addr, vid); 1011 } 1012 1013 int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 1014 struct net_device *dev, 1015 const unsigned char *addr, u16 vid) 1016 { 1017 struct dsa_port *dp = dsa_slave_to_port(dev); 1018 1019 return dsa_port_fdb_del(dp, addr, vid); 1020 } 1021 1022 static const struct net_device_ops dsa_slave_netdev_ops = { 1023 .ndo_open = dsa_slave_open, 1024 .ndo_stop = dsa_slave_close, 1025 .ndo_start_xmit = dsa_slave_xmit, 1026 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1027 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1028 .ndo_set_mac_address = dsa_slave_set_mac_address, 1029 .ndo_fdb_add = dsa_legacy_fdb_add, 1030 .ndo_fdb_del = dsa_legacy_fdb_del, 1031 .ndo_fdb_dump = dsa_slave_fdb_dump, 1032 .ndo_do_ioctl = dsa_slave_ioctl, 1033 .ndo_get_iflink = dsa_slave_get_iflink, 1034 #ifdef CONFIG_NET_POLL_CONTROLLER 1035 .ndo_netpoll_setup = dsa_slave_netpoll_setup, 1036 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 1037 .ndo_poll_controller = dsa_slave_poll_controller, 1038 #endif 1039 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1040 .ndo_setup_tc = dsa_slave_setup_tc, 1041 .ndo_get_stats64 = dsa_slave_get_stats64, 1042 }; 1043 1044 static const struct switchdev_ops dsa_slave_switchdev_ops = { 1045 .switchdev_port_attr_get = dsa_slave_port_attr_get, 1046 .switchdev_port_attr_set = dsa_slave_port_attr_set, 1047 .switchdev_port_obj_add = dsa_slave_port_obj_add, 1048 .switchdev_port_obj_del = dsa_slave_port_obj_del, 1049 }; 1050 1051 static struct device_type dsa_type = { 1052 .name = "dsa", 1053 }; 1054 1055 static void dsa_slave_adjust_link(struct net_device *dev) 1056 { 1057 struct dsa_port *dp = dsa_slave_to_port(dev); 1058 struct dsa_slave_priv *p = netdev_priv(dev); 1059 struct dsa_switch *ds = dp->ds; 1060 unsigned int status_changed = 0; 1061 1062 if (p->old_link != dev->phydev->link) { 1063 status_changed = 1; 1064 p->old_link = dev->phydev->link; 1065 } 1066 1067 if (p->old_duplex != dev->phydev->duplex) { 1068 status_changed = 1; 1069 p->old_duplex = dev->phydev->duplex; 1070 } 1071 1072 if (p->old_pause != dev->phydev->pause) { 1073 status_changed = 1; 1074 p->old_pause = dev->phydev->pause; 1075 } 1076 1077 if (ds->ops->adjust_link && status_changed) 1078 ds->ops->adjust_link(ds, dp->index, dev->phydev); 1079 1080 if (status_changed) 1081 phy_print_status(dev->phydev); 1082 } 1083 1084 static int dsa_slave_fixed_link_update(struct net_device *dev, 1085 struct fixed_phy_status *status) 1086 { 1087 struct dsa_switch *ds; 1088 struct dsa_port *dp; 1089 1090 if (dev) { 1091 dp = dsa_slave_to_port(dev); 1092 ds = dp->ds; 1093 if (ds->ops->fixed_link_update) 1094 ds->ops->fixed_link_update(ds, dp->index, status); 1095 } 1096 1097 return 0; 1098 } 1099 1100 /* slave device setup *******************************************************/ 1101 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1102 { 1103 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1104 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1105 struct dsa_switch *ds = dp->ds; 1106 1107 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 1108 if (!slave_dev->phydev) { 1109 netdev_err(slave_dev, "no phy at %d\n", addr); 1110 return -ENODEV; 1111 } 1112 1113 /* Use already configured phy mode */ 1114 if (p->phy_interface == PHY_INTERFACE_MODE_NA) 1115 p->phy_interface = slave_dev->phydev->interface; 1116 1117 return phy_connect_direct(slave_dev, slave_dev->phydev, 1118 dsa_slave_adjust_link, p->phy_interface); 1119 } 1120 1121 static int dsa_slave_phy_setup(struct net_device *slave_dev) 1122 { 1123 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1124 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1125 struct device_node *port_dn = dp->dn; 1126 struct dsa_switch *ds = dp->ds; 1127 struct device_node *phy_dn; 1128 bool phy_is_fixed = false; 1129 u32 phy_flags = 0; 1130 int mode, ret; 1131 1132 mode = of_get_phy_mode(port_dn); 1133 if (mode < 0) 1134 mode = PHY_INTERFACE_MODE_NA; 1135 p->phy_interface = mode; 1136 1137 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 1138 if (!phy_dn && of_phy_is_fixed_link(port_dn)) { 1139 /* In the case of a fixed PHY, the DT node associated 1140 * to the fixed PHY is the Port DT node 1141 */ 1142 ret = of_phy_register_fixed_link(port_dn); 1143 if (ret) { 1144 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); 1145 return ret; 1146 } 1147 phy_is_fixed = true; 1148 phy_dn = of_node_get(port_dn); 1149 } 1150 1151 if (ds->ops->get_phy_flags) 1152 phy_flags = ds->ops->get_phy_flags(ds, dp->index); 1153 1154 if (phy_dn) { 1155 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn, 1156 dsa_slave_adjust_link, 1157 phy_flags, 1158 p->phy_interface); 1159 of_node_put(phy_dn); 1160 } 1161 1162 if (slave_dev->phydev && phy_is_fixed) 1163 fixed_phy_set_link_update(slave_dev->phydev, 1164 dsa_slave_fixed_link_update); 1165 1166 /* We could not connect to a designated PHY, so use the switch internal 1167 * MDIO bus instead 1168 */ 1169 if (!slave_dev->phydev) { 1170 ret = dsa_slave_phy_connect(slave_dev, dp->index); 1171 if (ret) { 1172 netdev_err(slave_dev, "failed to connect to port %d: %d\n", 1173 dp->index, ret); 1174 if (phy_is_fixed) 1175 of_phy_deregister_fixed_link(port_dn); 1176 return ret; 1177 } 1178 } 1179 1180 phy_attached_info(slave_dev->phydev); 1181 1182 return 0; 1183 } 1184 1185 static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1186 static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1187 struct netdev_queue *txq, 1188 void *_unused) 1189 { 1190 lockdep_set_class(&txq->_xmit_lock, 1191 &dsa_slave_netdev_xmit_lock_key); 1192 } 1193 1194 int dsa_slave_suspend(struct net_device *slave_dev) 1195 { 1196 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1197 1198 if (!netif_running(slave_dev)) 1199 return 0; 1200 1201 netif_device_detach(slave_dev); 1202 1203 if (slave_dev->phydev) { 1204 phy_stop(slave_dev->phydev); 1205 p->old_pause = -1; 1206 p->old_link = -1; 1207 p->old_duplex = -1; 1208 phy_suspend(slave_dev->phydev); 1209 } 1210 1211 return 0; 1212 } 1213 1214 int dsa_slave_resume(struct net_device *slave_dev) 1215 { 1216 if (!netif_running(slave_dev)) 1217 return 0; 1218 1219 netif_device_attach(slave_dev); 1220 1221 if (slave_dev->phydev) { 1222 phy_resume(slave_dev->phydev); 1223 phy_start(slave_dev->phydev); 1224 } 1225 1226 return 0; 1227 } 1228 1229 static void dsa_slave_notify(struct net_device *dev, unsigned long val) 1230 { 1231 struct net_device *master = dsa_slave_to_master(dev); 1232 struct dsa_port *dp = dsa_slave_to_port(dev); 1233 struct dsa_notifier_register_info rinfo = { 1234 .switch_number = dp->ds->index, 1235 .port_number = dp->index, 1236 .master = master, 1237 .info.dev = dev, 1238 }; 1239 1240 call_dsa_notifiers(val, dev, &rinfo.info); 1241 } 1242 1243 int dsa_slave_create(struct dsa_port *port) 1244 { 1245 const struct dsa_port *cpu_dp = port->cpu_dp; 1246 struct net_device *master = cpu_dp->master; 1247 struct dsa_switch *ds = port->ds; 1248 const char *name = port->name; 1249 struct net_device *slave_dev; 1250 struct dsa_slave_priv *p; 1251 int ret; 1252 1253 if (!ds->num_tx_queues) 1254 ds->num_tx_queues = 1; 1255 1256 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 1257 NET_NAME_UNKNOWN, ether_setup, 1258 ds->num_tx_queues, 1); 1259 if (slave_dev == NULL) 1260 return -ENOMEM; 1261 1262 slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1263 slave_dev->hw_features |= NETIF_F_HW_TC; 1264 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 1265 eth_hw_addr_inherit(slave_dev, master); 1266 slave_dev->priv_flags |= IFF_NO_QUEUE; 1267 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 1268 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 1269 slave_dev->min_mtu = 0; 1270 slave_dev->max_mtu = ETH_MAX_MTU; 1271 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1272 1273 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1274 NULL); 1275 1276 SET_NETDEV_DEV(slave_dev, port->ds->dev); 1277 slave_dev->dev.of_node = port->dn; 1278 slave_dev->vlan_features = master->vlan_features; 1279 1280 p = netdev_priv(slave_dev); 1281 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1282 if (!p->stats64) { 1283 free_netdev(slave_dev); 1284 return -ENOMEM; 1285 } 1286 p->dp = port; 1287 INIT_LIST_HEAD(&p->mall_tc_list); 1288 p->xmit = cpu_dp->tag_ops->xmit; 1289 1290 p->old_pause = -1; 1291 p->old_link = -1; 1292 p->old_duplex = -1; 1293 1294 port->slave = slave_dev; 1295 1296 netif_carrier_off(slave_dev); 1297 1298 ret = dsa_slave_phy_setup(slave_dev); 1299 if (ret) { 1300 netdev_err(master, "error %d setting up slave phy\n", ret); 1301 goto out_free; 1302 } 1303 1304 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER); 1305 1306 ret = register_netdev(slave_dev); 1307 if (ret) { 1308 netdev_err(master, "error %d registering interface %s\n", 1309 ret, slave_dev->name); 1310 goto out_phy; 1311 } 1312 1313 return 0; 1314 1315 out_phy: 1316 phy_disconnect(slave_dev->phydev); 1317 if (of_phy_is_fixed_link(port->dn)) 1318 of_phy_deregister_fixed_link(port->dn); 1319 out_free: 1320 free_percpu(p->stats64); 1321 free_netdev(slave_dev); 1322 port->slave = NULL; 1323 return ret; 1324 } 1325 1326 void dsa_slave_destroy(struct net_device *slave_dev) 1327 { 1328 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1329 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1330 struct device_node *port_dn = dp->dn; 1331 1332 netif_carrier_off(slave_dev); 1333 if (slave_dev->phydev) { 1334 phy_disconnect(slave_dev->phydev); 1335 1336 if (of_phy_is_fixed_link(port_dn)) 1337 of_phy_deregister_fixed_link(port_dn); 1338 } 1339 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); 1340 unregister_netdev(slave_dev); 1341 free_percpu(p->stats64); 1342 free_netdev(slave_dev); 1343 } 1344 1345 static bool dsa_slave_dev_check(struct net_device *dev) 1346 { 1347 return dev->netdev_ops == &dsa_slave_netdev_ops; 1348 } 1349 1350 static int dsa_slave_changeupper(struct net_device *dev, 1351 struct netdev_notifier_changeupper_info *info) 1352 { 1353 struct dsa_port *dp = dsa_slave_to_port(dev); 1354 int err = NOTIFY_DONE; 1355 1356 if (netif_is_bridge_master(info->upper_dev)) { 1357 if (info->linking) { 1358 err = dsa_port_bridge_join(dp, info->upper_dev); 1359 err = notifier_from_errno(err); 1360 } else { 1361 dsa_port_bridge_leave(dp, info->upper_dev); 1362 err = NOTIFY_OK; 1363 } 1364 } 1365 1366 return err; 1367 } 1368 1369 static int dsa_slave_netdevice_event(struct notifier_block *nb, 1370 unsigned long event, void *ptr) 1371 { 1372 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1373 1374 if (!dsa_slave_dev_check(dev)) 1375 return NOTIFY_DONE; 1376 1377 if (event == NETDEV_CHANGEUPPER) 1378 return dsa_slave_changeupper(dev, ptr); 1379 1380 return NOTIFY_DONE; 1381 } 1382 1383 struct dsa_switchdev_event_work { 1384 struct work_struct work; 1385 struct switchdev_notifier_fdb_info fdb_info; 1386 struct net_device *dev; 1387 unsigned long event; 1388 }; 1389 1390 static void dsa_slave_switchdev_event_work(struct work_struct *work) 1391 { 1392 struct dsa_switchdev_event_work *switchdev_work = 1393 container_of(work, struct dsa_switchdev_event_work, work); 1394 struct net_device *dev = switchdev_work->dev; 1395 struct switchdev_notifier_fdb_info *fdb_info; 1396 struct dsa_port *dp = dsa_slave_to_port(dev); 1397 int err; 1398 1399 rtnl_lock(); 1400 switch (switchdev_work->event) { 1401 case SWITCHDEV_FDB_ADD_TO_DEVICE: 1402 fdb_info = &switchdev_work->fdb_info; 1403 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid); 1404 if (err) { 1405 netdev_dbg(dev, "fdb add failed err=%d\n", err); 1406 break; 1407 } 1408 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 1409 &fdb_info->info); 1410 break; 1411 1412 case SWITCHDEV_FDB_DEL_TO_DEVICE: 1413 fdb_info = &switchdev_work->fdb_info; 1414 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid); 1415 if (err) { 1416 netdev_dbg(dev, "fdb del failed err=%d\n", err); 1417 dev_close(dev); 1418 } 1419 break; 1420 } 1421 rtnl_unlock(); 1422 1423 kfree(switchdev_work->fdb_info.addr); 1424 kfree(switchdev_work); 1425 dev_put(dev); 1426 } 1427 1428 static int 1429 dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * 1430 switchdev_work, 1431 const struct switchdev_notifier_fdb_info * 1432 fdb_info) 1433 { 1434 memcpy(&switchdev_work->fdb_info, fdb_info, 1435 sizeof(switchdev_work->fdb_info)); 1436 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 1437 if (!switchdev_work->fdb_info.addr) 1438 return -ENOMEM; 1439 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 1440 fdb_info->addr); 1441 return 0; 1442 } 1443 1444 /* Called under rcu_read_lock() */ 1445 static int dsa_slave_switchdev_event(struct notifier_block *unused, 1446 unsigned long event, void *ptr) 1447 { 1448 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1449 struct dsa_switchdev_event_work *switchdev_work; 1450 1451 if (!dsa_slave_dev_check(dev)) 1452 return NOTIFY_DONE; 1453 1454 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 1455 if (!switchdev_work) 1456 return NOTIFY_BAD; 1457 1458 INIT_WORK(&switchdev_work->work, 1459 dsa_slave_switchdev_event_work); 1460 switchdev_work->dev = dev; 1461 switchdev_work->event = event; 1462 1463 switch (event) { 1464 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ 1465 case SWITCHDEV_FDB_DEL_TO_DEVICE: 1466 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, 1467 ptr)) 1468 goto err_fdb_work_init; 1469 dev_hold(dev); 1470 break; 1471 default: 1472 kfree(switchdev_work); 1473 return NOTIFY_DONE; 1474 } 1475 1476 dsa_schedule_work(&switchdev_work->work); 1477 return NOTIFY_OK; 1478 1479 err_fdb_work_init: 1480 kfree(switchdev_work); 1481 return NOTIFY_BAD; 1482 } 1483 1484 static struct notifier_block dsa_slave_nb __read_mostly = { 1485 .notifier_call = dsa_slave_netdevice_event, 1486 }; 1487 1488 static struct notifier_block dsa_slave_switchdev_notifier = { 1489 .notifier_call = dsa_slave_switchdev_event, 1490 }; 1491 1492 int dsa_slave_register_notifier(void) 1493 { 1494 int err; 1495 1496 err = register_netdevice_notifier(&dsa_slave_nb); 1497 if (err) 1498 return err; 1499 1500 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 1501 if (err) 1502 goto err_switchdev_nb; 1503 1504 return 0; 1505 1506 err_switchdev_nb: 1507 unregister_netdevice_notifier(&dsa_slave_nb); 1508 return err; 1509 } 1510 1511 void dsa_slave_unregister_notifier(void) 1512 { 1513 int err; 1514 1515 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 1516 if (err) 1517 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 1518 1519 err = unregister_netdevice_notifier(&dsa_slave_nb); 1520 if (err) 1521 pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 1522 } 1523
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.