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 { 1145077e2c8SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1155077e2c8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1165077e2c8SVladimir Oltean 1175077e2c8SVladimir Oltean dev_uc_add(master, addr); 1185077e2c8SVladimir Oltean 1195077e2c8SVladimir Oltean if (!dsa_switch_supports_uc_filtering(dp->ds)) 1205077e2c8SVladimir Oltean return 0; 1215077e2c8SVladimir Oltean 1225e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0); 1235e8a1e03SVladimir Oltean } 1245e8a1e03SVladimir Oltean 1255e8a1e03SVladimir Oltean static int dsa_slave_unsync_uc(struct net_device *dev, 1265e8a1e03SVladimir Oltean const unsigned char *addr) 1275e8a1e03SVladimir Oltean { 1285077e2c8SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1295077e2c8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1305077e2c8SVladimir Oltean 1315077e2c8SVladimir Oltean dev_uc_del(master, addr); 1325077e2c8SVladimir Oltean 1335077e2c8SVladimir Oltean if (!dsa_switch_supports_uc_filtering(dp->ds)) 1345077e2c8SVladimir Oltean return 0; 1355077e2c8SVladimir Oltean 1365e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0); 1375e8a1e03SVladimir Oltean } 1385e8a1e03SVladimir Oltean 1395e8a1e03SVladimir Oltean static int dsa_slave_sync_mc(struct net_device *dev, 1405e8a1e03SVladimir Oltean const unsigned char *addr) 1415e8a1e03SVladimir Oltean { 1425077e2c8SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1435077e2c8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1445077e2c8SVladimir Oltean 1455077e2c8SVladimir Oltean dev_mc_add(master, addr); 1465077e2c8SVladimir Oltean 1475077e2c8SVladimir Oltean if (!dsa_switch_supports_mc_filtering(dp->ds)) 1485077e2c8SVladimir Oltean return 0; 1495077e2c8SVladimir Oltean 1505e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0); 1515e8a1e03SVladimir Oltean } 1525e8a1e03SVladimir Oltean 1535e8a1e03SVladimir Oltean static int dsa_slave_unsync_mc(struct net_device *dev, 1545e8a1e03SVladimir Oltean const unsigned char *addr) 1555e8a1e03SVladimir Oltean { 1565077e2c8SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1575077e2c8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1585077e2c8SVladimir Oltean 1595077e2c8SVladimir Oltean dev_mc_del(master, addr); 1605077e2c8SVladimir Oltean 1615077e2c8SVladimir Oltean if (!dsa_switch_supports_mc_filtering(dp->ds)) 1625077e2c8SVladimir Oltean return 0; 1635077e2c8SVladimir Oltean 1645e8a1e03SVladimir Oltean return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0); 1655e8a1e03SVladimir Oltean } 1665e8a1e03SVladimir Oltean 16791da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 16891da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 16991da11f8SLennert Buytenhek { 17091da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 17191da11f8SLennert Buytenhek 1720d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 1739d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 17491da11f8SLennert Buytenhek 17591da11f8SLennert Buytenhek return 0xffff; 17691da11f8SLennert Buytenhek } 17791da11f8SLennert Buytenhek 17891da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 17991da11f8SLennert Buytenhek { 18091da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 18191da11f8SLennert Buytenhek 1820d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 1839d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 18491da11f8SLennert Buytenhek 18591da11f8SLennert Buytenhek return 0; 18691da11f8SLennert Buytenhek } 18791da11f8SLennert Buytenhek 18891da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 18991da11f8SLennert Buytenhek { 19091da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 19191da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 19291da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 19391da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 1940b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 19549463b7fSVivien Didelot ds->dst->index, ds->index); 196c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 19724df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 19891da11f8SLennert Buytenhek } 19991da11f8SLennert Buytenhek 20091da11f8SLennert Buytenhek 20191da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 202abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 203c0840801SLennert Buytenhek { 204d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 205c0840801SLennert Buytenhek } 206c0840801SLennert Buytenhek 20791da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 20891da11f8SLennert Buytenhek { 209d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 210d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 211499aa9e1SVladimir Oltean struct dsa_switch *ds = dp->ds; 212df02c6ffSLennert Buytenhek int err; 213df02c6ffSLennert Buytenhek 2149d5ef190SVladimir Oltean err = dev_open(master, NULL); 2159d5ef190SVladimir Oltean if (err < 0) { 2169d5ef190SVladimir Oltean netdev_err(dev, "failed to open master %s\n", master->name); 2179d5ef190SVladimir Oltean goto out; 2189d5ef190SVladimir Oltean } 219df02c6ffSLennert Buytenhek 220499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) { 221499aa9e1SVladimir Oltean err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0); 222499aa9e1SVladimir Oltean if (err) 223499aa9e1SVladimir Oltean goto out; 224499aa9e1SVladimir Oltean } 225499aa9e1SVladimir Oltean 2268feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 227a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 228df02c6ffSLennert Buytenhek if (err < 0) 229499aa9e1SVladimir Oltean goto del_host_addr; 230df02c6ffSLennert Buytenhek } 231df02c6ffSLennert Buytenhek 2328640f8dcSRussell King err = dsa_port_enable_rt(dp, dev->phydev); 233b2f2af21SFlorian Fainelli if (err) 23435aae5abSVladimir Oltean goto del_unicast; 235b73adef6SFlorian Fainelli 23691da11f8SLennert Buytenhek return 0; 237df02c6ffSLennert Buytenhek 238df02c6ffSLennert Buytenhek del_unicast: 2398feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 240a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 241499aa9e1SVladimir Oltean del_host_addr: 242499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 243499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 244df02c6ffSLennert Buytenhek out: 245df02c6ffSLennert Buytenhek return err; 24691da11f8SLennert Buytenhek } 24791da11f8SLennert Buytenhek 24891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 24991da11f8SLennert Buytenhek { 250d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 251d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 252499aa9e1SVladimir Oltean struct dsa_switch *ds = dp->ds; 253df02c6ffSLennert Buytenhek 2548640f8dcSRussell King dsa_port_disable_rt(dp); 2556457edfeSVivien Didelot 2568feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 257a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 258df02c6ffSLennert Buytenhek 259499aa9e1SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 260499aa9e1SVladimir Oltean dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0); 261499aa9e1SVladimir Oltean 26291da11f8SLennert Buytenhek return 0; 26391da11f8SLennert Buytenhek } 26491da11f8SLennert Buytenhek 26572c3b0c7SVladimir Oltean static void dsa_slave_manage_host_flood(struct net_device *dev) 2667569459aSVladimir Oltean { 26772c3b0c7SVladimir Oltean bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI); 26872c3b0c7SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 26972c3b0c7SVladimir Oltean bool uc = dev->flags & IFF_PROMISC; 2707569459aSVladimir Oltean 27172c3b0c7SVladimir Oltean dsa_port_set_host_flood(dp, uc, mc); 2727569459aSVladimir Oltean } 2737569459aSVladimir Oltean 27491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 27591da11f8SLennert Buytenhek { 276d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 2777569459aSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2787569459aSVladimir Oltean struct dsa_switch *ds = dp->ds; 27935aae5abSVladimir Oltean 28091da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 28117ab4f61SRundong Ge dev_set_allmulti(master, 28217ab4f61SRundong Ge dev->flags & IFF_ALLMULTI ? 1 : -1); 28391da11f8SLennert Buytenhek if (change & IFF_PROMISC) 28417ab4f61SRundong Ge dev_set_promiscuity(master, 28517ab4f61SRundong Ge dev->flags & IFF_PROMISC ? 1 : -1); 2867569459aSVladimir Oltean 2877569459aSVladimir Oltean if (dsa_switch_supports_uc_filtering(ds) && 2887569459aSVladimir Oltean dsa_switch_supports_mc_filtering(ds)) 28972c3b0c7SVladimir Oltean dsa_slave_manage_host_flood(dev); 29017ab4f61SRundong Ge } 29191da11f8SLennert Buytenhek 29291da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 29391da11f8SLennert Buytenhek { 2945e8a1e03SVladimir Oltean __dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc); 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; 4547414af30STobias Waldekranz case SWITCHDEV_ATTR_ID_PORT_MST_STATE: 4557414af30STobias Waldekranz if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 4567414af30STobias Waldekranz return -EOPNOTSUPP; 4577414af30STobias Waldekranz 4587414af30STobias Waldekranz ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack); 4597414af30STobias Waldekranz break; 460fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 461936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 46203cbb870SVladimir Oltean return -EOPNOTSUPP; 46303cbb870SVladimir Oltean 46489153ed6SVladimir Oltean ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 46589153ed6SVladimir Oltean extack); 466fb2dabadSVivien Didelot break; 46734a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 468936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 46903cbb870SVladimir Oltean return -EOPNOTSUPP; 47003cbb870SVladimir Oltean 471bae33f2bSVladimir Oltean ret = dsa_port_ageing_time(dp, attr->u.ageing_time); 47234a79f63SVivien Didelot break; 473332afc4cSTobias Waldekranz case SWITCHDEV_ATTR_ID_BRIDGE_MST: 474332afc4cSTobias Waldekranz if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 475332afc4cSTobias Waldekranz return -EOPNOTSUPP; 476332afc4cSTobias Waldekranz 477332afc4cSTobias Waldekranz ret = dsa_port_mst_enable(dp, attr->u.mst, extack); 478332afc4cSTobias Waldekranz break; 479ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 48003cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 48103cbb870SVladimir Oltean return -EOPNOTSUPP; 48203cbb870SVladimir Oltean 483a8b659e7SVladimir Oltean ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 484a8b659e7SVladimir Oltean extack); 485ea87005aSFlorian Fainelli break; 48657652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 48703cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev)) 48803cbb870SVladimir Oltean return -EOPNOTSUPP; 48903cbb870SVladimir Oltean 490a8b659e7SVladimir Oltean ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack); 49157652796SRussell King break; 4928e6598a7STobias Waldekranz case SWITCHDEV_ATTR_ID_VLAN_MSTI: 4938e6598a7STobias Waldekranz if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev)) 4948e6598a7STobias Waldekranz return -EOPNOTSUPP; 4958e6598a7STobias Waldekranz 4968e6598a7STobias Waldekranz ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti); 4978e6598a7STobias Waldekranz break; 49835636062SScott Feldman default: 49935636062SScott Feldman ret = -EOPNOTSUPP; 50035636062SScott Feldman break; 50135636062SScott Feldman } 50235636062SScott Feldman 50335636062SScott Feldman return ret; 50435636062SScott Feldman } 50535636062SScott Feldman 5061ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */ 5071ce39f0eSVladimir Oltean static int 5081ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave, 5091ce39f0eSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 5101ce39f0eSVladimir Oltean { 5111ce39f0eSVladimir Oltean struct net_device *upper_dev; 5121ce39f0eSVladimir Oltean struct list_head *iter; 5131ce39f0eSVladimir Oltean 5141ce39f0eSVladimir Oltean netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 5151ce39f0eSVladimir Oltean u16 vid; 5161ce39f0eSVladimir Oltean 5171ce39f0eSVladimir Oltean if (!is_vlan_dev(upper_dev)) 5181ce39f0eSVladimir Oltean continue; 5191ce39f0eSVladimir Oltean 5201ce39f0eSVladimir Oltean vid = vlan_dev_vlan_id(upper_dev); 521b7a9e0daSVladimir Oltean if (vid == vlan->vid) 5221ce39f0eSVladimir Oltean return -EBUSY; 5231ce39f0eSVladimir Oltean } 5241ce39f0eSVladimir Oltean 5251ce39f0eSVladimir Oltean return 0; 5261ce39f0eSVladimir Oltean } 5271ce39f0eSVladimir Oltean 528bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev, 5290ee2af4eSVladimir Oltean const struct switchdev_obj *obj, 5300ee2af4eSVladimir Oltean struct netlink_ext_ack *extack) 531bdcff080SVivien Didelot { 532bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 533134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 534b7a9e0daSVladimir Oltean int err; 535bdcff080SVivien Didelot 5360ee2af4eSVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 5370ee2af4eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 538c5335d73SVivien Didelot return 0; 5390ee2af4eSVladimir Oltean } 540c5335d73SVivien Didelot 541134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 542bdcff080SVivien Didelot 5431ce39f0eSVladimir Oltean /* Deny adding a bridge VLAN when there is already an 802.1Q upper with 5441ce39f0eSVladimir Oltean * the same VID. 5451ce39f0eSVladimir Oltean */ 54636cbf39bSVladimir Oltean if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) { 5471ce39f0eSVladimir Oltean rcu_read_lock(); 548134ef238SVladimir Oltean err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan); 5491ce39f0eSVladimir Oltean rcu_read_unlock(); 55031046a5fSVladimir Oltean if (err) { 55131046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 55231046a5fSVladimir Oltean "Port already has a VLAN upper with this VID"); 5531ce39f0eSVladimir Oltean return err; 5541ce39f0eSVladimir Oltean } 55531046a5fSVladimir Oltean } 5561ce39f0eSVladimir Oltean 557134ef238SVladimir Oltean return dsa_port_vlan_add(dp, vlan, extack); 558134ef238SVladimir Oltean } 559bdcff080SVivien Didelot 560164f861bSVladimir Oltean /* Offload a VLAN installed on the bridge or on a foreign interface by 561164f861bSVladimir Oltean * installing it as a VLAN towards the CPU port. 562164f861bSVladimir Oltean */ 563134ef238SVladimir Oltean static int dsa_slave_host_vlan_add(struct net_device *dev, 564134ef238SVladimir Oltean const struct switchdev_obj *obj, 565134ef238SVladimir Oltean struct netlink_ext_ack *extack) 566134ef238SVladimir Oltean { 567134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 568134ef238SVladimir Oltean struct switchdev_obj_port_vlan vlan; 569134ef238SVladimir Oltean 570164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 571164f861bSVladimir Oltean if (!dp->bridge) 572164f861bSVladimir Oltean return -EOPNOTSUPP; 573164f861bSVladimir Oltean 574134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) { 575134ef238SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN"); 576134ef238SVladimir Oltean return 0; 577134ef238SVladimir Oltean } 578134ef238SVladimir Oltean 579134ef238SVladimir Oltean vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 580134ef238SVladimir Oltean 581134ef238SVladimir Oltean /* Even though drivers often handle CPU membership in special ways, 582b9499904SVivien Didelot * it doesn't make sense to program a PVID, so clear this flag. 583b9499904SVivien Didelot */ 584b9499904SVivien Didelot vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 585b9499904SVivien Didelot 586134ef238SVladimir Oltean return dsa_port_host_vlan_add(dp, &vlan, extack); 587bdcff080SVivien Didelot } 588bdcff080SVivien Didelot 58969bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx, 590648b4a99SJiri Pirko const struct switchdev_obj *obj, 59179b139f4SVivien Didelot struct netlink_ext_ack *extack) 592ba14d9ebSVivien Didelot { 593d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 594ba14d9ebSVivien Didelot int err; 595ba14d9ebSVivien Didelot 5960d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 5970d2cfbd4SVladimir Oltean return 0; 5980d2cfbd4SVladimir Oltean 5999e8f4a54SJiri Pirko switch (obj->id) { 6008df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 60103cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 60279b139f4SVivien Didelot return -EOPNOTSUPP; 60303cbb870SVladimir Oltean 604ffb68fc5SVladimir Oltean err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6058df30255SVivien Didelot break; 6065f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 607936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 60803cbb870SVladimir Oltean return -EOPNOTSUPP; 60903cbb870SVladimir Oltean 61068d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6115f4dbc50SAndrew Lunn break; 61257d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 613164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 6140ee2af4eSVladimir Oltean err = dsa_slave_vlan_add(dev, obj, extack); 615164f861bSVladimir Oltean else 616164f861bSVladimir Oltean err = dsa_slave_host_vlan_add(dev, obj, extack); 61711149536SVivien Didelot break; 618c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 619936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 620c595c433SHoratiu Vultur return -EOPNOTSUPP; 62103cbb870SVladimir Oltean 622c595c433SHoratiu Vultur err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj)); 623c595c433SHoratiu Vultur break; 624c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 625936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 626c595c433SHoratiu Vultur return -EOPNOTSUPP; 62703cbb870SVladimir Oltean 628c595c433SHoratiu Vultur err = dsa_port_mrp_add_ring_role(dp, 629c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 630c595c433SHoratiu Vultur break; 631ba14d9ebSVivien Didelot default: 632ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 633ba14d9ebSVivien Didelot break; 634ba14d9ebSVivien Didelot } 635ba14d9ebSVivien Didelot 636ba14d9ebSVivien Didelot return err; 637ba14d9ebSVivien Didelot } 638ba14d9ebSVivien Didelot 639bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev, 640bdcff080SVivien Didelot const struct switchdev_obj *obj) 641bdcff080SVivien Didelot { 642bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 6432209158cSVladimir Oltean struct switchdev_obj_port_vlan *vlan; 644bdcff080SVivien Didelot 64554a0ed0dSRussell King if (dsa_port_skip_vlan_configuration(dp)) 646c5335d73SVivien Didelot return 0; 647c5335d73SVivien Didelot 6482209158cSVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 6492209158cSVladimir Oltean 650134ef238SVladimir Oltean return dsa_port_vlan_del(dp, vlan); 651134ef238SVladimir Oltean } 6522209158cSVladimir Oltean 653134ef238SVladimir Oltean static int dsa_slave_host_vlan_del(struct net_device *dev, 654134ef238SVladimir Oltean const struct switchdev_obj *obj) 655134ef238SVladimir Oltean { 656134ef238SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 657134ef238SVladimir Oltean struct switchdev_obj_port_vlan *vlan; 6582209158cSVladimir Oltean 659164f861bSVladimir Oltean /* Do nothing if this is a software bridge */ 660164f861bSVladimir Oltean if (!dp->bridge) 661164f861bSVladimir Oltean return -EOPNOTSUPP; 662164f861bSVladimir Oltean 663134ef238SVladimir Oltean if (dsa_port_skip_vlan_configuration(dp)) 6642209158cSVladimir Oltean return 0; 665134ef238SVladimir Oltean 666134ef238SVladimir Oltean vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 667134ef238SVladimir Oltean 668134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, vlan); 669bdcff080SVivien Didelot } 670bdcff080SVivien Didelot 67169bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx, 672648b4a99SJiri Pirko const struct switchdev_obj *obj) 673ba14d9ebSVivien Didelot { 674d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 675ba14d9ebSVivien Didelot int err; 676ba14d9ebSVivien Didelot 6770d2cfbd4SVladimir Oltean if (ctx && ctx != dp) 6780d2cfbd4SVladimir Oltean return 0; 6790d2cfbd4SVladimir Oltean 6809e8f4a54SJiri Pirko switch (obj->id) { 6818df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 68203cbb870SVladimir Oltean if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 68379b139f4SVivien Didelot return -EOPNOTSUPP; 68403cbb870SVladimir Oltean 685bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6868df30255SVivien Didelot break; 6875f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 688936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 68903cbb870SVladimir Oltean return -EOPNOTSUPP; 69003cbb870SVladimir Oltean 69168d6d71eSVladimir Oltean err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 6925f4dbc50SAndrew Lunn break; 69357d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 694164f861bSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, obj->orig_dev)) 695bdcff080SVivien Didelot err = dsa_slave_vlan_del(dev, obj); 696164f861bSVladimir Oltean else 697164f861bSVladimir Oltean err = dsa_slave_host_vlan_del(dev, obj); 69811149536SVivien Didelot break; 699c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_MRP: 700936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 701c595c433SHoratiu Vultur return -EOPNOTSUPP; 70203cbb870SVladimir Oltean 703c595c433SHoratiu Vultur err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj)); 704c595c433SHoratiu Vultur break; 705c595c433SHoratiu Vultur case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 706936db8a2SVladimir Oltean if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev)) 707c595c433SHoratiu Vultur return -EOPNOTSUPP; 70803cbb870SVladimir Oltean 709c595c433SHoratiu Vultur err = dsa_port_mrp_del_ring_role(dp, 710c595c433SHoratiu Vultur SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 711c595c433SHoratiu Vultur break; 712ba14d9ebSVivien Didelot default: 713ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 714ba14d9ebSVivien Didelot break; 715ba14d9ebSVivien Didelot } 716ba14d9ebSVivien Didelot 717ba14d9ebSVivien Didelot return err; 718ba14d9ebSVivien Didelot } 719ba14d9ebSVivien Didelot 7204fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 72104ff53f9SFlorian Fainelli struct sk_buff *skb) 72204ff53f9SFlorian Fainelli { 72304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 7244fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 7254fa7b718SVivien Didelot 726f78ed220SEric Dumazet return netpoll_send_skb(p->netpoll, skb); 72704ff53f9SFlorian Fainelli #else 72804ff53f9SFlorian Fainelli BUG(); 72904ff53f9SFlorian Fainelli return NETDEV_TX_OK; 730f78ed220SEric Dumazet #endif 73104ff53f9SFlorian Fainelli } 73204ff53f9SFlorian Fainelli 73390af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 73490af1059SBrandon Streiff struct sk_buff *skb) 73590af1059SBrandon Streiff { 73690af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 73790af1059SBrandon Streiff 738cfd12c06SYangbo Lu if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 739cfd12c06SYangbo Lu return; 740cfd12c06SYangbo Lu 74190af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 74290af1059SBrandon Streiff return; 74390af1059SBrandon Streiff 7445c5416f5SYangbo Lu ds->ops->port_txtstamp(ds, p->dp->index, skb); 74590af1059SBrandon Streiff } 74690af1059SBrandon Streiff 74797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 74897a69a0dSVladimir Oltean { 74997a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 75097a69a0dSVladimir Oltean * tag to be successfully transmitted 75197a69a0dSVladimir Oltean */ 75297a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 75397a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 75497a69a0dSVladimir Oltean 75597a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 75697a69a0dSVladimir Oltean * do not modify its EtherType 75797a69a0dSVladimir Oltean */ 75897a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 75997a69a0dSVladimir Oltean dev_queue_xmit(skb); 76097a69a0dSVladimir Oltean 76197a69a0dSVladimir Oltean return NETDEV_TX_OK; 76297a69a0dSVladimir Oltean } 76397a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 76497a69a0dSVladimir Oltean 765a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev) 766a3b0b647SVladimir Oltean { 767a3b0b647SVladimir Oltean int needed_headroom = dev->needed_headroom; 768a3b0b647SVladimir Oltean int needed_tailroom = dev->needed_tailroom; 769a3b0b647SVladimir Oltean 770a3b0b647SVladimir Oltean /* For tail taggers, we need to pad short frames ourselves, to ensure 771a3b0b647SVladimir Oltean * that the tail tag does not fail at its role of being at the end of 772a3b0b647SVladimir Oltean * the packet, once the master interface pads the frame. Account for 773a3b0b647SVladimir Oltean * that pad length here, and pad later. 774a3b0b647SVladimir Oltean */ 775a3b0b647SVladimir Oltean if (unlikely(needed_tailroom && skb->len < ETH_ZLEN)) 776a3b0b647SVladimir Oltean needed_tailroom += ETH_ZLEN - skb->len; 777a3b0b647SVladimir Oltean /* skb_headroom() returns unsigned int... */ 778a3b0b647SVladimir Oltean needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0); 779a3b0b647SVladimir Oltean needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0); 780a3b0b647SVladimir Oltean 781a3b0b647SVladimir Oltean if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb))) 782a3b0b647SVladimir Oltean /* No reallocation needed, yay! */ 783a3b0b647SVladimir Oltean return 0; 784a3b0b647SVladimir Oltean 785a3b0b647SVladimir Oltean return pskb_expand_head(skb, needed_headroom, needed_tailroom, 786a3b0b647SVladimir Oltean GFP_ATOMIC); 787a3b0b647SVladimir Oltean } 788a3b0b647SVladimir Oltean 7893e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 7903e8a72d1SFlorian Fainelli { 7913e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 7924ed70ce9SFlorian Fainelli struct sk_buff *nskb; 7933e8a72d1SFlorian Fainelli 7946a900628SHeiner Kallweit dev_sw_netstats_tx_add(dev, 1, skb->len); 7953e8a72d1SFlorian Fainelli 796c4b364ceSYangbo Lu memset(skb->cb, 0, sizeof(skb->cb)); 79787671375SVladimir Oltean 798cf536ea3SYangbo Lu /* Handle tx timestamp if any */ 79990af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 80090af1059SBrandon Streiff 801a3b0b647SVladimir Oltean if (dsa_realloc_skb(skb, dev)) { 802a3b0b647SVladimir Oltean dev_kfree_skb_any(skb); 803a3b0b647SVladimir Oltean return NETDEV_TX_OK; 804a3b0b647SVladimir Oltean } 805a3b0b647SVladimir Oltean 806a3b0b647SVladimir Oltean /* needed_tailroom should still be 'warm' in the cache line from 807a3b0b647SVladimir Oltean * dsa_realloc_skb(), which has also ensured that padding is safe. 808a3b0b647SVladimir Oltean */ 809a3b0b647SVladimir Oltean if (dev->needed_tailroom) 810a3b0b647SVladimir Oltean eth_skb_pad(skb); 811a3b0b647SVladimir Oltean 812fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 813fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 814fe47d563SVivien Didelot */ 8154ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 816fe47d563SVivien Didelot if (!nskb) { 817fe47d563SVivien Didelot kfree_skb(skb); 8184ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 819fe47d563SVivien Didelot } 8205aed85ceSFlorian Fainelli 82197a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 82297a69a0dSVladimir Oltean } 82304ff53f9SFlorian Fainelli 82491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 82591da11f8SLennert Buytenhek 82691da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 82791da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 82891da11f8SLennert Buytenhek { 829*e4d44b3dSWolfram Sang strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 830*e4d44b3dSWolfram Sang strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 831*e4d44b3dSWolfram Sang strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 83291da11f8SLennert Buytenhek } 83391da11f8SLennert Buytenhek 8343d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 8353d762a0fSGuenter Roeck { 836d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 837d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8383d762a0fSGuenter Roeck 8399d490b4eSVivien Didelot if (ds->ops->get_regs_len) 840d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 8413d762a0fSGuenter Roeck 8423d762a0fSGuenter Roeck return -EOPNOTSUPP; 8433d762a0fSGuenter Roeck } 8443d762a0fSGuenter Roeck 8453d762a0fSGuenter Roeck static void 8463d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 8473d762a0fSGuenter Roeck { 848d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 849d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8503d762a0fSGuenter Roeck 8519d490b4eSVivien Didelot if (ds->ops->get_regs) 852d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 8533d762a0fSGuenter Roeck } 8543d762a0fSGuenter Roeck 855aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 856aab9c406SFlorian Fainelli { 857aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 858aab9c406SFlorian Fainelli 859aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 860aab9c406SFlorian Fainelli } 861aab9c406SFlorian Fainelli 8626793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 8636793abb4SGuenter Roeck { 864d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 865d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8666793abb4SGuenter Roeck 8670e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 868ff04955cSAndrew Lunn return ds->cd->eeprom_len; 8696793abb4SGuenter Roeck 8709d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 8719d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 8726793abb4SGuenter Roeck 8736793abb4SGuenter Roeck return 0; 8746793abb4SGuenter Roeck } 8756793abb4SGuenter Roeck 8766793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 8776793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8786793abb4SGuenter Roeck { 879d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 880d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8816793abb4SGuenter Roeck 8829d490b4eSVivien Didelot if (ds->ops->get_eeprom) 8839d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 8846793abb4SGuenter Roeck 8856793abb4SGuenter Roeck return -EOPNOTSUPP; 8866793abb4SGuenter Roeck } 8876793abb4SGuenter Roeck 8886793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 8896793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 8906793abb4SGuenter Roeck { 891d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 892d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8936793abb4SGuenter Roeck 8949d490b4eSVivien Didelot if (ds->ops->set_eeprom) 8959d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 8966793abb4SGuenter Roeck 8976793abb4SGuenter Roeck return -EOPNOTSUPP; 8986793abb4SGuenter Roeck } 8996793abb4SGuenter Roeck 90091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 90191da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 90291da11f8SLennert Buytenhek { 903d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 904d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 90591da11f8SLennert Buytenhek 90691da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 90791da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 90891da11f8SLennert Buytenhek 90991da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 91091da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 91191da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 91291da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 9139d490b4eSVivien Didelot if (ds->ops->get_strings) 91489f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 91589f09048SFlorian Fainelli data + 4 * len); 916a71acad9SOleksij Rempel } else if (stringset == ETH_SS_TEST) { 917a71acad9SOleksij Rempel net_selftest_get_strings(data); 91891da11f8SLennert Buytenhek } 919a71acad9SOleksij Rempel 92091da11f8SLennert Buytenhek } 92191da11f8SLennert Buytenhek 92291da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 92391da11f8SLennert Buytenhek struct ethtool_stats *stats, 92491da11f8SLennert Buytenhek uint64_t *data) 92591da11f8SLennert Buytenhek { 926d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 927d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 9285f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 929f613ed66SFlorian Fainelli unsigned int start; 9305f6b4e14SFlorian Fainelli int i; 93191da11f8SLennert Buytenhek 9325f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 9335f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 9345f6b4e14SFlorian Fainelli 9356a900628SHeiner Kallweit s = per_cpu_ptr(dev->tstats, i); 936f613ed66SFlorian Fainelli do { 9375f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 9389962acefSEric Dumazet tx_packets = u64_stats_read(&s->tx_packets); 9399962acefSEric Dumazet tx_bytes = u64_stats_read(&s->tx_bytes); 9409962acefSEric Dumazet rx_packets = u64_stats_read(&s->rx_packets); 9419962acefSEric Dumazet rx_bytes = u64_stats_read(&s->rx_bytes); 9425f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 9435f6b4e14SFlorian Fainelli data[0] += tx_packets; 9445f6b4e14SFlorian Fainelli data[1] += tx_bytes; 9455f6b4e14SFlorian Fainelli data[2] += rx_packets; 9465f6b4e14SFlorian Fainelli data[3] += rx_bytes; 9475f6b4e14SFlorian Fainelli } 9489d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 949d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 95091da11f8SLennert Buytenhek } 95191da11f8SLennert Buytenhek 95291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 95391da11f8SLennert Buytenhek { 954d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 955d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 95691da11f8SLennert Buytenhek 95791da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 958b94cbc90SVladimir Oltean int count = 0; 95991da11f8SLennert Buytenhek 960b94cbc90SVladimir Oltean if (ds->ops->get_sset_count) { 961b94cbc90SVladimir Oltean count = ds->ops->get_sset_count(ds, dp->index, sset); 962b94cbc90SVladimir Oltean if (count < 0) 96391da11f8SLennert Buytenhek return count; 964b94cbc90SVladimir Oltean } 965b94cbc90SVladimir Oltean 966b94cbc90SVladimir Oltean return count + 4; 967a71acad9SOleksij Rempel } else if (sset == ETH_SS_TEST) { 968a71acad9SOleksij Rempel return net_selftest_get_count(); 96991da11f8SLennert Buytenhek } 97091da11f8SLennert Buytenhek 97191da11f8SLennert Buytenhek return -EOPNOTSUPP; 97291da11f8SLennert Buytenhek } 97391da11f8SLennert Buytenhek 974487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev, 975487d3855SAlvin Šipraga struct ethtool_eth_phy_stats *phy_stats) 976487d3855SAlvin Šipraga { 977487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 978487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 979487d3855SAlvin Šipraga 980487d3855SAlvin Šipraga if (ds->ops->get_eth_phy_stats) 981487d3855SAlvin Šipraga ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats); 982487d3855SAlvin Šipraga } 983487d3855SAlvin Šipraga 984487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev, 985487d3855SAlvin Šipraga struct ethtool_eth_mac_stats *mac_stats) 986487d3855SAlvin Šipraga { 987487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 988487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 989487d3855SAlvin Šipraga 990487d3855SAlvin Šipraga if (ds->ops->get_eth_mac_stats) 991487d3855SAlvin Šipraga ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats); 992487d3855SAlvin Šipraga } 993487d3855SAlvin Šipraga 994487d3855SAlvin Šipraga static void 995487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev, 996487d3855SAlvin Šipraga struct ethtool_eth_ctrl_stats *ctrl_stats) 997487d3855SAlvin Šipraga { 998487d3855SAlvin Šipraga struct dsa_port *dp = dsa_slave_to_port(dev); 999487d3855SAlvin Šipraga struct dsa_switch *ds = dp->ds; 1000487d3855SAlvin Šipraga 1001487d3855SAlvin Šipraga if (ds->ops->get_eth_ctrl_stats) 1002487d3855SAlvin Šipraga ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats); 1003487d3855SAlvin Šipraga } 1004487d3855SAlvin Šipraga 100567f38b1cSClément Léger static void 100667f38b1cSClément Léger dsa_slave_get_rmon_stats(struct net_device *dev, 100767f38b1cSClément Léger struct ethtool_rmon_stats *rmon_stats, 100867f38b1cSClément Léger const struct ethtool_rmon_hist_range **ranges) 100967f38b1cSClément Léger { 101067f38b1cSClément Léger struct dsa_port *dp = dsa_slave_to_port(dev); 101167f38b1cSClément Léger struct dsa_switch *ds = dp->ds; 101267f38b1cSClément Léger 101367f38b1cSClément Léger if (ds->ops->get_rmon_stats) 101467f38b1cSClément Léger ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges); 101567f38b1cSClément Léger } 101667f38b1cSClément Léger 1017a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev, 1018a71acad9SOleksij Rempel struct ethtool_test *etest, u64 *buf) 1019a71acad9SOleksij Rempel { 1020a71acad9SOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(ndev); 1021a71acad9SOleksij Rempel struct dsa_switch *ds = dp->ds; 1022a71acad9SOleksij Rempel 1023a71acad9SOleksij Rempel if (ds->ops->self_test) { 1024a71acad9SOleksij Rempel ds->ops->self_test(ds, dp->index, etest, buf); 1025a71acad9SOleksij Rempel return; 1026a71acad9SOleksij Rempel } 1027a71acad9SOleksij Rempel 1028a71acad9SOleksij Rempel net_selftest(ndev, etest, buf); 1029a71acad9SOleksij Rempel } 1030a71acad9SOleksij Rempel 103119e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 103219e57c4eSFlorian Fainelli { 1033d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1034d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 103519e57c4eSFlorian Fainelli 1036aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 1037aab9c406SFlorian Fainelli 10389d490b4eSVivien Didelot if (ds->ops->get_wol) 1039d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 104019e57c4eSFlorian Fainelli } 104119e57c4eSFlorian Fainelli 104219e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 104319e57c4eSFlorian Fainelli { 1044d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1045d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 104619e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 104719e57c4eSFlorian Fainelli 1048aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 1049aab9c406SFlorian Fainelli 10509d490b4eSVivien Didelot if (ds->ops->set_wol) 1051d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 105219e57c4eSFlorian Fainelli 105319e57c4eSFlorian Fainelli return ret; 105419e57c4eSFlorian Fainelli } 105519e57c4eSFlorian Fainelli 10567905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 10577905288fSFlorian Fainelli { 1058d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1059d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10607905288fSFlorian Fainelli int ret; 10617905288fSFlorian Fainelli 10627b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 106300670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10647b9cc738SVivien Didelot return -ENODEV; 10657b9cc738SVivien Didelot 106608f50061SVivien Didelot if (!ds->ops->set_mac_eee) 10677905288fSFlorian Fainelli return -EOPNOTSUPP; 10687905288fSFlorian Fainelli 1069d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 10707905288fSFlorian Fainelli if (ret) 10717905288fSFlorian Fainelli return ret; 10727905288fSFlorian Fainelli 1073aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 10747905288fSFlorian Fainelli } 10757905288fSFlorian Fainelli 10767905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 10777905288fSFlorian Fainelli { 1078d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1079d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10807905288fSFlorian Fainelli int ret; 10817905288fSFlorian Fainelli 10827b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 108300670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 10847b9cc738SVivien Didelot return -ENODEV; 10857b9cc738SVivien Didelot 108608f50061SVivien Didelot if (!ds->ops->get_mac_eee) 10877905288fSFlorian Fainelli return -EOPNOTSUPP; 10887905288fSFlorian Fainelli 1089d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 10907905288fSFlorian Fainelli if (ret) 10917905288fSFlorian Fainelli return ret; 10927905288fSFlorian Fainelli 1093aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 1094aab9c406SFlorian Fainelli } 1095aab9c406SFlorian Fainelli 1096aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 1097aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 1098aab9c406SFlorian Fainelli { 1099aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1100aab9c406SFlorian Fainelli 1101aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 1102aab9c406SFlorian Fainelli } 1103aab9c406SFlorian Fainelli 1104aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 1105aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 1106aab9c406SFlorian Fainelli { 1107aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1108aab9c406SFlorian Fainelli 1109aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 11107905288fSFlorian Fainelli } 11117905288fSFlorian Fainelli 11123d410403SOleksij Rempel static void dsa_slave_get_pause_stats(struct net_device *dev, 11133d410403SOleksij Rempel struct ethtool_pause_stats *pause_stats) 11143d410403SOleksij Rempel { 11153d410403SOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(dev); 11163d410403SOleksij Rempel struct dsa_switch *ds = dp->ds; 11173d410403SOleksij Rempel 11183d410403SOleksij Rempel if (ds->ops->get_pause_stats) 11193d410403SOleksij Rempel ds->ops->get_pause_stats(ds, dp->index, pause_stats); 11203d410403SOleksij Rempel } 11213d410403SOleksij Rempel 1122a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev, 1123a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1124a2a1a13bSHeiner Kallweit { 1125a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1126a2a1a13bSHeiner Kallweit 1127a2a1a13bSHeiner Kallweit phylink_ethtool_get_pauseparam(dp->pl, pause); 1128a2a1a13bSHeiner Kallweit } 1129a2a1a13bSHeiner Kallweit 1130a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev, 1131a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 1132a2a1a13bSHeiner Kallweit { 1133a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 1134a2a1a13bSHeiner Kallweit 1135a2a1a13bSHeiner Kallweit return phylink_ethtool_set_pauseparam(dp->pl, pause); 1136a2a1a13bSHeiner Kallweit } 1137a2a1a13bSHeiner Kallweit 113804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 113904ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 114004ff53f9SFlorian Fainelli struct netpoll_info *ni) 114104ff53f9SFlorian Fainelli { 1142d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 114304ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 114404ff53f9SFlorian Fainelli struct netpoll *netpoll; 114504ff53f9SFlorian Fainelli int err = 0; 114604ff53f9SFlorian Fainelli 114704ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 114804ff53f9SFlorian Fainelli if (!netpoll) 114904ff53f9SFlorian Fainelli return -ENOMEM; 115004ff53f9SFlorian Fainelli 115104ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 115204ff53f9SFlorian Fainelli if (err) { 115304ff53f9SFlorian Fainelli kfree(netpoll); 115404ff53f9SFlorian Fainelli goto out; 115504ff53f9SFlorian Fainelli } 115604ff53f9SFlorian Fainelli 115704ff53f9SFlorian Fainelli p->netpoll = netpoll; 115804ff53f9SFlorian Fainelli out: 115904ff53f9SFlorian Fainelli return err; 116004ff53f9SFlorian Fainelli } 116104ff53f9SFlorian Fainelli 116204ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 116304ff53f9SFlorian Fainelli { 116404ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 116504ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 116604ff53f9SFlorian Fainelli 116704ff53f9SFlorian Fainelli if (!netpoll) 116804ff53f9SFlorian Fainelli return; 116904ff53f9SFlorian Fainelli 117004ff53f9SFlorian Fainelli p->netpoll = NULL; 117104ff53f9SFlorian Fainelli 1172c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 117304ff53f9SFlorian Fainelli } 117404ff53f9SFlorian Fainelli 117504ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 117604ff53f9SFlorian Fainelli { 117704ff53f9SFlorian Fainelli } 117804ff53f9SFlorian Fainelli #endif 117904ff53f9SFlorian Fainelli 1180f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 11814fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 1182f50f2127SFlorian Fainelli { 11834fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 1184f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1185f50f2127SFlorian Fainelli 1186f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 1187f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 1188f50f2127SFlorian Fainelli return mall_tc_entry; 1189f50f2127SFlorian Fainelli 1190f50f2127SFlorian Fainelli return NULL; 1191f50f2127SFlorian Fainelli } 1192f50f2127SFlorian Fainelli 1193e13c2075SVladimir Oltean static int 1194e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev, 1195f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 1196f50f2127SFlorian Fainelli bool ingress) 1197f50f2127SFlorian Fainelli { 11980148bb50SVladimir Oltean struct netlink_ext_ack *extack = cls->common.extack; 1199d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1200f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1201e13c2075SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror; 1202f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1203d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 12049681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 1205d945097bSVivien Didelot struct dsa_port *to_dp; 1206e13c2075SVladimir Oltean int err; 1207e13c2075SVladimir Oltean 1208f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 120934297176SVladimir Oltean return -EOPNOTSUPP; 1210f50f2127SFlorian Fainelli 121153eca1f3SJakub Kicinski if (!flow_action_basic_hw_stats_check(&cls->rule->action, 1212319a1d19SJiri Pirko cls->common.extack)) 121334297176SVladimir Oltean return -EOPNOTSUPP; 1214319a1d19SJiri Pirko 12159681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 1216f50f2127SFlorian Fainelli 121765722159SVladimir Oltean if (!act->dev) 121865722159SVladimir Oltean return -EINVAL; 121965722159SVladimir Oltean 12209681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 1221f50f2127SFlorian Fainelli return -EOPNOTSUPP; 1222f50f2127SFlorian Fainelli 1223f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 1224f50f2127SFlorian Fainelli if (!mall_tc_entry) 1225f50f2127SFlorian Fainelli return -ENOMEM; 1226f50f2127SFlorian Fainelli 1227f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 1228f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 1229f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 1230f50f2127SFlorian Fainelli 12319681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 1232f50f2127SFlorian Fainelli 1233d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 1234f50f2127SFlorian Fainelli mirror->ingress = ingress; 1235f50f2127SFlorian Fainelli 12360148bb50SVladimir Oltean err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack); 1237f50f2127SFlorian Fainelli if (err) { 1238f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1239f50f2127SFlorian Fainelli return err; 1240f50f2127SFlorian Fainelli } 1241f50f2127SFlorian Fainelli 1242f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 1243e13c2075SVladimir Oltean 1244e13c2075SVladimir Oltean return err; 1245f50f2127SFlorian Fainelli } 1246f50f2127SFlorian Fainelli 124734297176SVladimir Oltean static int 124834297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev, 124934297176SVladimir Oltean struct tc_cls_matchall_offload *cls, 125034297176SVladimir Oltean bool ingress) 125134297176SVladimir Oltean { 125234297176SVladimir Oltean struct netlink_ext_ack *extack = cls->common.extack; 125334297176SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 125434297176SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 125534297176SVladimir Oltean struct dsa_mall_policer_tc_entry *policer; 125634297176SVladimir Oltean struct dsa_mall_tc_entry *mall_tc_entry; 125734297176SVladimir Oltean struct dsa_switch *ds = dp->ds; 125834297176SVladimir Oltean struct flow_action_entry *act; 125934297176SVladimir Oltean int err; 126034297176SVladimir Oltean 126134297176SVladimir Oltean if (!ds->ops->port_policer_add) { 126234297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1263c75a33c8SJacob Keller "Policing offload not implemented"); 126434297176SVladimir Oltean return -EOPNOTSUPP; 126534297176SVladimir Oltean } 126634297176SVladimir Oltean 126734297176SVladimir Oltean if (!ingress) { 126834297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1269c75a33c8SJacob Keller "Only supported on ingress qdisc"); 127034297176SVladimir Oltean return -EOPNOTSUPP; 127134297176SVladimir Oltean } 127234297176SVladimir Oltean 127334297176SVladimir Oltean if (!flow_action_basic_hw_stats_check(&cls->rule->action, 127434297176SVladimir Oltean cls->common.extack)) 127534297176SVladimir Oltean return -EOPNOTSUPP; 127634297176SVladimir Oltean 127734297176SVladimir Oltean list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) { 127834297176SVladimir Oltean if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) { 127934297176SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1280c75a33c8SJacob Keller "Only one port policer allowed"); 128134297176SVladimir Oltean return -EEXIST; 128234297176SVladimir Oltean } 128334297176SVladimir Oltean } 128434297176SVladimir Oltean 128534297176SVladimir Oltean act = &cls->rule->action.entries[0]; 128634297176SVladimir Oltean 128734297176SVladimir Oltean mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 128834297176SVladimir Oltean if (!mall_tc_entry) 128934297176SVladimir Oltean return -ENOMEM; 129034297176SVladimir Oltean 129134297176SVladimir Oltean mall_tc_entry->cookie = cls->cookie; 129234297176SVladimir Oltean mall_tc_entry->type = DSA_PORT_MALL_POLICER; 129334297176SVladimir Oltean policer = &mall_tc_entry->policer; 129434297176SVladimir Oltean policer->rate_bytes_per_sec = act->police.rate_bytes_ps; 129534297176SVladimir Oltean policer->burst = act->police.burst; 129634297176SVladimir Oltean 129734297176SVladimir Oltean err = ds->ops->port_policer_add(ds, dp->index, policer); 129834297176SVladimir Oltean if (err) { 129934297176SVladimir Oltean kfree(mall_tc_entry); 130034297176SVladimir Oltean return err; 130134297176SVladimir Oltean } 130234297176SVladimir Oltean 130334297176SVladimir Oltean list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 130434297176SVladimir Oltean 130534297176SVladimir Oltean return err; 130634297176SVladimir Oltean } 130734297176SVladimir Oltean 1308e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev, 1309e13c2075SVladimir Oltean struct tc_cls_matchall_offload *cls, 1310e13c2075SVladimir Oltean bool ingress) 1311e13c2075SVladimir Oltean { 1312e13c2075SVladimir Oltean int err = -EOPNOTSUPP; 1313e13c2075SVladimir Oltean 1314e13c2075SVladimir Oltean if (cls->common.protocol == htons(ETH_P_ALL) && 1315e13c2075SVladimir Oltean flow_offload_has_one_action(&cls->rule->action) && 1316e13c2075SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED) 1317e13c2075SVladimir Oltean err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress); 131834297176SVladimir Oltean else if (flow_offload_has_one_action(&cls->rule->action) && 131934297176SVladimir Oltean cls->rule->action.entries[0].id == FLOW_ACTION_POLICE) 132034297176SVladimir Oltean err = dsa_slave_add_cls_matchall_police(dev, cls, ingress); 1321e13c2075SVladimir Oltean 1322e13c2075SVladimir Oltean return err; 1323f50f2127SFlorian Fainelli } 1324f50f2127SFlorian Fainelli 1325f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 1326f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 1327f50f2127SFlorian Fainelli { 1328d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1329f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 1330d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1331f50f2127SFlorian Fainelli 13324fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 1333f50f2127SFlorian Fainelli if (!mall_tc_entry) 1334f50f2127SFlorian Fainelli return; 1335f50f2127SFlorian Fainelli 1336f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 1337f50f2127SFlorian Fainelli 1338f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 1339f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 134034297176SVladimir Oltean if (ds->ops->port_mirror_del) 134134297176SVladimir Oltean ds->ops->port_mirror_del(ds, dp->index, 134234297176SVladimir Oltean &mall_tc_entry->mirror); 134334297176SVladimir Oltean break; 134434297176SVladimir Oltean case DSA_PORT_MALL_POLICER: 134534297176SVladimir Oltean if (ds->ops->port_policer_del) 134634297176SVladimir Oltean ds->ops->port_policer_del(ds, dp->index); 1347f50f2127SFlorian Fainelli break; 1348f50f2127SFlorian Fainelli default: 1349f50f2127SFlorian Fainelli WARN_ON(1); 1350f50f2127SFlorian Fainelli } 1351f50f2127SFlorian Fainelli 1352f50f2127SFlorian Fainelli kfree(mall_tc_entry); 1353f50f2127SFlorian Fainelli } 1354f50f2127SFlorian Fainelli 13553fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 13566b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 13576b3eb752SJiri Pirko bool ingress) 1358f50f2127SFlorian Fainelli { 13595fd9fc4eSJiri Pirko if (cls->common.chain_index) 1360a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 1361f50f2127SFlorian Fainelli 13623fbae382SJiri Pirko switch (cls->command) { 13633fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 13645fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 13653fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 13663fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 13673fbae382SJiri Pirko return 0; 13683fbae382SJiri Pirko default: 13693fbae382SJiri Pirko return -EOPNOTSUPP; 13703fbae382SJiri Pirko } 13713fbae382SJiri Pirko } 13723fbae382SJiri Pirko 1373ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev, 1374ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1375ed11bb1fSVladimir Oltean bool ingress) 1376ed11bb1fSVladimir Oltean { 1377ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1378ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1379ed11bb1fSVladimir Oltean int port = dp->index; 1380ed11bb1fSVladimir Oltean 1381ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_add) 1382ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1383ed11bb1fSVladimir Oltean 1384ed11bb1fSVladimir Oltean return ds->ops->cls_flower_add(ds, port, cls, ingress); 1385ed11bb1fSVladimir Oltean } 1386ed11bb1fSVladimir Oltean 1387ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev, 1388ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1389ed11bb1fSVladimir Oltean bool ingress) 1390ed11bb1fSVladimir Oltean { 1391ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1392ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1393ed11bb1fSVladimir Oltean int port = dp->index; 1394ed11bb1fSVladimir Oltean 1395ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_del) 1396ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1397ed11bb1fSVladimir Oltean 1398ed11bb1fSVladimir Oltean return ds->ops->cls_flower_del(ds, port, cls, ingress); 1399ed11bb1fSVladimir Oltean } 1400ed11bb1fSVladimir Oltean 1401ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev, 1402ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1403ed11bb1fSVladimir Oltean bool ingress) 1404ed11bb1fSVladimir Oltean { 1405ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1406ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1407ed11bb1fSVladimir Oltean int port = dp->index; 1408ed11bb1fSVladimir Oltean 1409ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_stats) 1410ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1411ed11bb1fSVladimir Oltean 1412ed11bb1fSVladimir Oltean return ds->ops->cls_flower_stats(ds, port, cls, ingress); 1413ed11bb1fSVladimir Oltean } 1414ed11bb1fSVladimir Oltean 1415ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 1416ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 1417ed11bb1fSVladimir Oltean bool ingress) 1418ed11bb1fSVladimir Oltean { 1419ed11bb1fSVladimir Oltean switch (cls->command) { 1420ed11bb1fSVladimir Oltean case FLOW_CLS_REPLACE: 1421ed11bb1fSVladimir Oltean return dsa_slave_add_cls_flower(dev, cls, ingress); 1422ed11bb1fSVladimir Oltean case FLOW_CLS_DESTROY: 1423ed11bb1fSVladimir Oltean return dsa_slave_del_cls_flower(dev, cls, ingress); 1424ed11bb1fSVladimir Oltean case FLOW_CLS_STATS: 1425ed11bb1fSVladimir Oltean return dsa_slave_stats_cls_flower(dev, cls, ingress); 1426ed11bb1fSVladimir Oltean default: 1427ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1428ed11bb1fSVladimir Oltean } 1429ed11bb1fSVladimir Oltean } 1430ed11bb1fSVladimir Oltean 14316b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 14326b3eb752SJiri Pirko void *cb_priv, bool ingress) 14336b3eb752SJiri Pirko { 14346b3eb752SJiri Pirko struct net_device *dev = cb_priv; 14356b3eb752SJiri Pirko 143644ae12a7SJiri Pirko if (!tc_can_offload(dev)) 143744ae12a7SJiri Pirko return -EOPNOTSUPP; 143844ae12a7SJiri Pirko 14396b3eb752SJiri Pirko switch (type) { 14406b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 14416b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1442ed11bb1fSVladimir Oltean case TC_SETUP_CLSFLOWER: 1443ed11bb1fSVladimir Oltean return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 14446b3eb752SJiri Pirko default: 14456b3eb752SJiri Pirko return -EOPNOTSUPP; 14466b3eb752SJiri Pirko } 14476b3eb752SJiri Pirko } 14486b3eb752SJiri Pirko 14496b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 14506b3eb752SJiri Pirko void *type_data, void *cb_priv) 14516b3eb752SJiri Pirko { 14526b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 14536b3eb752SJiri Pirko } 14546b3eb752SJiri Pirko 14556b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 14566b3eb752SJiri Pirko void *type_data, void *cb_priv) 14576b3eb752SJiri Pirko { 14586b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 14596b3eb752SJiri Pirko } 14606b3eb752SJiri Pirko 1461955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 1462955bcb6eSPablo Neira Ayuso 14636b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 1464955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 14656b3eb752SJiri Pirko { 1466955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 1467a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 14686b3eb752SJiri Pirko 146932f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 14706b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 147132f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 14726b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 14736b3eb752SJiri Pirko else 14746b3eb752SJiri Pirko return -EOPNOTSUPP; 14756b3eb752SJiri Pirko 1476955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 1477955bcb6eSPablo Neira Ayuso 14786b3eb752SJiri Pirko switch (f->command) { 14799c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 14800d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 14810d4fd02eSPablo Neira Ayuso return -EBUSY; 14820d4fd02eSPablo Neira Ayuso 14830c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1484955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 1485955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 1486955bcb6eSPablo Neira Ayuso 1487955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 1488955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1489955bcb6eSPablo Neira Ayuso return 0; 14909c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 149114bfb13fSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f->block, cb, dev); 1492955bcb6eSPablo Neira Ayuso if (!block_cb) 1493955bcb6eSPablo Neira Ayuso return -ENOENT; 1494955bcb6eSPablo Neira Ayuso 1495955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 1496955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 14976b3eb752SJiri Pirko return 0; 14986b3eb752SJiri Pirko default: 14996b3eb752SJiri Pirko return -EOPNOTSUPP; 15006b3eb752SJiri Pirko } 15016b3eb752SJiri Pirko } 15026b3eb752SJiri Pirko 15033fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port, 15043fb24a43SPablo Neira Ayuso void *type_data) 15053fb24a43SPablo Neira Ayuso { 15063fb24a43SPablo Neira Ayuso struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp; 15073fb24a43SPablo Neira Ayuso struct net_device *master = cpu_dp->master; 15083fb24a43SPablo Neira Ayuso 15093fb24a43SPablo Neira Ayuso if (!master->netdev_ops->ndo_setup_tc) 15103fb24a43SPablo Neira Ayuso return -EOPNOTSUPP; 15113fb24a43SPablo Neira Ayuso 15123fb24a43SPablo Neira Ayuso return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data); 15133fb24a43SPablo Neira Ayuso } 15143fb24a43SPablo Neira Ayuso 15153fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1516de4784caSJiri Pirko void *type_data) 15173fbae382SJiri Pirko { 151847d23af2SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 151947d23af2SVladimir Oltean struct dsa_switch *ds = dp->ds; 152047d23af2SVladimir Oltean 15213fb24a43SPablo Neira Ayuso switch (type) { 15223fb24a43SPablo Neira Ayuso case TC_SETUP_BLOCK: 15236b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 15243fb24a43SPablo Neira Ayuso case TC_SETUP_FT: 15253fb24a43SPablo Neira Ayuso return dsa_slave_setup_ft_block(ds, dp->index, type_data); 15263fb24a43SPablo Neira Ayuso default: 15273fb24a43SPablo Neira Ayuso break; 15283fb24a43SPablo Neira Ayuso } 152947d23af2SVladimir Oltean 153047d23af2SVladimir Oltean if (!ds->ops->port_setup_tc) 1531a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 153247d23af2SVladimir Oltean 153347d23af2SVladimir Oltean return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1534f50f2127SFlorian Fainelli } 1535f50f2127SFlorian Fainelli 1536bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1537bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1538bf9f2648SFlorian Fainelli { 1539d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1540d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1541bf9f2648SFlorian Fainelli 1542bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1543bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1544bf9f2648SFlorian Fainelli 1545d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1546bf9f2648SFlorian Fainelli } 1547bf9f2648SFlorian Fainelli 1548bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1549bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1550bf9f2648SFlorian Fainelli { 1551d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1552d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1553bf9f2648SFlorian Fainelli 1554bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1555bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1556bf9f2648SFlorian Fainelli 1557d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1558bf9f2648SFlorian Fainelli } 1559bf9f2648SFlorian Fainelli 15600336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 15610336369dSBrandon Streiff struct ethtool_ts_info *ts) 15620336369dSBrandon Streiff { 15630336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 15640336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 15650336369dSBrandon Streiff 15660336369dSBrandon Streiff if (!ds->ops->get_ts_info) 15670336369dSBrandon Streiff return -EOPNOTSUPP; 15680336369dSBrandon Streiff 15690336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 15700336369dSBrandon Streiff } 15710336369dSBrandon Streiff 1572061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1573061f6a50SFlorian Fainelli u16 vid) 1574061f6a50SFlorian Fainelli { 1575061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 157688236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 157788236591SVladimir Oltean .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1578b7a9e0daSVladimir Oltean .vid = vid, 157988236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 158088236591SVladimir Oltean .flags = 0, 158188236591SVladimir Oltean }; 158231046a5fSVladimir Oltean struct netlink_ext_ack extack = {0}; 1583061f6a50SFlorian Fainelli int ret; 1584061f6a50SFlorian Fainelli 158588236591SVladimir Oltean /* User port... */ 158631046a5fSVladimir Oltean ret = dsa_port_vlan_add(dp, &vlan, &extack); 158731046a5fSVladimir Oltean if (ret) { 158831046a5fSVladimir Oltean if (extack._msg) 158931046a5fSVladimir Oltean netdev_err(dev, "%s\n", extack._msg); 159088236591SVladimir Oltean return ret; 159131046a5fSVladimir Oltean } 159288236591SVladimir Oltean 159388236591SVladimir Oltean /* And CPU port... */ 1594134ef238SVladimir Oltean ret = dsa_port_host_vlan_add(dp, &vlan, &extack); 159531046a5fSVladimir Oltean if (ret) { 159631046a5fSVladimir Oltean if (extack._msg) 159731046a5fSVladimir Oltean netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index, 159831046a5fSVladimir Oltean extack._msg); 15997e1741b4SVivien Didelot return ret; 160031046a5fSVladimir Oltean } 16017e1741b4SVivien Didelot 1602134ef238SVladimir Oltean return 0; 1603061f6a50SFlorian Fainelli } 1604061f6a50SFlorian Fainelli 1605061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1606061f6a50SFlorian Fainelli u16 vid) 1607061f6a50SFlorian Fainelli { 1608061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 160988236591SVladimir Oltean struct switchdev_obj_port_vlan vlan = { 1610b7a9e0daSVladimir Oltean .vid = vid, 161188236591SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 161288236591SVladimir Oltean .flags = 0, 161388236591SVladimir Oltean }; 16142209158cSVladimir Oltean int err; 1615061f6a50SFlorian Fainelli 16162209158cSVladimir Oltean err = dsa_port_vlan_del(dp, &vlan); 16172209158cSVladimir Oltean if (err) 16182209158cSVladimir Oltean return err; 16192209158cSVladimir Oltean 1620134ef238SVladimir Oltean return dsa_port_host_vlan_del(dp, &vlan); 1621061f6a50SFlorian Fainelli } 1622061f6a50SFlorian Fainelli 162306cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg) 162406cfb2dfSVladimir Oltean { 162506cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 162606cfb2dfSVladimir Oltean 162706cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_add_vid(arg, proto, vid); 162806cfb2dfSVladimir Oltean } 162906cfb2dfSVladimir Oltean 163006cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg) 163106cfb2dfSVladimir Oltean { 163206cfb2dfSVladimir Oltean __be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q); 163306cfb2dfSVladimir Oltean 163406cfb2dfSVladimir Oltean return dsa_slave_vlan_rx_kill_vid(arg, proto, vid); 163506cfb2dfSVladimir Oltean } 163606cfb2dfSVladimir Oltean 163706cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN 163806cfb2dfSVladimir Oltean * filtering is enabled. The baseline is that only ports that offload a 163906cfb2dfSVladimir Oltean * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware, 164006cfb2dfSVladimir Oltean * but there are exceptions for quirky hardware. 164106cfb2dfSVladimir Oltean * 164206cfb2dfSVladimir Oltean * If ds->vlan_filtering_is_global = true, then standalone ports which share 164306cfb2dfSVladimir Oltean * the same switch with other ports that offload a VLAN-aware bridge are also 164406cfb2dfSVladimir Oltean * inevitably VLAN-aware. 164506cfb2dfSVladimir Oltean * 164606cfb2dfSVladimir Oltean * To summarize, a DSA switch port offloads: 164706cfb2dfSVladimir Oltean * 164806cfb2dfSVladimir Oltean * - If standalone (this includes software bridge, software LAG): 164958adf9dcSVladimir Oltean * - if ds->needs_standalone_vlan_filtering = true, OR if 165058adf9dcSVladimir Oltean * (ds->vlan_filtering_is_global = true AND there are bridges spanning 165158adf9dcSVladimir Oltean * this switch chip which have vlan_filtering=1) 165206cfb2dfSVladimir Oltean * - the 8021q upper VLANs 165358adf9dcSVladimir Oltean * - else (standalone VLAN filtering is not needed, VLAN filtering is not 165458adf9dcSVladimir Oltean * global, or it is, but no port is under a VLAN-aware bridge): 165506cfb2dfSVladimir Oltean * - no VLAN (any 8021q upper is a software VLAN) 165606cfb2dfSVladimir Oltean * 165706cfb2dfSVladimir Oltean * - If under a vlan_filtering=0 bridge which it offload: 165806cfb2dfSVladimir Oltean * - if ds->configure_vlan_while_not_filtering = true (default): 165906cfb2dfSVladimir Oltean * - the bridge VLANs. These VLANs are committed to hardware but inactive. 166006cfb2dfSVladimir Oltean * - else (deprecated): 166106cfb2dfSVladimir Oltean * - no VLAN. The bridge VLANs are not restored when VLAN awareness is 166206cfb2dfSVladimir Oltean * enabled, so this behavior is broken and discouraged. 166306cfb2dfSVladimir Oltean * 166406cfb2dfSVladimir Oltean * - If under a vlan_filtering=1 bridge which it offload: 166506cfb2dfSVladimir Oltean * - the bridge VLANs 166606cfb2dfSVladimir Oltean * - the 8021q upper VLANs 166706cfb2dfSVladimir Oltean */ 166806cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave, 166906cfb2dfSVladimir Oltean bool vlan_filtering) 167006cfb2dfSVladimir Oltean { 167106cfb2dfSVladimir Oltean int err; 167206cfb2dfSVladimir Oltean 167306cfb2dfSVladimir Oltean if (vlan_filtering) { 167406cfb2dfSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 167506cfb2dfSVladimir Oltean 167606cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_restore_vlan, slave); 167706cfb2dfSVladimir Oltean if (err) { 167806cfb2dfSVladimir Oltean vlan_for_each(slave, dsa_slave_clear_vlan, slave); 167906cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 168006cfb2dfSVladimir Oltean return err; 168106cfb2dfSVladimir Oltean } 168206cfb2dfSVladimir Oltean } else { 168306cfb2dfSVladimir Oltean err = vlan_for_each(slave, dsa_slave_clear_vlan, slave); 168406cfb2dfSVladimir Oltean if (err) 168506cfb2dfSVladimir Oltean return err; 168606cfb2dfSVladimir Oltean 168706cfb2dfSVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 168806cfb2dfSVladimir Oltean } 168906cfb2dfSVladimir Oltean 169006cfb2dfSVladimir Oltean return 0; 169106cfb2dfSVladimir Oltean } 169206cfb2dfSVladimir Oltean 1693bff33f7eSVladimir Oltean struct dsa_hw_port { 1694bff33f7eSVladimir Oltean struct list_head list; 1695bff33f7eSVladimir Oltean struct net_device *dev; 1696bff33f7eSVladimir Oltean int old_mtu; 1697bff33f7eSVladimir Oltean }; 1698bff33f7eSVladimir Oltean 1699bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1700bff33f7eSVladimir Oltean { 1701bff33f7eSVladimir Oltean const struct dsa_hw_port *p; 1702bff33f7eSVladimir Oltean int err; 1703bff33f7eSVladimir Oltean 1704bff33f7eSVladimir Oltean list_for_each_entry(p, hw_port_list, list) { 1705bff33f7eSVladimir Oltean if (p->dev->mtu == mtu) 1706bff33f7eSVladimir Oltean continue; 1707bff33f7eSVladimir Oltean 1708bff33f7eSVladimir Oltean err = dev_set_mtu(p->dev, mtu); 1709bff33f7eSVladimir Oltean if (err) 1710bff33f7eSVladimir Oltean goto rollback; 1711bff33f7eSVladimir Oltean } 1712bff33f7eSVladimir Oltean 1713bff33f7eSVladimir Oltean return 0; 1714bff33f7eSVladimir Oltean 1715bff33f7eSVladimir Oltean rollback: 1716bff33f7eSVladimir Oltean list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1717bff33f7eSVladimir Oltean if (p->dev->mtu == p->old_mtu) 1718bff33f7eSVladimir Oltean continue; 1719bff33f7eSVladimir Oltean 1720bff33f7eSVladimir Oltean if (dev_set_mtu(p->dev, p->old_mtu)) 1721bff33f7eSVladimir Oltean netdev_err(p->dev, "Failed to restore MTU\n"); 1722bff33f7eSVladimir Oltean } 1723bff33f7eSVladimir Oltean 1724bff33f7eSVladimir Oltean return err; 1725bff33f7eSVladimir Oltean } 1726bff33f7eSVladimir Oltean 1727bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1728bff33f7eSVladimir Oltean { 1729bff33f7eSVladimir Oltean struct dsa_hw_port *p, *n; 1730bff33f7eSVladimir Oltean 1731bff33f7eSVladimir Oltean list_for_each_entry_safe(p, n, hw_port_list, list) 1732bff33f7eSVladimir Oltean kfree(p); 1733bff33f7eSVladimir Oltean } 1734bff33f7eSVladimir Oltean 1735bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */ 1736bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1737bff33f7eSVladimir Oltean { 1738bff33f7eSVladimir Oltean struct list_head hw_port_list; 1739bff33f7eSVladimir Oltean struct dsa_switch_tree *dst; 1740bff33f7eSVladimir Oltean int min_mtu = ETH_MAX_MTU; 1741bff33f7eSVladimir Oltean struct dsa_port *other_dp; 1742bff33f7eSVladimir Oltean int err; 1743bff33f7eSVladimir Oltean 1744bff33f7eSVladimir Oltean if (!dp->ds->mtu_enforcement_ingress) 1745bff33f7eSVladimir Oltean return; 1746bff33f7eSVladimir Oltean 1747d3eed0e5SVladimir Oltean if (!dp->bridge) 1748bff33f7eSVladimir Oltean return; 1749bff33f7eSVladimir Oltean 1750bff33f7eSVladimir Oltean INIT_LIST_HEAD(&hw_port_list); 1751bff33f7eSVladimir Oltean 1752bff33f7eSVladimir Oltean /* Populate the list of ports that are part of the same bridge 1753bff33f7eSVladimir Oltean * as the newly added/modified port 1754bff33f7eSVladimir Oltean */ 1755bff33f7eSVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) { 1756bff33f7eSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 1757bff33f7eSVladimir Oltean struct dsa_hw_port *hw_port; 1758bff33f7eSVladimir Oltean struct net_device *slave; 1759bff33f7eSVladimir Oltean 1760bff33f7eSVladimir Oltean if (other_dp->type != DSA_PORT_TYPE_USER) 1761bff33f7eSVladimir Oltean continue; 1762bff33f7eSVladimir Oltean 176336cbf39bSVladimir Oltean if (!dsa_port_bridge_same(dp, other_dp)) 1764bff33f7eSVladimir Oltean continue; 1765bff33f7eSVladimir Oltean 1766bff33f7eSVladimir Oltean if (!other_dp->ds->mtu_enforcement_ingress) 1767bff33f7eSVladimir Oltean continue; 1768bff33f7eSVladimir Oltean 1769bff33f7eSVladimir Oltean slave = other_dp->slave; 1770bff33f7eSVladimir Oltean 1771bff33f7eSVladimir Oltean if (min_mtu > slave->mtu) 1772bff33f7eSVladimir Oltean min_mtu = slave->mtu; 1773bff33f7eSVladimir Oltean 1774bff33f7eSVladimir Oltean hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1775bff33f7eSVladimir Oltean if (!hw_port) 1776bff33f7eSVladimir Oltean goto out; 1777bff33f7eSVladimir Oltean 1778bff33f7eSVladimir Oltean hw_port->dev = slave; 1779bff33f7eSVladimir Oltean hw_port->old_mtu = slave->mtu; 1780bff33f7eSVladimir Oltean 1781bff33f7eSVladimir Oltean list_add(&hw_port->list, &hw_port_list); 1782bff33f7eSVladimir Oltean } 1783bff33f7eSVladimir Oltean } 1784bff33f7eSVladimir Oltean 1785bff33f7eSVladimir Oltean /* Attempt to configure the entire hardware bridge to the newly added 1786bff33f7eSVladimir Oltean * interface's MTU first, regardless of whether the intention of the 1787bff33f7eSVladimir Oltean * user was to raise or lower it. 1788bff33f7eSVladimir Oltean */ 1789bff33f7eSVladimir Oltean err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1790bff33f7eSVladimir Oltean if (!err) 1791bff33f7eSVladimir Oltean goto out; 1792bff33f7eSVladimir Oltean 1793bff33f7eSVladimir Oltean /* Clearly that didn't work out so well, so just set the minimum MTU on 1794bff33f7eSVladimir Oltean * all hardware bridge ports now. If this fails too, then all ports will 1795bff33f7eSVladimir Oltean * still have their old MTU rolled back anyway. 1796bff33f7eSVladimir Oltean */ 1797bff33f7eSVladimir Oltean dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1798bff33f7eSVladimir Oltean 1799bff33f7eSVladimir Oltean out: 1800bff33f7eSVladimir Oltean dsa_hw_port_list_free(&hw_port_list); 1801bff33f7eSVladimir Oltean } 1802bff33f7eSVladimir Oltean 180353da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1804bfcb8132SVladimir Oltean { 1805bfcb8132SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1806bfcb8132SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1807cf1c39d3SVladimir Oltean struct dsa_port *cpu_dp = dp->cpu_dp; 18084715029fSVladimir Oltean struct dsa_switch *ds = dp->ds; 1809b2033a05SVladimir Oltean struct dsa_port *other_dp; 1810bfcb8132SVladimir Oltean int largest_mtu = 0; 1811bfcb8132SVladimir Oltean int new_master_mtu; 1812bfcb8132SVladimir Oltean int old_master_mtu; 1813bfcb8132SVladimir Oltean int mtu_limit; 1814bfcb8132SVladimir Oltean int cpu_mtu; 18154e4ab795SVladimir Oltean int err; 1816bfcb8132SVladimir Oltean 1817bfcb8132SVladimir Oltean if (!ds->ops->port_change_mtu) 1818bfcb8132SVladimir Oltean return -EOPNOTSUPP; 1819bfcb8132SVladimir Oltean 1820b2033a05SVladimir Oltean dsa_tree_for_each_user_port(other_dp, ds->dst) { 1821bfcb8132SVladimir Oltean int slave_mtu; 1822bfcb8132SVladimir Oltean 1823bfcb8132SVladimir Oltean /* During probe, this function will be called for each slave 1824bfcb8132SVladimir Oltean * device, while not all of them have been allocated. That's 1825bfcb8132SVladimir Oltean * ok, it doesn't change what the maximum is, so ignore it. 1826bfcb8132SVladimir Oltean */ 1827b2033a05SVladimir Oltean if (!other_dp->slave) 1828bfcb8132SVladimir Oltean continue; 1829bfcb8132SVladimir Oltean 1830bfcb8132SVladimir Oltean /* Pretend that we already applied the setting, which we 1831bfcb8132SVladimir Oltean * actually haven't (still haven't done all integrity checks) 1832bfcb8132SVladimir Oltean */ 1833b2033a05SVladimir Oltean if (dp == other_dp) 1834bfcb8132SVladimir Oltean slave_mtu = new_mtu; 1835bfcb8132SVladimir Oltean else 1836b2033a05SVladimir Oltean slave_mtu = other_dp->slave->mtu; 1837bfcb8132SVladimir Oltean 1838bfcb8132SVladimir Oltean if (largest_mtu < slave_mtu) 1839bfcb8132SVladimir Oltean largest_mtu = slave_mtu; 1840bfcb8132SVladimir Oltean } 1841bfcb8132SVladimir Oltean 1842bfcb8132SVladimir Oltean mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1843bfcb8132SVladimir Oltean old_master_mtu = master->mtu; 18444e500251SVladimir Oltean new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops); 1845bfcb8132SVladimir Oltean if (new_master_mtu > mtu_limit) 1846bfcb8132SVladimir Oltean return -ERANGE; 1847bfcb8132SVladimir Oltean 1848bfcb8132SVladimir Oltean /* If the master MTU isn't over limit, there's no need to check the CPU 1849bfcb8132SVladimir Oltean * MTU, since that surely isn't either. 1850bfcb8132SVladimir Oltean */ 1851bfcb8132SVladimir Oltean cpu_mtu = largest_mtu; 1852bfcb8132SVladimir Oltean 1853bfcb8132SVladimir Oltean /* Start applying stuff */ 1854bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) { 1855bfcb8132SVladimir Oltean err = dev_set_mtu(master, new_master_mtu); 1856bfcb8132SVladimir Oltean if (err < 0) 1857bfcb8132SVladimir Oltean goto out_master_failed; 1858bfcb8132SVladimir Oltean 1859bfcb8132SVladimir Oltean /* We only need to propagate the MTU of the CPU port to 1860be6ff966SVladimir Oltean * upstream switches, so emit a notifier which updates them. 1861bfcb8132SVladimir Oltean */ 1862be6ff966SVladimir Oltean err = dsa_port_mtu_change(cpu_dp, cpu_mtu); 1863bfcb8132SVladimir Oltean if (err) 1864bfcb8132SVladimir Oltean goto out_cpu_failed; 1865bfcb8132SVladimir Oltean } 1866bfcb8132SVladimir Oltean 1867be6ff966SVladimir Oltean err = ds->ops->port_change_mtu(ds, dp->index, new_mtu); 1868bfcb8132SVladimir Oltean if (err) 1869bfcb8132SVladimir Oltean goto out_port_failed; 1870bfcb8132SVladimir Oltean 1871bfcb8132SVladimir Oltean dev->mtu = new_mtu; 1872bfcb8132SVladimir Oltean 1873bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 1874bff33f7eSVladimir Oltean 1875bfcb8132SVladimir Oltean return 0; 1876bfcb8132SVladimir Oltean 1877bfcb8132SVladimir Oltean out_port_failed: 1878bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1879bfcb8132SVladimir Oltean dsa_port_mtu_change(cpu_dp, old_master_mtu - 1880be6ff966SVladimir Oltean dsa_tag_protocol_overhead(cpu_dp->tag_ops)); 1881bfcb8132SVladimir Oltean out_cpu_failed: 1882bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1883bfcb8132SVladimir Oltean dev_set_mtu(master, old_master_mtu); 1884bfcb8132SVladimir Oltean out_master_failed: 1885bfcb8132SVladimir Oltean return err; 1886bfcb8132SVladimir Oltean } 1887bfcb8132SVladimir Oltean 1888d538eca8SVladimir Oltean static int __maybe_unused 1889d538eca8SVladimir Oltean dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app) 1890d538eca8SVladimir Oltean { 1891d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1892d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1893d538eca8SVladimir Oltean unsigned long mask, new_prio; 1894d538eca8SVladimir Oltean int err, port = dp->index; 1895d538eca8SVladimir Oltean 1896d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1897d538eca8SVladimir Oltean return -EOPNOTSUPP; 1898d538eca8SVladimir Oltean 1899d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, app); 1900d538eca8SVladimir Oltean if (err) 1901d538eca8SVladimir Oltean return err; 1902d538eca8SVladimir Oltean 1903d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1904d538eca8SVladimir Oltean new_prio = __fls(mask); 1905d538eca8SVladimir Oltean 1906d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1907d538eca8SVladimir Oltean if (err) { 1908d538eca8SVladimir Oltean dcb_ieee_delapp(dev, app); 1909d538eca8SVladimir Oltean return err; 1910d538eca8SVladimir Oltean } 1911d538eca8SVladimir Oltean 1912d538eca8SVladimir Oltean return 0; 1913d538eca8SVladimir Oltean } 1914d538eca8SVladimir Oltean 191547d75f78SVladimir Oltean static int __maybe_unused 191647d75f78SVladimir Oltean dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app) 191747d75f78SVladimir Oltean { 191847d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 191947d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 192047d75f78SVladimir Oltean unsigned long mask, new_prio; 192147d75f78SVladimir Oltean int err, port = dp->index; 192247d75f78SVladimir Oltean u8 dscp = app->protocol; 192347d75f78SVladimir Oltean 192447d75f78SVladimir Oltean if (!ds->ops->port_add_dscp_prio) 192547d75f78SVladimir Oltean return -EOPNOTSUPP; 192647d75f78SVladimir Oltean 192747d75f78SVladimir Oltean if (dscp >= 64) { 192847d75f78SVladimir Oltean netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n", 192947d75f78SVladimir Oltean dscp); 193047d75f78SVladimir Oltean return -EINVAL; 193147d75f78SVladimir Oltean } 193247d75f78SVladimir Oltean 193347d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, app); 193447d75f78SVladimir Oltean if (err) 193547d75f78SVladimir Oltean return err; 193647d75f78SVladimir Oltean 193747d75f78SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 193847d75f78SVladimir Oltean new_prio = __fls(mask); 193947d75f78SVladimir Oltean 194047d75f78SVladimir Oltean err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio); 194147d75f78SVladimir Oltean if (err) { 194247d75f78SVladimir Oltean dcb_ieee_delapp(dev, app); 194347d75f78SVladimir Oltean return err; 194447d75f78SVladimir Oltean } 194547d75f78SVladimir Oltean 194647d75f78SVladimir Oltean return 0; 194747d75f78SVladimir Oltean } 194847d75f78SVladimir Oltean 1949d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev, 1950d538eca8SVladimir Oltean struct dcb_app *app) 1951d538eca8SVladimir Oltean { 1952d538eca8SVladimir Oltean switch (app->selector) { 1953d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 1954d538eca8SVladimir Oltean switch (app->protocol) { 1955d538eca8SVladimir Oltean case 0: 1956d538eca8SVladimir Oltean return dsa_slave_dcbnl_set_default_prio(dev, app); 1957d538eca8SVladimir Oltean default: 1958d538eca8SVladimir Oltean return -EOPNOTSUPP; 1959d538eca8SVladimir Oltean } 1960d538eca8SVladimir Oltean break; 196147d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 196247d75f78SVladimir Oltean return dsa_slave_dcbnl_add_dscp_prio(dev, app); 1963d538eca8SVladimir Oltean default: 1964d538eca8SVladimir Oltean return -EOPNOTSUPP; 1965d538eca8SVladimir Oltean } 1966d538eca8SVladimir Oltean } 1967d538eca8SVladimir Oltean 1968d538eca8SVladimir Oltean static int __maybe_unused 1969d538eca8SVladimir Oltean dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app) 1970d538eca8SVladimir Oltean { 1971d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1972d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 1973d538eca8SVladimir Oltean unsigned long mask, new_prio; 1974d538eca8SVladimir Oltean int err, port = dp->index; 1975d538eca8SVladimir Oltean 1976d538eca8SVladimir Oltean if (!ds->ops->port_set_default_prio) 1977d538eca8SVladimir Oltean return -EOPNOTSUPP; 1978d538eca8SVladimir Oltean 1979d538eca8SVladimir Oltean err = dcb_ieee_delapp(dev, app); 1980d538eca8SVladimir Oltean if (err) 1981d538eca8SVladimir Oltean return err; 1982d538eca8SVladimir Oltean 1983d538eca8SVladimir Oltean mask = dcb_ieee_getapp_mask(dev, app); 1984d538eca8SVladimir Oltean new_prio = mask ? __fls(mask) : 0; 1985d538eca8SVladimir Oltean 1986d538eca8SVladimir Oltean err = ds->ops->port_set_default_prio(ds, port, new_prio); 1987d538eca8SVladimir Oltean if (err) { 1988d538eca8SVladimir Oltean dcb_ieee_setapp(dev, app); 1989d538eca8SVladimir Oltean return err; 1990d538eca8SVladimir Oltean } 1991d538eca8SVladimir Oltean 1992d538eca8SVladimir Oltean return 0; 1993d538eca8SVladimir Oltean } 1994d538eca8SVladimir Oltean 199547d75f78SVladimir Oltean static int __maybe_unused 199647d75f78SVladimir Oltean dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app) 199747d75f78SVladimir Oltean { 199847d75f78SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 199947d75f78SVladimir Oltean struct dsa_switch *ds = dp->ds; 200047d75f78SVladimir Oltean int err, port = dp->index; 200147d75f78SVladimir Oltean u8 dscp = app->protocol; 200247d75f78SVladimir Oltean 200347d75f78SVladimir Oltean if (!ds->ops->port_del_dscp_prio) 200447d75f78SVladimir Oltean return -EOPNOTSUPP; 200547d75f78SVladimir Oltean 200647d75f78SVladimir Oltean err = dcb_ieee_delapp(dev, app); 200747d75f78SVladimir Oltean if (err) 200847d75f78SVladimir Oltean return err; 200947d75f78SVladimir Oltean 201047d75f78SVladimir Oltean err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority); 201147d75f78SVladimir Oltean if (err) { 201247d75f78SVladimir Oltean dcb_ieee_setapp(dev, app); 201347d75f78SVladimir Oltean return err; 201447d75f78SVladimir Oltean } 201547d75f78SVladimir Oltean 201647d75f78SVladimir Oltean return 0; 201747d75f78SVladimir Oltean } 201847d75f78SVladimir Oltean 2019d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev, 2020d538eca8SVladimir Oltean struct dcb_app *app) 2021d538eca8SVladimir Oltean { 2022d538eca8SVladimir Oltean switch (app->selector) { 2023d538eca8SVladimir Oltean case IEEE_8021QAZ_APP_SEL_ETHERTYPE: 2024d538eca8SVladimir Oltean switch (app->protocol) { 2025d538eca8SVladimir Oltean case 0: 2026d538eca8SVladimir Oltean return dsa_slave_dcbnl_del_default_prio(dev, app); 2027d538eca8SVladimir Oltean default: 2028d538eca8SVladimir Oltean return -EOPNOTSUPP; 2029d538eca8SVladimir Oltean } 2030d538eca8SVladimir Oltean break; 203147d75f78SVladimir Oltean case IEEE_8021QAZ_APP_SEL_DSCP: 203247d75f78SVladimir Oltean return dsa_slave_dcbnl_del_dscp_prio(dev, app); 2033d538eca8SVladimir Oltean default: 2034d538eca8SVladimir Oltean return -EOPNOTSUPP; 2035d538eca8SVladimir Oltean } 2036d538eca8SVladimir Oltean } 2037d538eca8SVladimir Oltean 2038d538eca8SVladimir Oltean /* Pre-populate the DCB application priority table with the priorities 2039d538eca8SVladimir Oltean * configured during switch setup, which we read from hardware here. 2040d538eca8SVladimir Oltean */ 2041d538eca8SVladimir Oltean static int dsa_slave_dcbnl_init(struct net_device *dev) 2042d538eca8SVladimir Oltean { 2043d538eca8SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2044d538eca8SVladimir Oltean struct dsa_switch *ds = dp->ds; 2045d538eca8SVladimir Oltean int port = dp->index; 2046d538eca8SVladimir Oltean int err; 2047d538eca8SVladimir Oltean 2048d538eca8SVladimir Oltean if (ds->ops->port_get_default_prio) { 2049d538eca8SVladimir Oltean int prio = ds->ops->port_get_default_prio(ds, port); 2050d538eca8SVladimir Oltean struct dcb_app app = { 2051d538eca8SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE, 2052d538eca8SVladimir Oltean .protocol = 0, 2053d538eca8SVladimir Oltean .priority = prio, 2054d538eca8SVladimir Oltean }; 2055d538eca8SVladimir Oltean 2056d538eca8SVladimir Oltean if (prio < 0) 2057d538eca8SVladimir Oltean return prio; 2058d538eca8SVladimir Oltean 2059d538eca8SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 2060d538eca8SVladimir Oltean if (err) 2061d538eca8SVladimir Oltean return err; 2062d538eca8SVladimir Oltean } 2063d538eca8SVladimir Oltean 206447d75f78SVladimir Oltean if (ds->ops->port_get_dscp_prio) { 206547d75f78SVladimir Oltean int protocol; 206647d75f78SVladimir Oltean 206747d75f78SVladimir Oltean for (protocol = 0; protocol < 64; protocol++) { 206847d75f78SVladimir Oltean struct dcb_app app = { 206947d75f78SVladimir Oltean .selector = IEEE_8021QAZ_APP_SEL_DSCP, 207047d75f78SVladimir Oltean .protocol = protocol, 207147d75f78SVladimir Oltean }; 207247d75f78SVladimir Oltean int prio; 207347d75f78SVladimir Oltean 207447d75f78SVladimir Oltean prio = ds->ops->port_get_dscp_prio(ds, port, protocol); 207547d75f78SVladimir Oltean if (prio == -EOPNOTSUPP) 207647d75f78SVladimir Oltean continue; 207747d75f78SVladimir Oltean if (prio < 0) 207847d75f78SVladimir Oltean return prio; 207947d75f78SVladimir Oltean 208047d75f78SVladimir Oltean app.priority = prio; 208147d75f78SVladimir Oltean 208247d75f78SVladimir Oltean err = dcb_ieee_setapp(dev, &app); 208347d75f78SVladimir Oltean if (err) 208447d75f78SVladimir Oltean return err; 208547d75f78SVladimir Oltean } 208647d75f78SVladimir Oltean } 208747d75f78SVladimir Oltean 2088d538eca8SVladimir Oltean return 0; 2089d538eca8SVladimir Oltean } 2090d538eca8SVladimir Oltean 209191da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 209291da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 20933d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 20943d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 2095aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 2096c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 20976793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 20986793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 20996793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 210091da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 210191da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 210291da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 2103487d3855SAlvin Šipraga .get_eth_phy_stats = dsa_slave_get_eth_phy_stats, 2104487d3855SAlvin Šipraga .get_eth_mac_stats = dsa_slave_get_eth_mac_stats, 2105487d3855SAlvin Šipraga .get_eth_ctrl_stats = dsa_slave_get_eth_ctrl_stats, 210667f38b1cSClément Léger .get_rmon_stats = dsa_slave_get_rmon_stats, 210719e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 210819e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 21097905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 21107905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 2111aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 2112aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 21133d410403SOleksij Rempel .get_pause_stats = dsa_slave_get_pause_stats, 2114a2a1a13bSHeiner Kallweit .get_pauseparam = dsa_slave_get_pauseparam, 2115a2a1a13bSHeiner Kallweit .set_pauseparam = dsa_slave_set_pauseparam, 2116bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 2117bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 21180336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 2119a71acad9SOleksij Rempel .self_test = dsa_slave_net_selftest, 212091da11f8SLennert Buytenhek }; 212191da11f8SLennert Buytenhek 2122d538eca8SVladimir Oltean static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = { 2123d538eca8SVladimir Oltean .ieee_setapp = dsa_slave_dcbnl_ieee_setapp, 2124d538eca8SVladimir Oltean .ieee_delapp = dsa_slave_dcbnl_ieee_delapp, 2125d538eca8SVladimir Oltean }; 2126d538eca8SVladimir Oltean 2127716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 2128716efee2SJiri Pirko { 2129716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 2130716efee2SJiri Pirko 213145b987d5SVladimir Oltean return &dp->devlink_port; 2132716efee2SJiri Pirko } 2133716efee2SJiri Pirko 2134c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev, 2135c2ec5f2eSOleksij Rempel struct rtnl_link_stats64 *s) 2136c2ec5f2eSOleksij Rempel { 2137c2ec5f2eSOleksij Rempel struct dsa_port *dp = dsa_slave_to_port(dev); 2138c2ec5f2eSOleksij Rempel struct dsa_switch *ds = dp->ds; 2139c2ec5f2eSOleksij Rempel 2140c2ec5f2eSOleksij Rempel if (ds->ops->get_stats64) 2141c2ec5f2eSOleksij Rempel ds->ops->get_stats64(ds, dp->index, s); 2142c2ec5f2eSOleksij Rempel else 2143c2ec5f2eSOleksij Rempel dev_get_tstats64(dev, s); 2144c2ec5f2eSOleksij Rempel } 2145c2ec5f2eSOleksij Rempel 21460994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx, 21470994d492SFelix Fietkau struct net_device_path *path) 21480994d492SFelix Fietkau { 21490994d492SFelix Fietkau struct dsa_port *dp = dsa_slave_to_port(ctx->dev); 21500994d492SFelix Fietkau struct dsa_port *cpu_dp = dp->cpu_dp; 21510994d492SFelix Fietkau 21520994d492SFelix Fietkau path->dev = ctx->dev; 21530994d492SFelix Fietkau path->type = DEV_PATH_DSA; 21540994d492SFelix Fietkau path->dsa.proto = cpu_dp->tag_ops->proto; 21550994d492SFelix Fietkau path->dsa.port = dp->index; 21560994d492SFelix Fietkau ctx->dev = cpu_dp->master; 21570994d492SFelix Fietkau 21580994d492SFelix Fietkau return 0; 21590994d492SFelix Fietkau } 21600994d492SFelix Fietkau 21613e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 2162d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 2163d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 21643e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 2165d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 2166d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 2167d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 21682bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 2169a7605370SArnd Bergmann .ndo_eth_ioctl = dsa_slave_ioctl, 2170abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 217104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 217204ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 217304ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 217404ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 217504ff53f9SFlorian Fainelli #endif 2176f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 2177c2ec5f2eSOleksij Rempel .ndo_get_stats64 = dsa_slave_get_stats64, 2178061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 2179061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 2180716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 2181bfcb8132SVladimir Oltean .ndo_change_mtu = dsa_slave_change_mtu, 21820994d492SFelix Fietkau .ndo_fill_forward_path = dsa_slave_fill_forward_path, 218398237d43SScott Feldman }; 218498237d43SScott Feldman 2185f37db85dSFlorian Fainelli static struct device_type dsa_type = { 2186f37db85dSFlorian Fainelli .name = "dsa", 2187f37db85dSFlorian Fainelli }; 2188f37db85dSFlorian Fainelli 2189aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 2190aab9c406SFlorian Fainelli { 2191aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 2192aab9c406SFlorian Fainelli 2193765bda93SRussell King if (dp->pl) 2194aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 2195aab9c406SFlorian Fainelli } 2196aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 2197aab9c406SFlorian Fainelli 21985c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config, 2199aab9c406SFlorian Fainelli struct phylink_link_state *state) 2200aab9c406SFlorian Fainelli { 22015c05c1dbSRussell King struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 2202aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 2203aab9c406SFlorian Fainelli 2204aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 2205aab9c406SFlorian Fainelli * not be called if it was not. 2206aab9c406SFlorian Fainelli */ 2207aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 2208ce31b31cSFlorian Fainelli } 2209ce31b31cSFlorian Fainelli 221091da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 2211c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr, 2212c916e8e1SOleksij Rempel u32 flags) 2213c305c165SFlorian Fainelli { 2214d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2215d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 2216c305c165SFlorian Fainelli 22170115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 22180115dcd1SVivien Didelot if (!slave_dev->phydev) { 2219d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 2220c305c165SFlorian Fainelli return -ENODEV; 2221d25b8e74SRussell King } 2222c305c165SFlorian Fainelli 2223c916e8e1SOleksij Rempel slave_dev->phydev->dev_flags |= flags; 2224c916e8e1SOleksij Rempel 2225aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 2226c305c165SFlorian Fainelli } 2227c305c165SFlorian Fainelli 22284fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 22290d8bcdd3SFlorian Fainelli { 2230d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2231d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 2232d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 22336819563eSFlorian Fainelli u32 phy_flags = 0; 22340c65b2b9SAndrew Lunn int ret; 22350d8bcdd3SFlorian Fainelli 223644cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 223744cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 223844cc27e4SIoana Ciornei 22395c05c1dbSRussell King /* The get_fixed_state callback takes precedence over polling the 22405c05c1dbSRussell King * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set 22415c05c1dbSRussell King * this if the switch provides such a callback. 22425c05c1dbSRussell King */ 22435c05c1dbSRussell King if (ds->ops->phylink_fixed_state) { 22445c05c1dbSRussell King dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state; 22455c05c1dbSRussell King dp->pl_config.poll_fixed_state = true; 22465c05c1dbSRussell King } 22475c05c1dbSRussell King 224821bd64bdSRussell King (Oracle) ret = dsa_port_phylink_create(dp); 224921bd64bdSRussell King (Oracle) if (ret) 225021bd64bdSRussell King (Oracle) return ret; 2251aab9c406SFlorian Fainelli 22529d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 2253d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 22546819563eSFlorian Fainelli 2255aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 22566146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 22576146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 22586146dd45SVladimir Oltean * use the switch internal MDIO bus instead 22590d8bcdd3SFlorian Fainelli */ 2260c916e8e1SOleksij Rempel ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags); 2261d25b8e74SRussell King } 22626a52e733SVladimir Oltean if (ret) { 22636a52e733SVladimir Oltean netdev_err(slave_dev, "failed to connect to PHY: %pe\n", 22646a52e733SVladimir Oltean ERR_PTR(ret)); 22656a52e733SVladimir Oltean phylink_destroy(dp->pl); 22660d8bcdd3SFlorian Fainelli } 22679697f1cdSFlorian Fainelli 22686146dd45SVladimir Oltean return ret; 2269b31f65fbSAndrew Lunn } 22700d8bcdd3SFlorian Fainelli 227153da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave) 227253da0ebaSVladimir Oltean { 227353da0ebaSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(slave); 227453da0ebaSVladimir Oltean struct dsa_slave_priv *p = netdev_priv(slave); 227553da0ebaSVladimir Oltean const struct dsa_port *cpu_dp = dp->cpu_dp; 227653da0ebaSVladimir Oltean struct net_device *master = cpu_dp->master; 227758adf9dcSVladimir Oltean const struct dsa_switch *ds = dp->ds; 227853da0ebaSVladimir Oltean 22794e500251SVladimir Oltean slave->needed_headroom = cpu_dp->tag_ops->needed_headroom; 22804e500251SVladimir Oltean slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom; 228153da0ebaSVladimir Oltean /* Try to save one extra realloc later in the TX path (in the master) 228253da0ebaSVladimir Oltean * by also inheriting the master's needed headroom and tailroom. 228353da0ebaSVladimir Oltean * The 8021q driver also does this. 228453da0ebaSVladimir Oltean */ 228553da0ebaSVladimir Oltean slave->needed_headroom += master->needed_headroom; 228653da0ebaSVladimir Oltean slave->needed_tailroom += master->needed_tailroom; 228753da0ebaSVladimir Oltean 228853da0ebaSVladimir Oltean p->xmit = cpu_dp->tag_ops->xmit; 228921cf377aSLino Sanfilippo 229021cf377aSLino Sanfilippo slave->features = master->vlan_features | NETIF_F_HW_TC; 229121cf377aSLino Sanfilippo slave->hw_features |= NETIF_F_HW_TC; 229221cf377aSLino Sanfilippo slave->features |= NETIF_F_LLTX; 229321cf377aSLino Sanfilippo if (slave->needed_tailroom) 229421cf377aSLino Sanfilippo slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST); 229558adf9dcSVladimir Oltean if (ds->needs_standalone_vlan_filtering) 229658adf9dcSVladimir Oltean slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 229753da0ebaSVladimir Oltean } 229853da0ebaSVladimir Oltean 229924462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 230024462549SFlorian Fainelli { 2301aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 230224462549SFlorian Fainelli 2303a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2304a94c689eSFlorian Fainelli return 0; 2305a94c689eSFlorian Fainelli 2306f154be24SFlorian Fainelli netif_device_detach(slave_dev); 2307f154be24SFlorian Fainelli 2308aab9c406SFlorian Fainelli rtnl_lock(); 2309aab9c406SFlorian Fainelli phylink_stop(dp->pl); 2310aab9c406SFlorian Fainelli rtnl_unlock(); 231124462549SFlorian Fainelli 231224462549SFlorian Fainelli return 0; 231324462549SFlorian Fainelli } 231424462549SFlorian Fainelli 231524462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 231624462549SFlorian Fainelli { 2317aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2318aab9c406SFlorian Fainelli 2319a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 2320a94c689eSFlorian Fainelli return 0; 2321a94c689eSFlorian Fainelli 232224462549SFlorian Fainelli netif_device_attach(slave_dev); 232324462549SFlorian Fainelli 2324aab9c406SFlorian Fainelli rtnl_lock(); 2325aab9c406SFlorian Fainelli phylink_start(dp->pl); 2326aab9c406SFlorian Fainelli rtnl_unlock(); 232724462549SFlorian Fainelli 232824462549SFlorian Fainelli return 0; 232924462549SFlorian Fainelli } 233024462549SFlorian Fainelli 2331951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 233291da11f8SLennert Buytenhek { 233324a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 2334f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 23354cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 2336951259aaSVivien Didelot const char *name = port->name; 233791da11f8SLennert Buytenhek struct net_device *slave_dev; 233891da11f8SLennert Buytenhek struct dsa_slave_priv *p; 233991da11f8SLennert Buytenhek int ret; 234091da11f8SLennert Buytenhek 234155199df6SFlorian Fainelli if (!ds->num_tx_queues) 234255199df6SFlorian Fainelli ds->num_tx_queues = 1; 234355199df6SFlorian Fainelli 234455199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 234555199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 234655199df6SFlorian Fainelli ds->num_tx_queues, 1); 234791da11f8SLennert Buytenhek if (slave_dev == NULL) 2348d87d6f44SGuenter Roeck return -ENOMEM; 234991da11f8SLennert Buytenhek 23507ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 2351d538eca8SVladimir Oltean #if IS_ENABLED(CONFIG_DCB) 2352d538eca8SVladimir Oltean slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops; 2353d538eca8SVladimir Oltean #endif 235483216e39SMichael Walle if (!is_zero_ether_addr(port->mac)) 2355e35b8d7dSJakub Kicinski eth_hw_addr_set(slave_dev, port->mac); 2356a2c7023fSXiaofei Shen else 23572fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 23580a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 23595e8a1e03SVladimir Oltean if (dsa_switch_supports_uc_filtering(ds)) 23605e8a1e03SVladimir Oltean slave_dev->priv_flags |= IFF_UNICAST_FLT; 23613e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 2362bfcb8132SVladimir Oltean if (ds->ops->port_max_mtu) 2363bfcb8132SVladimir Oltean slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 2364f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 2365d442ad4aSStephen Hemminger 23664cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 23674cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 236891da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 236991da11f8SLennert Buytenhek 237091da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 23716a900628SHeiner Kallweit slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 23726a900628SHeiner Kallweit if (!slave_dev->tstats) { 23735f6b4e14SFlorian Fainelli free_netdev(slave_dev); 23745f6b4e14SFlorian Fainelli return -ENOMEM; 23755f6b4e14SFlorian Fainelli } 2376e131a563SAlexander Lobakin 2377e131a563SAlexander Lobakin ret = gro_cells_init(&p->gcells, slave_dev); 2378e131a563SAlexander Lobakin if (ret) 2379e131a563SAlexander Lobakin goto out_free; 2380e131a563SAlexander Lobakin 23814cfbf09cSVivien Didelot p->dp = port; 2382f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 2383f8b8b1cdSVivien Didelot port->slave = slave_dev; 238453da0ebaSVladimir Oltean dsa_slave_setup_tagger(slave_dev); 238591da11f8SLennert Buytenhek 238691da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 238791da11f8SLennert Buytenhek 23884fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 23890071f56eSAndrew Lunn if (ret) { 2390c9ebf126SVladimir Oltean netdev_err(slave_dev, 2391c9ebf126SVladimir Oltean "error %d setting up PHY for tree %d, switch %d, port %d\n", 2392c9ebf126SVladimir Oltean ret, ds->dst->index, ds->index, port->index); 2393e131a563SAlexander Lobakin goto out_gcells; 2394e804441cSFlorian Fainelli } 2395e804441cSFlorian Fainelli 23962f1e8ea7SVladimir Oltean rtnl_lock(); 2397e31dbd3bSVladimir Oltean 2398904e112aSVladimir Oltean ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 2399904e112aSVladimir Oltean if (ret && ret != -EOPNOTSUPP) 2400904e112aSVladimir Oltean dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n", 2401904e112aSVladimir Oltean ret, ETH_DATA_LEN, port->index); 2402904e112aSVladimir Oltean 24032f1e8ea7SVladimir Oltean ret = register_netdevice(slave_dev); 2404e804441cSFlorian Fainelli if (ret) { 2405e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 2406e804441cSFlorian Fainelli ret, slave_dev->name); 24072f1e8ea7SVladimir Oltean rtnl_unlock(); 2408e804441cSFlorian Fainelli goto out_phy; 24090071f56eSAndrew Lunn } 24100071f56eSAndrew Lunn 2411d538eca8SVladimir Oltean if (IS_ENABLED(CONFIG_DCB)) { 2412d538eca8SVladimir Oltean ret = dsa_slave_dcbnl_init(slave_dev); 2413d538eca8SVladimir Oltean if (ret) { 2414d538eca8SVladimir Oltean netdev_err(slave_dev, 2415d538eca8SVladimir Oltean "failed to initialize DCB: %pe\n", 2416d538eca8SVladimir Oltean ERR_PTR(ret)); 2417d538eca8SVladimir Oltean rtnl_unlock(); 2418d538eca8SVladimir Oltean goto out_unregister; 2419d538eca8SVladimir Oltean } 2420d538eca8SVladimir Oltean } 2421d538eca8SVladimir Oltean 24222f1e8ea7SVladimir Oltean ret = netdev_upper_dev_link(master, slave_dev, NULL); 24232f1e8ea7SVladimir Oltean 24242f1e8ea7SVladimir Oltean rtnl_unlock(); 24252f1e8ea7SVladimir Oltean 24262f1e8ea7SVladimir Oltean if (ret) 24272f1e8ea7SVladimir Oltean goto out_unregister; 24282f1e8ea7SVladimir Oltean 2429d87d6f44SGuenter Roeck return 0; 2430e804441cSFlorian Fainelli 24312f1e8ea7SVladimir Oltean out_unregister: 24322f1e8ea7SVladimir Oltean unregister_netdev(slave_dev); 2433e804441cSFlorian Fainelli out_phy: 2434aab9c406SFlorian Fainelli rtnl_lock(); 2435aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 2436aab9c406SFlorian Fainelli rtnl_unlock(); 2437aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 2438e131a563SAlexander Lobakin out_gcells: 2439e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 2440e804441cSFlorian Fainelli out_free: 24416a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2442e804441cSFlorian Fainelli free_netdev(slave_dev); 2443f8b8b1cdSVivien Didelot port->slave = NULL; 2444e804441cSFlorian Fainelli return ret; 244591da11f8SLennert Buytenhek } 2446b73adef6SFlorian Fainelli 2447cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 2448cda5c15bSNeil Armstrong { 24492f1e8ea7SVladimir Oltean struct net_device *master = dsa_slave_to_master(slave_dev); 2450d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 2451cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 2452cda5c15bSNeil Armstrong 2453cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 2454aab9c406SFlorian Fainelli rtnl_lock(); 24552f1e8ea7SVladimir Oltean netdev_upper_dev_unlink(master, slave_dev); 24562f1e8ea7SVladimir Oltean unregister_netdevice(slave_dev); 2457aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 2458aab9c406SFlorian Fainelli rtnl_unlock(); 2459881eadabSJohan Hovold 2460aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 2461e131a563SAlexander Lobakin gro_cells_destroy(&p->gcells); 24626a900628SHeiner Kallweit free_percpu(slave_dev->tstats); 2463cda5c15bSNeil Armstrong free_netdev(slave_dev); 2464cda5c15bSNeil Armstrong } 2465cda5c15bSNeil Armstrong 24664d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev) 2467b73adef6SFlorian Fainelli { 2468b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 2469b73adef6SFlorian Fainelli } 2470a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check); 2471b73adef6SFlorian Fainelli 24728e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 24738e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 2474b73adef6SFlorian Fainelli { 2475d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 24762afc526aSVladimir Oltean struct netlink_ext_ack *extack; 24778e92ab3aSVivien Didelot int err = NOTIFY_DONE; 2478b73adef6SFlorian Fainelli 24792afc526aSVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 24802afc526aSVladimir Oltean 24818e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 24828e92ab3aSVivien Didelot if (info->linking) { 24832afc526aSVladimir Oltean err = dsa_port_bridge_join(dp, info->upper_dev, extack); 2484bff33f7eSVladimir Oltean if (!err) 2485bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 248667b5fb5dSVladimir Oltean if (err == -EOPNOTSUPP) { 24871c6e8088SClément Léger if (!extack->_msg) 248867b5fb5dSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 248967b5fb5dSVladimir Oltean "Offloading not supported"); 249067b5fb5dSVladimir Oltean err = 0; 249167b5fb5dSVladimir Oltean } 24928e92ab3aSVivien Didelot err = notifier_from_errno(err); 24938e92ab3aSVivien Didelot } else { 249417d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 24958e92ab3aSVivien Didelot err = NOTIFY_OK; 24968e92ab3aSVivien Didelot } 2497058102a6STobias Waldekranz } else if (netif_is_lag_master(info->upper_dev)) { 2498058102a6STobias Waldekranz if (info->linking) { 2499058102a6STobias Waldekranz err = dsa_port_lag_join(dp, info->upper_dev, 25002afc526aSVladimir Oltean info->upper_info, extack); 2501058102a6STobias Waldekranz if (err == -EOPNOTSUPP) { 2502058102a6STobias Waldekranz NL_SET_ERR_MSG_MOD(info->info.extack, 2503058102a6STobias Waldekranz "Offloading not supported"); 2504058102a6STobias Waldekranz err = 0; 2505058102a6STobias Waldekranz } 2506058102a6STobias Waldekranz err = notifier_from_errno(err); 2507058102a6STobias Waldekranz } else { 2508058102a6STobias Waldekranz dsa_port_lag_leave(dp, info->upper_dev); 2509058102a6STobias Waldekranz err = NOTIFY_OK; 2510058102a6STobias Waldekranz } 251118596f50SGeorge McCollister } else if (is_hsr_master(info->upper_dev)) { 251218596f50SGeorge McCollister if (info->linking) { 251318596f50SGeorge McCollister err = dsa_port_hsr_join(dp, info->upper_dev); 251418596f50SGeorge McCollister if (err == -EOPNOTSUPP) { 251518596f50SGeorge McCollister NL_SET_ERR_MSG_MOD(info->info.extack, 251618596f50SGeorge McCollister "Offloading not supported"); 251718596f50SGeorge McCollister err = 0; 251818596f50SGeorge McCollister } 251918596f50SGeorge McCollister err = notifier_from_errno(err); 252018596f50SGeorge McCollister } else { 252118596f50SGeorge McCollister dsa_port_hsr_leave(dp, info->upper_dev); 252218596f50SGeorge McCollister err = NOTIFY_OK; 252318596f50SGeorge McCollister } 2524058102a6STobias Waldekranz } 2525058102a6STobias Waldekranz 2526058102a6STobias Waldekranz return err; 2527058102a6STobias Waldekranz } 2528058102a6STobias Waldekranz 252974918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev, 253074918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 253174918945SVladimir Oltean { 253274918945SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 253374918945SVladimir Oltean 253474918945SVladimir Oltean if (netif_is_bridge_master(info->upper_dev) && !info->linking) 25354e51bf44SVladimir Oltean dsa_port_pre_bridge_leave(dp, info->upper_dev); 253674918945SVladimir Oltean else if (netif_is_lag_master(info->upper_dev) && !info->linking) 25374e51bf44SVladimir Oltean dsa_port_pre_lag_leave(dp, info->upper_dev); 253874918945SVladimir Oltean /* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be 253974918945SVladimir Oltean * meaningfully enslaved to a bridge yet 254074918945SVladimir Oltean */ 254174918945SVladimir Oltean 25424e51bf44SVladimir Oltean return NOTIFY_DONE; 254374918945SVladimir Oltean } 254474918945SVladimir Oltean 2545058102a6STobias Waldekranz static int 2546058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev, 2547058102a6STobias Waldekranz struct netdev_notifier_changeupper_info *info) 2548058102a6STobias Waldekranz { 2549058102a6STobias Waldekranz struct net_device *lower; 2550058102a6STobias Waldekranz struct list_head *iter; 2551058102a6STobias Waldekranz int err = NOTIFY_DONE; 2552058102a6STobias Waldekranz struct dsa_port *dp; 2553058102a6STobias Waldekranz 2554058102a6STobias Waldekranz netdev_for_each_lower_dev(dev, lower, iter) { 2555058102a6STobias Waldekranz if (!dsa_slave_dev_check(lower)) 2556058102a6STobias Waldekranz continue; 2557058102a6STobias Waldekranz 2558058102a6STobias Waldekranz dp = dsa_slave_to_port(lower); 2559dedd6a00SVladimir Oltean if (!dp->lag) 2560058102a6STobias Waldekranz /* Software LAG */ 2561058102a6STobias Waldekranz continue; 2562058102a6STobias Waldekranz 2563058102a6STobias Waldekranz err = dsa_slave_changeupper(lower, info); 2564058102a6STobias Waldekranz if (notifier_to_errno(err)) 2565058102a6STobias Waldekranz break; 25666debb68aSVivien Didelot } 2567b73adef6SFlorian Fainelli 25688e92ab3aSVivien Didelot return err; 2569b73adef6SFlorian Fainelli } 2570b73adef6SFlorian Fainelli 257174918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls 257274918945SVladimir Oltean * dsa_slave_prechangeupper() 257374918945SVladimir Oltean */ 257474918945SVladimir Oltean static int 257574918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev, 257674918945SVladimir Oltean struct netdev_notifier_changeupper_info *info) 257774918945SVladimir Oltean { 257874918945SVladimir Oltean struct net_device *lower; 257974918945SVladimir Oltean struct list_head *iter; 258074918945SVladimir Oltean int err = NOTIFY_DONE; 258174918945SVladimir Oltean struct dsa_port *dp; 258274918945SVladimir Oltean 258374918945SVladimir Oltean netdev_for_each_lower_dev(dev, lower, iter) { 258474918945SVladimir Oltean if (!dsa_slave_dev_check(lower)) 258574918945SVladimir Oltean continue; 258674918945SVladimir Oltean 258774918945SVladimir Oltean dp = dsa_slave_to_port(lower); 2588dedd6a00SVladimir Oltean if (!dp->lag) 258974918945SVladimir Oltean /* Software LAG */ 259074918945SVladimir Oltean continue; 259174918945SVladimir Oltean 259274918945SVladimir Oltean err = dsa_slave_prechangeupper(lower, info); 259374918945SVladimir Oltean if (notifier_to_errno(err)) 259474918945SVladimir Oltean break; 259574918945SVladimir Oltean } 259674918945SVladimir Oltean 259774918945SVladimir Oltean return err; 259874918945SVladimir Oltean } 259974918945SVladimir Oltean 2600eb46e8daSVladimir Oltean static int 2601eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev, 2602eb46e8daSVladimir Oltean struct netdev_notifier_changeupper_info *info) 2603cc1d5bdaSFlorian Fainelli { 2604cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 260536cbf39bSVladimir Oltean struct net_device *slave, *br; 2606cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 2607cc1d5bdaSFlorian Fainelli 2608cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 2609cc1d5bdaSFlorian Fainelli 2610cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 2611cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2612cc1d5bdaSFlorian Fainelli 2613cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 2614cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 2615cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2616cc1d5bdaSFlorian Fainelli 2617cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 261836cbf39bSVladimir Oltean br = dsa_port_bridge_dev_get(dp); 261936cbf39bSVladimir Oltean if (!br) 2620cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2621cc1d5bdaSFlorian Fainelli 2622cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 262336cbf39bSVladimir Oltean if (br_vlan_enabled(br) && 2624cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 2625cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 2626cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 2627cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 2628cc1d5bdaSFlorian Fainelli } 2629cc1d5bdaSFlorian Fainelli 2630cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 2631cc1d5bdaSFlorian Fainelli } 2632cc1d5bdaSFlorian Fainelli 26332b138406SVladimir Oltean static int 26342b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev, 26352b138406SVladimir Oltean struct netdev_notifier_changeupper_info *info) 26362b138406SVladimir Oltean { 26372b138406SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 263836cbf39bSVladimir Oltean struct net_device *br = dsa_port_bridge_dev_get(dp); 26392b138406SVladimir Oltean struct bridge_vlan_info br_info; 26402b138406SVladimir Oltean struct netlink_ext_ack *extack; 26412b138406SVladimir Oltean int err = NOTIFY_DONE; 26422b138406SVladimir Oltean u16 vid; 26432b138406SVladimir Oltean 2644adb256ebSVladimir Oltean if (!br || !br_vlan_enabled(br)) 26452b138406SVladimir Oltean return NOTIFY_DONE; 26462b138406SVladimir Oltean 26472b138406SVladimir Oltean extack = netdev_notifier_info_to_extack(&info->info); 26482b138406SVladimir Oltean vid = vlan_dev_vlan_id(info->upper_dev); 26492b138406SVladimir Oltean 26502b138406SVladimir Oltean /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 26512b138406SVladimir Oltean * device, respectively the VID is not found, returning 26522b138406SVladimir Oltean * 0 means success, which is a failure for us here. 26532b138406SVladimir Oltean */ 26542b138406SVladimir Oltean err = br_vlan_get_info(br, vid, &br_info); 26552b138406SVladimir Oltean if (err == 0) { 26562b138406SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 26572b138406SVladimir Oltean "This VLAN is already configured by the bridge"); 26582b138406SVladimir Oltean return notifier_from_errno(-EBUSY); 26592b138406SVladimir Oltean } 26602b138406SVladimir Oltean 26612b138406SVladimir Oltean return NOTIFY_DONE; 26622b138406SVladimir Oltean } 26632b138406SVladimir Oltean 26644ede74e7SVladimir Oltean static int 26654ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev, 26664ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info) 2667b73adef6SFlorian Fainelli { 2668e358bef7SVladimir Oltean struct dsa_switch *ds; 2669e358bef7SVladimir Oltean struct dsa_port *dp; 2670e358bef7SVladimir Oltean int err; 26712b138406SVladimir Oltean 267253bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 26734ede74e7SVladimir Oltean return dsa_prevent_bridging_8021q_upper(dev, info); 26742b138406SVladimir Oltean 2675e358bef7SVladimir Oltean dp = dsa_slave_to_port(dev); 2676e358bef7SVladimir Oltean ds = dp->ds; 2677e358bef7SVladimir Oltean 2678e358bef7SVladimir Oltean if (ds->ops->port_prechangeupper) { 2679e358bef7SVladimir Oltean err = ds->ops->port_prechangeupper(ds, dp->index, info); 2680e358bef7SVladimir Oltean if (err) 2681e358bef7SVladimir Oltean return notifier_from_errno(err); 2682e358bef7SVladimir Oltean } 2683e358bef7SVladimir Oltean 26842b138406SVladimir Oltean if (is_vlan_dev(info->upper_dev)) 26854ede74e7SVladimir Oltean return dsa_slave_check_8021q_upper(dev, info); 26864ede74e7SVladimir Oltean 26874ede74e7SVladimir Oltean return NOTIFY_DONE; 26884ede74e7SVladimir Oltean } 26894ede74e7SVladimir Oltean 26904ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb, 26914ede74e7SVladimir Oltean unsigned long event, void *ptr) 26924ede74e7SVladimir Oltean { 26934ede74e7SVladimir Oltean struct net_device *dev = netdev_notifier_info_to_dev(ptr); 26944ede74e7SVladimir Oltean 26954ede74e7SVladimir Oltean switch (event) { 26964ede74e7SVladimir Oltean case NETDEV_PRECHANGEUPPER: { 26974ede74e7SVladimir Oltean struct netdev_notifier_changeupper_info *info = ptr; 26984ede74e7SVladimir Oltean int err; 26994ede74e7SVladimir Oltean 27004ede74e7SVladimir Oltean err = dsa_slave_prechangeupper_sanity_check(dev, info); 27014ede74e7SVladimir Oltean if (err != NOTIFY_DONE) 27024ede74e7SVladimir Oltean return err; 27034ede74e7SVladimir Oltean 270474918945SVladimir Oltean if (dsa_slave_dev_check(dev)) 270574918945SVladimir Oltean return dsa_slave_prechangeupper(dev, ptr); 270674918945SVladimir Oltean 270774918945SVladimir Oltean if (netif_is_lag_master(dev)) 270874918945SVladimir Oltean return dsa_slave_lag_prechangeupper(dev, ptr); 270974918945SVladimir Oltean 271083501299SVladimir Oltean break; 27112b138406SVladimir Oltean } 271283501299SVladimir Oltean case NETDEV_CHANGEUPPER: 2713058102a6STobias Waldekranz if (dsa_slave_dev_check(dev)) 27148e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 2715058102a6STobias Waldekranz 2716058102a6STobias Waldekranz if (netif_is_lag_master(dev)) 2717058102a6STobias Waldekranz return dsa_slave_lag_changeupper(dev, ptr); 2718058102a6STobias Waldekranz 2719058102a6STobias Waldekranz break; 2720058102a6STobias Waldekranz case NETDEV_CHANGELOWERSTATE: { 2721058102a6STobias Waldekranz struct netdev_notifier_changelowerstate_info *info = ptr; 2722058102a6STobias Waldekranz struct dsa_port *dp; 2723058102a6STobias Waldekranz int err; 2724058102a6STobias Waldekranz 2725058102a6STobias Waldekranz if (!dsa_slave_dev_check(dev)) 2726058102a6STobias Waldekranz break; 2727058102a6STobias Waldekranz 2728058102a6STobias Waldekranz dp = dsa_slave_to_port(dev); 2729058102a6STobias Waldekranz 2730058102a6STobias Waldekranz err = dsa_port_lag_change(dp, info->lower_state_info); 2731058102a6STobias Waldekranz return notifier_from_errno(err); 2732058102a6STobias Waldekranz } 2733295ab96fSVladimir Oltean case NETDEV_CHANGE: 2734295ab96fSVladimir Oltean case NETDEV_UP: { 2735295ab96fSVladimir Oltean /* Track state of master port. 2736295ab96fSVladimir Oltean * DSA driver may require the master port (and indirectly 2737295ab96fSVladimir Oltean * the tagger) to be available for some special operation. 2738295ab96fSVladimir Oltean */ 2739295ab96fSVladimir Oltean if (netdev_uses_dsa(dev)) { 2740295ab96fSVladimir Oltean struct dsa_port *cpu_dp = dev->dsa_ptr; 2741295ab96fSVladimir Oltean struct dsa_switch_tree *dst = cpu_dp->ds->dst; 2742295ab96fSVladimir Oltean 2743295ab96fSVladimir Oltean /* Track when the master port is UP */ 2744295ab96fSVladimir Oltean dsa_tree_master_oper_state_change(dst, dev, 2745295ab96fSVladimir Oltean netif_oper_up(dev)); 2746295ab96fSVladimir Oltean 2747295ab96fSVladimir Oltean /* Track when the master port is ready and can accept 2748295ab96fSVladimir Oltean * packet. 2749295ab96fSVladimir Oltean * NETDEV_UP event is not enough to flag a port as ready. 2750295ab96fSVladimir Oltean * We also have to wait for linkwatch_do_dev to dev_activate 2751295ab96fSVladimir Oltean * and emit a NETDEV_CHANGE event. 2752295ab96fSVladimir Oltean * We check if a master port is ready by checking if the dev 2753295ab96fSVladimir Oltean * have a qdisc assigned and is not noop. 2754295ab96fSVladimir Oltean */ 2755295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, 2756295ab96fSVladimir Oltean !qdisc_tx_is_noop(dev)); 2757295ab96fSVladimir Oltean 2758295ab96fSVladimir Oltean return NOTIFY_OK; 2759295ab96fSVladimir Oltean } 2760295ab96fSVladimir Oltean 2761295ab96fSVladimir Oltean return NOTIFY_DONE; 2762295ab96fSVladimir Oltean } 2763c0a8a9c2SVladimir Oltean case NETDEV_GOING_DOWN: { 2764c0a8a9c2SVladimir Oltean struct dsa_port *dp, *cpu_dp; 2765c0a8a9c2SVladimir Oltean struct dsa_switch_tree *dst; 2766c0a8a9c2SVladimir Oltean LIST_HEAD(close_list); 2767c0a8a9c2SVladimir Oltean 2768c0a8a9c2SVladimir Oltean if (!netdev_uses_dsa(dev)) 2769c0a8a9c2SVladimir Oltean return NOTIFY_DONE; 2770c0a8a9c2SVladimir Oltean 2771c0a8a9c2SVladimir Oltean cpu_dp = dev->dsa_ptr; 2772c0a8a9c2SVladimir Oltean dst = cpu_dp->ds->dst; 2773c0a8a9c2SVladimir Oltean 2774295ab96fSVladimir Oltean dsa_tree_master_admin_state_change(dst, dev, false); 2775295ab96fSVladimir Oltean 2776c0a8a9c2SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 2777d0004a02SVladimir Oltean if (!dsa_port_is_user(dp)) 2778c0a8a9c2SVladimir Oltean continue; 2779c0a8a9c2SVladimir Oltean 2780c0a8a9c2SVladimir Oltean list_add(&dp->slave->close_list, &close_list); 2781c0a8a9c2SVladimir Oltean } 2782c0a8a9c2SVladimir Oltean 2783c0a8a9c2SVladimir Oltean dev_close_many(&close_list, true); 2784c0a8a9c2SVladimir Oltean 2785c0a8a9c2SVladimir Oltean return NOTIFY_OK; 2786c0a8a9c2SVladimir Oltean } 2787c0a8a9c2SVladimir Oltean default: 2788c0a8a9c2SVladimir Oltean break; 2789cc1d5bdaSFlorian Fainelli } 2790b73adef6SFlorian Fainelli 2791b73adef6SFlorian Fainelli return NOTIFY_DONE; 2792b73adef6SFlorian Fainelli } 279388e4f0caSVivien Didelot 2794c4bb76a9SVladimir Oltean static void 2795c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work) 2796c4bb76a9SVladimir Oltean { 2797c35b57ceSVladimir Oltean struct switchdev_notifier_fdb_info info = {}; 2798c4bb76a9SVladimir Oltean 2799c4bb76a9SVladimir Oltean info.addr = switchdev_work->addr; 2800c4bb76a9SVladimir Oltean info.vid = switchdev_work->vid; 2801c4bb76a9SVladimir Oltean info.offloaded = true; 2802c4bb76a9SVladimir Oltean call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 280393c79823SVladimir Oltean switchdev_work->orig_dev, &info.info, NULL); 2804c4bb76a9SVladimir Oltean } 2805c9eb3e0fSArkadi Sharshevsky 2806c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 2807c9eb3e0fSArkadi Sharshevsky { 2808c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 2809c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 281068d6d71eSVladimir Oltean const unsigned char *addr = switchdev_work->addr; 2811e35f12e9SVladimir Oltean struct net_device *dev = switchdev_work->dev; 281268d6d71eSVladimir Oltean u16 vid = switchdev_work->vid; 2813e35f12e9SVladimir Oltean struct dsa_switch *ds; 2814c4bb76a9SVladimir Oltean struct dsa_port *dp; 2815c9eb3e0fSArkadi Sharshevsky int err; 2816c9eb3e0fSArkadi Sharshevsky 2817e35f12e9SVladimir Oltean dp = dsa_slave_to_port(dev); 2818e35f12e9SVladimir Oltean ds = dp->ds; 2819c4bb76a9SVladimir Oltean 2820c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 2821c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 28223dc80afcSVladimir Oltean if (switchdev_work->host_addr) 282368d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_add(dp, addr, vid); 2824e212fa7cSVladimir Oltean else if (dp->lag) 282568d6d71eSVladimir Oltean err = dsa_port_lag_fdb_add(dp, addr, vid); 28263dc80afcSVladimir Oltean else 282768d6d71eSVladimir Oltean err = dsa_port_fdb_add(dp, addr, vid); 2828c9eb3e0fSArkadi Sharshevsky if (err) { 2829c4bb76a9SVladimir Oltean dev_err(ds->dev, 2830c4bb76a9SVladimir Oltean "port %d failed to add %pM vid %d to fdb: %d\n", 283168d6d71eSVladimir Oltean dp->index, addr, vid, err); 2832c9eb3e0fSArkadi Sharshevsky break; 2833c9eb3e0fSArkadi Sharshevsky } 2834c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(switchdev_work); 2835c9eb3e0fSArkadi Sharshevsky break; 2836c9eb3e0fSArkadi Sharshevsky 2837c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 28383dc80afcSVladimir Oltean if (switchdev_work->host_addr) 283968d6d71eSVladimir Oltean err = dsa_port_bridge_host_fdb_del(dp, addr, vid); 2840e212fa7cSVladimir Oltean else if (dp->lag) 284168d6d71eSVladimir Oltean err = dsa_port_lag_fdb_del(dp, addr, vid); 28423dc80afcSVladimir Oltean else 284368d6d71eSVladimir Oltean err = dsa_port_fdb_del(dp, addr, vid); 2844c9eb3e0fSArkadi Sharshevsky if (err) { 2845c4bb76a9SVladimir Oltean dev_err(ds->dev, 2846c4bb76a9SVladimir Oltean "port %d failed to delete %pM vid %d from fdb: %d\n", 284768d6d71eSVladimir Oltean dp->index, addr, vid, err); 2848c9eb3e0fSArkadi Sharshevsky } 28492fd18650SVladimir Oltean 2850c9eb3e0fSArkadi Sharshevsky break; 2851c9eb3e0fSArkadi Sharshevsky } 2852c9eb3e0fSArkadi Sharshevsky 2853c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 2854c9eb3e0fSArkadi Sharshevsky } 2855c9eb3e0fSArkadi Sharshevsky 2856b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev, 2857b94dc99cSVladimir Oltean const struct net_device *foreign_dev) 2858d5f19486SVladimir Oltean { 2859b94dc99cSVladimir Oltean const struct dsa_port *dp = dsa_slave_to_port(dev); 2860b94dc99cSVladimir Oltean struct dsa_switch_tree *dst = dp->ds->dst; 2861b94dc99cSVladimir Oltean 2862b94dc99cSVladimir Oltean if (netif_is_bridge_master(foreign_dev)) 2863936db8a2SVladimir Oltean return !dsa_tree_offloads_bridge_dev(dst, foreign_dev); 2864b94dc99cSVladimir Oltean 2865b94dc99cSVladimir Oltean if (netif_is_bridge_port(foreign_dev)) 2866b94dc99cSVladimir Oltean return !dsa_tree_offloads_bridge_port(dst, foreign_dev); 2867b94dc99cSVladimir Oltean 2868b94dc99cSVladimir Oltean /* Everything else is foreign */ 2869b94dc99cSVladimir Oltean return true; 2870d5f19486SVladimir Oltean } 2871d5f19486SVladimir Oltean 2872b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev, 2873716a30a9SVladimir Oltean struct net_device *orig_dev, 2874716a30a9SVladimir Oltean unsigned long event, const void *ctx, 2875716a30a9SVladimir Oltean const struct switchdev_notifier_fdb_info *fdb_info) 2876b94dc99cSVladimir Oltean { 2877b94dc99cSVladimir Oltean struct dsa_switchdev_event_work *switchdev_work; 2878b94dc99cSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 2879b94dc99cSVladimir Oltean bool host_addr = fdb_info->is_local; 2880b94dc99cSVladimir Oltean struct dsa_switch *ds = dp->ds; 2881b94dc99cSVladimir Oltean 2882b94dc99cSVladimir Oltean if (ctx && ctx != dp) 2883b94dc99cSVladimir Oltean return 0; 2884b94dc99cSVladimir Oltean 2885a860352eSTobias Waldekranz if (!dp->bridge) 2886a860352eSTobias Waldekranz return 0; 2887a860352eSTobias Waldekranz 2888e212fa7cSVladimir Oltean if (switchdev_fdb_is_dynamically_learned(fdb_info)) { 2889e212fa7cSVladimir Oltean if (dsa_port_offloads_bridge_port(dp, orig_dev)) 2890b94dc99cSVladimir Oltean return 0; 2891b94dc99cSVladimir Oltean 2892e212fa7cSVladimir Oltean /* FDB entries learned by the software bridge or by foreign 2893e212fa7cSVladimir Oltean * bridge ports should be installed as host addresses only if 2894e212fa7cSVladimir Oltean * the driver requests assisted learning. 2895b94dc99cSVladimir Oltean */ 2896e212fa7cSVladimir Oltean if (!ds->assisted_learning_on_cpu_port) 2897b94dc99cSVladimir Oltean return 0; 2898e212fa7cSVladimir Oltean } 2899b94dc99cSVladimir Oltean 2900b94dc99cSVladimir Oltean /* Also treat FDB entries on foreign interfaces bridged with us as host 2901b94dc99cSVladimir Oltean * addresses. 2902b94dc99cSVladimir Oltean */ 2903b94dc99cSVladimir Oltean if (dsa_foreign_dev_check(dev, orig_dev)) 2904b94dc99cSVladimir Oltean host_addr = true; 2905b94dc99cSVladimir Oltean 2906e212fa7cSVladimir Oltean /* Check early that we're not doing work in vain. 2907e212fa7cSVladimir Oltean * Host addresses on LAG ports still require regular FDB ops, 2908e212fa7cSVladimir Oltean * since the CPU port isn't in a LAG. 2909e212fa7cSVladimir Oltean */ 2910e212fa7cSVladimir Oltean if (dp->lag && !host_addr) { 2911e212fa7cSVladimir Oltean if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del) 2912e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2913e212fa7cSVladimir Oltean } else { 2914e212fa7cSVladimir Oltean if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del) 2915e212fa7cSVladimir Oltean return -EOPNOTSUPP; 2916e212fa7cSVladimir Oltean } 2917e212fa7cSVladimir Oltean 2918b94dc99cSVladimir Oltean switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 2919b94dc99cSVladimir Oltean if (!switchdev_work) 2920b94dc99cSVladimir Oltean return -ENOMEM; 2921b94dc99cSVladimir Oltean 2922b94dc99cSVladimir Oltean netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n", 2923b94dc99cSVladimir Oltean event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting", 2924b94dc99cSVladimir Oltean orig_dev->name, fdb_info->addr, fdb_info->vid, 2925b94dc99cSVladimir Oltean host_addr ? " as host address" : ""); 2926b94dc99cSVladimir Oltean 2927b94dc99cSVladimir Oltean INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work); 2928b94dc99cSVladimir Oltean switchdev_work->event = event; 2929b94dc99cSVladimir Oltean switchdev_work->dev = dev; 293093c79823SVladimir Oltean switchdev_work->orig_dev = orig_dev; 2931b94dc99cSVladimir Oltean 2932b94dc99cSVladimir Oltean ether_addr_copy(switchdev_work->addr, fdb_info->addr); 2933b94dc99cSVladimir Oltean switchdev_work->vid = fdb_info->vid; 2934b94dc99cSVladimir Oltean switchdev_work->host_addr = host_addr; 2935b94dc99cSVladimir Oltean 2936b94dc99cSVladimir Oltean dsa_schedule_work(&switchdev_work->work); 2937b94dc99cSVladimir Oltean 2938d5f19486SVladimir Oltean return 0; 2939d5f19486SVladimir Oltean } 2940d5f19486SVladimir Oltean 2941c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 2942c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 2943c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 2944c9eb3e0fSArkadi Sharshevsky { 2945c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 294679b139f4SVivien Didelot int err; 294779b139f4SVivien Didelot 2948447d290aSVladimir Oltean switch (event) { 2949447d290aSVladimir Oltean case SWITCHDEV_PORT_ATTR_SET: 295079b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 295179b139f4SVivien Didelot dsa_slave_dev_check, 295279b139f4SVivien Didelot dsa_slave_port_attr_set); 295379b139f4SVivien Didelot return notifier_from_errno(err); 2954447d290aSVladimir Oltean case SWITCHDEV_FDB_ADD_TO_DEVICE: 2955447d290aSVladimir Oltean case SWITCHDEV_FDB_DEL_TO_DEVICE: 2956716a30a9SVladimir Oltean err = switchdev_handle_fdb_event_to_device(dev, event, ptr, 2957b94dc99cSVladimir Oltean dsa_slave_dev_check, 2958b94dc99cSVladimir Oltean dsa_foreign_dev_check, 2959ec638740SVladimir Oltean dsa_slave_fdb_event); 2960b94dc99cSVladimir Oltean return notifier_from_errno(err); 2961c9eb3e0fSArkadi Sharshevsky default: 2962c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 2963c9eb3e0fSArkadi Sharshevsky } 2964c9eb3e0fSArkadi Sharshevsky 2965c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 2966c9eb3e0fSArkadi Sharshevsky } 2967c9eb3e0fSArkadi Sharshevsky 29682b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 29692b239f67SPetr Machata unsigned long event, void *ptr) 29702b239f67SPetr Machata { 29712b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 297279b139f4SVivien Didelot int err; 29732b239f67SPetr Machata 29742b239f67SPetr Machata switch (event) { 297579b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 2976164f861bSVladimir Oltean err = switchdev_handle_port_obj_add_foreign(dev, ptr, 297779b139f4SVivien Didelot dsa_slave_dev_check, 2978164f861bSVladimir Oltean dsa_foreign_dev_check, 297979b139f4SVivien Didelot dsa_slave_port_obj_add); 298079b139f4SVivien Didelot return notifier_from_errno(err); 29812b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 2982164f861bSVladimir Oltean err = switchdev_handle_port_obj_del_foreign(dev, ptr, 298379b139f4SVivien Didelot dsa_slave_dev_check, 2984164f861bSVladimir Oltean dsa_foreign_dev_check, 298579b139f4SVivien Didelot dsa_slave_port_obj_del); 298679b139f4SVivien Didelot return notifier_from_errno(err); 29879ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 298879b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 298979b139f4SVivien Didelot dsa_slave_dev_check, 299079b139f4SVivien Didelot dsa_slave_port_attr_set); 299179b139f4SVivien Didelot return notifier_from_errno(err); 29922b239f67SPetr Machata } 29932b239f67SPetr Machata 29942b239f67SPetr Machata return NOTIFY_DONE; 29952b239f67SPetr Machata } 29962b239f67SPetr Machata 299788e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 299888e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 299988e4f0caSVivien Didelot }; 300088e4f0caSVivien Didelot 3001010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = { 3002c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 3003c9eb3e0fSArkadi Sharshevsky }; 3004c9eb3e0fSArkadi Sharshevsky 3005010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = { 30062b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 30072b239f67SPetr Machata }; 30082b239f67SPetr Machata 300988e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 301088e4f0caSVivien Didelot { 30112b239f67SPetr Machata struct notifier_block *nb; 3012c9eb3e0fSArkadi Sharshevsky int err; 3013c9eb3e0fSArkadi Sharshevsky 3014c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 3015c9eb3e0fSArkadi Sharshevsky if (err) 3016c9eb3e0fSArkadi Sharshevsky return err; 3017c9eb3e0fSArkadi Sharshevsky 3018c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 3019c9eb3e0fSArkadi Sharshevsky if (err) 3020c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 3021c9eb3e0fSArkadi Sharshevsky 30222b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 30232b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 30242b239f67SPetr Machata if (err) 30252b239f67SPetr Machata goto err_switchdev_blocking_nb; 30262b239f67SPetr Machata 3027c9eb3e0fSArkadi Sharshevsky return 0; 3028c9eb3e0fSArkadi Sharshevsky 30292b239f67SPetr Machata err_switchdev_blocking_nb: 30302b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3031c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 3032c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 3033c9eb3e0fSArkadi Sharshevsky return err; 303488e4f0caSVivien Didelot } 303588e4f0caSVivien Didelot 303688e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 303788e4f0caSVivien Didelot { 30382b239f67SPetr Machata struct notifier_block *nb; 303988e4f0caSVivien Didelot int err; 304088e4f0caSVivien Didelot 30412b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 30422b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 30432b239f67SPetr Machata if (err) 30442b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 30452b239f67SPetr Machata 3046c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 3047c9eb3e0fSArkadi Sharshevsky if (err) 3048c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 3049c9eb3e0fSArkadi Sharshevsky 305088e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 305188e4f0caSVivien Didelot if (err) 305288e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 305388e4f0caSVivien Didelot } 3054