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; 467ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 46803cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 46903cbb870SVladimir Oltean return -EOPNOTSUPP; 47003cbb870SVladimir Oltean 471a8b659e7SVladimir Oltean ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 472a8b659e7SVladimir Oltean extack); 473ea87005aSFlorian Fainelli break; 47457652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 47503cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 47603cbb870SVladimir Oltean return -EOPNOTSUPP; 47703cbb870SVladimir Oltean 478a8b659e7SVladimir Oltean ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack); 47957652796SRussell King break; 48035636062SScott Feldman default: 48135636062SScott Feldman ret = -EOPNOTSUPP; 48235636062SScott Feldman break; 48335636062SScott Feldman } 48435636062SScott Feldman 48535636062SScott Feldman return ret; 48635636062SScott Feldman } 48735636062SScott Feldman 4881ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */ 4891ce39f0eSVladimir Oltean static int 4901ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 4911ce39f0eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 4921ce39f0eSVladimir Oltean { 4931ce39f0eSVladimir Oltean struct net_device *upper_dev; 4941ce39f0eSVladimir Oltean struct list_head *iter; 4951ce39f0eSVladimir Oltean 4961ce39f0eSVladimir Oltean netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 4971ce39f0eSVladimir Oltean u16 vid; 4981ce39f0eSVladimir Oltean 4991ce39f0eSVladimir Oltean if (!is_vlan_dev(upper_dev)) 5001ce39f0eSVladimir Oltean continue; 5011ce39f0eSVladimir Oltean 5021ce39f0eSVladimir Oltean vid = vlan_dev_vlan_id(upper_dev); 503b7a9e0daSVladimir Oltean if (vid == vlan->vid) 5041ce39f0eSVladimir Oltean return -EBUSY; 5051ce39f0eSVladimir Oltean } 5061ce39f0eSVladimir Oltean 5071ce39f0eSVladimir Oltean return 0; 5081ce39f0eSVladimir Oltean } 5091ce39f0eSVladimir Oltean 510bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev, 5110ee2af4eSVladimir Oltean const struct switchdev_obj *obj, 5120ee2af4eSVladimir Oltean struct netlink_ext_ack *extack) 513bdcff080SVivien Didelot { 514bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 515134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 516b7a9e0daSVladimir Oltean int err; 517bdcff080SVivien Didelot 5180ee2af4eSVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 5190ee2af4eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 520c5335d73SVivien Didelot return 0; 5210ee2af4eSVladimir Oltean } 522c5335d73SVivien Didelot 523134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 524bdcff080SVivien Didelot 5251ce39f0eSVladimir Oltean /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 5261ce39f0eSVladimir Oltean * the same VID. 5271ce39f0eSVladimir Oltean */ 52836cbf39bSVladimir Oltean if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) { 5291ce39f0eSVladimir Oltean rcu_read_lock(); 530134ef238SVladimir Oltean err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan); 5311ce39f0eSVladimir Oltean rcu_read_unlock(); 53231046a5fSVladimir Oltean if (err) { 53331046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 53431046a5fSVladimir Oltean "Port already has a VLAN upper with this VID"); 5351ce39f0eSVladimir Oltean return err; 5361ce39f0eSVladimir Oltean } 53731046a5fSVladimir Oltean } 5381ce39f0eSVladimir Oltean 539134ef238SVladimir Oltean return dsa_port_vlan_add(dp, vlan, extack); 540134ef238SVladimir Oltean } 541bdcff080SVivien Didelot 542164f861bSVladimir Oltean /* Offload a VLAN installed on the bridge or on a foreign interface by 543164f861bSVladimir Oltean * installing it as a VLAN towards the CPU port. 544164f861bSVladimir Oltean */ 545134ef238SVladimir Oltean static int dsa_slave_host_vlan_add(struct net_device *dev, 546134ef238SVladimir Oltean const struct switchdev_obj *obj, 547134ef238SVladimir Oltean struct netlink_ext_ack *extack) 548134ef238SVladimir Oltean { 549134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 550134ef238SVladimir Oltean struct switchdev_obj_port_vlan vlan; 551134ef238SVladimir Oltean 552164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 553164f861bSVladimir Oltean if (!dp->bridge) 554164f861bSVladimir Oltean return -EOPNOTSUPP; 555164f861bSVladimir Oltean 556134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 557134ef238SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 558134ef238SVladimir Oltean return 0; 559134ef238SVladimir Oltean } 560134ef238SVladimir Oltean 561134ef238SVladimir Oltean vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 562134ef238SVladimir Oltean 563134ef238SVladimir Oltean /* Even though drivers often handle CPU membership in special ways, 564b9499904SVivien Didelot * it doesn't make sense to program a PVID, so clear this flag. 565b9499904SVivien Didelot */ 566b9499904SVivien Didelot vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 567b9499904SVivien Didelot 568134ef238SVladimir Oltean return dsa_port_host_vlan_add(dp, &vlan, extack); 569bdcff080SVivien Didelot } 570bdcff080SVivien Didelot 57169bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx, 572648b4a99SJiri Pirko const struct switchdev_obj *obj, 57379b139f4SVivien Didelot struct netlink_ext_ack *extack) 574ba14d9ebSVivien Didelot { 575d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 576ba14d9ebSVivien Didelot int err; 577ba14d9ebSVivien Didelot 5780d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 5790d2cfbd4SVladimir Oltean return 0; 5800d2cfbd4SVladimir Oltean 5819e8f4a54SJiri Pirko switch (obj->id) { 5828df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 58303cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 58479b139f4SVivien Didelot return -EOPNOTSUPP; 58503cbb870SVladimir Oltean 586ffb68fc5SVladimir Oltean err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 5878df30255SVivien Didelot break; 5885f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 589936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 59003cbb870SVladimir Oltean return -EOPNOTSUPP; 59103cbb870SVladimir Oltean 59268d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 5935f4dbc50SAndrew Lunn break; 59457d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 595164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 5960ee2af4eSVladimir Oltean err = dsa_slave_vlan_add(dev, obj, extack); 597164f861bSVladimir Oltean else 598164f861bSVladimir Oltean err = dsa_slave_host_vlan_add(dev, obj, extack); 59911149536SVivien Didelot break; 600c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 601936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 602c595c433SHoratiu Vultur return -EOPNOTSUPP; 60303cbb870SVladimir Oltean 604c595c433SHoratiu Vultur err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj)); 605c595c433SHoratiu Vultur break; 606c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_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_ring_role(dp, 611c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 612c595c433SHoratiu Vultur break; 613ba14d9ebSVivien Didelot default: 614ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 615ba14d9ebSVivien Didelot break; 616ba14d9ebSVivien Didelot } 617ba14d9ebSVivien Didelot 618ba14d9ebSVivien Didelot return err; 619ba14d9ebSVivien Didelot } 620ba14d9ebSVivien Didelot 621bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev, 622bdcff080SVivien Didelot const struct switchdev_obj *obj) 623bdcff080SVivien Didelot { 624bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 6252209158cSVladimir Oltean struct switchdev_obj_port_vlan *vlan; 626bdcff080SVivien Didelot 62754a0ed0dSRussell King if (dsa_port_skip_vlan_configuration(dp)) 628c5335d73SVivien Didelot return 0; 629c5335d73SVivien Didelot 6302209158cSVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 6312209158cSVladimir Oltean 632134ef238SVladimir Oltean return dsa_port_vlan_del(dp, vlan); 633134ef238SVladimir Oltean } 6342209158cSVladimir Oltean 635134ef238SVladimir Oltean static int dsa_slave_host_vlan_del(struct net_device *dev, 636134ef238SVladimir Oltean const struct switchdev_obj *obj) 637134ef238SVladimir Oltean { 638134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 639134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 6402209158cSVladimir Oltean 641164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 642164f861bSVladimir Oltean if (!dp->bridge) 643164f861bSVladimir Oltean return -EOPNOTSUPP; 644164f861bSVladimir Oltean 645134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) 6462209158cSVladimir Oltean return 0; 647134ef238SVladimir Oltean 648134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 649134ef238SVladimir Oltean 650134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, vlan); 651bdcff080SVivien Didelot } 652bdcff080SVivien Didelot 65369bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx, 654648b4a99SJiri Pirko const struct switchdev_obj *obj) 655ba14d9ebSVivien Didelot { 656d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 657ba14d9ebSVivien Didelot int err; 658ba14d9ebSVivien Didelot 6590d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 6600d2cfbd4SVladimir Oltean return 0; 6610d2cfbd4SVladimir Oltean 6629e8f4a54SJiri Pirko switch (obj->id) { 6638df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 66403cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 66579b139f4SVivien Didelot return -EOPNOTSUPP; 66603cbb870SVladimir Oltean 667bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6688df30255SVivien Didelot break; 6695f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 670936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 67103cbb870SVladimir Oltean return -EOPNOTSUPP; 67203cbb870SVladimir Oltean 67368d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6745f4dbc50SAndrew Lunn break; 67557d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 676164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 677bdcff080SVivien Didelot err = dsa_slave_vlan_del(dev, obj); 678164f861bSVladimir Oltean else 679164f861bSVladimir Oltean err = dsa_slave_host_vlan_del(dev, obj); 68011149536SVivien Didelot break; 681c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 682936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 683c595c433SHoratiu Vultur return -EOPNOTSUPP; 68403cbb870SVladimir Oltean 685c595c433SHoratiu Vultur err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj)); 686c595c433SHoratiu Vultur break; 687c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_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_ring_role(dp, 692c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 693c595c433SHoratiu Vultur break; 694ba14d9ebSVivien Didelot default: 695ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 696ba14d9ebSVivien Didelot break; 697ba14d9ebSVivien Didelot } 698ba14d9ebSVivien Didelot 699ba14d9ebSVivien Didelot return err; 700ba14d9ebSVivien Didelot } 701ba14d9ebSVivien Didelot 7024fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 70304ff53f9SFlorian Fainelli struct sk_buff *skb) 70404ff53f9SFlorian Fainelli { 70504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 7064fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 7074fa7b718SVivien Didelot 708f78ed220SEric Dumazet return netpoll_send_skb(p->netpoll, skb); 70904ff53f9SFlorian Fainelli #else 71004ff53f9SFlorian Fainelli BUG(); 71104ff53f9SFlorian Fainelli return NETDEV_TX_OK; 712f78ed220SEric Dumazet #endif 71304ff53f9SFlorian Fainelli } 71404ff53f9SFlorian Fainelli 71590af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 71690af1059SBrandon Streiff struct sk_buff *skb) 71790af1059SBrandon Streiff { 71890af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 71990af1059SBrandon Streiff 720cfd12c06SYangbo Lu if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 721cfd12c06SYangbo Lu return; 722cfd12c06SYangbo Lu 72390af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 72490af1059SBrandon Streiff return; 72590af1059SBrandon Streiff 7265c5416f5SYangbo Lu ds->ops->port_txtstamp(ds, p->dp->index, skb); 72790af1059SBrandon Streiff } 72890af1059SBrandon Streiff 72997a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 73097a69a0dSVladimir Oltean { 73197a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 73297a69a0dSVladimir Oltean * tag to be successfully transmitted 73397a69a0dSVladimir Oltean */ 73497a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 73597a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 73697a69a0dSVladimir Oltean 73797a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 73897a69a0dSVladimir Oltean * do not modify its EtherType 73997a69a0dSVladimir Oltean */ 74097a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 74197a69a0dSVladimir Oltean dev_queue_xmit(skb); 74297a69a0dSVladimir Oltean 74397a69a0dSVladimir Oltean return NETDEV_TX_OK; 74497a69a0dSVladimir Oltean } 74597a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 74697a69a0dSVladimir Oltean 747a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 748a3b0b647SVladimir Oltean { 749a3b0b647SVladimir Oltean int needed_headroom = dev->needed_headroom; 750a3b0b647SVladimir Oltean int needed_tailroom = dev->needed_tailroom; 751a3b0b647SVladimir Oltean 752a3b0b647SVladimir Oltean /* For tail taggers, we need to pad short frames ourselves, to ensure 753a3b0b647SVladimir Oltean * that the tail tag does not fail at its role of being at the end of 754a3b0b647SVladimir Oltean * the packet, once the master interface pads the frame. Account for 755a3b0b647SVladimir Oltean * that pad length here, and pad later. 756a3b0b647SVladimir Oltean */ 757a3b0b647SVladimir Oltean if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 758a3b0b647SVladimir Oltean needed_tailroom += ETH_ZLEN - skb->len; 759a3b0b647SVladimir Oltean /* skb_headroom() returns unsigned int... */ 760a3b0b647SVladimir Oltean needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 761a3b0b647SVladimir Oltean needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 762a3b0b647SVladimir Oltean 763a3b0b647SVladimir Oltean if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 764a3b0b647SVladimir Oltean /* No reallocation needed, yay! */ 765a3b0b647SVladimir Oltean return 0; 766a3b0b647SVladimir Oltean 767a3b0b647SVladimir Oltean return pskb_expand_head(skb, needed_headroom, needed_tailroom, 768a3b0b647SVladimir Oltean GFP_ATOMIC); 769a3b0b647SVladimir Oltean } 770a3b0b647SVladimir Oltean 7713e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 7723e8a72d1SFlorian Fainelli { 7733e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 7744ed70ce9SFlorian Fainelli struct sk_buff *nskb; 7753e8a72d1SFlorian Fainelli 7766a900628SHeiner Kallweit dev_sw_netstats_tx_add(dev, 1, skb->len); 7773e8a72d1SFlorian Fainelli 778c4b364ceSYangbo Lu memset(skb->cb, 0, sizeof(skb->cb)); 77987671375SVladimir Oltean 780cf536ea3SYangbo Lu /* Handle tx timestamp if any */ 78190af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 78290af1059SBrandon Streiff 783a3b0b647SVladimir Oltean if (dsa_realloc_skb(skb, dev)) { 784a3b0b647SVladimir Oltean dev_kfree_skb_any(skb); 785a3b0b647SVladimir Oltean return NETDEV_TX_OK; 786a3b0b647SVladimir Oltean } 787a3b0b647SVladimir Oltean 788a3b0b647SVladimir Oltean /* needed_tailroom should still be 'warm' in the cache line from 789a3b0b647SVladimir Oltean * dsa_realloc_skb(), which has also ensured that padding is safe. 790a3b0b647SVladimir Oltean */ 791a3b0b647SVladimir Oltean if (dev->needed_tailroom) 792a3b0b647SVladimir Oltean eth_skb_pad(skb); 793a3b0b647SVladimir Oltean 794fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 795fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 796fe47d563SVivien Didelot */ 7974ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 798fe47d563SVivien Didelot if (!nskb) { 799fe47d563SVivien Didelot kfree_skb(skb); 8004ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 801fe47d563SVivien Didelot } 8025aed85ceSFlorian Fainelli 80397a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 80497a69a0dSVladimir Oltean } 80504ff53f9SFlorian Fainelli 80691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 80791da11f8SLennert Buytenhek 80891da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 80991da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 81091da11f8SLennert Buytenhek { 8117826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 8127826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 8137826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 81491da11f8SLennert Buytenhek } 81591da11f8SLennert Buytenhek 8163d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 8173d762a0fSGuenter Roeck { 818d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 819d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8203d762a0fSGuenter Roeck 8219d490b4eSVivien Didelot if (ds->ops->get_regs_len) 822d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 8233d762a0fSGuenter Roeck 8243d762a0fSGuenter Roeck return -EOPNOTSUPP; 8253d762a0fSGuenter Roeck } 8263d762a0fSGuenter Roeck 8273d762a0fSGuenter Roeck static void 8283d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 8293d762a0fSGuenter Roeck { 830d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 831d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8323d762a0fSGuenter Roeck 8339d490b4eSVivien Didelot if (ds->ops->get_regs) 834d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 8353d762a0fSGuenter Roeck } 8363d762a0fSGuenter Roeck 837aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 838aab9c406SFlorian Fainelli { 839aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 840aab9c406SFlorian Fainelli 841aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 842aab9c406SFlorian Fainelli } 843aab9c406SFlorian Fainelli 8446793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 8456793abb4SGuenter Roeck { 846d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 847d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8486793abb4SGuenter Roeck 8490e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 850ff04955cSAndrew Lunn return ds->cd->eeprom_len; 8516793abb4SGuenter Roeck 8529d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 8539d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 8546793abb4SGuenter Roeck 8556793abb4SGuenter Roeck return 0; 8566793abb4SGuenter Roeck } 8576793abb4SGuenter Roeck 8586793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 8596793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8606793abb4SGuenter Roeck { 861d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 862d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8636793abb4SGuenter Roeck 8649d490b4eSVivien Didelot if (ds->ops->get_eeprom) 8659d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 8666793abb4SGuenter Roeck 8676793abb4SGuenter Roeck return -EOPNOTSUPP; 8686793abb4SGuenter Roeck } 8696793abb4SGuenter Roeck 8706793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 8716793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8726793abb4SGuenter Roeck { 873d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 874d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8756793abb4SGuenter Roeck 8769d490b4eSVivien Didelot if (ds->ops->set_eeprom) 8779d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 8786793abb4SGuenter Roeck 8796793abb4SGuenter Roeck return -EOPNOTSUPP; 8806793abb4SGuenter Roeck } 8816793abb4SGuenter Roeck 88291da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 88391da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 88491da11f8SLennert Buytenhek { 885d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 886d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 88791da11f8SLennert Buytenhek 88891da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 88991da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 89091da11f8SLennert Buytenhek 89191da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 89291da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 89391da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 89491da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 8959d490b4eSVivien Didelot if (ds->ops->get_strings) 89689f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 89789f09048SFlorian Fainelli data + 4 * len); 898a71acad9SOleksij Rempel } else if (stringset == ETH_SS_TEST) { 899a71acad9SOleksij Rempel net_selftest_get_strings(data); 90091da11f8SLennert Buytenhek } 901a71acad9SOleksij Rempel 90291da11f8SLennert Buytenhek } 90391da11f8SLennert Buytenhek 90491da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 90591da11f8SLennert Buytenhek struct ethtool_stats *stats, 90691da11f8SLennert Buytenhek uint64_t *data) 90791da11f8SLennert Buytenhek { 908d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 909d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 9105f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 911f613ed66SFlorian Fainelli unsigned int start; 9125f6b4e14SFlorian Fainelli int i; 91391da11f8SLennert Buytenhek 9145f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 9155f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 9165f6b4e14SFlorian Fainelli 9176a900628SHeiner Kallweit s = per_cpu_ptr(dev->tstats, i); 918f613ed66SFlorian Fainelli do { 9195f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 9205f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 9215f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 9225f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 9235f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 9245f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 9255f6b4e14SFlorian Fainelli data[0] += tx_packets; 9265f6b4e14SFlorian Fainelli data[1] += tx_bytes; 9275f6b4e14SFlorian Fainelli data[2] += rx_packets; 9285f6b4e14SFlorian Fainelli data[3] += rx_bytes; 9295f6b4e14SFlorian Fainelli } 9309d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 931d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 93291da11f8SLennert Buytenhek } 93391da11f8SLennert Buytenhek 93491da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 93591da11f8SLennert Buytenhek { 936d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 937d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 93891da11f8SLennert Buytenhek 93991da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 940b94cbc90SVladimir Oltean int count = 0; 94191da11f8SLennert Buytenhek 942b94cbc90SVladimir Oltean if (ds->ops->get_sset_count) { 943b94cbc90SVladimir Oltean count = ds->ops->get_sset_count(ds, dp->index, sset); 944b94cbc90SVladimir Oltean if (count < 0) 94591da11f8SLennert Buytenhek return count; 946b94cbc90SVladimir Oltean } 947b94cbc90SVladimir Oltean 948b94cbc90SVladimir Oltean return count + 4; 949a71acad9SOleksij Rempel } else if (sset == ETH_SS_TEST) { 950a71acad9SOleksij Rempel return net_selftest_get_count(); 95191da11f8SLennert Buytenhek } 95291da11f8SLennert Buytenhek 95391da11f8SLennert Buytenhek return -EOPNOTSUPP; 95491da11f8SLennert Buytenhek } 95591da11f8SLennert Buytenhek 956487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev, 957487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats) 958487d3855SAlvin Šipraga { 959487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 960487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 961487d3855SAlvin Šipraga 962487d3855SAlvin Šipraga if (ds->ops->get_eth_phy_stats) 963487d3855SAlvin Šipraga ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats); 964487d3855SAlvin Šipraga } 965487d3855SAlvin Šipraga 966487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev, 967487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats) 968487d3855SAlvin Šipraga { 969487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 970487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 971487d3855SAlvin Šipraga 972487d3855SAlvin Šipraga if (ds->ops->get_eth_mac_stats) 973487d3855SAlvin Šipraga ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats); 974487d3855SAlvin Šipraga } 975487d3855SAlvin Šipraga 976487d3855SAlvin Šipraga static void 977487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev, 978487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats) 979487d3855SAlvin Šipraga { 980487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 981487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 982487d3855SAlvin Šipraga 983487d3855SAlvin Šipraga if (ds->ops->get_eth_ctrl_stats) 984487d3855SAlvin Šipraga ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats); 985487d3855SAlvin Šipraga } 986487d3855SAlvin Šipraga 987a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev, 988a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *buf) 989a71acad9SOleksij Rempel { 990a71acad9SOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(ndev); 991a71acad9SOleksij Rempel struct dsa_switch *ds = dp->ds; 992a71acad9SOleksij Rempel 993a71acad9SOleksij Rempel if (ds->ops->self_test) { 994a71acad9SOleksij Rempel ds->ops->self_test(ds, dp->index, etest, buf); 995a71acad9SOleksij Rempel return; 996a71acad9SOleksij Rempel } 997a71acad9SOleksij Rempel 998a71acad9SOleksij Rempel net_selftest(ndev, etest, buf); 999a71acad9SOleksij Rempel } 1000a71acad9SOleksij Rempel 100119e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 100219e57c4eSFlorian Fainelli { 1003d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1004d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 100519e57c4eSFlorian Fainelli 1006aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 1007aab9c406SFlorian Fainelli 10089d490b4eSVivien Didelot if (ds->ops->get_wol) 1009d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 101019e57c4eSFlorian Fainelli } 101119e57c4eSFlorian Fainelli 101219e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 101319e57c4eSFlorian Fainelli { 1014d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1015d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 101619e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 101719e57c4eSFlorian Fainelli 1018aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 1019aab9c406SFlorian Fainelli 10209d490b4eSVivien Didelot if (ds->ops->set_wol) 1021d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 102219e57c4eSFlorian Fainelli 102319e57c4eSFlorian Fainelli return ret; 102419e57c4eSFlorian Fainelli } 102519e57c4eSFlorian Fainelli 10267905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 10277905288fSFlorian Fainelli { 1028d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1029d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10307905288fSFlorian Fainelli int ret; 10317905288fSFlorian Fainelli 10327b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 103300670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10347b9cc738SVivien Didelot return -ENODEV; 10357b9cc738SVivien Didelot 103608f50061SVivien Didelot if (!ds->ops->set_mac_eee) 10377905288fSFlorian Fainelli return -EOPNOTSUPP; 10387905288fSFlorian Fainelli 1039d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 10407905288fSFlorian Fainelli if (ret) 10417905288fSFlorian Fainelli return ret; 10427905288fSFlorian Fainelli 1043aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 10447905288fSFlorian Fainelli } 10457905288fSFlorian Fainelli 10467905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 10477905288fSFlorian Fainelli { 1048d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1049d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10507905288fSFlorian Fainelli int ret; 10517905288fSFlorian Fainelli 10527b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 105300670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10547b9cc738SVivien Didelot return -ENODEV; 10557b9cc738SVivien Didelot 105608f50061SVivien Didelot if (!ds->ops->get_mac_eee) 10577905288fSFlorian Fainelli return -EOPNOTSUPP; 10587905288fSFlorian Fainelli 1059d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 10607905288fSFlorian Fainelli if (ret) 10617905288fSFlorian Fainelli return ret; 10627905288fSFlorian Fainelli 1063aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 1064aab9c406SFlorian Fainelli } 1065aab9c406SFlorian Fainelli 1066aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 1067aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 1068aab9c406SFlorian Fainelli { 1069aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1070aab9c406SFlorian Fainelli 1071aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 1072aab9c406SFlorian Fainelli } 1073aab9c406SFlorian Fainelli 1074aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 1075aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 1076aab9c406SFlorian Fainelli { 1077aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1078aab9c406SFlorian Fainelli 1079aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 10807905288fSFlorian Fainelli } 10817905288fSFlorian Fainelli 1082a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev, 1083a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1084a2a1a13bSHeiner Kallweit { 1085a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1086a2a1a13bSHeiner Kallweit 1087a2a1a13bSHeiner Kallweit phylink_ethtool_get_pauseparam(dp->pl, pause); 1088a2a1a13bSHeiner Kallweit } 1089a2a1a13bSHeiner Kallweit 1090a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev, 1091a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1092a2a1a13bSHeiner Kallweit { 1093a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1094a2a1a13bSHeiner Kallweit 1095a2a1a13bSHeiner Kallweit return phylink_ethtool_set_pauseparam(dp->pl, pause); 1096a2a1a13bSHeiner Kallweit } 1097a2a1a13bSHeiner Kallweit 109804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 109904ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 110004ff53f9SFlorian Fainelli struct netpoll_info *ni) 110104ff53f9SFlorian Fainelli { 1102d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 110304ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 110404ff53f9SFlorian Fainelli struct netpoll *netpoll; 110504ff53f9SFlorian Fainelli int err = 0; 110604ff53f9SFlorian Fainelli 110704ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 110804ff53f9SFlorian Fainelli if (!netpoll) 110904ff53f9SFlorian Fainelli return -ENOMEM; 111004ff53f9SFlorian Fainelli 111104ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 111204ff53f9SFlorian Fainelli if (err) { 111304ff53f9SFlorian Fainelli kfree(netpoll); 111404ff53f9SFlorian Fainelli goto out; 111504ff53f9SFlorian Fainelli } 111604ff53f9SFlorian Fainelli 111704ff53f9SFlorian Fainelli p->netpoll = netpoll; 111804ff53f9SFlorian Fainelli out: 111904ff53f9SFlorian Fainelli return err; 112004ff53f9SFlorian Fainelli } 112104ff53f9SFlorian Fainelli 112204ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 112304ff53f9SFlorian Fainelli { 112404ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 112504ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 112604ff53f9SFlorian Fainelli 112704ff53f9SFlorian Fainelli if (!netpoll) 112804ff53f9SFlorian Fainelli return; 112904ff53f9SFlorian Fainelli 113004ff53f9SFlorian Fainelli p->netpoll = NULL; 113104ff53f9SFlorian Fainelli 1132c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 113304ff53f9SFlorian Fainelli } 113404ff53f9SFlorian Fainelli 113504ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 113604ff53f9SFlorian Fainelli { 113704ff53f9SFlorian Fainelli } 113804ff53f9SFlorian Fainelli #endif 113904ff53f9SFlorian Fainelli 1140f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 11414fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 1142f50f2127SFlorian Fainelli { 11434fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 1144f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1145f50f2127SFlorian Fainelli 1146f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 1147f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 1148f50f2127SFlorian Fainelli return mall_tc_entry; 1149f50f2127SFlorian Fainelli 1150f50f2127SFlorian Fainelli return NULL; 1151f50f2127SFlorian Fainelli } 1152f50f2127SFlorian Fainelli 1153e13c2075SVladimir Oltean static int 1154e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 1155f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 1156f50f2127SFlorian Fainelli bool ingress) 1157f50f2127SFlorian Fainelli { 1158d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1159f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1160e13c2075SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror; 1161f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1162d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 11639681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 1164d945097bSVivien Didelot struct dsa_port *to_dp; 1165e13c2075SVladimir Oltean int err; 1166e13c2075SVladimir Oltean 1167f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 116834297176SVladimir Oltean return -EOPNOTSUPP; 1169f50f2127SFlorian Fainelli 117053eca1f3SJakub Kicinski if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1171319a1d19SJiri Pirko cls->common.extack)) 117234297176SVladimir Oltean return -EOPNOTSUPP; 1173319a1d19SJiri Pirko 11749681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 1175f50f2127SFlorian Fainelli 117665722159SVladimir Oltean if (!act->dev) 117765722159SVladimir Oltean return -EINVAL; 117865722159SVladimir Oltean 11799681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 1180f50f2127SFlorian Fainelli return -EOPNOTSUPP; 1181f50f2127SFlorian Fainelli 1182f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1183f50f2127SFlorian Fainelli if (!mall_tc_entry) 1184f50f2127SFlorian Fainelli return -ENOMEM; 1185f50f2127SFlorian Fainelli 1186f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 1187f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1188f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 1189f50f2127SFlorian Fainelli 11909681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 1191f50f2127SFlorian Fainelli 1192d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 1193f50f2127SFlorian Fainelli mirror->ingress = ingress; 1194f50f2127SFlorian Fainelli 1195d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 1196f50f2127SFlorian Fainelli if (err) { 1197f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1198f50f2127SFlorian Fainelli return err; 1199f50f2127SFlorian Fainelli } 1200f50f2127SFlorian Fainelli 1201f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1202e13c2075SVladimir Oltean 1203e13c2075SVladimir Oltean return err; 1204f50f2127SFlorian Fainelli } 1205f50f2127SFlorian Fainelli 120634297176SVladimir Oltean static int 120734297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev, 120834297176SVladimir Oltean struct tc_cls_matchall_offload *cls, 120934297176SVladimir Oltean bool ingress) 121034297176SVladimir Oltean { 121134297176SVladimir Oltean struct netlink_ext_ack *extack = cls->common.extack; 121234297176SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 121334297176SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 121434297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer; 121534297176SVladimir Oltean struct dsa_mall_tc_entry *mall_tc_entry; 121634297176SVladimir Oltean struct dsa_switch *ds = dp->ds; 121734297176SVladimir Oltean struct flow_action_entry *act; 121834297176SVladimir Oltean int err; 121934297176SVladimir Oltean 122034297176SVladimir Oltean if (!ds->ops->port_policer_add) { 122134297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1222c75a33c8SJacob Keller "Policing offload not implemented"); 122334297176SVladimir Oltean return -EOPNOTSUPP; 122434297176SVladimir Oltean } 122534297176SVladimir Oltean 122634297176SVladimir Oltean if (!ingress) { 122734297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1228c75a33c8SJacob Keller "Only supported on ingress qdisc"); 122934297176SVladimir Oltean return -EOPNOTSUPP; 123034297176SVladimir Oltean } 123134297176SVladimir Oltean 123234297176SVladimir Oltean if (!flow_action_basic_hw_stats_check(&cls->rule->action, 123334297176SVladimir Oltean cls->common.extack)) 123434297176SVladimir Oltean return -EOPNOTSUPP; 123534297176SVladimir Oltean 123634297176SVladimir Oltean list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 123734297176SVladimir Oltean if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 123834297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1239c75a33c8SJacob Keller "Only one port policer allowed"); 124034297176SVladimir Oltean return -EEXIST; 124134297176SVladimir Oltean } 124234297176SVladimir Oltean } 124334297176SVladimir Oltean 124434297176SVladimir Oltean act = &cls->rule->action.entries[0]; 124534297176SVladimir Oltean 124634297176SVladimir Oltean mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 124734297176SVladimir Oltean if (!mall_tc_entry) 124834297176SVladimir Oltean return -ENOMEM; 124934297176SVladimir Oltean 125034297176SVladimir Oltean mall_tc_entry->cookie = cls->cookie; 125134297176SVladimir Oltean mall_tc_entry->type = DSA_PORT_MALL_POLICER; 125234297176SVladimir Oltean policer = &mall_tc_entry->policer; 125334297176SVladimir Oltean policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 125434297176SVladimir Oltean policer->burst = act->police.burst; 125534297176SVladimir Oltean 125634297176SVladimir Oltean err = ds->ops->port_policer_add(ds, dp->index, policer); 125734297176SVladimir Oltean if (err) { 125834297176SVladimir Oltean kfree(mall_tc_entry); 125934297176SVladimir Oltean return err; 126034297176SVladimir Oltean } 126134297176SVladimir Oltean 126234297176SVladimir Oltean list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 126334297176SVladimir Oltean 126434297176SVladimir Oltean return err; 126534297176SVladimir Oltean } 126634297176SVladimir Oltean 1267e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev, 1268e13c2075SVladimir Oltean struct tc_cls_matchall_offload *cls, 1269e13c2075SVladimir Oltean bool ingress) 1270e13c2075SVladimir Oltean { 1271e13c2075SVladimir Oltean int err = -EOPNOTSUPP; 1272e13c2075SVladimir Oltean 1273e13c2075SVladimir Oltean if (cls->common.protocol == htons(ETH_P_ALL) && 1274e13c2075SVladimir Oltean flow_offload_has_one_action(&cls->rule->action) && 1275e13c2075SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1276e13c2075SVladimir Oltean err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 127734297176SVladimir Oltean else if (flow_offload_has_one_action(&cls->rule->action) && 127834297176SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 127934297176SVladimir Oltean err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1280e13c2075SVladimir Oltean 1281e13c2075SVladimir Oltean return err; 1282f50f2127SFlorian Fainelli } 1283f50f2127SFlorian Fainelli 1284f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 1285f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 1286f50f2127SFlorian Fainelli { 1287d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1288f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1289d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1290f50f2127SFlorian Fainelli 12914fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1292f50f2127SFlorian Fainelli if (!mall_tc_entry) 1293f50f2127SFlorian Fainelli return; 1294f50f2127SFlorian Fainelli 1295f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 1296f50f2127SFlorian Fainelli 1297f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 1298f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 129934297176SVladimir Oltean if (ds->ops->port_mirror_del) 130034297176SVladimir Oltean ds->ops->port_mirror_del(ds, dp->index, 130134297176SVladimir Oltean &mall_tc_entry->mirror); 130234297176SVladimir Oltean break; 130334297176SVladimir Oltean case DSA_PORT_MALL_POLICER: 130434297176SVladimir Oltean if (ds->ops->port_policer_del) 130534297176SVladimir Oltean ds->ops->port_policer_del(ds, dp->index); 1306f50f2127SFlorian Fainelli break; 1307f50f2127SFlorian Fainelli default: 1308f50f2127SFlorian Fainelli WARN_ON(1); 1309f50f2127SFlorian Fainelli } 1310f50f2127SFlorian Fainelli 1311f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1312f50f2127SFlorian Fainelli } 1313f50f2127SFlorian Fainelli 13143fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 13156b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 13166b3eb752SJiri Pirko bool ingress) 1317f50f2127SFlorian Fainelli { 13185fd9fc4eSJiri Pirko if (cls->common.chain_index) 1319a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 1320f50f2127SFlorian Fainelli 13213fbae382SJiri Pirko switch (cls->command) { 13223fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 13235fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 13243fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 13253fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 13263fbae382SJiri Pirko return 0; 13273fbae382SJiri Pirko default: 13283fbae382SJiri Pirko return -EOPNOTSUPP; 13293fbae382SJiri Pirko } 13303fbae382SJiri Pirko } 13313fbae382SJiri Pirko 1332ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev, 1333ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1334ed11bb1fSVladimir Oltean bool ingress) 1335ed11bb1fSVladimir Oltean { 1336ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1337ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1338ed11bb1fSVladimir Oltean int port = dp->index; 1339ed11bb1fSVladimir Oltean 1340ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_add) 1341ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1342ed11bb1fSVladimir Oltean 1343ed11bb1fSVladimir Oltean return ds->ops->cls_flower_add(ds, port, cls, ingress); 1344ed11bb1fSVladimir Oltean } 1345ed11bb1fSVladimir Oltean 1346ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev, 1347ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1348ed11bb1fSVladimir Oltean bool ingress) 1349ed11bb1fSVladimir Oltean { 1350ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1351ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1352ed11bb1fSVladimir Oltean int port = dp->index; 1353ed11bb1fSVladimir Oltean 1354ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_del) 1355ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1356ed11bb1fSVladimir Oltean 1357ed11bb1fSVladimir Oltean return ds->ops->cls_flower_del(ds, port, cls, ingress); 1358ed11bb1fSVladimir Oltean } 1359ed11bb1fSVladimir Oltean 1360ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev, 1361ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1362ed11bb1fSVladimir Oltean bool ingress) 1363ed11bb1fSVladimir Oltean { 1364ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1365ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1366ed11bb1fSVladimir Oltean int port = dp->index; 1367ed11bb1fSVladimir Oltean 1368ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_stats) 1369ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1370ed11bb1fSVladimir Oltean 1371ed11bb1fSVladimir Oltean return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1372ed11bb1fSVladimir Oltean } 1373ed11bb1fSVladimir Oltean 1374ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1375ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1376ed11bb1fSVladimir Oltean bool ingress) 1377ed11bb1fSVladimir Oltean { 1378ed11bb1fSVladimir Oltean switch (cls->command) { 1379ed11bb1fSVladimir Oltean case FLOW_CLS_REPLACE: 1380ed11bb1fSVladimir Oltean return dsa_slave_add_cls_flower(dev, cls, ingress); 1381ed11bb1fSVladimir Oltean case FLOW_CLS_DESTROY: 1382ed11bb1fSVladimir Oltean return dsa_slave_del_cls_flower(dev, cls, ingress); 1383ed11bb1fSVladimir Oltean case FLOW_CLS_STATS: 1384ed11bb1fSVladimir Oltean return dsa_slave_stats_cls_flower(dev, cls, ingress); 1385ed11bb1fSVladimir Oltean default: 1386ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1387ed11bb1fSVladimir Oltean } 1388ed11bb1fSVladimir Oltean } 1389ed11bb1fSVladimir Oltean 13906b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 13916b3eb752SJiri Pirko void *cb_priv, bool ingress) 13926b3eb752SJiri Pirko { 13936b3eb752SJiri Pirko struct net_device *dev = cb_priv; 13946b3eb752SJiri Pirko 139544ae12a7SJiri Pirko if (!tc_can_offload(dev)) 139644ae12a7SJiri Pirko return -EOPNOTSUPP; 139744ae12a7SJiri Pirko 13986b3eb752SJiri Pirko switch (type) { 13996b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 14006b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1401ed11bb1fSVladimir Oltean case TC_SETUP_CLSFLOWER: 1402ed11bb1fSVladimir Oltean return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 14036b3eb752SJiri Pirko default: 14046b3eb752SJiri Pirko return -EOPNOTSUPP; 14056b3eb752SJiri Pirko } 14066b3eb752SJiri Pirko } 14076b3eb752SJiri Pirko 14086b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 14096b3eb752SJiri Pirko void *type_data, void *cb_priv) 14106b3eb752SJiri Pirko { 14116b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 14126b3eb752SJiri Pirko } 14136b3eb752SJiri Pirko 14146b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(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, false); 14186b3eb752SJiri Pirko } 14196b3eb752SJiri Pirko 1420955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 1421955bcb6eSPablo Neira Ayuso 14226b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 1423955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 14246b3eb752SJiri Pirko { 1425955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 1426a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 14276b3eb752SJiri Pirko 142832f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 14296b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 143032f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 14316b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 14326b3eb752SJiri Pirko else 14336b3eb752SJiri Pirko return -EOPNOTSUPP; 14346b3eb752SJiri Pirko 1435955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 1436955bcb6eSPablo Neira Ayuso 14376b3eb752SJiri Pirko switch (f->command) { 14389c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 14390d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 14400d4fd02eSPablo Neira Ayuso return -EBUSY; 14410d4fd02eSPablo Neira Ayuso 14420c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1443955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 1444955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 1445955bcb6eSPablo Neira Ayuso 1446955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 1447955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1448955bcb6eSPablo Neira Ayuso return 0; 14499c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 145014bfb13fSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f->block, cb, dev); 1451955bcb6eSPablo Neira Ayuso if (!block_cb) 1452955bcb6eSPablo Neira Ayuso return -ENOENT; 1453955bcb6eSPablo Neira Ayuso 1454955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 1455955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 14566b3eb752SJiri Pirko return 0; 14576b3eb752SJiri Pirko default: 14586b3eb752SJiri Pirko return -EOPNOTSUPP; 14596b3eb752SJiri Pirko } 14606b3eb752SJiri Pirko } 14616b3eb752SJiri Pirko 14623fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 14633fb24a43SPablo Neira Ayuso void *type_data) 14643fb24a43SPablo Neira Ayuso { 14653fb24a43SPablo Neira Ayuso struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 14663fb24a43SPablo Neira Ayuso struct net_device *master = cpu_dp->master; 14673fb24a43SPablo Neira Ayuso 14683fb24a43SPablo Neira Ayuso if (!master->netdev_ops->ndo_setup_tc) 14693fb24a43SPablo Neira Ayuso return -EOPNOTSUPP; 14703fb24a43SPablo Neira Ayuso 14713fb24a43SPablo Neira Ayuso return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 14723fb24a43SPablo Neira Ayuso } 14733fb24a43SPablo Neira Ayuso 14743fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1475de4784caSJiri Pirko void *type_data) 14763fbae382SJiri Pirko { 147747d23af2SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 147847d23af2SVladimir Oltean struct dsa_switch *ds = dp->ds; 147947d23af2SVladimir Oltean 14803fb24a43SPablo Neira Ayuso switch (type) { 14813fb24a43SPablo Neira Ayuso case TC_SETUP_BLOCK: 14826b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 14833fb24a43SPablo Neira Ayuso case TC_SETUP_FT: 14843fb24a43SPablo Neira Ayuso return dsa_slave_setup_ft_block(ds, dp->index, type_data); 14853fb24a43SPablo Neira Ayuso default: 14863fb24a43SPablo Neira Ayuso break; 14873fb24a43SPablo Neira Ayuso } 148847d23af2SVladimir Oltean 148947d23af2SVladimir Oltean if (!ds->ops->port_setup_tc) 1490a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 149147d23af2SVladimir Oltean 149247d23af2SVladimir Oltean return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1493f50f2127SFlorian Fainelli } 1494f50f2127SFlorian Fainelli 1495bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1496bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1497bf9f2648SFlorian Fainelli { 1498d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1499d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1500bf9f2648SFlorian Fainelli 1501bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1502bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1503bf9f2648SFlorian Fainelli 1504d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1505bf9f2648SFlorian Fainelli } 1506bf9f2648SFlorian Fainelli 1507bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1508bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1509bf9f2648SFlorian Fainelli { 1510d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1511d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1512bf9f2648SFlorian Fainelli 1513bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1514bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1515bf9f2648SFlorian Fainelli 1516d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1517bf9f2648SFlorian Fainelli } 1518bf9f2648SFlorian Fainelli 15190336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 15200336369dSBrandon Streiff struct ethtool_ts_info *ts) 15210336369dSBrandon Streiff { 15220336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 15230336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 15240336369dSBrandon Streiff 15250336369dSBrandon Streiff if (!ds->ops->get_ts_info) 15260336369dSBrandon Streiff return -EOPNOTSUPP; 15270336369dSBrandon Streiff 15280336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 15290336369dSBrandon Streiff } 15300336369dSBrandon Streiff 1531061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1532061f6a50SFlorian Fainelli u16 vid) 1533061f6a50SFlorian Fainelli { 1534061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 153588236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 153688236591SVladimir Oltean .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1537b7a9e0daSVladimir Oltean .vid = vid, 153888236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 153988236591SVladimir Oltean .flags = 0, 154088236591SVladimir Oltean }; 154131046a5fSVladimir Oltean struct netlink_ext_ack extack = {0}; 1542061f6a50SFlorian Fainelli int ret; 1543061f6a50SFlorian Fainelli 154488236591SVladimir Oltean /* User port... */ 154531046a5fSVladimir Oltean ret = dsa_port_vlan_add(dp, &vlan, &extack); 154631046a5fSVladimir Oltean if (ret) { 154731046a5fSVladimir Oltean if (extack._msg) 154831046a5fSVladimir Oltean netdev_err(dev, "%s\n", extack._msg); 154988236591SVladimir Oltean return ret; 155031046a5fSVladimir Oltean } 155188236591SVladimir Oltean 155288236591SVladimir Oltean /* And CPU port... */ 1553134ef238SVladimir Oltean ret = dsa_port_host_vlan_add(dp, &vlan, &extack); 155431046a5fSVladimir Oltean if (ret) { 155531046a5fSVladimir Oltean if (extack._msg) 155631046a5fSVladimir Oltean netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 155731046a5fSVladimir Oltean extack._msg); 15587e1741b4SVivien Didelot return ret; 155931046a5fSVladimir Oltean } 15607e1741b4SVivien Didelot 1561134ef238SVladimir Oltean return 0; 1562061f6a50SFlorian Fainelli } 1563061f6a50SFlorian Fainelli 1564061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1565061f6a50SFlorian Fainelli u16 vid) 1566061f6a50SFlorian Fainelli { 1567061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 156888236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 1569b7a9e0daSVladimir Oltean .vid = vid, 157088236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 157188236591SVladimir Oltean .flags = 0, 157288236591SVladimir Oltean }; 15732209158cSVladimir Oltean int err; 1574061f6a50SFlorian Fainelli 15752209158cSVladimir Oltean err = dsa_port_vlan_del(dp, &vlan); 15762209158cSVladimir Oltean if (err) 15772209158cSVladimir Oltean return err; 15782209158cSVladimir Oltean 1579134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, &vlan); 1580061f6a50SFlorian Fainelli } 1581061f6a50SFlorian Fainelli 158206cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg) 158306cfb2dfSVladimir Oltean { 158406cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 158506cfb2dfSVladimir Oltean 158606cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_add_vid(arg, proto, vid); 158706cfb2dfSVladimir Oltean } 158806cfb2dfSVladimir Oltean 158906cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg) 159006cfb2dfSVladimir Oltean { 159106cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 159206cfb2dfSVladimir Oltean 159306cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_kill_vid(arg, proto, vid); 159406cfb2dfSVladimir Oltean } 159506cfb2dfSVladimir Oltean 159606cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN 159706cfb2dfSVladimir Oltean * filtering is enabled. The baseline is that only ports that offload a 159806cfb2dfSVladimir Oltean * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware, 159906cfb2dfSVladimir Oltean * but there are exceptions for quirky hardware. 160006cfb2dfSVladimir Oltean * 160106cfb2dfSVladimir Oltean * If ds->vlan_filtering_is_global = true, then standalone ports which share 160206cfb2dfSVladimir Oltean * the same switch with other ports that offload a VLAN-aware bridge are also 160306cfb2dfSVladimir Oltean * inevitably VLAN-aware. 160406cfb2dfSVladimir Oltean * 160506cfb2dfSVladimir Oltean * To summarize, a DSA switch port offloads: 160606cfb2dfSVladimir Oltean * 160706cfb2dfSVladimir Oltean * - If standalone (this includes software bridge, software LAG): 160858adf9dcSVladimir Oltean * - if ds->needs_standalone_vlan_filtering = true, OR if 160958adf9dcSVladimir Oltean * (ds->vlan_filtering_is_global = true AND there are bridges spanning 161058adf9dcSVladimir Oltean * this switch chip which have vlan_filtering=1) 161106cfb2dfSVladimir Oltean * - the 8021q upper VLANs 161258adf9dcSVladimir Oltean * - else (standalone VLAN filtering is not needed, VLAN filtering is not 161358adf9dcSVladimir Oltean * global, or it is, but no port is under a VLAN-aware bridge): 161406cfb2dfSVladimir Oltean * - no VLAN (any 8021q upper is a software VLAN) 161506cfb2dfSVladimir Oltean * 161606cfb2dfSVladimir Oltean * - If under a vlan_filtering=0 bridge which it offload: 161706cfb2dfSVladimir Oltean * - if ds->configure_vlan_while_not_filtering = true (default): 161806cfb2dfSVladimir Oltean * - the bridge VLANs. These VLANs are committed to hardware but inactive. 161906cfb2dfSVladimir Oltean * - else (deprecated): 162006cfb2dfSVladimir Oltean * - no VLAN. The bridge VLANs are not restored when VLAN awareness is 162106cfb2dfSVladimir Oltean * enabled, so this behavior is broken and discouraged. 162206cfb2dfSVladimir Oltean * 162306cfb2dfSVladimir Oltean * - If under a vlan_filtering=1 bridge which it offload: 162406cfb2dfSVladimir Oltean * - the bridge VLANs 162506cfb2dfSVladimir Oltean * - the 8021q upper VLANs 162606cfb2dfSVladimir Oltean */ 162706cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave, 162806cfb2dfSVladimir Oltean bool vlan_filtering) 162906cfb2dfSVladimir Oltean { 163006cfb2dfSVladimir Oltean int err; 163106cfb2dfSVladimir Oltean 163206cfb2dfSVladimir Oltean if (vlan_filtering) { 163306cfb2dfSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 163406cfb2dfSVladimir Oltean 163506cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_restore_vlan, slave); 163606cfb2dfSVladimir Oltean if (err) { 163706cfb2dfSVladimir Oltean vlan_for_each(slave, dsa_slave_clear_vlan, slave); 163806cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 163906cfb2dfSVladimir Oltean return err; 164006cfb2dfSVladimir Oltean } 164106cfb2dfSVladimir Oltean } else { 164206cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_clear_vlan, slave); 164306cfb2dfSVladimir Oltean if (err) 164406cfb2dfSVladimir Oltean return err; 164506cfb2dfSVladimir Oltean 164606cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 164706cfb2dfSVladimir Oltean } 164806cfb2dfSVladimir Oltean 164906cfb2dfSVladimir Oltean return 0; 165006cfb2dfSVladimir Oltean } 165106cfb2dfSVladimir Oltean 1652bff33f7eSVladimir Oltean struct dsa_hw_port { 1653bff33f7eSVladimir Oltean struct list_head list; 1654bff33f7eSVladimir Oltean struct net_device *dev; 1655bff33f7eSVladimir Oltean int old_mtu; 1656bff33f7eSVladimir Oltean }; 1657bff33f7eSVladimir Oltean 1658bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1659bff33f7eSVladimir Oltean { 1660bff33f7eSVladimir Oltean const struct dsa_hw_port *p; 1661bff33f7eSVladimir Oltean int err; 1662bff33f7eSVladimir Oltean 1663bff33f7eSVladimir Oltean list_for_each_entry(p, hw_port_list, list) { 1664bff33f7eSVladimir Oltean if (p->dev->mtu == mtu) 1665bff33f7eSVladimir Oltean continue; 1666bff33f7eSVladimir Oltean 1667bff33f7eSVladimir Oltean err = dev_set_mtu(p->dev, mtu); 1668bff33f7eSVladimir Oltean if (err) 1669bff33f7eSVladimir Oltean goto rollback; 1670bff33f7eSVladimir Oltean } 1671bff33f7eSVladimir Oltean 1672bff33f7eSVladimir Oltean return 0; 1673bff33f7eSVladimir Oltean 1674bff33f7eSVladimir Oltean rollback: 1675bff33f7eSVladimir Oltean list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1676bff33f7eSVladimir Oltean if (p->dev->mtu == p->old_mtu) 1677bff33f7eSVladimir Oltean continue; 1678bff33f7eSVladimir Oltean 1679bff33f7eSVladimir Oltean if (dev_set_mtu(p->dev, p->old_mtu)) 1680bff33f7eSVladimir Oltean netdev_err(p->dev, "Failed to restore MTU\n"); 1681bff33f7eSVladimir Oltean } 1682bff33f7eSVladimir Oltean 1683bff33f7eSVladimir Oltean return err; 1684bff33f7eSVladimir Oltean } 1685bff33f7eSVladimir Oltean 1686bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1687bff33f7eSVladimir Oltean { 1688bff33f7eSVladimir Oltean struct dsa_hw_port *p, *n; 1689bff33f7eSVladimir Oltean 1690bff33f7eSVladimir Oltean list_for_each_entry_safe(p, n, hw_port_list, list) 1691bff33f7eSVladimir Oltean kfree(p); 1692bff33f7eSVladimir Oltean } 1693bff33f7eSVladimir Oltean 1694bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */ 1695bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1696bff33f7eSVladimir Oltean { 1697bff33f7eSVladimir Oltean struct list_head hw_port_list; 1698bff33f7eSVladimir Oltean struct dsa_switch_tree *dst; 1699bff33f7eSVladimir Oltean int min_mtu = ETH_MAX_MTU; 1700bff33f7eSVladimir Oltean struct dsa_port *other_dp; 1701bff33f7eSVladimir Oltean int err; 1702bff33f7eSVladimir Oltean 1703bff33f7eSVladimir Oltean if (!dp->ds->mtu_enforcement_ingress) 1704bff33f7eSVladimir Oltean return; 1705bff33f7eSVladimir Oltean 1706d3eed0e5SVladimir Oltean if (!dp->bridge) 1707bff33f7eSVladimir Oltean return; 1708bff33f7eSVladimir Oltean 1709bff33f7eSVladimir Oltean INIT_LIST_HEAD(&hw_port_list); 1710bff33f7eSVladimir Oltean 1711bff33f7eSVladimir Oltean /* Populate the list of ports that are part of the same bridge 1712bff33f7eSVladimir Oltean * as the newly added/modified port 1713bff33f7eSVladimir Oltean */ 1714bff33f7eSVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) { 1715bff33f7eSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 1716bff33f7eSVladimir Oltean struct dsa_hw_port *hw_port; 1717bff33f7eSVladimir Oltean struct net_device *slave; 1718bff33f7eSVladimir Oltean 1719bff33f7eSVladimir Oltean if (other_dp->type != DSA_PORT_TYPE_USER) 1720bff33f7eSVladimir Oltean continue; 1721bff33f7eSVladimir Oltean 172236cbf39bSVladimir Oltean if (!dsa_port_bridge_same(dp, other_dp)) 1723bff33f7eSVladimir Oltean continue; 1724bff33f7eSVladimir Oltean 1725bff33f7eSVladimir Oltean if (!other_dp->ds->mtu_enforcement_ingress) 1726bff33f7eSVladimir Oltean continue; 1727bff33f7eSVladimir Oltean 1728bff33f7eSVladimir Oltean slave = other_dp->slave; 1729bff33f7eSVladimir Oltean 1730bff33f7eSVladimir Oltean if (min_mtu > slave->mtu) 1731bff33f7eSVladimir Oltean min_mtu = slave->mtu; 1732bff33f7eSVladimir Oltean 1733bff33f7eSVladimir Oltean hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1734bff33f7eSVladimir Oltean if (!hw_port) 1735bff33f7eSVladimir Oltean goto out; 1736bff33f7eSVladimir Oltean 1737bff33f7eSVladimir Oltean hw_port->dev = slave; 1738bff33f7eSVladimir Oltean hw_port->old_mtu = slave->mtu; 1739bff33f7eSVladimir Oltean 1740bff33f7eSVladimir Oltean list_add(&hw_port->list, &hw_port_list); 1741bff33f7eSVladimir Oltean } 1742bff33f7eSVladimir Oltean } 1743bff33f7eSVladimir Oltean 1744bff33f7eSVladimir Oltean /* Attempt to configure the entire hardware bridge to the newly added 1745bff33f7eSVladimir Oltean * interface's MTU first, regardless of whether the intention of the 1746bff33f7eSVladimir Oltean * user was to raise or lower it. 1747bff33f7eSVladimir Oltean */ 1748bff33f7eSVladimir Oltean err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1749bff33f7eSVladimir Oltean if (!err) 1750bff33f7eSVladimir Oltean goto out; 1751bff33f7eSVladimir Oltean 1752bff33f7eSVladimir Oltean /* Clearly that didn't work out so well, so just set the minimum MTU on 1753bff33f7eSVladimir Oltean * all hardware bridge ports now. If this fails too, then all ports will 1754bff33f7eSVladimir Oltean * still have their old MTU rolled back anyway. 1755bff33f7eSVladimir Oltean */ 1756bff33f7eSVladimir Oltean dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1757bff33f7eSVladimir Oltean 1758bff33f7eSVladimir Oltean out: 1759bff33f7eSVladimir Oltean dsa_hw_port_list_free(&hw_port_list); 1760bff33f7eSVladimir Oltean } 1761bff33f7eSVladimir Oltean 176253da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1763bfcb8132SVladimir Oltean { 1764bfcb8132SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1765bfcb8132SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1766bfcb8132SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 1767bfcb8132SVladimir Oltean struct dsa_switch *ds = p->dp->ds; 17684e4ab795SVladimir Oltean struct dsa_port *dp_iter; 1769bfcb8132SVladimir Oltean struct dsa_port *cpu_dp; 1770bfcb8132SVladimir Oltean int port = p->dp->index; 1771bfcb8132SVladimir Oltean int largest_mtu = 0; 1772bfcb8132SVladimir Oltean int new_master_mtu; 1773bfcb8132SVladimir Oltean int old_master_mtu; 1774bfcb8132SVladimir Oltean int mtu_limit; 1775bfcb8132SVladimir Oltean int cpu_mtu; 17764e4ab795SVladimir Oltean int err; 1777bfcb8132SVladimir Oltean 1778bfcb8132SVladimir Oltean if (!ds->ops->port_change_mtu) 1779bfcb8132SVladimir Oltean return -EOPNOTSUPP; 1780bfcb8132SVladimir Oltean 17814e4ab795SVladimir Oltean list_for_each_entry(dp_iter, &ds->dst->ports, list) { 1782bfcb8132SVladimir Oltean int slave_mtu; 1783bfcb8132SVladimir Oltean 17844e4ab795SVladimir Oltean if (!dsa_port_is_user(dp_iter)) 1785bfcb8132SVladimir Oltean continue; 1786bfcb8132SVladimir Oltean 1787bfcb8132SVladimir Oltean /* During probe, this function will be called for each slave 1788bfcb8132SVladimir Oltean * device, while not all of them have been allocated. That's 1789bfcb8132SVladimir Oltean * ok, it doesn't change what the maximum is, so ignore it. 1790bfcb8132SVladimir Oltean */ 17914e4ab795SVladimir Oltean if (!dp_iter->slave) 1792bfcb8132SVladimir Oltean continue; 1793bfcb8132SVladimir Oltean 1794bfcb8132SVladimir Oltean /* Pretend that we already applied the setting, which we 1795bfcb8132SVladimir Oltean * actually haven't (still haven't done all integrity checks) 1796bfcb8132SVladimir Oltean */ 17974e4ab795SVladimir Oltean if (dp_iter == dp) 1798bfcb8132SVladimir Oltean slave_mtu = new_mtu; 1799bfcb8132SVladimir Oltean else 18004e4ab795SVladimir Oltean slave_mtu = dp_iter->slave->mtu; 1801bfcb8132SVladimir Oltean 1802bfcb8132SVladimir Oltean if (largest_mtu < slave_mtu) 1803bfcb8132SVladimir Oltean largest_mtu = slave_mtu; 1804bfcb8132SVladimir Oltean } 1805bfcb8132SVladimir Oltean 1806bfcb8132SVladimir Oltean cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1807bfcb8132SVladimir Oltean 1808bfcb8132SVladimir Oltean mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1809bfcb8132SVladimir Oltean old_master_mtu = master->mtu; 18104e500251SVladimir Oltean new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops); 1811bfcb8132SVladimir Oltean if (new_master_mtu > mtu_limit) 1812bfcb8132SVladimir Oltean return -ERANGE; 1813bfcb8132SVladimir Oltean 1814bfcb8132SVladimir Oltean /* If the master MTU isn't over limit, there's no need to check the CPU 1815bfcb8132SVladimir Oltean * MTU, since that surely isn't either. 1816bfcb8132SVladimir Oltean */ 1817bfcb8132SVladimir Oltean cpu_mtu = largest_mtu; 1818bfcb8132SVladimir Oltean 1819bfcb8132SVladimir Oltean /* Start applying stuff */ 1820bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) { 1821bfcb8132SVladimir Oltean err = dev_set_mtu(master, new_master_mtu); 1822bfcb8132SVladimir Oltean if (err < 0) 1823bfcb8132SVladimir Oltean goto out_master_failed; 1824bfcb8132SVladimir Oltean 1825bfcb8132SVladimir Oltean /* We only need to propagate the MTU of the CPU port to 182688faba20SVladimir Oltean * upstream switches, so create a non-targeted notifier which 182788faba20SVladimir Oltean * updates all switches. 1828bfcb8132SVladimir Oltean */ 182988faba20SVladimir Oltean err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false); 1830bfcb8132SVladimir Oltean if (err) 1831bfcb8132SVladimir Oltean goto out_cpu_failed; 1832bfcb8132SVladimir Oltean } 1833bfcb8132SVladimir Oltean 183488faba20SVladimir Oltean err = dsa_port_mtu_change(dp, new_mtu, true); 1835bfcb8132SVladimir Oltean if (err) 1836bfcb8132SVladimir Oltean goto out_port_failed; 1837bfcb8132SVladimir Oltean 1838bfcb8132SVladimir Oltean dev->mtu = new_mtu; 1839bfcb8132SVladimir Oltean 1840bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 1841bff33f7eSVladimir Oltean 1842bfcb8132SVladimir Oltean return 0; 1843bfcb8132SVladimir Oltean 1844bfcb8132SVladimir Oltean out_port_failed: 1845bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1846bfcb8132SVladimir Oltean dsa_port_mtu_change(cpu_dp, old_master_mtu - 18474e500251SVladimir Oltean dsa_tag_protocol_overhead(cpu_dp->tag_ops), 184888faba20SVladimir Oltean false); 1849bfcb8132SVladimir Oltean out_cpu_failed: 1850bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1851bfcb8132SVladimir Oltean dev_set_mtu(master, old_master_mtu); 1852bfcb8132SVladimir Oltean out_master_failed: 1853bfcb8132SVladimir Oltean return err; 1854bfcb8132SVladimir Oltean } 1855bfcb8132SVladimir Oltean 1856d538eca8SVladimir Oltean static int __maybe_unused 1857d538eca8SVladimir Oltean dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app) 1858d538eca8SVladimir Oltean { 1859d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1860d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1861d538eca8SVladimir Oltean unsigned long mask, new_prio; 1862d538eca8SVladimir Oltean int err, port = dp->index; 1863d538eca8SVladimir Oltean 1864d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1865d538eca8SVladimir Oltean return -EOPNOTSUPP; 1866d538eca8SVladimir Oltean 1867d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, app); 1868d538eca8SVladimir Oltean if (err) 1869d538eca8SVladimir Oltean return err; 1870d538eca8SVladimir Oltean 1871d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1872d538eca8SVladimir Oltean new_prio = __fls(mask); 1873d538eca8SVladimir Oltean 1874d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1875d538eca8SVladimir Oltean if (err) { 1876d538eca8SVladimir Oltean dcb_ieee_delapp(dev, app); 1877d538eca8SVladimir Oltean return err; 1878d538eca8SVladimir Oltean } 1879d538eca8SVladimir Oltean 1880d538eca8SVladimir Oltean return 0; 1881d538eca8SVladimir Oltean } 1882d538eca8SVladimir Oltean 188347d75f78SVladimir Oltean static int __maybe_unused 188447d75f78SVladimir Oltean dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app) 188547d75f78SVladimir Oltean { 188647d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 188747d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 188847d75f78SVladimir Oltean unsigned long mask, new_prio; 188947d75f78SVladimir Oltean int err, port = dp->index; 189047d75f78SVladimir Oltean u8 dscp = app->protocol; 189147d75f78SVladimir Oltean 189247d75f78SVladimir Oltean if (!ds->ops->port_add_dscp_prio) 189347d75f78SVladimir Oltean return -EOPNOTSUPP; 189447d75f78SVladimir Oltean 189547d75f78SVladimir Oltean if (dscp >= 64) { 189647d75f78SVladimir Oltean netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n", 189747d75f78SVladimir Oltean dscp); 189847d75f78SVladimir Oltean return -EINVAL; 189947d75f78SVladimir Oltean } 190047d75f78SVladimir Oltean 190147d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, app); 190247d75f78SVladimir Oltean if (err) 190347d75f78SVladimir Oltean return err; 190447d75f78SVladimir Oltean 190547d75f78SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 190647d75f78SVladimir Oltean new_prio = __fls(mask); 190747d75f78SVladimir Oltean 190847d75f78SVladimir Oltean err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio); 190947d75f78SVladimir Oltean if (err) { 191047d75f78SVladimir Oltean dcb_ieee_delapp(dev, app); 191147d75f78SVladimir Oltean return err; 191247d75f78SVladimir Oltean } 191347d75f78SVladimir Oltean 191447d75f78SVladimir Oltean return 0; 191547d75f78SVladimir Oltean } 191647d75f78SVladimir Oltean 1917d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev, 1918d538eca8SVladimir Oltean struct dcb_app *app) 1919d538eca8SVladimir Oltean { 1920d538eca8SVladimir Oltean switch (app->selector) { 1921d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1922d538eca8SVladimir Oltean switch (app->protocol) { 1923d538eca8SVladimir Oltean case 0: 1924d538eca8SVladimir Oltean return dsa_slave_dcbnl_set_default_prio(dev, app); 1925d538eca8SVladimir Oltean default: 1926d538eca8SVladimir Oltean return -EOPNOTSUPP; 1927d538eca8SVladimir Oltean } 1928d538eca8SVladimir Oltean break; 192947d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 193047d75f78SVladimir Oltean return dsa_slave_dcbnl_add_dscp_prio(dev, app); 1931d538eca8SVladimir Oltean default: 1932d538eca8SVladimir Oltean return -EOPNOTSUPP; 1933d538eca8SVladimir Oltean } 1934d538eca8SVladimir Oltean } 1935d538eca8SVladimir Oltean 1936d538eca8SVladimir Oltean static int __maybe_unused 1937d538eca8SVladimir Oltean dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app) 1938d538eca8SVladimir Oltean { 1939d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1940d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1941d538eca8SVladimir Oltean unsigned long mask, new_prio; 1942d538eca8SVladimir Oltean int err, port = dp->index; 1943d538eca8SVladimir Oltean 1944d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1945d538eca8SVladimir Oltean return -EOPNOTSUPP; 1946d538eca8SVladimir Oltean 1947d538eca8SVladimir Oltean err = dcb_ieee_delapp(dev, app); 1948d538eca8SVladimir Oltean if (err) 1949d538eca8SVladimir Oltean return err; 1950d538eca8SVladimir Oltean 1951d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1952d538eca8SVladimir Oltean new_prio = mask ? __fls(mask) : 0; 1953d538eca8SVladimir Oltean 1954d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1955d538eca8SVladimir Oltean if (err) { 1956d538eca8SVladimir Oltean dcb_ieee_setapp(dev, app); 1957d538eca8SVladimir Oltean return err; 1958d538eca8SVladimir Oltean } 1959d538eca8SVladimir Oltean 1960d538eca8SVladimir Oltean return 0; 1961d538eca8SVladimir Oltean } 1962d538eca8SVladimir Oltean 196347d75f78SVladimir Oltean static int __maybe_unused 196447d75f78SVladimir Oltean dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app) 196547d75f78SVladimir Oltean { 196647d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 196747d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 196847d75f78SVladimir Oltean int err, port = dp->index; 196947d75f78SVladimir Oltean u8 dscp = app->protocol; 197047d75f78SVladimir Oltean 197147d75f78SVladimir Oltean if (!ds->ops->port_del_dscp_prio) 197247d75f78SVladimir Oltean return -EOPNOTSUPP; 197347d75f78SVladimir Oltean 197447d75f78SVladimir Oltean err = dcb_ieee_delapp(dev, app); 197547d75f78SVladimir Oltean if (err) 197647d75f78SVladimir Oltean return err; 197747d75f78SVladimir Oltean 197847d75f78SVladimir Oltean err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority); 197947d75f78SVladimir Oltean if (err) { 198047d75f78SVladimir Oltean dcb_ieee_setapp(dev, app); 198147d75f78SVladimir Oltean return err; 198247d75f78SVladimir Oltean } 198347d75f78SVladimir Oltean 198447d75f78SVladimir Oltean return 0; 198547d75f78SVladimir Oltean } 198647d75f78SVladimir Oltean 1987d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev, 1988d538eca8SVladimir Oltean struct dcb_app *app) 1989d538eca8SVladimir Oltean { 1990d538eca8SVladimir Oltean switch (app->selector) { 1991d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1992d538eca8SVladimir Oltean switch (app->protocol) { 1993d538eca8SVladimir Oltean case 0: 1994d538eca8SVladimir Oltean return dsa_slave_dcbnl_del_default_prio(dev, app); 1995d538eca8SVladimir Oltean default: 1996d538eca8SVladimir Oltean return -EOPNOTSUPP; 1997d538eca8SVladimir Oltean } 1998d538eca8SVladimir Oltean break; 199947d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 200047d75f78SVladimir Oltean return dsa_slave_dcbnl_del_dscp_prio(dev, app); 2001d538eca8SVladimir Oltean default: 2002d538eca8SVladimir Oltean return -EOPNOTSUPP; 2003d538eca8SVladimir Oltean } 2004d538eca8SVladimir Oltean } 2005d538eca8SVladimir Oltean 2006d538eca8SVladimir Oltean /* Pre-populate the DCB application priority table with the priorities 2007d538eca8SVladimir Oltean * configured during switch setup, which we read from hardware here. 2008d538eca8SVladimir Oltean */ 2009d538eca8SVladimir Oltean static int dsa_slave_dcbnl_init(struct net_device *dev) 2010d538eca8SVladimir Oltean { 2011d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2012d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 2013d538eca8SVladimir Oltean int port = dp->index; 2014d538eca8SVladimir Oltean int err; 2015d538eca8SVladimir Oltean 2016d538eca8SVladimir Oltean if (ds->ops->port_get_default_prio) { 2017d538eca8SVladimir Oltean int prio = ds->ops->port_get_default_prio(ds, port); 2018d538eca8SVladimir Oltean struct dcb_app app = { 2019d538eca8SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE, 2020d538eca8SVladimir Oltean .protocol = 0, 2021d538eca8SVladimir Oltean .priority = prio, 2022d538eca8SVladimir Oltean }; 2023d538eca8SVladimir Oltean 2024d538eca8SVladimir Oltean if (prio < 0) 2025d538eca8SVladimir Oltean return prio; 2026d538eca8SVladimir Oltean 2027d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 2028d538eca8SVladimir Oltean if (err) 2029d538eca8SVladimir Oltean return err; 2030d538eca8SVladimir Oltean } 2031d538eca8SVladimir Oltean 203247d75f78SVladimir Oltean if (ds->ops->port_get_dscp_prio) { 203347d75f78SVladimir Oltean int protocol; 203447d75f78SVladimir Oltean 203547d75f78SVladimir Oltean for (protocol = 0; protocol < 64; protocol++) { 203647d75f78SVladimir Oltean struct dcb_app app = { 203747d75f78SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_DSCP, 203847d75f78SVladimir Oltean .protocol = protocol, 203947d75f78SVladimir Oltean }; 204047d75f78SVladimir Oltean int prio; 204147d75f78SVladimir Oltean 204247d75f78SVladimir Oltean prio = ds->ops->port_get_dscp_prio(ds, port, protocol); 204347d75f78SVladimir Oltean if (prio == -EOPNOTSUPP) 204447d75f78SVladimir Oltean continue; 204547d75f78SVladimir Oltean if (prio < 0) 204647d75f78SVladimir Oltean return prio; 204747d75f78SVladimir Oltean 204847d75f78SVladimir Oltean app.priority = prio; 204947d75f78SVladimir Oltean 205047d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 205147d75f78SVladimir Oltean if (err) 205247d75f78SVladimir Oltean return err; 205347d75f78SVladimir Oltean } 205447d75f78SVladimir Oltean } 205547d75f78SVladimir Oltean 2056d538eca8SVladimir Oltean return 0; 2057d538eca8SVladimir Oltean } 2058d538eca8SVladimir Oltean 205991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 206091da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 20613d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 20623d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 2063aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 2064c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 20656793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 20666793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 20676793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 206891da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 206991da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 207091da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 2071487d3855SAlvin Šipraga .get_eth_phy_stats = dsa_slave_get_eth_phy_stats, 2072487d3855SAlvin Šipraga .get_eth_mac_stats = dsa_slave_get_eth_mac_stats, 2073487d3855SAlvin Šipraga .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats, 207419e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 207519e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 20767905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 20777905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 2078aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 2079aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 2080a2a1a13bSHeiner Kallweit .get_pauseparam = dsa_slave_get_pauseparam, 2081a2a1a13bSHeiner Kallweit .set_pauseparam = dsa_slave_set_pauseparam, 2082bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 2083bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 20840336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 2085a71acad9SOleksij Rempel .self_test = dsa_slave_net_selftest, 208691da11f8SLennert Buytenhek }; 208791da11f8SLennert Buytenhek 2088d538eca8SVladimir Oltean static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = { 2089d538eca8SVladimir Oltean .ieee_setapp = dsa_slave_dcbnl_ieee_setapp, 2090d538eca8SVladimir Oltean .ieee_delapp = dsa_slave_dcbnl_ieee_delapp, 2091d538eca8SVladimir Oltean }; 2092d538eca8SVladimir Oltean 2093716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 2094716efee2SJiri Pirko { 2095716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 2096716efee2SJiri Pirko 209745b987d5SVladimir Oltean return &dp->devlink_port; 2098716efee2SJiri Pirko } 2099716efee2SJiri Pirko 2100c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev, 2101c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s) 2102c2ec5f2eSOleksij Rempel { 2103c2ec5f2eSOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(dev); 2104c2ec5f2eSOleksij Rempel struct dsa_switch *ds = dp->ds; 2105c2ec5f2eSOleksij Rempel 2106c2ec5f2eSOleksij Rempel if (ds->ops->get_stats64) 2107c2ec5f2eSOleksij Rempel ds->ops->get_stats64(ds, dp->index, s); 2108c2ec5f2eSOleksij Rempel else 2109c2ec5f2eSOleksij Rempel dev_get_tstats64(dev, s); 2110c2ec5f2eSOleksij Rempel } 2111c2ec5f2eSOleksij Rempel 21120994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 21130994d492SFelix Fietkau struct net_device_path *path) 21140994d492SFelix Fietkau { 21150994d492SFelix Fietkau struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 21160994d492SFelix Fietkau struct dsa_port *cpu_dp = dp->cpu_dp; 21170994d492SFelix Fietkau 21180994d492SFelix Fietkau path->dev = ctx->dev; 21190994d492SFelix Fietkau path->type = DEV_PATH_DSA; 21200994d492SFelix Fietkau path->dsa.proto = cpu_dp->tag_ops->proto; 21210994d492SFelix Fietkau path->dsa.port = dp->index; 21220994d492SFelix Fietkau ctx->dev = cpu_dp->master; 21230994d492SFelix Fietkau 21240994d492SFelix Fietkau return 0; 21250994d492SFelix Fietkau } 21260994d492SFelix Fietkau 21273e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 2128d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 2129d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 21303e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 2131d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 2132d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 2133d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 21342bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 2135a7605370SArnd Bergmann .ndo_eth_ioctl = dsa_slave_ioctl, 2136abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 213704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 213804ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 213904ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 214004ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 214104ff53f9SFlorian Fainelli #endif 2142f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 2143c2ec5f2eSOleksij Rempel .ndo_get_stats64 = dsa_slave_get_stats64, 2144061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 2145061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 2146716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 2147bfcb8132SVladimir Oltean .ndo_change_mtu = dsa_slave_change_mtu, 21480994d492SFelix Fietkau .ndo_fill_forward_path = dsa_slave_fill_forward_path, 214998237d43SScott Feldman }; 215098237d43SScott Feldman 2151f37db85dSFlorian Fainelli static struct device_type dsa_type = { 2152f37db85dSFlorian Fainelli .name = "dsa", 2153f37db85dSFlorian Fainelli }; 2154f37db85dSFlorian Fainelli 2155aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 2156aab9c406SFlorian Fainelli { 2157aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 2158aab9c406SFlorian Fainelli 2159765bda93SRussell King if (dp->pl) 2160aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 2161aab9c406SFlorian Fainelli } 2162aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 2163aab9c406SFlorian Fainelli 21645c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 2165aab9c406SFlorian Fainelli struct phylink_link_state *state) 2166aab9c406SFlorian Fainelli { 21675c05c1dbSRussell King struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 2168aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 2169aab9c406SFlorian Fainelli 2170aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 2171aab9c406SFlorian Fainelli * not be called if it was not. 2172aab9c406SFlorian Fainelli */ 2173aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 2174ce31b31cSFlorian Fainelli } 2175ce31b31cSFlorian Fainelli 217691da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 2177c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr, 2178c916e8e1SOleksij Rempel u32 flags) 2179c305c165SFlorian Fainelli { 2180d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2181d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 2182c305c165SFlorian Fainelli 21830115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 21840115dcd1SVivien Didelot if (!slave_dev->phydev) { 2185d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 2186c305c165SFlorian Fainelli return -ENODEV; 2187d25b8e74SRussell King } 2188c305c165SFlorian Fainelli 2189c916e8e1SOleksij Rempel slave_dev->phydev->dev_flags |= flags; 2190c916e8e1SOleksij Rempel 2191aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 2192c305c165SFlorian Fainelli } 2193c305c165SFlorian Fainelli 21944fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 21950d8bcdd3SFlorian Fainelli { 2196d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2197d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 2198d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 21996819563eSFlorian Fainelli u32 phy_flags = 0; 22000c65b2b9SAndrew Lunn int ret; 22010d8bcdd3SFlorian Fainelli 220244cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 220344cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 220444cc27e4SIoana Ciornei 22055c05c1dbSRussell King /* The get_fixed_state callback takes precedence over polling the 22065c05c1dbSRussell King * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 22075c05c1dbSRussell King * this if the switch provides such a callback. 22085c05c1dbSRussell King */ 22095c05c1dbSRussell King if (ds->ops->phylink_fixed_state) { 22105c05c1dbSRussell King dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 22115c05c1dbSRussell King dp->pl_config.poll_fixed_state = true; 22125c05c1dbSRussell King } 22135c05c1dbSRussell King 221421bd64bdSRussell King (Oracle) ret = dsa_port_phylink_create(dp); 221521bd64bdSRussell King (Oracle) if (ret) 221621bd64bdSRussell King (Oracle) return ret; 2217aab9c406SFlorian Fainelli 22189d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 2219d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 22206819563eSFlorian Fainelli 2221aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 22226146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 22236146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 22246146dd45SVladimir Oltean * use the switch internal MDIO bus instead 22250d8bcdd3SFlorian Fainelli */ 2226c916e8e1SOleksij Rempel ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags); 2227d25b8e74SRussell King } 22286a52e733SVladimir Oltean if (ret) { 22296a52e733SVladimir Oltean netdev_err(slave_dev, "failed to connect to PHY: %pe\n", 22306a52e733SVladimir Oltean ERR_PTR(ret)); 22316a52e733SVladimir Oltean phylink_destroy(dp->pl); 22320d8bcdd3SFlorian Fainelli } 22339697f1cdSFlorian Fainelli 22346146dd45SVladimir Oltean return ret; 2235b31f65fbSAndrew Lunn } 22360d8bcdd3SFlorian Fainelli 223753da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave) 223853da0ebaSVladimir Oltean { 223953da0ebaSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(slave); 224053da0ebaSVladimir Oltean struct dsa_slave_priv *p = netdev_priv(slave); 224153da0ebaSVladimir Oltean const struct dsa_port *cpu_dp = dp->cpu_dp; 224253da0ebaSVladimir Oltean struct net_device *master = cpu_dp->master; 224358adf9dcSVladimir Oltean const struct dsa_switch *ds = dp->ds; 224453da0ebaSVladimir Oltean 22454e500251SVladimir Oltean slave->needed_headroom = cpu_dp->tag_ops->needed_headroom; 22464e500251SVladimir Oltean slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom; 224753da0ebaSVladimir Oltean /* Try to save one extra realloc later in the TX path (in the master) 224853da0ebaSVladimir Oltean * by also inheriting the master's needed headroom and tailroom. 224953da0ebaSVladimir Oltean * The 8021q driver also does this. 225053da0ebaSVladimir Oltean */ 225153da0ebaSVladimir Oltean slave->needed_headroom += master->needed_headroom; 225253da0ebaSVladimir Oltean slave->needed_tailroom += master->needed_tailroom; 225353da0ebaSVladimir Oltean 225453da0ebaSVladimir Oltean p->xmit = cpu_dp->tag_ops->xmit; 225521cf377aSLino Sanfilippo 225621cf377aSLino Sanfilippo slave->features = master->vlan_features | NETIF_F_HW_TC; 225721cf377aSLino Sanfilippo slave->hw_features |= NETIF_F_HW_TC; 225821cf377aSLino Sanfilippo slave->features |= NETIF_F_LLTX; 225921cf377aSLino Sanfilippo if (slave->needed_tailroom) 226021cf377aSLino Sanfilippo slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST); 226158adf9dcSVladimir Oltean if (ds->needs_standalone_vlan_filtering) 226258adf9dcSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 226353da0ebaSVladimir Oltean } 226453da0ebaSVladimir Oltean 226524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 226624462549SFlorian Fainelli { 2267aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 226824462549SFlorian Fainelli 2269a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2270a94c689eSFlorian Fainelli return 0; 2271a94c689eSFlorian Fainelli 2272f154be24SFlorian Fainelli netif_device_detach(slave_dev); 2273f154be24SFlorian Fainelli 2274aab9c406SFlorian Fainelli rtnl_lock(); 2275aab9c406SFlorian Fainelli phylink_stop(dp->pl); 2276aab9c406SFlorian Fainelli rtnl_unlock(); 227724462549SFlorian Fainelli 227824462549SFlorian Fainelli return 0; 227924462549SFlorian Fainelli } 228024462549SFlorian Fainelli 228124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 228224462549SFlorian Fainelli { 2283aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2284aab9c406SFlorian Fainelli 2285a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2286a94c689eSFlorian Fainelli return 0; 2287a94c689eSFlorian Fainelli 228824462549SFlorian Fainelli netif_device_attach(slave_dev); 228924462549SFlorian Fainelli 2290aab9c406SFlorian Fainelli rtnl_lock(); 2291aab9c406SFlorian Fainelli phylink_start(dp->pl); 2292aab9c406SFlorian Fainelli rtnl_unlock(); 229324462549SFlorian Fainelli 229424462549SFlorian Fainelli return 0; 229524462549SFlorian Fainelli } 229624462549SFlorian Fainelli 2297951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 229891da11f8SLennert Buytenhek { 229924a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 2300f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 23014cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 2302951259aaSVivien Didelot const char *name = port->name; 230391da11f8SLennert Buytenhek struct net_device *slave_dev; 230491da11f8SLennert Buytenhek struct dsa_slave_priv *p; 230591da11f8SLennert Buytenhek int ret; 230691da11f8SLennert Buytenhek 230755199df6SFlorian Fainelli if (!ds->num_tx_queues) 230855199df6SFlorian Fainelli ds->num_tx_queues = 1; 230955199df6SFlorian Fainelli 231055199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 231155199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 231255199df6SFlorian Fainelli ds->num_tx_queues, 1); 231391da11f8SLennert Buytenhek if (slave_dev == NULL) 2314d87d6f44SGuenter Roeck return -ENOMEM; 231591da11f8SLennert Buytenhek 23167ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 2317d538eca8SVladimir Oltean #if IS_ENABLED(CONFIG_DCB) 2318d538eca8SVladimir Oltean slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops; 2319d538eca8SVladimir Oltean #endif 232083216e39SMichael Walle if (!is_zero_ether_addr(port->mac)) 2321e35b8d7dSJakub Kicinski eth_hw_addr_set(slave_dev, port->mac); 2322a2c7023fSXiaofei Shen else 23232fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 23240a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 23255e8a1e03SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 23265e8a1e03SVladimir Oltean slave_dev->priv_flags |= IFF_UNICAST_FLT; 23273e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 2328bfcb8132SVladimir Oltean if (ds->ops->port_max_mtu) 2329bfcb8132SVladimir Oltean slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 2330f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 2331d442ad4aSStephen Hemminger 23324cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 23334cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 233491da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 233591da11f8SLennert Buytenhek 233691da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 23376a900628SHeiner Kallweit slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 23386a900628SHeiner Kallweit if (!slave_dev->tstats) { 23395f6b4e14SFlorian Fainelli free_netdev(slave_dev); 23405f6b4e14SFlorian Fainelli return -ENOMEM; 23415f6b4e14SFlorian Fainelli } 2342e131a563SAlexander Lobakin 2343e131a563SAlexander Lobakin ret = gro_cells_init(&p->gcells, slave_dev); 2344e131a563SAlexander Lobakin if (ret) 2345e131a563SAlexander Lobakin goto out_free; 2346e131a563SAlexander Lobakin 23474cfbf09cSVivien Didelot p->dp = port; 2348f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 2349f8b8b1cdSVivien Didelot port->slave = slave_dev; 235053da0ebaSVladimir Oltean dsa_slave_setup_tagger(slave_dev); 235191da11f8SLennert Buytenhek 235291da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 235391da11f8SLennert Buytenhek 23544fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 23550071f56eSAndrew Lunn if (ret) { 2356c9ebf126SVladimir Oltean netdev_err(slave_dev, 2357c9ebf126SVladimir Oltean "error %d setting up PHY for tree %d, switch %d, port %d\n", 2358c9ebf126SVladimir Oltean ret, ds->dst->index, ds->index, port->index); 2359e131a563SAlexander Lobakin goto out_gcells; 2360e804441cSFlorian Fainelli } 2361e804441cSFlorian Fainelli 23622f1e8ea7SVladimir Oltean rtnl_lock(); 2363e31dbd3bSVladimir Oltean 2364904e112aSVladimir Oltean ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 2365904e112aSVladimir Oltean if (ret && ret != -EOPNOTSUPP) 2366904e112aSVladimir Oltean dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 2367904e112aSVladimir Oltean ret, ETH_DATA_LEN, port->index); 2368904e112aSVladimir Oltean 23692f1e8ea7SVladimir Oltean ret = register_netdevice(slave_dev); 2370e804441cSFlorian Fainelli if (ret) { 2371e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 2372e804441cSFlorian Fainelli ret, slave_dev->name); 23732f1e8ea7SVladimir Oltean rtnl_unlock(); 2374e804441cSFlorian Fainelli goto out_phy; 23750071f56eSAndrew Lunn } 23760071f56eSAndrew Lunn 2377d538eca8SVladimir Oltean if (IS_ENABLED(CONFIG_DCB)) { 2378d538eca8SVladimir Oltean ret = dsa_slave_dcbnl_init(slave_dev); 2379d538eca8SVladimir Oltean if (ret) { 2380d538eca8SVladimir Oltean netdev_err(slave_dev, 2381d538eca8SVladimir Oltean "failed to initialize DCB: %pe\n", 2382d538eca8SVladimir Oltean ERR_PTR(ret)); 2383d538eca8SVladimir Oltean rtnl_unlock(); 2384d538eca8SVladimir Oltean goto out_unregister; 2385d538eca8SVladimir Oltean } 2386d538eca8SVladimir Oltean } 2387d538eca8SVladimir Oltean 23882f1e8ea7SVladimir Oltean ret = netdev_upper_dev_link(master, slave_dev, NULL); 23892f1e8ea7SVladimir Oltean 23902f1e8ea7SVladimir Oltean rtnl_unlock(); 23912f1e8ea7SVladimir Oltean 23922f1e8ea7SVladimir Oltean if (ret) 23932f1e8ea7SVladimir Oltean goto out_unregister; 23942f1e8ea7SVladimir Oltean 2395d87d6f44SGuenter Roeck return 0; 2396e804441cSFlorian Fainelli 23972f1e8ea7SVladimir Oltean out_unregister: 23982f1e8ea7SVladimir Oltean unregister_netdev(slave_dev); 2399e804441cSFlorian Fainelli out_phy: 2400aab9c406SFlorian Fainelli rtnl_lock(); 2401aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 2402aab9c406SFlorian Fainelli rtnl_unlock(); 2403aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 2404e131a563SAlexander Lobakin out_gcells: 2405e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 2406e804441cSFlorian Fainelli out_free: 24076a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2408e804441cSFlorian Fainelli free_netdev(slave_dev); 2409f8b8b1cdSVivien Didelot port->slave = NULL; 2410e804441cSFlorian Fainelli return ret; 241191da11f8SLennert Buytenhek } 2412b73adef6SFlorian Fainelli 2413cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 2414cda5c15bSNeil Armstrong { 24152f1e8ea7SVladimir Oltean struct net_device *master = dsa_slave_to_master(slave_dev); 2416d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2417cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 2418cda5c15bSNeil Armstrong 2419cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 2420aab9c406SFlorian Fainelli rtnl_lock(); 24212f1e8ea7SVladimir Oltean netdev_upper_dev_unlink(master, slave_dev); 24222f1e8ea7SVladimir Oltean unregister_netdevice(slave_dev); 2423aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 2424aab9c406SFlorian Fainelli rtnl_unlock(); 2425881eadabSJohan Hovold 2426aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 2427e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 24286a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2429cda5c15bSNeil Armstrong free_netdev(slave_dev); 2430cda5c15bSNeil Armstrong } 2431cda5c15bSNeil Armstrong 24324d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev) 2433b73adef6SFlorian Fainelli { 2434b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 2435b73adef6SFlorian Fainelli } 2436a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2437b73adef6SFlorian Fainelli 24388e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 24398e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 2440b73adef6SFlorian Fainelli { 2441d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 24422afc526aSVladimir Oltean struct netlink_ext_ack *extack; 24438e92ab3aSVivien Didelot int err = NOTIFY_DONE; 2444b73adef6SFlorian Fainelli 24452afc526aSVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 24462afc526aSVladimir Oltean 24478e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 24488e92ab3aSVivien Didelot if (info->linking) { 24492afc526aSVladimir Oltean err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2450bff33f7eSVladimir Oltean if (!err) 2451bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 245267b5fb5dSVladimir Oltean if (err == -EOPNOTSUPP) { 245367b5fb5dSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 245467b5fb5dSVladimir Oltean "Offloading not supported"); 245567b5fb5dSVladimir Oltean err = 0; 245667b5fb5dSVladimir Oltean } 24578e92ab3aSVivien Didelot err = notifier_from_errno(err); 24588e92ab3aSVivien Didelot } else { 245917d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 24608e92ab3aSVivien Didelot err = NOTIFY_OK; 24618e92ab3aSVivien Didelot } 2462058102a6STobias Waldekranz } else if (netif_is_lag_master(info->upper_dev)) { 2463058102a6STobias Waldekranz if (info->linking) { 2464058102a6STobias Waldekranz err = dsa_port_lag_join(dp, info->upper_dev, 24652afc526aSVladimir Oltean info->upper_info, extack); 2466058102a6STobias Waldekranz if (err == -EOPNOTSUPP) { 2467058102a6STobias Waldekranz NL_SET_ERR_MSG_MOD(info->info.extack, 2468058102a6STobias Waldekranz "Offloading not supported"); 2469058102a6STobias Waldekranz err = 0; 2470058102a6STobias Waldekranz } 2471058102a6STobias Waldekranz err = notifier_from_errno(err); 2472058102a6STobias Waldekranz } else { 2473058102a6STobias Waldekranz dsa_port_lag_leave(dp, info->upper_dev); 2474058102a6STobias Waldekranz err = NOTIFY_OK; 2475058102a6STobias Waldekranz } 247618596f50SGeorge McCollister } else if (is_hsr_master(info->upper_dev)) { 247718596f50SGeorge McCollister if (info->linking) { 247818596f50SGeorge McCollister err = dsa_port_hsr_join(dp, info->upper_dev); 247918596f50SGeorge McCollister if (err == -EOPNOTSUPP) { 248018596f50SGeorge McCollister NL_SET_ERR_MSG_MOD(info->info.extack, 248118596f50SGeorge McCollister "Offloading not supported"); 248218596f50SGeorge McCollister err = 0; 248318596f50SGeorge McCollister } 248418596f50SGeorge McCollister err = notifier_from_errno(err); 248518596f50SGeorge McCollister } else { 248618596f50SGeorge McCollister dsa_port_hsr_leave(dp, info->upper_dev); 248718596f50SGeorge McCollister err = NOTIFY_OK; 248818596f50SGeorge McCollister } 2489058102a6STobias Waldekranz } 2490058102a6STobias Waldekranz 2491058102a6STobias Waldekranz return err; 2492058102a6STobias Waldekranz } 2493058102a6STobias Waldekranz 249474918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev, 249574918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 249674918945SVladimir Oltean { 249774918945SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 249874918945SVladimir Oltean 249974918945SVladimir Oltean if (netif_is_bridge_master(info->upper_dev) && !info->linking) 25004e51bf44SVladimir Oltean dsa_port_pre_bridge_leave(dp, info->upper_dev); 250174918945SVladimir Oltean else if (netif_is_lag_master(info->upper_dev) && !info->linking) 25024e51bf44SVladimir Oltean dsa_port_pre_lag_leave(dp, info->upper_dev); 250374918945SVladimir Oltean /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be 250474918945SVladimir Oltean * meaningfully enslaved to a bridge yet 250574918945SVladimir Oltean */ 250674918945SVladimir Oltean 25074e51bf44SVladimir Oltean return NOTIFY_DONE; 250874918945SVladimir Oltean } 250974918945SVladimir Oltean 2510058102a6STobias Waldekranz static int 2511058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev, 2512058102a6STobias Waldekranz struct netdev_notifier_changeupper_info *info) 2513058102a6STobias Waldekranz { 2514058102a6STobias Waldekranz struct net_device *lower; 2515058102a6STobias Waldekranz struct list_head *iter; 2516058102a6STobias Waldekranz int err = NOTIFY_DONE; 2517058102a6STobias Waldekranz struct dsa_port *dp; 2518058102a6STobias Waldekranz 2519058102a6STobias Waldekranz netdev_for_each_lower_dev(dev, lower, iter) { 2520058102a6STobias Waldekranz if (!dsa_slave_dev_check(lower)) 2521058102a6STobias Waldekranz continue; 2522058102a6STobias Waldekranz 2523058102a6STobias Waldekranz dp = dsa_slave_to_port(lower); 2524dedd6a00SVladimir Oltean if (!dp->lag) 2525058102a6STobias Waldekranz /* Software LAG */ 2526058102a6STobias Waldekranz continue; 2527058102a6STobias Waldekranz 2528058102a6STobias Waldekranz err = dsa_slave_changeupper(lower, info); 2529058102a6STobias Waldekranz if (notifier_to_errno(err)) 2530058102a6STobias Waldekranz break; 25316debb68aSVivien Didelot } 2532b73adef6SFlorian Fainelli 25338e92ab3aSVivien Didelot return err; 2534b73adef6SFlorian Fainelli } 2535b73adef6SFlorian Fainelli 253674918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls 253774918945SVladimir Oltean * dsa_slave_prechangeupper() 253874918945SVladimir Oltean */ 253974918945SVladimir Oltean static int 254074918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev, 254174918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 254274918945SVladimir Oltean { 254374918945SVladimir Oltean struct net_device *lower; 254474918945SVladimir Oltean struct list_head *iter; 254574918945SVladimir Oltean int err = NOTIFY_DONE; 254674918945SVladimir Oltean struct dsa_port *dp; 254774918945SVladimir Oltean 254874918945SVladimir Oltean netdev_for_each_lower_dev(dev, lower, iter) { 254974918945SVladimir Oltean if (!dsa_slave_dev_check(lower)) 255074918945SVladimir Oltean continue; 255174918945SVladimir Oltean 255274918945SVladimir Oltean dp = dsa_slave_to_port(lower); 2553dedd6a00SVladimir Oltean if (!dp->lag) 255474918945SVladimir Oltean /* Software LAG */ 255574918945SVladimir Oltean continue; 255674918945SVladimir Oltean 255774918945SVladimir Oltean err = dsa_slave_prechangeupper(lower, info); 255874918945SVladimir Oltean if (notifier_to_errno(err)) 255974918945SVladimir Oltean break; 256074918945SVladimir Oltean } 256174918945SVladimir Oltean 256274918945SVladimir Oltean return err; 256374918945SVladimir Oltean } 256474918945SVladimir Oltean 2565eb46e8daSVladimir Oltean static int 2566eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2567eb46e8daSVladimir Oltean struct netdev_notifier_changeupper_info *info) 2568cc1d5bdaSFlorian Fainelli { 2569cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 257036cbf39bSVladimir Oltean struct net_device *slave, *br; 2571cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 2572cc1d5bdaSFlorian Fainelli 2573cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 2574cc1d5bdaSFlorian Fainelli 2575cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 2576cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2577cc1d5bdaSFlorian Fainelli 2578cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 2579cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 2580cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2581cc1d5bdaSFlorian Fainelli 2582cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 258336cbf39bSVladimir Oltean br = dsa_port_bridge_dev_get(dp); 258436cbf39bSVladimir Oltean if (!br) 2585cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2586cc1d5bdaSFlorian Fainelli 2587cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 258836cbf39bSVladimir Oltean if (br_vlan_enabled(br) && 2589cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 2590cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 2591cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 2592cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 2593cc1d5bdaSFlorian Fainelli } 2594cc1d5bdaSFlorian Fainelli 2595cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2596cc1d5bdaSFlorian Fainelli } 2597cc1d5bdaSFlorian Fainelli 25982b138406SVladimir Oltean static int 25992b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev, 26002b138406SVladimir Oltean struct netdev_notifier_changeupper_info *info) 26012b138406SVladimir Oltean { 26022b138406SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 260336cbf39bSVladimir Oltean struct net_device *br = dsa_port_bridge_dev_get(dp); 26042b138406SVladimir Oltean struct bridge_vlan_info br_info; 26052b138406SVladimir Oltean struct netlink_ext_ack *extack; 26062b138406SVladimir Oltean int err = NOTIFY_DONE; 26072b138406SVladimir Oltean u16 vid; 26082b138406SVladimir Oltean 2609adb256ebSVladimir Oltean if (!br || !br_vlan_enabled(br)) 26102b138406SVladimir Oltean return NOTIFY_DONE; 26112b138406SVladimir Oltean 26122b138406SVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 26132b138406SVladimir Oltean vid = vlan_dev_vlan_id(info->upper_dev); 26142b138406SVladimir Oltean 26152b138406SVladimir Oltean /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 26162b138406SVladimir Oltean * device, respectively the VID is not found, returning 26172b138406SVladimir Oltean * 0 means success, which is a failure for us here. 26182b138406SVladimir Oltean */ 26192b138406SVladimir Oltean err = br_vlan_get_info(br, vid, &br_info); 26202b138406SVladimir Oltean if (err == 0) { 26212b138406SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 26222b138406SVladimir Oltean "This VLAN is already configured by the bridge"); 26232b138406SVladimir Oltean return notifier_from_errno(-EBUSY); 26242b138406SVladimir Oltean } 26252b138406SVladimir Oltean 26262b138406SVladimir Oltean return NOTIFY_DONE; 26272b138406SVladimir Oltean } 26282b138406SVladimir Oltean 26294ede74e7SVladimir Oltean static int 26304ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev, 26314ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info) 2632b73adef6SFlorian Fainelli { 2633e358bef7SVladimir Oltean struct dsa_switch *ds; 2634e358bef7SVladimir Oltean struct dsa_port *dp; 2635e358bef7SVladimir Oltean int err; 26362b138406SVladimir Oltean 263753bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 26384ede74e7SVladimir Oltean return dsa_prevent_bridging_8021q_upper(dev, info); 26392b138406SVladimir Oltean 2640e358bef7SVladimir Oltean dp = dsa_slave_to_port(dev); 2641e358bef7SVladimir Oltean ds = dp->ds; 2642e358bef7SVladimir Oltean 2643e358bef7SVladimir Oltean if (ds->ops->port_prechangeupper) { 2644e358bef7SVladimir Oltean err = ds->ops->port_prechangeupper(ds, dp->index, info); 2645e358bef7SVladimir Oltean if (err) 2646e358bef7SVladimir Oltean return notifier_from_errno(err); 2647e358bef7SVladimir Oltean } 2648e358bef7SVladimir Oltean 26492b138406SVladimir Oltean if (is_vlan_dev(info->upper_dev)) 26504ede74e7SVladimir Oltean return dsa_slave_check_8021q_upper(dev, info); 26514ede74e7SVladimir Oltean 26524ede74e7SVladimir Oltean return NOTIFY_DONE; 26534ede74e7SVladimir Oltean } 26544ede74e7SVladimir Oltean 26554ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb, 26564ede74e7SVladimir Oltean unsigned long event, void *ptr) 26574ede74e7SVladimir Oltean { 26584ede74e7SVladimir Oltean struct net_device *dev = netdev_notifier_info_to_dev(ptr); 26594ede74e7SVladimir Oltean 26604ede74e7SVladimir Oltean switch (event) { 26614ede74e7SVladimir Oltean case NETDEV_PRECHANGEUPPER: { 26624ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info = ptr; 26634ede74e7SVladimir Oltean int err; 26644ede74e7SVladimir Oltean 26654ede74e7SVladimir Oltean err = dsa_slave_prechangeupper_sanity_check(dev, info); 26664ede74e7SVladimir Oltean if (err != NOTIFY_DONE) 26674ede74e7SVladimir Oltean return err; 26684ede74e7SVladimir Oltean 266974918945SVladimir Oltean if (dsa_slave_dev_check(dev)) 267074918945SVladimir Oltean return dsa_slave_prechangeupper(dev, ptr); 267174918945SVladimir Oltean 267274918945SVladimir Oltean if (netif_is_lag_master(dev)) 267374918945SVladimir Oltean return dsa_slave_lag_prechangeupper(dev, ptr); 267474918945SVladimir Oltean 267583501299SVladimir Oltean break; 26762b138406SVladimir Oltean } 267783501299SVladimir Oltean case NETDEV_CHANGEUPPER: 2678058102a6STobias Waldekranz if (dsa_slave_dev_check(dev)) 26798e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 2680058102a6STobias Waldekranz 2681058102a6STobias Waldekranz if (netif_is_lag_master(dev)) 2682058102a6STobias Waldekranz return dsa_slave_lag_changeupper(dev, ptr); 2683058102a6STobias Waldekranz 2684058102a6STobias Waldekranz break; 2685058102a6STobias Waldekranz case NETDEV_CHANGELOWERSTATE: { 2686058102a6STobias Waldekranz struct netdev_notifier_changelowerstate_info *info = ptr; 2687058102a6STobias Waldekranz struct dsa_port *dp; 2688058102a6STobias Waldekranz int err; 2689058102a6STobias Waldekranz 2690058102a6STobias Waldekranz if (!dsa_slave_dev_check(dev)) 2691058102a6STobias Waldekranz break; 2692058102a6STobias Waldekranz 2693058102a6STobias Waldekranz dp = dsa_slave_to_port(dev); 2694058102a6STobias Waldekranz 2695058102a6STobias Waldekranz err = dsa_port_lag_change(dp, info->lower_state_info); 2696058102a6STobias Waldekranz return notifier_from_errno(err); 2697058102a6STobias Waldekranz } 2698295ab96fSVladimir Oltean case NETDEV_CHANGE: 2699295ab96fSVladimir Oltean case NETDEV_UP: { 2700295ab96fSVladimir Oltean /* Track state of master port. 2701295ab96fSVladimir Oltean * DSA driver may require the master port (and indirectly 2702295ab96fSVladimir Oltean * the tagger) to be available for some special operation. 2703295ab96fSVladimir Oltean */ 2704295ab96fSVladimir Oltean if (netdev_uses_dsa(dev)) { 2705295ab96fSVladimir Oltean struct dsa_port *cpu_dp = dev->dsa_ptr; 2706295ab96fSVladimir Oltean struct dsa_switch_tree *dst = cpu_dp->ds->dst; 2707295ab96fSVladimir Oltean 2708295ab96fSVladimir Oltean /* Track when the master port is UP */ 2709295ab96fSVladimir Oltean dsa_tree_master_oper_state_change(dst, dev, 2710295ab96fSVladimir Oltean netif_oper_up(dev)); 2711295ab96fSVladimir Oltean 2712295ab96fSVladimir Oltean /* Track when the master port is ready and can accept 2713295ab96fSVladimir Oltean * packet. 2714295ab96fSVladimir Oltean * NETDEV_UP event is not enough to flag a port as ready. 2715295ab96fSVladimir Oltean * We also have to wait for linkwatch_do_dev to dev_activate 2716295ab96fSVladimir Oltean * and emit a NETDEV_CHANGE event. 2717295ab96fSVladimir Oltean * We check if a master port is ready by checking if the dev 2718295ab96fSVladimir Oltean * have a qdisc assigned and is not noop. 2719295ab96fSVladimir Oltean */ 2720295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, 2721295ab96fSVladimir Oltean !qdisc_tx_is_noop(dev)); 2722295ab96fSVladimir Oltean 2723295ab96fSVladimir Oltean return NOTIFY_OK; 2724295ab96fSVladimir Oltean } 2725295ab96fSVladimir Oltean 2726295ab96fSVladimir Oltean return NOTIFY_DONE; 2727295ab96fSVladimir Oltean } 2728c0a8a9c2SVladimir Oltean case NETDEV_GOING_DOWN: { 2729c0a8a9c2SVladimir Oltean struct dsa_port *dp, *cpu_dp; 2730c0a8a9c2SVladimir Oltean struct dsa_switch_tree *dst; 2731c0a8a9c2SVladimir Oltean LIST_HEAD(close_list); 2732c0a8a9c2SVladimir Oltean 2733c0a8a9c2SVladimir Oltean if (!netdev_uses_dsa(dev)) 2734c0a8a9c2SVladimir Oltean return NOTIFY_DONE; 2735c0a8a9c2SVladimir Oltean 2736c0a8a9c2SVladimir Oltean cpu_dp = dev->dsa_ptr; 2737c0a8a9c2SVladimir Oltean dst = cpu_dp->ds->dst; 2738c0a8a9c2SVladimir Oltean 2739295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, false); 2740295ab96fSVladimir Oltean 2741c0a8a9c2SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 2742d0004a02SVladimir Oltean if (!dsa_port_is_user(dp)) 2743c0a8a9c2SVladimir Oltean continue; 2744c0a8a9c2SVladimir Oltean 2745c0a8a9c2SVladimir Oltean list_add(&dp->slave->close_list, &close_list); 2746c0a8a9c2SVladimir Oltean } 2747c0a8a9c2SVladimir Oltean 2748c0a8a9c2SVladimir Oltean dev_close_many(&close_list, true); 2749c0a8a9c2SVladimir Oltean 2750c0a8a9c2SVladimir Oltean return NOTIFY_OK; 2751c0a8a9c2SVladimir Oltean } 2752c0a8a9c2SVladimir Oltean default: 2753c0a8a9c2SVladimir Oltean break; 2754cc1d5bdaSFlorian Fainelli } 2755b73adef6SFlorian Fainelli 2756b73adef6SFlorian Fainelli return NOTIFY_DONE; 2757b73adef6SFlorian Fainelli } 275888e4f0caSVivien Didelot 2759c4bb76a9SVladimir Oltean static void 2760c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2761c4bb76a9SVladimir Oltean { 2762c35b57ceSVladimir Oltean struct switchdev_notifier_fdb_info info = {}; 2763c4bb76a9SVladimir Oltean 2764c4bb76a9SVladimir Oltean info.addr = switchdev_work->addr; 2765c4bb76a9SVladimir Oltean info.vid = switchdev_work->vid; 2766c4bb76a9SVladimir Oltean info.offloaded = true; 2767c4bb76a9SVladimir Oltean call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 276893c79823SVladimir Oltean switchdev_work->orig_dev, &info.info, NULL); 2769c4bb76a9SVladimir Oltean } 2770c9eb3e0fSArkadi Sharshevsky 2771c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 2772c9eb3e0fSArkadi Sharshevsky { 2773c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 2774c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 277568d6d71eSVladimir Oltean const unsigned char *addr = switchdev_work->addr; 2776e35f12e9SVladimir Oltean struct net_device *dev = switchdev_work->dev; 277768d6d71eSVladimir Oltean u16 vid = switchdev_work->vid; 2778e35f12e9SVladimir Oltean struct dsa_switch *ds; 2779c4bb76a9SVladimir Oltean struct dsa_port *dp; 2780c9eb3e0fSArkadi Sharshevsky int err; 2781c9eb3e0fSArkadi Sharshevsky 2782e35f12e9SVladimir Oltean dp = dsa_slave_to_port(dev); 2783e35f12e9SVladimir Oltean ds = dp->ds; 2784c4bb76a9SVladimir Oltean 2785c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 2786c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 27873dc80afcSVladimir Oltean if (switchdev_work->host_addr) 278868d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_add(dp, addr, vid); 2789e212fa7cSVladimir Oltean else if (dp->lag) 279068d6d71eSVladimir Oltean err = dsa_port_lag_fdb_add(dp, addr, vid); 27913dc80afcSVladimir Oltean else 279268d6d71eSVladimir Oltean err = dsa_port_fdb_add(dp, addr, vid); 2793c9eb3e0fSArkadi Sharshevsky if (err) { 2794c4bb76a9SVladimir Oltean dev_err(ds->dev, 2795c4bb76a9SVladimir Oltean "port %d failed to add %pM vid %d to fdb: %d\n", 279668d6d71eSVladimir Oltean dp->index, addr, vid, err); 2797c9eb3e0fSArkadi Sharshevsky break; 2798c9eb3e0fSArkadi Sharshevsky } 2799c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(switchdev_work); 2800c9eb3e0fSArkadi Sharshevsky break; 2801c9eb3e0fSArkadi Sharshevsky 2802c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 28033dc80afcSVladimir Oltean if (switchdev_work->host_addr) 280468d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_del(dp, addr, vid); 2805e212fa7cSVladimir Oltean else if (dp->lag) 280668d6d71eSVladimir Oltean err = dsa_port_lag_fdb_del(dp, addr, vid); 28073dc80afcSVladimir Oltean else 280868d6d71eSVladimir Oltean err = dsa_port_fdb_del(dp, addr, vid); 2809c9eb3e0fSArkadi Sharshevsky if (err) { 2810c4bb76a9SVladimir Oltean dev_err(ds->dev, 2811c4bb76a9SVladimir Oltean "port %d failed to delete %pM vid %d from fdb: %d\n", 281268d6d71eSVladimir Oltean dp->index, addr, vid, err); 2813c9eb3e0fSArkadi Sharshevsky } 28142fd18650SVladimir Oltean 2815c9eb3e0fSArkadi Sharshevsky break; 2816c9eb3e0fSArkadi Sharshevsky } 2817c9eb3e0fSArkadi Sharshevsky 2818c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 2819c9eb3e0fSArkadi Sharshevsky } 2820c9eb3e0fSArkadi Sharshevsky 2821b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev, 2822b94dc99cSVladimir Oltean const struct net_device *foreign_dev) 2823d5f19486SVladimir Oltean { 2824b94dc99cSVladimir Oltean const struct dsa_port *dp = dsa_slave_to_port(dev); 2825b94dc99cSVladimir Oltean struct dsa_switch_tree *dst = dp->ds->dst; 2826b94dc99cSVladimir Oltean 2827b94dc99cSVladimir Oltean if (netif_is_bridge_master(foreign_dev)) 2828936db8a2SVladimir Oltean return !dsa_tree_offloads_bridge_dev(dst, foreign_dev); 2829b94dc99cSVladimir Oltean 2830b94dc99cSVladimir Oltean if (netif_is_bridge_port(foreign_dev)) 2831b94dc99cSVladimir Oltean return !dsa_tree_offloads_bridge_port(dst, foreign_dev); 2832b94dc99cSVladimir Oltean 2833b94dc99cSVladimir Oltean /* Everything else is foreign */ 2834b94dc99cSVladimir Oltean return true; 2835d5f19486SVladimir Oltean } 2836d5f19486SVladimir Oltean 2837b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev, 2838716a30a9SVladimir Oltean struct net_device *orig_dev, 2839716a30a9SVladimir Oltean unsigned long event, const void *ctx, 2840716a30a9SVladimir Oltean const struct switchdev_notifier_fdb_info *fdb_info) 2841b94dc99cSVladimir Oltean { 2842b94dc99cSVladimir Oltean struct dsa_switchdev_event_work *switchdev_work; 2843b94dc99cSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2844b94dc99cSVladimir Oltean bool host_addr = fdb_info->is_local; 2845b94dc99cSVladimir Oltean struct dsa_switch *ds = dp->ds; 2846b94dc99cSVladimir Oltean 2847b94dc99cSVladimir Oltean if (ctx && ctx != dp) 2848b94dc99cSVladimir Oltean return 0; 2849b94dc99cSVladimir Oltean 2850*a860352eSTobias Waldekranz if (!dp->bridge) 2851*a860352eSTobias Waldekranz return 0; 2852*a860352eSTobias Waldekranz 2853e212fa7cSVladimir Oltean if (switchdev_fdb_is_dynamically_learned(fdb_info)) { 2854e212fa7cSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, orig_dev)) 2855b94dc99cSVladimir Oltean return 0; 2856b94dc99cSVladimir Oltean 2857e212fa7cSVladimir Oltean /* FDB entries learned by the software bridge or by foreign 2858e212fa7cSVladimir Oltean * bridge ports should be installed as host addresses only if 2859e212fa7cSVladimir Oltean * the driver requests assisted learning. 2860b94dc99cSVladimir Oltean */ 2861e212fa7cSVladimir Oltean if (!ds->assisted_learning_on_cpu_port) 2862b94dc99cSVladimir Oltean return 0; 2863e212fa7cSVladimir Oltean } 2864b94dc99cSVladimir Oltean 2865b94dc99cSVladimir Oltean /* Also treat FDB entries on foreign interfaces bridged with us as host 2866b94dc99cSVladimir Oltean * addresses. 2867b94dc99cSVladimir Oltean */ 2868b94dc99cSVladimir Oltean if (dsa_foreign_dev_check(dev, orig_dev)) 2869b94dc99cSVladimir Oltean host_addr = true; 2870b94dc99cSVladimir Oltean 2871e212fa7cSVladimir Oltean /* Check early that we're not doing work in vain. 2872e212fa7cSVladimir Oltean * Host addresses on LAG ports still require regular FDB ops, 2873e212fa7cSVladimir Oltean * since the CPU port isn't in a LAG. 2874e212fa7cSVladimir Oltean */ 2875e212fa7cSVladimir Oltean if (dp->lag && !host_addr) { 2876e212fa7cSVladimir Oltean if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del) 2877e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2878e212fa7cSVladimir Oltean } else { 2879e212fa7cSVladimir Oltean if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del) 2880e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2881e212fa7cSVladimir Oltean } 2882e212fa7cSVladimir Oltean 2883b94dc99cSVladimir Oltean switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2884b94dc99cSVladimir Oltean if (!switchdev_work) 2885b94dc99cSVladimir Oltean return -ENOMEM; 2886b94dc99cSVladimir Oltean 2887b94dc99cSVladimir Oltean netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n", 2888b94dc99cSVladimir Oltean event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting", 2889b94dc99cSVladimir Oltean orig_dev->name, fdb_info->addr, fdb_info->vid, 2890b94dc99cSVladimir Oltean host_addr ? " as host address" : ""); 2891b94dc99cSVladimir Oltean 2892b94dc99cSVladimir Oltean INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work); 2893b94dc99cSVladimir Oltean switchdev_work->event = event; 2894b94dc99cSVladimir Oltean switchdev_work->dev = dev; 289593c79823SVladimir Oltean switchdev_work->orig_dev = orig_dev; 2896b94dc99cSVladimir Oltean 2897b94dc99cSVladimir Oltean ether_addr_copy(switchdev_work->addr, fdb_info->addr); 2898b94dc99cSVladimir Oltean switchdev_work->vid = fdb_info->vid; 2899b94dc99cSVladimir Oltean switchdev_work->host_addr = host_addr; 2900b94dc99cSVladimir Oltean 2901b94dc99cSVladimir Oltean dsa_schedule_work(&switchdev_work->work); 2902b94dc99cSVladimir Oltean 2903d5f19486SVladimir Oltean return 0; 2904d5f19486SVladimir Oltean } 2905d5f19486SVladimir Oltean 2906c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 2907c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 2908c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 2909c9eb3e0fSArkadi Sharshevsky { 2910c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 291179b139f4SVivien Didelot int err; 291279b139f4SVivien Didelot 2913447d290aSVladimir Oltean switch (event) { 2914447d290aSVladimir Oltean case SWITCHDEV_PORT_ATTR_SET: 291579b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 291679b139f4SVivien Didelot dsa_slave_dev_check, 291779b139f4SVivien Didelot dsa_slave_port_attr_set); 291879b139f4SVivien Didelot return notifier_from_errno(err); 2919447d290aSVladimir Oltean case SWITCHDEV_FDB_ADD_TO_DEVICE: 2920447d290aSVladimir Oltean case SWITCHDEV_FDB_DEL_TO_DEVICE: 2921716a30a9SVladimir Oltean err = switchdev_handle_fdb_event_to_device(dev, event, ptr, 2922b94dc99cSVladimir Oltean dsa_slave_dev_check, 2923b94dc99cSVladimir Oltean dsa_foreign_dev_check, 2924ec638740SVladimir Oltean dsa_slave_fdb_event); 2925b94dc99cSVladimir Oltean return notifier_from_errno(err); 2926c9eb3e0fSArkadi Sharshevsky default: 2927c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 2928c9eb3e0fSArkadi Sharshevsky } 2929c9eb3e0fSArkadi Sharshevsky 2930c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 2931c9eb3e0fSArkadi Sharshevsky } 2932c9eb3e0fSArkadi Sharshevsky 29332b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 29342b239f67SPetr Machata unsigned long event, void *ptr) 29352b239f67SPetr Machata { 29362b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 293779b139f4SVivien Didelot int err; 29382b239f67SPetr Machata 29392b239f67SPetr Machata switch (event) { 294079b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 2941164f861bSVladimir Oltean err = switchdev_handle_port_obj_add_foreign(dev, ptr, 294279b139f4SVivien Didelot dsa_slave_dev_check, 2943164f861bSVladimir Oltean dsa_foreign_dev_check, 294479b139f4SVivien Didelot dsa_slave_port_obj_add); 294579b139f4SVivien Didelot return notifier_from_errno(err); 29462b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 2947164f861bSVladimir Oltean err = switchdev_handle_port_obj_del_foreign(dev, ptr, 294879b139f4SVivien Didelot dsa_slave_dev_check, 2949164f861bSVladimir Oltean dsa_foreign_dev_check, 295079b139f4SVivien Didelot dsa_slave_port_obj_del); 295179b139f4SVivien Didelot return notifier_from_errno(err); 29529ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 295379b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 295479b139f4SVivien Didelot dsa_slave_dev_check, 295579b139f4SVivien Didelot dsa_slave_port_attr_set); 295679b139f4SVivien Didelot return notifier_from_errno(err); 29572b239f67SPetr Machata } 29582b239f67SPetr Machata 29592b239f67SPetr Machata return NOTIFY_DONE; 29602b239f67SPetr Machata } 29612b239f67SPetr Machata 296288e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 296388e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 296488e4f0caSVivien Didelot }; 296588e4f0caSVivien Didelot 2966010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = { 2967c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 2968c9eb3e0fSArkadi Sharshevsky }; 2969c9eb3e0fSArkadi Sharshevsky 2970010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = { 29712b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 29722b239f67SPetr Machata }; 29732b239f67SPetr Machata 297488e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 297588e4f0caSVivien Didelot { 29762b239f67SPetr Machata struct notifier_block *nb; 2977c9eb3e0fSArkadi Sharshevsky int err; 2978c9eb3e0fSArkadi Sharshevsky 2979c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 2980c9eb3e0fSArkadi Sharshevsky if (err) 2981c9eb3e0fSArkadi Sharshevsky return err; 2982c9eb3e0fSArkadi Sharshevsky 2983c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 2984c9eb3e0fSArkadi Sharshevsky if (err) 2985c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 2986c9eb3e0fSArkadi Sharshevsky 29872b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 29882b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 29892b239f67SPetr Machata if (err) 29902b239f67SPetr Machata goto err_switchdev_blocking_nb; 29912b239f67SPetr Machata 2992c9eb3e0fSArkadi Sharshevsky return 0; 2993c9eb3e0fSArkadi Sharshevsky 29942b239f67SPetr Machata err_switchdev_blocking_nb: 29952b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2996c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 2997c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 2998c9eb3e0fSArkadi Sharshevsky return err; 299988e4f0caSVivien Didelot } 300088e4f0caSVivien Didelot 300188e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 300288e4f0caSVivien Didelot { 30032b239f67SPetr Machata struct notifier_block *nb; 300488e4f0caSVivien Didelot int err; 300588e4f0caSVivien Didelot 30062b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 30072b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 30082b239f67SPetr Machata if (err) 30092b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 30102b239f67SPetr Machata 3011c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3012c9eb3e0fSArkadi Sharshevsky if (err) 3013c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 3014c9eb3e0fSArkadi Sharshevsky 301588e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 301688e4f0caSVivien Didelot if (err) 301788e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 301888e4f0caSVivien Didelot } 3019