1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/dsa/slave.c - Slave device handling 4 * Copyright (c) 2008-2009 Marvell Semiconductor 5 */ 6 7 #include <linux/list.h> 8 #include <linux/etherdevice.h> 9 #include <linux/netdevice.h> 10 #include <linux/phy.h> 11 #include <linux/phy_fixed.h> 12 #include <linux/phylink.h> 13 #include <linux/of_net.h> 14 #include <linux/of_mdio.h> 15 #include <linux/mdio.h> 16 #include <net/rtnetlink.h> 17 #include <net/pkt_cls.h> 18 #include <net/selftests.h> 19 #include <net/tc_act/tc_mirred.h> 20 #include <linux/if_bridge.h> 21 #include <linux/if_hsr.h> 22 #include <net/dcbnl.h> 23 #include <linux/netpoll.h> 24 25 #include "dsa_priv.h" 26 27 static void dsa_slave_standalone_event_work(struct work_struct *work) 28 { 29 struct dsa_standalone_event_work *standalone_work = 30 container_of(work, struct dsa_standalone_event_work, work); 31 const unsigned char *addr = standalone_work->addr; 32 struct net_device *dev = standalone_work->dev; 33 struct dsa_port *dp = dsa_slave_to_port(dev); 34 struct switchdev_obj_port_mdb mdb; 35 struct dsa_switch *ds = dp->ds; 36 u16 vid = standalone_work->vid; 37 int err; 38 39 switch (standalone_work->event) { 40 case DSA_UC_ADD: 41 err = dsa_port_standalone_host_fdb_add(dp, addr, vid); 42 if (err) { 43 dev_err(ds->dev, 44 "port %d failed to add %pM vid %d to fdb: %d\n", 45 dp->index, addr, vid, err); 46 break; 47 } 48 break; 49 50 case DSA_UC_DEL: 51 err = dsa_port_standalone_host_fdb_del(dp, addr, vid); 52 if (err) { 53 dev_err(ds->dev, 54 "port %d failed to delete %pM vid %d from fdb: %d\n", 55 dp->index, addr, vid, err); 56 } 57 58 break; 59 case DSA_MC_ADD: 60 ether_addr_copy(mdb.addr, addr); 61 mdb.vid = vid; 62 63 err = dsa_port_standalone_host_mdb_add(dp, &mdb); 64 if (err) { 65 dev_err(ds->dev, 66 "port %d failed to add %pM vid %d to mdb: %d\n", 67 dp->index, addr, vid, err); 68 break; 69 } 70 break; 71 case DSA_MC_DEL: 72 ether_addr_copy(mdb.addr, addr); 73 mdb.vid = vid; 74 75 err = dsa_port_standalone_host_mdb_del(dp, &mdb); 76 if (err) { 77 dev_err(ds->dev, 78 "port %d failed to delete %pM vid %d from mdb: %d\n", 79 dp->index, addr, vid, err); 80 } 81 82 break; 83 } 84 85 kfree(standalone_work); 86 } 87 88 static int dsa_slave_schedule_standalone_work(struct net_device *dev, 89 enum dsa_standalone_event event, 90 const unsigned char *addr, 91 u16 vid) 92 { 93 struct dsa_standalone_event_work *standalone_work; 94 95 standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC); 96 if (!standalone_work) 97 return -ENOMEM; 98 99 INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work); 100 standalone_work->event = event; 101 standalone_work->dev = dev; 102 103 ether_addr_copy(standalone_work->addr, addr); 104 standalone_work->vid = vid; 105 106 dsa_schedule_work(&standalone_work->work); 107 108 return 0; 109 } 110 111 static int dsa_slave_sync_uc(struct net_device *dev, 112 const unsigned char *addr) 113 { 114 struct net_device *master = dsa_slave_to_master(dev); 115 struct dsa_port *dp = dsa_slave_to_port(dev); 116 117 dev_uc_add(master, addr); 118 119 if (!dsa_switch_supports_uc_filtering(dp->ds)) 120 return 0; 121 122 return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0); 123 } 124 125 static int dsa_slave_unsync_uc(struct net_device *dev, 126 const unsigned char *addr) 127 { 128 struct net_device *master = dsa_slave_to_master(dev); 129 struct dsa_port *dp = dsa_slave_to_port(dev); 130 131 dev_uc_del(master, addr); 132 133 if (!dsa_switch_supports_uc_filtering(dp->ds)) 134 return 0; 135 136 return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0); 137 } 138 139 static int dsa_slave_sync_mc(struct net_device *dev, 140 const unsigned char *addr) 141 { 142 struct net_device *master = dsa_slave_to_master(dev); 143 struct dsa_port *dp = dsa_slave_to_port(dev); 144 145 dev_mc_add(master, addr); 146 147 if (!dsa_switch_supports_mc_filtering(dp->ds)) 148 return 0; 149 150 return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0); 151 } 152 153 static int dsa_slave_unsync_mc(struct net_device *dev, 154 const unsigned char *addr) 155 { 156 struct net_device *master = dsa_slave_to_master(dev); 157 struct dsa_port *dp = dsa_slave_to_port(dev); 158 159 dev_mc_del(master, addr); 160 161 if (!dsa_switch_supports_mc_filtering(dp->ds)) 162 return 0; 163 164 return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0); 165 } 166 167 /* slave mii_bus handling ***************************************************/ 168 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 169 { 170 struct dsa_switch *ds = bus->priv; 171 172 if (ds->phys_mii_mask & (1 << addr)) 173 return ds->ops->phy_read(ds, addr, reg); 174 175 return 0xffff; 176 } 177 178 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 179 { 180 struct dsa_switch *ds = bus->priv; 181 182 if (ds->phys_mii_mask & (1 << addr)) 183 return ds->ops->phy_write(ds, addr, reg, val); 184 185 return 0; 186 } 187 188 void dsa_slave_mii_bus_init(struct dsa_switch *ds) 189 { 190 ds->slave_mii_bus->priv = (void *)ds; 191 ds->slave_mii_bus->name = "dsa slave smi"; 192 ds->slave_mii_bus->read = dsa_slave_phy_read; 193 ds->slave_mii_bus->write = dsa_slave_phy_write; 194 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 195 ds->dst->index, ds->index); 196 ds->slave_mii_bus->parent = ds->dev; 197 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 198 } 199 200 201 /* slave device handling ****************************************************/ 202 static int dsa_slave_get_iflink(const struct net_device *dev) 203 { 204 return dsa_slave_to_master(dev)->ifindex; 205 } 206 207 static int dsa_slave_open(struct net_device *dev) 208 { 209 struct net_device *master = dsa_slave_to_master(dev); 210 struct dsa_port *dp = dsa_slave_to_port(dev); 211 struct dsa_switch *ds = dp->ds; 212 int err; 213 214 err = dev_open(master, NULL); 215 if (err < 0) { 216 netdev_err(dev, "failed to open master %s\n", master->name); 217 goto out; 218 } 219 220 if (dsa_switch_supports_uc_filtering(ds)) { 221 err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0); 222 if (err) 223 goto out; 224 } 225 226 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 227 err = dev_uc_add(master, dev->dev_addr); 228 if (err < 0) 229 goto del_host_addr; 230 } 231 232 err = dsa_port_enable_rt(dp, dev->phydev); 233 if (err) 234 goto del_unicast; 235 236 return 0; 237 238 del_unicast: 239 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 240 dev_uc_del(master, dev->dev_addr); 241 del_host_addr: 242 if (dsa_switch_supports_uc_filtering(ds)) 243 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 244 out: 245 return err; 246 } 247 248 static int dsa_slave_close(struct net_device *dev) 249 { 250 struct net_device *master = dsa_slave_to_master(dev); 251 struct dsa_port *dp = dsa_slave_to_port(dev); 252 struct dsa_switch *ds = dp->ds; 253 254 dsa_port_disable_rt(dp); 255 256 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 257 dev_uc_del(master, dev->dev_addr); 258 259 if (dsa_switch_supports_uc_filtering(ds)) 260 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 261 262 return 0; 263 } 264 265 /* Keep flooding enabled towards this port's CPU port as long as it serves at 266 * least one port in the tree that requires it. 267 */ 268 static void dsa_port_manage_cpu_flood(struct dsa_port *dp) 269 { 270 struct switchdev_brport_flags flags = { 271 .mask = BR_FLOOD | BR_MCAST_FLOOD, 272 }; 273 struct dsa_switch_tree *dst = dp->ds->dst; 274 struct dsa_port *cpu_dp = dp->cpu_dp; 275 struct dsa_port *other_dp; 276 int err; 277 278 list_for_each_entry(other_dp, &dst->ports, list) { 279 if (!dsa_port_is_user(other_dp)) 280 continue; 281 282 if (other_dp->cpu_dp != cpu_dp) 283 continue; 284 285 if (other_dp->slave->flags & IFF_ALLMULTI) 286 flags.val |= BR_MCAST_FLOOD; 287 if (other_dp->slave->flags & IFF_PROMISC) 288 flags.val |= BR_FLOOD | BR_MCAST_FLOOD; 289 } 290 291 err = dsa_port_pre_bridge_flags(dp, flags, NULL); 292 if (err) 293 return; 294 295 dsa_port_bridge_flags(cpu_dp, flags, NULL); 296 } 297 298 static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 299 { 300 struct net_device *master = dsa_slave_to_master(dev); 301 struct dsa_port *dp = dsa_slave_to_port(dev); 302 struct dsa_switch *ds = dp->ds; 303 304 if (change & IFF_ALLMULTI) 305 dev_set_allmulti(master, 306 dev->flags & IFF_ALLMULTI ? 1 : -1); 307 if (change & IFF_PROMISC) 308 dev_set_promiscuity(master, 309 dev->flags & IFF_PROMISC ? 1 : -1); 310 311 if (dsa_switch_supports_uc_filtering(ds) && 312 dsa_switch_supports_mc_filtering(ds)) 313 dsa_port_manage_cpu_flood(dp); 314 } 315 316 static void dsa_slave_set_rx_mode(struct net_device *dev) 317 { 318 __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc); 319 __dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc); 320 } 321 322 static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 323 { 324 struct net_device *master = dsa_slave_to_master(dev); 325 struct dsa_port *dp = dsa_slave_to_port(dev); 326 struct dsa_switch *ds = dp->ds; 327 struct sockaddr *addr = a; 328 int err; 329 330 if (!is_valid_ether_addr(addr->sa_data)) 331 return -EADDRNOTAVAIL; 332 333 /* If the port is down, the address isn't synced yet to hardware or 334 * to the DSA master, so there is nothing to change. 335 */ 336 if (!(dev->flags & IFF_UP)) 337 goto out_change_dev_addr; 338 339 if (dsa_switch_supports_uc_filtering(ds)) { 340 err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0); 341 if (err) 342 return err; 343 } 344 345 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 346 err = dev_uc_add(master, addr->sa_data); 347 if (err < 0) 348 goto del_unicast; 349 } 350 351 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 352 dev_uc_del(master, dev->dev_addr); 353 354 if (dsa_switch_supports_uc_filtering(ds)) 355 dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 356 357 out_change_dev_addr: 358 eth_hw_addr_set(dev, addr->sa_data); 359 360 return 0; 361 362 del_unicast: 363 if (dsa_switch_supports_uc_filtering(ds)) 364 dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0); 365 366 return err; 367 } 368 369 struct dsa_slave_dump_ctx { 370 struct net_device *dev; 371 struct sk_buff *skb; 372 struct netlink_callback *cb; 373 int idx; 374 }; 375 376 static int 377 dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 378 bool is_static, void *data) 379 { 380 struct dsa_slave_dump_ctx *dump = data; 381 u32 portid = NETLINK_CB(dump->cb->skb).portid; 382 u32 seq = dump->cb->nlh->nlmsg_seq; 383 struct nlmsghdr *nlh; 384 struct ndmsg *ndm; 385 386 if (dump->idx < dump->cb->args[2]) 387 goto skip; 388 389 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 390 sizeof(*ndm), NLM_F_MULTI); 391 if (!nlh) 392 return -EMSGSIZE; 393 394 ndm = nlmsg_data(nlh); 395 ndm->ndm_family = AF_BRIDGE; 396 ndm->ndm_pad1 = 0; 397 ndm->ndm_pad2 = 0; 398 ndm->ndm_flags = NTF_SELF; 399 ndm->ndm_type = 0; 400 ndm->ndm_ifindex = dump->dev->ifindex; 401 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 402 403 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 404 goto nla_put_failure; 405 406 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 407 goto nla_put_failure; 408 409 nlmsg_end(dump->skb, nlh); 410 411 skip: 412 dump->idx++; 413 return 0; 414 415 nla_put_failure: 416 nlmsg_cancel(dump->skb, nlh); 417 return -EMSGSIZE; 418 } 419 420 static int 421 dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 422 struct net_device *dev, struct net_device *filter_dev, 423 int *idx) 424 { 425 struct dsa_port *dp = dsa_slave_to_port(dev); 426 struct dsa_slave_dump_ctx dump = { 427 .dev = dev, 428 .skb = skb, 429 .cb = cb, 430 .idx = *idx, 431 }; 432 int err; 433 434 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 435 *idx = dump.idx; 436 437 return err; 438 } 439 440 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 441 { 442 struct dsa_slave_priv *p = netdev_priv(dev); 443 struct dsa_switch *ds = p->dp->ds; 444 int port = p->dp->index; 445 446 /* Pass through to switch driver if it supports timestamping */ 447 switch (cmd) { 448 case SIOCGHWTSTAMP: 449 if (ds->ops->port_hwtstamp_get) 450 return ds->ops->port_hwtstamp_get(ds, port, ifr); 451 break; 452 case SIOCSHWTSTAMP: 453 if (ds->ops->port_hwtstamp_set) 454 return ds->ops->port_hwtstamp_set(ds, port, ifr); 455 break; 456 } 457 458 return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 459 } 460 461 static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx, 462 const struct switchdev_attr *attr, 463 struct netlink_ext_ack *extack) 464 { 465 struct dsa_port *dp = dsa_slave_to_port(dev); 466 int ret; 467 468 if (ctx && ctx != dp) 469 return 0; 470 471 switch (attr->id) { 472 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 473 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 474 return -EOPNOTSUPP; 475 476 ret = dsa_port_set_state(dp, attr->u.stp_state, true); 477 break; 478 case SWITCHDEV_ATTR_ID_PORT_MST_STATE: 479 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 480 return -EOPNOTSUPP; 481 482 ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack); 483 break; 484 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 485 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 486 return -EOPNOTSUPP; 487 488 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 489 extack); 490 break; 491 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 492 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 493 return -EOPNOTSUPP; 494 495 ret = dsa_port_ageing_time(dp, attr->u.ageing_time); 496 break; 497 case SWITCHDEV_ATTR_ID_BRIDGE_MST: 498 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 499 return -EOPNOTSUPP; 500 501 ret = dsa_port_mst_enable(dp, attr->u.mst, extack); 502 break; 503 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 504 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 505 return -EOPNOTSUPP; 506 507 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 508 extack); 509 break; 510 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 511 if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 512 return -EOPNOTSUPP; 513 514 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack); 515 break; 516 case SWITCHDEV_ATTR_ID_VLAN_MSTI: 517 if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 518 return -EOPNOTSUPP; 519 520 ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti); 521 break; 522 default: 523 ret = -EOPNOTSUPP; 524 break; 525 } 526 527 return ret; 528 } 529 530 /* Must be called under rcu_read_lock() */ 531 static int 532 dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 533 const struct switchdev_obj_port_vlan *vlan) 534 { 535 struct net_device *upper_dev; 536 struct list_head *iter; 537 538 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 539 u16 vid; 540 541 if (!is_vlan_dev(upper_dev)) 542 continue; 543 544 vid = vlan_dev_vlan_id(upper_dev); 545 if (vid == vlan->vid) 546 return -EBUSY; 547 } 548 549 return 0; 550 } 551 552 static int dsa_slave_vlan_add(struct net_device *dev, 553 const struct switchdev_obj *obj, 554 struct netlink_ext_ack *extack) 555 { 556 struct dsa_port *dp = dsa_slave_to_port(dev); 557 struct switchdev_obj_port_vlan *vlan; 558 int err; 559 560 if (dsa_port_skip_vlan_configuration(dp)) { 561 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 562 return 0; 563 } 564 565 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 566 567 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 568 * the same VID. 569 */ 570 if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) { 571 rcu_read_lock(); 572 err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan); 573 rcu_read_unlock(); 574 if (err) { 575 NL_SET_ERR_MSG_MOD(extack, 576 "Port already has a VLAN upper with this VID"); 577 return err; 578 } 579 } 580 581 return dsa_port_vlan_add(dp, vlan, extack); 582 } 583 584 /* Offload a VLAN installed on the bridge or on a foreign interface by 585 * installing it as a VLAN towards the CPU port. 586 */ 587 static int dsa_slave_host_vlan_add(struct net_device *dev, 588 const struct switchdev_obj *obj, 589 struct netlink_ext_ack *extack) 590 { 591 struct dsa_port *dp = dsa_slave_to_port(dev); 592 struct switchdev_obj_port_vlan vlan; 593 594 /* Do nothing if this is a software bridge */ 595 if (!dp->bridge) 596 return -EOPNOTSUPP; 597 598 if (dsa_port_skip_vlan_configuration(dp)) { 599 NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 600 return 0; 601 } 602 603 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 604 605 /* Even though drivers often handle CPU membership in special ways, 606 * it doesn't make sense to program a PVID, so clear this flag. 607 */ 608 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 609 610 return dsa_port_host_vlan_add(dp, &vlan, extack); 611 } 612 613 static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx, 614 const struct switchdev_obj *obj, 615 struct netlink_ext_ack *extack) 616 { 617 struct dsa_port *dp = dsa_slave_to_port(dev); 618 int err; 619 620 if (ctx && ctx != dp) 621 return 0; 622 623 switch (obj->id) { 624 case SWITCHDEV_OBJ_ID_PORT_MDB: 625 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 626 return -EOPNOTSUPP; 627 628 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 629 break; 630 case SWITCHDEV_OBJ_ID_HOST_MDB: 631 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 632 return -EOPNOTSUPP; 633 634 err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 635 break; 636 case SWITCHDEV_OBJ_ID_PORT_VLAN: 637 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 638 err = dsa_slave_vlan_add(dev, obj, extack); 639 else 640 err = dsa_slave_host_vlan_add(dev, obj, extack); 641 break; 642 case SWITCHDEV_OBJ_ID_MRP: 643 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 644 return -EOPNOTSUPP; 645 646 err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj)); 647 break; 648 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 649 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 650 return -EOPNOTSUPP; 651 652 err = dsa_port_mrp_add_ring_role(dp, 653 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 654 break; 655 default: 656 err = -EOPNOTSUPP; 657 break; 658 } 659 660 return err; 661 } 662 663 static int dsa_slave_vlan_del(struct net_device *dev, 664 const struct switchdev_obj *obj) 665 { 666 struct dsa_port *dp = dsa_slave_to_port(dev); 667 struct switchdev_obj_port_vlan *vlan; 668 669 if (dsa_port_skip_vlan_configuration(dp)) 670 return 0; 671 672 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 673 674 return dsa_port_vlan_del(dp, vlan); 675 } 676 677 static int dsa_slave_host_vlan_del(struct net_device *dev, 678 const struct switchdev_obj *obj) 679 { 680 struct dsa_port *dp = dsa_slave_to_port(dev); 681 struct switchdev_obj_port_vlan *vlan; 682 683 /* Do nothing if this is a software bridge */ 684 if (!dp->bridge) 685 return -EOPNOTSUPP; 686 687 if (dsa_port_skip_vlan_configuration(dp)) 688 return 0; 689 690 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 691 692 return dsa_port_host_vlan_del(dp, vlan); 693 } 694 695 static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx, 696 const struct switchdev_obj *obj) 697 { 698 struct dsa_port *dp = dsa_slave_to_port(dev); 699 int err; 700 701 if (ctx && ctx != dp) 702 return 0; 703 704 switch (obj->id) { 705 case SWITCHDEV_OBJ_ID_PORT_MDB: 706 if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 707 return -EOPNOTSUPP; 708 709 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 710 break; 711 case SWITCHDEV_OBJ_ID_HOST_MDB: 712 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 713 return -EOPNOTSUPP; 714 715 err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 716 break; 717 case SWITCHDEV_OBJ_ID_PORT_VLAN: 718 if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 719 err = dsa_slave_vlan_del(dev, obj); 720 else 721 err = dsa_slave_host_vlan_del(dev, obj); 722 break; 723 case SWITCHDEV_OBJ_ID_MRP: 724 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 725 return -EOPNOTSUPP; 726 727 err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj)); 728 break; 729 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 730 if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 731 return -EOPNOTSUPP; 732 733 err = dsa_port_mrp_del_ring_role(dp, 734 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 735 break; 736 default: 737 err = -EOPNOTSUPP; 738 break; 739 } 740 741 return err; 742 } 743 744 static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 745 struct sk_buff *skb) 746 { 747 #ifdef CONFIG_NET_POLL_CONTROLLER 748 struct dsa_slave_priv *p = netdev_priv(dev); 749 750 return netpoll_send_skb(p->netpoll, skb); 751 #else 752 BUG(); 753 return NETDEV_TX_OK; 754 #endif 755 } 756 757 static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 758 struct sk_buff *skb) 759 { 760 struct dsa_switch *ds = p->dp->ds; 761 762 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 763 return; 764 765 if (!ds->ops->port_txtstamp) 766 return; 767 768 ds->ops->port_txtstamp(ds, p->dp->index, skb); 769 } 770 771 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 772 { 773 /* SKB for netpoll still need to be mangled with the protocol-specific 774 * tag to be successfully transmitted 775 */ 776 if (unlikely(netpoll_tx_running(dev))) 777 return dsa_slave_netpoll_send_skb(dev, skb); 778 779 /* Queue the SKB for transmission on the parent interface, but 780 * do not modify its EtherType 781 */ 782 skb->dev = dsa_slave_to_master(dev); 783 dev_queue_xmit(skb); 784 785 return NETDEV_TX_OK; 786 } 787 EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 788 789 static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 790 { 791 int needed_headroom = dev->needed_headroom; 792 int needed_tailroom = dev->needed_tailroom; 793 794 /* For tail taggers, we need to pad short frames ourselves, to ensure 795 * that the tail tag does not fail at its role of being at the end of 796 * the packet, once the master interface pads the frame. Account for 797 * that pad length here, and pad later. 798 */ 799 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 800 needed_tailroom += ETH_ZLEN - skb->len; 801 /* skb_headroom() returns unsigned int... */ 802 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 803 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 804 805 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 806 /* No reallocation needed, yay! */ 807 return 0; 808 809 return pskb_expand_head(skb, needed_headroom, needed_tailroom, 810 GFP_ATOMIC); 811 } 812 813 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 814 { 815 struct dsa_slave_priv *p = netdev_priv(dev); 816 struct sk_buff *nskb; 817 818 dev_sw_netstats_tx_add(dev, 1, skb->len); 819 820 memset(skb->cb, 0, sizeof(skb->cb)); 821 822 /* Handle tx timestamp if any */ 823 dsa_skb_tx_timestamp(p, skb); 824 825 if (dsa_realloc_skb(skb, dev)) { 826 dev_kfree_skb_any(skb); 827 return NETDEV_TX_OK; 828 } 829 830 /* needed_tailroom should still be 'warm' in the cache line from 831 * dsa_realloc_skb(), which has also ensured that padding is safe. 832 */ 833 if (dev->needed_tailroom) 834 eth_skb_pad(skb); 835 836 /* Transmit function may have to reallocate the original SKB, 837 * in which case it must have freed it. Only free it here on error. 838 */ 839 nskb = p->xmit(skb, dev); 840 if (!nskb) { 841 kfree_skb(skb); 842 return NETDEV_TX_OK; 843 } 844 845 return dsa_enqueue_skb(nskb, dev); 846 } 847 848 /* ethtool operations *******************************************************/ 849 850 static void dsa_slave_get_drvinfo(struct net_device *dev, 851 struct ethtool_drvinfo *drvinfo) 852 { 853 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 854 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 855 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 856 } 857 858 static int dsa_slave_get_regs_len(struct net_device *dev) 859 { 860 struct dsa_port *dp = dsa_slave_to_port(dev); 861 struct dsa_switch *ds = dp->ds; 862 863 if (ds->ops->get_regs_len) 864 return ds->ops->get_regs_len(ds, dp->index); 865 866 return -EOPNOTSUPP; 867 } 868 869 static void 870 dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 871 { 872 struct dsa_port *dp = dsa_slave_to_port(dev); 873 struct dsa_switch *ds = dp->ds; 874 875 if (ds->ops->get_regs) 876 ds->ops->get_regs(ds, dp->index, regs, _p); 877 } 878 879 static int dsa_slave_nway_reset(struct net_device *dev) 880 { 881 struct dsa_port *dp = dsa_slave_to_port(dev); 882 883 return phylink_ethtool_nway_reset(dp->pl); 884 } 885 886 static int dsa_slave_get_eeprom_len(struct net_device *dev) 887 { 888 struct dsa_port *dp = dsa_slave_to_port(dev); 889 struct dsa_switch *ds = dp->ds; 890 891 if (ds->cd && ds->cd->eeprom_len) 892 return ds->cd->eeprom_len; 893 894 if (ds->ops->get_eeprom_len) 895 return ds->ops->get_eeprom_len(ds); 896 897 return 0; 898 } 899 900 static int dsa_slave_get_eeprom(struct net_device *dev, 901 struct ethtool_eeprom *eeprom, u8 *data) 902 { 903 struct dsa_port *dp = dsa_slave_to_port(dev); 904 struct dsa_switch *ds = dp->ds; 905 906 if (ds->ops->get_eeprom) 907 return ds->ops->get_eeprom(ds, eeprom, data); 908 909 return -EOPNOTSUPP; 910 } 911 912 static int dsa_slave_set_eeprom(struct net_device *dev, 913 struct ethtool_eeprom *eeprom, u8 *data) 914 { 915 struct dsa_port *dp = dsa_slave_to_port(dev); 916 struct dsa_switch *ds = dp->ds; 917 918 if (ds->ops->set_eeprom) 919 return ds->ops->set_eeprom(ds, eeprom, data); 920 921 return -EOPNOTSUPP; 922 } 923 924 static void dsa_slave_get_strings(struct net_device *dev, 925 uint32_t stringset, uint8_t *data) 926 { 927 struct dsa_port *dp = dsa_slave_to_port(dev); 928 struct dsa_switch *ds = dp->ds; 929 930 if (stringset == ETH_SS_STATS) { 931 int len = ETH_GSTRING_LEN; 932 933 strncpy(data, "tx_packets", len); 934 strncpy(data + len, "tx_bytes", len); 935 strncpy(data + 2 * len, "rx_packets", len); 936 strncpy(data + 3 * len, "rx_bytes", len); 937 if (ds->ops->get_strings) 938 ds->ops->get_strings(ds, dp->index, stringset, 939 data + 4 * len); 940 } else if (stringset == ETH_SS_TEST) { 941 net_selftest_get_strings(data); 942 } 943 944 } 945 946 static void dsa_slave_get_ethtool_stats(struct net_device *dev, 947 struct ethtool_stats *stats, 948 uint64_t *data) 949 { 950 struct dsa_port *dp = dsa_slave_to_port(dev); 951 struct dsa_switch *ds = dp->ds; 952 struct pcpu_sw_netstats *s; 953 unsigned int start; 954 int i; 955 956 for_each_possible_cpu(i) { 957 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 958 959 s = per_cpu_ptr(dev->tstats, i); 960 do { 961 start = u64_stats_fetch_begin_irq(&s->syncp); 962 tx_packets = s->tx_packets; 963 tx_bytes = s->tx_bytes; 964 rx_packets = s->rx_packets; 965 rx_bytes = s->rx_bytes; 966 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 967 data[0] += tx_packets; 968 data[1] += tx_bytes; 969 data[2] += rx_packets; 970 data[3] += rx_bytes; 971 } 972 if (ds->ops->get_ethtool_stats) 973 ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 974 } 975 976 static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 977 { 978 struct dsa_port *dp = dsa_slave_to_port(dev); 979 struct dsa_switch *ds = dp->ds; 980 981 if (sset == ETH_SS_STATS) { 982 int count = 0; 983 984 if (ds->ops->get_sset_count) { 985 count = ds->ops->get_sset_count(ds, dp->index, sset); 986 if (count < 0) 987 return count; 988 } 989 990 return count + 4; 991 } else if (sset == ETH_SS_TEST) { 992 return net_selftest_get_count(); 993 } 994 995 return -EOPNOTSUPP; 996 } 997 998 static void dsa_slave_get_eth_phy_stats(struct net_device *dev, 999 struct ethtool_eth_phy_stats *phy_stats) 1000 { 1001 struct dsa_port *dp = dsa_slave_to_port(dev); 1002 struct dsa_switch *ds = dp->ds; 1003 1004 if (ds->ops->get_eth_phy_stats) 1005 ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats); 1006 } 1007 1008 static void dsa_slave_get_eth_mac_stats(struct net_device *dev, 1009 struct ethtool_eth_mac_stats *mac_stats) 1010 { 1011 struct dsa_port *dp = dsa_slave_to_port(dev); 1012 struct dsa_switch *ds = dp->ds; 1013 1014 if (ds->ops->get_eth_mac_stats) 1015 ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats); 1016 } 1017 1018 static void 1019 dsa_slave_get_eth_ctrl_stats(struct net_device *dev, 1020 struct ethtool_eth_ctrl_stats *ctrl_stats) 1021 { 1022 struct dsa_port *dp = dsa_slave_to_port(dev); 1023 struct dsa_switch *ds = dp->ds; 1024 1025 if (ds->ops->get_eth_ctrl_stats) 1026 ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats); 1027 } 1028 1029 static void dsa_slave_net_selftest(struct net_device *ndev, 1030 struct ethtool_test *etest, u64 *buf) 1031 { 1032 struct dsa_port *dp = dsa_slave_to_port(ndev); 1033 struct dsa_switch *ds = dp->ds; 1034 1035 if (ds->ops->self_test) { 1036 ds->ops->self_test(ds, dp->index, etest, buf); 1037 return; 1038 } 1039 1040 net_selftest(ndev, etest, buf); 1041 } 1042 1043 static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 1044 { 1045 struct dsa_port *dp = dsa_slave_to_port(dev); 1046 struct dsa_switch *ds = dp->ds; 1047 1048 phylink_ethtool_get_wol(dp->pl, w); 1049 1050 if (ds->ops->get_wol) 1051 ds->ops->get_wol(ds, dp->index, w); 1052 } 1053 1054 static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 1055 { 1056 struct dsa_port *dp = dsa_slave_to_port(dev); 1057 struct dsa_switch *ds = dp->ds; 1058 int ret = -EOPNOTSUPP; 1059 1060 phylink_ethtool_set_wol(dp->pl, w); 1061 1062 if (ds->ops->set_wol) 1063 ret = ds->ops->set_wol(ds, dp->index, w); 1064 1065 return ret; 1066 } 1067 1068 static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 1069 { 1070 struct dsa_port *dp = dsa_slave_to_port(dev); 1071 struct dsa_switch *ds = dp->ds; 1072 int ret; 1073 1074 /* Port's PHY and MAC both need to be EEE capable */ 1075 if (!dev->phydev || !dp->pl) 1076 return -ENODEV; 1077 1078 if (!ds->ops->set_mac_eee) 1079 return -EOPNOTSUPP; 1080 1081 ret = ds->ops->set_mac_eee(ds, dp->index, e); 1082 if (ret) 1083 return ret; 1084 1085 return phylink_ethtool_set_eee(dp->pl, e); 1086 } 1087 1088 static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 1089 { 1090 struct dsa_port *dp = dsa_slave_to_port(dev); 1091 struct dsa_switch *ds = dp->ds; 1092 int ret; 1093 1094 /* Port's PHY and MAC both need to be EEE capable */ 1095 if (!dev->phydev || !dp->pl) 1096 return -ENODEV; 1097 1098 if (!ds->ops->get_mac_eee) 1099 return -EOPNOTSUPP; 1100 1101 ret = ds->ops->get_mac_eee(ds, dp->index, e); 1102 if (ret) 1103 return ret; 1104 1105 return phylink_ethtool_get_eee(dp->pl, e); 1106 } 1107 1108 static int dsa_slave_get_link_ksettings(struct net_device *dev, 1109 struct ethtool_link_ksettings *cmd) 1110 { 1111 struct dsa_port *dp = dsa_slave_to_port(dev); 1112 1113 return phylink_ethtool_ksettings_get(dp->pl, cmd); 1114 } 1115 1116 static int dsa_slave_set_link_ksettings(struct net_device *dev, 1117 const struct ethtool_link_ksettings *cmd) 1118 { 1119 struct dsa_port *dp = dsa_slave_to_port(dev); 1120 1121 return phylink_ethtool_ksettings_set(dp->pl, cmd); 1122 } 1123 1124 static void dsa_slave_get_pauseparam(struct net_device *dev, 1125 struct ethtool_pauseparam *pause) 1126 { 1127 struct dsa_port *dp = dsa_slave_to_port(dev); 1128 1129 phylink_ethtool_get_pauseparam(dp->pl, pause); 1130 } 1131 1132 static int dsa_slave_set_pauseparam(struct net_device *dev, 1133 struct ethtool_pauseparam *pause) 1134 { 1135 struct dsa_port *dp = dsa_slave_to_port(dev); 1136 1137 return phylink_ethtool_set_pauseparam(dp->pl, pause); 1138 } 1139 1140 #ifdef CONFIG_NET_POLL_CONTROLLER 1141 static int dsa_slave_netpoll_setup(struct net_device *dev, 1142 struct netpoll_info *ni) 1143 { 1144 struct net_device *master = dsa_slave_to_master(dev); 1145 struct dsa_slave_priv *p = netdev_priv(dev); 1146 struct netpoll *netpoll; 1147 int err = 0; 1148 1149 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 1150 if (!netpoll) 1151 return -ENOMEM; 1152 1153 err = __netpoll_setup(netpoll, master); 1154 if (err) { 1155 kfree(netpoll); 1156 goto out; 1157 } 1158 1159 p->netpoll = netpoll; 1160 out: 1161 return err; 1162 } 1163 1164 static void dsa_slave_netpoll_cleanup(struct net_device *dev) 1165 { 1166 struct dsa_slave_priv *p = netdev_priv(dev); 1167 struct netpoll *netpoll = p->netpoll; 1168 1169 if (!netpoll) 1170 return; 1171 1172 p->netpoll = NULL; 1173 1174 __netpoll_free(netpoll); 1175 } 1176 1177 static void dsa_slave_poll_controller(struct net_device *dev) 1178 { 1179 } 1180 #endif 1181 1182 static struct dsa_mall_tc_entry * 1183 dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 1184 { 1185 struct dsa_slave_priv *p = netdev_priv(dev); 1186 struct dsa_mall_tc_entry *mall_tc_entry; 1187 1188 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 1189 if (mall_tc_entry->cookie == cookie) 1190 return mall_tc_entry; 1191 1192 return NULL; 1193 } 1194 1195 static int 1196 dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 1197 struct tc_cls_matchall_offload *cls, 1198 bool ingress) 1199 { 1200 struct netlink_ext_ack *extack = cls->common.extack; 1201 struct dsa_port *dp = dsa_slave_to_port(dev); 1202 struct dsa_slave_priv *p = netdev_priv(dev); 1203 struct dsa_mall_mirror_tc_entry *mirror; 1204 struct dsa_mall_tc_entry *mall_tc_entry; 1205 struct dsa_switch *ds = dp->ds; 1206 struct flow_action_entry *act; 1207 struct dsa_port *to_dp; 1208 int err; 1209 1210 if (!ds->ops->port_mirror_add) 1211 return -EOPNOTSUPP; 1212 1213 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1214 cls->common.extack)) 1215 return -EOPNOTSUPP; 1216 1217 act = &cls->rule->action.entries[0]; 1218 1219 if (!act->dev) 1220 return -EINVAL; 1221 1222 if (!dsa_slave_dev_check(act->dev)) 1223 return -EOPNOTSUPP; 1224 1225 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1226 if (!mall_tc_entry) 1227 return -ENOMEM; 1228 1229 mall_tc_entry->cookie = cls->cookie; 1230 mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1231 mirror = &mall_tc_entry->mirror; 1232 1233 to_dp = dsa_slave_to_port(act->dev); 1234 1235 mirror->to_local_port = to_dp->index; 1236 mirror->ingress = ingress; 1237 1238 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack); 1239 if (err) { 1240 kfree(mall_tc_entry); 1241 return err; 1242 } 1243 1244 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1245 1246 return err; 1247 } 1248 1249 static int 1250 dsa_slave_add_cls_matchall_police(struct net_device *dev, 1251 struct tc_cls_matchall_offload *cls, 1252 bool ingress) 1253 { 1254 struct netlink_ext_ack *extack = cls->common.extack; 1255 struct dsa_port *dp = dsa_slave_to_port(dev); 1256 struct dsa_slave_priv *p = netdev_priv(dev); 1257 struct dsa_mall_policer_tc_entry *policer; 1258 struct dsa_mall_tc_entry *mall_tc_entry; 1259 struct dsa_switch *ds = dp->ds; 1260 struct flow_action_entry *act; 1261 int err; 1262 1263 if (!ds->ops->port_policer_add) { 1264 NL_SET_ERR_MSG_MOD(extack, 1265 "Policing offload not implemented"); 1266 return -EOPNOTSUPP; 1267 } 1268 1269 if (!ingress) { 1270 NL_SET_ERR_MSG_MOD(extack, 1271 "Only supported on ingress qdisc"); 1272 return -EOPNOTSUPP; 1273 } 1274 1275 if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1276 cls->common.extack)) 1277 return -EOPNOTSUPP; 1278 1279 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 1280 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 1281 NL_SET_ERR_MSG_MOD(extack, 1282 "Only one port policer allowed"); 1283 return -EEXIST; 1284 } 1285 } 1286 1287 act = &cls->rule->action.entries[0]; 1288 1289 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1290 if (!mall_tc_entry) 1291 return -ENOMEM; 1292 1293 mall_tc_entry->cookie = cls->cookie; 1294 mall_tc_entry->type = DSA_PORT_MALL_POLICER; 1295 policer = &mall_tc_entry->policer; 1296 policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 1297 policer->burst = act->police.burst; 1298 1299 err = ds->ops->port_policer_add(ds, dp->index, policer); 1300 if (err) { 1301 kfree(mall_tc_entry); 1302 return err; 1303 } 1304 1305 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1306 1307 return err; 1308 } 1309 1310 static int dsa_slave_add_cls_matchall(struct net_device *dev, 1311 struct tc_cls_matchall_offload *cls, 1312 bool ingress) 1313 { 1314 int err = -EOPNOTSUPP; 1315 1316 if (cls->common.protocol == htons(ETH_P_ALL) && 1317 flow_offload_has_one_action(&cls->rule->action) && 1318 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1319 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 1320 else if (flow_offload_has_one_action(&cls->rule->action) && 1321 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 1322 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1323 1324 return err; 1325 } 1326 1327 static void dsa_slave_del_cls_matchall(struct net_device *dev, 1328 struct tc_cls_matchall_offload *cls) 1329 { 1330 struct dsa_port *dp = dsa_slave_to_port(dev); 1331 struct dsa_mall_tc_entry *mall_tc_entry; 1332 struct dsa_switch *ds = dp->ds; 1333 1334 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1335 if (!mall_tc_entry) 1336 return; 1337 1338 list_del(&mall_tc_entry->list); 1339 1340 switch (mall_tc_entry->type) { 1341 case DSA_PORT_MALL_MIRROR: 1342 if (ds->ops->port_mirror_del) 1343 ds->ops->port_mirror_del(ds, dp->index, 1344 &mall_tc_entry->mirror); 1345 break; 1346 case DSA_PORT_MALL_POLICER: 1347 if (ds->ops->port_policer_del) 1348 ds->ops->port_policer_del(ds, dp->index); 1349 break; 1350 default: 1351 WARN_ON(1); 1352 } 1353 1354 kfree(mall_tc_entry); 1355 } 1356 1357 static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 1358 struct tc_cls_matchall_offload *cls, 1359 bool ingress) 1360 { 1361 if (cls->common.chain_index) 1362 return -EOPNOTSUPP; 1363 1364 switch (cls->command) { 1365 case TC_CLSMATCHALL_REPLACE: 1366 return dsa_slave_add_cls_matchall(dev, cls, ingress); 1367 case TC_CLSMATCHALL_DESTROY: 1368 dsa_slave_del_cls_matchall(dev, cls); 1369 return 0; 1370 default: 1371 return -EOPNOTSUPP; 1372 } 1373 } 1374 1375 static int dsa_slave_add_cls_flower(struct net_device *dev, 1376 struct flow_cls_offload *cls, 1377 bool ingress) 1378 { 1379 struct dsa_port *dp = dsa_slave_to_port(dev); 1380 struct dsa_switch *ds = dp->ds; 1381 int port = dp->index; 1382 1383 if (!ds->ops->cls_flower_add) 1384 return -EOPNOTSUPP; 1385 1386 return ds->ops->cls_flower_add(ds, port, cls, ingress); 1387 } 1388 1389 static int dsa_slave_del_cls_flower(struct net_device *dev, 1390 struct flow_cls_offload *cls, 1391 bool ingress) 1392 { 1393 struct dsa_port *dp = dsa_slave_to_port(dev); 1394 struct dsa_switch *ds = dp->ds; 1395 int port = dp->index; 1396 1397 if (!ds->ops->cls_flower_del) 1398 return -EOPNOTSUPP; 1399 1400 return ds->ops->cls_flower_del(ds, port, cls, ingress); 1401 } 1402 1403 static int dsa_slave_stats_cls_flower(struct net_device *dev, 1404 struct flow_cls_offload *cls, 1405 bool ingress) 1406 { 1407 struct dsa_port *dp = dsa_slave_to_port(dev); 1408 struct dsa_switch *ds = dp->ds; 1409 int port = dp->index; 1410 1411 if (!ds->ops->cls_flower_stats) 1412 return -EOPNOTSUPP; 1413 1414 return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1415 } 1416 1417 static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1418 struct flow_cls_offload *cls, 1419 bool ingress) 1420 { 1421 switch (cls->command) { 1422 case FLOW_CLS_REPLACE: 1423 return dsa_slave_add_cls_flower(dev, cls, ingress); 1424 case FLOW_CLS_DESTROY: 1425 return dsa_slave_del_cls_flower(dev, cls, ingress); 1426 case FLOW_CLS_STATS: 1427 return dsa_slave_stats_cls_flower(dev, cls, ingress); 1428 default: 1429 return -EOPNOTSUPP; 1430 } 1431 } 1432 1433 static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 1434 void *cb_priv, bool ingress) 1435 { 1436 struct net_device *dev = cb_priv; 1437 1438 if (!tc_can_offload(dev)) 1439 return -EOPNOTSUPP; 1440 1441 switch (type) { 1442 case TC_SETUP_CLSMATCHALL: 1443 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1444 case TC_SETUP_CLSFLOWER: 1445 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 1446 default: 1447 return -EOPNOTSUPP; 1448 } 1449 } 1450 1451 static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 1452 void *type_data, void *cb_priv) 1453 { 1454 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 1455 } 1456 1457 static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 1458 void *type_data, void *cb_priv) 1459 { 1460 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 1461 } 1462 1463 static LIST_HEAD(dsa_slave_block_cb_list); 1464 1465 static int dsa_slave_setup_tc_block(struct net_device *dev, 1466 struct flow_block_offload *f) 1467 { 1468 struct flow_block_cb *block_cb; 1469 flow_setup_cb_t *cb; 1470 1471 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 1472 cb = dsa_slave_setup_tc_block_cb_ig; 1473 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 1474 cb = dsa_slave_setup_tc_block_cb_eg; 1475 else 1476 return -EOPNOTSUPP; 1477 1478 f->driver_block_list = &dsa_slave_block_cb_list; 1479 1480 switch (f->command) { 1481 case FLOW_BLOCK_BIND: 1482 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 1483 return -EBUSY; 1484 1485 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1486 if (IS_ERR(block_cb)) 1487 return PTR_ERR(block_cb); 1488 1489 flow_block_cb_add(block_cb, f); 1490 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1491 return 0; 1492 case FLOW_BLOCK_UNBIND: 1493 block_cb = flow_block_cb_lookup(f->block, cb, dev); 1494 if (!block_cb) 1495 return -ENOENT; 1496 1497 flow_block_cb_remove(block_cb, f); 1498 list_del(&block_cb->driver_list); 1499 return 0; 1500 default: 1501 return -EOPNOTSUPP; 1502 } 1503 } 1504 1505 static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 1506 void *type_data) 1507 { 1508 struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1509 struct net_device *master = cpu_dp->master; 1510 1511 if (!master->netdev_ops->ndo_setup_tc) 1512 return -EOPNOTSUPP; 1513 1514 return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 1515 } 1516 1517 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1518 void *type_data) 1519 { 1520 struct dsa_port *dp = dsa_slave_to_port(dev); 1521 struct dsa_switch *ds = dp->ds; 1522 1523 switch (type) { 1524 case TC_SETUP_BLOCK: 1525 return dsa_slave_setup_tc_block(dev, type_data); 1526 case TC_SETUP_FT: 1527 return dsa_slave_setup_ft_block(ds, dp->index, type_data); 1528 default: 1529 break; 1530 } 1531 1532 if (!ds->ops->port_setup_tc) 1533 return -EOPNOTSUPP; 1534 1535 return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1536 } 1537 1538 static int dsa_slave_get_rxnfc(struct net_device *dev, 1539 struct ethtool_rxnfc *nfc, u32 *rule_locs) 1540 { 1541 struct dsa_port *dp = dsa_slave_to_port(dev); 1542 struct dsa_switch *ds = dp->ds; 1543 1544 if (!ds->ops->get_rxnfc) 1545 return -EOPNOTSUPP; 1546 1547 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1548 } 1549 1550 static int dsa_slave_set_rxnfc(struct net_device *dev, 1551 struct ethtool_rxnfc *nfc) 1552 { 1553 struct dsa_port *dp = dsa_slave_to_port(dev); 1554 struct dsa_switch *ds = dp->ds; 1555 1556 if (!ds->ops->set_rxnfc) 1557 return -EOPNOTSUPP; 1558 1559 return ds->ops->set_rxnfc(ds, dp->index, nfc); 1560 } 1561 1562 static int dsa_slave_get_ts_info(struct net_device *dev, 1563 struct ethtool_ts_info *ts) 1564 { 1565 struct dsa_slave_priv *p = netdev_priv(dev); 1566 struct dsa_switch *ds = p->dp->ds; 1567 1568 if (!ds->ops->get_ts_info) 1569 return -EOPNOTSUPP; 1570 1571 return ds->ops->get_ts_info(ds, p->dp->index, ts); 1572 } 1573 1574 static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1575 u16 vid) 1576 { 1577 struct dsa_port *dp = dsa_slave_to_port(dev); 1578 struct switchdev_obj_port_vlan vlan = { 1579 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1580 .vid = vid, 1581 /* This API only allows programming tagged, non-PVID VIDs */ 1582 .flags = 0, 1583 }; 1584 struct netlink_ext_ack extack = {0}; 1585 int ret; 1586 1587 /* User port... */ 1588 ret = dsa_port_vlan_add(dp, &vlan, &extack); 1589 if (ret) { 1590 if (extack._msg) 1591 netdev_err(dev, "%s\n", extack._msg); 1592 return ret; 1593 } 1594 1595 /* And CPU port... */ 1596 ret = dsa_port_host_vlan_add(dp, &vlan, &extack); 1597 if (ret) { 1598 if (extack._msg) 1599 netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 1600 extack._msg); 1601 return ret; 1602 } 1603 1604 return 0; 1605 } 1606 1607 static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1608 u16 vid) 1609 { 1610 struct dsa_port *dp = dsa_slave_to_port(dev); 1611 struct switchdev_obj_port_vlan vlan = { 1612 .vid = vid, 1613 /* This API only allows programming tagged, non-PVID VIDs */ 1614 .flags = 0, 1615 }; 1616 int err; 1617 1618 err = dsa_port_vlan_del(dp, &vlan); 1619 if (err) 1620 return err; 1621 1622 return dsa_port_host_vlan_del(dp, &vlan); 1623 } 1624 1625 static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg) 1626 { 1627 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 1628 1629 return dsa_slave_vlan_rx_add_vid(arg, proto, vid); 1630 } 1631 1632 static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg) 1633 { 1634 __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 1635 1636 return dsa_slave_vlan_rx_kill_vid(arg, proto, vid); 1637 } 1638 1639 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN 1640 * filtering is enabled. The baseline is that only ports that offload a 1641 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware, 1642 * but there are exceptions for quirky hardware. 1643 * 1644 * If ds->vlan_filtering_is_global = true, then standalone ports which share 1645 * the same switch with other ports that offload a VLAN-aware bridge are also 1646 * inevitably VLAN-aware. 1647 * 1648 * To summarize, a DSA switch port offloads: 1649 * 1650 * - If standalone (this includes software bridge, software LAG): 1651 * - if ds->needs_standalone_vlan_filtering = true, OR if 1652 * (ds->vlan_filtering_is_global = true AND there are bridges spanning 1653 * this switch chip which have vlan_filtering=1) 1654 * - the 8021q upper VLANs 1655 * - else (standalone VLAN filtering is not needed, VLAN filtering is not 1656 * global, or it is, but no port is under a VLAN-aware bridge): 1657 * - no VLAN (any 8021q upper is a software VLAN) 1658 * 1659 * - If under a vlan_filtering=0 bridge which it offload: 1660 * - if ds->configure_vlan_while_not_filtering = true (default): 1661 * - the bridge VLANs. These VLANs are committed to hardware but inactive. 1662 * - else (deprecated): 1663 * - no VLAN. The bridge VLANs are not restored when VLAN awareness is 1664 * enabled, so this behavior is broken and discouraged. 1665 * 1666 * - If under a vlan_filtering=1 bridge which it offload: 1667 * - the bridge VLANs 1668 * - the 8021q upper VLANs 1669 */ 1670 int dsa_slave_manage_vlan_filtering(struct net_device *slave, 1671 bool vlan_filtering) 1672 { 1673 int err; 1674 1675 if (vlan_filtering) { 1676 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1677 1678 err = vlan_for_each(slave, dsa_slave_restore_vlan, slave); 1679 if (err) { 1680 vlan_for_each(slave, dsa_slave_clear_vlan, slave); 1681 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 1682 return err; 1683 } 1684 } else { 1685 err = vlan_for_each(slave, dsa_slave_clear_vlan, slave); 1686 if (err) 1687 return err; 1688 1689 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 1690 } 1691 1692 return 0; 1693 } 1694 1695 struct dsa_hw_port { 1696 struct list_head list; 1697 struct net_device *dev; 1698 int old_mtu; 1699 }; 1700 1701 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1702 { 1703 const struct dsa_hw_port *p; 1704 int err; 1705 1706 list_for_each_entry(p, hw_port_list, list) { 1707 if (p->dev->mtu == mtu) 1708 continue; 1709 1710 err = dev_set_mtu(p->dev, mtu); 1711 if (err) 1712 goto rollback; 1713 } 1714 1715 return 0; 1716 1717 rollback: 1718 list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1719 if (p->dev->mtu == p->old_mtu) 1720 continue; 1721 1722 if (dev_set_mtu(p->dev, p->old_mtu)) 1723 netdev_err(p->dev, "Failed to restore MTU\n"); 1724 } 1725 1726 return err; 1727 } 1728 1729 static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1730 { 1731 struct dsa_hw_port *p, *n; 1732 1733 list_for_each_entry_safe(p, n, hw_port_list, list) 1734 kfree(p); 1735 } 1736 1737 /* Make the hardware datapath to/from @dev limited to a common MTU */ 1738 static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1739 { 1740 struct list_head hw_port_list; 1741 struct dsa_switch_tree *dst; 1742 int min_mtu = ETH_MAX_MTU; 1743 struct dsa_port *other_dp; 1744 int err; 1745 1746 if (!dp->ds->mtu_enforcement_ingress) 1747 return; 1748 1749 if (!dp->bridge) 1750 return; 1751 1752 INIT_LIST_HEAD(&hw_port_list); 1753 1754 /* Populate the list of ports that are part of the same bridge 1755 * as the newly added/modified port 1756 */ 1757 list_for_each_entry(dst, &dsa_tree_list, list) { 1758 list_for_each_entry(other_dp, &dst->ports, list) { 1759 struct dsa_hw_port *hw_port; 1760 struct net_device *slave; 1761 1762 if (other_dp->type != DSA_PORT_TYPE_USER) 1763 continue; 1764 1765 if (!dsa_port_bridge_same(dp, other_dp)) 1766 continue; 1767 1768 if (!other_dp->ds->mtu_enforcement_ingress) 1769 continue; 1770 1771 slave = other_dp->slave; 1772 1773 if (min_mtu > slave->mtu) 1774 min_mtu = slave->mtu; 1775 1776 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1777 if (!hw_port) 1778 goto out; 1779 1780 hw_port->dev = slave; 1781 hw_port->old_mtu = slave->mtu; 1782 1783 list_add(&hw_port->list, &hw_port_list); 1784 } 1785 } 1786 1787 /* Attempt to configure the entire hardware bridge to the newly added 1788 * interface's MTU first, regardless of whether the intention of the 1789 * user was to raise or lower it. 1790 */ 1791 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1792 if (!err) 1793 goto out; 1794 1795 /* Clearly that didn't work out so well, so just set the minimum MTU on 1796 * all hardware bridge ports now. If this fails too, then all ports will 1797 * still have their old MTU rolled back anyway. 1798 */ 1799 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1800 1801 out: 1802 dsa_hw_port_list_free(&hw_port_list); 1803 } 1804 1805 int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1806 { 1807 struct net_device *master = dsa_slave_to_master(dev); 1808 struct dsa_port *dp = dsa_slave_to_port(dev); 1809 struct dsa_port *cpu_dp = dp->cpu_dp; 1810 struct dsa_switch *ds = dp->ds; 1811 struct dsa_port *other_dp; 1812 int largest_mtu = 0; 1813 int new_master_mtu; 1814 int old_master_mtu; 1815 int mtu_limit; 1816 int cpu_mtu; 1817 int err; 1818 1819 if (!ds->ops->port_change_mtu) 1820 return -EOPNOTSUPP; 1821 1822 dsa_tree_for_each_user_port(other_dp, ds->dst) { 1823 int slave_mtu; 1824 1825 /* During probe, this function will be called for each slave 1826 * device, while not all of them have been allocated. That's 1827 * ok, it doesn't change what the maximum is, so ignore it. 1828 */ 1829 if (!other_dp->slave) 1830 continue; 1831 1832 /* Pretend that we already applied the setting, which we 1833 * actually haven't (still haven't done all integrity checks) 1834 */ 1835 if (dp == other_dp) 1836 slave_mtu = new_mtu; 1837 else 1838 slave_mtu = other_dp->slave->mtu; 1839 1840 if (largest_mtu < slave_mtu) 1841 largest_mtu = slave_mtu; 1842 } 1843 1844 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1845 old_master_mtu = master->mtu; 1846 new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops); 1847 if (new_master_mtu > mtu_limit) 1848 return -ERANGE; 1849 1850 /* If the master MTU isn't over limit, there's no need to check the CPU 1851 * MTU, since that surely isn't either. 1852 */ 1853 cpu_mtu = largest_mtu; 1854 1855 /* Start applying stuff */ 1856 if (new_master_mtu != old_master_mtu) { 1857 err = dev_set_mtu(master, new_master_mtu); 1858 if (err < 0) 1859 goto out_master_failed; 1860 1861 /* We only need to propagate the MTU of the CPU port to 1862 * upstream switches, so emit a notifier which updates them. 1863 */ 1864 err = dsa_port_mtu_change(cpu_dp, cpu_mtu); 1865 if (err) 1866 goto out_cpu_failed; 1867 } 1868 1869 err = ds->ops->port_change_mtu(ds, dp->index, new_mtu); 1870 if (err) 1871 goto out_port_failed; 1872 1873 dev->mtu = new_mtu; 1874 1875 dsa_bridge_mtu_normalization(dp); 1876 1877 return 0; 1878 1879 out_port_failed: 1880 if (new_master_mtu != old_master_mtu) 1881 dsa_port_mtu_change(cpu_dp, old_master_mtu - 1882 dsa_tag_protocol_overhead(cpu_dp->tag_ops)); 1883 out_cpu_failed: 1884 if (new_master_mtu != old_master_mtu) 1885 dev_set_mtu(master, old_master_mtu); 1886 out_master_failed: 1887 return err; 1888 } 1889 1890 static int __maybe_unused 1891 dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app) 1892 { 1893 struct dsa_port *dp = dsa_slave_to_port(dev); 1894 struct dsa_switch *ds = dp->ds; 1895 unsigned long mask, new_prio; 1896 int err, port = dp->index; 1897 1898 if (!ds->ops->port_set_default_prio) 1899 return -EOPNOTSUPP; 1900 1901 err = dcb_ieee_setapp(dev, app); 1902 if (err) 1903 return err; 1904 1905 mask = dcb_ieee_getapp_mask(dev, app); 1906 new_prio = __fls(mask); 1907 1908 err = ds->ops->port_set_default_prio(ds, port, new_prio); 1909 if (err) { 1910 dcb_ieee_delapp(dev, app); 1911 return err; 1912 } 1913 1914 return 0; 1915 } 1916 1917 static int __maybe_unused 1918 dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app) 1919 { 1920 struct dsa_port *dp = dsa_slave_to_port(dev); 1921 struct dsa_switch *ds = dp->ds; 1922 unsigned long mask, new_prio; 1923 int err, port = dp->index; 1924 u8 dscp = app->protocol; 1925 1926 if (!ds->ops->port_add_dscp_prio) 1927 return -EOPNOTSUPP; 1928 1929 if (dscp >= 64) { 1930 netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n", 1931 dscp); 1932 return -EINVAL; 1933 } 1934 1935 err = dcb_ieee_setapp(dev, app); 1936 if (err) 1937 return err; 1938 1939 mask = dcb_ieee_getapp_mask(dev, app); 1940 new_prio = __fls(mask); 1941 1942 err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio); 1943 if (err) { 1944 dcb_ieee_delapp(dev, app); 1945 return err; 1946 } 1947 1948 return 0; 1949 } 1950 1951 static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev, 1952 struct dcb_app *app) 1953 { 1954 switch (app->selector) { 1955 case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1956 switch (app->protocol) { 1957 case 0: 1958 return dsa_slave_dcbnl_set_default_prio(dev, app); 1959 default: 1960 return -EOPNOTSUPP; 1961 } 1962 break; 1963 case IEEE_8021QAZ_APP_SEL_DSCP: 1964 return dsa_slave_dcbnl_add_dscp_prio(dev, app); 1965 default: 1966 return -EOPNOTSUPP; 1967 } 1968 } 1969 1970 static int __maybe_unused 1971 dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app) 1972 { 1973 struct dsa_port *dp = dsa_slave_to_port(dev); 1974 struct dsa_switch *ds = dp->ds; 1975 unsigned long mask, new_prio; 1976 int err, port = dp->index; 1977 1978 if (!ds->ops->port_set_default_prio) 1979 return -EOPNOTSUPP; 1980 1981 err = dcb_ieee_delapp(dev, app); 1982 if (err) 1983 return err; 1984 1985 mask = dcb_ieee_getapp_mask(dev, app); 1986 new_prio = mask ? __fls(mask) : 0; 1987 1988 err = ds->ops->port_set_default_prio(ds, port, new_prio); 1989 if (err) { 1990 dcb_ieee_setapp(dev, app); 1991 return err; 1992 } 1993 1994 return 0; 1995 } 1996 1997 static int __maybe_unused 1998 dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app) 1999 { 2000 struct dsa_port *dp = dsa_slave_to_port(dev); 2001 struct dsa_switch *ds = dp->ds; 2002 int err, port = dp->index; 2003 u8 dscp = app->protocol; 2004 2005 if (!ds->ops->port_del_dscp_prio) 2006 return -EOPNOTSUPP; 2007 2008 err = dcb_ieee_delapp(dev, app); 2009 if (err) 2010 return err; 2011 2012 err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority); 2013 if (err) { 2014 dcb_ieee_setapp(dev, app); 2015 return err; 2016 } 2017 2018 return 0; 2019 } 2020 2021 static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev, 2022 struct dcb_app *app) 2023 { 2024 switch (app->selector) { 2025 case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 2026 switch (app->protocol) { 2027 case 0: 2028 return dsa_slave_dcbnl_del_default_prio(dev, app); 2029 default: 2030 return -EOPNOTSUPP; 2031 } 2032 break; 2033 case IEEE_8021QAZ_APP_SEL_DSCP: 2034 return dsa_slave_dcbnl_del_dscp_prio(dev, app); 2035 default: 2036 return -EOPNOTSUPP; 2037 } 2038 } 2039 2040 /* Pre-populate the DCB application priority table with the priorities 2041 * configured during switch setup, which we read from hardware here. 2042 */ 2043 static int dsa_slave_dcbnl_init(struct net_device *dev) 2044 { 2045 struct dsa_port *dp = dsa_slave_to_port(dev); 2046 struct dsa_switch *ds = dp->ds; 2047 int port = dp->index; 2048 int err; 2049 2050 if (ds->ops->port_get_default_prio) { 2051 int prio = ds->ops->port_get_default_prio(ds, port); 2052 struct dcb_app app = { 2053 .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE, 2054 .protocol = 0, 2055 .priority = prio, 2056 }; 2057 2058 if (prio < 0) 2059 return prio; 2060 2061 err = dcb_ieee_setapp(dev, &app); 2062 if (err) 2063 return err; 2064 } 2065 2066 if (ds->ops->port_get_dscp_prio) { 2067 int protocol; 2068 2069 for (protocol = 0; protocol < 64; protocol++) { 2070 struct dcb_app app = { 2071 .selector = IEEE_8021QAZ_APP_SEL_DSCP, 2072 .protocol = protocol, 2073 }; 2074 int prio; 2075 2076 prio = ds->ops->port_get_dscp_prio(ds, port, protocol); 2077 if (prio == -EOPNOTSUPP) 2078 continue; 2079 if (prio < 0) 2080 return prio; 2081 2082 app.priority = prio; 2083 2084 err = dcb_ieee_setapp(dev, &app); 2085 if (err) 2086 return err; 2087 } 2088 } 2089 2090 return 0; 2091 } 2092 2093 static const struct ethtool_ops dsa_slave_ethtool_ops = { 2094 .get_drvinfo = dsa_slave_get_drvinfo, 2095 .get_regs_len = dsa_slave_get_regs_len, 2096 .get_regs = dsa_slave_get_regs, 2097 .nway_reset = dsa_slave_nway_reset, 2098 .get_link = ethtool_op_get_link, 2099 .get_eeprom_len = dsa_slave_get_eeprom_len, 2100 .get_eeprom = dsa_slave_get_eeprom, 2101 .set_eeprom = dsa_slave_set_eeprom, 2102 .get_strings = dsa_slave_get_strings, 2103 .get_ethtool_stats = dsa_slave_get_ethtool_stats, 2104 .get_sset_count = dsa_slave_get_sset_count, 2105 .get_eth_phy_stats = dsa_slave_get_eth_phy_stats, 2106 .get_eth_mac_stats = dsa_slave_get_eth_mac_stats, 2107 .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats, 2108 .set_wol = dsa_slave_set_wol, 2109 .get_wol = dsa_slave_get_wol, 2110 .set_eee = dsa_slave_set_eee, 2111 .get_eee = dsa_slave_get_eee, 2112 .get_link_ksettings = dsa_slave_get_link_ksettings, 2113 .set_link_ksettings = dsa_slave_set_link_ksettings, 2114 .get_pauseparam = dsa_slave_get_pauseparam, 2115 .set_pauseparam = dsa_slave_set_pauseparam, 2116 .get_rxnfc = dsa_slave_get_rxnfc, 2117 .set_rxnfc = dsa_slave_set_rxnfc, 2118 .get_ts_info = dsa_slave_get_ts_info, 2119 .self_test = dsa_slave_net_selftest, 2120 }; 2121 2122 static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = { 2123 .ieee_setapp = dsa_slave_dcbnl_ieee_setapp, 2124 .ieee_delapp = dsa_slave_dcbnl_ieee_delapp, 2125 }; 2126 2127 static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 2128 { 2129 struct dsa_port *dp = dsa_slave_to_port(dev); 2130 2131 return &dp->devlink_port; 2132 } 2133 2134 static void dsa_slave_get_stats64(struct net_device *dev, 2135 struct rtnl_link_stats64 *s) 2136 { 2137 struct dsa_port *dp = dsa_slave_to_port(dev); 2138 struct dsa_switch *ds = dp->ds; 2139 2140 if (ds->ops->get_stats64) 2141 ds->ops->get_stats64(ds, dp->index, s); 2142 else 2143 dev_get_tstats64(dev, s); 2144 } 2145 2146 static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 2147 struct net_device_path *path) 2148 { 2149 struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 2150 struct dsa_port *cpu_dp = dp->cpu_dp; 2151 2152 path->dev = ctx->dev; 2153 path->type = DEV_PATH_DSA; 2154 path->dsa.proto = cpu_dp->tag_ops->proto; 2155 path->dsa.port = dp->index; 2156 ctx->dev = cpu_dp->master; 2157 2158 return 0; 2159 } 2160 2161 static const struct net_device_ops dsa_slave_netdev_ops = { 2162 .ndo_open = dsa_slave_open, 2163 .ndo_stop = dsa_slave_close, 2164 .ndo_start_xmit = dsa_slave_xmit, 2165 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 2166 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 2167 .ndo_set_mac_address = dsa_slave_set_mac_address, 2168 .ndo_fdb_dump = dsa_slave_fdb_dump, 2169 .ndo_eth_ioctl = dsa_slave_ioctl, 2170 .ndo_get_iflink = dsa_slave_get_iflink, 2171 #ifdef CONFIG_NET_POLL_CONTROLLER 2172 .ndo_netpoll_setup = dsa_slave_netpoll_setup, 2173 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 2174 .ndo_poll_controller = dsa_slave_poll_controller, 2175 #endif 2176 .ndo_setup_tc = dsa_slave_setup_tc, 2177 .ndo_get_stats64 = dsa_slave_get_stats64, 2178 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 2179 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 2180 .ndo_get_devlink_port = dsa_slave_get_devlink_port, 2181 .ndo_change_mtu = dsa_slave_change_mtu, 2182 .ndo_fill_forward_path = dsa_slave_fill_forward_path, 2183 }; 2184 2185 static struct device_type dsa_type = { 2186 .name = "dsa", 2187 }; 2188 2189 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 2190 { 2191 const struct dsa_port *dp = dsa_to_port(ds, port); 2192 2193 if (dp->pl) 2194 phylink_mac_change(dp->pl, up); 2195 } 2196 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 2197 2198 static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 2199 struct phylink_link_state *state) 2200 { 2201 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 2202 struct dsa_switch *ds = dp->ds; 2203 2204 /* No need to check that this operation is valid, the callback would 2205 * not be called if it was not. 2206 */ 2207 ds->ops->phylink_fixed_state(ds, dp->index, state); 2208 } 2209 2210 /* slave device setup *******************************************************/ 2211 static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr, 2212 u32 flags) 2213 { 2214 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2215 struct dsa_switch *ds = dp->ds; 2216 2217 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 2218 if (!slave_dev->phydev) { 2219 netdev_err(slave_dev, "no phy at %d\n", addr); 2220 return -ENODEV; 2221 } 2222 2223 slave_dev->phydev->dev_flags |= flags; 2224 2225 return phylink_connect_phy(dp->pl, slave_dev->phydev); 2226 } 2227 2228 static int dsa_slave_phy_setup(struct net_device *slave_dev) 2229 { 2230 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2231 struct device_node *port_dn = dp->dn; 2232 struct dsa_switch *ds = dp->ds; 2233 u32 phy_flags = 0; 2234 int ret; 2235 2236 dp->pl_config.dev = &slave_dev->dev; 2237 dp->pl_config.type = PHYLINK_NETDEV; 2238 2239 /* The get_fixed_state callback takes precedence over polling the 2240 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 2241 * this if the switch provides such a callback. 2242 */ 2243 if (ds->ops->phylink_fixed_state) { 2244 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 2245 dp->pl_config.poll_fixed_state = true; 2246 } 2247 2248 ret = dsa_port_phylink_create(dp); 2249 if (ret) 2250 return ret; 2251 2252 if (ds->ops->get_phy_flags) 2253 phy_flags = ds->ops->get_phy_flags(ds, dp->index); 2254 2255 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 2256 if (ret == -ENODEV && ds->slave_mii_bus) { 2257 /* We could not connect to a designated PHY or SFP, so try to 2258 * use the switch internal MDIO bus instead 2259 */ 2260 ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags); 2261 } 2262 if (ret) { 2263 netdev_err(slave_dev, "failed to connect to PHY: %pe\n", 2264 ERR_PTR(ret)); 2265 phylink_destroy(dp->pl); 2266 } 2267 2268 return ret; 2269 } 2270 2271 void dsa_slave_setup_tagger(struct net_device *slave) 2272 { 2273 struct dsa_port *dp = dsa_slave_to_port(slave); 2274 struct dsa_slave_priv *p = netdev_priv(slave); 2275 const struct dsa_port *cpu_dp = dp->cpu_dp; 2276 struct net_device *master = cpu_dp->master; 2277 const struct dsa_switch *ds = dp->ds; 2278 2279 slave->needed_headroom = cpu_dp->tag_ops->needed_headroom; 2280 slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom; 2281 /* Try to save one extra realloc later in the TX path (in the master) 2282 * by also inheriting the master's needed headroom and tailroom. 2283 * The 8021q driver also does this. 2284 */ 2285 slave->needed_headroom += master->needed_headroom; 2286 slave->needed_tailroom += master->needed_tailroom; 2287 2288 p->xmit = cpu_dp->tag_ops->xmit; 2289 2290 slave->features = master->vlan_features | NETIF_F_HW_TC; 2291 slave->hw_features |= NETIF_F_HW_TC; 2292 slave->features |= NETIF_F_LLTX; 2293 if (slave->needed_tailroom) 2294 slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST); 2295 if (ds->needs_standalone_vlan_filtering) 2296 slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2297 } 2298 2299 int dsa_slave_suspend(struct net_device *slave_dev) 2300 { 2301 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2302 2303 if (!netif_running(slave_dev)) 2304 return 0; 2305 2306 netif_device_detach(slave_dev); 2307 2308 rtnl_lock(); 2309 phylink_stop(dp->pl); 2310 rtnl_unlock(); 2311 2312 return 0; 2313 } 2314 2315 int dsa_slave_resume(struct net_device *slave_dev) 2316 { 2317 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2318 2319 if (!netif_running(slave_dev)) 2320 return 0; 2321 2322 netif_device_attach(slave_dev); 2323 2324 rtnl_lock(); 2325 phylink_start(dp->pl); 2326 rtnl_unlock(); 2327 2328 return 0; 2329 } 2330 2331 int dsa_slave_create(struct dsa_port *port) 2332 { 2333 const struct dsa_port *cpu_dp = port->cpu_dp; 2334 struct net_device *master = cpu_dp->master; 2335 struct dsa_switch *ds = port->ds; 2336 const char *name = port->name; 2337 struct net_device *slave_dev; 2338 struct dsa_slave_priv *p; 2339 int ret; 2340 2341 if (!ds->num_tx_queues) 2342 ds->num_tx_queues = 1; 2343 2344 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 2345 NET_NAME_UNKNOWN, ether_setup, 2346 ds->num_tx_queues, 1); 2347 if (slave_dev == NULL) 2348 return -ENOMEM; 2349 2350 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 2351 #if IS_ENABLED(CONFIG_DCB) 2352 slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops; 2353 #endif 2354 if (!is_zero_ether_addr(port->mac)) 2355 eth_hw_addr_set(slave_dev, port->mac); 2356 else 2357 eth_hw_addr_inherit(slave_dev, master); 2358 slave_dev->priv_flags |= IFF_NO_QUEUE; 2359 if (dsa_switch_supports_uc_filtering(ds)) 2360 slave_dev->priv_flags |= IFF_UNICAST_FLT; 2361 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 2362 if (ds->ops->port_max_mtu) 2363 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 2364 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 2365 2366 SET_NETDEV_DEV(slave_dev, port->ds->dev); 2367 slave_dev->dev.of_node = port->dn; 2368 slave_dev->vlan_features = master->vlan_features; 2369 2370 p = netdev_priv(slave_dev); 2371 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 2372 if (!slave_dev->tstats) { 2373 free_netdev(slave_dev); 2374 return -ENOMEM; 2375 } 2376 2377 ret = gro_cells_init(&p->gcells, slave_dev); 2378 if (ret) 2379 goto out_free; 2380 2381 p->dp = port; 2382 INIT_LIST_HEAD(&p->mall_tc_list); 2383 port->slave = slave_dev; 2384 dsa_slave_setup_tagger(slave_dev); 2385 2386 netif_carrier_off(slave_dev); 2387 2388 ret = dsa_slave_phy_setup(slave_dev); 2389 if (ret) { 2390 netdev_err(slave_dev, 2391 "error %d setting up PHY for tree %d, switch %d, port %d\n", 2392 ret, ds->dst->index, ds->index, port->index); 2393 goto out_gcells; 2394 } 2395 2396 rtnl_lock(); 2397 2398 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 2399 if (ret && ret != -EOPNOTSUPP) 2400 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 2401 ret, ETH_DATA_LEN, port->index); 2402 2403 ret = register_netdevice(slave_dev); 2404 if (ret) { 2405 netdev_err(master, "error %d registering interface %s\n", 2406 ret, slave_dev->name); 2407 rtnl_unlock(); 2408 goto out_phy; 2409 } 2410 2411 if (IS_ENABLED(CONFIG_DCB)) { 2412 ret = dsa_slave_dcbnl_init(slave_dev); 2413 if (ret) { 2414 netdev_err(slave_dev, 2415 "failed to initialize DCB: %pe\n", 2416 ERR_PTR(ret)); 2417 rtnl_unlock(); 2418 goto out_unregister; 2419 } 2420 } 2421 2422 ret = netdev_upper_dev_link(master, slave_dev, NULL); 2423 2424 rtnl_unlock(); 2425 2426 if (ret) 2427 goto out_unregister; 2428 2429 return 0; 2430 2431 out_unregister: 2432 unregister_netdev(slave_dev); 2433 out_phy: 2434 rtnl_lock(); 2435 phylink_disconnect_phy(p->dp->pl); 2436 rtnl_unlock(); 2437 phylink_destroy(p->dp->pl); 2438 out_gcells: 2439 gro_cells_destroy(&p->gcells); 2440 out_free: 2441 free_percpu(slave_dev->tstats); 2442 free_netdev(slave_dev); 2443 port->slave = NULL; 2444 return ret; 2445 } 2446 2447 void dsa_slave_destroy(struct net_device *slave_dev) 2448 { 2449 struct net_device *master = dsa_slave_to_master(slave_dev); 2450 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2451 struct dsa_slave_priv *p = netdev_priv(slave_dev); 2452 2453 netif_carrier_off(slave_dev); 2454 rtnl_lock(); 2455 netdev_upper_dev_unlink(master, slave_dev); 2456 unregister_netdevice(slave_dev); 2457 phylink_disconnect_phy(dp->pl); 2458 rtnl_unlock(); 2459 2460 phylink_destroy(dp->pl); 2461 gro_cells_destroy(&p->gcells); 2462 free_percpu(slave_dev->tstats); 2463 free_netdev(slave_dev); 2464 } 2465 2466 bool dsa_slave_dev_check(const struct net_device *dev) 2467 { 2468 return dev->netdev_ops == &dsa_slave_netdev_ops; 2469 } 2470 EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2471 2472 static int dsa_slave_changeupper(struct net_device *dev, 2473 struct netdev_notifier_changeupper_info *info) 2474 { 2475 struct dsa_port *dp = dsa_slave_to_port(dev); 2476 struct netlink_ext_ack *extack; 2477 int err = NOTIFY_DONE; 2478 2479 extack = netdev_notifier_info_to_extack(&info->info); 2480 2481 if (netif_is_bridge_master(info->upper_dev)) { 2482 if (info->linking) { 2483 err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2484 if (!err) 2485 dsa_bridge_mtu_normalization(dp); 2486 if (err == -EOPNOTSUPP) { 2487 NL_SET_ERR_MSG_MOD(extack, 2488 "Offloading not supported"); 2489 err = 0; 2490 } 2491 err = notifier_from_errno(err); 2492 } else { 2493 dsa_port_bridge_leave(dp, info->upper_dev); 2494 err = NOTIFY_OK; 2495 } 2496 } else if (netif_is_lag_master(info->upper_dev)) { 2497 if (info->linking) { 2498 err = dsa_port_lag_join(dp, info->upper_dev, 2499 info->upper_info, extack); 2500 if (err == -EOPNOTSUPP) { 2501 NL_SET_ERR_MSG_MOD(info->info.extack, 2502 "Offloading not supported"); 2503 err = 0; 2504 } 2505 err = notifier_from_errno(err); 2506 } else { 2507 dsa_port_lag_leave(dp, info->upper_dev); 2508 err = NOTIFY_OK; 2509 } 2510 } else if (is_hsr_master(info->upper_dev)) { 2511 if (info->linking) { 2512 err = dsa_port_hsr_join(dp, info->upper_dev); 2513 if (err == -EOPNOTSUPP) { 2514 NL_SET_ERR_MSG_MOD(info->info.extack, 2515 "Offloading not supported"); 2516 err = 0; 2517 } 2518 err = notifier_from_errno(err); 2519 } else { 2520 dsa_port_hsr_leave(dp, info->upper_dev); 2521 err = NOTIFY_OK; 2522 } 2523 } 2524 2525 return err; 2526 } 2527 2528 static int dsa_slave_prechangeupper(struct net_device *dev, 2529 struct netdev_notifier_changeupper_info *info) 2530 { 2531 struct dsa_port *dp = dsa_slave_to_port(dev); 2532 2533 if (netif_is_bridge_master(info->upper_dev) && !info->linking) 2534 dsa_port_pre_bridge_leave(dp, info->upper_dev); 2535 else if (netif_is_lag_master(info->upper_dev) && !info->linking) 2536 dsa_port_pre_lag_leave(dp, info->upper_dev); 2537 /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be 2538 * meaningfully enslaved to a bridge yet 2539 */ 2540 2541 return NOTIFY_DONE; 2542 } 2543 2544 static int 2545 dsa_slave_lag_changeupper(struct net_device *dev, 2546 struct netdev_notifier_changeupper_info *info) 2547 { 2548 struct net_device *lower; 2549 struct list_head *iter; 2550 int err = NOTIFY_DONE; 2551 struct dsa_port *dp; 2552 2553 netdev_for_each_lower_dev(dev, lower, iter) { 2554 if (!dsa_slave_dev_check(lower)) 2555 continue; 2556 2557 dp = dsa_slave_to_port(lower); 2558 if (!dp->lag) 2559 /* Software LAG */ 2560 continue; 2561 2562 err = dsa_slave_changeupper(lower, info); 2563 if (notifier_to_errno(err)) 2564 break; 2565 } 2566 2567 return err; 2568 } 2569 2570 /* Same as dsa_slave_lag_changeupper() except that it calls 2571 * dsa_slave_prechangeupper() 2572 */ 2573 static int 2574 dsa_slave_lag_prechangeupper(struct net_device *dev, 2575 struct netdev_notifier_changeupper_info *info) 2576 { 2577 struct net_device *lower; 2578 struct list_head *iter; 2579 int err = NOTIFY_DONE; 2580 struct dsa_port *dp; 2581 2582 netdev_for_each_lower_dev(dev, lower, iter) { 2583 if (!dsa_slave_dev_check(lower)) 2584 continue; 2585 2586 dp = dsa_slave_to_port(lower); 2587 if (!dp->lag) 2588 /* Software LAG */ 2589 continue; 2590 2591 err = dsa_slave_prechangeupper(lower, info); 2592 if (notifier_to_errno(err)) 2593 break; 2594 } 2595 2596 return err; 2597 } 2598 2599 static int 2600 dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2601 struct netdev_notifier_changeupper_info *info) 2602 { 2603 struct netlink_ext_ack *ext_ack; 2604 struct net_device *slave, *br; 2605 struct dsa_port *dp; 2606 2607 ext_ack = netdev_notifier_info_to_extack(&info->info); 2608 2609 if (!is_vlan_dev(dev)) 2610 return NOTIFY_DONE; 2611 2612 slave = vlan_dev_real_dev(dev); 2613 if (!dsa_slave_dev_check(slave)) 2614 return NOTIFY_DONE; 2615 2616 dp = dsa_slave_to_port(slave); 2617 br = dsa_port_bridge_dev_get(dp); 2618 if (!br) 2619 return NOTIFY_DONE; 2620 2621 /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 2622 if (br_vlan_enabled(br) && 2623 netif_is_bridge_master(info->upper_dev) && info->linking) { 2624 NL_SET_ERR_MSG_MOD(ext_ack, 2625 "Cannot enslave VLAN device into VLAN aware bridge"); 2626 return notifier_from_errno(-EINVAL); 2627 } 2628 2629 return NOTIFY_DONE; 2630 } 2631 2632 static int 2633 dsa_slave_check_8021q_upper(struct net_device *dev, 2634 struct netdev_notifier_changeupper_info *info) 2635 { 2636 struct dsa_port *dp = dsa_slave_to_port(dev); 2637 struct net_device *br = dsa_port_bridge_dev_get(dp); 2638 struct bridge_vlan_info br_info; 2639 struct netlink_ext_ack *extack; 2640 int err = NOTIFY_DONE; 2641 u16 vid; 2642 2643 if (!br || !br_vlan_enabled(br)) 2644 return NOTIFY_DONE; 2645 2646 extack = netdev_notifier_info_to_extack(&info->info); 2647 vid = vlan_dev_vlan_id(info->upper_dev); 2648 2649 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 2650 * device, respectively the VID is not found, returning 2651 * 0 means success, which is a failure for us here. 2652 */ 2653 err = br_vlan_get_info(br, vid, &br_info); 2654 if (err == 0) { 2655 NL_SET_ERR_MSG_MOD(extack, 2656 "This VLAN is already configured by the bridge"); 2657 return notifier_from_errno(-EBUSY); 2658 } 2659 2660 return NOTIFY_DONE; 2661 } 2662 2663 static int 2664 dsa_slave_prechangeupper_sanity_check(struct net_device *dev, 2665 struct netdev_notifier_changeupper_info *info) 2666 { 2667 struct dsa_switch *ds; 2668 struct dsa_port *dp; 2669 int err; 2670 2671 if (!dsa_slave_dev_check(dev)) 2672 return dsa_prevent_bridging_8021q_upper(dev, info); 2673 2674 dp = dsa_slave_to_port(dev); 2675 ds = dp->ds; 2676 2677 if (ds->ops->port_prechangeupper) { 2678 err = ds->ops->port_prechangeupper(ds, dp->index, info); 2679 if (err) 2680 return notifier_from_errno(err); 2681 } 2682 2683 if (is_vlan_dev(info->upper_dev)) 2684 return dsa_slave_check_8021q_upper(dev, info); 2685 2686 return NOTIFY_DONE; 2687 } 2688 2689 static int dsa_slave_netdevice_event(struct notifier_block *nb, 2690 unsigned long event, void *ptr) 2691 { 2692 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2693 2694 switch (event) { 2695 case NETDEV_PRECHANGEUPPER: { 2696 struct netdev_notifier_changeupper_info *info = ptr; 2697 int err; 2698 2699 err = dsa_slave_prechangeupper_sanity_check(dev, info); 2700 if (err != NOTIFY_DONE) 2701 return err; 2702 2703 if (dsa_slave_dev_check(dev)) 2704 return dsa_slave_prechangeupper(dev, ptr); 2705 2706 if (netif_is_lag_master(dev)) 2707 return dsa_slave_lag_prechangeupper(dev, ptr); 2708 2709 break; 2710 } 2711 case NETDEV_CHANGEUPPER: 2712 if (dsa_slave_dev_check(dev)) 2713 return dsa_slave_changeupper(dev, ptr); 2714 2715 if (netif_is_lag_master(dev)) 2716 return dsa_slave_lag_changeupper(dev, ptr); 2717 2718 break; 2719 case NETDEV_CHANGELOWERSTATE: { 2720 struct netdev_notifier_changelowerstate_info *info = ptr; 2721 struct dsa_port *dp; 2722 int err; 2723 2724 if (!dsa_slave_dev_check(dev)) 2725 break; 2726 2727 dp = dsa_slave_to_port(dev); 2728 2729 err = dsa_port_lag_change(dp, info->lower_state_info); 2730 return notifier_from_errno(err); 2731 } 2732 case NETDEV_CHANGE: 2733 case NETDEV_UP: { 2734 /* Track state of master port. 2735 * DSA driver may require the master port (and indirectly 2736 * the tagger) to be available for some special operation. 2737 */ 2738 if (netdev_uses_dsa(dev)) { 2739 struct dsa_port *cpu_dp = dev->dsa_ptr; 2740 struct dsa_switch_tree *dst = cpu_dp->ds->dst; 2741 2742 /* Track when the master port is UP */ 2743 dsa_tree_master_oper_state_change(dst, dev, 2744 netif_oper_up(dev)); 2745 2746 /* Track when the master port is ready and can accept 2747 * packet. 2748 * NETDEV_UP event is not enough to flag a port as ready. 2749 * We also have to wait for linkwatch_do_dev to dev_activate 2750 * and emit a NETDEV_CHANGE event. 2751 * We check if a master port is ready by checking if the dev 2752 * have a qdisc assigned and is not noop. 2753 */ 2754 dsa_tree_master_admin_state_change(dst, dev, 2755 !qdisc_tx_is_noop(dev)); 2756 2757 return NOTIFY_OK; 2758 } 2759 2760 return NOTIFY_DONE; 2761 } 2762 case NETDEV_GOING_DOWN: { 2763 struct dsa_port *dp, *cpu_dp; 2764 struct dsa_switch_tree *dst; 2765 LIST_HEAD(close_list); 2766 2767 if (!netdev_uses_dsa(dev)) 2768 return NOTIFY_DONE; 2769 2770 cpu_dp = dev->dsa_ptr; 2771 dst = cpu_dp->ds->dst; 2772 2773 dsa_tree_master_admin_state_change(dst, dev, false); 2774 2775 list_for_each_entry(dp, &dst->ports, list) { 2776 if (!dsa_port_is_user(dp)) 2777 continue; 2778 2779 list_add(&dp->slave->close_list, &close_list); 2780 } 2781 2782 dev_close_many(&close_list, true); 2783 2784 return NOTIFY_OK; 2785 } 2786 default: 2787 break; 2788 } 2789 2790 return NOTIFY_DONE; 2791 } 2792 2793 static void 2794 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2795 { 2796 struct switchdev_notifier_fdb_info info = {}; 2797 2798 info.addr = switchdev_work->addr; 2799 info.vid = switchdev_work->vid; 2800 info.offloaded = true; 2801 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 2802 switchdev_work->orig_dev, &info.info, NULL); 2803 } 2804 2805 static void dsa_slave_switchdev_event_work(struct work_struct *work) 2806 { 2807 struct dsa_switchdev_event_work *switchdev_work = 2808 container_of(work, struct dsa_switchdev_event_work, work); 2809 const unsigned char *addr = switchdev_work->addr; 2810 struct net_device *dev = switchdev_work->dev; 2811 u16 vid = switchdev_work->vid; 2812 struct dsa_switch *ds; 2813 struct dsa_port *dp; 2814 int err; 2815 2816 dp = dsa_slave_to_port(dev); 2817 ds = dp->ds; 2818 2819 switch (switchdev_work->event) { 2820 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2821 if (switchdev_work->host_addr) 2822 err = dsa_port_bridge_host_fdb_add(dp, addr, vid); 2823 else if (dp->lag) 2824 err = dsa_port_lag_fdb_add(dp, addr, vid); 2825 else 2826 err = dsa_port_fdb_add(dp, addr, vid); 2827 if (err) { 2828 dev_err(ds->dev, 2829 "port %d failed to add %pM vid %d to fdb: %d\n", 2830 dp->index, addr, vid, err); 2831 break; 2832 } 2833 dsa_fdb_offload_notify(switchdev_work); 2834 break; 2835 2836 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2837 if (switchdev_work->host_addr) 2838 err = dsa_port_bridge_host_fdb_del(dp, addr, vid); 2839 else if (dp->lag) 2840 err = dsa_port_lag_fdb_del(dp, addr, vid); 2841 else 2842 err = dsa_port_fdb_del(dp, addr, vid); 2843 if (err) { 2844 dev_err(ds->dev, 2845 "port %d failed to delete %pM vid %d from fdb: %d\n", 2846 dp->index, addr, vid, err); 2847 } 2848 2849 break; 2850 } 2851 2852 kfree(switchdev_work); 2853 } 2854 2855 static bool dsa_foreign_dev_check(const struct net_device *dev, 2856 const struct net_device *foreign_dev) 2857 { 2858 const struct dsa_port *dp = dsa_slave_to_port(dev); 2859 struct dsa_switch_tree *dst = dp->ds->dst; 2860 2861 if (netif_is_bridge_master(foreign_dev)) 2862 return !dsa_tree_offloads_bridge_dev(dst, foreign_dev); 2863 2864 if (netif_is_bridge_port(foreign_dev)) 2865 return !dsa_tree_offloads_bridge_port(dst, foreign_dev); 2866 2867 /* Everything else is foreign */ 2868 return true; 2869 } 2870 2871 static int dsa_slave_fdb_event(struct net_device *dev, 2872 struct net_device *orig_dev, 2873 unsigned long event, const void *ctx, 2874 const struct switchdev_notifier_fdb_info *fdb_info) 2875 { 2876 struct dsa_switchdev_event_work *switchdev_work; 2877 struct dsa_port *dp = dsa_slave_to_port(dev); 2878 bool host_addr = fdb_info->is_local; 2879 struct dsa_switch *ds = dp->ds; 2880 2881 if (ctx && ctx != dp) 2882 return 0; 2883 2884 if (!dp->bridge) 2885 return 0; 2886 2887 if (switchdev_fdb_is_dynamically_learned(fdb_info)) { 2888 if (dsa_port_offloads_bridge_port(dp, orig_dev)) 2889 return 0; 2890 2891 /* FDB entries learned by the software bridge or by foreign 2892 * bridge ports should be installed as host addresses only if 2893 * the driver requests assisted learning. 2894 */ 2895 if (!ds->assisted_learning_on_cpu_port) 2896 return 0; 2897 } 2898 2899 /* Also treat FDB entries on foreign interfaces bridged with us as host 2900 * addresses. 2901 */ 2902 if (dsa_foreign_dev_check(dev, orig_dev)) 2903 host_addr = true; 2904 2905 /* Check early that we're not doing work in vain. 2906 * Host addresses on LAG ports still require regular FDB ops, 2907 * since the CPU port isn't in a LAG. 2908 */ 2909 if (dp->lag && !host_addr) { 2910 if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del) 2911 return -EOPNOTSUPP; 2912 } else { 2913 if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del) 2914 return -EOPNOTSUPP; 2915 } 2916 2917 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2918 if (!switchdev_work) 2919 return -ENOMEM; 2920 2921 netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n", 2922 event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting", 2923 orig_dev->name, fdb_info->addr, fdb_info->vid, 2924 host_addr ? " as host address" : ""); 2925 2926 INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work); 2927 switchdev_work->event = event; 2928 switchdev_work->dev = dev; 2929 switchdev_work->orig_dev = orig_dev; 2930 2931 ether_addr_copy(switchdev_work->addr, fdb_info->addr); 2932 switchdev_work->vid = fdb_info->vid; 2933 switchdev_work->host_addr = host_addr; 2934 2935 dsa_schedule_work(&switchdev_work->work); 2936 2937 return 0; 2938 } 2939 2940 /* Called under rcu_read_lock() */ 2941 static int dsa_slave_switchdev_event(struct notifier_block *unused, 2942 unsigned long event, void *ptr) 2943 { 2944 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2945 int err; 2946 2947 switch (event) { 2948 case SWITCHDEV_PORT_ATTR_SET: 2949 err = switchdev_handle_port_attr_set(dev, ptr, 2950 dsa_slave_dev_check, 2951 dsa_slave_port_attr_set); 2952 return notifier_from_errno(err); 2953 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2954 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2955 err = switchdev_handle_fdb_event_to_device(dev, event, ptr, 2956 dsa_slave_dev_check, 2957 dsa_foreign_dev_check, 2958 dsa_slave_fdb_event); 2959 return notifier_from_errno(err); 2960 default: 2961 return NOTIFY_DONE; 2962 } 2963 2964 return NOTIFY_OK; 2965 } 2966 2967 static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 2968 unsigned long event, void *ptr) 2969 { 2970 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2971 int err; 2972 2973 switch (event) { 2974 case SWITCHDEV_PORT_OBJ_ADD: 2975 err = switchdev_handle_port_obj_add_foreign(dev, ptr, 2976 dsa_slave_dev_check, 2977 dsa_foreign_dev_check, 2978 dsa_slave_port_obj_add); 2979 return notifier_from_errno(err); 2980 case SWITCHDEV_PORT_OBJ_DEL: 2981 err = switchdev_handle_port_obj_del_foreign(dev, ptr, 2982 dsa_slave_dev_check, 2983 dsa_foreign_dev_check, 2984 dsa_slave_port_obj_del); 2985 return notifier_from_errno(err); 2986 case SWITCHDEV_PORT_ATTR_SET: 2987 err = switchdev_handle_port_attr_set(dev, ptr, 2988 dsa_slave_dev_check, 2989 dsa_slave_port_attr_set); 2990 return notifier_from_errno(err); 2991 } 2992 2993 return NOTIFY_DONE; 2994 } 2995 2996 static struct notifier_block dsa_slave_nb __read_mostly = { 2997 .notifier_call = dsa_slave_netdevice_event, 2998 }; 2999 3000 struct notifier_block dsa_slave_switchdev_notifier = { 3001 .notifier_call = dsa_slave_switchdev_event, 3002 }; 3003 3004 struct notifier_block dsa_slave_switchdev_blocking_notifier = { 3005 .notifier_call = dsa_slave_switchdev_blocking_event, 3006 }; 3007 3008 int dsa_slave_register_notifier(void) 3009 { 3010 struct notifier_block *nb; 3011 int err; 3012 3013 err = register_netdevice_notifier(&dsa_slave_nb); 3014 if (err) 3015 return err; 3016 3017 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 3018 if (err) 3019 goto err_switchdev_nb; 3020 3021 nb = &dsa_slave_switchdev_blocking_notifier; 3022 err = register_switchdev_blocking_notifier(nb); 3023 if (err) 3024 goto err_switchdev_blocking_nb; 3025 3026 return 0; 3027 3028 err_switchdev_blocking_nb: 3029 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3030 err_switchdev_nb: 3031 unregister_netdevice_notifier(&dsa_slave_nb); 3032 return err; 3033 } 3034 3035 void dsa_slave_unregister_notifier(void) 3036 { 3037 struct notifier_block *nb; 3038 int err; 3039 3040 nb = &dsa_slave_switchdev_blocking_notifier; 3041 err = unregister_switchdev_blocking_notifier(nb); 3042 if (err) 3043 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 3044 3045 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3046 if (err) 3047 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 3048 3049 err = unregister_netdevice_notifier(&dsa_slave_nb); 3050 if (err) 3051 pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 3052 } 3053