1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/dsa/slave.c - Slave device handling 4 * Copyright (c) 2008-2009 Marvell Semiconductor 5 */ 6 7 #include <linux/list.h> 8 #include <linux/etherdevice.h> 9 #include <linux/netdevice.h> 10 #include <linux/phy.h> 11 #include <linux/phy_fixed.h> 12 #include <linux/phylink.h> 13 #include <linux/of_net.h> 14 #include <linux/of_mdio.h> 15 #include <linux/mdio.h> 16 #include <net/rtnetlink.h> 17 #include <net/pkt_cls.h> 18 #include <net/selftests.h> 19 #include <net/tc_act/tc_mirred.h> 20 #include <linux/if_bridge.h> 21 #include <linux/if_hsr.h> 22 #include <linux/netpoll.h> 23 24 #include "dsa_priv.h" 25 26 /* slave mii_bus handling ***************************************************/ 27 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 28 { 29 struct dsa_switch *ds = bus->priv; 30 31 if (ds->phys_mii_mask & (1 << addr)) 32 return ds->ops->phy_read(ds, addr, reg); 33 34 return 0xffff; 35 } 36 37 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 38 { 39 struct dsa_switch *ds = bus->priv; 40 41 if (ds->phys_mii_mask & (1 << addr)) 42 return ds->ops->phy_write(ds, addr, reg, val); 43 44 return 0; 45 } 46 47 void dsa_slave_mii_bus_init(struct dsa_switch *ds) 48 { 49 ds->slave_mii_bus->priv = (void *)ds; 50 ds->slave_mii_bus->name = "dsa slave smi"; 51 ds->slave_mii_bus->read = dsa_slave_phy_read; 52 ds->slave_mii_bus->write = dsa_slave_phy_write; 53 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 54 ds->dst->index, ds->index); 55 ds->slave_mii_bus->parent = ds->dev; 56 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 57 } 58 59 60 /* slave device handling ****************************************************/ 61 static int dsa_slave_get_iflink(const struct net_device *dev) 62 { 63 return dsa_slave_to_master(dev)->ifindex; 64 } 65 66 static int dsa_slave_open(struct net_device *dev) 67 { 68 struct net_device *master = dsa_slave_to_master(dev); 69 struct dsa_port *dp = dsa_slave_to_port(dev); 70 int err; 71 72 err = dev_open(master, NULL); 73 if (err < 0) { 74 netdev_err(dev, "failed to open master %s\n", master->name); 75 goto out; 76 } 77 78 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 79 err = dev_uc_add(master, dev->dev_addr); 80 if (err < 0) 81 goto out; 82 } 83 84 if (dev->flags & IFF_ALLMULTI) { 85 err = dev_set_allmulti(master, 1); 86 if (err < 0) 87 goto del_unicast; 88 } 89 if (dev->flags & IFF_PROMISC) { 90 err = dev_set_promiscuity(master, 1); 91 if (err < 0) 92 goto clear_allmulti; 93 } 94 95 err = dsa_port_enable_rt(dp, dev->phydev); 96 if (err) 97 goto clear_promisc; 98 99 return 0; 100 101 clear_promisc: 102 if (dev->flags & IFF_PROMISC) 103 dev_set_promiscuity(master, -1); 104 clear_allmulti: 105 if (dev->flags & IFF_ALLMULTI) 106 dev_set_allmulti(master, -1); 107 del_unicast: 108 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 109 dev_uc_del(master, dev->dev_addr); 110 out: 111 return err; 112 } 113 114 static int dsa_slave_close(struct net_device *dev) 115 { 116 struct net_device *master = dsa_slave_to_master(dev); 117 struct dsa_port *dp = dsa_slave_to_port(dev); 118 119 dsa_port_disable_rt(dp); 120 121 dev_mc_unsync(master, dev); 122 dev_uc_unsync(master, dev); 123 if (dev->flags & IFF_ALLMULTI) 124 dev_set_allmulti(master, -1); 125 if (dev->flags & IFF_PROMISC) 126 dev_set_promiscuity(master, -1); 127 128 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 129 dev_uc_del(master, dev->dev_addr); 130 131 return 0; 132 } 133 134 static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 135 { 136 struct net_device *master = dsa_slave_to_master(dev); 137 if (dev->flags & IFF_UP) { 138 if (change & IFF_ALLMULTI) 139 dev_set_allmulti(master, 140 dev->flags & IFF_ALLMULTI ? 1 : -1); 141 if (change & IFF_PROMISC) 142 dev_set_promiscuity(master, 143 dev->flags & IFF_PROMISC ? 1 : -1); 144 } 145 } 146 147 static void dsa_slave_set_rx_mode(struct net_device *dev) 148 { 149 struct net_device *master = dsa_slave_to_master(dev); 150 151 dev_mc_sync(master, dev); 152 dev_uc_sync(master, dev); 153 } 154 155 static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 156 { 157 struct net_device *master = dsa_slave_to_master(dev); 158 struct sockaddr *addr = a; 159 int err; 160 161 if (!is_valid_ether_addr(addr->sa_data)) 162 return -EADDRNOTAVAIL; 163 164 if (!(dev->flags & IFF_UP)) 165 goto out; 166 167 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 168 err = dev_uc_add(master, addr->sa_data); 169 if (err < 0) 170 return err; 171 } 172 173 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 174 dev_uc_del(master, dev->dev_addr); 175 176 out: 177 ether_addr_copy(dev->dev_addr, addr->sa_data); 178 179 return 0; 180 } 181 182 struct dsa_slave_dump_ctx { 183 struct net_device *dev; 184 struct sk_buff *skb; 185 struct netlink_callback *cb; 186 int idx; 187 }; 188 189 static int 190 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 191 bool is_static, void *data) 192 { 193 struct dsa_slave_dump_ctx *dump = data; 194 u32 portid = NETLINK_CB(dump->cb->skb).portid; 195 u32 seq = dump->cb->nlh->nlmsg_seq; 196 struct nlmsghdr *nlh; 197 struct ndmsg *ndm; 198 199 if (dump->idx < dump->cb->args[2]) 200 goto skip; 201 202 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 203 sizeof(*ndm), NLM_F_MULTI); 204 if (!nlh) 205 return -EMSGSIZE; 206 207 ndm = nlmsg_data(nlh); 208 ndm->ndm_family = AF_BRIDGE; 209 ndm->ndm_pad1 = 0; 210 ndm->ndm_pad2 = 0; 211 ndm->ndm_flags = NTF_SELF; 212 ndm->ndm_type = 0; 213 ndm->ndm_ifindex = dump->dev->ifindex; 214 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 215 216 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 217 goto nla_put_failure; 218 219 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 220 goto nla_put_failure; 221 222 nlmsg_end(dump->skb, nlh); 223 224 skip: 225 dump->idx++; 226 return 0; 227 228 nla_put_failure: 229 nlmsg_cancel(dump->skb, nlh); 230 return -EMSGSIZE; 231 } 232 233 static int 234 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 235 struct net_device *dev, struct net_device *filter_dev, 236 int *idx) 237 { 238 struct dsa_port *dp = dsa_slave_to_port(dev); 239 struct dsa_slave_dump_ctx dump = { 240 .dev = dev, 241 .skb = skb, 242 .cb = cb, 243 .idx = *idx, 244 }; 245 int err; 246 247 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 248 *idx = dump.idx; 249 250 return err; 251 } 252 253 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 254 { 255 struct dsa_slave_priv *p = netdev_priv(dev); 256 struct dsa_switch *ds = p->dp->ds; 257 int port = p->dp->index; 258 259 /* Pass through to switch driver if it supports timestamping */ 260 switch (cmd) { 261 case SIOCGHWTSTAMP: 262 if (ds->ops->port_hwtstamp_get) 263 return ds->ops->port_hwtstamp_get(ds, port, ifr); 264 break; 265 case SIOCSHWTSTAMP: 266 if (ds->ops->port_hwtstamp_set) 267 return ds->ops->port_hwtstamp_set(ds, port, ifr); 268 break; 269 } 270 271 return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 272 } 273 274 static int dsa_slave_port_attr_set(struct net_device *dev, 275 const struct switchdev_attr *attr, 276 struct netlink_ext_ack *extack) 277 { 278 struct dsa_port *dp = dsa_slave_to_port(dev); 279 int ret; 280 281 switch (attr->id) { 282 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 283 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 284 return -EOPNOTSUPP; 285 286 ret = dsa_port_set_state(dp, attr->u.stp_state); 287 break; 288 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 289 if (!dsa_port_offloads_bridge(dp, attr->orig_dev)) 290 return -EOPNOTSUPP; 291 292 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 293 extack); 294 break; 295 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 296 if (!dsa_port_offloads_bridge(dp, attr->orig_dev)) 297 return -EOPNOTSUPP; 298 299 ret = dsa_port_ageing_time(dp, attr->u.ageing_time); 300 break; 301 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 302 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 303 return -EOPNOTSUPP; 304 305 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 306 extack); 307 break; 308 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 309 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 310 return -EOPNOTSUPP; 311 312 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack); 313 break; 314 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER: 315 if (!dsa_port_offloads_bridge(dp, attr->orig_dev)) 316 return -EOPNOTSUPP; 317 318 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, extack); 319 break; 320 default: 321 ret = -EOPNOTSUPP; 322 break; 323 } 324 325 return ret; 326 } 327 328 /* Must be called under rcu_read_lock() */ 329 static int 330 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 331 const struct switchdev_obj_port_vlan *vlan) 332 { 333 struct net_device *upper_dev; 334 struct list_head *iter; 335 336 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 337 u16 vid; 338 339 if (!is_vlan_dev(upper_dev)) 340 continue; 341 342 vid = vlan_dev_vlan_id(upper_dev); 343 if (vid == vlan->vid) 344 return -EBUSY; 345 } 346 347 return 0; 348 } 349 350 static int dsa_slave_vlan_add(struct net_device *dev, 351 const struct switchdev_obj *obj, 352 struct netlink_ext_ack *extack) 353 { 354 struct net_device *master = dsa_slave_to_master(dev); 355 struct dsa_port *dp = dsa_slave_to_port(dev); 356 struct switchdev_obj_port_vlan vlan; 357 int err; 358 359 if (dsa_port_skip_vlan_configuration(dp)) { 360 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 361 return 0; 362 } 363 364 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 365 366 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 367 * the same VID. 368 */ 369 if (br_vlan_enabled(dp->bridge_dev)) { 370 rcu_read_lock(); 371 err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan); 372 rcu_read_unlock(); 373 if (err) { 374 NL_SET_ERR_MSG_MOD(extack, 375 "Port already has a VLAN upper with this VID"); 376 return err; 377 } 378 } 379 380 err = dsa_port_vlan_add(dp, &vlan, extack); 381 if (err) 382 return err; 383 384 /* We need the dedicated CPU port to be a member of the VLAN as well. 385 * Even though drivers often handle CPU membership in special ways, 386 * it doesn't make sense to program a PVID, so clear this flag. 387 */ 388 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 389 390 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack); 391 if (err) 392 return err; 393 394 return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid); 395 } 396 397 static int dsa_slave_port_obj_add(struct net_device *dev, 398 const struct switchdev_obj *obj, 399 struct netlink_ext_ack *extack) 400 { 401 struct dsa_port *dp = dsa_slave_to_port(dev); 402 int err; 403 404 switch (obj->id) { 405 case SWITCHDEV_OBJ_ID_PORT_MDB: 406 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 407 return -EOPNOTSUPP; 408 409 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 410 break; 411 case SWITCHDEV_OBJ_ID_HOST_MDB: 412 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 413 return -EOPNOTSUPP; 414 415 /* DSA can directly translate this to a normal MDB add, 416 * but on the CPU port. 417 */ 418 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 419 break; 420 case SWITCHDEV_OBJ_ID_PORT_VLAN: 421 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 422 return -EOPNOTSUPP; 423 424 err = dsa_slave_vlan_add(dev, obj, extack); 425 break; 426 case SWITCHDEV_OBJ_ID_MRP: 427 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 428 return -EOPNOTSUPP; 429 430 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj)); 431 break; 432 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 433 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 434 return -EOPNOTSUPP; 435 436 err = dsa_port_mrp_add_ring_role(dp, 437 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 438 break; 439 default: 440 err = -EOPNOTSUPP; 441 break; 442 } 443 444 return err; 445 } 446 447 static int dsa_slave_vlan_del(struct net_device *dev, 448 const struct switchdev_obj *obj) 449 { 450 struct net_device *master = dsa_slave_to_master(dev); 451 struct dsa_port *dp = dsa_slave_to_port(dev); 452 struct switchdev_obj_port_vlan *vlan; 453 int err; 454 455 if (dsa_port_skip_vlan_configuration(dp)) 456 return 0; 457 458 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 459 460 /* Do not deprogram the CPU port as it may be shared with other user 461 * ports which can be members of this VLAN as well. 462 */ 463 err = dsa_port_vlan_del(dp, vlan); 464 if (err) 465 return err; 466 467 vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid); 468 469 return 0; 470 } 471 472 static int dsa_slave_port_obj_del(struct net_device *dev, 473 const struct switchdev_obj *obj) 474 { 475 struct dsa_port *dp = dsa_slave_to_port(dev); 476 int err; 477 478 switch (obj->id) { 479 case SWITCHDEV_OBJ_ID_PORT_MDB: 480 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 481 return -EOPNOTSUPP; 482 483 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 484 break; 485 case SWITCHDEV_OBJ_ID_HOST_MDB: 486 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 487 return -EOPNOTSUPP; 488 489 /* DSA can directly translate this to a normal MDB add, 490 * but on the CPU port. 491 */ 492 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 493 break; 494 case SWITCHDEV_OBJ_ID_PORT_VLAN: 495 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 496 return -EOPNOTSUPP; 497 498 err = dsa_slave_vlan_del(dev, obj); 499 break; 500 case SWITCHDEV_OBJ_ID_MRP: 501 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 502 return -EOPNOTSUPP; 503 504 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj)); 505 break; 506 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 507 if (!dsa_port_offloads_bridge(dp, obj->orig_dev)) 508 return -EOPNOTSUPP; 509 510 err = dsa_port_mrp_del_ring_role(dp, 511 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 512 break; 513 default: 514 err = -EOPNOTSUPP; 515 break; 516 } 517 518 return err; 519 } 520 521 static int dsa_slave_get_port_parent_id(struct net_device *dev, 522 struct netdev_phys_item_id *ppid) 523 { 524 struct dsa_port *dp = dsa_slave_to_port(dev); 525 struct dsa_switch *ds = dp->ds; 526 struct dsa_switch_tree *dst = ds->dst; 527 528 /* For non-legacy ports, devlink is used and it takes 529 * care of the name generation. This ndo implementation 530 * should be removed with legacy support. 531 */ 532 if (dp->ds->devlink) 533 return -EOPNOTSUPP; 534 535 ppid->id_len = sizeof(dst->index); 536 memcpy(&ppid->id, &dst->index, ppid->id_len); 537 538 return 0; 539 } 540 541 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 542 struct sk_buff *skb) 543 { 544 #ifdef CONFIG_NET_POLL_CONTROLLER 545 struct dsa_slave_priv *p = netdev_priv(dev); 546 547 return netpoll_send_skb(p->netpoll, skb); 548 #else 549 BUG(); 550 return NETDEV_TX_OK; 551 #endif 552 } 553 554 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 555 struct sk_buff *skb) 556 { 557 struct dsa_switch *ds = p->dp->ds; 558 559 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 560 return; 561 562 if (!ds->ops->port_txtstamp) 563 return; 564 565 ds->ops->port_txtstamp(ds, p->dp->index, skb); 566 } 567 568 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 569 { 570 /* SKB for netpoll still need to be mangled with the protocol-specific 571 * tag to be successfully transmitted 572 */ 573 if (unlikely(netpoll_tx_running(dev))) 574 return dsa_slave_netpoll_send_skb(dev, skb); 575 576 /* Queue the SKB for transmission on the parent interface, but 577 * do not modify its EtherType 578 */ 579 skb->dev = dsa_slave_to_master(dev); 580 dev_queue_xmit(skb); 581 582 return NETDEV_TX_OK; 583 } 584 EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 585 586 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 587 { 588 int needed_headroom = dev->needed_headroom; 589 int needed_tailroom = dev->needed_tailroom; 590 591 /* For tail taggers, we need to pad short frames ourselves, to ensure 592 * that the tail tag does not fail at its role of being at the end of 593 * the packet, once the master interface pads the frame. Account for 594 * that pad length here, and pad later. 595 */ 596 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 597 needed_tailroom += ETH_ZLEN - skb->len; 598 /* skb_headroom() returns unsigned int... */ 599 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 600 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 601 602 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 603 /* No reallocation needed, yay! */ 604 return 0; 605 606 return pskb_expand_head(skb, needed_headroom, needed_tailroom, 607 GFP_ATOMIC); 608 } 609 610 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 611 { 612 struct dsa_slave_priv *p = netdev_priv(dev); 613 struct sk_buff *nskb; 614 615 dev_sw_netstats_tx_add(dev, 1, skb->len); 616 617 memset(skb->cb, 0, sizeof(skb->cb)); 618 619 /* Handle tx timestamp if any */ 620 dsa_skb_tx_timestamp(p, skb); 621 622 if (dsa_realloc_skb(skb, dev)) { 623 dev_kfree_skb_any(skb); 624 return NETDEV_TX_OK; 625 } 626 627 /* needed_tailroom should still be 'warm' in the cache line from 628 * dsa_realloc_skb(), which has also ensured that padding is safe. 629 */ 630 if (dev->needed_tailroom) 631 eth_skb_pad(skb); 632 633 /* Transmit function may have to reallocate the original SKB, 634 * in which case it must have freed it. Only free it here on error. 635 */ 636 nskb = p->xmit(skb, dev); 637 if (!nskb) { 638 kfree_skb(skb); 639 return NETDEV_TX_OK; 640 } 641 642 return dsa_enqueue_skb(nskb, dev); 643 } 644 645 /* ethtool operations *******************************************************/ 646 647 static void dsa_slave_get_drvinfo(struct net_device *dev, 648 struct ethtool_drvinfo *drvinfo) 649 { 650 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 651 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 652 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 653 } 654 655 static int dsa_slave_get_regs_len(struct net_device *dev) 656 { 657 struct dsa_port *dp = dsa_slave_to_port(dev); 658 struct dsa_switch *ds = dp->ds; 659 660 if (ds->ops->get_regs_len) 661 return ds->ops->get_regs_len(ds, dp->index); 662 663 return -EOPNOTSUPP; 664 } 665 666 static void 667 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 668 { 669 struct dsa_port *dp = dsa_slave_to_port(dev); 670 struct dsa_switch *ds = dp->ds; 671 672 if (ds->ops->get_regs) 673 ds->ops->get_regs(ds, dp->index, regs, _p); 674 } 675 676 static int dsa_slave_nway_reset(struct net_device *dev) 677 { 678 struct dsa_port *dp = dsa_slave_to_port(dev); 679 680 return phylink_ethtool_nway_reset(dp->pl); 681 } 682 683 static int dsa_slave_get_eeprom_len(struct net_device *dev) 684 { 685 struct dsa_port *dp = dsa_slave_to_port(dev); 686 struct dsa_switch *ds = dp->ds; 687 688 if (ds->cd && ds->cd->eeprom_len) 689 return ds->cd->eeprom_len; 690 691 if (ds->ops->get_eeprom_len) 692 return ds->ops->get_eeprom_len(ds); 693 694 return 0; 695 } 696 697 static int dsa_slave_get_eeprom(struct net_device *dev, 698 struct ethtool_eeprom *eeprom, u8 *data) 699 { 700 struct dsa_port *dp = dsa_slave_to_port(dev); 701 struct dsa_switch *ds = dp->ds; 702 703 if (ds->ops->get_eeprom) 704 return ds->ops->get_eeprom(ds, eeprom, data); 705 706 return -EOPNOTSUPP; 707 } 708 709 static int dsa_slave_set_eeprom(struct net_device *dev, 710 struct ethtool_eeprom *eeprom, u8 *data) 711 { 712 struct dsa_port *dp = dsa_slave_to_port(dev); 713 struct dsa_switch *ds = dp->ds; 714 715 if (ds->ops->set_eeprom) 716 return ds->ops->set_eeprom(ds, eeprom, data); 717 718 return -EOPNOTSUPP; 719 } 720 721 static void dsa_slave_get_strings(struct net_device *dev, 722 uint32_t stringset, uint8_t *data) 723 { 724 struct dsa_port *dp = dsa_slave_to_port(dev); 725 struct dsa_switch *ds = dp->ds; 726 727 if (stringset == ETH_SS_STATS) { 728 int len = ETH_GSTRING_LEN; 729 730 strncpy(data, "tx_packets", len); 731 strncpy(data + len, "tx_bytes", len); 732 strncpy(data + 2 * len, "rx_packets", len); 733 strncpy(data + 3 * len, "rx_bytes", len); 734 if (ds->ops->get_strings) 735 ds->ops->get_strings(ds, dp->index, stringset, 736 data + 4 * len); 737 } else if (stringset == ETH_SS_TEST) { 738 net_selftest_get_strings(data); 739 } 740 741 } 742 743 static void dsa_slave_get_ethtool_stats(struct net_device *dev, 744 struct ethtool_stats *stats, 745 uint64_t *data) 746 { 747 struct dsa_port *dp = dsa_slave_to_port(dev); 748 struct dsa_switch *ds = dp->ds; 749 struct pcpu_sw_netstats *s; 750 unsigned int start; 751 int i; 752 753 for_each_possible_cpu(i) { 754 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 755 756 s = per_cpu_ptr(dev->tstats, i); 757 do { 758 start = u64_stats_fetch_begin_irq(&s->syncp); 759 tx_packets = s->tx_packets; 760 tx_bytes = s->tx_bytes; 761 rx_packets = s->rx_packets; 762 rx_bytes = s->rx_bytes; 763 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 764 data[0] += tx_packets; 765 data[1] += tx_bytes; 766 data[2] += rx_packets; 767 data[3] += rx_bytes; 768 } 769 if (ds->ops->get_ethtool_stats) 770 ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 771 } 772 773 static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 774 { 775 struct dsa_port *dp = dsa_slave_to_port(dev); 776 struct dsa_switch *ds = dp->ds; 777 778 if (sset == ETH_SS_STATS) { 779 int count = 0; 780 781 if (ds->ops->get_sset_count) { 782 count = ds->ops->get_sset_count(ds, dp->index, sset); 783 if (count < 0) 784 return count; 785 } 786 787 return count + 4; 788 } else if (sset == ETH_SS_TEST) { 789 return net_selftest_get_count(); 790 } 791 792 return -EOPNOTSUPP; 793 } 794 795 static void dsa_slave_net_selftest(struct net_device *ndev, 796 struct ethtool_test *etest, u64 *buf) 797 { 798 struct dsa_port *dp = dsa_slave_to_port(ndev); 799 struct dsa_switch *ds = dp->ds; 800 801 if (ds->ops->self_test) { 802 ds->ops->self_test(ds, dp->index, etest, buf); 803 return; 804 } 805 806 net_selftest(ndev, etest, buf); 807 } 808 809 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 810 { 811 struct dsa_port *dp = dsa_slave_to_port(dev); 812 struct dsa_switch *ds = dp->ds; 813 814 phylink_ethtool_get_wol(dp->pl, w); 815 816 if (ds->ops->get_wol) 817 ds->ops->get_wol(ds, dp->index, w); 818 } 819 820 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 821 { 822 struct dsa_port *dp = dsa_slave_to_port(dev); 823 struct dsa_switch *ds = dp->ds; 824 int ret = -EOPNOTSUPP; 825 826 phylink_ethtool_set_wol(dp->pl, w); 827 828 if (ds->ops->set_wol) 829 ret = ds->ops->set_wol(ds, dp->index, w); 830 831 return ret; 832 } 833 834 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 835 { 836 struct dsa_port *dp = dsa_slave_to_port(dev); 837 struct dsa_switch *ds = dp->ds; 838 int ret; 839 840 /* Port's PHY and MAC both need to be EEE capable */ 841 if (!dev->phydev || !dp->pl) 842 return -ENODEV; 843 844 if (!ds->ops->set_mac_eee) 845 return -EOPNOTSUPP; 846 847 ret = ds->ops->set_mac_eee(ds, dp->index, e); 848 if (ret) 849 return ret; 850 851 return phylink_ethtool_set_eee(dp->pl, e); 852 } 853 854 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 855 { 856 struct dsa_port *dp = dsa_slave_to_port(dev); 857 struct dsa_switch *ds = dp->ds; 858 int ret; 859 860 /* Port's PHY and MAC both need to be EEE capable */ 861 if (!dev->phydev || !dp->pl) 862 return -ENODEV; 863 864 if (!ds->ops->get_mac_eee) 865 return -EOPNOTSUPP; 866 867 ret = ds->ops->get_mac_eee(ds, dp->index, e); 868 if (ret) 869 return ret; 870 871 return phylink_ethtool_get_eee(dp->pl, e); 872 } 873 874 static int dsa_slave_get_link_ksettings(struct net_device *dev, 875 struct ethtool_link_ksettings *cmd) 876 { 877 struct dsa_port *dp = dsa_slave_to_port(dev); 878 879 return phylink_ethtool_ksettings_get(dp->pl, cmd); 880 } 881 882 static int dsa_slave_set_link_ksettings(struct net_device *dev, 883 const struct ethtool_link_ksettings *cmd) 884 { 885 struct dsa_port *dp = dsa_slave_to_port(dev); 886 887 return phylink_ethtool_ksettings_set(dp->pl, cmd); 888 } 889 890 static void dsa_slave_get_pauseparam(struct net_device *dev, 891 struct ethtool_pauseparam *pause) 892 { 893 struct dsa_port *dp = dsa_slave_to_port(dev); 894 895 phylink_ethtool_get_pauseparam(dp->pl, pause); 896 } 897 898 static int dsa_slave_set_pauseparam(struct net_device *dev, 899 struct ethtool_pauseparam *pause) 900 { 901 struct dsa_port *dp = dsa_slave_to_port(dev); 902 903 return phylink_ethtool_set_pauseparam(dp->pl, pause); 904 } 905 906 #ifdef CONFIG_NET_POLL_CONTROLLER 907 static int dsa_slave_netpoll_setup(struct net_device *dev, 908 struct netpoll_info *ni) 909 { 910 struct net_device *master = dsa_slave_to_master(dev); 911 struct dsa_slave_priv *p = netdev_priv(dev); 912 struct netpoll *netpoll; 913 int err = 0; 914 915 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 916 if (!netpoll) 917 return -ENOMEM; 918 919 err = __netpoll_setup(netpoll, master); 920 if (err) { 921 kfree(netpoll); 922 goto out; 923 } 924 925 p->netpoll = netpoll; 926 out: 927 return err; 928 } 929 930 static void dsa_slave_netpoll_cleanup(struct net_device *dev) 931 { 932 struct dsa_slave_priv *p = netdev_priv(dev); 933 struct netpoll *netpoll = p->netpoll; 934 935 if (!netpoll) 936 return; 937 938 p->netpoll = NULL; 939 940 __netpoll_free(netpoll); 941 } 942 943 static void dsa_slave_poll_controller(struct net_device *dev) 944 { 945 } 946 #endif 947 948 static int dsa_slave_get_phys_port_name(struct net_device *dev, 949 char *name, size_t len) 950 { 951 struct dsa_port *dp = dsa_slave_to_port(dev); 952 953 /* For non-legacy ports, devlink is used and it takes 954 * care of the name generation. This ndo implementation 955 * should be removed with legacy support. 956 */ 957 if (dp->ds->devlink) 958 return -EOPNOTSUPP; 959 960 if (snprintf(name, len, "p%d", dp->index) >= len) 961 return -EINVAL; 962 963 return 0; 964 } 965 966 static struct dsa_mall_tc_entry * 967 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 968 { 969 struct dsa_slave_priv *p = netdev_priv(dev); 970 struct dsa_mall_tc_entry *mall_tc_entry; 971 972 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 973 if (mall_tc_entry->cookie == cookie) 974 return mall_tc_entry; 975 976 return NULL; 977 } 978 979 static int 980 dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 981 struct tc_cls_matchall_offload *cls, 982 bool ingress) 983 { 984 struct dsa_port *dp = dsa_slave_to_port(dev); 985 struct dsa_slave_priv *p = netdev_priv(dev); 986 struct dsa_mall_mirror_tc_entry *mirror; 987 struct dsa_mall_tc_entry *mall_tc_entry; 988 struct dsa_switch *ds = dp->ds; 989 struct flow_action_entry *act; 990 struct dsa_port *to_dp; 991 int err; 992 993 if (!ds->ops->port_mirror_add) 994 return -EOPNOTSUPP; 995 996 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 997 cls->common.extack)) 998 return -EOPNOTSUPP; 999 1000 act = &cls->rule->action.entries[0]; 1001 1002 if (!act->dev) 1003 return -EINVAL; 1004 1005 if (!dsa_slave_dev_check(act->dev)) 1006 return -EOPNOTSUPP; 1007 1008 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1009 if (!mall_tc_entry) 1010 return -ENOMEM; 1011 1012 mall_tc_entry->cookie = cls->cookie; 1013 mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1014 mirror = &mall_tc_entry->mirror; 1015 1016 to_dp = dsa_slave_to_port(act->dev); 1017 1018 mirror->to_local_port = to_dp->index; 1019 mirror->ingress = ingress; 1020 1021 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 1022 if (err) { 1023 kfree(mall_tc_entry); 1024 return err; 1025 } 1026 1027 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1028 1029 return err; 1030 } 1031 1032 static int 1033 dsa_slave_add_cls_matchall_police(struct net_device *dev, 1034 struct tc_cls_matchall_offload *cls, 1035 bool ingress) 1036 { 1037 struct netlink_ext_ack *extack = cls->common.extack; 1038 struct dsa_port *dp = dsa_slave_to_port(dev); 1039 struct dsa_slave_priv *p = netdev_priv(dev); 1040 struct dsa_mall_policer_tc_entry *policer; 1041 struct dsa_mall_tc_entry *mall_tc_entry; 1042 struct dsa_switch *ds = dp->ds; 1043 struct flow_action_entry *act; 1044 int err; 1045 1046 if (!ds->ops->port_policer_add) { 1047 NL_SET_ERR_MSG_MOD(extack, 1048 "Policing offload not implemented"); 1049 return -EOPNOTSUPP; 1050 } 1051 1052 if (!ingress) { 1053 NL_SET_ERR_MSG_MOD(extack, 1054 "Only supported on ingress qdisc"); 1055 return -EOPNOTSUPP; 1056 } 1057 1058 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1059 cls->common.extack)) 1060 return -EOPNOTSUPP; 1061 1062 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 1063 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 1064 NL_SET_ERR_MSG_MOD(extack, 1065 "Only one port policer allowed"); 1066 return -EEXIST; 1067 } 1068 } 1069 1070 act = &cls->rule->action.entries[0]; 1071 1072 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1073 if (!mall_tc_entry) 1074 return -ENOMEM; 1075 1076 mall_tc_entry->cookie = cls->cookie; 1077 mall_tc_entry->type = DSA_PORT_MALL_POLICER; 1078 policer = &mall_tc_entry->policer; 1079 policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 1080 policer->burst = act->police.burst; 1081 1082 err = ds->ops->port_policer_add(ds, dp->index, policer); 1083 if (err) { 1084 kfree(mall_tc_entry); 1085 return err; 1086 } 1087 1088 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1089 1090 return err; 1091 } 1092 1093 static int dsa_slave_add_cls_matchall(struct net_device *dev, 1094 struct tc_cls_matchall_offload *cls, 1095 bool ingress) 1096 { 1097 int err = -EOPNOTSUPP; 1098 1099 if (cls->common.protocol == htons(ETH_P_ALL) && 1100 flow_offload_has_one_action(&cls->rule->action) && 1101 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1102 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 1103 else if (flow_offload_has_one_action(&cls->rule->action) && 1104 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 1105 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1106 1107 return err; 1108 } 1109 1110 static void dsa_slave_del_cls_matchall(struct net_device *dev, 1111 struct tc_cls_matchall_offload *cls) 1112 { 1113 struct dsa_port *dp = dsa_slave_to_port(dev); 1114 struct dsa_mall_tc_entry *mall_tc_entry; 1115 struct dsa_switch *ds = dp->ds; 1116 1117 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1118 if (!mall_tc_entry) 1119 return; 1120 1121 list_del(&mall_tc_entry->list); 1122 1123 switch (mall_tc_entry->type) { 1124 case DSA_PORT_MALL_MIRROR: 1125 if (ds->ops->port_mirror_del) 1126 ds->ops->port_mirror_del(ds, dp->index, 1127 &mall_tc_entry->mirror); 1128 break; 1129 case DSA_PORT_MALL_POLICER: 1130 if (ds->ops->port_policer_del) 1131 ds->ops->port_policer_del(ds, dp->index); 1132 break; 1133 default: 1134 WARN_ON(1); 1135 } 1136 1137 kfree(mall_tc_entry); 1138 } 1139 1140 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 1141 struct tc_cls_matchall_offload *cls, 1142 bool ingress) 1143 { 1144 if (cls->common.chain_index) 1145 return -EOPNOTSUPP; 1146 1147 switch (cls->command) { 1148 case TC_CLSMATCHALL_REPLACE: 1149 return dsa_slave_add_cls_matchall(dev, cls, ingress); 1150 case TC_CLSMATCHALL_DESTROY: 1151 dsa_slave_del_cls_matchall(dev, cls); 1152 return 0; 1153 default: 1154 return -EOPNOTSUPP; 1155 } 1156 } 1157 1158 static int dsa_slave_add_cls_flower(struct net_device *dev, 1159 struct flow_cls_offload *cls, 1160 bool ingress) 1161 { 1162 struct dsa_port *dp = dsa_slave_to_port(dev); 1163 struct dsa_switch *ds = dp->ds; 1164 int port = dp->index; 1165 1166 if (!ds->ops->cls_flower_add) 1167 return -EOPNOTSUPP; 1168 1169 return ds->ops->cls_flower_add(ds, port, cls, ingress); 1170 } 1171 1172 static int dsa_slave_del_cls_flower(struct net_device *dev, 1173 struct flow_cls_offload *cls, 1174 bool ingress) 1175 { 1176 struct dsa_port *dp = dsa_slave_to_port(dev); 1177 struct dsa_switch *ds = dp->ds; 1178 int port = dp->index; 1179 1180 if (!ds->ops->cls_flower_del) 1181 return -EOPNOTSUPP; 1182 1183 return ds->ops->cls_flower_del(ds, port, cls, ingress); 1184 } 1185 1186 static int dsa_slave_stats_cls_flower(struct net_device *dev, 1187 struct flow_cls_offload *cls, 1188 bool ingress) 1189 { 1190 struct dsa_port *dp = dsa_slave_to_port(dev); 1191 struct dsa_switch *ds = dp->ds; 1192 int port = dp->index; 1193 1194 if (!ds->ops->cls_flower_stats) 1195 return -EOPNOTSUPP; 1196 1197 return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1198 } 1199 1200 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1201 struct flow_cls_offload *cls, 1202 bool ingress) 1203 { 1204 switch (cls->command) { 1205 case FLOW_CLS_REPLACE: 1206 return dsa_slave_add_cls_flower(dev, cls, ingress); 1207 case FLOW_CLS_DESTROY: 1208 return dsa_slave_del_cls_flower(dev, cls, ingress); 1209 case FLOW_CLS_STATS: 1210 return dsa_slave_stats_cls_flower(dev, cls, ingress); 1211 default: 1212 return -EOPNOTSUPP; 1213 } 1214 } 1215 1216 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 1217 void *cb_priv, bool ingress) 1218 { 1219 struct net_device *dev = cb_priv; 1220 1221 if (!tc_can_offload(dev)) 1222 return -EOPNOTSUPP; 1223 1224 switch (type) { 1225 case TC_SETUP_CLSMATCHALL: 1226 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1227 case TC_SETUP_CLSFLOWER: 1228 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 1229 default: 1230 return -EOPNOTSUPP; 1231 } 1232 } 1233 1234 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 1235 void *type_data, void *cb_priv) 1236 { 1237 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 1238 } 1239 1240 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 1241 void *type_data, void *cb_priv) 1242 { 1243 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 1244 } 1245 1246 static LIST_HEAD(dsa_slave_block_cb_list); 1247 1248 static int dsa_slave_setup_tc_block(struct net_device *dev, 1249 struct flow_block_offload *f) 1250 { 1251 struct flow_block_cb *block_cb; 1252 flow_setup_cb_t *cb; 1253 1254 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 1255 cb = dsa_slave_setup_tc_block_cb_ig; 1256 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 1257 cb = dsa_slave_setup_tc_block_cb_eg; 1258 else 1259 return -EOPNOTSUPP; 1260 1261 f->driver_block_list = &dsa_slave_block_cb_list; 1262 1263 switch (f->command) { 1264 case FLOW_BLOCK_BIND: 1265 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 1266 return -EBUSY; 1267 1268 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1269 if (IS_ERR(block_cb)) 1270 return PTR_ERR(block_cb); 1271 1272 flow_block_cb_add(block_cb, f); 1273 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1274 return 0; 1275 case FLOW_BLOCK_UNBIND: 1276 block_cb = flow_block_cb_lookup(f->block, cb, dev); 1277 if (!block_cb) 1278 return -ENOENT; 1279 1280 flow_block_cb_remove(block_cb, f); 1281 list_del(&block_cb->driver_list); 1282 return 0; 1283 default: 1284 return -EOPNOTSUPP; 1285 } 1286 } 1287 1288 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 1289 void *type_data) 1290 { 1291 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1292 struct net_device *master = cpu_dp->master; 1293 1294 if (!master->netdev_ops->ndo_setup_tc) 1295 return -EOPNOTSUPP; 1296 1297 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 1298 } 1299 1300 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1301 void *type_data) 1302 { 1303 struct dsa_port *dp = dsa_slave_to_port(dev); 1304 struct dsa_switch *ds = dp->ds; 1305 1306 switch (type) { 1307 case TC_SETUP_BLOCK: 1308 return dsa_slave_setup_tc_block(dev, type_data); 1309 case TC_SETUP_FT: 1310 return dsa_slave_setup_ft_block(ds, dp->index, type_data); 1311 default: 1312 break; 1313 } 1314 1315 if (!ds->ops->port_setup_tc) 1316 return -EOPNOTSUPP; 1317 1318 return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1319 } 1320 1321 static int dsa_slave_get_rxnfc(struct net_device *dev, 1322 struct ethtool_rxnfc *nfc, u32 *rule_locs) 1323 { 1324 struct dsa_port *dp = dsa_slave_to_port(dev); 1325 struct dsa_switch *ds = dp->ds; 1326 1327 if (!ds->ops->get_rxnfc) 1328 return -EOPNOTSUPP; 1329 1330 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1331 } 1332 1333 static int dsa_slave_set_rxnfc(struct net_device *dev, 1334 struct ethtool_rxnfc *nfc) 1335 { 1336 struct dsa_port *dp = dsa_slave_to_port(dev); 1337 struct dsa_switch *ds = dp->ds; 1338 1339 if (!ds->ops->set_rxnfc) 1340 return -EOPNOTSUPP; 1341 1342 return ds->ops->set_rxnfc(ds, dp->index, nfc); 1343 } 1344 1345 static int dsa_slave_get_ts_info(struct net_device *dev, 1346 struct ethtool_ts_info *ts) 1347 { 1348 struct dsa_slave_priv *p = netdev_priv(dev); 1349 struct dsa_switch *ds = p->dp->ds; 1350 1351 if (!ds->ops->get_ts_info) 1352 return -EOPNOTSUPP; 1353 1354 return ds->ops->get_ts_info(ds, p->dp->index, ts); 1355 } 1356 1357 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1358 u16 vid) 1359 { 1360 struct net_device *master = dsa_slave_to_master(dev); 1361 struct dsa_port *dp = dsa_slave_to_port(dev); 1362 struct switchdev_obj_port_vlan vlan = { 1363 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1364 .vid = vid, 1365 /* This API only allows programming tagged, non-PVID VIDs */ 1366 .flags = 0, 1367 }; 1368 struct netlink_ext_ack extack = {0}; 1369 int ret; 1370 1371 /* User port... */ 1372 ret = dsa_port_vlan_add(dp, &vlan, &extack); 1373 if (ret) { 1374 if (extack._msg) 1375 netdev_err(dev, "%s\n", extack._msg); 1376 return ret; 1377 } 1378 1379 /* And CPU port... */ 1380 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack); 1381 if (ret) { 1382 if (extack._msg) 1383 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 1384 extack._msg); 1385 return ret; 1386 } 1387 1388 return vlan_vid_add(master, proto, vid); 1389 } 1390 1391 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1392 u16 vid) 1393 { 1394 struct net_device *master = dsa_slave_to_master(dev); 1395 struct dsa_port *dp = dsa_slave_to_port(dev); 1396 struct switchdev_obj_port_vlan vlan = { 1397 .vid = vid, 1398 /* This API only allows programming tagged, non-PVID VIDs */ 1399 .flags = 0, 1400 }; 1401 int err; 1402 1403 /* Do not deprogram the CPU port as it may be shared with other user 1404 * ports which can be members of this VLAN as well. 1405 */ 1406 err = dsa_port_vlan_del(dp, &vlan); 1407 if (err) 1408 return err; 1409 1410 vlan_vid_del(master, proto, vid); 1411 1412 return 0; 1413 } 1414 1415 struct dsa_hw_port { 1416 struct list_head list; 1417 struct net_device *dev; 1418 int old_mtu; 1419 }; 1420 1421 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1422 { 1423 const struct dsa_hw_port *p; 1424 int err; 1425 1426 list_for_each_entry(p, hw_port_list, list) { 1427 if (p->dev->mtu == mtu) 1428 continue; 1429 1430 err = dev_set_mtu(p->dev, mtu); 1431 if (err) 1432 goto rollback; 1433 } 1434 1435 return 0; 1436 1437 rollback: 1438 list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1439 if (p->dev->mtu == p->old_mtu) 1440 continue; 1441 1442 if (dev_set_mtu(p->dev, p->old_mtu)) 1443 netdev_err(p->dev, "Failed to restore MTU\n"); 1444 } 1445 1446 return err; 1447 } 1448 1449 static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1450 { 1451 struct dsa_hw_port *p, *n; 1452 1453 list_for_each_entry_safe(p, n, hw_port_list, list) 1454 kfree(p); 1455 } 1456 1457 /* Make the hardware datapath to/from @dev limited to a common MTU */ 1458 static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1459 { 1460 struct list_head hw_port_list; 1461 struct dsa_switch_tree *dst; 1462 int min_mtu = ETH_MAX_MTU; 1463 struct dsa_port *other_dp; 1464 int err; 1465 1466 if (!dp->ds->mtu_enforcement_ingress) 1467 return; 1468 1469 if (!dp->bridge_dev) 1470 return; 1471 1472 INIT_LIST_HEAD(&hw_port_list); 1473 1474 /* Populate the list of ports that are part of the same bridge 1475 * as the newly added/modified port 1476 */ 1477 list_for_each_entry(dst, &dsa_tree_list, list) { 1478 list_for_each_entry(other_dp, &dst->ports, list) { 1479 struct dsa_hw_port *hw_port; 1480 struct net_device *slave; 1481 1482 if (other_dp->type != DSA_PORT_TYPE_USER) 1483 continue; 1484 1485 if (other_dp->bridge_dev != dp->bridge_dev) 1486 continue; 1487 1488 if (!other_dp->ds->mtu_enforcement_ingress) 1489 continue; 1490 1491 slave = other_dp->slave; 1492 1493 if (min_mtu > slave->mtu) 1494 min_mtu = slave->mtu; 1495 1496 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1497 if (!hw_port) 1498 goto out; 1499 1500 hw_port->dev = slave; 1501 hw_port->old_mtu = slave->mtu; 1502 1503 list_add(&hw_port->list, &hw_port_list); 1504 } 1505 } 1506 1507 /* Attempt to configure the entire hardware bridge to the newly added 1508 * interface's MTU first, regardless of whether the intention of the 1509 * user was to raise or lower it. 1510 */ 1511 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1512 if (!err) 1513 goto out; 1514 1515 /* Clearly that didn't work out so well, so just set the minimum MTU on 1516 * all hardware bridge ports now. If this fails too, then all ports will 1517 * still have their old MTU rolled back anyway. 1518 */ 1519 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1520 1521 out: 1522 dsa_hw_port_list_free(&hw_port_list); 1523 } 1524 1525 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1526 { 1527 struct net_device *master = dsa_slave_to_master(dev); 1528 struct dsa_port *dp = dsa_slave_to_port(dev); 1529 struct dsa_slave_priv *p = netdev_priv(dev); 1530 struct dsa_switch *ds = p->dp->ds; 1531 struct dsa_port *cpu_dp; 1532 int port = p->dp->index; 1533 int largest_mtu = 0; 1534 int new_master_mtu; 1535 int old_master_mtu; 1536 int mtu_limit; 1537 int cpu_mtu; 1538 int err, i; 1539 1540 if (!ds->ops->port_change_mtu) 1541 return -EOPNOTSUPP; 1542 1543 for (i = 0; i < ds->num_ports; i++) { 1544 int slave_mtu; 1545 1546 if (!dsa_is_user_port(ds, i)) 1547 continue; 1548 1549 /* During probe, this function will be called for each slave 1550 * device, while not all of them have been allocated. That's 1551 * ok, it doesn't change what the maximum is, so ignore it. 1552 */ 1553 if (!dsa_to_port(ds, i)->slave) 1554 continue; 1555 1556 /* Pretend that we already applied the setting, which we 1557 * actually haven't (still haven't done all integrity checks) 1558 */ 1559 if (i == port) 1560 slave_mtu = new_mtu; 1561 else 1562 slave_mtu = dsa_to_port(ds, i)->slave->mtu; 1563 1564 if (largest_mtu < slave_mtu) 1565 largest_mtu = slave_mtu; 1566 } 1567 1568 cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1569 1570 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1571 old_master_mtu = master->mtu; 1572 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops); 1573 if (new_master_mtu > mtu_limit) 1574 return -ERANGE; 1575 1576 /* If the master MTU isn't over limit, there's no need to check the CPU 1577 * MTU, since that surely isn't either. 1578 */ 1579 cpu_mtu = largest_mtu; 1580 1581 /* Start applying stuff */ 1582 if (new_master_mtu != old_master_mtu) { 1583 err = dev_set_mtu(master, new_master_mtu); 1584 if (err < 0) 1585 goto out_master_failed; 1586 1587 /* We only need to propagate the MTU of the CPU port to 1588 * upstream switches. 1589 */ 1590 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true); 1591 if (err) 1592 goto out_cpu_failed; 1593 } 1594 1595 err = dsa_port_mtu_change(dp, new_mtu, false); 1596 if (err) 1597 goto out_port_failed; 1598 1599 dev->mtu = new_mtu; 1600 1601 dsa_bridge_mtu_normalization(dp); 1602 1603 return 0; 1604 1605 out_port_failed: 1606 if (new_master_mtu != old_master_mtu) 1607 dsa_port_mtu_change(cpu_dp, old_master_mtu - 1608 dsa_tag_protocol_overhead(cpu_dp->tag_ops), 1609 true); 1610 out_cpu_failed: 1611 if (new_master_mtu != old_master_mtu) 1612 dev_set_mtu(master, old_master_mtu); 1613 out_master_failed: 1614 return err; 1615 } 1616 1617 static const struct ethtool_ops dsa_slave_ethtool_ops = { 1618 .get_drvinfo = dsa_slave_get_drvinfo, 1619 .get_regs_len = dsa_slave_get_regs_len, 1620 .get_regs = dsa_slave_get_regs, 1621 .nway_reset = dsa_slave_nway_reset, 1622 .get_link = ethtool_op_get_link, 1623 .get_eeprom_len = dsa_slave_get_eeprom_len, 1624 .get_eeprom = dsa_slave_get_eeprom, 1625 .set_eeprom = dsa_slave_set_eeprom, 1626 .get_strings = dsa_slave_get_strings, 1627 .get_ethtool_stats = dsa_slave_get_ethtool_stats, 1628 .get_sset_count = dsa_slave_get_sset_count, 1629 .set_wol = dsa_slave_set_wol, 1630 .get_wol = dsa_slave_get_wol, 1631 .set_eee = dsa_slave_set_eee, 1632 .get_eee = dsa_slave_get_eee, 1633 .get_link_ksettings = dsa_slave_get_link_ksettings, 1634 .set_link_ksettings = dsa_slave_set_link_ksettings, 1635 .get_pauseparam = dsa_slave_get_pauseparam, 1636 .set_pauseparam = dsa_slave_set_pauseparam, 1637 .get_rxnfc = dsa_slave_get_rxnfc, 1638 .set_rxnfc = dsa_slave_set_rxnfc, 1639 .get_ts_info = dsa_slave_get_ts_info, 1640 .self_test = dsa_slave_net_selftest, 1641 }; 1642 1643 /* legacy way, bypassing the bridge *****************************************/ 1644 static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 1645 struct net_device *dev, 1646 const unsigned char *addr, u16 vid, 1647 u16 flags, 1648 struct netlink_ext_ack *extack) 1649 { 1650 struct dsa_port *dp = dsa_slave_to_port(dev); 1651 1652 return dsa_port_fdb_add(dp, addr, vid); 1653 } 1654 1655 static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 1656 struct net_device *dev, 1657 const unsigned char *addr, u16 vid) 1658 { 1659 struct dsa_port *dp = dsa_slave_to_port(dev); 1660 1661 return dsa_port_fdb_del(dp, addr, vid); 1662 } 1663 1664 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 1665 { 1666 struct dsa_port *dp = dsa_slave_to_port(dev); 1667 1668 return dp->ds->devlink ? &dp->devlink_port : NULL; 1669 } 1670 1671 static void dsa_slave_get_stats64(struct net_device *dev, 1672 struct rtnl_link_stats64 *s) 1673 { 1674 struct dsa_port *dp = dsa_slave_to_port(dev); 1675 struct dsa_switch *ds = dp->ds; 1676 1677 if (ds->ops->get_stats64) 1678 ds->ops->get_stats64(ds, dp->index, s); 1679 else 1680 dev_get_tstats64(dev, s); 1681 } 1682 1683 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 1684 struct net_device_path *path) 1685 { 1686 struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 1687 struct dsa_port *cpu_dp = dp->cpu_dp; 1688 1689 path->dev = ctx->dev; 1690 path->type = DEV_PATH_DSA; 1691 path->dsa.proto = cpu_dp->tag_ops->proto; 1692 path->dsa.port = dp->index; 1693 ctx->dev = cpu_dp->master; 1694 1695 return 0; 1696 } 1697 1698 static const struct net_device_ops dsa_slave_netdev_ops = { 1699 .ndo_open = dsa_slave_open, 1700 .ndo_stop = dsa_slave_close, 1701 .ndo_start_xmit = dsa_slave_xmit, 1702 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1703 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1704 .ndo_set_mac_address = dsa_slave_set_mac_address, 1705 .ndo_fdb_add = dsa_legacy_fdb_add, 1706 .ndo_fdb_del = dsa_legacy_fdb_del, 1707 .ndo_fdb_dump = dsa_slave_fdb_dump, 1708 .ndo_do_ioctl = dsa_slave_ioctl, 1709 .ndo_get_iflink = dsa_slave_get_iflink, 1710 #ifdef CONFIG_NET_POLL_CONTROLLER 1711 .ndo_netpoll_setup = dsa_slave_netpoll_setup, 1712 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 1713 .ndo_poll_controller = dsa_slave_poll_controller, 1714 #endif 1715 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1716 .ndo_setup_tc = dsa_slave_setup_tc, 1717 .ndo_get_stats64 = dsa_slave_get_stats64, 1718 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id, 1719 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 1720 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 1721 .ndo_get_devlink_port = dsa_slave_get_devlink_port, 1722 .ndo_change_mtu = dsa_slave_change_mtu, 1723 .ndo_fill_forward_path = dsa_slave_fill_forward_path, 1724 }; 1725 1726 static struct device_type dsa_type = { 1727 .name = "dsa", 1728 }; 1729 1730 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1731 { 1732 const struct dsa_port *dp = dsa_to_port(ds, port); 1733 1734 if (dp->pl) 1735 phylink_mac_change(dp->pl, up); 1736 } 1737 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 1738 1739 static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 1740 struct phylink_link_state *state) 1741 { 1742 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1743 struct dsa_switch *ds = dp->ds; 1744 1745 /* No need to check that this operation is valid, the callback would 1746 * not be called if it was not. 1747 */ 1748 ds->ops->phylink_fixed_state(ds, dp->index, state); 1749 } 1750 1751 /* slave device setup *******************************************************/ 1752 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1753 { 1754 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1755 struct dsa_switch *ds = dp->ds; 1756 1757 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 1758 if (!slave_dev->phydev) { 1759 netdev_err(slave_dev, "no phy at %d\n", addr); 1760 return -ENODEV; 1761 } 1762 1763 return phylink_connect_phy(dp->pl, slave_dev->phydev); 1764 } 1765 1766 static int dsa_slave_phy_setup(struct net_device *slave_dev) 1767 { 1768 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1769 struct device_node *port_dn = dp->dn; 1770 struct dsa_switch *ds = dp->ds; 1771 phy_interface_t mode; 1772 u32 phy_flags = 0; 1773 int ret; 1774 1775 ret = of_get_phy_mode(port_dn, &mode); 1776 if (ret) 1777 mode = PHY_INTERFACE_MODE_NA; 1778 1779 dp->pl_config.dev = &slave_dev->dev; 1780 dp->pl_config.type = PHYLINK_NETDEV; 1781 1782 /* The get_fixed_state callback takes precedence over polling the 1783 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 1784 * this if the switch provides such a callback. 1785 */ 1786 if (ds->ops->phylink_fixed_state) { 1787 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 1788 dp->pl_config.poll_fixed_state = true; 1789 } 1790 1791 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 1792 &dsa_port_phylink_mac_ops); 1793 if (IS_ERR(dp->pl)) { 1794 netdev_err(slave_dev, 1795 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1796 return PTR_ERR(dp->pl); 1797 } 1798 1799 if (ds->ops->get_phy_flags) 1800 phy_flags = ds->ops->get_phy_flags(ds, dp->index); 1801 1802 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 1803 if (ret == -ENODEV && ds->slave_mii_bus) { 1804 /* We could not connect to a designated PHY or SFP, so try to 1805 * use the switch internal MDIO bus instead 1806 */ 1807 ret = dsa_slave_phy_connect(slave_dev, dp->index); 1808 if (ret) { 1809 netdev_err(slave_dev, 1810 "failed to connect to port %d: %d\n", 1811 dp->index, ret); 1812 phylink_destroy(dp->pl); 1813 return ret; 1814 } 1815 } 1816 1817 return ret; 1818 } 1819 1820 void dsa_slave_setup_tagger(struct net_device *slave) 1821 { 1822 struct dsa_port *dp = dsa_slave_to_port(slave); 1823 struct dsa_slave_priv *p = netdev_priv(slave); 1824 const struct dsa_port *cpu_dp = dp->cpu_dp; 1825 struct net_device *master = cpu_dp->master; 1826 1827 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom; 1828 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom; 1829 /* Try to save one extra realloc later in the TX path (in the master) 1830 * by also inheriting the master's needed headroom and tailroom. 1831 * The 8021q driver also does this. 1832 */ 1833 slave->needed_headroom += master->needed_headroom; 1834 slave->needed_tailroom += master->needed_tailroom; 1835 1836 p->xmit = cpu_dp->tag_ops->xmit; 1837 } 1838 1839 static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1840 static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1841 struct netdev_queue *txq, 1842 void *_unused) 1843 { 1844 lockdep_set_class(&txq->_xmit_lock, 1845 &dsa_slave_netdev_xmit_lock_key); 1846 } 1847 1848 int dsa_slave_suspend(struct net_device *slave_dev) 1849 { 1850 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1851 1852 if (!netif_running(slave_dev)) 1853 return 0; 1854 1855 netif_device_detach(slave_dev); 1856 1857 rtnl_lock(); 1858 phylink_stop(dp->pl); 1859 rtnl_unlock(); 1860 1861 return 0; 1862 } 1863 1864 int dsa_slave_resume(struct net_device *slave_dev) 1865 { 1866 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1867 1868 if (!netif_running(slave_dev)) 1869 return 0; 1870 1871 netif_device_attach(slave_dev); 1872 1873 rtnl_lock(); 1874 phylink_start(dp->pl); 1875 rtnl_unlock(); 1876 1877 return 0; 1878 } 1879 1880 int dsa_slave_create(struct dsa_port *port) 1881 { 1882 const struct dsa_port *cpu_dp = port->cpu_dp; 1883 struct net_device *master = cpu_dp->master; 1884 struct dsa_switch *ds = port->ds; 1885 const char *name = port->name; 1886 struct net_device *slave_dev; 1887 struct dsa_slave_priv *p; 1888 int ret; 1889 1890 if (!ds->num_tx_queues) 1891 ds->num_tx_queues = 1; 1892 1893 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 1894 NET_NAME_UNKNOWN, ether_setup, 1895 ds->num_tx_queues, 1); 1896 if (slave_dev == NULL) 1897 return -ENOMEM; 1898 1899 slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1900 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del) 1901 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1902 slave_dev->hw_features |= NETIF_F_HW_TC; 1903 slave_dev->features |= NETIF_F_LLTX; 1904 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 1905 if (!is_zero_ether_addr(port->mac)) 1906 ether_addr_copy(slave_dev->dev_addr, port->mac); 1907 else 1908 eth_hw_addr_inherit(slave_dev, master); 1909 slave_dev->priv_flags |= IFF_NO_QUEUE; 1910 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 1911 if (ds->ops->port_max_mtu) 1912 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 1913 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1914 1915 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1916 NULL); 1917 1918 SET_NETDEV_DEV(slave_dev, port->ds->dev); 1919 slave_dev->dev.of_node = port->dn; 1920 slave_dev->vlan_features = master->vlan_features; 1921 1922 p = netdev_priv(slave_dev); 1923 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1924 if (!slave_dev->tstats) { 1925 free_netdev(slave_dev); 1926 return -ENOMEM; 1927 } 1928 1929 ret = gro_cells_init(&p->gcells, slave_dev); 1930 if (ret) 1931 goto out_free; 1932 1933 p->dp = port; 1934 INIT_LIST_HEAD(&p->mall_tc_list); 1935 port->slave = slave_dev; 1936 dsa_slave_setup_tagger(slave_dev); 1937 1938 rtnl_lock(); 1939 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 1940 rtnl_unlock(); 1941 if (ret && ret != -EOPNOTSUPP) 1942 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 1943 ret, ETH_DATA_LEN, port->index); 1944 1945 netif_carrier_off(slave_dev); 1946 1947 ret = dsa_slave_phy_setup(slave_dev); 1948 if (ret) { 1949 netdev_err(slave_dev, 1950 "error %d setting up PHY for tree %d, switch %d, port %d\n", 1951 ret, ds->dst->index, ds->index, port->index); 1952 goto out_gcells; 1953 } 1954 1955 rtnl_lock(); 1956 1957 ret = register_netdevice(slave_dev); 1958 if (ret) { 1959 netdev_err(master, "error %d registering interface %s\n", 1960 ret, slave_dev->name); 1961 rtnl_unlock(); 1962 goto out_phy; 1963 } 1964 1965 ret = netdev_upper_dev_link(master, slave_dev, NULL); 1966 1967 rtnl_unlock(); 1968 1969 if (ret) 1970 goto out_unregister; 1971 1972 return 0; 1973 1974 out_unregister: 1975 unregister_netdev(slave_dev); 1976 out_phy: 1977 rtnl_lock(); 1978 phylink_disconnect_phy(p->dp->pl); 1979 rtnl_unlock(); 1980 phylink_destroy(p->dp->pl); 1981 out_gcells: 1982 gro_cells_destroy(&p->gcells); 1983 out_free: 1984 free_percpu(slave_dev->tstats); 1985 free_netdev(slave_dev); 1986 port->slave = NULL; 1987 return ret; 1988 } 1989 1990 void dsa_slave_destroy(struct net_device *slave_dev) 1991 { 1992 struct net_device *master = dsa_slave_to_master(slave_dev); 1993 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1994 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1995 1996 netif_carrier_off(slave_dev); 1997 rtnl_lock(); 1998 netdev_upper_dev_unlink(master, slave_dev); 1999 unregister_netdevice(slave_dev); 2000 phylink_disconnect_phy(dp->pl); 2001 rtnl_unlock(); 2002 2003 phylink_destroy(dp->pl); 2004 gro_cells_destroy(&p->gcells); 2005 free_percpu(slave_dev->tstats); 2006 free_netdev(slave_dev); 2007 } 2008 2009 bool dsa_slave_dev_check(const struct net_device *dev) 2010 { 2011 return dev->netdev_ops == &dsa_slave_netdev_ops; 2012 } 2013 EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2014 2015 static int dsa_slave_changeupper(struct net_device *dev, 2016 struct netdev_notifier_changeupper_info *info) 2017 { 2018 struct dsa_port *dp = dsa_slave_to_port(dev); 2019 struct netlink_ext_ack *extack; 2020 int err = NOTIFY_DONE; 2021 2022 extack = netdev_notifier_info_to_extack(&info->info); 2023 2024 if (netif_is_bridge_master(info->upper_dev)) { 2025 if (info->linking) { 2026 err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2027 if (!err) 2028 dsa_bridge_mtu_normalization(dp); 2029 err = notifier_from_errno(err); 2030 } else { 2031 dsa_port_bridge_leave(dp, info->upper_dev); 2032 err = NOTIFY_OK; 2033 } 2034 } else if (netif_is_lag_master(info->upper_dev)) { 2035 if (info->linking) { 2036 err = dsa_port_lag_join(dp, info->upper_dev, 2037 info->upper_info, extack); 2038 if (err == -EOPNOTSUPP) { 2039 NL_SET_ERR_MSG_MOD(info->info.extack, 2040 "Offloading not supported"); 2041 err = 0; 2042 } 2043 err = notifier_from_errno(err); 2044 } else { 2045 dsa_port_lag_leave(dp, info->upper_dev); 2046 err = NOTIFY_OK; 2047 } 2048 } else if (is_hsr_master(info->upper_dev)) { 2049 if (info->linking) { 2050 err = dsa_port_hsr_join(dp, info->upper_dev); 2051 if (err == -EOPNOTSUPP) { 2052 NL_SET_ERR_MSG_MOD(info->info.extack, 2053 "Offloading not supported"); 2054 err = 0; 2055 } 2056 err = notifier_from_errno(err); 2057 } else { 2058 dsa_port_hsr_leave(dp, info->upper_dev); 2059 err = NOTIFY_OK; 2060 } 2061 } 2062 2063 return err; 2064 } 2065 2066 static int 2067 dsa_slave_lag_changeupper(struct net_device *dev, 2068 struct netdev_notifier_changeupper_info *info) 2069 { 2070 struct net_device *lower; 2071 struct list_head *iter; 2072 int err = NOTIFY_DONE; 2073 struct dsa_port *dp; 2074 2075 netdev_for_each_lower_dev(dev, lower, iter) { 2076 if (!dsa_slave_dev_check(lower)) 2077 continue; 2078 2079 dp = dsa_slave_to_port(lower); 2080 if (!dp->lag_dev) 2081 /* Software LAG */ 2082 continue; 2083 2084 err = dsa_slave_changeupper(lower, info); 2085 if (notifier_to_errno(err)) 2086 break; 2087 } 2088 2089 return err; 2090 } 2091 2092 static int 2093 dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2094 struct netdev_notifier_changeupper_info *info) 2095 { 2096 struct netlink_ext_ack *ext_ack; 2097 struct net_device *slave; 2098 struct dsa_port *dp; 2099 2100 ext_ack = netdev_notifier_info_to_extack(&info->info); 2101 2102 if (!is_vlan_dev(dev)) 2103 return NOTIFY_DONE; 2104 2105 slave = vlan_dev_real_dev(dev); 2106 if (!dsa_slave_dev_check(slave)) 2107 return NOTIFY_DONE; 2108 2109 dp = dsa_slave_to_port(slave); 2110 if (!dp->bridge_dev) 2111 return NOTIFY_DONE; 2112 2113 /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 2114 if (br_vlan_enabled(dp->bridge_dev) && 2115 netif_is_bridge_master(info->upper_dev) && info->linking) { 2116 NL_SET_ERR_MSG_MOD(ext_ack, 2117 "Cannot enslave VLAN device into VLAN aware bridge"); 2118 return notifier_from_errno(-EINVAL); 2119 } 2120 2121 return NOTIFY_DONE; 2122 } 2123 2124 static int 2125 dsa_slave_check_8021q_upper(struct net_device *dev, 2126 struct netdev_notifier_changeupper_info *info) 2127 { 2128 struct dsa_port *dp = dsa_slave_to_port(dev); 2129 struct net_device *br = dp->bridge_dev; 2130 struct bridge_vlan_info br_info; 2131 struct netlink_ext_ack *extack; 2132 int err = NOTIFY_DONE; 2133 u16 vid; 2134 2135 if (!br || !br_vlan_enabled(br)) 2136 return NOTIFY_DONE; 2137 2138 extack = netdev_notifier_info_to_extack(&info->info); 2139 vid = vlan_dev_vlan_id(info->upper_dev); 2140 2141 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 2142 * device, respectively the VID is not found, returning 2143 * 0 means success, which is a failure for us here. 2144 */ 2145 err = br_vlan_get_info(br, vid, &br_info); 2146 if (err == 0) { 2147 NL_SET_ERR_MSG_MOD(extack, 2148 "This VLAN is already configured by the bridge"); 2149 return notifier_from_errno(-EBUSY); 2150 } 2151 2152 return NOTIFY_DONE; 2153 } 2154 2155 static int dsa_slave_netdevice_event(struct notifier_block *nb, 2156 unsigned long event, void *ptr) 2157 { 2158 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2159 2160 switch (event) { 2161 case NETDEV_PRECHANGEUPPER: { 2162 struct netdev_notifier_changeupper_info *info = ptr; 2163 struct dsa_switch *ds; 2164 struct dsa_port *dp; 2165 int err; 2166 2167 if (!dsa_slave_dev_check(dev)) 2168 return dsa_prevent_bridging_8021q_upper(dev, ptr); 2169 2170 dp = dsa_slave_to_port(dev); 2171 ds = dp->ds; 2172 2173 if (ds->ops->port_prechangeupper) { 2174 err = ds->ops->port_prechangeupper(ds, dp->index, info); 2175 if (err) 2176 return notifier_from_errno(err); 2177 } 2178 2179 if (is_vlan_dev(info->upper_dev)) 2180 return dsa_slave_check_8021q_upper(dev, ptr); 2181 break; 2182 } 2183 case NETDEV_CHANGEUPPER: 2184 if (dsa_slave_dev_check(dev)) 2185 return dsa_slave_changeupper(dev, ptr); 2186 2187 if (netif_is_lag_master(dev)) 2188 return dsa_slave_lag_changeupper(dev, ptr); 2189 2190 break; 2191 case NETDEV_CHANGELOWERSTATE: { 2192 struct netdev_notifier_changelowerstate_info *info = ptr; 2193 struct dsa_port *dp; 2194 int err; 2195 2196 if (!dsa_slave_dev_check(dev)) 2197 break; 2198 2199 dp = dsa_slave_to_port(dev); 2200 2201 err = dsa_port_lag_change(dp, info->lower_state_info); 2202 return notifier_from_errno(err); 2203 } 2204 case NETDEV_GOING_DOWN: { 2205 struct dsa_port *dp, *cpu_dp; 2206 struct dsa_switch_tree *dst; 2207 LIST_HEAD(close_list); 2208 2209 if (!netdev_uses_dsa(dev)) 2210 return NOTIFY_DONE; 2211 2212 cpu_dp = dev->dsa_ptr; 2213 dst = cpu_dp->ds->dst; 2214 2215 list_for_each_entry(dp, &dst->ports, list) { 2216 if (!dsa_is_user_port(dp->ds, dp->index)) 2217 continue; 2218 2219 list_add(&dp->slave->close_list, &close_list); 2220 } 2221 2222 dev_close_many(&close_list, true); 2223 2224 return NOTIFY_OK; 2225 } 2226 default: 2227 break; 2228 } 2229 2230 return NOTIFY_DONE; 2231 } 2232 2233 static void 2234 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2235 { 2236 struct dsa_switch *ds = switchdev_work->ds; 2237 struct switchdev_notifier_fdb_info info; 2238 struct dsa_port *dp; 2239 2240 if (!dsa_is_user_port(ds, switchdev_work->port)) 2241 return; 2242 2243 info.addr = switchdev_work->addr; 2244 info.vid = switchdev_work->vid; 2245 info.offloaded = true; 2246 dp = dsa_to_port(ds, switchdev_work->port); 2247 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 2248 dp->slave, &info.info, NULL); 2249 } 2250 2251 static void dsa_slave_switchdev_event_work(struct work_struct *work) 2252 { 2253 struct dsa_switchdev_event_work *switchdev_work = 2254 container_of(work, struct dsa_switchdev_event_work, work); 2255 struct dsa_switch *ds = switchdev_work->ds; 2256 struct dsa_port *dp; 2257 int err; 2258 2259 dp = dsa_to_port(ds, switchdev_work->port); 2260 2261 rtnl_lock(); 2262 switch (switchdev_work->event) { 2263 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2264 err = dsa_port_fdb_add(dp, switchdev_work->addr, 2265 switchdev_work->vid); 2266 if (err) { 2267 dev_err(ds->dev, 2268 "port %d failed to add %pM vid %d to fdb: %d\n", 2269 dp->index, switchdev_work->addr, 2270 switchdev_work->vid, err); 2271 break; 2272 } 2273 dsa_fdb_offload_notify(switchdev_work); 2274 break; 2275 2276 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2277 err = dsa_port_fdb_del(dp, switchdev_work->addr, 2278 switchdev_work->vid); 2279 if (err) { 2280 dev_err(ds->dev, 2281 "port %d failed to delete %pM vid %d from fdb: %d\n", 2282 dp->index, switchdev_work->addr, 2283 switchdev_work->vid, err); 2284 } 2285 2286 break; 2287 } 2288 rtnl_unlock(); 2289 2290 kfree(switchdev_work); 2291 if (dsa_is_user_port(ds, dp->index)) 2292 dev_put(dp->slave); 2293 } 2294 2295 static int dsa_lower_dev_walk(struct net_device *lower_dev, 2296 struct netdev_nested_priv *priv) 2297 { 2298 if (dsa_slave_dev_check(lower_dev)) { 2299 priv->data = (void *)netdev_priv(lower_dev); 2300 return 1; 2301 } 2302 2303 return 0; 2304 } 2305 2306 static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev) 2307 { 2308 struct netdev_nested_priv priv = { 2309 .data = NULL, 2310 }; 2311 2312 netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv); 2313 2314 return (struct dsa_slave_priv *)priv.data; 2315 } 2316 2317 /* Called under rcu_read_lock() */ 2318 static int dsa_slave_switchdev_event(struct notifier_block *unused, 2319 unsigned long event, void *ptr) 2320 { 2321 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2322 const struct switchdev_notifier_fdb_info *fdb_info; 2323 struct dsa_switchdev_event_work *switchdev_work; 2324 struct dsa_port *dp; 2325 int err; 2326 2327 switch (event) { 2328 case SWITCHDEV_PORT_ATTR_SET: 2329 err = switchdev_handle_port_attr_set(dev, ptr, 2330 dsa_slave_dev_check, 2331 dsa_slave_port_attr_set); 2332 return notifier_from_errno(err); 2333 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2334 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2335 fdb_info = ptr; 2336 2337 if (dsa_slave_dev_check(dev)) { 2338 if (!fdb_info->added_by_user || fdb_info->is_local) 2339 return NOTIFY_OK; 2340 2341 dp = dsa_slave_to_port(dev); 2342 } else { 2343 /* Snoop addresses learnt on foreign interfaces 2344 * bridged with us, for switches that don't 2345 * automatically learn SA from CPU-injected traffic 2346 */ 2347 struct net_device *br_dev; 2348 struct dsa_slave_priv *p; 2349 2350 br_dev = netdev_master_upper_dev_get_rcu(dev); 2351 if (!br_dev) 2352 return NOTIFY_DONE; 2353 2354 if (!netif_is_bridge_master(br_dev)) 2355 return NOTIFY_DONE; 2356 2357 p = dsa_slave_dev_lower_find(br_dev); 2358 if (!p) 2359 return NOTIFY_DONE; 2360 2361 dp = p->dp->cpu_dp; 2362 2363 if (!dp->ds->assisted_learning_on_cpu_port) 2364 return NOTIFY_DONE; 2365 2366 /* When the bridge learns an address on an offloaded 2367 * LAG we don't want to send traffic to the CPU, the 2368 * other ports bridged with the LAG should be able to 2369 * autonomously forward towards it. 2370 */ 2371 if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev)) 2372 return NOTIFY_DONE; 2373 } 2374 2375 if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del) 2376 return NOTIFY_DONE; 2377 2378 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2379 if (!switchdev_work) 2380 return NOTIFY_BAD; 2381 2382 INIT_WORK(&switchdev_work->work, 2383 dsa_slave_switchdev_event_work); 2384 switchdev_work->ds = dp->ds; 2385 switchdev_work->port = dp->index; 2386 switchdev_work->event = event; 2387 2388 ether_addr_copy(switchdev_work->addr, 2389 fdb_info->addr); 2390 switchdev_work->vid = fdb_info->vid; 2391 2392 /* Hold a reference on the slave for dsa_fdb_offload_notify */ 2393 if (dsa_is_user_port(dp->ds, dp->index)) 2394 dev_hold(dev); 2395 dsa_schedule_work(&switchdev_work->work); 2396 break; 2397 default: 2398 return NOTIFY_DONE; 2399 } 2400 2401 return NOTIFY_OK; 2402 } 2403 2404 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 2405 unsigned long event, void *ptr) 2406 { 2407 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2408 int err; 2409 2410 switch (event) { 2411 case SWITCHDEV_PORT_OBJ_ADD: 2412 err = switchdev_handle_port_obj_add(dev, ptr, 2413 dsa_slave_dev_check, 2414 dsa_slave_port_obj_add); 2415 return notifier_from_errno(err); 2416 case SWITCHDEV_PORT_OBJ_DEL: 2417 err = switchdev_handle_port_obj_del(dev, ptr, 2418 dsa_slave_dev_check, 2419 dsa_slave_port_obj_del); 2420 return notifier_from_errno(err); 2421 case SWITCHDEV_PORT_ATTR_SET: 2422 err = switchdev_handle_port_attr_set(dev, ptr, 2423 dsa_slave_dev_check, 2424 dsa_slave_port_attr_set); 2425 return notifier_from_errno(err); 2426 } 2427 2428 return NOTIFY_DONE; 2429 } 2430 2431 static struct notifier_block dsa_slave_nb __read_mostly = { 2432 .notifier_call = dsa_slave_netdevice_event, 2433 }; 2434 2435 struct notifier_block dsa_slave_switchdev_notifier = { 2436 .notifier_call = dsa_slave_switchdev_event, 2437 }; 2438 2439 struct notifier_block dsa_slave_switchdev_blocking_notifier = { 2440 .notifier_call = dsa_slave_switchdev_blocking_event, 2441 }; 2442 2443 int dsa_slave_register_notifier(void) 2444 { 2445 struct notifier_block *nb; 2446 int err; 2447 2448 err = register_netdevice_notifier(&dsa_slave_nb); 2449 if (err) 2450 return err; 2451 2452 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 2453 if (err) 2454 goto err_switchdev_nb; 2455 2456 nb = &dsa_slave_switchdev_blocking_notifier; 2457 err = register_switchdev_blocking_notifier(nb); 2458 if (err) 2459 goto err_switchdev_blocking_nb; 2460 2461 return 0; 2462 2463 err_switchdev_blocking_nb: 2464 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2465 err_switchdev_nb: 2466 unregister_netdevice_notifier(&dsa_slave_nb); 2467 return err; 2468 } 2469 2470 void dsa_slave_unregister_notifier(void) 2471 { 2472 struct notifier_block *nb; 2473 int err; 2474 2475 nb = &dsa_slave_switchdev_blocking_notifier; 2476 err = unregister_switchdev_blocking_notifier(nb); 2477 if (err) 2478 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 2479 2480 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2481 if (err) 2482 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 2483 2484 err = unregister_netdevice_notifier(&dsa_slave_nb); 2485 if (err) 2486 pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 2487 } 2488