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