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/tc_act/tc_mirred.h> 19 #include <linux/if_bridge.h> 20 #include <linux/if_hsr.h> 21 #include <linux/netpoll.h> 22 #include <linux/ptp_classify.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 struct sk_buff *clone; 559 unsigned int type; 560 561 type = ptp_classify_raw(skb); 562 if (type == PTP_CLASS_NONE) 563 return; 564 565 if (!ds->ops->port_txtstamp) 566 return; 567 568 clone = skb_clone_sk(skb); 569 if (!clone) 570 return; 571 572 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) { 573 DSA_SKB_CB(skb)->clone = clone; 574 return; 575 } 576 577 kfree_skb(clone); 578 } 579 580 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 581 { 582 /* SKB for netpoll still need to be mangled with the protocol-specific 583 * tag to be successfully transmitted 584 */ 585 if (unlikely(netpoll_tx_running(dev))) 586 return dsa_slave_netpoll_send_skb(dev, skb); 587 588 /* Queue the SKB for transmission on the parent interface, but 589 * do not modify its EtherType 590 */ 591 skb->dev = dsa_slave_to_master(dev); 592 dev_queue_xmit(skb); 593 594 return NETDEV_TX_OK; 595 } 596 EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 597 598 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 599 { 600 int needed_headroom = dev->needed_headroom; 601 int needed_tailroom = dev->needed_tailroom; 602 603 /* For tail taggers, we need to pad short frames ourselves, to ensure 604 * that the tail tag does not fail at its role of being at the end of 605 * the packet, once the master interface pads the frame. Account for 606 * that pad length here, and pad later. 607 */ 608 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 609 needed_tailroom += ETH_ZLEN - skb->len; 610 /* skb_headroom() returns unsigned int... */ 611 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 612 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 613 614 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 615 /* No reallocation needed, yay! */ 616 return 0; 617 618 return pskb_expand_head(skb, needed_headroom, needed_tailroom, 619 GFP_ATOMIC); 620 } 621 622 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 623 { 624 struct dsa_slave_priv *p = netdev_priv(dev); 625 struct sk_buff *nskb; 626 627 dev_sw_netstats_tx_add(dev, 1, skb->len); 628 629 DSA_SKB_CB(skb)->clone = NULL; 630 631 /* Identify PTP protocol packets, clone them, and pass them to the 632 * switch driver 633 */ 634 dsa_skb_tx_timestamp(p, skb); 635 636 if (dsa_realloc_skb(skb, dev)) { 637 dev_kfree_skb_any(skb); 638 return NETDEV_TX_OK; 639 } 640 641 /* needed_tailroom should still be 'warm' in the cache line from 642 * dsa_realloc_skb(), which has also ensured that padding is safe. 643 */ 644 if (dev->needed_tailroom) 645 eth_skb_pad(skb); 646 647 /* Transmit function may have to reallocate the original SKB, 648 * in which case it must have freed it. Only free it here on error. 649 */ 650 nskb = p->xmit(skb, dev); 651 if (!nskb) { 652 kfree_skb(skb); 653 return NETDEV_TX_OK; 654 } 655 656 return dsa_enqueue_skb(nskb, dev); 657 } 658 659 /* ethtool operations *******************************************************/ 660 661 static void dsa_slave_get_drvinfo(struct net_device *dev, 662 struct ethtool_drvinfo *drvinfo) 663 { 664 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 665 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 666 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 667 } 668 669 static int dsa_slave_get_regs_len(struct net_device *dev) 670 { 671 struct dsa_port *dp = dsa_slave_to_port(dev); 672 struct dsa_switch *ds = dp->ds; 673 674 if (ds->ops->get_regs_len) 675 return ds->ops->get_regs_len(ds, dp->index); 676 677 return -EOPNOTSUPP; 678 } 679 680 static void 681 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 682 { 683 struct dsa_port *dp = dsa_slave_to_port(dev); 684 struct dsa_switch *ds = dp->ds; 685 686 if (ds->ops->get_regs) 687 ds->ops->get_regs(ds, dp->index, regs, _p); 688 } 689 690 static int dsa_slave_nway_reset(struct net_device *dev) 691 { 692 struct dsa_port *dp = dsa_slave_to_port(dev); 693 694 return phylink_ethtool_nway_reset(dp->pl); 695 } 696 697 static int dsa_slave_get_eeprom_len(struct net_device *dev) 698 { 699 struct dsa_port *dp = dsa_slave_to_port(dev); 700 struct dsa_switch *ds = dp->ds; 701 702 if (ds->cd && ds->cd->eeprom_len) 703 return ds->cd->eeprom_len; 704 705 if (ds->ops->get_eeprom_len) 706 return ds->ops->get_eeprom_len(ds); 707 708 return 0; 709 } 710 711 static int dsa_slave_get_eeprom(struct net_device *dev, 712 struct ethtool_eeprom *eeprom, u8 *data) 713 { 714 struct dsa_port *dp = dsa_slave_to_port(dev); 715 struct dsa_switch *ds = dp->ds; 716 717 if (ds->ops->get_eeprom) 718 return ds->ops->get_eeprom(ds, eeprom, data); 719 720 return -EOPNOTSUPP; 721 } 722 723 static int dsa_slave_set_eeprom(struct net_device *dev, 724 struct ethtool_eeprom *eeprom, u8 *data) 725 { 726 struct dsa_port *dp = dsa_slave_to_port(dev); 727 struct dsa_switch *ds = dp->ds; 728 729 if (ds->ops->set_eeprom) 730 return ds->ops->set_eeprom(ds, eeprom, data); 731 732 return -EOPNOTSUPP; 733 } 734 735 static void dsa_slave_get_strings(struct net_device *dev, 736 uint32_t stringset, uint8_t *data) 737 { 738 struct dsa_port *dp = dsa_slave_to_port(dev); 739 struct dsa_switch *ds = dp->ds; 740 741 if (stringset == ETH_SS_STATS) { 742 int len = ETH_GSTRING_LEN; 743 744 strncpy(data, "tx_packets", len); 745 strncpy(data + len, "tx_bytes", len); 746 strncpy(data + 2 * len, "rx_packets", len); 747 strncpy(data + 3 * len, "rx_bytes", len); 748 if (ds->ops->get_strings) 749 ds->ops->get_strings(ds, dp->index, stringset, 750 data + 4 * len); 751 } 752 } 753 754 static void dsa_slave_get_ethtool_stats(struct net_device *dev, 755 struct ethtool_stats *stats, 756 uint64_t *data) 757 { 758 struct dsa_port *dp = dsa_slave_to_port(dev); 759 struct dsa_switch *ds = dp->ds; 760 struct pcpu_sw_netstats *s; 761 unsigned int start; 762 int i; 763 764 for_each_possible_cpu(i) { 765 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 766 767 s = per_cpu_ptr(dev->tstats, i); 768 do { 769 start = u64_stats_fetch_begin_irq(&s->syncp); 770 tx_packets = s->tx_packets; 771 tx_bytes = s->tx_bytes; 772 rx_packets = s->rx_packets; 773 rx_bytes = s->rx_bytes; 774 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 775 data[0] += tx_packets; 776 data[1] += tx_bytes; 777 data[2] += rx_packets; 778 data[3] += rx_bytes; 779 } 780 if (ds->ops->get_ethtool_stats) 781 ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 782 } 783 784 static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 785 { 786 struct dsa_port *dp = dsa_slave_to_port(dev); 787 struct dsa_switch *ds = dp->ds; 788 789 if (sset == ETH_SS_STATS) { 790 int count; 791 792 count = 4; 793 if (ds->ops->get_sset_count) 794 count += ds->ops->get_sset_count(ds, dp->index, sset); 795 796 return count; 797 } 798 799 return -EOPNOTSUPP; 800 } 801 802 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 803 { 804 struct dsa_port *dp = dsa_slave_to_port(dev); 805 struct dsa_switch *ds = dp->ds; 806 807 phylink_ethtool_get_wol(dp->pl, w); 808 809 if (ds->ops->get_wol) 810 ds->ops->get_wol(ds, dp->index, w); 811 } 812 813 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 814 { 815 struct dsa_port *dp = dsa_slave_to_port(dev); 816 struct dsa_switch *ds = dp->ds; 817 int ret = -EOPNOTSUPP; 818 819 phylink_ethtool_set_wol(dp->pl, w); 820 821 if (ds->ops->set_wol) 822 ret = ds->ops->set_wol(ds, dp->index, w); 823 824 return ret; 825 } 826 827 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 828 { 829 struct dsa_port *dp = dsa_slave_to_port(dev); 830 struct dsa_switch *ds = dp->ds; 831 int ret; 832 833 /* Port's PHY and MAC both need to be EEE capable */ 834 if (!dev->phydev || !dp->pl) 835 return -ENODEV; 836 837 if (!ds->ops->set_mac_eee) 838 return -EOPNOTSUPP; 839 840 ret = ds->ops->set_mac_eee(ds, dp->index, e); 841 if (ret) 842 return ret; 843 844 return phylink_ethtool_set_eee(dp->pl, e); 845 } 846 847 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 848 { 849 struct dsa_port *dp = dsa_slave_to_port(dev); 850 struct dsa_switch *ds = dp->ds; 851 int ret; 852 853 /* Port's PHY and MAC both need to be EEE capable */ 854 if (!dev->phydev || !dp->pl) 855 return -ENODEV; 856 857 if (!ds->ops->get_mac_eee) 858 return -EOPNOTSUPP; 859 860 ret = ds->ops->get_mac_eee(ds, dp->index, e); 861 if (ret) 862 return ret; 863 864 return phylink_ethtool_get_eee(dp->pl, e); 865 } 866 867 static int dsa_slave_get_link_ksettings(struct net_device *dev, 868 struct ethtool_link_ksettings *cmd) 869 { 870 struct dsa_port *dp = dsa_slave_to_port(dev); 871 872 return phylink_ethtool_ksettings_get(dp->pl, cmd); 873 } 874 875 static int dsa_slave_set_link_ksettings(struct net_device *dev, 876 const struct ethtool_link_ksettings *cmd) 877 { 878 struct dsa_port *dp = dsa_slave_to_port(dev); 879 880 return phylink_ethtool_ksettings_set(dp->pl, cmd); 881 } 882 883 static void dsa_slave_get_pauseparam(struct net_device *dev, 884 struct ethtool_pauseparam *pause) 885 { 886 struct dsa_port *dp = dsa_slave_to_port(dev); 887 888 phylink_ethtool_get_pauseparam(dp->pl, pause); 889 } 890 891 static int dsa_slave_set_pauseparam(struct net_device *dev, 892 struct ethtool_pauseparam *pause) 893 { 894 struct dsa_port *dp = dsa_slave_to_port(dev); 895 896 return phylink_ethtool_set_pauseparam(dp->pl, pause); 897 } 898 899 #ifdef CONFIG_NET_POLL_CONTROLLER 900 static int dsa_slave_netpoll_setup(struct net_device *dev, 901 struct netpoll_info *ni) 902 { 903 struct net_device *master = dsa_slave_to_master(dev); 904 struct dsa_slave_priv *p = netdev_priv(dev); 905 struct netpoll *netpoll; 906 int err = 0; 907 908 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 909 if (!netpoll) 910 return -ENOMEM; 911 912 err = __netpoll_setup(netpoll, master); 913 if (err) { 914 kfree(netpoll); 915 goto out; 916 } 917 918 p->netpoll = netpoll; 919 out: 920 return err; 921 } 922 923 static void dsa_slave_netpoll_cleanup(struct net_device *dev) 924 { 925 struct dsa_slave_priv *p = netdev_priv(dev); 926 struct netpoll *netpoll = p->netpoll; 927 928 if (!netpoll) 929 return; 930 931 p->netpoll = NULL; 932 933 __netpoll_free(netpoll); 934 } 935 936 static void dsa_slave_poll_controller(struct net_device *dev) 937 { 938 } 939 #endif 940 941 static int dsa_slave_get_phys_port_name(struct net_device *dev, 942 char *name, size_t len) 943 { 944 struct dsa_port *dp = dsa_slave_to_port(dev); 945 946 /* For non-legacy ports, devlink is used and it takes 947 * care of the name generation. This ndo implementation 948 * should be removed with legacy support. 949 */ 950 if (dp->ds->devlink) 951 return -EOPNOTSUPP; 952 953 if (snprintf(name, len, "p%d", dp->index) >= len) 954 return -EINVAL; 955 956 return 0; 957 } 958 959 static struct dsa_mall_tc_entry * 960 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 961 { 962 struct dsa_slave_priv *p = netdev_priv(dev); 963 struct dsa_mall_tc_entry *mall_tc_entry; 964 965 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 966 if (mall_tc_entry->cookie == cookie) 967 return mall_tc_entry; 968 969 return NULL; 970 } 971 972 static int 973 dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 974 struct tc_cls_matchall_offload *cls, 975 bool ingress) 976 { 977 struct dsa_port *dp = dsa_slave_to_port(dev); 978 struct dsa_slave_priv *p = netdev_priv(dev); 979 struct dsa_mall_mirror_tc_entry *mirror; 980 struct dsa_mall_tc_entry *mall_tc_entry; 981 struct dsa_switch *ds = dp->ds; 982 struct flow_action_entry *act; 983 struct dsa_port *to_dp; 984 int err; 985 986 if (!ds->ops->port_mirror_add) 987 return -EOPNOTSUPP; 988 989 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 990 cls->common.extack)) 991 return -EOPNOTSUPP; 992 993 act = &cls->rule->action.entries[0]; 994 995 if (!act->dev) 996 return -EINVAL; 997 998 if (!dsa_slave_dev_check(act->dev)) 999 return -EOPNOTSUPP; 1000 1001 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1002 if (!mall_tc_entry) 1003 return -ENOMEM; 1004 1005 mall_tc_entry->cookie = cls->cookie; 1006 mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1007 mirror = &mall_tc_entry->mirror; 1008 1009 to_dp = dsa_slave_to_port(act->dev); 1010 1011 mirror->to_local_port = to_dp->index; 1012 mirror->ingress = ingress; 1013 1014 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 1015 if (err) { 1016 kfree(mall_tc_entry); 1017 return err; 1018 } 1019 1020 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1021 1022 return err; 1023 } 1024 1025 static int 1026 dsa_slave_add_cls_matchall_police(struct net_device *dev, 1027 struct tc_cls_matchall_offload *cls, 1028 bool ingress) 1029 { 1030 struct netlink_ext_ack *extack = cls->common.extack; 1031 struct dsa_port *dp = dsa_slave_to_port(dev); 1032 struct dsa_slave_priv *p = netdev_priv(dev); 1033 struct dsa_mall_policer_tc_entry *policer; 1034 struct dsa_mall_tc_entry *mall_tc_entry; 1035 struct dsa_switch *ds = dp->ds; 1036 struct flow_action_entry *act; 1037 int err; 1038 1039 if (!ds->ops->port_policer_add) { 1040 NL_SET_ERR_MSG_MOD(extack, 1041 "Policing offload not implemented"); 1042 return -EOPNOTSUPP; 1043 } 1044 1045 if (!ingress) { 1046 NL_SET_ERR_MSG_MOD(extack, 1047 "Only supported on ingress qdisc"); 1048 return -EOPNOTSUPP; 1049 } 1050 1051 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1052 cls->common.extack)) 1053 return -EOPNOTSUPP; 1054 1055 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 1056 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 1057 NL_SET_ERR_MSG_MOD(extack, 1058 "Only one port policer allowed"); 1059 return -EEXIST; 1060 } 1061 } 1062 1063 act = &cls->rule->action.entries[0]; 1064 1065 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1066 if (!mall_tc_entry) 1067 return -ENOMEM; 1068 1069 mall_tc_entry->cookie = cls->cookie; 1070 mall_tc_entry->type = DSA_PORT_MALL_POLICER; 1071 policer = &mall_tc_entry->policer; 1072 policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 1073 policer->burst = act->police.burst; 1074 1075 err = ds->ops->port_policer_add(ds, dp->index, policer); 1076 if (err) { 1077 kfree(mall_tc_entry); 1078 return err; 1079 } 1080 1081 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1082 1083 return err; 1084 } 1085 1086 static int dsa_slave_add_cls_matchall(struct net_device *dev, 1087 struct tc_cls_matchall_offload *cls, 1088 bool ingress) 1089 { 1090 int err = -EOPNOTSUPP; 1091 1092 if (cls->common.protocol == htons(ETH_P_ALL) && 1093 flow_offload_has_one_action(&cls->rule->action) && 1094 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1095 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 1096 else if (flow_offload_has_one_action(&cls->rule->action) && 1097 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 1098 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1099 1100 return err; 1101 } 1102 1103 static void dsa_slave_del_cls_matchall(struct net_device *dev, 1104 struct tc_cls_matchall_offload *cls) 1105 { 1106 struct dsa_port *dp = dsa_slave_to_port(dev); 1107 struct dsa_mall_tc_entry *mall_tc_entry; 1108 struct dsa_switch *ds = dp->ds; 1109 1110 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1111 if (!mall_tc_entry) 1112 return; 1113 1114 list_del(&mall_tc_entry->list); 1115 1116 switch (mall_tc_entry->type) { 1117 case DSA_PORT_MALL_MIRROR: 1118 if (ds->ops->port_mirror_del) 1119 ds->ops->port_mirror_del(ds, dp->index, 1120 &mall_tc_entry->mirror); 1121 break; 1122 case DSA_PORT_MALL_POLICER: 1123 if (ds->ops->port_policer_del) 1124 ds->ops->port_policer_del(ds, dp->index); 1125 break; 1126 default: 1127 WARN_ON(1); 1128 } 1129 1130 kfree(mall_tc_entry); 1131 } 1132 1133 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 1134 struct tc_cls_matchall_offload *cls, 1135 bool ingress) 1136 { 1137 if (cls->common.chain_index) 1138 return -EOPNOTSUPP; 1139 1140 switch (cls->command) { 1141 case TC_CLSMATCHALL_REPLACE: 1142 return dsa_slave_add_cls_matchall(dev, cls, ingress); 1143 case TC_CLSMATCHALL_DESTROY: 1144 dsa_slave_del_cls_matchall(dev, cls); 1145 return 0; 1146 default: 1147 return -EOPNOTSUPP; 1148 } 1149 } 1150 1151 static int dsa_slave_add_cls_flower(struct net_device *dev, 1152 struct flow_cls_offload *cls, 1153 bool ingress) 1154 { 1155 struct dsa_port *dp = dsa_slave_to_port(dev); 1156 struct dsa_switch *ds = dp->ds; 1157 int port = dp->index; 1158 1159 if (!ds->ops->cls_flower_add) 1160 return -EOPNOTSUPP; 1161 1162 return ds->ops->cls_flower_add(ds, port, cls, ingress); 1163 } 1164 1165 static int dsa_slave_del_cls_flower(struct net_device *dev, 1166 struct flow_cls_offload *cls, 1167 bool ingress) 1168 { 1169 struct dsa_port *dp = dsa_slave_to_port(dev); 1170 struct dsa_switch *ds = dp->ds; 1171 int port = dp->index; 1172 1173 if (!ds->ops->cls_flower_del) 1174 return -EOPNOTSUPP; 1175 1176 return ds->ops->cls_flower_del(ds, port, cls, ingress); 1177 } 1178 1179 static int dsa_slave_stats_cls_flower(struct net_device *dev, 1180 struct flow_cls_offload *cls, 1181 bool ingress) 1182 { 1183 struct dsa_port *dp = dsa_slave_to_port(dev); 1184 struct dsa_switch *ds = dp->ds; 1185 int port = dp->index; 1186 1187 if (!ds->ops->cls_flower_stats) 1188 return -EOPNOTSUPP; 1189 1190 return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1191 } 1192 1193 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1194 struct flow_cls_offload *cls, 1195 bool ingress) 1196 { 1197 switch (cls->command) { 1198 case FLOW_CLS_REPLACE: 1199 return dsa_slave_add_cls_flower(dev, cls, ingress); 1200 case FLOW_CLS_DESTROY: 1201 return dsa_slave_del_cls_flower(dev, cls, ingress); 1202 case FLOW_CLS_STATS: 1203 return dsa_slave_stats_cls_flower(dev, cls, ingress); 1204 default: 1205 return -EOPNOTSUPP; 1206 } 1207 } 1208 1209 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 1210 void *cb_priv, bool ingress) 1211 { 1212 struct net_device *dev = cb_priv; 1213 1214 if (!tc_can_offload(dev)) 1215 return -EOPNOTSUPP; 1216 1217 switch (type) { 1218 case TC_SETUP_CLSMATCHALL: 1219 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1220 case TC_SETUP_CLSFLOWER: 1221 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 1222 default: 1223 return -EOPNOTSUPP; 1224 } 1225 } 1226 1227 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 1228 void *type_data, void *cb_priv) 1229 { 1230 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 1231 } 1232 1233 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 1234 void *type_data, void *cb_priv) 1235 { 1236 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 1237 } 1238 1239 static LIST_HEAD(dsa_slave_block_cb_list); 1240 1241 static int dsa_slave_setup_tc_block(struct net_device *dev, 1242 struct flow_block_offload *f) 1243 { 1244 struct flow_block_cb *block_cb; 1245 flow_setup_cb_t *cb; 1246 1247 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 1248 cb = dsa_slave_setup_tc_block_cb_ig; 1249 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 1250 cb = dsa_slave_setup_tc_block_cb_eg; 1251 else 1252 return -EOPNOTSUPP; 1253 1254 f->driver_block_list = &dsa_slave_block_cb_list; 1255 1256 switch (f->command) { 1257 case FLOW_BLOCK_BIND: 1258 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 1259 return -EBUSY; 1260 1261 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1262 if (IS_ERR(block_cb)) 1263 return PTR_ERR(block_cb); 1264 1265 flow_block_cb_add(block_cb, f); 1266 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1267 return 0; 1268 case FLOW_BLOCK_UNBIND: 1269 block_cb = flow_block_cb_lookup(f->block, cb, dev); 1270 if (!block_cb) 1271 return -ENOENT; 1272 1273 flow_block_cb_remove(block_cb, f); 1274 list_del(&block_cb->driver_list); 1275 return 0; 1276 default: 1277 return -EOPNOTSUPP; 1278 } 1279 } 1280 1281 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 1282 void *type_data) 1283 { 1284 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1285 struct net_device *master = cpu_dp->master; 1286 1287 if (!master->netdev_ops->ndo_setup_tc) 1288 return -EOPNOTSUPP; 1289 1290 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 1291 } 1292 1293 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1294 void *type_data) 1295 { 1296 struct dsa_port *dp = dsa_slave_to_port(dev); 1297 struct dsa_switch *ds = dp->ds; 1298 1299 switch (type) { 1300 case TC_SETUP_BLOCK: 1301 return dsa_slave_setup_tc_block(dev, type_data); 1302 case TC_SETUP_FT: 1303 return dsa_slave_setup_ft_block(ds, dp->index, type_data); 1304 default: 1305 break; 1306 } 1307 1308 if (!ds->ops->port_setup_tc) 1309 return -EOPNOTSUPP; 1310 1311 return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1312 } 1313 1314 static int dsa_slave_get_rxnfc(struct net_device *dev, 1315 struct ethtool_rxnfc *nfc, u32 *rule_locs) 1316 { 1317 struct dsa_port *dp = dsa_slave_to_port(dev); 1318 struct dsa_switch *ds = dp->ds; 1319 1320 if (!ds->ops->get_rxnfc) 1321 return -EOPNOTSUPP; 1322 1323 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1324 } 1325 1326 static int dsa_slave_set_rxnfc(struct net_device *dev, 1327 struct ethtool_rxnfc *nfc) 1328 { 1329 struct dsa_port *dp = dsa_slave_to_port(dev); 1330 struct dsa_switch *ds = dp->ds; 1331 1332 if (!ds->ops->set_rxnfc) 1333 return -EOPNOTSUPP; 1334 1335 return ds->ops->set_rxnfc(ds, dp->index, nfc); 1336 } 1337 1338 static int dsa_slave_get_ts_info(struct net_device *dev, 1339 struct ethtool_ts_info *ts) 1340 { 1341 struct dsa_slave_priv *p = netdev_priv(dev); 1342 struct dsa_switch *ds = p->dp->ds; 1343 1344 if (!ds->ops->get_ts_info) 1345 return -EOPNOTSUPP; 1346 1347 return ds->ops->get_ts_info(ds, p->dp->index, ts); 1348 } 1349 1350 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1351 u16 vid) 1352 { 1353 struct net_device *master = dsa_slave_to_master(dev); 1354 struct dsa_port *dp = dsa_slave_to_port(dev); 1355 struct switchdev_obj_port_vlan vlan = { 1356 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1357 .vid = vid, 1358 /* This API only allows programming tagged, non-PVID VIDs */ 1359 .flags = 0, 1360 }; 1361 struct netlink_ext_ack extack = {0}; 1362 int ret; 1363 1364 /* User port... */ 1365 ret = dsa_port_vlan_add(dp, &vlan, &extack); 1366 if (ret) { 1367 if (extack._msg) 1368 netdev_err(dev, "%s\n", extack._msg); 1369 return ret; 1370 } 1371 1372 /* And CPU port... */ 1373 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack); 1374 if (ret) { 1375 if (extack._msg) 1376 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 1377 extack._msg); 1378 return ret; 1379 } 1380 1381 return vlan_vid_add(master, proto, vid); 1382 } 1383 1384 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1385 u16 vid) 1386 { 1387 struct net_device *master = dsa_slave_to_master(dev); 1388 struct dsa_port *dp = dsa_slave_to_port(dev); 1389 struct switchdev_obj_port_vlan vlan = { 1390 .vid = vid, 1391 /* This API only allows programming tagged, non-PVID VIDs */ 1392 .flags = 0, 1393 }; 1394 int err; 1395 1396 /* Do not deprogram the CPU port as it may be shared with other user 1397 * ports which can be members of this VLAN as well. 1398 */ 1399 err = dsa_port_vlan_del(dp, &vlan); 1400 if (err) 1401 return err; 1402 1403 vlan_vid_del(master, proto, vid); 1404 1405 return 0; 1406 } 1407 1408 struct dsa_hw_port { 1409 struct list_head list; 1410 struct net_device *dev; 1411 int old_mtu; 1412 }; 1413 1414 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1415 { 1416 const struct dsa_hw_port *p; 1417 int err; 1418 1419 list_for_each_entry(p, hw_port_list, list) { 1420 if (p->dev->mtu == mtu) 1421 continue; 1422 1423 err = dev_set_mtu(p->dev, mtu); 1424 if (err) 1425 goto rollback; 1426 } 1427 1428 return 0; 1429 1430 rollback: 1431 list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1432 if (p->dev->mtu == p->old_mtu) 1433 continue; 1434 1435 if (dev_set_mtu(p->dev, p->old_mtu)) 1436 netdev_err(p->dev, "Failed to restore MTU\n"); 1437 } 1438 1439 return err; 1440 } 1441 1442 static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1443 { 1444 struct dsa_hw_port *p, *n; 1445 1446 list_for_each_entry_safe(p, n, hw_port_list, list) 1447 kfree(p); 1448 } 1449 1450 /* Make the hardware datapath to/from @dev limited to a common MTU */ 1451 static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1452 { 1453 struct list_head hw_port_list; 1454 struct dsa_switch_tree *dst; 1455 int min_mtu = ETH_MAX_MTU; 1456 struct dsa_port *other_dp; 1457 int err; 1458 1459 if (!dp->ds->mtu_enforcement_ingress) 1460 return; 1461 1462 if (!dp->bridge_dev) 1463 return; 1464 1465 INIT_LIST_HEAD(&hw_port_list); 1466 1467 /* Populate the list of ports that are part of the same bridge 1468 * as the newly added/modified port 1469 */ 1470 list_for_each_entry(dst, &dsa_tree_list, list) { 1471 list_for_each_entry(other_dp, &dst->ports, list) { 1472 struct dsa_hw_port *hw_port; 1473 struct net_device *slave; 1474 1475 if (other_dp->type != DSA_PORT_TYPE_USER) 1476 continue; 1477 1478 if (other_dp->bridge_dev != dp->bridge_dev) 1479 continue; 1480 1481 if (!other_dp->ds->mtu_enforcement_ingress) 1482 continue; 1483 1484 slave = other_dp->slave; 1485 1486 if (min_mtu > slave->mtu) 1487 min_mtu = slave->mtu; 1488 1489 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1490 if (!hw_port) 1491 goto out; 1492 1493 hw_port->dev = slave; 1494 hw_port->old_mtu = slave->mtu; 1495 1496 list_add(&hw_port->list, &hw_port_list); 1497 } 1498 } 1499 1500 /* Attempt to configure the entire hardware bridge to the newly added 1501 * interface's MTU first, regardless of whether the intention of the 1502 * user was to raise or lower it. 1503 */ 1504 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1505 if (!err) 1506 goto out; 1507 1508 /* Clearly that didn't work out so well, so just set the minimum MTU on 1509 * all hardware bridge ports now. If this fails too, then all ports will 1510 * still have their old MTU rolled back anyway. 1511 */ 1512 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1513 1514 out: 1515 dsa_hw_port_list_free(&hw_port_list); 1516 } 1517 1518 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1519 { 1520 struct net_device *master = dsa_slave_to_master(dev); 1521 struct dsa_port *dp = dsa_slave_to_port(dev); 1522 struct dsa_slave_priv *p = netdev_priv(dev); 1523 struct dsa_switch *ds = p->dp->ds; 1524 struct dsa_port *cpu_dp; 1525 int port = p->dp->index; 1526 int largest_mtu = 0; 1527 int new_master_mtu; 1528 int old_master_mtu; 1529 int mtu_limit; 1530 int cpu_mtu; 1531 int err, i; 1532 1533 if (!ds->ops->port_change_mtu) 1534 return -EOPNOTSUPP; 1535 1536 for (i = 0; i < ds->num_ports; i++) { 1537 int slave_mtu; 1538 1539 if (!dsa_is_user_port(ds, i)) 1540 continue; 1541 1542 /* During probe, this function will be called for each slave 1543 * device, while not all of them have been allocated. That's 1544 * ok, it doesn't change what the maximum is, so ignore it. 1545 */ 1546 if (!dsa_to_port(ds, i)->slave) 1547 continue; 1548 1549 /* Pretend that we already applied the setting, which we 1550 * actually haven't (still haven't done all integrity checks) 1551 */ 1552 if (i == port) 1553 slave_mtu = new_mtu; 1554 else 1555 slave_mtu = dsa_to_port(ds, i)->slave->mtu; 1556 1557 if (largest_mtu < slave_mtu) 1558 largest_mtu = slave_mtu; 1559 } 1560 1561 cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1562 1563 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1564 old_master_mtu = master->mtu; 1565 new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead; 1566 if (new_master_mtu > mtu_limit) 1567 return -ERANGE; 1568 1569 /* If the master MTU isn't over limit, there's no need to check the CPU 1570 * MTU, since that surely isn't either. 1571 */ 1572 cpu_mtu = largest_mtu; 1573 1574 /* Start applying stuff */ 1575 if (new_master_mtu != old_master_mtu) { 1576 err = dev_set_mtu(master, new_master_mtu); 1577 if (err < 0) 1578 goto out_master_failed; 1579 1580 /* We only need to propagate the MTU of the CPU port to 1581 * upstream switches. 1582 */ 1583 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true); 1584 if (err) 1585 goto out_cpu_failed; 1586 } 1587 1588 err = dsa_port_mtu_change(dp, new_mtu, false); 1589 if (err) 1590 goto out_port_failed; 1591 1592 dev->mtu = new_mtu; 1593 1594 dsa_bridge_mtu_normalization(dp); 1595 1596 return 0; 1597 1598 out_port_failed: 1599 if (new_master_mtu != old_master_mtu) 1600 dsa_port_mtu_change(cpu_dp, old_master_mtu - 1601 cpu_dp->tag_ops->overhead, 1602 true); 1603 out_cpu_failed: 1604 if (new_master_mtu != old_master_mtu) 1605 dev_set_mtu(master, old_master_mtu); 1606 out_master_failed: 1607 return err; 1608 } 1609 1610 static const struct ethtool_ops dsa_slave_ethtool_ops = { 1611 .get_drvinfo = dsa_slave_get_drvinfo, 1612 .get_regs_len = dsa_slave_get_regs_len, 1613 .get_regs = dsa_slave_get_regs, 1614 .nway_reset = dsa_slave_nway_reset, 1615 .get_link = ethtool_op_get_link, 1616 .get_eeprom_len = dsa_slave_get_eeprom_len, 1617 .get_eeprom = dsa_slave_get_eeprom, 1618 .set_eeprom = dsa_slave_set_eeprom, 1619 .get_strings = dsa_slave_get_strings, 1620 .get_ethtool_stats = dsa_slave_get_ethtool_stats, 1621 .get_sset_count = dsa_slave_get_sset_count, 1622 .set_wol = dsa_slave_set_wol, 1623 .get_wol = dsa_slave_get_wol, 1624 .set_eee = dsa_slave_set_eee, 1625 .get_eee = dsa_slave_get_eee, 1626 .get_link_ksettings = dsa_slave_get_link_ksettings, 1627 .set_link_ksettings = dsa_slave_set_link_ksettings, 1628 .get_pauseparam = dsa_slave_get_pauseparam, 1629 .set_pauseparam = dsa_slave_set_pauseparam, 1630 .get_rxnfc = dsa_slave_get_rxnfc, 1631 .set_rxnfc = dsa_slave_set_rxnfc, 1632 .get_ts_info = dsa_slave_get_ts_info, 1633 }; 1634 1635 /* legacy way, bypassing the bridge *****************************************/ 1636 static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 1637 struct net_device *dev, 1638 const unsigned char *addr, u16 vid, 1639 u16 flags, 1640 struct netlink_ext_ack *extack) 1641 { 1642 struct dsa_port *dp = dsa_slave_to_port(dev); 1643 1644 return dsa_port_fdb_add(dp, addr, vid); 1645 } 1646 1647 static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 1648 struct net_device *dev, 1649 const unsigned char *addr, u16 vid) 1650 { 1651 struct dsa_port *dp = dsa_slave_to_port(dev); 1652 1653 return dsa_port_fdb_del(dp, addr, vid); 1654 } 1655 1656 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 1657 { 1658 struct dsa_port *dp = dsa_slave_to_port(dev); 1659 1660 return dp->ds->devlink ? &dp->devlink_port : NULL; 1661 } 1662 1663 static void dsa_slave_get_stats64(struct net_device *dev, 1664 struct rtnl_link_stats64 *s) 1665 { 1666 struct dsa_port *dp = dsa_slave_to_port(dev); 1667 struct dsa_switch *ds = dp->ds; 1668 1669 if (ds->ops->get_stats64) 1670 ds->ops->get_stats64(ds, dp->index, s); 1671 else 1672 dev_get_tstats64(dev, s); 1673 } 1674 1675 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 1676 struct net_device_path *path) 1677 { 1678 struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 1679 struct dsa_port *cpu_dp = dp->cpu_dp; 1680 1681 path->dev = ctx->dev; 1682 path->type = DEV_PATH_DSA; 1683 path->dsa.proto = cpu_dp->tag_ops->proto; 1684 path->dsa.port = dp->index; 1685 ctx->dev = cpu_dp->master; 1686 1687 return 0; 1688 } 1689 1690 static const struct net_device_ops dsa_slave_netdev_ops = { 1691 .ndo_open = dsa_slave_open, 1692 .ndo_stop = dsa_slave_close, 1693 .ndo_start_xmit = dsa_slave_xmit, 1694 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1695 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1696 .ndo_set_mac_address = dsa_slave_set_mac_address, 1697 .ndo_fdb_add = dsa_legacy_fdb_add, 1698 .ndo_fdb_del = dsa_legacy_fdb_del, 1699 .ndo_fdb_dump = dsa_slave_fdb_dump, 1700 .ndo_do_ioctl = dsa_slave_ioctl, 1701 .ndo_get_iflink = dsa_slave_get_iflink, 1702 #ifdef CONFIG_NET_POLL_CONTROLLER 1703 .ndo_netpoll_setup = dsa_slave_netpoll_setup, 1704 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 1705 .ndo_poll_controller = dsa_slave_poll_controller, 1706 #endif 1707 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1708 .ndo_setup_tc = dsa_slave_setup_tc, 1709 .ndo_get_stats64 = dsa_slave_get_stats64, 1710 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id, 1711 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 1712 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 1713 .ndo_get_devlink_port = dsa_slave_get_devlink_port, 1714 .ndo_change_mtu = dsa_slave_change_mtu, 1715 .ndo_fill_forward_path = dsa_slave_fill_forward_path, 1716 }; 1717 1718 static struct device_type dsa_type = { 1719 .name = "dsa", 1720 }; 1721 1722 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1723 { 1724 const struct dsa_port *dp = dsa_to_port(ds, port); 1725 1726 if (dp->pl) 1727 phylink_mac_change(dp->pl, up); 1728 } 1729 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 1730 1731 static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 1732 struct phylink_link_state *state) 1733 { 1734 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1735 struct dsa_switch *ds = dp->ds; 1736 1737 /* No need to check that this operation is valid, the callback would 1738 * not be called if it was not. 1739 */ 1740 ds->ops->phylink_fixed_state(ds, dp->index, state); 1741 } 1742 1743 /* slave device setup *******************************************************/ 1744 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1745 { 1746 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1747 struct dsa_switch *ds = dp->ds; 1748 1749 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 1750 if (!slave_dev->phydev) { 1751 netdev_err(slave_dev, "no phy at %d\n", addr); 1752 return -ENODEV; 1753 } 1754 1755 return phylink_connect_phy(dp->pl, slave_dev->phydev); 1756 } 1757 1758 static int dsa_slave_phy_setup(struct net_device *slave_dev) 1759 { 1760 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1761 struct device_node *port_dn = dp->dn; 1762 struct dsa_switch *ds = dp->ds; 1763 phy_interface_t mode; 1764 u32 phy_flags = 0; 1765 int ret; 1766 1767 ret = of_get_phy_mode(port_dn, &mode); 1768 if (ret) 1769 mode = PHY_INTERFACE_MODE_NA; 1770 1771 dp->pl_config.dev = &slave_dev->dev; 1772 dp->pl_config.type = PHYLINK_NETDEV; 1773 1774 /* The get_fixed_state callback takes precedence over polling the 1775 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 1776 * this if the switch provides such a callback. 1777 */ 1778 if (ds->ops->phylink_fixed_state) { 1779 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 1780 dp->pl_config.poll_fixed_state = true; 1781 } 1782 1783 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 1784 &dsa_port_phylink_mac_ops); 1785 if (IS_ERR(dp->pl)) { 1786 netdev_err(slave_dev, 1787 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1788 return PTR_ERR(dp->pl); 1789 } 1790 1791 if (ds->ops->get_phy_flags) 1792 phy_flags = ds->ops->get_phy_flags(ds, dp->index); 1793 1794 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 1795 if (ret == -ENODEV && ds->slave_mii_bus) { 1796 /* We could not connect to a designated PHY or SFP, so try to 1797 * use the switch internal MDIO bus instead 1798 */ 1799 ret = dsa_slave_phy_connect(slave_dev, dp->index); 1800 if (ret) { 1801 netdev_err(slave_dev, 1802 "failed to connect to port %d: %d\n", 1803 dp->index, ret); 1804 phylink_destroy(dp->pl); 1805 return ret; 1806 } 1807 } 1808 1809 return ret; 1810 } 1811 1812 void dsa_slave_setup_tagger(struct net_device *slave) 1813 { 1814 struct dsa_port *dp = dsa_slave_to_port(slave); 1815 struct dsa_slave_priv *p = netdev_priv(slave); 1816 const struct dsa_port *cpu_dp = dp->cpu_dp; 1817 struct net_device *master = cpu_dp->master; 1818 1819 if (cpu_dp->tag_ops->tail_tag) 1820 slave->needed_tailroom = cpu_dp->tag_ops->overhead; 1821 else 1822 slave->needed_headroom = cpu_dp->tag_ops->overhead; 1823 /* Try to save one extra realloc later in the TX path (in the master) 1824 * by also inheriting the master's needed headroom and tailroom. 1825 * The 8021q driver also does this. 1826 */ 1827 slave->needed_headroom += master->needed_headroom; 1828 slave->needed_tailroom += master->needed_tailroom; 1829 1830 p->xmit = cpu_dp->tag_ops->xmit; 1831 } 1832 1833 static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1834 static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1835 struct netdev_queue *txq, 1836 void *_unused) 1837 { 1838 lockdep_set_class(&txq->_xmit_lock, 1839 &dsa_slave_netdev_xmit_lock_key); 1840 } 1841 1842 int dsa_slave_suspend(struct net_device *slave_dev) 1843 { 1844 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1845 1846 if (!netif_running(slave_dev)) 1847 return 0; 1848 1849 netif_device_detach(slave_dev); 1850 1851 rtnl_lock(); 1852 phylink_stop(dp->pl); 1853 rtnl_unlock(); 1854 1855 return 0; 1856 } 1857 1858 int dsa_slave_resume(struct net_device *slave_dev) 1859 { 1860 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1861 1862 if (!netif_running(slave_dev)) 1863 return 0; 1864 1865 netif_device_attach(slave_dev); 1866 1867 rtnl_lock(); 1868 phylink_start(dp->pl); 1869 rtnl_unlock(); 1870 1871 return 0; 1872 } 1873 1874 int dsa_slave_create(struct dsa_port *port) 1875 { 1876 const struct dsa_port *cpu_dp = port->cpu_dp; 1877 struct net_device *master = cpu_dp->master; 1878 struct dsa_switch *ds = port->ds; 1879 const char *name = port->name; 1880 struct net_device *slave_dev; 1881 struct dsa_slave_priv *p; 1882 int ret; 1883 1884 if (!ds->num_tx_queues) 1885 ds->num_tx_queues = 1; 1886 1887 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 1888 NET_NAME_UNKNOWN, ether_setup, 1889 ds->num_tx_queues, 1); 1890 if (slave_dev == NULL) 1891 return -ENOMEM; 1892 1893 slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1894 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del) 1895 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1896 slave_dev->hw_features |= NETIF_F_HW_TC; 1897 slave_dev->features |= NETIF_F_LLTX; 1898 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 1899 if (!is_zero_ether_addr(port->mac)) 1900 ether_addr_copy(slave_dev->dev_addr, port->mac); 1901 else 1902 eth_hw_addr_inherit(slave_dev, master); 1903 slave_dev->priv_flags |= IFF_NO_QUEUE; 1904 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 1905 if (ds->ops->port_max_mtu) 1906 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 1907 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1908 1909 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1910 NULL); 1911 1912 SET_NETDEV_DEV(slave_dev, port->ds->dev); 1913 slave_dev->dev.of_node = port->dn; 1914 slave_dev->vlan_features = master->vlan_features; 1915 1916 p = netdev_priv(slave_dev); 1917 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1918 if (!slave_dev->tstats) { 1919 free_netdev(slave_dev); 1920 return -ENOMEM; 1921 } 1922 1923 ret = gro_cells_init(&p->gcells, slave_dev); 1924 if (ret) 1925 goto out_free; 1926 1927 p->dp = port; 1928 INIT_LIST_HEAD(&p->mall_tc_list); 1929 port->slave = slave_dev; 1930 dsa_slave_setup_tagger(slave_dev); 1931 1932 rtnl_lock(); 1933 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 1934 rtnl_unlock(); 1935 if (ret && ret != -EOPNOTSUPP) 1936 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 1937 ret, ETH_DATA_LEN, port->index); 1938 1939 netif_carrier_off(slave_dev); 1940 1941 ret = dsa_slave_phy_setup(slave_dev); 1942 if (ret) { 1943 netdev_err(slave_dev, 1944 "error %d setting up PHY for tree %d, switch %d, port %d\n", 1945 ret, ds->dst->index, ds->index, port->index); 1946 goto out_gcells; 1947 } 1948 1949 rtnl_lock(); 1950 1951 ret = register_netdevice(slave_dev); 1952 if (ret) { 1953 netdev_err(master, "error %d registering interface %s\n", 1954 ret, slave_dev->name); 1955 rtnl_unlock(); 1956 goto out_phy; 1957 } 1958 1959 ret = netdev_upper_dev_link(master, slave_dev, NULL); 1960 1961 rtnl_unlock(); 1962 1963 if (ret) 1964 goto out_unregister; 1965 1966 return 0; 1967 1968 out_unregister: 1969 unregister_netdev(slave_dev); 1970 out_phy: 1971 rtnl_lock(); 1972 phylink_disconnect_phy(p->dp->pl); 1973 rtnl_unlock(); 1974 phylink_destroy(p->dp->pl); 1975 out_gcells: 1976 gro_cells_destroy(&p->gcells); 1977 out_free: 1978 free_percpu(slave_dev->tstats); 1979 free_netdev(slave_dev); 1980 port->slave = NULL; 1981 return ret; 1982 } 1983 1984 void dsa_slave_destroy(struct net_device *slave_dev) 1985 { 1986 struct net_device *master = dsa_slave_to_master(slave_dev); 1987 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1988 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1989 1990 netif_carrier_off(slave_dev); 1991 rtnl_lock(); 1992 netdev_upper_dev_unlink(master, slave_dev); 1993 unregister_netdevice(slave_dev); 1994 phylink_disconnect_phy(dp->pl); 1995 rtnl_unlock(); 1996 1997 phylink_destroy(dp->pl); 1998 gro_cells_destroy(&p->gcells); 1999 free_percpu(slave_dev->tstats); 2000 free_netdev(slave_dev); 2001 } 2002 2003 bool dsa_slave_dev_check(const struct net_device *dev) 2004 { 2005 return dev->netdev_ops == &dsa_slave_netdev_ops; 2006 } 2007 EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2008 2009 static int dsa_slave_changeupper(struct net_device *dev, 2010 struct netdev_notifier_changeupper_info *info) 2011 { 2012 struct dsa_port *dp = dsa_slave_to_port(dev); 2013 struct netlink_ext_ack *extack; 2014 int err = NOTIFY_DONE; 2015 2016 extack = netdev_notifier_info_to_extack(&info->info); 2017 2018 if (netif_is_bridge_master(info->upper_dev)) { 2019 if (info->linking) { 2020 err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2021 if (!err) 2022 dsa_bridge_mtu_normalization(dp); 2023 err = notifier_from_errno(err); 2024 } else { 2025 dsa_port_bridge_leave(dp, info->upper_dev); 2026 err = NOTIFY_OK; 2027 } 2028 } else if (netif_is_lag_master(info->upper_dev)) { 2029 if (info->linking) { 2030 err = dsa_port_lag_join(dp, info->upper_dev, 2031 info->upper_info, extack); 2032 if (err == -EOPNOTSUPP) { 2033 NL_SET_ERR_MSG_MOD(info->info.extack, 2034 "Offloading not supported"); 2035 err = 0; 2036 } 2037 err = notifier_from_errno(err); 2038 } else { 2039 dsa_port_lag_leave(dp, info->upper_dev); 2040 err = NOTIFY_OK; 2041 } 2042 } else if (is_hsr_master(info->upper_dev)) { 2043 if (info->linking) { 2044 err = dsa_port_hsr_join(dp, info->upper_dev); 2045 if (err == -EOPNOTSUPP) { 2046 NL_SET_ERR_MSG_MOD(info->info.extack, 2047 "Offloading not supported"); 2048 err = 0; 2049 } 2050 err = notifier_from_errno(err); 2051 } else { 2052 dsa_port_hsr_leave(dp, info->upper_dev); 2053 err = NOTIFY_OK; 2054 } 2055 } 2056 2057 return err; 2058 } 2059 2060 static int 2061 dsa_slave_lag_changeupper(struct net_device *dev, 2062 struct netdev_notifier_changeupper_info *info) 2063 { 2064 struct net_device *lower; 2065 struct list_head *iter; 2066 int err = NOTIFY_DONE; 2067 struct dsa_port *dp; 2068 2069 netdev_for_each_lower_dev(dev, lower, iter) { 2070 if (!dsa_slave_dev_check(lower)) 2071 continue; 2072 2073 dp = dsa_slave_to_port(lower); 2074 if (!dp->lag_dev) 2075 /* Software LAG */ 2076 continue; 2077 2078 err = dsa_slave_changeupper(lower, info); 2079 if (notifier_to_errno(err)) 2080 break; 2081 } 2082 2083 return err; 2084 } 2085 2086 static int 2087 dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2088 struct netdev_notifier_changeupper_info *info) 2089 { 2090 struct netlink_ext_ack *ext_ack; 2091 struct net_device *slave; 2092 struct dsa_port *dp; 2093 2094 ext_ack = netdev_notifier_info_to_extack(&info->info); 2095 2096 if (!is_vlan_dev(dev)) 2097 return NOTIFY_DONE; 2098 2099 slave = vlan_dev_real_dev(dev); 2100 if (!dsa_slave_dev_check(slave)) 2101 return NOTIFY_DONE; 2102 2103 dp = dsa_slave_to_port(slave); 2104 if (!dp->bridge_dev) 2105 return NOTIFY_DONE; 2106 2107 /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 2108 if (br_vlan_enabled(dp->bridge_dev) && 2109 netif_is_bridge_master(info->upper_dev) && info->linking) { 2110 NL_SET_ERR_MSG_MOD(ext_ack, 2111 "Cannot enslave VLAN device into VLAN aware bridge"); 2112 return notifier_from_errno(-EINVAL); 2113 } 2114 2115 return NOTIFY_DONE; 2116 } 2117 2118 static int 2119 dsa_slave_check_8021q_upper(struct net_device *dev, 2120 struct netdev_notifier_changeupper_info *info) 2121 { 2122 struct dsa_port *dp = dsa_slave_to_port(dev); 2123 struct net_device *br = dp->bridge_dev; 2124 struct bridge_vlan_info br_info; 2125 struct netlink_ext_ack *extack; 2126 int err = NOTIFY_DONE; 2127 u16 vid; 2128 2129 if (!br || !br_vlan_enabled(br)) 2130 return NOTIFY_DONE; 2131 2132 extack = netdev_notifier_info_to_extack(&info->info); 2133 vid = vlan_dev_vlan_id(info->upper_dev); 2134 2135 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 2136 * device, respectively the VID is not found, returning 2137 * 0 means success, which is a failure for us here. 2138 */ 2139 err = br_vlan_get_info(br, vid, &br_info); 2140 if (err == 0) { 2141 NL_SET_ERR_MSG_MOD(extack, 2142 "This VLAN is already configured by the bridge"); 2143 return notifier_from_errno(-EBUSY); 2144 } 2145 2146 return NOTIFY_DONE; 2147 } 2148 2149 static int dsa_slave_netdevice_event(struct notifier_block *nb, 2150 unsigned long event, void *ptr) 2151 { 2152 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2153 2154 switch (event) { 2155 case NETDEV_PRECHANGEUPPER: { 2156 struct netdev_notifier_changeupper_info *info = ptr; 2157 struct dsa_switch *ds; 2158 struct dsa_port *dp; 2159 int err; 2160 2161 if (!dsa_slave_dev_check(dev)) 2162 return dsa_prevent_bridging_8021q_upper(dev, ptr); 2163 2164 dp = dsa_slave_to_port(dev); 2165 ds = dp->ds; 2166 2167 if (ds->ops->port_prechangeupper) { 2168 err = ds->ops->port_prechangeupper(ds, dp->index, info); 2169 if (err) 2170 return notifier_from_errno(err); 2171 } 2172 2173 if (is_vlan_dev(info->upper_dev)) 2174 return dsa_slave_check_8021q_upper(dev, ptr); 2175 break; 2176 } 2177 case NETDEV_CHANGEUPPER: 2178 if (dsa_slave_dev_check(dev)) 2179 return dsa_slave_changeupper(dev, ptr); 2180 2181 if (netif_is_lag_master(dev)) 2182 return dsa_slave_lag_changeupper(dev, ptr); 2183 2184 break; 2185 case NETDEV_CHANGELOWERSTATE: { 2186 struct netdev_notifier_changelowerstate_info *info = ptr; 2187 struct dsa_port *dp; 2188 int err; 2189 2190 if (!dsa_slave_dev_check(dev)) 2191 break; 2192 2193 dp = dsa_slave_to_port(dev); 2194 2195 err = dsa_port_lag_change(dp, info->lower_state_info); 2196 return notifier_from_errno(err); 2197 } 2198 case NETDEV_GOING_DOWN: { 2199 struct dsa_port *dp, *cpu_dp; 2200 struct dsa_switch_tree *dst; 2201 LIST_HEAD(close_list); 2202 2203 if (!netdev_uses_dsa(dev)) 2204 return NOTIFY_DONE; 2205 2206 cpu_dp = dev->dsa_ptr; 2207 dst = cpu_dp->ds->dst; 2208 2209 list_for_each_entry(dp, &dst->ports, list) { 2210 if (!dsa_is_user_port(dp->ds, dp->index)) 2211 continue; 2212 2213 list_add(&dp->slave->close_list, &close_list); 2214 } 2215 2216 dev_close_many(&close_list, true); 2217 2218 return NOTIFY_OK; 2219 } 2220 default: 2221 break; 2222 } 2223 2224 return NOTIFY_DONE; 2225 } 2226 2227 static void 2228 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2229 { 2230 struct dsa_switch *ds = switchdev_work->ds; 2231 struct switchdev_notifier_fdb_info info; 2232 struct dsa_port *dp; 2233 2234 if (!dsa_is_user_port(ds, switchdev_work->port)) 2235 return; 2236 2237 info.addr = switchdev_work->addr; 2238 info.vid = switchdev_work->vid; 2239 info.offloaded = true; 2240 dp = dsa_to_port(ds, switchdev_work->port); 2241 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 2242 dp->slave, &info.info, NULL); 2243 } 2244 2245 static void dsa_slave_switchdev_event_work(struct work_struct *work) 2246 { 2247 struct dsa_switchdev_event_work *switchdev_work = 2248 container_of(work, struct dsa_switchdev_event_work, work); 2249 struct dsa_switch *ds = switchdev_work->ds; 2250 struct dsa_port *dp; 2251 int err; 2252 2253 dp = dsa_to_port(ds, switchdev_work->port); 2254 2255 rtnl_lock(); 2256 switch (switchdev_work->event) { 2257 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2258 err = dsa_port_fdb_add(dp, switchdev_work->addr, 2259 switchdev_work->vid); 2260 if (err) { 2261 dev_err(ds->dev, 2262 "port %d failed to add %pM vid %d to fdb: %d\n", 2263 dp->index, switchdev_work->addr, 2264 switchdev_work->vid, err); 2265 break; 2266 } 2267 dsa_fdb_offload_notify(switchdev_work); 2268 break; 2269 2270 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2271 err = dsa_port_fdb_del(dp, switchdev_work->addr, 2272 switchdev_work->vid); 2273 if (err) { 2274 dev_err(ds->dev, 2275 "port %d failed to delete %pM vid %d from fdb: %d\n", 2276 dp->index, switchdev_work->addr, 2277 switchdev_work->vid, err); 2278 } 2279 2280 break; 2281 } 2282 rtnl_unlock(); 2283 2284 kfree(switchdev_work); 2285 if (dsa_is_user_port(ds, dp->index)) 2286 dev_put(dp->slave); 2287 } 2288 2289 static int dsa_lower_dev_walk(struct net_device *lower_dev, 2290 struct netdev_nested_priv *priv) 2291 { 2292 if (dsa_slave_dev_check(lower_dev)) { 2293 priv->data = (void *)netdev_priv(lower_dev); 2294 return 1; 2295 } 2296 2297 return 0; 2298 } 2299 2300 static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev) 2301 { 2302 struct netdev_nested_priv priv = { 2303 .data = NULL, 2304 }; 2305 2306 netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv); 2307 2308 return (struct dsa_slave_priv *)priv.data; 2309 } 2310 2311 /* Called under rcu_read_lock() */ 2312 static int dsa_slave_switchdev_event(struct notifier_block *unused, 2313 unsigned long event, void *ptr) 2314 { 2315 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2316 const struct switchdev_notifier_fdb_info *fdb_info; 2317 struct dsa_switchdev_event_work *switchdev_work; 2318 struct dsa_port *dp; 2319 int err; 2320 2321 switch (event) { 2322 case SWITCHDEV_PORT_ATTR_SET: 2323 err = switchdev_handle_port_attr_set(dev, ptr, 2324 dsa_slave_dev_check, 2325 dsa_slave_port_attr_set); 2326 return notifier_from_errno(err); 2327 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2328 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2329 fdb_info = ptr; 2330 2331 if (dsa_slave_dev_check(dev)) { 2332 if (!fdb_info->added_by_user || fdb_info->is_local) 2333 return NOTIFY_OK; 2334 2335 dp = dsa_slave_to_port(dev); 2336 } else { 2337 /* Snoop addresses learnt on foreign interfaces 2338 * bridged with us, for switches that don't 2339 * automatically learn SA from CPU-injected traffic 2340 */ 2341 struct net_device *br_dev; 2342 struct dsa_slave_priv *p; 2343 2344 br_dev = netdev_master_upper_dev_get_rcu(dev); 2345 if (!br_dev) 2346 return NOTIFY_DONE; 2347 2348 if (!netif_is_bridge_master(br_dev)) 2349 return NOTIFY_DONE; 2350 2351 p = dsa_slave_dev_lower_find(br_dev); 2352 if (!p) 2353 return NOTIFY_DONE; 2354 2355 dp = p->dp->cpu_dp; 2356 2357 if (!dp->ds->assisted_learning_on_cpu_port) 2358 return NOTIFY_DONE; 2359 2360 /* When the bridge learns an address on an offloaded 2361 * LAG we don't want to send traffic to the CPU, the 2362 * other ports bridged with the LAG should be able to 2363 * autonomously forward towards it. 2364 */ 2365 if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev)) 2366 return NOTIFY_DONE; 2367 } 2368 2369 if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del) 2370 return NOTIFY_DONE; 2371 2372 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2373 if (!switchdev_work) 2374 return NOTIFY_BAD; 2375 2376 INIT_WORK(&switchdev_work->work, 2377 dsa_slave_switchdev_event_work); 2378 switchdev_work->ds = dp->ds; 2379 switchdev_work->port = dp->index; 2380 switchdev_work->event = event; 2381 2382 ether_addr_copy(switchdev_work->addr, 2383 fdb_info->addr); 2384 switchdev_work->vid = fdb_info->vid; 2385 2386 /* Hold a reference on the slave for dsa_fdb_offload_notify */ 2387 if (dsa_is_user_port(dp->ds, dp->index)) 2388 dev_hold(dev); 2389 dsa_schedule_work(&switchdev_work->work); 2390 break; 2391 default: 2392 return NOTIFY_DONE; 2393 } 2394 2395 return NOTIFY_OK; 2396 } 2397 2398 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 2399 unsigned long event, void *ptr) 2400 { 2401 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2402 int err; 2403 2404 switch (event) { 2405 case SWITCHDEV_PORT_OBJ_ADD: 2406 err = switchdev_handle_port_obj_add(dev, ptr, 2407 dsa_slave_dev_check, 2408 dsa_slave_port_obj_add); 2409 return notifier_from_errno(err); 2410 case SWITCHDEV_PORT_OBJ_DEL: 2411 err = switchdev_handle_port_obj_del(dev, ptr, 2412 dsa_slave_dev_check, 2413 dsa_slave_port_obj_del); 2414 return notifier_from_errno(err); 2415 case SWITCHDEV_PORT_ATTR_SET: 2416 err = switchdev_handle_port_attr_set(dev, ptr, 2417 dsa_slave_dev_check, 2418 dsa_slave_port_attr_set); 2419 return notifier_from_errno(err); 2420 } 2421 2422 return NOTIFY_DONE; 2423 } 2424 2425 static struct notifier_block dsa_slave_nb __read_mostly = { 2426 .notifier_call = dsa_slave_netdevice_event, 2427 }; 2428 2429 struct notifier_block dsa_slave_switchdev_notifier = { 2430 .notifier_call = dsa_slave_switchdev_event, 2431 }; 2432 2433 struct notifier_block dsa_slave_switchdev_blocking_notifier = { 2434 .notifier_call = dsa_slave_switchdev_blocking_event, 2435 }; 2436 2437 int dsa_slave_register_notifier(void) 2438 { 2439 struct notifier_block *nb; 2440 int err; 2441 2442 err = register_netdevice_notifier(&dsa_slave_nb); 2443 if (err) 2444 return err; 2445 2446 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 2447 if (err) 2448 goto err_switchdev_nb; 2449 2450 nb = &dsa_slave_switchdev_blocking_notifier; 2451 err = register_switchdev_blocking_notifier(nb); 2452 if (err) 2453 goto err_switchdev_blocking_nb; 2454 2455 return 0; 2456 2457 err_switchdev_blocking_nb: 2458 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2459 err_switchdev_nb: 2460 unregister_netdevice_notifier(&dsa_slave_nb); 2461 return err; 2462 } 2463 2464 void dsa_slave_unregister_notifier(void) 2465 { 2466 struct notifier_block *nb; 2467 int err; 2468 2469 nb = &dsa_slave_switchdev_blocking_notifier; 2470 err = unregister_switchdev_blocking_notifier(nb); 2471 if (err) 2472 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 2473 2474 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2475 if (err) 2476 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 2477 2478 err = unregister_netdevice_notifier(&dsa_slave_nb); 2479 if (err) 2480 pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 2481 } 2482