12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 291da11f8SLennert Buytenhek /* 391da11f8SLennert Buytenhek * net/dsa/slave.c - Slave device handling 4e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 591da11f8SLennert Buytenhek */ 691da11f8SLennert Buytenhek 791da11f8SLennert Buytenhek #include <linux/list.h> 8df02c6ffSLennert Buytenhek #include <linux/etherdevice.h> 9b73adef6SFlorian Fainelli #include <linux/netdevice.h> 1091da11f8SLennert Buytenhek #include <linux/phy.h> 11a2820543SFlorian Fainelli #include <linux/phy_fixed.h> 12aab9c406SFlorian Fainelli #include <linux/phylink.h> 130d8bcdd3SFlorian Fainelli #include <linux/of_net.h> 140d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h> 157f854420SAndrew Lunn #include <linux/mdio.h> 16b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 17f50f2127SFlorian Fainelli #include <net/pkt_cls.h> 18a71acad9SOleksij Rempel #include <net/selftests.h> 19f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 20b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2118596f50SGeorge McCollister #include <linux/if_hsr.h> 22d538eca8SVladimir Oltean #include <net/dcbnl.h> 2304ff53f9SFlorian Fainelli #include <linux/netpoll.h> 24ea5dd34bSVivien Didelot 2591da11f8SLennert Buytenhek #include "dsa_priv.h" 2691da11f8SLennert Buytenhek 275e8a1e03SVladimir Oltean static void dsa_slave_standalone_event_work(struct work_struct *work) 285e8a1e03SVladimir Oltean { 295e8a1e03SVladimir Oltean struct dsa_standalone_event_work *standalone_work = 305e8a1e03SVladimir Oltean container_of(work, struct dsa_standalone_event_work, work); 315e8a1e03SVladimir Oltean const unsigned char *addr = standalone_work->addr; 325e8a1e03SVladimir Oltean struct net_device *dev = standalone_work->dev; 335e8a1e03SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 345e8a1e03SVladimir Oltean struct switchdev_obj_port_mdb mdb; 355e8a1e03SVladimir Oltean struct dsa_switch *ds = dp->ds; 365e8a1e03SVladimir Oltean u16 vid = standalone_work->vid; 375e8a1e03SVladimir Oltean int err; 385e8a1e03SVladimir Oltean 395e8a1e03SVladimir Oltean switch (standalone_work->event) { 405e8a1e03SVladimir Oltean case DSA_UC_ADD: 415e8a1e03SVladimir Oltean err = dsa_port_standalone_host_fdb_add(dp, addr, vid); 425e8a1e03SVladimir Oltean if (err) { 435e8a1e03SVladimir Oltean dev_err(ds->dev, 445e8a1e03SVladimir Oltean "port %d failed to add %pM vid %d to fdb: %d\n", 455e8a1e03SVladimir Oltean dp->index, addr, vid, err); 465e8a1e03SVladimir Oltean break; 475e8a1e03SVladimir Oltean } 485e8a1e03SVladimir Oltean break; 495e8a1e03SVladimir Oltean 505e8a1e03SVladimir Oltean case DSA_UC_DEL: 515e8a1e03SVladimir Oltean err = dsa_port_standalone_host_fdb_del(dp, addr, vid); 525e8a1e03SVladimir Oltean if (err) { 535e8a1e03SVladimir Oltean dev_err(ds->dev, 545e8a1e03SVladimir Oltean "port %d failed to delete %pM vid %d from fdb: %d\n", 555e8a1e03SVladimir Oltean dp->index, addr, vid, err); 565e8a1e03SVladimir Oltean } 575e8a1e03SVladimir Oltean 585e8a1e03SVladimir Oltean break; 595e8a1e03SVladimir Oltean case DSA_MC_ADD: 605e8a1e03SVladimir Oltean ether_addr_copy(mdb.addr, addr); 615e8a1e03SVladimir Oltean mdb.vid = vid; 625e8a1e03SVladimir Oltean 635e8a1e03SVladimir Oltean err = dsa_port_standalone_host_mdb_add(dp, &mdb); 645e8a1e03SVladimir Oltean if (err) { 655e8a1e03SVladimir Oltean dev_err(ds->dev, 665e8a1e03SVladimir Oltean "port %d failed to add %pM vid %d to mdb: %d\n", 675e8a1e03SVladimir Oltean dp->index, addr, vid, err); 685e8a1e03SVladimir Oltean break; 695e8a1e03SVladimir Oltean } 705e8a1e03SVladimir Oltean break; 715e8a1e03SVladimir Oltean case DSA_MC_DEL: 725e8a1e03SVladimir Oltean ether_addr_copy(mdb.addr, addr); 735e8a1e03SVladimir Oltean mdb.vid = vid; 745e8a1e03SVladimir Oltean 755e8a1e03SVladimir Oltean err = dsa_port_standalone_host_mdb_del(dp, &mdb); 765e8a1e03SVladimir Oltean if (err) { 775e8a1e03SVladimir Oltean dev_err(ds->dev, 785e8a1e03SVladimir Oltean "port %d failed to delete %pM vid %d from mdb: %d\n", 795e8a1e03SVladimir Oltean dp->index, addr, vid, err); 805e8a1e03SVladimir Oltean } 815e8a1e03SVladimir Oltean 825e8a1e03SVladimir Oltean break; 835e8a1e03SVladimir Oltean } 845e8a1e03SVladimir Oltean 855e8a1e03SVladimir Oltean kfree(standalone_work); 865e8a1e03SVladimir Oltean } 875e8a1e03SVladimir Oltean 885e8a1e03SVladimir Oltean static int dsa_slave_schedule_standalone_work(struct net_device *dev, 895e8a1e03SVladimir Oltean enum dsa_standalone_event event, 905e8a1e03SVladimir Oltean const unsigned char *addr, 915e8a1e03SVladimir Oltean u16 vid) 925e8a1e03SVladimir Oltean { 935e8a1e03SVladimir Oltean struct dsa_standalone_event_work *standalone_work; 945e8a1e03SVladimir Oltean 955e8a1e03SVladimir Oltean standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC); 965e8a1e03SVladimir Oltean if (!standalone_work) 975e8a1e03SVladimir Oltean return -ENOMEM; 985e8a1e03SVladimir Oltean 995e8a1e03SVladimir Oltean INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work); 1005e8a1e03SVladimir Oltean standalone_work->event = event; 1015e8a1e03SVladimir Oltean standalone_work->dev = dev; 1025e8a1e03SVladimir Oltean 1035e8a1e03SVladimir Oltean ether_addr_copy(standalone_work->addr, addr); 1045e8a1e03SVladimir Oltean standalone_work->vid = vid; 1055e8a1e03SVladimir Oltean 1065e8a1e03SVladimir Oltean dsa_schedule_work(&standalone_work->work); 1075e8a1e03SVladimir Oltean 1085e8a1e03SVladimir Oltean return 0; 1095e8a1e03SVladimir Oltean } 1105e8a1e03SVladimir Oltean 1115e8a1e03SVladimir Oltean static int dsa_slave_sync_uc(struct net_device *dev, 1125e8a1e03SVladimir Oltean const unsigned char *addr) 1135e8a1e03SVladimir Oltean { 1145e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0); 1155e8a1e03SVladimir Oltean } 1165e8a1e03SVladimir Oltean 1175e8a1e03SVladimir Oltean static int dsa_slave_unsync_uc(struct net_device *dev, 1185e8a1e03SVladimir Oltean const unsigned char *addr) 1195e8a1e03SVladimir Oltean { 1205e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0); 1215e8a1e03SVladimir Oltean } 1225e8a1e03SVladimir Oltean 1235e8a1e03SVladimir Oltean static int dsa_slave_sync_mc(struct net_device *dev, 1245e8a1e03SVladimir Oltean const unsigned char *addr) 1255e8a1e03SVladimir Oltean { 1265e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0); 1275e8a1e03SVladimir Oltean } 1285e8a1e03SVladimir Oltean 1295e8a1e03SVladimir Oltean static int dsa_slave_unsync_mc(struct net_device *dev, 1305e8a1e03SVladimir Oltean const unsigned char *addr) 1315e8a1e03SVladimir Oltean { 1325e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0); 1335e8a1e03SVladimir Oltean } 1345e8a1e03SVladimir Oltean 13591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 13691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 13791da11f8SLennert Buytenhek { 13891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 13991da11f8SLennert Buytenhek 1400d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 1419d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 14291da11f8SLennert Buytenhek 14391da11f8SLennert Buytenhek return 0xffff; 14491da11f8SLennert Buytenhek } 14591da11f8SLennert Buytenhek 14691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 14791da11f8SLennert Buytenhek { 14891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 14991da11f8SLennert Buytenhek 1500d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 1519d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 15291da11f8SLennert Buytenhek 15391da11f8SLennert Buytenhek return 0; 15491da11f8SLennert Buytenhek } 15591da11f8SLennert Buytenhek 15691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 15791da11f8SLennert Buytenhek { 15891da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 15991da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 16091da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 16191da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 1620b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 16349463b7fSVivien Didelot ds->dst->index, ds->index); 164c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 16524df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 16691da11f8SLennert Buytenhek } 16791da11f8SLennert Buytenhek 16891da11f8SLennert Buytenhek 16991da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 170abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 171c0840801SLennert Buytenhek { 172d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 173c0840801SLennert Buytenhek } 174c0840801SLennert Buytenhek 17591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 17691da11f8SLennert Buytenhek { 177d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 178d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 179499aa9e1SVladimir Oltean struct dsa_switch *ds = dp->ds; 180df02c6ffSLennert Buytenhek int err; 181df02c6ffSLennert Buytenhek 1829d5ef190SVladimir Oltean err = dev_open(master, NULL); 1839d5ef190SVladimir Oltean if (err < 0) { 1849d5ef190SVladimir Oltean netdev_err(dev, "failed to open master %s\n", master->name); 1859d5ef190SVladimir Oltean goto out; 1869d5ef190SVladimir Oltean } 187df02c6ffSLennert Buytenhek 188499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) { 189499aa9e1SVladimir Oltean err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0); 190499aa9e1SVladimir Oltean if (err) 191499aa9e1SVladimir Oltean goto out; 192499aa9e1SVladimir Oltean } 193499aa9e1SVladimir Oltean 1948feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 195a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 196df02c6ffSLennert Buytenhek if (err < 0) 197499aa9e1SVladimir Oltean goto del_host_addr; 198df02c6ffSLennert Buytenhek } 199df02c6ffSLennert Buytenhek 2008640f8dcSRussell King err = dsa_port_enable_rt(dp, dev->phydev); 201b2f2af21SFlorian Fainelli if (err) 20235aae5abSVladimir Oltean goto del_unicast; 203b73adef6SFlorian Fainelli 20491da11f8SLennert Buytenhek return 0; 205df02c6ffSLennert Buytenhek 206df02c6ffSLennert Buytenhek del_unicast: 2078feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 208a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 209499aa9e1SVladimir Oltean del_host_addr: 210499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 211499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 212df02c6ffSLennert Buytenhek out: 213df02c6ffSLennert Buytenhek return err; 21491da11f8SLennert Buytenhek } 21591da11f8SLennert Buytenhek 21691da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 21791da11f8SLennert Buytenhek { 218d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 219d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 220499aa9e1SVladimir Oltean struct dsa_switch *ds = dp->ds; 221df02c6ffSLennert Buytenhek 2228640f8dcSRussell King dsa_port_disable_rt(dp); 2236457edfeSVivien Didelot 2248feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 225a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 226df02c6ffSLennert Buytenhek 227499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 228499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 229499aa9e1SVladimir Oltean 23091da11f8SLennert Buytenhek return 0; 23191da11f8SLennert Buytenhek } 23291da11f8SLennert Buytenhek 2337569459aSVladimir Oltean /* Keep flooding enabled towards this port's CPU port as long as it serves at 2347569459aSVladimir Oltean * least one port in the tree that requires it. 2357569459aSVladimir Oltean */ 2367569459aSVladimir Oltean static void dsa_port_manage_cpu_flood(struct dsa_port *dp) 2377569459aSVladimir Oltean { 2387569459aSVladimir Oltean struct switchdev_brport_flags flags = { 2397569459aSVladimir Oltean .mask = BR_FLOOD | BR_MCAST_FLOOD, 2407569459aSVladimir Oltean }; 2417569459aSVladimir Oltean struct dsa_switch_tree *dst = dp->ds->dst; 2427569459aSVladimir Oltean struct dsa_port *cpu_dp = dp->cpu_dp; 2437569459aSVladimir Oltean struct dsa_port *other_dp; 2447569459aSVladimir Oltean int err; 2457569459aSVladimir Oltean 2467569459aSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 2477569459aSVladimir Oltean if (!dsa_port_is_user(other_dp)) 2487569459aSVladimir Oltean continue; 2497569459aSVladimir Oltean 2507569459aSVladimir Oltean if (other_dp->cpu_dp != cpu_dp) 2517569459aSVladimir Oltean continue; 2527569459aSVladimir Oltean 2537569459aSVladimir Oltean if (other_dp->slave->flags & IFF_ALLMULTI) 2547569459aSVladimir Oltean flags.val |= BR_MCAST_FLOOD; 2557569459aSVladimir Oltean if (other_dp->slave->flags & IFF_PROMISC) 2567569459aSVladimir Oltean flags.val |= BR_FLOOD; 2577569459aSVladimir Oltean } 2587569459aSVladimir Oltean 2597569459aSVladimir Oltean err = dsa_port_pre_bridge_flags(dp, flags, NULL); 2607569459aSVladimir Oltean if (err) 2617569459aSVladimir Oltean return; 2627569459aSVladimir Oltean 2637569459aSVladimir Oltean dsa_port_bridge_flags(cpu_dp, flags, NULL); 2647569459aSVladimir Oltean } 2657569459aSVladimir Oltean 26691da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 26791da11f8SLennert Buytenhek { 268d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 2697569459aSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2707569459aSVladimir Oltean struct dsa_switch *ds = dp->ds; 27135aae5abSVladimir Oltean 27291da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 27317ab4f61SRundong Ge dev_set_allmulti(master, 27417ab4f61SRundong Ge dev->flags & IFF_ALLMULTI ? 1 : -1); 27591da11f8SLennert Buytenhek if (change & IFF_PROMISC) 27617ab4f61SRundong Ge dev_set_promiscuity(master, 27717ab4f61SRundong Ge dev->flags & IFF_PROMISC ? 1 : -1); 2787569459aSVladimir Oltean 2797569459aSVladimir Oltean if (dsa_switch_supports_uc_filtering(ds) && 2807569459aSVladimir Oltean dsa_switch_supports_mc_filtering(ds)) 2817569459aSVladimir Oltean dsa_port_manage_cpu_flood(dp); 28217ab4f61SRundong Ge } 28391da11f8SLennert Buytenhek 28491da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 28591da11f8SLennert Buytenhek { 286d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 2875e8a1e03SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2885e8a1e03SVladimir Oltean struct dsa_switch *ds = dp->ds; 28991da11f8SLennert Buytenhek 29091da11f8SLennert Buytenhek dev_mc_sync(master, dev); 291a748ee24SJiri Pirko dev_uc_sync(master, dev); 2925e8a1e03SVladimir Oltean if (dsa_switch_supports_mc_filtering(ds)) 2935e8a1e03SVladimir Oltean __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc); 2945e8a1e03SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 2955e8a1e03SVladimir Oltean __dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc); 29691da11f8SLennert Buytenhek } 29791da11f8SLennert Buytenhek 298df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 29991da11f8SLennert Buytenhek { 300d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 301499aa9e1SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 302499aa9e1SVladimir Oltean struct dsa_switch *ds = dp->ds; 303df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 304df02c6ffSLennert Buytenhek int err; 305df02c6ffSLennert Buytenhek 306df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 307df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 308df02c6ffSLennert Buytenhek 309e2d0576fSVladimir Oltean /* If the port is down, the address isn't synced yet to hardware or 310e2d0576fSVladimir Oltean * to the DSA master, so there is nothing to change. 311e2d0576fSVladimir Oltean */ 312e2d0576fSVladimir Oltean if (!(dev->flags & IFF_UP)) 313e2d0576fSVladimir Oltean goto out_change_dev_addr; 314e2d0576fSVladimir Oltean 315499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) { 316499aa9e1SVladimir Oltean err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0); 317499aa9e1SVladimir Oltean if (err) 318499aa9e1SVladimir Oltean return err; 319499aa9e1SVladimir Oltean } 320499aa9e1SVladimir Oltean 3218feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 322a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 323df02c6ffSLennert Buytenhek if (err < 0) 324499aa9e1SVladimir Oltean goto del_unicast; 325df02c6ffSLennert Buytenhek } 326df02c6ffSLennert Buytenhek 3278feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 328a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 329df02c6ffSLennert Buytenhek 330499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 331499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 332499aa9e1SVladimir Oltean 333e2d0576fSVladimir Oltean out_change_dev_addr: 334e35b8d7dSJakub Kicinski eth_hw_addr_set(dev, addr->sa_data); 33591da11f8SLennert Buytenhek 33691da11f8SLennert Buytenhek return 0; 337499aa9e1SVladimir Oltean 338499aa9e1SVladimir Oltean del_unicast: 339499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 340499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0); 341499aa9e1SVladimir Oltean 342499aa9e1SVladimir Oltean return err; 34391da11f8SLennert Buytenhek } 34491da11f8SLennert Buytenhek 3452bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx { 3462bedde1aSArkadi Sharshevsky struct net_device *dev; 3472bedde1aSArkadi Sharshevsky struct sk_buff *skb; 3482bedde1aSArkadi Sharshevsky struct netlink_callback *cb; 3492bedde1aSArkadi Sharshevsky int idx; 3502bedde1aSArkadi Sharshevsky }; 3512bedde1aSArkadi Sharshevsky 3522bedde1aSArkadi Sharshevsky static int 3532bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 3542bedde1aSArkadi Sharshevsky bool is_static, void *data) 3552bedde1aSArkadi Sharshevsky { 3562bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx *dump = data; 3572bedde1aSArkadi Sharshevsky u32 portid = NETLINK_CB(dump->cb->skb).portid; 3582bedde1aSArkadi Sharshevsky u32 seq = dump->cb->nlh->nlmsg_seq; 3592bedde1aSArkadi Sharshevsky struct nlmsghdr *nlh; 3602bedde1aSArkadi Sharshevsky struct ndmsg *ndm; 3612bedde1aSArkadi Sharshevsky 3622bedde1aSArkadi Sharshevsky if (dump->idx < dump->cb->args[2]) 3632bedde1aSArkadi Sharshevsky goto skip; 3642bedde1aSArkadi Sharshevsky 3652bedde1aSArkadi Sharshevsky nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 3662bedde1aSArkadi Sharshevsky sizeof(*ndm), NLM_F_MULTI); 3672bedde1aSArkadi Sharshevsky if (!nlh) 3682bedde1aSArkadi Sharshevsky return -EMSGSIZE; 3692bedde1aSArkadi Sharshevsky 3702bedde1aSArkadi Sharshevsky ndm = nlmsg_data(nlh); 3712bedde1aSArkadi Sharshevsky ndm->ndm_family = AF_BRIDGE; 3722bedde1aSArkadi Sharshevsky ndm->ndm_pad1 = 0; 3732bedde1aSArkadi Sharshevsky ndm->ndm_pad2 = 0; 3742bedde1aSArkadi Sharshevsky ndm->ndm_flags = NTF_SELF; 3752bedde1aSArkadi Sharshevsky ndm->ndm_type = 0; 3762bedde1aSArkadi Sharshevsky ndm->ndm_ifindex = dump->dev->ifindex; 3772bedde1aSArkadi Sharshevsky ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 3782bedde1aSArkadi Sharshevsky 3792bedde1aSArkadi Sharshevsky if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 3802bedde1aSArkadi Sharshevsky goto nla_put_failure; 3812bedde1aSArkadi Sharshevsky 3822bedde1aSArkadi Sharshevsky if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 3832bedde1aSArkadi Sharshevsky goto nla_put_failure; 3842bedde1aSArkadi Sharshevsky 3852bedde1aSArkadi Sharshevsky nlmsg_end(dump->skb, nlh); 3862bedde1aSArkadi Sharshevsky 3872bedde1aSArkadi Sharshevsky skip: 3882bedde1aSArkadi Sharshevsky dump->idx++; 3892bedde1aSArkadi Sharshevsky return 0; 3902bedde1aSArkadi Sharshevsky 3912bedde1aSArkadi Sharshevsky nla_put_failure: 3922bedde1aSArkadi Sharshevsky nlmsg_cancel(dump->skb, nlh); 3932bedde1aSArkadi Sharshevsky return -EMSGSIZE; 3942bedde1aSArkadi Sharshevsky } 3952bedde1aSArkadi Sharshevsky 3962bedde1aSArkadi Sharshevsky static int 3972bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 3982bedde1aSArkadi Sharshevsky struct net_device *dev, struct net_device *filter_dev, 3992bedde1aSArkadi Sharshevsky int *idx) 4002bedde1aSArkadi Sharshevsky { 401d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 4022bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx dump = { 4032bedde1aSArkadi Sharshevsky .dev = dev, 4042bedde1aSArkadi Sharshevsky .skb = skb, 4052bedde1aSArkadi Sharshevsky .cb = cb, 4062bedde1aSArkadi Sharshevsky .idx = *idx, 4072bedde1aSArkadi Sharshevsky }; 4082bedde1aSArkadi Sharshevsky int err; 4092bedde1aSArkadi Sharshevsky 410de40fc5dSVivien Didelot err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 4112bedde1aSArkadi Sharshevsky *idx = dump.idx; 412de40fc5dSVivien Didelot 4132bedde1aSArkadi Sharshevsky return err; 4142bedde1aSArkadi Sharshevsky } 4152bedde1aSArkadi Sharshevsky 41691da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 41791da11f8SLennert Buytenhek { 4180336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 4190336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 4200336369dSBrandon Streiff int port = p->dp->index; 4210336369dSBrandon Streiff 4220336369dSBrandon Streiff /* Pass through to switch driver if it supports timestamping */ 4230336369dSBrandon Streiff switch (cmd) { 4240336369dSBrandon Streiff case SIOCGHWTSTAMP: 4250336369dSBrandon Streiff if (ds->ops->port_hwtstamp_get) 4260336369dSBrandon Streiff return ds->ops->port_hwtstamp_get(ds, port, ifr); 4270336369dSBrandon Streiff break; 4280336369dSBrandon Streiff case SIOCSHWTSTAMP: 4290336369dSBrandon Streiff if (ds->ops->port_hwtstamp_set) 4300336369dSBrandon Streiff return ds->ops->port_hwtstamp_set(ds, port, ifr); 4310336369dSBrandon Streiff break; 4320336369dSBrandon Streiff } 4330336369dSBrandon Streiff 434aab9c406SFlorian Fainelli return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 43591da11f8SLennert Buytenhek } 43691da11f8SLennert Buytenhek 43769bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx, 4384c08c586SVladimir Oltean const struct switchdev_attr *attr, 4394c08c586SVladimir Oltean struct netlink_ext_ack *extack) 44035636062SScott Feldman { 441d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 442b8d866acSVivien Didelot int ret; 44335636062SScott Feldman 4440d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 4450d2cfbd4SVladimir Oltean return 0; 4460d2cfbd4SVladimir Oltean 44735636062SScott Feldman switch (attr->id) { 4481f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 44903cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 45003cbb870SVladimir Oltean return -EOPNOTSUPP; 45103cbb870SVladimir Oltean 45239f32101SVladimir Oltean ret = dsa_port_set_state(dp, attr->u.stp_state, true); 45335636062SScott Feldman break; 454fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 455936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 45603cbb870SVladimir Oltean return -EOPNOTSUPP; 45703cbb870SVladimir Oltean 45889153ed6SVladimir Oltean ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 45989153ed6SVladimir Oltean extack); 460fb2dabadSVivien Didelot break; 46134a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 462936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 46303cbb870SVladimir Oltean return -EOPNOTSUPP; 46403cbb870SVladimir Oltean 465bae33f2bSVladimir Oltean ret = dsa_port_ageing_time(dp, attr->u.ageing_time); 46634a79f63SVivien Didelot break; 467*332afc4cSTobias Waldekranz case SWITCHDEV_ATTR_ID_BRIDGE_MST: 468*332afc4cSTobias Waldekranz if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 469*332afc4cSTobias Waldekranz return -EOPNOTSUPP; 470*332afc4cSTobias Waldekranz 471*332afc4cSTobias Waldekranz ret = dsa_port_mst_enable(dp, attr->u.mst, extack); 472*332afc4cSTobias Waldekranz break; 473ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 47403cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 47503cbb870SVladimir Oltean return -EOPNOTSUPP; 47603cbb870SVladimir Oltean 477a8b659e7SVladimir Oltean ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 478a8b659e7SVladimir Oltean extack); 479ea87005aSFlorian Fainelli break; 48057652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 48103cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 48203cbb870SVladimir Oltean return -EOPNOTSUPP; 48303cbb870SVladimir Oltean 484a8b659e7SVladimir Oltean ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack); 48557652796SRussell King break; 48635636062SScott Feldman default: 48735636062SScott Feldman ret = -EOPNOTSUPP; 48835636062SScott Feldman break; 48935636062SScott Feldman } 49035636062SScott Feldman 49135636062SScott Feldman return ret; 49235636062SScott Feldman } 49335636062SScott Feldman 4941ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */ 4951ce39f0eSVladimir Oltean static int 4961ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 4971ce39f0eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 4981ce39f0eSVladimir Oltean { 4991ce39f0eSVladimir Oltean struct net_device *upper_dev; 5001ce39f0eSVladimir Oltean struct list_head *iter; 5011ce39f0eSVladimir Oltean 5021ce39f0eSVladimir Oltean netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 5031ce39f0eSVladimir Oltean u16 vid; 5041ce39f0eSVladimir Oltean 5051ce39f0eSVladimir Oltean if (!is_vlan_dev(upper_dev)) 5061ce39f0eSVladimir Oltean continue; 5071ce39f0eSVladimir Oltean 5081ce39f0eSVladimir Oltean vid = vlan_dev_vlan_id(upper_dev); 509b7a9e0daSVladimir Oltean if (vid == vlan->vid) 5101ce39f0eSVladimir Oltean return -EBUSY; 5111ce39f0eSVladimir Oltean } 5121ce39f0eSVladimir Oltean 5131ce39f0eSVladimir Oltean return 0; 5141ce39f0eSVladimir Oltean } 5151ce39f0eSVladimir Oltean 516bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev, 5170ee2af4eSVladimir Oltean const struct switchdev_obj *obj, 5180ee2af4eSVladimir Oltean struct netlink_ext_ack *extack) 519bdcff080SVivien Didelot { 520bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 521134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 522b7a9e0daSVladimir Oltean int err; 523bdcff080SVivien Didelot 5240ee2af4eSVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 5250ee2af4eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 526c5335d73SVivien Didelot return 0; 5270ee2af4eSVladimir Oltean } 528c5335d73SVivien Didelot 529134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 530bdcff080SVivien Didelot 5311ce39f0eSVladimir Oltean /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 5321ce39f0eSVladimir Oltean * the same VID. 5331ce39f0eSVladimir Oltean */ 53436cbf39bSVladimir Oltean if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) { 5351ce39f0eSVladimir Oltean rcu_read_lock(); 536134ef238SVladimir Oltean err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan); 5371ce39f0eSVladimir Oltean rcu_read_unlock(); 53831046a5fSVladimir Oltean if (err) { 53931046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 54031046a5fSVladimir Oltean "Port already has a VLAN upper with this VID"); 5411ce39f0eSVladimir Oltean return err; 5421ce39f0eSVladimir Oltean } 54331046a5fSVladimir Oltean } 5441ce39f0eSVladimir Oltean 545134ef238SVladimir Oltean return dsa_port_vlan_add(dp, vlan, extack); 546134ef238SVladimir Oltean } 547bdcff080SVivien Didelot 548164f861bSVladimir Oltean /* Offload a VLAN installed on the bridge or on a foreign interface by 549164f861bSVladimir Oltean * installing it as a VLAN towards the CPU port. 550164f861bSVladimir Oltean */ 551134ef238SVladimir Oltean static int dsa_slave_host_vlan_add(struct net_device *dev, 552134ef238SVladimir Oltean const struct switchdev_obj *obj, 553134ef238SVladimir Oltean struct netlink_ext_ack *extack) 554134ef238SVladimir Oltean { 555134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 556134ef238SVladimir Oltean struct switchdev_obj_port_vlan vlan; 557134ef238SVladimir Oltean 558164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 559164f861bSVladimir Oltean if (!dp->bridge) 560164f861bSVladimir Oltean return -EOPNOTSUPP; 561164f861bSVladimir Oltean 562134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 563134ef238SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 564134ef238SVladimir Oltean return 0; 565134ef238SVladimir Oltean } 566134ef238SVladimir Oltean 567134ef238SVladimir Oltean vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 568134ef238SVladimir Oltean 569134ef238SVladimir Oltean /* Even though drivers often handle CPU membership in special ways, 570b9499904SVivien Didelot * it doesn't make sense to program a PVID, so clear this flag. 571b9499904SVivien Didelot */ 572b9499904SVivien Didelot vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 573b9499904SVivien Didelot 574134ef238SVladimir Oltean return dsa_port_host_vlan_add(dp, &vlan, extack); 575bdcff080SVivien Didelot } 576bdcff080SVivien Didelot 57769bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx, 578648b4a99SJiri Pirko const struct switchdev_obj *obj, 57979b139f4SVivien Didelot struct netlink_ext_ack *extack) 580ba14d9ebSVivien Didelot { 581d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 582ba14d9ebSVivien Didelot int err; 583ba14d9ebSVivien Didelot 5840d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 5850d2cfbd4SVladimir Oltean return 0; 5860d2cfbd4SVladimir Oltean 5879e8f4a54SJiri Pirko switch (obj->id) { 5888df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 58903cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 59079b139f4SVivien Didelot return -EOPNOTSUPP; 59103cbb870SVladimir Oltean 592ffb68fc5SVladimir Oltean err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 5938df30255SVivien Didelot break; 5945f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 595936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 59603cbb870SVladimir Oltean return -EOPNOTSUPP; 59703cbb870SVladimir Oltean 59868d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 5995f4dbc50SAndrew Lunn break; 60057d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 601164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 6020ee2af4eSVladimir Oltean err = dsa_slave_vlan_add(dev, obj, extack); 603164f861bSVladimir Oltean else 604164f861bSVladimir Oltean err = dsa_slave_host_vlan_add(dev, obj, extack); 60511149536SVivien Didelot break; 606c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 607936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 608c595c433SHoratiu Vultur return -EOPNOTSUPP; 60903cbb870SVladimir Oltean 610c595c433SHoratiu Vultur err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj)); 611c595c433SHoratiu Vultur break; 612c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 613936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 614c595c433SHoratiu Vultur return -EOPNOTSUPP; 61503cbb870SVladimir Oltean 616c595c433SHoratiu Vultur err = dsa_port_mrp_add_ring_role(dp, 617c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 618c595c433SHoratiu Vultur break; 619ba14d9ebSVivien Didelot default: 620ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 621ba14d9ebSVivien Didelot break; 622ba14d9ebSVivien Didelot } 623ba14d9ebSVivien Didelot 624ba14d9ebSVivien Didelot return err; 625ba14d9ebSVivien Didelot } 626ba14d9ebSVivien Didelot 627bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev, 628bdcff080SVivien Didelot const struct switchdev_obj *obj) 629bdcff080SVivien Didelot { 630bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 6312209158cSVladimir Oltean struct switchdev_obj_port_vlan *vlan; 632bdcff080SVivien Didelot 63354a0ed0dSRussell King if (dsa_port_skip_vlan_configuration(dp)) 634c5335d73SVivien Didelot return 0; 635c5335d73SVivien Didelot 6362209158cSVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 6372209158cSVladimir Oltean 638134ef238SVladimir Oltean return dsa_port_vlan_del(dp, vlan); 639134ef238SVladimir Oltean } 6402209158cSVladimir Oltean 641134ef238SVladimir Oltean static int dsa_slave_host_vlan_del(struct net_device *dev, 642134ef238SVladimir Oltean const struct switchdev_obj *obj) 643134ef238SVladimir Oltean { 644134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 645134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 6462209158cSVladimir Oltean 647164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 648164f861bSVladimir Oltean if (!dp->bridge) 649164f861bSVladimir Oltean return -EOPNOTSUPP; 650164f861bSVladimir Oltean 651134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) 6522209158cSVladimir Oltean return 0; 653134ef238SVladimir Oltean 654134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 655134ef238SVladimir Oltean 656134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, vlan); 657bdcff080SVivien Didelot } 658bdcff080SVivien Didelot 65969bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx, 660648b4a99SJiri Pirko const struct switchdev_obj *obj) 661ba14d9ebSVivien Didelot { 662d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 663ba14d9ebSVivien Didelot int err; 664ba14d9ebSVivien Didelot 6650d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 6660d2cfbd4SVladimir Oltean return 0; 6670d2cfbd4SVladimir Oltean 6689e8f4a54SJiri Pirko switch (obj->id) { 6698df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 67003cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 67179b139f4SVivien Didelot return -EOPNOTSUPP; 67203cbb870SVladimir Oltean 673bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6748df30255SVivien Didelot break; 6755f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 676936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 67703cbb870SVladimir Oltean return -EOPNOTSUPP; 67803cbb870SVladimir Oltean 67968d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6805f4dbc50SAndrew Lunn break; 68157d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 682164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 683bdcff080SVivien Didelot err = dsa_slave_vlan_del(dev, obj); 684164f861bSVladimir Oltean else 685164f861bSVladimir Oltean err = dsa_slave_host_vlan_del(dev, obj); 68611149536SVivien Didelot break; 687c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 688936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 689c595c433SHoratiu Vultur return -EOPNOTSUPP; 69003cbb870SVladimir Oltean 691c595c433SHoratiu Vultur err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj)); 692c595c433SHoratiu Vultur break; 693c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 694936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 695c595c433SHoratiu Vultur return -EOPNOTSUPP; 69603cbb870SVladimir Oltean 697c595c433SHoratiu Vultur err = dsa_port_mrp_del_ring_role(dp, 698c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 699c595c433SHoratiu Vultur break; 700ba14d9ebSVivien Didelot default: 701ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 702ba14d9ebSVivien Didelot break; 703ba14d9ebSVivien Didelot } 704ba14d9ebSVivien Didelot 705ba14d9ebSVivien Didelot return err; 706ba14d9ebSVivien Didelot } 707ba14d9ebSVivien Didelot 7084fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 70904ff53f9SFlorian Fainelli struct sk_buff *skb) 71004ff53f9SFlorian Fainelli { 71104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 7124fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 7134fa7b718SVivien Didelot 714f78ed220SEric Dumazet return netpoll_send_skb(p->netpoll, skb); 71504ff53f9SFlorian Fainelli #else 71604ff53f9SFlorian Fainelli BUG(); 71704ff53f9SFlorian Fainelli return NETDEV_TX_OK; 718f78ed220SEric Dumazet #endif 71904ff53f9SFlorian Fainelli } 72004ff53f9SFlorian Fainelli 72190af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 72290af1059SBrandon Streiff struct sk_buff *skb) 72390af1059SBrandon Streiff { 72490af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 72590af1059SBrandon Streiff 726cfd12c06SYangbo Lu if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 727cfd12c06SYangbo Lu return; 728cfd12c06SYangbo Lu 72990af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 73090af1059SBrandon Streiff return; 73190af1059SBrandon Streiff 7325c5416f5SYangbo Lu ds->ops->port_txtstamp(ds, p->dp->index, skb); 73390af1059SBrandon Streiff } 73490af1059SBrandon Streiff 73597a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 73697a69a0dSVladimir Oltean { 73797a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 73897a69a0dSVladimir Oltean * tag to be successfully transmitted 73997a69a0dSVladimir Oltean */ 74097a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 74197a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 74297a69a0dSVladimir Oltean 74397a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 74497a69a0dSVladimir Oltean * do not modify its EtherType 74597a69a0dSVladimir Oltean */ 74697a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 74797a69a0dSVladimir Oltean dev_queue_xmit(skb); 74897a69a0dSVladimir Oltean 74997a69a0dSVladimir Oltean return NETDEV_TX_OK; 75097a69a0dSVladimir Oltean } 75197a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 75297a69a0dSVladimir Oltean 753a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 754a3b0b647SVladimir Oltean { 755a3b0b647SVladimir Oltean int needed_headroom = dev->needed_headroom; 756a3b0b647SVladimir Oltean int needed_tailroom = dev->needed_tailroom; 757a3b0b647SVladimir Oltean 758a3b0b647SVladimir Oltean /* For tail taggers, we need to pad short frames ourselves, to ensure 759a3b0b647SVladimir Oltean * that the tail tag does not fail at its role of being at the end of 760a3b0b647SVladimir Oltean * the packet, once the master interface pads the frame. Account for 761a3b0b647SVladimir Oltean * that pad length here, and pad later. 762a3b0b647SVladimir Oltean */ 763a3b0b647SVladimir Oltean if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 764a3b0b647SVladimir Oltean needed_tailroom += ETH_ZLEN - skb->len; 765a3b0b647SVladimir Oltean /* skb_headroom() returns unsigned int... */ 766a3b0b647SVladimir Oltean needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 767a3b0b647SVladimir Oltean needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 768a3b0b647SVladimir Oltean 769a3b0b647SVladimir Oltean if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 770a3b0b647SVladimir Oltean /* No reallocation needed, yay! */ 771a3b0b647SVladimir Oltean return 0; 772a3b0b647SVladimir Oltean 773a3b0b647SVladimir Oltean return pskb_expand_head(skb, needed_headroom, needed_tailroom, 774a3b0b647SVladimir Oltean GFP_ATOMIC); 775a3b0b647SVladimir Oltean } 776a3b0b647SVladimir Oltean 7773e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 7783e8a72d1SFlorian Fainelli { 7793e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 7804ed70ce9SFlorian Fainelli struct sk_buff *nskb; 7813e8a72d1SFlorian Fainelli 7826a900628SHeiner Kallweit dev_sw_netstats_tx_add(dev, 1, skb->len); 7833e8a72d1SFlorian Fainelli 784c4b364ceSYangbo Lu memset(skb->cb, 0, sizeof(skb->cb)); 78587671375SVladimir Oltean 786cf536ea3SYangbo Lu /* Handle tx timestamp if any */ 78790af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 78890af1059SBrandon Streiff 789a3b0b647SVladimir Oltean if (dsa_realloc_skb(skb, dev)) { 790a3b0b647SVladimir Oltean dev_kfree_skb_any(skb); 791a3b0b647SVladimir Oltean return NETDEV_TX_OK; 792a3b0b647SVladimir Oltean } 793a3b0b647SVladimir Oltean 794a3b0b647SVladimir Oltean /* needed_tailroom should still be 'warm' in the cache line from 795a3b0b647SVladimir Oltean * dsa_realloc_skb(), which has also ensured that padding is safe. 796a3b0b647SVladimir Oltean */ 797a3b0b647SVladimir Oltean if (dev->needed_tailroom) 798a3b0b647SVladimir Oltean eth_skb_pad(skb); 799a3b0b647SVladimir Oltean 800fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 801fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 802fe47d563SVivien Didelot */ 8034ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 804fe47d563SVivien Didelot if (!nskb) { 805fe47d563SVivien Didelot kfree_skb(skb); 8064ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 807fe47d563SVivien Didelot } 8085aed85ceSFlorian Fainelli 80997a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 81097a69a0dSVladimir Oltean } 81104ff53f9SFlorian Fainelli 81291da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 81391da11f8SLennert Buytenhek 81491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 81591da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 81691da11f8SLennert Buytenhek { 8177826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 8187826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 8197826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 82091da11f8SLennert Buytenhek } 82191da11f8SLennert Buytenhek 8223d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 8233d762a0fSGuenter Roeck { 824d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 825d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8263d762a0fSGuenter Roeck 8279d490b4eSVivien Didelot if (ds->ops->get_regs_len) 828d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 8293d762a0fSGuenter Roeck 8303d762a0fSGuenter Roeck return -EOPNOTSUPP; 8313d762a0fSGuenter Roeck } 8323d762a0fSGuenter Roeck 8333d762a0fSGuenter Roeck static void 8343d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 8353d762a0fSGuenter Roeck { 836d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 837d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8383d762a0fSGuenter Roeck 8399d490b4eSVivien Didelot if (ds->ops->get_regs) 840d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 8413d762a0fSGuenter Roeck } 8423d762a0fSGuenter Roeck 843aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 844aab9c406SFlorian Fainelli { 845aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 846aab9c406SFlorian Fainelli 847aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 848aab9c406SFlorian Fainelli } 849aab9c406SFlorian Fainelli 8506793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 8516793abb4SGuenter Roeck { 852d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 853d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8546793abb4SGuenter Roeck 8550e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 856ff04955cSAndrew Lunn return ds->cd->eeprom_len; 8576793abb4SGuenter Roeck 8589d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 8599d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 8606793abb4SGuenter Roeck 8616793abb4SGuenter Roeck return 0; 8626793abb4SGuenter Roeck } 8636793abb4SGuenter Roeck 8646793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 8656793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8666793abb4SGuenter Roeck { 867d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 868d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8696793abb4SGuenter Roeck 8709d490b4eSVivien Didelot if (ds->ops->get_eeprom) 8719d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 8726793abb4SGuenter Roeck 8736793abb4SGuenter Roeck return -EOPNOTSUPP; 8746793abb4SGuenter Roeck } 8756793abb4SGuenter Roeck 8766793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 8776793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8786793abb4SGuenter Roeck { 879d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 880d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8816793abb4SGuenter Roeck 8829d490b4eSVivien Didelot if (ds->ops->set_eeprom) 8839d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 8846793abb4SGuenter Roeck 8856793abb4SGuenter Roeck return -EOPNOTSUPP; 8866793abb4SGuenter Roeck } 8876793abb4SGuenter Roeck 88891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 88991da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 89091da11f8SLennert Buytenhek { 891d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 892d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 89391da11f8SLennert Buytenhek 89491da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 89591da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 89691da11f8SLennert Buytenhek 89791da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 89891da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 89991da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 90091da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 9019d490b4eSVivien Didelot if (ds->ops->get_strings) 90289f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 90389f09048SFlorian Fainelli data + 4 * len); 904a71acad9SOleksij Rempel } else if (stringset == ETH_SS_TEST) { 905a71acad9SOleksij Rempel net_selftest_get_strings(data); 90691da11f8SLennert Buytenhek } 907a71acad9SOleksij Rempel 90891da11f8SLennert Buytenhek } 90991da11f8SLennert Buytenhek 91091da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 91191da11f8SLennert Buytenhek struct ethtool_stats *stats, 91291da11f8SLennert Buytenhek uint64_t *data) 91391da11f8SLennert Buytenhek { 914d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 915d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 9165f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 917f613ed66SFlorian Fainelli unsigned int start; 9185f6b4e14SFlorian Fainelli int i; 91991da11f8SLennert Buytenhek 9205f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 9215f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 9225f6b4e14SFlorian Fainelli 9236a900628SHeiner Kallweit s = per_cpu_ptr(dev->tstats, i); 924f613ed66SFlorian Fainelli do { 9255f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 9265f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 9275f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 9285f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 9295f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 9305f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 9315f6b4e14SFlorian Fainelli data[0] += tx_packets; 9325f6b4e14SFlorian Fainelli data[1] += tx_bytes; 9335f6b4e14SFlorian Fainelli data[2] += rx_packets; 9345f6b4e14SFlorian Fainelli data[3] += rx_bytes; 9355f6b4e14SFlorian Fainelli } 9369d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 937d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 93891da11f8SLennert Buytenhek } 93991da11f8SLennert Buytenhek 94091da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 94191da11f8SLennert Buytenhek { 942d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 943d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 94491da11f8SLennert Buytenhek 94591da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 946b94cbc90SVladimir Oltean int count = 0; 94791da11f8SLennert Buytenhek 948b94cbc90SVladimir Oltean if (ds->ops->get_sset_count) { 949b94cbc90SVladimir Oltean count = ds->ops->get_sset_count(ds, dp->index, sset); 950b94cbc90SVladimir Oltean if (count < 0) 95191da11f8SLennert Buytenhek return count; 952b94cbc90SVladimir Oltean } 953b94cbc90SVladimir Oltean 954b94cbc90SVladimir Oltean return count + 4; 955a71acad9SOleksij Rempel } else if (sset == ETH_SS_TEST) { 956a71acad9SOleksij Rempel return net_selftest_get_count(); 95791da11f8SLennert Buytenhek } 95891da11f8SLennert Buytenhek 95991da11f8SLennert Buytenhek return -EOPNOTSUPP; 96091da11f8SLennert Buytenhek } 96191da11f8SLennert Buytenhek 962487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev, 963487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats) 964487d3855SAlvin Šipraga { 965487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 966487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 967487d3855SAlvin Šipraga 968487d3855SAlvin Šipraga if (ds->ops->get_eth_phy_stats) 969487d3855SAlvin Šipraga ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats); 970487d3855SAlvin Šipraga } 971487d3855SAlvin Šipraga 972487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev, 973487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats) 974487d3855SAlvin Šipraga { 975487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 976487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 977487d3855SAlvin Šipraga 978487d3855SAlvin Šipraga if (ds->ops->get_eth_mac_stats) 979487d3855SAlvin Šipraga ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats); 980487d3855SAlvin Šipraga } 981487d3855SAlvin Šipraga 982487d3855SAlvin Šipraga static void 983487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev, 984487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats) 985487d3855SAlvin Šipraga { 986487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 987487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 988487d3855SAlvin Šipraga 989487d3855SAlvin Šipraga if (ds->ops->get_eth_ctrl_stats) 990487d3855SAlvin Šipraga ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats); 991487d3855SAlvin Šipraga } 992487d3855SAlvin Šipraga 993a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev, 994a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *buf) 995a71acad9SOleksij Rempel { 996a71acad9SOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(ndev); 997a71acad9SOleksij Rempel struct dsa_switch *ds = dp->ds; 998a71acad9SOleksij Rempel 999a71acad9SOleksij Rempel if (ds->ops->self_test) { 1000a71acad9SOleksij Rempel ds->ops->self_test(ds, dp->index, etest, buf); 1001a71acad9SOleksij Rempel return; 1002a71acad9SOleksij Rempel } 1003a71acad9SOleksij Rempel 1004a71acad9SOleksij Rempel net_selftest(ndev, etest, buf); 1005a71acad9SOleksij Rempel } 1006a71acad9SOleksij Rempel 100719e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 100819e57c4eSFlorian Fainelli { 1009d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1010d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 101119e57c4eSFlorian Fainelli 1012aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 1013aab9c406SFlorian Fainelli 10149d490b4eSVivien Didelot if (ds->ops->get_wol) 1015d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 101619e57c4eSFlorian Fainelli } 101719e57c4eSFlorian Fainelli 101819e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 101919e57c4eSFlorian Fainelli { 1020d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1021d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 102219e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 102319e57c4eSFlorian Fainelli 1024aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 1025aab9c406SFlorian Fainelli 10269d490b4eSVivien Didelot if (ds->ops->set_wol) 1027d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 102819e57c4eSFlorian Fainelli 102919e57c4eSFlorian Fainelli return ret; 103019e57c4eSFlorian Fainelli } 103119e57c4eSFlorian Fainelli 10327905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 10337905288fSFlorian Fainelli { 1034d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1035d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10367905288fSFlorian Fainelli int ret; 10377905288fSFlorian Fainelli 10387b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 103900670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10407b9cc738SVivien Didelot return -ENODEV; 10417b9cc738SVivien Didelot 104208f50061SVivien Didelot if (!ds->ops->set_mac_eee) 10437905288fSFlorian Fainelli return -EOPNOTSUPP; 10447905288fSFlorian Fainelli 1045d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 10467905288fSFlorian Fainelli if (ret) 10477905288fSFlorian Fainelli return ret; 10487905288fSFlorian Fainelli 1049aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 10507905288fSFlorian Fainelli } 10517905288fSFlorian Fainelli 10527905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 10537905288fSFlorian Fainelli { 1054d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1055d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10567905288fSFlorian Fainelli int ret; 10577905288fSFlorian Fainelli 10587b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 105900670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10607b9cc738SVivien Didelot return -ENODEV; 10617b9cc738SVivien Didelot 106208f50061SVivien Didelot if (!ds->ops->get_mac_eee) 10637905288fSFlorian Fainelli return -EOPNOTSUPP; 10647905288fSFlorian Fainelli 1065d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 10667905288fSFlorian Fainelli if (ret) 10677905288fSFlorian Fainelli return ret; 10687905288fSFlorian Fainelli 1069aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 1070aab9c406SFlorian Fainelli } 1071aab9c406SFlorian Fainelli 1072aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 1073aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 1074aab9c406SFlorian Fainelli { 1075aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1076aab9c406SFlorian Fainelli 1077aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 1078aab9c406SFlorian Fainelli } 1079aab9c406SFlorian Fainelli 1080aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 1081aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 1082aab9c406SFlorian Fainelli { 1083aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1084aab9c406SFlorian Fainelli 1085aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 10867905288fSFlorian Fainelli } 10877905288fSFlorian Fainelli 1088a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev, 1089a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1090a2a1a13bSHeiner Kallweit { 1091a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1092a2a1a13bSHeiner Kallweit 1093a2a1a13bSHeiner Kallweit phylink_ethtool_get_pauseparam(dp->pl, pause); 1094a2a1a13bSHeiner Kallweit } 1095a2a1a13bSHeiner Kallweit 1096a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev, 1097a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1098a2a1a13bSHeiner Kallweit { 1099a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1100a2a1a13bSHeiner Kallweit 1101a2a1a13bSHeiner Kallweit return phylink_ethtool_set_pauseparam(dp->pl, pause); 1102a2a1a13bSHeiner Kallweit } 1103a2a1a13bSHeiner Kallweit 110404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 110504ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 110604ff53f9SFlorian Fainelli struct netpoll_info *ni) 110704ff53f9SFlorian Fainelli { 1108d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 110904ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 111004ff53f9SFlorian Fainelli struct netpoll *netpoll; 111104ff53f9SFlorian Fainelli int err = 0; 111204ff53f9SFlorian Fainelli 111304ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 111404ff53f9SFlorian Fainelli if (!netpoll) 111504ff53f9SFlorian Fainelli return -ENOMEM; 111604ff53f9SFlorian Fainelli 111704ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 111804ff53f9SFlorian Fainelli if (err) { 111904ff53f9SFlorian Fainelli kfree(netpoll); 112004ff53f9SFlorian Fainelli goto out; 112104ff53f9SFlorian Fainelli } 112204ff53f9SFlorian Fainelli 112304ff53f9SFlorian Fainelli p->netpoll = netpoll; 112404ff53f9SFlorian Fainelli out: 112504ff53f9SFlorian Fainelli return err; 112604ff53f9SFlorian Fainelli } 112704ff53f9SFlorian Fainelli 112804ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 112904ff53f9SFlorian Fainelli { 113004ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 113104ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 113204ff53f9SFlorian Fainelli 113304ff53f9SFlorian Fainelli if (!netpoll) 113404ff53f9SFlorian Fainelli return; 113504ff53f9SFlorian Fainelli 113604ff53f9SFlorian Fainelli p->netpoll = NULL; 113704ff53f9SFlorian Fainelli 1138c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 113904ff53f9SFlorian Fainelli } 114004ff53f9SFlorian Fainelli 114104ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 114204ff53f9SFlorian Fainelli { 114304ff53f9SFlorian Fainelli } 114404ff53f9SFlorian Fainelli #endif 114504ff53f9SFlorian Fainelli 1146f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 11474fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 1148f50f2127SFlorian Fainelli { 11494fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 1150f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1151f50f2127SFlorian Fainelli 1152f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 1153f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 1154f50f2127SFlorian Fainelli return mall_tc_entry; 1155f50f2127SFlorian Fainelli 1156f50f2127SFlorian Fainelli return NULL; 1157f50f2127SFlorian Fainelli } 1158f50f2127SFlorian Fainelli 1159e13c2075SVladimir Oltean static int 1160e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 1161f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 1162f50f2127SFlorian Fainelli bool ingress) 1163f50f2127SFlorian Fainelli { 1164d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1165f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1166e13c2075SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror; 1167f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1168d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 11699681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 1170d945097bSVivien Didelot struct dsa_port *to_dp; 1171e13c2075SVladimir Oltean int err; 1172e13c2075SVladimir Oltean 1173f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 117434297176SVladimir Oltean return -EOPNOTSUPP; 1175f50f2127SFlorian Fainelli 117653eca1f3SJakub Kicinski if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1177319a1d19SJiri Pirko cls->common.extack)) 117834297176SVladimir Oltean return -EOPNOTSUPP; 1179319a1d19SJiri Pirko 11809681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 1181f50f2127SFlorian Fainelli 118265722159SVladimir Oltean if (!act->dev) 118365722159SVladimir Oltean return -EINVAL; 118465722159SVladimir Oltean 11859681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 1186f50f2127SFlorian Fainelli return -EOPNOTSUPP; 1187f50f2127SFlorian Fainelli 1188f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1189f50f2127SFlorian Fainelli if (!mall_tc_entry) 1190f50f2127SFlorian Fainelli return -ENOMEM; 1191f50f2127SFlorian Fainelli 1192f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 1193f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1194f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 1195f50f2127SFlorian Fainelli 11969681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 1197f50f2127SFlorian Fainelli 1198d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 1199f50f2127SFlorian Fainelli mirror->ingress = ingress; 1200f50f2127SFlorian Fainelli 1201d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 1202f50f2127SFlorian Fainelli if (err) { 1203f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1204f50f2127SFlorian Fainelli return err; 1205f50f2127SFlorian Fainelli } 1206f50f2127SFlorian Fainelli 1207f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1208e13c2075SVladimir Oltean 1209e13c2075SVladimir Oltean return err; 1210f50f2127SFlorian Fainelli } 1211f50f2127SFlorian Fainelli 121234297176SVladimir Oltean static int 121334297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev, 121434297176SVladimir Oltean struct tc_cls_matchall_offload *cls, 121534297176SVladimir Oltean bool ingress) 121634297176SVladimir Oltean { 121734297176SVladimir Oltean struct netlink_ext_ack *extack = cls->common.extack; 121834297176SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 121934297176SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 122034297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer; 122134297176SVladimir Oltean struct dsa_mall_tc_entry *mall_tc_entry; 122234297176SVladimir Oltean struct dsa_switch *ds = dp->ds; 122334297176SVladimir Oltean struct flow_action_entry *act; 122434297176SVladimir Oltean int err; 122534297176SVladimir Oltean 122634297176SVladimir Oltean if (!ds->ops->port_policer_add) { 122734297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1228c75a33c8SJacob Keller "Policing offload not implemented"); 122934297176SVladimir Oltean return -EOPNOTSUPP; 123034297176SVladimir Oltean } 123134297176SVladimir Oltean 123234297176SVladimir Oltean if (!ingress) { 123334297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1234c75a33c8SJacob Keller "Only supported on ingress qdisc"); 123534297176SVladimir Oltean return -EOPNOTSUPP; 123634297176SVladimir Oltean } 123734297176SVladimir Oltean 123834297176SVladimir Oltean if (!flow_action_basic_hw_stats_check(&cls->rule->action, 123934297176SVladimir Oltean cls->common.extack)) 124034297176SVladimir Oltean return -EOPNOTSUPP; 124134297176SVladimir Oltean 124234297176SVladimir Oltean list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 124334297176SVladimir Oltean if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 124434297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1245c75a33c8SJacob Keller "Only one port policer allowed"); 124634297176SVladimir Oltean return -EEXIST; 124734297176SVladimir Oltean } 124834297176SVladimir Oltean } 124934297176SVladimir Oltean 125034297176SVladimir Oltean act = &cls->rule->action.entries[0]; 125134297176SVladimir Oltean 125234297176SVladimir Oltean mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 125334297176SVladimir Oltean if (!mall_tc_entry) 125434297176SVladimir Oltean return -ENOMEM; 125534297176SVladimir Oltean 125634297176SVladimir Oltean mall_tc_entry->cookie = cls->cookie; 125734297176SVladimir Oltean mall_tc_entry->type = DSA_PORT_MALL_POLICER; 125834297176SVladimir Oltean policer = &mall_tc_entry->policer; 125934297176SVladimir Oltean policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 126034297176SVladimir Oltean policer->burst = act->police.burst; 126134297176SVladimir Oltean 126234297176SVladimir Oltean err = ds->ops->port_policer_add(ds, dp->index, policer); 126334297176SVladimir Oltean if (err) { 126434297176SVladimir Oltean kfree(mall_tc_entry); 126534297176SVladimir Oltean return err; 126634297176SVladimir Oltean } 126734297176SVladimir Oltean 126834297176SVladimir Oltean list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 126934297176SVladimir Oltean 127034297176SVladimir Oltean return err; 127134297176SVladimir Oltean } 127234297176SVladimir Oltean 1273e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev, 1274e13c2075SVladimir Oltean struct tc_cls_matchall_offload *cls, 1275e13c2075SVladimir Oltean bool ingress) 1276e13c2075SVladimir Oltean { 1277e13c2075SVladimir Oltean int err = -EOPNOTSUPP; 1278e13c2075SVladimir Oltean 1279e13c2075SVladimir Oltean if (cls->common.protocol == htons(ETH_P_ALL) && 1280e13c2075SVladimir Oltean flow_offload_has_one_action(&cls->rule->action) && 1281e13c2075SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1282e13c2075SVladimir Oltean err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 128334297176SVladimir Oltean else if (flow_offload_has_one_action(&cls->rule->action) && 128434297176SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 128534297176SVladimir Oltean err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1286e13c2075SVladimir Oltean 1287e13c2075SVladimir Oltean return err; 1288f50f2127SFlorian Fainelli } 1289f50f2127SFlorian Fainelli 1290f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 1291f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 1292f50f2127SFlorian Fainelli { 1293d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1294f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1295d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1296f50f2127SFlorian Fainelli 12974fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1298f50f2127SFlorian Fainelli if (!mall_tc_entry) 1299f50f2127SFlorian Fainelli return; 1300f50f2127SFlorian Fainelli 1301f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 1302f50f2127SFlorian Fainelli 1303f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 1304f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 130534297176SVladimir Oltean if (ds->ops->port_mirror_del) 130634297176SVladimir Oltean ds->ops->port_mirror_del(ds, dp->index, 130734297176SVladimir Oltean &mall_tc_entry->mirror); 130834297176SVladimir Oltean break; 130934297176SVladimir Oltean case DSA_PORT_MALL_POLICER: 131034297176SVladimir Oltean if (ds->ops->port_policer_del) 131134297176SVladimir Oltean ds->ops->port_policer_del(ds, dp->index); 1312f50f2127SFlorian Fainelli break; 1313f50f2127SFlorian Fainelli default: 1314f50f2127SFlorian Fainelli WARN_ON(1); 1315f50f2127SFlorian Fainelli } 1316f50f2127SFlorian Fainelli 1317f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1318f50f2127SFlorian Fainelli } 1319f50f2127SFlorian Fainelli 13203fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 13216b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 13226b3eb752SJiri Pirko bool ingress) 1323f50f2127SFlorian Fainelli { 13245fd9fc4eSJiri Pirko if (cls->common.chain_index) 1325a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 1326f50f2127SFlorian Fainelli 13273fbae382SJiri Pirko switch (cls->command) { 13283fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 13295fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 13303fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 13313fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 13323fbae382SJiri Pirko return 0; 13333fbae382SJiri Pirko default: 13343fbae382SJiri Pirko return -EOPNOTSUPP; 13353fbae382SJiri Pirko } 13363fbae382SJiri Pirko } 13373fbae382SJiri Pirko 1338ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev, 1339ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1340ed11bb1fSVladimir Oltean bool ingress) 1341ed11bb1fSVladimir Oltean { 1342ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1343ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1344ed11bb1fSVladimir Oltean int port = dp->index; 1345ed11bb1fSVladimir Oltean 1346ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_add) 1347ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1348ed11bb1fSVladimir Oltean 1349ed11bb1fSVladimir Oltean return ds->ops->cls_flower_add(ds, port, cls, ingress); 1350ed11bb1fSVladimir Oltean } 1351ed11bb1fSVladimir Oltean 1352ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev, 1353ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1354ed11bb1fSVladimir Oltean bool ingress) 1355ed11bb1fSVladimir Oltean { 1356ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1357ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1358ed11bb1fSVladimir Oltean int port = dp->index; 1359ed11bb1fSVladimir Oltean 1360ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_del) 1361ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1362ed11bb1fSVladimir Oltean 1363ed11bb1fSVladimir Oltean return ds->ops->cls_flower_del(ds, port, cls, ingress); 1364ed11bb1fSVladimir Oltean } 1365ed11bb1fSVladimir Oltean 1366ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev, 1367ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1368ed11bb1fSVladimir Oltean bool ingress) 1369ed11bb1fSVladimir Oltean { 1370ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1371ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1372ed11bb1fSVladimir Oltean int port = dp->index; 1373ed11bb1fSVladimir Oltean 1374ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_stats) 1375ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1376ed11bb1fSVladimir Oltean 1377ed11bb1fSVladimir Oltean return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1378ed11bb1fSVladimir Oltean } 1379ed11bb1fSVladimir Oltean 1380ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1381ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1382ed11bb1fSVladimir Oltean bool ingress) 1383ed11bb1fSVladimir Oltean { 1384ed11bb1fSVladimir Oltean switch (cls->command) { 1385ed11bb1fSVladimir Oltean case FLOW_CLS_REPLACE: 1386ed11bb1fSVladimir Oltean return dsa_slave_add_cls_flower(dev, cls, ingress); 1387ed11bb1fSVladimir Oltean case FLOW_CLS_DESTROY: 1388ed11bb1fSVladimir Oltean return dsa_slave_del_cls_flower(dev, cls, ingress); 1389ed11bb1fSVladimir Oltean case FLOW_CLS_STATS: 1390ed11bb1fSVladimir Oltean return dsa_slave_stats_cls_flower(dev, cls, ingress); 1391ed11bb1fSVladimir Oltean default: 1392ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1393ed11bb1fSVladimir Oltean } 1394ed11bb1fSVladimir Oltean } 1395ed11bb1fSVladimir Oltean 13966b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 13976b3eb752SJiri Pirko void *cb_priv, bool ingress) 13986b3eb752SJiri Pirko { 13996b3eb752SJiri Pirko struct net_device *dev = cb_priv; 14006b3eb752SJiri Pirko 140144ae12a7SJiri Pirko if (!tc_can_offload(dev)) 140244ae12a7SJiri Pirko return -EOPNOTSUPP; 140344ae12a7SJiri Pirko 14046b3eb752SJiri Pirko switch (type) { 14056b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 14066b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1407ed11bb1fSVladimir Oltean case TC_SETUP_CLSFLOWER: 1408ed11bb1fSVladimir Oltean return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 14096b3eb752SJiri Pirko default: 14106b3eb752SJiri Pirko return -EOPNOTSUPP; 14116b3eb752SJiri Pirko } 14126b3eb752SJiri Pirko } 14136b3eb752SJiri Pirko 14146b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 14156b3eb752SJiri Pirko void *type_data, void *cb_priv) 14166b3eb752SJiri Pirko { 14176b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 14186b3eb752SJiri Pirko } 14196b3eb752SJiri Pirko 14206b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 14216b3eb752SJiri Pirko void *type_data, void *cb_priv) 14226b3eb752SJiri Pirko { 14236b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 14246b3eb752SJiri Pirko } 14256b3eb752SJiri Pirko 1426955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 1427955bcb6eSPablo Neira Ayuso 14286b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 1429955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 14306b3eb752SJiri Pirko { 1431955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 1432a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 14336b3eb752SJiri Pirko 143432f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 14356b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 143632f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 14376b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 14386b3eb752SJiri Pirko else 14396b3eb752SJiri Pirko return -EOPNOTSUPP; 14406b3eb752SJiri Pirko 1441955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 1442955bcb6eSPablo Neira Ayuso 14436b3eb752SJiri Pirko switch (f->command) { 14449c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 14450d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 14460d4fd02eSPablo Neira Ayuso return -EBUSY; 14470d4fd02eSPablo Neira Ayuso 14480c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1449955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 1450955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 1451955bcb6eSPablo Neira Ayuso 1452955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 1453955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1454955bcb6eSPablo Neira Ayuso return 0; 14559c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 145614bfb13fSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f->block, cb, dev); 1457955bcb6eSPablo Neira Ayuso if (!block_cb) 1458955bcb6eSPablo Neira Ayuso return -ENOENT; 1459955bcb6eSPablo Neira Ayuso 1460955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 1461955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 14626b3eb752SJiri Pirko return 0; 14636b3eb752SJiri Pirko default: 14646b3eb752SJiri Pirko return -EOPNOTSUPP; 14656b3eb752SJiri Pirko } 14666b3eb752SJiri Pirko } 14676b3eb752SJiri Pirko 14683fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 14693fb24a43SPablo Neira Ayuso void *type_data) 14703fb24a43SPablo Neira Ayuso { 14713fb24a43SPablo Neira Ayuso struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 14723fb24a43SPablo Neira Ayuso struct net_device *master = cpu_dp->master; 14733fb24a43SPablo Neira Ayuso 14743fb24a43SPablo Neira Ayuso if (!master->netdev_ops->ndo_setup_tc) 14753fb24a43SPablo Neira Ayuso return -EOPNOTSUPP; 14763fb24a43SPablo Neira Ayuso 14773fb24a43SPablo Neira Ayuso return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 14783fb24a43SPablo Neira Ayuso } 14793fb24a43SPablo Neira Ayuso 14803fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1481de4784caSJiri Pirko void *type_data) 14823fbae382SJiri Pirko { 148347d23af2SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 148447d23af2SVladimir Oltean struct dsa_switch *ds = dp->ds; 148547d23af2SVladimir Oltean 14863fb24a43SPablo Neira Ayuso switch (type) { 14873fb24a43SPablo Neira Ayuso case TC_SETUP_BLOCK: 14886b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 14893fb24a43SPablo Neira Ayuso case TC_SETUP_FT: 14903fb24a43SPablo Neira Ayuso return dsa_slave_setup_ft_block(ds, dp->index, type_data); 14913fb24a43SPablo Neira Ayuso default: 14923fb24a43SPablo Neira Ayuso break; 14933fb24a43SPablo Neira Ayuso } 149447d23af2SVladimir Oltean 149547d23af2SVladimir Oltean if (!ds->ops->port_setup_tc) 1496a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 149747d23af2SVladimir Oltean 149847d23af2SVladimir Oltean return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1499f50f2127SFlorian Fainelli } 1500f50f2127SFlorian Fainelli 1501bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1502bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1503bf9f2648SFlorian Fainelli { 1504d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1505d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1506bf9f2648SFlorian Fainelli 1507bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1508bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1509bf9f2648SFlorian Fainelli 1510d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1511bf9f2648SFlorian Fainelli } 1512bf9f2648SFlorian Fainelli 1513bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1514bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1515bf9f2648SFlorian Fainelli { 1516d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1517d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1518bf9f2648SFlorian Fainelli 1519bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1520bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1521bf9f2648SFlorian Fainelli 1522d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1523bf9f2648SFlorian Fainelli } 1524bf9f2648SFlorian Fainelli 15250336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 15260336369dSBrandon Streiff struct ethtool_ts_info *ts) 15270336369dSBrandon Streiff { 15280336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 15290336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 15300336369dSBrandon Streiff 15310336369dSBrandon Streiff if (!ds->ops->get_ts_info) 15320336369dSBrandon Streiff return -EOPNOTSUPP; 15330336369dSBrandon Streiff 15340336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 15350336369dSBrandon Streiff } 15360336369dSBrandon Streiff 1537061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1538061f6a50SFlorian Fainelli u16 vid) 1539061f6a50SFlorian Fainelli { 1540061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 154188236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 154288236591SVladimir Oltean .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1543b7a9e0daSVladimir Oltean .vid = vid, 154488236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 154588236591SVladimir Oltean .flags = 0, 154688236591SVladimir Oltean }; 154731046a5fSVladimir Oltean struct netlink_ext_ack extack = {0}; 1548061f6a50SFlorian Fainelli int ret; 1549061f6a50SFlorian Fainelli 155088236591SVladimir Oltean /* User port... */ 155131046a5fSVladimir Oltean ret = dsa_port_vlan_add(dp, &vlan, &extack); 155231046a5fSVladimir Oltean if (ret) { 155331046a5fSVladimir Oltean if (extack._msg) 155431046a5fSVladimir Oltean netdev_err(dev, "%s\n", extack._msg); 155588236591SVladimir Oltean return ret; 155631046a5fSVladimir Oltean } 155788236591SVladimir Oltean 155888236591SVladimir Oltean /* And CPU port... */ 1559134ef238SVladimir Oltean ret = dsa_port_host_vlan_add(dp, &vlan, &extack); 156031046a5fSVladimir Oltean if (ret) { 156131046a5fSVladimir Oltean if (extack._msg) 156231046a5fSVladimir Oltean netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 156331046a5fSVladimir Oltean extack._msg); 15647e1741b4SVivien Didelot return ret; 156531046a5fSVladimir Oltean } 15667e1741b4SVivien Didelot 1567134ef238SVladimir Oltean return 0; 1568061f6a50SFlorian Fainelli } 1569061f6a50SFlorian Fainelli 1570061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1571061f6a50SFlorian Fainelli u16 vid) 1572061f6a50SFlorian Fainelli { 1573061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 157488236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 1575b7a9e0daSVladimir Oltean .vid = vid, 157688236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 157788236591SVladimir Oltean .flags = 0, 157888236591SVladimir Oltean }; 15792209158cSVladimir Oltean int err; 1580061f6a50SFlorian Fainelli 15812209158cSVladimir Oltean err = dsa_port_vlan_del(dp, &vlan); 15822209158cSVladimir Oltean if (err) 15832209158cSVladimir Oltean return err; 15842209158cSVladimir Oltean 1585134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, &vlan); 1586061f6a50SFlorian Fainelli } 1587061f6a50SFlorian Fainelli 158806cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg) 158906cfb2dfSVladimir Oltean { 159006cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 159106cfb2dfSVladimir Oltean 159206cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_add_vid(arg, proto, vid); 159306cfb2dfSVladimir Oltean } 159406cfb2dfSVladimir Oltean 159506cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg) 159606cfb2dfSVladimir Oltean { 159706cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 159806cfb2dfSVladimir Oltean 159906cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_kill_vid(arg, proto, vid); 160006cfb2dfSVladimir Oltean } 160106cfb2dfSVladimir Oltean 160206cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN 160306cfb2dfSVladimir Oltean * filtering is enabled. The baseline is that only ports that offload a 160406cfb2dfSVladimir Oltean * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware, 160506cfb2dfSVladimir Oltean * but there are exceptions for quirky hardware. 160606cfb2dfSVladimir Oltean * 160706cfb2dfSVladimir Oltean * If ds->vlan_filtering_is_global = true, then standalone ports which share 160806cfb2dfSVladimir Oltean * the same switch with other ports that offload a VLAN-aware bridge are also 160906cfb2dfSVladimir Oltean * inevitably VLAN-aware. 161006cfb2dfSVladimir Oltean * 161106cfb2dfSVladimir Oltean * To summarize, a DSA switch port offloads: 161206cfb2dfSVladimir Oltean * 161306cfb2dfSVladimir Oltean * - If standalone (this includes software bridge, software LAG): 161458adf9dcSVladimir Oltean * - if ds->needs_standalone_vlan_filtering = true, OR if 161558adf9dcSVladimir Oltean * (ds->vlan_filtering_is_global = true AND there are bridges spanning 161658adf9dcSVladimir Oltean * this switch chip which have vlan_filtering=1) 161706cfb2dfSVladimir Oltean * - the 8021q upper VLANs 161858adf9dcSVladimir Oltean * - else (standalone VLAN filtering is not needed, VLAN filtering is not 161958adf9dcSVladimir Oltean * global, or it is, but no port is under a VLAN-aware bridge): 162006cfb2dfSVladimir Oltean * - no VLAN (any 8021q upper is a software VLAN) 162106cfb2dfSVladimir Oltean * 162206cfb2dfSVladimir Oltean * - If under a vlan_filtering=0 bridge which it offload: 162306cfb2dfSVladimir Oltean * - if ds->configure_vlan_while_not_filtering = true (default): 162406cfb2dfSVladimir Oltean * - the bridge VLANs. These VLANs are committed to hardware but inactive. 162506cfb2dfSVladimir Oltean * - else (deprecated): 162606cfb2dfSVladimir Oltean * - no VLAN. The bridge VLANs are not restored when VLAN awareness is 162706cfb2dfSVladimir Oltean * enabled, so this behavior is broken and discouraged. 162806cfb2dfSVladimir Oltean * 162906cfb2dfSVladimir Oltean * - If under a vlan_filtering=1 bridge which it offload: 163006cfb2dfSVladimir Oltean * - the bridge VLANs 163106cfb2dfSVladimir Oltean * - the 8021q upper VLANs 163206cfb2dfSVladimir Oltean */ 163306cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave, 163406cfb2dfSVladimir Oltean bool vlan_filtering) 163506cfb2dfSVladimir Oltean { 163606cfb2dfSVladimir Oltean int err; 163706cfb2dfSVladimir Oltean 163806cfb2dfSVladimir Oltean if (vlan_filtering) { 163906cfb2dfSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 164006cfb2dfSVladimir Oltean 164106cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_restore_vlan, slave); 164206cfb2dfSVladimir Oltean if (err) { 164306cfb2dfSVladimir Oltean vlan_for_each(slave, dsa_slave_clear_vlan, slave); 164406cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 164506cfb2dfSVladimir Oltean return err; 164606cfb2dfSVladimir Oltean } 164706cfb2dfSVladimir Oltean } else { 164806cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_clear_vlan, slave); 164906cfb2dfSVladimir Oltean if (err) 165006cfb2dfSVladimir Oltean return err; 165106cfb2dfSVladimir Oltean 165206cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 165306cfb2dfSVladimir Oltean } 165406cfb2dfSVladimir Oltean 165506cfb2dfSVladimir Oltean return 0; 165606cfb2dfSVladimir Oltean } 165706cfb2dfSVladimir Oltean 1658bff33f7eSVladimir Oltean struct dsa_hw_port { 1659bff33f7eSVladimir Oltean struct list_head list; 1660bff33f7eSVladimir Oltean struct net_device *dev; 1661bff33f7eSVladimir Oltean int old_mtu; 1662bff33f7eSVladimir Oltean }; 1663bff33f7eSVladimir Oltean 1664bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1665bff33f7eSVladimir Oltean { 1666bff33f7eSVladimir Oltean const struct dsa_hw_port *p; 1667bff33f7eSVladimir Oltean int err; 1668bff33f7eSVladimir Oltean 1669bff33f7eSVladimir Oltean list_for_each_entry(p, hw_port_list, list) { 1670bff33f7eSVladimir Oltean if (p->dev->mtu == mtu) 1671bff33f7eSVladimir Oltean continue; 1672bff33f7eSVladimir Oltean 1673bff33f7eSVladimir Oltean err = dev_set_mtu(p->dev, mtu); 1674bff33f7eSVladimir Oltean if (err) 1675bff33f7eSVladimir Oltean goto rollback; 1676bff33f7eSVladimir Oltean } 1677bff33f7eSVladimir Oltean 1678bff33f7eSVladimir Oltean return 0; 1679bff33f7eSVladimir Oltean 1680bff33f7eSVladimir Oltean rollback: 1681bff33f7eSVladimir Oltean list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1682bff33f7eSVladimir Oltean if (p->dev->mtu == p->old_mtu) 1683bff33f7eSVladimir Oltean continue; 1684bff33f7eSVladimir Oltean 1685bff33f7eSVladimir Oltean if (dev_set_mtu(p->dev, p->old_mtu)) 1686bff33f7eSVladimir Oltean netdev_err(p->dev, "Failed to restore MTU\n"); 1687bff33f7eSVladimir Oltean } 1688bff33f7eSVladimir Oltean 1689bff33f7eSVladimir Oltean return err; 1690bff33f7eSVladimir Oltean } 1691bff33f7eSVladimir Oltean 1692bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1693bff33f7eSVladimir Oltean { 1694bff33f7eSVladimir Oltean struct dsa_hw_port *p, *n; 1695bff33f7eSVladimir Oltean 1696bff33f7eSVladimir Oltean list_for_each_entry_safe(p, n, hw_port_list, list) 1697bff33f7eSVladimir Oltean kfree(p); 1698bff33f7eSVladimir Oltean } 1699bff33f7eSVladimir Oltean 1700bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */ 1701bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1702bff33f7eSVladimir Oltean { 1703bff33f7eSVladimir Oltean struct list_head hw_port_list; 1704bff33f7eSVladimir Oltean struct dsa_switch_tree *dst; 1705bff33f7eSVladimir Oltean int min_mtu = ETH_MAX_MTU; 1706bff33f7eSVladimir Oltean struct dsa_port *other_dp; 1707bff33f7eSVladimir Oltean int err; 1708bff33f7eSVladimir Oltean 1709bff33f7eSVladimir Oltean if (!dp->ds->mtu_enforcement_ingress) 1710bff33f7eSVladimir Oltean return; 1711bff33f7eSVladimir Oltean 1712d3eed0e5SVladimir Oltean if (!dp->bridge) 1713bff33f7eSVladimir Oltean return; 1714bff33f7eSVladimir Oltean 1715bff33f7eSVladimir Oltean INIT_LIST_HEAD(&hw_port_list); 1716bff33f7eSVladimir Oltean 1717bff33f7eSVladimir Oltean /* Populate the list of ports that are part of the same bridge 1718bff33f7eSVladimir Oltean * as the newly added/modified port 1719bff33f7eSVladimir Oltean */ 1720bff33f7eSVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) { 1721bff33f7eSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 1722bff33f7eSVladimir Oltean struct dsa_hw_port *hw_port; 1723bff33f7eSVladimir Oltean struct net_device *slave; 1724bff33f7eSVladimir Oltean 1725bff33f7eSVladimir Oltean if (other_dp->type != DSA_PORT_TYPE_USER) 1726bff33f7eSVladimir Oltean continue; 1727bff33f7eSVladimir Oltean 172836cbf39bSVladimir Oltean if (!dsa_port_bridge_same(dp, other_dp)) 1729bff33f7eSVladimir Oltean continue; 1730bff33f7eSVladimir Oltean 1731bff33f7eSVladimir Oltean if (!other_dp->ds->mtu_enforcement_ingress) 1732bff33f7eSVladimir Oltean continue; 1733bff33f7eSVladimir Oltean 1734bff33f7eSVladimir Oltean slave = other_dp->slave; 1735bff33f7eSVladimir Oltean 1736bff33f7eSVladimir Oltean if (min_mtu > slave->mtu) 1737bff33f7eSVladimir Oltean min_mtu = slave->mtu; 1738bff33f7eSVladimir Oltean 1739bff33f7eSVladimir Oltean hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1740bff33f7eSVladimir Oltean if (!hw_port) 1741bff33f7eSVladimir Oltean goto out; 1742bff33f7eSVladimir Oltean 1743bff33f7eSVladimir Oltean hw_port->dev = slave; 1744bff33f7eSVladimir Oltean hw_port->old_mtu = slave->mtu; 1745bff33f7eSVladimir Oltean 1746bff33f7eSVladimir Oltean list_add(&hw_port->list, &hw_port_list); 1747bff33f7eSVladimir Oltean } 1748bff33f7eSVladimir Oltean } 1749bff33f7eSVladimir Oltean 1750bff33f7eSVladimir Oltean /* Attempt to configure the entire hardware bridge to the newly added 1751bff33f7eSVladimir Oltean * interface's MTU first, regardless of whether the intention of the 1752bff33f7eSVladimir Oltean * user was to raise or lower it. 1753bff33f7eSVladimir Oltean */ 1754bff33f7eSVladimir Oltean err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1755bff33f7eSVladimir Oltean if (!err) 1756bff33f7eSVladimir Oltean goto out; 1757bff33f7eSVladimir Oltean 1758bff33f7eSVladimir Oltean /* Clearly that didn't work out so well, so just set the minimum MTU on 1759bff33f7eSVladimir Oltean * all hardware bridge ports now. If this fails too, then all ports will 1760bff33f7eSVladimir Oltean * still have their old MTU rolled back anyway. 1761bff33f7eSVladimir Oltean */ 1762bff33f7eSVladimir Oltean dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1763bff33f7eSVladimir Oltean 1764bff33f7eSVladimir Oltean out: 1765bff33f7eSVladimir Oltean dsa_hw_port_list_free(&hw_port_list); 1766bff33f7eSVladimir Oltean } 1767bff33f7eSVladimir Oltean 176853da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1769bfcb8132SVladimir Oltean { 1770bfcb8132SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1771bfcb8132SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1772bfcb8132SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 1773bfcb8132SVladimir Oltean struct dsa_switch *ds = p->dp->ds; 17744e4ab795SVladimir Oltean struct dsa_port *dp_iter; 1775bfcb8132SVladimir Oltean struct dsa_port *cpu_dp; 1776bfcb8132SVladimir Oltean int port = p->dp->index; 1777bfcb8132SVladimir Oltean int largest_mtu = 0; 1778bfcb8132SVladimir Oltean int new_master_mtu; 1779bfcb8132SVladimir Oltean int old_master_mtu; 1780bfcb8132SVladimir Oltean int mtu_limit; 1781bfcb8132SVladimir Oltean int cpu_mtu; 17824e4ab795SVladimir Oltean int err; 1783bfcb8132SVladimir Oltean 1784bfcb8132SVladimir Oltean if (!ds->ops->port_change_mtu) 1785bfcb8132SVladimir Oltean return -EOPNOTSUPP; 1786bfcb8132SVladimir Oltean 17874e4ab795SVladimir Oltean list_for_each_entry(dp_iter, &ds->dst->ports, list) { 1788bfcb8132SVladimir Oltean int slave_mtu; 1789bfcb8132SVladimir Oltean 17904e4ab795SVladimir Oltean if (!dsa_port_is_user(dp_iter)) 1791bfcb8132SVladimir Oltean continue; 1792bfcb8132SVladimir Oltean 1793bfcb8132SVladimir Oltean /* During probe, this function will be called for each slave 1794bfcb8132SVladimir Oltean * device, while not all of them have been allocated. That's 1795bfcb8132SVladimir Oltean * ok, it doesn't change what the maximum is, so ignore it. 1796bfcb8132SVladimir Oltean */ 17974e4ab795SVladimir Oltean if (!dp_iter->slave) 1798bfcb8132SVladimir Oltean continue; 1799bfcb8132SVladimir Oltean 1800bfcb8132SVladimir Oltean /* Pretend that we already applied the setting, which we 1801bfcb8132SVladimir Oltean * actually haven't (still haven't done all integrity checks) 1802bfcb8132SVladimir Oltean */ 18034e4ab795SVladimir Oltean if (dp_iter == dp) 1804bfcb8132SVladimir Oltean slave_mtu = new_mtu; 1805bfcb8132SVladimir Oltean else 18064e4ab795SVladimir Oltean slave_mtu = dp_iter->slave->mtu; 1807bfcb8132SVladimir Oltean 1808bfcb8132SVladimir Oltean if (largest_mtu < slave_mtu) 1809bfcb8132SVladimir Oltean largest_mtu = slave_mtu; 1810bfcb8132SVladimir Oltean } 1811bfcb8132SVladimir Oltean 1812bfcb8132SVladimir Oltean cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1813bfcb8132SVladimir Oltean 1814bfcb8132SVladimir Oltean mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1815bfcb8132SVladimir Oltean old_master_mtu = master->mtu; 18164e500251SVladimir Oltean new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops); 1817bfcb8132SVladimir Oltean if (new_master_mtu > mtu_limit) 1818bfcb8132SVladimir Oltean return -ERANGE; 1819bfcb8132SVladimir Oltean 1820bfcb8132SVladimir Oltean /* If the master MTU isn't over limit, there's no need to check the CPU 1821bfcb8132SVladimir Oltean * MTU, since that surely isn't either. 1822bfcb8132SVladimir Oltean */ 1823bfcb8132SVladimir Oltean cpu_mtu = largest_mtu; 1824bfcb8132SVladimir Oltean 1825bfcb8132SVladimir Oltean /* Start applying stuff */ 1826bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) { 1827bfcb8132SVladimir Oltean err = dev_set_mtu(master, new_master_mtu); 1828bfcb8132SVladimir Oltean if (err < 0) 1829bfcb8132SVladimir Oltean goto out_master_failed; 1830bfcb8132SVladimir Oltean 1831bfcb8132SVladimir Oltean /* We only need to propagate the MTU of the CPU port to 183288faba20SVladimir Oltean * upstream switches, so create a non-targeted notifier which 183388faba20SVladimir Oltean * updates all switches. 1834bfcb8132SVladimir Oltean */ 183588faba20SVladimir Oltean err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false); 1836bfcb8132SVladimir Oltean if (err) 1837bfcb8132SVladimir Oltean goto out_cpu_failed; 1838bfcb8132SVladimir Oltean } 1839bfcb8132SVladimir Oltean 184088faba20SVladimir Oltean err = dsa_port_mtu_change(dp, new_mtu, true); 1841bfcb8132SVladimir Oltean if (err) 1842bfcb8132SVladimir Oltean goto out_port_failed; 1843bfcb8132SVladimir Oltean 1844bfcb8132SVladimir Oltean dev->mtu = new_mtu; 1845bfcb8132SVladimir Oltean 1846bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 1847bff33f7eSVladimir Oltean 1848bfcb8132SVladimir Oltean return 0; 1849bfcb8132SVladimir Oltean 1850bfcb8132SVladimir Oltean out_port_failed: 1851bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1852bfcb8132SVladimir Oltean dsa_port_mtu_change(cpu_dp, old_master_mtu - 18534e500251SVladimir Oltean dsa_tag_protocol_overhead(cpu_dp->tag_ops), 185488faba20SVladimir Oltean false); 1855bfcb8132SVladimir Oltean out_cpu_failed: 1856bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1857bfcb8132SVladimir Oltean dev_set_mtu(master, old_master_mtu); 1858bfcb8132SVladimir Oltean out_master_failed: 1859bfcb8132SVladimir Oltean return err; 1860bfcb8132SVladimir Oltean } 1861bfcb8132SVladimir Oltean 1862d538eca8SVladimir Oltean static int __maybe_unused 1863d538eca8SVladimir Oltean dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app) 1864d538eca8SVladimir Oltean { 1865d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1866d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1867d538eca8SVladimir Oltean unsigned long mask, new_prio; 1868d538eca8SVladimir Oltean int err, port = dp->index; 1869d538eca8SVladimir Oltean 1870d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1871d538eca8SVladimir Oltean return -EOPNOTSUPP; 1872d538eca8SVladimir Oltean 1873d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, app); 1874d538eca8SVladimir Oltean if (err) 1875d538eca8SVladimir Oltean return err; 1876d538eca8SVladimir Oltean 1877d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1878d538eca8SVladimir Oltean new_prio = __fls(mask); 1879d538eca8SVladimir Oltean 1880d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1881d538eca8SVladimir Oltean if (err) { 1882d538eca8SVladimir Oltean dcb_ieee_delapp(dev, app); 1883d538eca8SVladimir Oltean return err; 1884d538eca8SVladimir Oltean } 1885d538eca8SVladimir Oltean 1886d538eca8SVladimir Oltean return 0; 1887d538eca8SVladimir Oltean } 1888d538eca8SVladimir Oltean 188947d75f78SVladimir Oltean static int __maybe_unused 189047d75f78SVladimir Oltean dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app) 189147d75f78SVladimir Oltean { 189247d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 189347d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 189447d75f78SVladimir Oltean unsigned long mask, new_prio; 189547d75f78SVladimir Oltean int err, port = dp->index; 189647d75f78SVladimir Oltean u8 dscp = app->protocol; 189747d75f78SVladimir Oltean 189847d75f78SVladimir Oltean if (!ds->ops->port_add_dscp_prio) 189947d75f78SVladimir Oltean return -EOPNOTSUPP; 190047d75f78SVladimir Oltean 190147d75f78SVladimir Oltean if (dscp >= 64) { 190247d75f78SVladimir Oltean netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n", 190347d75f78SVladimir Oltean dscp); 190447d75f78SVladimir Oltean return -EINVAL; 190547d75f78SVladimir Oltean } 190647d75f78SVladimir Oltean 190747d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, app); 190847d75f78SVladimir Oltean if (err) 190947d75f78SVladimir Oltean return err; 191047d75f78SVladimir Oltean 191147d75f78SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 191247d75f78SVladimir Oltean new_prio = __fls(mask); 191347d75f78SVladimir Oltean 191447d75f78SVladimir Oltean err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio); 191547d75f78SVladimir Oltean if (err) { 191647d75f78SVladimir Oltean dcb_ieee_delapp(dev, app); 191747d75f78SVladimir Oltean return err; 191847d75f78SVladimir Oltean } 191947d75f78SVladimir Oltean 192047d75f78SVladimir Oltean return 0; 192147d75f78SVladimir Oltean } 192247d75f78SVladimir Oltean 1923d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev, 1924d538eca8SVladimir Oltean struct dcb_app *app) 1925d538eca8SVladimir Oltean { 1926d538eca8SVladimir Oltean switch (app->selector) { 1927d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1928d538eca8SVladimir Oltean switch (app->protocol) { 1929d538eca8SVladimir Oltean case 0: 1930d538eca8SVladimir Oltean return dsa_slave_dcbnl_set_default_prio(dev, app); 1931d538eca8SVladimir Oltean default: 1932d538eca8SVladimir Oltean return -EOPNOTSUPP; 1933d538eca8SVladimir Oltean } 1934d538eca8SVladimir Oltean break; 193547d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 193647d75f78SVladimir Oltean return dsa_slave_dcbnl_add_dscp_prio(dev, app); 1937d538eca8SVladimir Oltean default: 1938d538eca8SVladimir Oltean return -EOPNOTSUPP; 1939d538eca8SVladimir Oltean } 1940d538eca8SVladimir Oltean } 1941d538eca8SVladimir Oltean 1942d538eca8SVladimir Oltean static int __maybe_unused 1943d538eca8SVladimir Oltean dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app) 1944d538eca8SVladimir Oltean { 1945d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1946d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1947d538eca8SVladimir Oltean unsigned long mask, new_prio; 1948d538eca8SVladimir Oltean int err, port = dp->index; 1949d538eca8SVladimir Oltean 1950d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1951d538eca8SVladimir Oltean return -EOPNOTSUPP; 1952d538eca8SVladimir Oltean 1953d538eca8SVladimir Oltean err = dcb_ieee_delapp(dev, app); 1954d538eca8SVladimir Oltean if (err) 1955d538eca8SVladimir Oltean return err; 1956d538eca8SVladimir Oltean 1957d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1958d538eca8SVladimir Oltean new_prio = mask ? __fls(mask) : 0; 1959d538eca8SVladimir Oltean 1960d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1961d538eca8SVladimir Oltean if (err) { 1962d538eca8SVladimir Oltean dcb_ieee_setapp(dev, app); 1963d538eca8SVladimir Oltean return err; 1964d538eca8SVladimir Oltean } 1965d538eca8SVladimir Oltean 1966d538eca8SVladimir Oltean return 0; 1967d538eca8SVladimir Oltean } 1968d538eca8SVladimir Oltean 196947d75f78SVladimir Oltean static int __maybe_unused 197047d75f78SVladimir Oltean dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app) 197147d75f78SVladimir Oltean { 197247d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 197347d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 197447d75f78SVladimir Oltean int err, port = dp->index; 197547d75f78SVladimir Oltean u8 dscp = app->protocol; 197647d75f78SVladimir Oltean 197747d75f78SVladimir Oltean if (!ds->ops->port_del_dscp_prio) 197847d75f78SVladimir Oltean return -EOPNOTSUPP; 197947d75f78SVladimir Oltean 198047d75f78SVladimir Oltean err = dcb_ieee_delapp(dev, app); 198147d75f78SVladimir Oltean if (err) 198247d75f78SVladimir Oltean return err; 198347d75f78SVladimir Oltean 198447d75f78SVladimir Oltean err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority); 198547d75f78SVladimir Oltean if (err) { 198647d75f78SVladimir Oltean dcb_ieee_setapp(dev, app); 198747d75f78SVladimir Oltean return err; 198847d75f78SVladimir Oltean } 198947d75f78SVladimir Oltean 199047d75f78SVladimir Oltean return 0; 199147d75f78SVladimir Oltean } 199247d75f78SVladimir Oltean 1993d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev, 1994d538eca8SVladimir Oltean struct dcb_app *app) 1995d538eca8SVladimir Oltean { 1996d538eca8SVladimir Oltean switch (app->selector) { 1997d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1998d538eca8SVladimir Oltean switch (app->protocol) { 1999d538eca8SVladimir Oltean case 0: 2000d538eca8SVladimir Oltean return dsa_slave_dcbnl_del_default_prio(dev, app); 2001d538eca8SVladimir Oltean default: 2002d538eca8SVladimir Oltean return -EOPNOTSUPP; 2003d538eca8SVladimir Oltean } 2004d538eca8SVladimir Oltean break; 200547d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 200647d75f78SVladimir Oltean return dsa_slave_dcbnl_del_dscp_prio(dev, app); 2007d538eca8SVladimir Oltean default: 2008d538eca8SVladimir Oltean return -EOPNOTSUPP; 2009d538eca8SVladimir Oltean } 2010d538eca8SVladimir Oltean } 2011d538eca8SVladimir Oltean 2012d538eca8SVladimir Oltean /* Pre-populate the DCB application priority table with the priorities 2013d538eca8SVladimir Oltean * configured during switch setup, which we read from hardware here. 2014d538eca8SVladimir Oltean */ 2015d538eca8SVladimir Oltean static int dsa_slave_dcbnl_init(struct net_device *dev) 2016d538eca8SVladimir Oltean { 2017d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2018d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 2019d538eca8SVladimir Oltean int port = dp->index; 2020d538eca8SVladimir Oltean int err; 2021d538eca8SVladimir Oltean 2022d538eca8SVladimir Oltean if (ds->ops->port_get_default_prio) { 2023d538eca8SVladimir Oltean int prio = ds->ops->port_get_default_prio(ds, port); 2024d538eca8SVladimir Oltean struct dcb_app app = { 2025d538eca8SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE, 2026d538eca8SVladimir Oltean .protocol = 0, 2027d538eca8SVladimir Oltean .priority = prio, 2028d538eca8SVladimir Oltean }; 2029d538eca8SVladimir Oltean 2030d538eca8SVladimir Oltean if (prio < 0) 2031d538eca8SVladimir Oltean return prio; 2032d538eca8SVladimir Oltean 2033d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 2034d538eca8SVladimir Oltean if (err) 2035d538eca8SVladimir Oltean return err; 2036d538eca8SVladimir Oltean } 2037d538eca8SVladimir Oltean 203847d75f78SVladimir Oltean if (ds->ops->port_get_dscp_prio) { 203947d75f78SVladimir Oltean int protocol; 204047d75f78SVladimir Oltean 204147d75f78SVladimir Oltean for (protocol = 0; protocol < 64; protocol++) { 204247d75f78SVladimir Oltean struct dcb_app app = { 204347d75f78SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_DSCP, 204447d75f78SVladimir Oltean .protocol = protocol, 204547d75f78SVladimir Oltean }; 204647d75f78SVladimir Oltean int prio; 204747d75f78SVladimir Oltean 204847d75f78SVladimir Oltean prio = ds->ops->port_get_dscp_prio(ds, port, protocol); 204947d75f78SVladimir Oltean if (prio == -EOPNOTSUPP) 205047d75f78SVladimir Oltean continue; 205147d75f78SVladimir Oltean if (prio < 0) 205247d75f78SVladimir Oltean return prio; 205347d75f78SVladimir Oltean 205447d75f78SVladimir Oltean app.priority = prio; 205547d75f78SVladimir Oltean 205647d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 205747d75f78SVladimir Oltean if (err) 205847d75f78SVladimir Oltean return err; 205947d75f78SVladimir Oltean } 206047d75f78SVladimir Oltean } 206147d75f78SVladimir Oltean 2062d538eca8SVladimir Oltean return 0; 2063d538eca8SVladimir Oltean } 2064d538eca8SVladimir Oltean 206591da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 206691da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 20673d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 20683d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 2069aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 2070c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 20716793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 20726793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 20736793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 207491da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 207591da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 207691da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 2077487d3855SAlvin Šipraga .get_eth_phy_stats = dsa_slave_get_eth_phy_stats, 2078487d3855SAlvin Šipraga .get_eth_mac_stats = dsa_slave_get_eth_mac_stats, 2079487d3855SAlvin Šipraga .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats, 208019e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 208119e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 20827905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 20837905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 2084aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 2085aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 2086a2a1a13bSHeiner Kallweit .get_pauseparam = dsa_slave_get_pauseparam, 2087a2a1a13bSHeiner Kallweit .set_pauseparam = dsa_slave_set_pauseparam, 2088bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 2089bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 20900336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 2091a71acad9SOleksij Rempel .self_test = dsa_slave_net_selftest, 209291da11f8SLennert Buytenhek }; 209391da11f8SLennert Buytenhek 2094d538eca8SVladimir Oltean static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = { 2095d538eca8SVladimir Oltean .ieee_setapp = dsa_slave_dcbnl_ieee_setapp, 2096d538eca8SVladimir Oltean .ieee_delapp = dsa_slave_dcbnl_ieee_delapp, 2097d538eca8SVladimir Oltean }; 2098d538eca8SVladimir Oltean 2099716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 2100716efee2SJiri Pirko { 2101716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 2102716efee2SJiri Pirko 210345b987d5SVladimir Oltean return &dp->devlink_port; 2104716efee2SJiri Pirko } 2105716efee2SJiri Pirko 2106c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev, 2107c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s) 2108c2ec5f2eSOleksij Rempel { 2109c2ec5f2eSOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(dev); 2110c2ec5f2eSOleksij Rempel struct dsa_switch *ds = dp->ds; 2111c2ec5f2eSOleksij Rempel 2112c2ec5f2eSOleksij Rempel if (ds->ops->get_stats64) 2113c2ec5f2eSOleksij Rempel ds->ops->get_stats64(ds, dp->index, s); 2114c2ec5f2eSOleksij Rempel else 2115c2ec5f2eSOleksij Rempel dev_get_tstats64(dev, s); 2116c2ec5f2eSOleksij Rempel } 2117c2ec5f2eSOleksij Rempel 21180994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 21190994d492SFelix Fietkau struct net_device_path *path) 21200994d492SFelix Fietkau { 21210994d492SFelix Fietkau struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 21220994d492SFelix Fietkau struct dsa_port *cpu_dp = dp->cpu_dp; 21230994d492SFelix Fietkau 21240994d492SFelix Fietkau path->dev = ctx->dev; 21250994d492SFelix Fietkau path->type = DEV_PATH_DSA; 21260994d492SFelix Fietkau path->dsa.proto = cpu_dp->tag_ops->proto; 21270994d492SFelix Fietkau path->dsa.port = dp->index; 21280994d492SFelix Fietkau ctx->dev = cpu_dp->master; 21290994d492SFelix Fietkau 21300994d492SFelix Fietkau return 0; 21310994d492SFelix Fietkau } 21320994d492SFelix Fietkau 21333e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 2134d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 2135d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 21363e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 2137d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 2138d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 2139d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 21402bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 2141a7605370SArnd Bergmann .ndo_eth_ioctl = dsa_slave_ioctl, 2142abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 214304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 214404ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 214504ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 214604ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 214704ff53f9SFlorian Fainelli #endif 2148f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 2149c2ec5f2eSOleksij Rempel .ndo_get_stats64 = dsa_slave_get_stats64, 2150061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 2151061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 2152716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 2153bfcb8132SVladimir Oltean .ndo_change_mtu = dsa_slave_change_mtu, 21540994d492SFelix Fietkau .ndo_fill_forward_path = dsa_slave_fill_forward_path, 215598237d43SScott Feldman }; 215698237d43SScott Feldman 2157f37db85dSFlorian Fainelli static struct device_type dsa_type = { 2158f37db85dSFlorian Fainelli .name = "dsa", 2159f37db85dSFlorian Fainelli }; 2160f37db85dSFlorian Fainelli 2161aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 2162aab9c406SFlorian Fainelli { 2163aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 2164aab9c406SFlorian Fainelli 2165765bda93SRussell King if (dp->pl) 2166aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 2167aab9c406SFlorian Fainelli } 2168aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 2169aab9c406SFlorian Fainelli 21705c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 2171aab9c406SFlorian Fainelli struct phylink_link_state *state) 2172aab9c406SFlorian Fainelli { 21735c05c1dbSRussell King struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 2174aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 2175aab9c406SFlorian Fainelli 2176aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 2177aab9c406SFlorian Fainelli * not be called if it was not. 2178aab9c406SFlorian Fainelli */ 2179aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 2180ce31b31cSFlorian Fainelli } 2181ce31b31cSFlorian Fainelli 218291da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 2183c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr, 2184c916e8e1SOleksij Rempel u32 flags) 2185c305c165SFlorian Fainelli { 2186d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2187d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 2188c305c165SFlorian Fainelli 21890115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 21900115dcd1SVivien Didelot if (!slave_dev->phydev) { 2191d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 2192c305c165SFlorian Fainelli return -ENODEV; 2193d25b8e74SRussell King } 2194c305c165SFlorian Fainelli 2195c916e8e1SOleksij Rempel slave_dev->phydev->dev_flags |= flags; 2196c916e8e1SOleksij Rempel 2197aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 2198c305c165SFlorian Fainelli } 2199c305c165SFlorian Fainelli 22004fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 22010d8bcdd3SFlorian Fainelli { 2202d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2203d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 2204d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 22056819563eSFlorian Fainelli u32 phy_flags = 0; 22060c65b2b9SAndrew Lunn int ret; 22070d8bcdd3SFlorian Fainelli 220844cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 220944cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 221044cc27e4SIoana Ciornei 22115c05c1dbSRussell King /* The get_fixed_state callback takes precedence over polling the 22125c05c1dbSRussell King * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 22135c05c1dbSRussell King * this if the switch provides such a callback. 22145c05c1dbSRussell King */ 22155c05c1dbSRussell King if (ds->ops->phylink_fixed_state) { 22165c05c1dbSRussell King dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 22175c05c1dbSRussell King dp->pl_config.poll_fixed_state = true; 22185c05c1dbSRussell King } 22195c05c1dbSRussell King 222021bd64bdSRussell King (Oracle) ret = dsa_port_phylink_create(dp); 222121bd64bdSRussell King (Oracle) if (ret) 222221bd64bdSRussell King (Oracle) return ret; 2223aab9c406SFlorian Fainelli 22249d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 2225d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 22266819563eSFlorian Fainelli 2227aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 22286146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 22296146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 22306146dd45SVladimir Oltean * use the switch internal MDIO bus instead 22310d8bcdd3SFlorian Fainelli */ 2232c916e8e1SOleksij Rempel ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags); 2233d25b8e74SRussell King } 22346a52e733SVladimir Oltean if (ret) { 22356a52e733SVladimir Oltean netdev_err(slave_dev, "failed to connect to PHY: %pe\n", 22366a52e733SVladimir Oltean ERR_PTR(ret)); 22376a52e733SVladimir Oltean phylink_destroy(dp->pl); 22380d8bcdd3SFlorian Fainelli } 22399697f1cdSFlorian Fainelli 22406146dd45SVladimir Oltean return ret; 2241b31f65fbSAndrew Lunn } 22420d8bcdd3SFlorian Fainelli 224353da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave) 224453da0ebaSVladimir Oltean { 224553da0ebaSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(slave); 224653da0ebaSVladimir Oltean struct dsa_slave_priv *p = netdev_priv(slave); 224753da0ebaSVladimir Oltean const struct dsa_port *cpu_dp = dp->cpu_dp; 224853da0ebaSVladimir Oltean struct net_device *master = cpu_dp->master; 224958adf9dcSVladimir Oltean const struct dsa_switch *ds = dp->ds; 225053da0ebaSVladimir Oltean 22514e500251SVladimir Oltean slave->needed_headroom = cpu_dp->tag_ops->needed_headroom; 22524e500251SVladimir Oltean slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom; 225353da0ebaSVladimir Oltean /* Try to save one extra realloc later in the TX path (in the master) 225453da0ebaSVladimir Oltean * by also inheriting the master's needed headroom and tailroom. 225553da0ebaSVladimir Oltean * The 8021q driver also does this. 225653da0ebaSVladimir Oltean */ 225753da0ebaSVladimir Oltean slave->needed_headroom += master->needed_headroom; 225853da0ebaSVladimir Oltean slave->needed_tailroom += master->needed_tailroom; 225953da0ebaSVladimir Oltean 226053da0ebaSVladimir Oltean p->xmit = cpu_dp->tag_ops->xmit; 226121cf377aSLino Sanfilippo 226221cf377aSLino Sanfilippo slave->features = master->vlan_features | NETIF_F_HW_TC; 226321cf377aSLino Sanfilippo slave->hw_features |= NETIF_F_HW_TC; 226421cf377aSLino Sanfilippo slave->features |= NETIF_F_LLTX; 226521cf377aSLino Sanfilippo if (slave->needed_tailroom) 226621cf377aSLino Sanfilippo slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST); 226758adf9dcSVladimir Oltean if (ds->needs_standalone_vlan_filtering) 226858adf9dcSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 226953da0ebaSVladimir Oltean } 227053da0ebaSVladimir Oltean 227124462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 227224462549SFlorian Fainelli { 2273aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 227424462549SFlorian Fainelli 2275a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2276a94c689eSFlorian Fainelli return 0; 2277a94c689eSFlorian Fainelli 2278f154be24SFlorian Fainelli netif_device_detach(slave_dev); 2279f154be24SFlorian Fainelli 2280aab9c406SFlorian Fainelli rtnl_lock(); 2281aab9c406SFlorian Fainelli phylink_stop(dp->pl); 2282aab9c406SFlorian Fainelli rtnl_unlock(); 228324462549SFlorian Fainelli 228424462549SFlorian Fainelli return 0; 228524462549SFlorian Fainelli } 228624462549SFlorian Fainelli 228724462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 228824462549SFlorian Fainelli { 2289aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2290aab9c406SFlorian Fainelli 2291a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2292a94c689eSFlorian Fainelli return 0; 2293a94c689eSFlorian Fainelli 229424462549SFlorian Fainelli netif_device_attach(slave_dev); 229524462549SFlorian Fainelli 2296aab9c406SFlorian Fainelli rtnl_lock(); 2297aab9c406SFlorian Fainelli phylink_start(dp->pl); 2298aab9c406SFlorian Fainelli rtnl_unlock(); 229924462549SFlorian Fainelli 230024462549SFlorian Fainelli return 0; 230124462549SFlorian Fainelli } 230224462549SFlorian Fainelli 2303951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 230491da11f8SLennert Buytenhek { 230524a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 2306f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 23074cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 2308951259aaSVivien Didelot const char *name = port->name; 230991da11f8SLennert Buytenhek struct net_device *slave_dev; 231091da11f8SLennert Buytenhek struct dsa_slave_priv *p; 231191da11f8SLennert Buytenhek int ret; 231291da11f8SLennert Buytenhek 231355199df6SFlorian Fainelli if (!ds->num_tx_queues) 231455199df6SFlorian Fainelli ds->num_tx_queues = 1; 231555199df6SFlorian Fainelli 231655199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 231755199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 231855199df6SFlorian Fainelli ds->num_tx_queues, 1); 231991da11f8SLennert Buytenhek if (slave_dev == NULL) 2320d87d6f44SGuenter Roeck return -ENOMEM; 232191da11f8SLennert Buytenhek 23227ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 2323d538eca8SVladimir Oltean #if IS_ENABLED(CONFIG_DCB) 2324d538eca8SVladimir Oltean slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops; 2325d538eca8SVladimir Oltean #endif 232683216e39SMichael Walle if (!is_zero_ether_addr(port->mac)) 2327e35b8d7dSJakub Kicinski eth_hw_addr_set(slave_dev, port->mac); 2328a2c7023fSXiaofei Shen else 23292fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 23300a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 23315e8a1e03SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 23325e8a1e03SVladimir Oltean slave_dev->priv_flags |= IFF_UNICAST_FLT; 23333e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 2334bfcb8132SVladimir Oltean if (ds->ops->port_max_mtu) 2335bfcb8132SVladimir Oltean slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 2336f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 2337d442ad4aSStephen Hemminger 23384cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 23394cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 234091da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 234191da11f8SLennert Buytenhek 234291da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 23436a900628SHeiner Kallweit slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 23446a900628SHeiner Kallweit if (!slave_dev->tstats) { 23455f6b4e14SFlorian Fainelli free_netdev(slave_dev); 23465f6b4e14SFlorian Fainelli return -ENOMEM; 23475f6b4e14SFlorian Fainelli } 2348e131a563SAlexander Lobakin 2349e131a563SAlexander Lobakin ret = gro_cells_init(&p->gcells, slave_dev); 2350e131a563SAlexander Lobakin if (ret) 2351e131a563SAlexander Lobakin goto out_free; 2352e131a563SAlexander Lobakin 23534cfbf09cSVivien Didelot p->dp = port; 2354f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 2355f8b8b1cdSVivien Didelot port->slave = slave_dev; 235653da0ebaSVladimir Oltean dsa_slave_setup_tagger(slave_dev); 235791da11f8SLennert Buytenhek 235891da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 235991da11f8SLennert Buytenhek 23604fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 23610071f56eSAndrew Lunn if (ret) { 2362c9ebf126SVladimir Oltean netdev_err(slave_dev, 2363c9ebf126SVladimir Oltean "error %d setting up PHY for tree %d, switch %d, port %d\n", 2364c9ebf126SVladimir Oltean ret, ds->dst->index, ds->index, port->index); 2365e131a563SAlexander Lobakin goto out_gcells; 2366e804441cSFlorian Fainelli } 2367e804441cSFlorian Fainelli 23682f1e8ea7SVladimir Oltean rtnl_lock(); 2369e31dbd3bSVladimir Oltean 2370904e112aSVladimir Oltean ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 2371904e112aSVladimir Oltean if (ret && ret != -EOPNOTSUPP) 2372904e112aSVladimir Oltean dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 2373904e112aSVladimir Oltean ret, ETH_DATA_LEN, port->index); 2374904e112aSVladimir Oltean 23752f1e8ea7SVladimir Oltean ret = register_netdevice(slave_dev); 2376e804441cSFlorian Fainelli if (ret) { 2377e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 2378e804441cSFlorian Fainelli ret, slave_dev->name); 23792f1e8ea7SVladimir Oltean rtnl_unlock(); 2380e804441cSFlorian Fainelli goto out_phy; 23810071f56eSAndrew Lunn } 23820071f56eSAndrew Lunn 2383d538eca8SVladimir Oltean if (IS_ENABLED(CONFIG_DCB)) { 2384d538eca8SVladimir Oltean ret = dsa_slave_dcbnl_init(slave_dev); 2385d538eca8SVladimir Oltean if (ret) { 2386d538eca8SVladimir Oltean netdev_err(slave_dev, 2387d538eca8SVladimir Oltean "failed to initialize DCB: %pe\n", 2388d538eca8SVladimir Oltean ERR_PTR(ret)); 2389d538eca8SVladimir Oltean rtnl_unlock(); 2390d538eca8SVladimir Oltean goto out_unregister; 2391d538eca8SVladimir Oltean } 2392d538eca8SVladimir Oltean } 2393d538eca8SVladimir Oltean 23942f1e8ea7SVladimir Oltean ret = netdev_upper_dev_link(master, slave_dev, NULL); 23952f1e8ea7SVladimir Oltean 23962f1e8ea7SVladimir Oltean rtnl_unlock(); 23972f1e8ea7SVladimir Oltean 23982f1e8ea7SVladimir Oltean if (ret) 23992f1e8ea7SVladimir Oltean goto out_unregister; 24002f1e8ea7SVladimir Oltean 2401d87d6f44SGuenter Roeck return 0; 2402e804441cSFlorian Fainelli 24032f1e8ea7SVladimir Oltean out_unregister: 24042f1e8ea7SVladimir Oltean unregister_netdev(slave_dev); 2405e804441cSFlorian Fainelli out_phy: 2406aab9c406SFlorian Fainelli rtnl_lock(); 2407aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 2408aab9c406SFlorian Fainelli rtnl_unlock(); 2409aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 2410e131a563SAlexander Lobakin out_gcells: 2411e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 2412e804441cSFlorian Fainelli out_free: 24136a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2414e804441cSFlorian Fainelli free_netdev(slave_dev); 2415f8b8b1cdSVivien Didelot port->slave = NULL; 2416e804441cSFlorian Fainelli return ret; 241791da11f8SLennert Buytenhek } 2418b73adef6SFlorian Fainelli 2419cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 2420cda5c15bSNeil Armstrong { 24212f1e8ea7SVladimir Oltean struct net_device *master = dsa_slave_to_master(slave_dev); 2422d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2423cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 2424cda5c15bSNeil Armstrong 2425cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 2426aab9c406SFlorian Fainelli rtnl_lock(); 24272f1e8ea7SVladimir Oltean netdev_upper_dev_unlink(master, slave_dev); 24282f1e8ea7SVladimir Oltean unregister_netdevice(slave_dev); 2429aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 2430aab9c406SFlorian Fainelli rtnl_unlock(); 2431881eadabSJohan Hovold 2432aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 2433e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 24346a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2435cda5c15bSNeil Armstrong free_netdev(slave_dev); 2436cda5c15bSNeil Armstrong } 2437cda5c15bSNeil Armstrong 24384d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev) 2439b73adef6SFlorian Fainelli { 2440b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 2441b73adef6SFlorian Fainelli } 2442a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2443b73adef6SFlorian Fainelli 24448e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 24458e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 2446b73adef6SFlorian Fainelli { 2447d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 24482afc526aSVladimir Oltean struct netlink_ext_ack *extack; 24498e92ab3aSVivien Didelot int err = NOTIFY_DONE; 2450b73adef6SFlorian Fainelli 24512afc526aSVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 24522afc526aSVladimir Oltean 24538e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 24548e92ab3aSVivien Didelot if (info->linking) { 24552afc526aSVladimir Oltean err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2456bff33f7eSVladimir Oltean if (!err) 2457bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 245867b5fb5dSVladimir Oltean if (err == -EOPNOTSUPP) { 245967b5fb5dSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 246067b5fb5dSVladimir Oltean "Offloading not supported"); 246167b5fb5dSVladimir Oltean err = 0; 246267b5fb5dSVladimir Oltean } 24638e92ab3aSVivien Didelot err = notifier_from_errno(err); 24648e92ab3aSVivien Didelot } else { 246517d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 24668e92ab3aSVivien Didelot err = NOTIFY_OK; 24678e92ab3aSVivien Didelot } 2468058102a6STobias Waldekranz } else if (netif_is_lag_master(info->upper_dev)) { 2469058102a6STobias Waldekranz if (info->linking) { 2470058102a6STobias Waldekranz err = dsa_port_lag_join(dp, info->upper_dev, 24712afc526aSVladimir Oltean info->upper_info, extack); 2472058102a6STobias Waldekranz if (err == -EOPNOTSUPP) { 2473058102a6STobias Waldekranz NL_SET_ERR_MSG_MOD(info->info.extack, 2474058102a6STobias Waldekranz "Offloading not supported"); 2475058102a6STobias Waldekranz err = 0; 2476058102a6STobias Waldekranz } 2477058102a6STobias Waldekranz err = notifier_from_errno(err); 2478058102a6STobias Waldekranz } else { 2479058102a6STobias Waldekranz dsa_port_lag_leave(dp, info->upper_dev); 2480058102a6STobias Waldekranz err = NOTIFY_OK; 2481058102a6STobias Waldekranz } 248218596f50SGeorge McCollister } else if (is_hsr_master(info->upper_dev)) { 248318596f50SGeorge McCollister if (info->linking) { 248418596f50SGeorge McCollister err = dsa_port_hsr_join(dp, info->upper_dev); 248518596f50SGeorge McCollister if (err == -EOPNOTSUPP) { 248618596f50SGeorge McCollister NL_SET_ERR_MSG_MOD(info->info.extack, 248718596f50SGeorge McCollister "Offloading not supported"); 248818596f50SGeorge McCollister err = 0; 248918596f50SGeorge McCollister } 249018596f50SGeorge McCollister err = notifier_from_errno(err); 249118596f50SGeorge McCollister } else { 249218596f50SGeorge McCollister dsa_port_hsr_leave(dp, info->upper_dev); 249318596f50SGeorge McCollister err = NOTIFY_OK; 249418596f50SGeorge McCollister } 2495058102a6STobias Waldekranz } 2496058102a6STobias Waldekranz 2497058102a6STobias Waldekranz return err; 2498058102a6STobias Waldekranz } 2499058102a6STobias Waldekranz 250074918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev, 250174918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 250274918945SVladimir Oltean { 250374918945SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 250474918945SVladimir Oltean 250574918945SVladimir Oltean if (netif_is_bridge_master(info->upper_dev) && !info->linking) 25064e51bf44SVladimir Oltean dsa_port_pre_bridge_leave(dp, info->upper_dev); 250774918945SVladimir Oltean else if (netif_is_lag_master(info->upper_dev) && !info->linking) 25084e51bf44SVladimir Oltean dsa_port_pre_lag_leave(dp, info->upper_dev); 250974918945SVladimir Oltean /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be 251074918945SVladimir Oltean * meaningfully enslaved to a bridge yet 251174918945SVladimir Oltean */ 251274918945SVladimir Oltean 25134e51bf44SVladimir Oltean return NOTIFY_DONE; 251474918945SVladimir Oltean } 251574918945SVladimir Oltean 2516058102a6STobias Waldekranz static int 2517058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev, 2518058102a6STobias Waldekranz struct netdev_notifier_changeupper_info *info) 2519058102a6STobias Waldekranz { 2520058102a6STobias Waldekranz struct net_device *lower; 2521058102a6STobias Waldekranz struct list_head *iter; 2522058102a6STobias Waldekranz int err = NOTIFY_DONE; 2523058102a6STobias Waldekranz struct dsa_port *dp; 2524058102a6STobias Waldekranz 2525058102a6STobias Waldekranz netdev_for_each_lower_dev(dev, lower, iter) { 2526058102a6STobias Waldekranz if (!dsa_slave_dev_check(lower)) 2527058102a6STobias Waldekranz continue; 2528058102a6STobias Waldekranz 2529058102a6STobias Waldekranz dp = dsa_slave_to_port(lower); 2530dedd6a00SVladimir Oltean if (!dp->lag) 2531058102a6STobias Waldekranz /* Software LAG */ 2532058102a6STobias Waldekranz continue; 2533058102a6STobias Waldekranz 2534058102a6STobias Waldekranz err = dsa_slave_changeupper(lower, info); 2535058102a6STobias Waldekranz if (notifier_to_errno(err)) 2536058102a6STobias Waldekranz break; 25376debb68aSVivien Didelot } 2538b73adef6SFlorian Fainelli 25398e92ab3aSVivien Didelot return err; 2540b73adef6SFlorian Fainelli } 2541b73adef6SFlorian Fainelli 254274918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls 254374918945SVladimir Oltean * dsa_slave_prechangeupper() 254474918945SVladimir Oltean */ 254574918945SVladimir Oltean static int 254674918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev, 254774918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 254874918945SVladimir Oltean { 254974918945SVladimir Oltean struct net_device *lower; 255074918945SVladimir Oltean struct list_head *iter; 255174918945SVladimir Oltean int err = NOTIFY_DONE; 255274918945SVladimir Oltean struct dsa_port *dp; 255374918945SVladimir Oltean 255474918945SVladimir Oltean netdev_for_each_lower_dev(dev, lower, iter) { 255574918945SVladimir Oltean if (!dsa_slave_dev_check(lower)) 255674918945SVladimir Oltean continue; 255774918945SVladimir Oltean 255874918945SVladimir Oltean dp = dsa_slave_to_port(lower); 2559dedd6a00SVladimir Oltean if (!dp->lag) 256074918945SVladimir Oltean /* Software LAG */ 256174918945SVladimir Oltean continue; 256274918945SVladimir Oltean 256374918945SVladimir Oltean err = dsa_slave_prechangeupper(lower, info); 256474918945SVladimir Oltean if (notifier_to_errno(err)) 256574918945SVladimir Oltean break; 256674918945SVladimir Oltean } 256774918945SVladimir Oltean 256874918945SVladimir Oltean return err; 256974918945SVladimir Oltean } 257074918945SVladimir Oltean 2571eb46e8daSVladimir Oltean static int 2572eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2573eb46e8daSVladimir Oltean struct netdev_notifier_changeupper_info *info) 2574cc1d5bdaSFlorian Fainelli { 2575cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 257636cbf39bSVladimir Oltean struct net_device *slave, *br; 2577cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 2578cc1d5bdaSFlorian Fainelli 2579cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 2580cc1d5bdaSFlorian Fainelli 2581cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 2582cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2583cc1d5bdaSFlorian Fainelli 2584cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 2585cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 2586cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2587cc1d5bdaSFlorian Fainelli 2588cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 258936cbf39bSVladimir Oltean br = dsa_port_bridge_dev_get(dp); 259036cbf39bSVladimir Oltean if (!br) 2591cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2592cc1d5bdaSFlorian Fainelli 2593cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 259436cbf39bSVladimir Oltean if (br_vlan_enabled(br) && 2595cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 2596cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 2597cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 2598cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 2599cc1d5bdaSFlorian Fainelli } 2600cc1d5bdaSFlorian Fainelli 2601cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2602cc1d5bdaSFlorian Fainelli } 2603cc1d5bdaSFlorian Fainelli 26042b138406SVladimir Oltean static int 26052b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev, 26062b138406SVladimir Oltean struct netdev_notifier_changeupper_info *info) 26072b138406SVladimir Oltean { 26082b138406SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 260936cbf39bSVladimir Oltean struct net_device *br = dsa_port_bridge_dev_get(dp); 26102b138406SVladimir Oltean struct bridge_vlan_info br_info; 26112b138406SVladimir Oltean struct netlink_ext_ack *extack; 26122b138406SVladimir Oltean int err = NOTIFY_DONE; 26132b138406SVladimir Oltean u16 vid; 26142b138406SVladimir Oltean 2615adb256ebSVladimir Oltean if (!br || !br_vlan_enabled(br)) 26162b138406SVladimir Oltean return NOTIFY_DONE; 26172b138406SVladimir Oltean 26182b138406SVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 26192b138406SVladimir Oltean vid = vlan_dev_vlan_id(info->upper_dev); 26202b138406SVladimir Oltean 26212b138406SVladimir Oltean /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 26222b138406SVladimir Oltean * device, respectively the VID is not found, returning 26232b138406SVladimir Oltean * 0 means success, which is a failure for us here. 26242b138406SVladimir Oltean */ 26252b138406SVladimir Oltean err = br_vlan_get_info(br, vid, &br_info); 26262b138406SVladimir Oltean if (err == 0) { 26272b138406SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 26282b138406SVladimir Oltean "This VLAN is already configured by the bridge"); 26292b138406SVladimir Oltean return notifier_from_errno(-EBUSY); 26302b138406SVladimir Oltean } 26312b138406SVladimir Oltean 26322b138406SVladimir Oltean return NOTIFY_DONE; 26332b138406SVladimir Oltean } 26342b138406SVladimir Oltean 26354ede74e7SVladimir Oltean static int 26364ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev, 26374ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info) 2638b73adef6SFlorian Fainelli { 2639e358bef7SVladimir Oltean struct dsa_switch *ds; 2640e358bef7SVladimir Oltean struct dsa_port *dp; 2641e358bef7SVladimir Oltean int err; 26422b138406SVladimir Oltean 264353bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 26444ede74e7SVladimir Oltean return dsa_prevent_bridging_8021q_upper(dev, info); 26452b138406SVladimir Oltean 2646e358bef7SVladimir Oltean dp = dsa_slave_to_port(dev); 2647e358bef7SVladimir Oltean ds = dp->ds; 2648e358bef7SVladimir Oltean 2649e358bef7SVladimir Oltean if (ds->ops->port_prechangeupper) { 2650e358bef7SVladimir Oltean err = ds->ops->port_prechangeupper(ds, dp->index, info); 2651e358bef7SVladimir Oltean if (err) 2652e358bef7SVladimir Oltean return notifier_from_errno(err); 2653e358bef7SVladimir Oltean } 2654e358bef7SVladimir Oltean 26552b138406SVladimir Oltean if (is_vlan_dev(info->upper_dev)) 26564ede74e7SVladimir Oltean return dsa_slave_check_8021q_upper(dev, info); 26574ede74e7SVladimir Oltean 26584ede74e7SVladimir Oltean return NOTIFY_DONE; 26594ede74e7SVladimir Oltean } 26604ede74e7SVladimir Oltean 26614ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb, 26624ede74e7SVladimir Oltean unsigned long event, void *ptr) 26634ede74e7SVladimir Oltean { 26644ede74e7SVladimir Oltean struct net_device *dev = netdev_notifier_info_to_dev(ptr); 26654ede74e7SVladimir Oltean 26664ede74e7SVladimir Oltean switch (event) { 26674ede74e7SVladimir Oltean case NETDEV_PRECHANGEUPPER: { 26684ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info = ptr; 26694ede74e7SVladimir Oltean int err; 26704ede74e7SVladimir Oltean 26714ede74e7SVladimir Oltean err = dsa_slave_prechangeupper_sanity_check(dev, info); 26724ede74e7SVladimir Oltean if (err != NOTIFY_DONE) 26734ede74e7SVladimir Oltean return err; 26744ede74e7SVladimir Oltean 267574918945SVladimir Oltean if (dsa_slave_dev_check(dev)) 267674918945SVladimir Oltean return dsa_slave_prechangeupper(dev, ptr); 267774918945SVladimir Oltean 267874918945SVladimir Oltean if (netif_is_lag_master(dev)) 267974918945SVladimir Oltean return dsa_slave_lag_prechangeupper(dev, ptr); 268074918945SVladimir Oltean 268183501299SVladimir Oltean break; 26822b138406SVladimir Oltean } 268383501299SVladimir Oltean case NETDEV_CHANGEUPPER: 2684058102a6STobias Waldekranz if (dsa_slave_dev_check(dev)) 26858e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 2686058102a6STobias Waldekranz 2687058102a6STobias Waldekranz if (netif_is_lag_master(dev)) 2688058102a6STobias Waldekranz return dsa_slave_lag_changeupper(dev, ptr); 2689058102a6STobias Waldekranz 2690058102a6STobias Waldekranz break; 2691058102a6STobias Waldekranz case NETDEV_CHANGELOWERSTATE: { 2692058102a6STobias Waldekranz struct netdev_notifier_changelowerstate_info *info = ptr; 2693058102a6STobias Waldekranz struct dsa_port *dp; 2694058102a6STobias Waldekranz int err; 2695058102a6STobias Waldekranz 2696058102a6STobias Waldekranz if (!dsa_slave_dev_check(dev)) 2697058102a6STobias Waldekranz break; 2698058102a6STobias Waldekranz 2699058102a6STobias Waldekranz dp = dsa_slave_to_port(dev); 2700058102a6STobias Waldekranz 2701058102a6STobias Waldekranz err = dsa_port_lag_change(dp, info->lower_state_info); 2702058102a6STobias Waldekranz return notifier_from_errno(err); 2703058102a6STobias Waldekranz } 2704295ab96fSVladimir Oltean case NETDEV_CHANGE: 2705295ab96fSVladimir Oltean case NETDEV_UP: { 2706295ab96fSVladimir Oltean /* Track state of master port. 2707295ab96fSVladimir Oltean * DSA driver may require the master port (and indirectly 2708295ab96fSVladimir Oltean * the tagger) to be available for some special operation. 2709295ab96fSVladimir Oltean */ 2710295ab96fSVladimir Oltean if (netdev_uses_dsa(dev)) { 2711295ab96fSVladimir Oltean struct dsa_port *cpu_dp = dev->dsa_ptr; 2712295ab96fSVladimir Oltean struct dsa_switch_tree *dst = cpu_dp->ds->dst; 2713295ab96fSVladimir Oltean 2714295ab96fSVladimir Oltean /* Track when the master port is UP */ 2715295ab96fSVladimir Oltean dsa_tree_master_oper_state_change(dst, dev, 2716295ab96fSVladimir Oltean netif_oper_up(dev)); 2717295ab96fSVladimir Oltean 2718295ab96fSVladimir Oltean /* Track when the master port is ready and can accept 2719295ab96fSVladimir Oltean * packet. 2720295ab96fSVladimir Oltean * NETDEV_UP event is not enough to flag a port as ready. 2721295ab96fSVladimir Oltean * We also have to wait for linkwatch_do_dev to dev_activate 2722295ab96fSVladimir Oltean * and emit a NETDEV_CHANGE event. 2723295ab96fSVladimir Oltean * We check if a master port is ready by checking if the dev 2724295ab96fSVladimir Oltean * have a qdisc assigned and is not noop. 2725295ab96fSVladimir Oltean */ 2726295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, 2727295ab96fSVladimir Oltean !qdisc_tx_is_noop(dev)); 2728295ab96fSVladimir Oltean 2729295ab96fSVladimir Oltean return NOTIFY_OK; 2730295ab96fSVladimir Oltean } 2731295ab96fSVladimir Oltean 2732295ab96fSVladimir Oltean return NOTIFY_DONE; 2733295ab96fSVladimir Oltean } 2734c0a8a9c2SVladimir Oltean case NETDEV_GOING_DOWN: { 2735c0a8a9c2SVladimir Oltean struct dsa_port *dp, *cpu_dp; 2736c0a8a9c2SVladimir Oltean struct dsa_switch_tree *dst; 2737c0a8a9c2SVladimir Oltean LIST_HEAD(close_list); 2738c0a8a9c2SVladimir Oltean 2739c0a8a9c2SVladimir Oltean if (!netdev_uses_dsa(dev)) 2740c0a8a9c2SVladimir Oltean return NOTIFY_DONE; 2741c0a8a9c2SVladimir Oltean 2742c0a8a9c2SVladimir Oltean cpu_dp = dev->dsa_ptr; 2743c0a8a9c2SVladimir Oltean dst = cpu_dp->ds->dst; 2744c0a8a9c2SVladimir Oltean 2745295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, false); 2746295ab96fSVladimir Oltean 2747c0a8a9c2SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 2748d0004a02SVladimir Oltean if (!dsa_port_is_user(dp)) 2749c0a8a9c2SVladimir Oltean continue; 2750c0a8a9c2SVladimir Oltean 2751c0a8a9c2SVladimir Oltean list_add(&dp->slave->close_list, &close_list); 2752c0a8a9c2SVladimir Oltean } 2753c0a8a9c2SVladimir Oltean 2754c0a8a9c2SVladimir Oltean dev_close_many(&close_list, true); 2755c0a8a9c2SVladimir Oltean 2756c0a8a9c2SVladimir Oltean return NOTIFY_OK; 2757c0a8a9c2SVladimir Oltean } 2758c0a8a9c2SVladimir Oltean default: 2759c0a8a9c2SVladimir Oltean break; 2760cc1d5bdaSFlorian Fainelli } 2761b73adef6SFlorian Fainelli 2762b73adef6SFlorian Fainelli return NOTIFY_DONE; 2763b73adef6SFlorian Fainelli } 276488e4f0caSVivien Didelot 2765c4bb76a9SVladimir Oltean static void 2766c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2767c4bb76a9SVladimir Oltean { 2768c35b57ceSVladimir Oltean struct switchdev_notifier_fdb_info info = {}; 2769c4bb76a9SVladimir Oltean 2770c4bb76a9SVladimir Oltean info.addr = switchdev_work->addr; 2771c4bb76a9SVladimir Oltean info.vid = switchdev_work->vid; 2772c4bb76a9SVladimir Oltean info.offloaded = true; 2773c4bb76a9SVladimir Oltean call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 277493c79823SVladimir Oltean switchdev_work->orig_dev, &info.info, NULL); 2775c4bb76a9SVladimir Oltean } 2776c9eb3e0fSArkadi Sharshevsky 2777c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 2778c9eb3e0fSArkadi Sharshevsky { 2779c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 2780c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 278168d6d71eSVladimir Oltean const unsigned char *addr = switchdev_work->addr; 2782e35f12e9SVladimir Oltean struct net_device *dev = switchdev_work->dev; 278368d6d71eSVladimir Oltean u16 vid = switchdev_work->vid; 2784e35f12e9SVladimir Oltean struct dsa_switch *ds; 2785c4bb76a9SVladimir Oltean struct dsa_port *dp; 2786c9eb3e0fSArkadi Sharshevsky int err; 2787c9eb3e0fSArkadi Sharshevsky 2788e35f12e9SVladimir Oltean dp = dsa_slave_to_port(dev); 2789e35f12e9SVladimir Oltean ds = dp->ds; 2790c4bb76a9SVladimir Oltean 2791c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 2792c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 27933dc80afcSVladimir Oltean if (switchdev_work->host_addr) 279468d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_add(dp, addr, vid); 2795e212fa7cSVladimir Oltean else if (dp->lag) 279668d6d71eSVladimir Oltean err = dsa_port_lag_fdb_add(dp, addr, vid); 27973dc80afcSVladimir Oltean else 279868d6d71eSVladimir Oltean err = dsa_port_fdb_add(dp, addr, vid); 2799c9eb3e0fSArkadi Sharshevsky if (err) { 2800c4bb76a9SVladimir Oltean dev_err(ds->dev, 2801c4bb76a9SVladimir Oltean "port %d failed to add %pM vid %d to fdb: %d\n", 280268d6d71eSVladimir Oltean dp->index, addr, vid, err); 2803c9eb3e0fSArkadi Sharshevsky break; 2804c9eb3e0fSArkadi Sharshevsky } 2805c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(switchdev_work); 2806c9eb3e0fSArkadi Sharshevsky break; 2807c9eb3e0fSArkadi Sharshevsky 2808c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 28093dc80afcSVladimir Oltean if (switchdev_work->host_addr) 281068d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_del(dp, addr, vid); 2811e212fa7cSVladimir Oltean else if (dp->lag) 281268d6d71eSVladimir Oltean err = dsa_port_lag_fdb_del(dp, addr, vid); 28133dc80afcSVladimir Oltean else 281468d6d71eSVladimir Oltean err = dsa_port_fdb_del(dp, addr, vid); 2815c9eb3e0fSArkadi Sharshevsky if (err) { 2816c4bb76a9SVladimir Oltean dev_err(ds->dev, 2817c4bb76a9SVladimir Oltean "port %d failed to delete %pM vid %d from fdb: %d\n", 281868d6d71eSVladimir Oltean dp->index, addr, vid, err); 2819c9eb3e0fSArkadi Sharshevsky } 28202fd18650SVladimir Oltean 2821c9eb3e0fSArkadi Sharshevsky break; 2822c9eb3e0fSArkadi Sharshevsky } 2823c9eb3e0fSArkadi Sharshevsky 2824c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 2825c9eb3e0fSArkadi Sharshevsky } 2826c9eb3e0fSArkadi Sharshevsky 2827b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev, 2828b94dc99cSVladimir Oltean const struct net_device *foreign_dev) 2829d5f19486SVladimir Oltean { 2830b94dc99cSVladimir Oltean const struct dsa_port *dp = dsa_slave_to_port(dev); 2831b94dc99cSVladimir Oltean struct dsa_switch_tree *dst = dp->ds->dst; 2832b94dc99cSVladimir Oltean 2833b94dc99cSVladimir Oltean if (netif_is_bridge_master(foreign_dev)) 2834936db8a2SVladimir Oltean return !dsa_tree_offloads_bridge_dev(dst, foreign_dev); 2835b94dc99cSVladimir Oltean 2836b94dc99cSVladimir Oltean if (netif_is_bridge_port(foreign_dev)) 2837b94dc99cSVladimir Oltean return !dsa_tree_offloads_bridge_port(dst, foreign_dev); 2838b94dc99cSVladimir Oltean 2839b94dc99cSVladimir Oltean /* Everything else is foreign */ 2840b94dc99cSVladimir Oltean return true; 2841d5f19486SVladimir Oltean } 2842d5f19486SVladimir Oltean 2843b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev, 2844716a30a9SVladimir Oltean struct net_device *orig_dev, 2845716a30a9SVladimir Oltean unsigned long event, const void *ctx, 2846716a30a9SVladimir Oltean const struct switchdev_notifier_fdb_info *fdb_info) 2847b94dc99cSVladimir Oltean { 2848b94dc99cSVladimir Oltean struct dsa_switchdev_event_work *switchdev_work; 2849b94dc99cSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2850b94dc99cSVladimir Oltean bool host_addr = fdb_info->is_local; 2851b94dc99cSVladimir Oltean struct dsa_switch *ds = dp->ds; 2852b94dc99cSVladimir Oltean 2853b94dc99cSVladimir Oltean if (ctx && ctx != dp) 2854b94dc99cSVladimir Oltean return 0; 2855b94dc99cSVladimir Oltean 2856a860352eSTobias Waldekranz if (!dp->bridge) 2857a860352eSTobias Waldekranz return 0; 2858a860352eSTobias Waldekranz 2859e212fa7cSVladimir Oltean if (switchdev_fdb_is_dynamically_learned(fdb_info)) { 2860e212fa7cSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, orig_dev)) 2861b94dc99cSVladimir Oltean return 0; 2862b94dc99cSVladimir Oltean 2863e212fa7cSVladimir Oltean /* FDB entries learned by the software bridge or by foreign 2864e212fa7cSVladimir Oltean * bridge ports should be installed as host addresses only if 2865e212fa7cSVladimir Oltean * the driver requests assisted learning. 2866b94dc99cSVladimir Oltean */ 2867e212fa7cSVladimir Oltean if (!ds->assisted_learning_on_cpu_port) 2868b94dc99cSVladimir Oltean return 0; 2869e212fa7cSVladimir Oltean } 2870b94dc99cSVladimir Oltean 2871b94dc99cSVladimir Oltean /* Also treat FDB entries on foreign interfaces bridged with us as host 2872b94dc99cSVladimir Oltean * addresses. 2873b94dc99cSVladimir Oltean */ 2874b94dc99cSVladimir Oltean if (dsa_foreign_dev_check(dev, orig_dev)) 2875b94dc99cSVladimir Oltean host_addr = true; 2876b94dc99cSVladimir Oltean 2877e212fa7cSVladimir Oltean /* Check early that we're not doing work in vain. 2878e212fa7cSVladimir Oltean * Host addresses on LAG ports still require regular FDB ops, 2879e212fa7cSVladimir Oltean * since the CPU port isn't in a LAG. 2880e212fa7cSVladimir Oltean */ 2881e212fa7cSVladimir Oltean if (dp->lag && !host_addr) { 2882e212fa7cSVladimir Oltean if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del) 2883e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2884e212fa7cSVladimir Oltean } else { 2885e212fa7cSVladimir Oltean if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del) 2886e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2887e212fa7cSVladimir Oltean } 2888e212fa7cSVladimir Oltean 2889b94dc99cSVladimir Oltean switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2890b94dc99cSVladimir Oltean if (!switchdev_work) 2891b94dc99cSVladimir Oltean return -ENOMEM; 2892b94dc99cSVladimir Oltean 2893b94dc99cSVladimir Oltean netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n", 2894b94dc99cSVladimir Oltean event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting", 2895b94dc99cSVladimir Oltean orig_dev->name, fdb_info->addr, fdb_info->vid, 2896b94dc99cSVladimir Oltean host_addr ? " as host address" : ""); 2897b94dc99cSVladimir Oltean 2898b94dc99cSVladimir Oltean INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work); 2899b94dc99cSVladimir Oltean switchdev_work->event = event; 2900b94dc99cSVladimir Oltean switchdev_work->dev = dev; 290193c79823SVladimir Oltean switchdev_work->orig_dev = orig_dev; 2902b94dc99cSVladimir Oltean 2903b94dc99cSVladimir Oltean ether_addr_copy(switchdev_work->addr, fdb_info->addr); 2904b94dc99cSVladimir Oltean switchdev_work->vid = fdb_info->vid; 2905b94dc99cSVladimir Oltean switchdev_work->host_addr = host_addr; 2906b94dc99cSVladimir Oltean 2907b94dc99cSVladimir Oltean dsa_schedule_work(&switchdev_work->work); 2908b94dc99cSVladimir Oltean 2909d5f19486SVladimir Oltean return 0; 2910d5f19486SVladimir Oltean } 2911d5f19486SVladimir Oltean 2912c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 2913c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 2914c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 2915c9eb3e0fSArkadi Sharshevsky { 2916c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 291779b139f4SVivien Didelot int err; 291879b139f4SVivien Didelot 2919447d290aSVladimir Oltean switch (event) { 2920447d290aSVladimir Oltean case SWITCHDEV_PORT_ATTR_SET: 292179b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 292279b139f4SVivien Didelot dsa_slave_dev_check, 292379b139f4SVivien Didelot dsa_slave_port_attr_set); 292479b139f4SVivien Didelot return notifier_from_errno(err); 2925447d290aSVladimir Oltean case SWITCHDEV_FDB_ADD_TO_DEVICE: 2926447d290aSVladimir Oltean case SWITCHDEV_FDB_DEL_TO_DEVICE: 2927716a30a9SVladimir Oltean err = switchdev_handle_fdb_event_to_device(dev, event, ptr, 2928b94dc99cSVladimir Oltean dsa_slave_dev_check, 2929b94dc99cSVladimir Oltean dsa_foreign_dev_check, 2930ec638740SVladimir Oltean dsa_slave_fdb_event); 2931b94dc99cSVladimir Oltean return notifier_from_errno(err); 2932c9eb3e0fSArkadi Sharshevsky default: 2933c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 2934c9eb3e0fSArkadi Sharshevsky } 2935c9eb3e0fSArkadi Sharshevsky 2936c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 2937c9eb3e0fSArkadi Sharshevsky } 2938c9eb3e0fSArkadi Sharshevsky 29392b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 29402b239f67SPetr Machata unsigned long event, void *ptr) 29412b239f67SPetr Machata { 29422b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 294379b139f4SVivien Didelot int err; 29442b239f67SPetr Machata 29452b239f67SPetr Machata switch (event) { 294679b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 2947164f861bSVladimir Oltean err = switchdev_handle_port_obj_add_foreign(dev, ptr, 294879b139f4SVivien Didelot dsa_slave_dev_check, 2949164f861bSVladimir Oltean dsa_foreign_dev_check, 295079b139f4SVivien Didelot dsa_slave_port_obj_add); 295179b139f4SVivien Didelot return notifier_from_errno(err); 29522b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 2953164f861bSVladimir Oltean err = switchdev_handle_port_obj_del_foreign(dev, ptr, 295479b139f4SVivien Didelot dsa_slave_dev_check, 2955164f861bSVladimir Oltean dsa_foreign_dev_check, 295679b139f4SVivien Didelot dsa_slave_port_obj_del); 295779b139f4SVivien Didelot return notifier_from_errno(err); 29589ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 295979b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 296079b139f4SVivien Didelot dsa_slave_dev_check, 296179b139f4SVivien Didelot dsa_slave_port_attr_set); 296279b139f4SVivien Didelot return notifier_from_errno(err); 29632b239f67SPetr Machata } 29642b239f67SPetr Machata 29652b239f67SPetr Machata return NOTIFY_DONE; 29662b239f67SPetr Machata } 29672b239f67SPetr Machata 296888e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 296988e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 297088e4f0caSVivien Didelot }; 297188e4f0caSVivien Didelot 2972010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = { 2973c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 2974c9eb3e0fSArkadi Sharshevsky }; 2975c9eb3e0fSArkadi Sharshevsky 2976010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = { 29772b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 29782b239f67SPetr Machata }; 29792b239f67SPetr Machata 298088e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 298188e4f0caSVivien Didelot { 29822b239f67SPetr Machata struct notifier_block *nb; 2983c9eb3e0fSArkadi Sharshevsky int err; 2984c9eb3e0fSArkadi Sharshevsky 2985c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 2986c9eb3e0fSArkadi Sharshevsky if (err) 2987c9eb3e0fSArkadi Sharshevsky return err; 2988c9eb3e0fSArkadi Sharshevsky 2989c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 2990c9eb3e0fSArkadi Sharshevsky if (err) 2991c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 2992c9eb3e0fSArkadi Sharshevsky 29932b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 29942b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 29952b239f67SPetr Machata if (err) 29962b239f67SPetr Machata goto err_switchdev_blocking_nb; 29972b239f67SPetr Machata 2998c9eb3e0fSArkadi Sharshevsky return 0; 2999c9eb3e0fSArkadi Sharshevsky 30002b239f67SPetr Machata err_switchdev_blocking_nb: 30012b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3002c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 3003c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 3004c9eb3e0fSArkadi Sharshevsky return err; 300588e4f0caSVivien Didelot } 300688e4f0caSVivien Didelot 300788e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 300888e4f0caSVivien Didelot { 30092b239f67SPetr Machata struct notifier_block *nb; 301088e4f0caSVivien Didelot int err; 301188e4f0caSVivien Didelot 30122b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 30132b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 30142b239f67SPetr Machata if (err) 30152b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 30162b239f67SPetr Machata 3017c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3018c9eb3e0fSArkadi Sharshevsky if (err) 3019c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 3020c9eb3e0fSArkadi Sharshevsky 302188e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 302288e4f0caSVivien Didelot if (err) 302388e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 302488e4f0caSVivien Didelot } 3025