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