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> 18f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 19b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2004ff53f9SFlorian Fainelli #include <linux/netpoll.h> 2190af1059SBrandon Streiff #include <linux/ptp_classify.h> 22ea5dd34bSVivien Didelot 2391da11f8SLennert Buytenhek #include "dsa_priv.h" 2491da11f8SLennert Buytenhek 25f3b78049SVivien Didelot static bool dsa_slave_dev_check(const struct net_device *dev); 26f50f2127SFlorian Fainelli 2791da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2891da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2991da11f8SLennert Buytenhek { 3091da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3191da11f8SLennert Buytenhek 320d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 339d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 3491da11f8SLennert Buytenhek 3591da11f8SLennert Buytenhek return 0xffff; 3691da11f8SLennert Buytenhek } 3791da11f8SLennert Buytenhek 3891da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3991da11f8SLennert Buytenhek { 4091da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 4191da11f8SLennert Buytenhek 420d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 439d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 4491da11f8SLennert Buytenhek 4591da11f8SLennert Buytenhek return 0; 4691da11f8SLennert Buytenhek } 4791da11f8SLennert Buytenhek 4891da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4991da11f8SLennert Buytenhek { 5091da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 5191da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 5291da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 5391da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 540b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 5549463b7fSVivien Didelot ds->dst->index, ds->index); 56c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 5724df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5891da11f8SLennert Buytenhek } 5991da11f8SLennert Buytenhek 6091da11f8SLennert Buytenhek 6191da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 62abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 63c0840801SLennert Buytenhek { 64d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 65c0840801SLennert Buytenhek } 66c0840801SLennert Buytenhek 6791da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 6891da11f8SLennert Buytenhek { 69d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 70d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 71df02c6ffSLennert Buytenhek int err; 72df02c6ffSLennert Buytenhek 73df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 74df02c6ffSLennert Buytenhek return -ENETDOWN; 75df02c6ffSLennert Buytenhek 768feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 77a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 78df02c6ffSLennert Buytenhek if (err < 0) 79df02c6ffSLennert Buytenhek goto out; 80df02c6ffSLennert Buytenhek } 81df02c6ffSLennert Buytenhek 82df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 83df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 84df02c6ffSLennert Buytenhek if (err < 0) 85df02c6ffSLennert Buytenhek goto del_unicast; 86df02c6ffSLennert Buytenhek } 87df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 88df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 89df02c6ffSLennert Buytenhek if (err < 0) 90df02c6ffSLennert Buytenhek goto clear_allmulti; 91df02c6ffSLennert Buytenhek } 92df02c6ffSLennert Buytenhek 930115dcd1SVivien Didelot err = dsa_port_enable(dp, dev->phydev); 94b2f2af21SFlorian Fainelli if (err) 95b2f2af21SFlorian Fainelli goto clear_promisc; 96b73adef6SFlorian Fainelli 97aab9c406SFlorian Fainelli phylink_start(dp->pl); 98f7f1de51SFlorian Fainelli 9991da11f8SLennert Buytenhek return 0; 100df02c6ffSLennert Buytenhek 101b2f2af21SFlorian Fainelli clear_promisc: 102b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 1034fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 104df02c6ffSLennert Buytenhek clear_allmulti: 105df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 106df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 107df02c6ffSLennert Buytenhek del_unicast: 1088feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 109a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 110df02c6ffSLennert Buytenhek out: 111df02c6ffSLennert Buytenhek return err; 11291da11f8SLennert Buytenhek } 11391da11f8SLennert Buytenhek 11491da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 11591da11f8SLennert Buytenhek { 116d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 117d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 118df02c6ffSLennert Buytenhek 11997a69a0dSVladimir Oltean cancel_work_sync(&dp->xmit_work); 12097a69a0dSVladimir Oltean skb_queue_purge(&dp->xmit_queue); 12197a69a0dSVladimir Oltean 122aab9c406SFlorian Fainelli phylink_stop(dp->pl); 123f7f1de51SFlorian Fainelli 12475104db0SAndrew Lunn dsa_port_disable(dp); 1256457edfeSVivien Didelot 126df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 127a748ee24SJiri Pirko dev_uc_unsync(master, dev); 128df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 129df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 130df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 131df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 132df02c6ffSLennert Buytenhek 1338feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 134a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 135df02c6ffSLennert Buytenhek 13691da11f8SLennert Buytenhek return 0; 13791da11f8SLennert Buytenhek } 13891da11f8SLennert Buytenhek 13991da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 14091da11f8SLennert Buytenhek { 141d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 14217ab4f61SRundong Ge if (dev->flags & IFF_UP) { 14391da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 14417ab4f61SRundong Ge dev_set_allmulti(master, 14517ab4f61SRundong Ge dev->flags & IFF_ALLMULTI ? 1 : -1); 14691da11f8SLennert Buytenhek if (change & IFF_PROMISC) 14717ab4f61SRundong Ge dev_set_promiscuity(master, 14817ab4f61SRundong Ge dev->flags & IFF_PROMISC ? 1 : -1); 14917ab4f61SRundong Ge } 15091da11f8SLennert Buytenhek } 15191da11f8SLennert Buytenhek 15291da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 15391da11f8SLennert Buytenhek { 154d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 15591da11f8SLennert Buytenhek 15691da11f8SLennert Buytenhek dev_mc_sync(master, dev); 157a748ee24SJiri Pirko dev_uc_sync(master, dev); 15891da11f8SLennert Buytenhek } 15991da11f8SLennert Buytenhek 160df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 16191da11f8SLennert Buytenhek { 162d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 163df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 164df02c6ffSLennert Buytenhek int err; 165df02c6ffSLennert Buytenhek 166df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 167df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 168df02c6ffSLennert Buytenhek 169df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 170df02c6ffSLennert Buytenhek goto out; 171df02c6ffSLennert Buytenhek 1728feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 173a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 174df02c6ffSLennert Buytenhek if (err < 0) 175df02c6ffSLennert Buytenhek return err; 176df02c6ffSLennert Buytenhek } 177df02c6ffSLennert Buytenhek 1788feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 179a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 180df02c6ffSLennert Buytenhek 181df02c6ffSLennert Buytenhek out: 182d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 18391da11f8SLennert Buytenhek 18491da11f8SLennert Buytenhek return 0; 18591da11f8SLennert Buytenhek } 18691da11f8SLennert Buytenhek 1872bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx { 1882bedde1aSArkadi Sharshevsky struct net_device *dev; 1892bedde1aSArkadi Sharshevsky struct sk_buff *skb; 1902bedde1aSArkadi Sharshevsky struct netlink_callback *cb; 1912bedde1aSArkadi Sharshevsky int idx; 1922bedde1aSArkadi Sharshevsky }; 1932bedde1aSArkadi Sharshevsky 1942bedde1aSArkadi Sharshevsky static int 1952bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1962bedde1aSArkadi Sharshevsky bool is_static, void *data) 1972bedde1aSArkadi Sharshevsky { 1982bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx *dump = data; 1992bedde1aSArkadi Sharshevsky u32 portid = NETLINK_CB(dump->cb->skb).portid; 2002bedde1aSArkadi Sharshevsky u32 seq = dump->cb->nlh->nlmsg_seq; 2012bedde1aSArkadi Sharshevsky struct nlmsghdr *nlh; 2022bedde1aSArkadi Sharshevsky struct ndmsg *ndm; 2032bedde1aSArkadi Sharshevsky 2042bedde1aSArkadi Sharshevsky if (dump->idx < dump->cb->args[2]) 2052bedde1aSArkadi Sharshevsky goto skip; 2062bedde1aSArkadi Sharshevsky 2072bedde1aSArkadi Sharshevsky nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 2082bedde1aSArkadi Sharshevsky sizeof(*ndm), NLM_F_MULTI); 2092bedde1aSArkadi Sharshevsky if (!nlh) 2102bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2112bedde1aSArkadi Sharshevsky 2122bedde1aSArkadi Sharshevsky ndm = nlmsg_data(nlh); 2132bedde1aSArkadi Sharshevsky ndm->ndm_family = AF_BRIDGE; 2142bedde1aSArkadi Sharshevsky ndm->ndm_pad1 = 0; 2152bedde1aSArkadi Sharshevsky ndm->ndm_pad2 = 0; 2162bedde1aSArkadi Sharshevsky ndm->ndm_flags = NTF_SELF; 2172bedde1aSArkadi Sharshevsky ndm->ndm_type = 0; 2182bedde1aSArkadi Sharshevsky ndm->ndm_ifindex = dump->dev->ifindex; 2192bedde1aSArkadi Sharshevsky ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 2202bedde1aSArkadi Sharshevsky 2212bedde1aSArkadi Sharshevsky if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 2222bedde1aSArkadi Sharshevsky goto nla_put_failure; 2232bedde1aSArkadi Sharshevsky 2242bedde1aSArkadi Sharshevsky if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 2252bedde1aSArkadi Sharshevsky goto nla_put_failure; 2262bedde1aSArkadi Sharshevsky 2272bedde1aSArkadi Sharshevsky nlmsg_end(dump->skb, nlh); 2282bedde1aSArkadi Sharshevsky 2292bedde1aSArkadi Sharshevsky skip: 2302bedde1aSArkadi Sharshevsky dump->idx++; 2312bedde1aSArkadi Sharshevsky return 0; 2322bedde1aSArkadi Sharshevsky 2332bedde1aSArkadi Sharshevsky nla_put_failure: 2342bedde1aSArkadi Sharshevsky nlmsg_cancel(dump->skb, nlh); 2352bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2362bedde1aSArkadi Sharshevsky } 2372bedde1aSArkadi Sharshevsky 2382bedde1aSArkadi Sharshevsky static int 2392bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 2402bedde1aSArkadi Sharshevsky struct net_device *dev, struct net_device *filter_dev, 2412bedde1aSArkadi Sharshevsky int *idx) 2422bedde1aSArkadi Sharshevsky { 243d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 2442bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx dump = { 2452bedde1aSArkadi Sharshevsky .dev = dev, 2462bedde1aSArkadi Sharshevsky .skb = skb, 2472bedde1aSArkadi Sharshevsky .cb = cb, 2482bedde1aSArkadi Sharshevsky .idx = *idx, 2492bedde1aSArkadi Sharshevsky }; 2502bedde1aSArkadi Sharshevsky int err; 2512bedde1aSArkadi Sharshevsky 252de40fc5dSVivien Didelot err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 2532bedde1aSArkadi Sharshevsky *idx = dump.idx; 254de40fc5dSVivien Didelot 2552bedde1aSArkadi Sharshevsky return err; 2562bedde1aSArkadi Sharshevsky } 2572bedde1aSArkadi Sharshevsky 25891da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 25991da11f8SLennert Buytenhek { 2600336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 2610336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 2620336369dSBrandon Streiff int port = p->dp->index; 2630336369dSBrandon Streiff 2640336369dSBrandon Streiff /* Pass through to switch driver if it supports timestamping */ 2650336369dSBrandon Streiff switch (cmd) { 2660336369dSBrandon Streiff case SIOCGHWTSTAMP: 2670336369dSBrandon Streiff if (ds->ops->port_hwtstamp_get) 2680336369dSBrandon Streiff return ds->ops->port_hwtstamp_get(ds, port, ifr); 2690336369dSBrandon Streiff break; 2700336369dSBrandon Streiff case SIOCSHWTSTAMP: 2710336369dSBrandon Streiff if (ds->ops->port_hwtstamp_set) 2720336369dSBrandon Streiff return ds->ops->port_hwtstamp_set(ds, port, ifr); 2730336369dSBrandon Streiff break; 2740336369dSBrandon Streiff } 2750336369dSBrandon Streiff 276aab9c406SFlorian Fainelli return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 27791da11f8SLennert Buytenhek } 27891da11f8SLennert Buytenhek 27935636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 280f7fadf30SJiri Pirko const struct switchdev_attr *attr, 2817ea6eb3fSJiri Pirko struct switchdev_trans *trans) 28235636062SScott Feldman { 283d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 284b8d866acSVivien Didelot int ret; 28535636062SScott Feldman 28635636062SScott Feldman switch (attr->id) { 2871f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 288fd364541SVivien Didelot ret = dsa_port_set_state(dp, attr->u.stp_state, trans); 28935636062SScott Feldman break; 290fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 291c02c4175SVivien Didelot ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 292c02c4175SVivien Didelot trans); 293fb2dabadSVivien Didelot break; 29434a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 295072bb190SVivien Didelot ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); 29634a79f63SVivien Didelot break; 297ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 298ea87005aSFlorian Fainelli ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 299ea87005aSFlorian Fainelli trans); 300ea87005aSFlorian Fainelli break; 30157652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 30257652796SRussell King ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans); 30357652796SRussell King break; 30408cc83ccSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER: 30508cc83ccSVivien Didelot ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans); 30608cc83ccSVivien Didelot break; 30735636062SScott Feldman default: 30835636062SScott Feldman ret = -EOPNOTSUPP; 30935636062SScott Feldman break; 31035636062SScott Feldman } 31135636062SScott Feldman 31235636062SScott Feldman return ret; 31335636062SScott Feldman } 31435636062SScott Feldman 315ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 316648b4a99SJiri Pirko const struct switchdev_obj *obj, 31779b139f4SVivien Didelot struct switchdev_trans *trans, 31879b139f4SVivien Didelot struct netlink_ext_ack *extack) 319ba14d9ebSVivien Didelot { 320d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 321ba14d9ebSVivien Didelot int err; 322ba14d9ebSVivien Didelot 323ba14d9ebSVivien Didelot /* For the prepare phase, ensure the full set of changes is feasable in 324ba14d9ebSVivien Didelot * one go in order to signal a failure properly. If an operation is not 325ba14d9ebSVivien Didelot * supported, return -EOPNOTSUPP. 326ba14d9ebSVivien Didelot */ 327ba14d9ebSVivien Didelot 3289e8f4a54SJiri Pirko switch (obj->id) { 3298df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 33079b139f4SVivien Didelot if (obj->orig_dev != dev) 33179b139f4SVivien Didelot return -EOPNOTSUPP; 332bcebb976SVivien Didelot err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); 3338df30255SVivien Didelot break; 3345f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3355f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3365f4dbc50SAndrew Lunn * but on the CPU port. 3375f4dbc50SAndrew Lunn */ 3385f4dbc50SAndrew Lunn err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj), 3395f4dbc50SAndrew Lunn trans); 3405f4dbc50SAndrew Lunn break; 34157d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 34279b139f4SVivien Didelot if (obj->orig_dev != dev) 34379b139f4SVivien Didelot return -EOPNOTSUPP; 34401676d12SVivien Didelot err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), 345648b4a99SJiri Pirko trans); 34611149536SVivien Didelot break; 347ba14d9ebSVivien Didelot default: 348ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 349ba14d9ebSVivien Didelot break; 350ba14d9ebSVivien Didelot } 351ba14d9ebSVivien Didelot 352ba14d9ebSVivien Didelot return err; 353ba14d9ebSVivien Didelot } 354ba14d9ebSVivien Didelot 355ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 356648b4a99SJiri Pirko const struct switchdev_obj *obj) 357ba14d9ebSVivien Didelot { 358d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 359ba14d9ebSVivien Didelot int err; 360ba14d9ebSVivien Didelot 3619e8f4a54SJiri Pirko switch (obj->id) { 3628df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 36379b139f4SVivien Didelot if (obj->orig_dev != dev) 36479b139f4SVivien Didelot return -EOPNOTSUPP; 365bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3668df30255SVivien Didelot break; 3675f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3685f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3695f4dbc50SAndrew Lunn * but on the CPU port. 3705f4dbc50SAndrew Lunn */ 3715f4dbc50SAndrew Lunn err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3725f4dbc50SAndrew Lunn break; 37357d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 37479b139f4SVivien Didelot if (obj->orig_dev != dev) 37579b139f4SVivien Didelot return -EOPNOTSUPP; 37601676d12SVivien Didelot err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); 37711149536SVivien Didelot break; 378ba14d9ebSVivien Didelot default: 379ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 380ba14d9ebSVivien Didelot break; 381ba14d9ebSVivien Didelot } 382ba14d9ebSVivien Didelot 383ba14d9ebSVivien Didelot return err; 384ba14d9ebSVivien Didelot } 385ba14d9ebSVivien Didelot 386929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev, 387929d6c14SFlorian Fainelli struct netdev_phys_item_id *ppid) 388b73adef6SFlorian Fainelli { 389d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 390d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 391a42c8e33SAndrew Lunn struct dsa_switch_tree *dst = ds->dst; 392b73adef6SFlorian Fainelli 39315b04aceSJiri Pirko /* For non-legacy ports, devlink is used and it takes 39415b04aceSJiri Pirko * care of the name generation. This ndo implementation 39515b04aceSJiri Pirko * should be removed with legacy support. 39615b04aceSJiri Pirko */ 39715b04aceSJiri Pirko if (dp->ds->devlink) 39815b04aceSJiri Pirko return -EOPNOTSUPP; 39915b04aceSJiri Pirko 400929d6c14SFlorian Fainelli ppid->id_len = sizeof(dst->index); 401929d6c14SFlorian Fainelli memcpy(&ppid->id, &dst->index, ppid->id_len); 402929d6c14SFlorian Fainelli 403929d6c14SFlorian Fainelli return 0; 404929d6c14SFlorian Fainelli } 405929d6c14SFlorian Fainelli 4064fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 40704ff53f9SFlorian Fainelli struct sk_buff *skb) 40804ff53f9SFlorian Fainelli { 40904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 4104fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 4114fa7b718SVivien Didelot 41204ff53f9SFlorian Fainelli if (p->netpoll) 41304ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 41404ff53f9SFlorian Fainelli #else 41504ff53f9SFlorian Fainelli BUG(); 41604ff53f9SFlorian Fainelli #endif 41704ff53f9SFlorian Fainelli return NETDEV_TX_OK; 41804ff53f9SFlorian Fainelli } 41904ff53f9SFlorian Fainelli 42090af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 42190af1059SBrandon Streiff struct sk_buff *skb) 42290af1059SBrandon Streiff { 42390af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 42490af1059SBrandon Streiff struct sk_buff *clone; 42590af1059SBrandon Streiff unsigned int type; 42690af1059SBrandon Streiff 42790af1059SBrandon Streiff type = ptp_classify_raw(skb); 42890af1059SBrandon Streiff if (type == PTP_CLASS_NONE) 42990af1059SBrandon Streiff return; 43090af1059SBrandon Streiff 43190af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 43290af1059SBrandon Streiff return; 43390af1059SBrandon Streiff 43490af1059SBrandon Streiff clone = skb_clone_sk(skb); 43590af1059SBrandon Streiff if (!clone) 43690af1059SBrandon Streiff return; 43790af1059SBrandon Streiff 438146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = clone; 439146d442cSVladimir Oltean 44090af1059SBrandon Streiff if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) 44190af1059SBrandon Streiff return; 44290af1059SBrandon Streiff 44390af1059SBrandon Streiff kfree_skb(clone); 44490af1059SBrandon Streiff } 44590af1059SBrandon Streiff 44697a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 44797a69a0dSVladimir Oltean { 44897a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 44997a69a0dSVladimir Oltean * tag to be successfully transmitted 45097a69a0dSVladimir Oltean */ 45197a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 45297a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 45397a69a0dSVladimir Oltean 45497a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 45597a69a0dSVladimir Oltean * do not modify its EtherType 45697a69a0dSVladimir Oltean */ 45797a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 45897a69a0dSVladimir Oltean dev_queue_xmit(skb); 45997a69a0dSVladimir Oltean 46097a69a0dSVladimir Oltean return NETDEV_TX_OK; 46197a69a0dSVladimir Oltean } 46297a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 46397a69a0dSVladimir Oltean 4643e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 4653e8a72d1SFlorian Fainelli { 4663e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 4675f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 4684ed70ce9SFlorian Fainelli struct sk_buff *nskb; 4693e8a72d1SFlorian Fainelli 4705f6b4e14SFlorian Fainelli s = this_cpu_ptr(p->stats64); 4715f6b4e14SFlorian Fainelli u64_stats_update_begin(&s->syncp); 4725f6b4e14SFlorian Fainelli s->tx_packets++; 4735f6b4e14SFlorian Fainelli s->tx_bytes += skb->len; 4745f6b4e14SFlorian Fainelli u64_stats_update_end(&s->syncp); 4753e8a72d1SFlorian Fainelli 47687671375SVladimir Oltean DSA_SKB_CB(skb)->deferred_xmit = false; 477146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = NULL; 47887671375SVladimir Oltean 47990af1059SBrandon Streiff /* Identify PTP protocol packets, clone them, and pass them to the 48090af1059SBrandon Streiff * switch driver 48190af1059SBrandon Streiff */ 48290af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 48390af1059SBrandon Streiff 484fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 485fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 486fe47d563SVivien Didelot */ 4874ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 488fe47d563SVivien Didelot if (!nskb) { 48997a69a0dSVladimir Oltean if (!DSA_SKB_CB(skb)->deferred_xmit) 490fe47d563SVivien Didelot kfree_skb(skb); 4914ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 492fe47d563SVivien Didelot } 4935aed85ceSFlorian Fainelli 49497a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 49597a69a0dSVladimir Oltean } 49604ff53f9SFlorian Fainelli 49797a69a0dSVladimir Oltean void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev) 49897a69a0dSVladimir Oltean { 49997a69a0dSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 5005aed85ceSFlorian Fainelli 50197a69a0dSVladimir Oltean DSA_SKB_CB(skb)->deferred_xmit = true; 50297a69a0dSVladimir Oltean 50397a69a0dSVladimir Oltean skb_queue_tail(&dp->xmit_queue, skb); 50497a69a0dSVladimir Oltean schedule_work(&dp->xmit_work); 50597a69a0dSVladimir Oltean return NULL; 50697a69a0dSVladimir Oltean } 50797a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_defer_xmit); 50897a69a0dSVladimir Oltean 50997a69a0dSVladimir Oltean static void dsa_port_xmit_work(struct work_struct *work) 51097a69a0dSVladimir Oltean { 51197a69a0dSVladimir Oltean struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work); 51297a69a0dSVladimir Oltean struct dsa_switch *ds = dp->ds; 51397a69a0dSVladimir Oltean struct sk_buff *skb; 51497a69a0dSVladimir Oltean 51597a69a0dSVladimir Oltean if (unlikely(!ds->ops->port_deferred_xmit)) 51697a69a0dSVladimir Oltean return; 51797a69a0dSVladimir Oltean 51897a69a0dSVladimir Oltean while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL) 51997a69a0dSVladimir Oltean ds->ops->port_deferred_xmit(ds, dp->index, skb); 5205aed85ceSFlorian Fainelli } 5215aed85ceSFlorian Fainelli 52291da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 52391da11f8SLennert Buytenhek 52491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 52591da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 52691da11f8SLennert Buytenhek { 5277826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 5287826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 5297826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 53091da11f8SLennert Buytenhek } 53191da11f8SLennert Buytenhek 5323d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 5333d762a0fSGuenter Roeck { 534d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 535d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5363d762a0fSGuenter Roeck 5379d490b4eSVivien Didelot if (ds->ops->get_regs_len) 538d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 5393d762a0fSGuenter Roeck 5403d762a0fSGuenter Roeck return -EOPNOTSUPP; 5413d762a0fSGuenter Roeck } 5423d762a0fSGuenter Roeck 5433d762a0fSGuenter Roeck static void 5443d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 5453d762a0fSGuenter Roeck { 546d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 547d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5483d762a0fSGuenter Roeck 5499d490b4eSVivien Didelot if (ds->ops->get_regs) 550d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 5513d762a0fSGuenter Roeck } 5523d762a0fSGuenter Roeck 553aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 554aab9c406SFlorian Fainelli { 555aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 556aab9c406SFlorian Fainelli 557aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 558aab9c406SFlorian Fainelli } 559aab9c406SFlorian Fainelli 5606793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 5616793abb4SGuenter Roeck { 562d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 563d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5646793abb4SGuenter Roeck 5650e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 566ff04955cSAndrew Lunn return ds->cd->eeprom_len; 5676793abb4SGuenter Roeck 5689d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 5699d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 5706793abb4SGuenter Roeck 5716793abb4SGuenter Roeck return 0; 5726793abb4SGuenter Roeck } 5736793abb4SGuenter Roeck 5746793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 5756793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5766793abb4SGuenter Roeck { 577d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 578d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5796793abb4SGuenter Roeck 5809d490b4eSVivien Didelot if (ds->ops->get_eeprom) 5819d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 5826793abb4SGuenter Roeck 5836793abb4SGuenter Roeck return -EOPNOTSUPP; 5846793abb4SGuenter Roeck } 5856793abb4SGuenter Roeck 5866793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 5876793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5886793abb4SGuenter Roeck { 589d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 590d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5916793abb4SGuenter Roeck 5929d490b4eSVivien Didelot if (ds->ops->set_eeprom) 5939d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 5946793abb4SGuenter Roeck 5956793abb4SGuenter Roeck return -EOPNOTSUPP; 5966793abb4SGuenter Roeck } 5976793abb4SGuenter Roeck 59891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 59991da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 60091da11f8SLennert Buytenhek { 601d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 602d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 60391da11f8SLennert Buytenhek 60491da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 60591da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 60691da11f8SLennert Buytenhek 60791da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 60891da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 60991da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 61091da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 6119d490b4eSVivien Didelot if (ds->ops->get_strings) 61289f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 61389f09048SFlorian Fainelli data + 4 * len); 61491da11f8SLennert Buytenhek } 61591da11f8SLennert Buytenhek } 61691da11f8SLennert Buytenhek 61791da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 61891da11f8SLennert Buytenhek struct ethtool_stats *stats, 61991da11f8SLennert Buytenhek uint64_t *data) 62091da11f8SLennert Buytenhek { 621d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 62291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 623d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6245f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 625f613ed66SFlorian Fainelli unsigned int start; 6265f6b4e14SFlorian Fainelli int i; 62791da11f8SLennert Buytenhek 6285f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 6295f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 6305f6b4e14SFlorian Fainelli 6315f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 632f613ed66SFlorian Fainelli do { 6335f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 6345f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 6355f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 6365f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 6375f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 6385f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 6395f6b4e14SFlorian Fainelli data[0] += tx_packets; 6405f6b4e14SFlorian Fainelli data[1] += tx_bytes; 6415f6b4e14SFlorian Fainelli data[2] += rx_packets; 6425f6b4e14SFlorian Fainelli data[3] += rx_bytes; 6435f6b4e14SFlorian Fainelli } 6449d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 645d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 64691da11f8SLennert Buytenhek } 64791da11f8SLennert Buytenhek 64891da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 64991da11f8SLennert Buytenhek { 650d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 651d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 65291da11f8SLennert Buytenhek 65391da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 65491da11f8SLennert Buytenhek int count; 65591da11f8SLennert Buytenhek 65691da11f8SLennert Buytenhek count = 4; 6579d490b4eSVivien Didelot if (ds->ops->get_sset_count) 65889f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, dp->index, sset); 65991da11f8SLennert Buytenhek 66091da11f8SLennert Buytenhek return count; 66191da11f8SLennert Buytenhek } 66291da11f8SLennert Buytenhek 66391da11f8SLennert Buytenhek return -EOPNOTSUPP; 66491da11f8SLennert Buytenhek } 66591da11f8SLennert Buytenhek 66619e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 66719e57c4eSFlorian Fainelli { 668d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 669d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 67019e57c4eSFlorian Fainelli 671aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 672aab9c406SFlorian Fainelli 6739d490b4eSVivien Didelot if (ds->ops->get_wol) 674d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 67519e57c4eSFlorian Fainelli } 67619e57c4eSFlorian Fainelli 67719e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 67819e57c4eSFlorian Fainelli { 679d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 680d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 68119e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 68219e57c4eSFlorian Fainelli 683aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 684aab9c406SFlorian Fainelli 6859d490b4eSVivien Didelot if (ds->ops->set_wol) 686d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 68719e57c4eSFlorian Fainelli 68819e57c4eSFlorian Fainelli return ret; 68919e57c4eSFlorian Fainelli } 69019e57c4eSFlorian Fainelli 6917905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 6927905288fSFlorian Fainelli { 693d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 694d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6957905288fSFlorian Fainelli int ret; 6967905288fSFlorian Fainelli 6977b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 69800670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 6997b9cc738SVivien Didelot return -ENODEV; 7007b9cc738SVivien Didelot 70108f50061SVivien Didelot if (!ds->ops->set_mac_eee) 7027905288fSFlorian Fainelli return -EOPNOTSUPP; 7037905288fSFlorian Fainelli 704d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 7057905288fSFlorian Fainelli if (ret) 7067905288fSFlorian Fainelli return ret; 7077905288fSFlorian Fainelli 708aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 7097905288fSFlorian Fainelli } 7107905288fSFlorian Fainelli 7117905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 7127905288fSFlorian Fainelli { 713d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 714d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7157905288fSFlorian Fainelli int ret; 7167905288fSFlorian Fainelli 7177b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 71800670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 7197b9cc738SVivien Didelot return -ENODEV; 7207b9cc738SVivien Didelot 72108f50061SVivien Didelot if (!ds->ops->get_mac_eee) 7227905288fSFlorian Fainelli return -EOPNOTSUPP; 7237905288fSFlorian Fainelli 724d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 7257905288fSFlorian Fainelli if (ret) 7267905288fSFlorian Fainelli return ret; 7277905288fSFlorian Fainelli 728aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 729aab9c406SFlorian Fainelli } 730aab9c406SFlorian Fainelli 731aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 732aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 733aab9c406SFlorian Fainelli { 734aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 735aab9c406SFlorian Fainelli 736aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 737aab9c406SFlorian Fainelli } 738aab9c406SFlorian Fainelli 739aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 740aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 741aab9c406SFlorian Fainelli { 742aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 743aab9c406SFlorian Fainelli 744aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 7457905288fSFlorian Fainelli } 7467905288fSFlorian Fainelli 74704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 74804ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 74904ff53f9SFlorian Fainelli struct netpoll_info *ni) 75004ff53f9SFlorian Fainelli { 751d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 75204ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 75304ff53f9SFlorian Fainelli struct netpoll *netpoll; 75404ff53f9SFlorian Fainelli int err = 0; 75504ff53f9SFlorian Fainelli 75604ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 75704ff53f9SFlorian Fainelli if (!netpoll) 75804ff53f9SFlorian Fainelli return -ENOMEM; 75904ff53f9SFlorian Fainelli 76004ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 76104ff53f9SFlorian Fainelli if (err) { 76204ff53f9SFlorian Fainelli kfree(netpoll); 76304ff53f9SFlorian Fainelli goto out; 76404ff53f9SFlorian Fainelli } 76504ff53f9SFlorian Fainelli 76604ff53f9SFlorian Fainelli p->netpoll = netpoll; 76704ff53f9SFlorian Fainelli out: 76804ff53f9SFlorian Fainelli return err; 76904ff53f9SFlorian Fainelli } 77004ff53f9SFlorian Fainelli 77104ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 77204ff53f9SFlorian Fainelli { 77304ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 77404ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 77504ff53f9SFlorian Fainelli 77604ff53f9SFlorian Fainelli if (!netpoll) 77704ff53f9SFlorian Fainelli return; 77804ff53f9SFlorian Fainelli 77904ff53f9SFlorian Fainelli p->netpoll = NULL; 78004ff53f9SFlorian Fainelli 781c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 78204ff53f9SFlorian Fainelli } 78304ff53f9SFlorian Fainelli 78404ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 78504ff53f9SFlorian Fainelli { 78604ff53f9SFlorian Fainelli } 78704ff53f9SFlorian Fainelli #endif 78804ff53f9SFlorian Fainelli 78944bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 79044bb765cSFlorian Fainelli char *name, size_t len) 79144bb765cSFlorian Fainelli { 792d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 79344bb765cSFlorian Fainelli 794d484210bSJiri Pirko /* For non-legacy ports, devlink is used and it takes 795d484210bSJiri Pirko * care of the name generation. This ndo implementation 796d484210bSJiri Pirko * should be removed with legacy support. 797d484210bSJiri Pirko */ 798d484210bSJiri Pirko if (dp->ds->devlink) 799d484210bSJiri Pirko return -EOPNOTSUPP; 800d484210bSJiri Pirko 801d945097bSVivien Didelot if (snprintf(name, len, "p%d", dp->index) >= len) 80244bb765cSFlorian Fainelli return -EINVAL; 8033a543ef4SFlorian Fainelli 8043a543ef4SFlorian Fainelli return 0; 8053a543ef4SFlorian Fainelli } 8063a543ef4SFlorian Fainelli 807f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 8084fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 809f50f2127SFlorian Fainelli { 8104fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 811f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 812f50f2127SFlorian Fainelli 813f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 814f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 815f50f2127SFlorian Fainelli return mall_tc_entry; 816f50f2127SFlorian Fainelli 817f50f2127SFlorian Fainelli return NULL; 818f50f2127SFlorian Fainelli } 819f50f2127SFlorian Fainelli 820f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev, 821f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 822f50f2127SFlorian Fainelli bool ingress) 823f50f2127SFlorian Fainelli { 824d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 825f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 826f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 8275fd9fc4eSJiri Pirko __be16 protocol = cls->common.protocol; 828d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8299681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 830d945097bSVivien Didelot struct dsa_port *to_dp; 831f50f2127SFlorian Fainelli int err = -EOPNOTSUPP; 832f50f2127SFlorian Fainelli 833f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 834f50f2127SFlorian Fainelli return err; 835f50f2127SFlorian Fainelli 8369681e8b3SPieter Jansen van Vuuren if (!flow_offload_has_one_action(&cls->rule->action)) 837f50f2127SFlorian Fainelli return err; 838f50f2127SFlorian Fainelli 8399681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 840f50f2127SFlorian Fainelli 8419681e8b3SPieter Jansen van Vuuren if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) { 842f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror; 843f50f2127SFlorian Fainelli 8449681e8b3SPieter Jansen van Vuuren if (!act->dev) 845f50f2127SFlorian Fainelli return -EINVAL; 846f50f2127SFlorian Fainelli 8479681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 848f50f2127SFlorian Fainelli return -EOPNOTSUPP; 849f50f2127SFlorian Fainelli 850f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 851f50f2127SFlorian Fainelli if (!mall_tc_entry) 852f50f2127SFlorian Fainelli return -ENOMEM; 853f50f2127SFlorian Fainelli 854f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 855f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 856f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 857f50f2127SFlorian Fainelli 8589681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 859f50f2127SFlorian Fainelli 860d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 861f50f2127SFlorian Fainelli mirror->ingress = ingress; 862f50f2127SFlorian Fainelli 863d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 864f50f2127SFlorian Fainelli if (err) { 865f50f2127SFlorian Fainelli kfree(mall_tc_entry); 866f50f2127SFlorian Fainelli return err; 867f50f2127SFlorian Fainelli } 868f50f2127SFlorian Fainelli 869f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 870f50f2127SFlorian Fainelli } 871f50f2127SFlorian Fainelli 872f50f2127SFlorian Fainelli return 0; 873f50f2127SFlorian Fainelli } 874f50f2127SFlorian Fainelli 875f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 876f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 877f50f2127SFlorian Fainelli { 878d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 879f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 880d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 881f50f2127SFlorian Fainelli 882f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_del) 883f50f2127SFlorian Fainelli return; 884f50f2127SFlorian Fainelli 8854fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 886f50f2127SFlorian Fainelli if (!mall_tc_entry) 887f50f2127SFlorian Fainelli return; 888f50f2127SFlorian Fainelli 889f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 890f50f2127SFlorian Fainelli 891f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 892f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 893d945097bSVivien Didelot ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror); 894f50f2127SFlorian Fainelli break; 895f50f2127SFlorian Fainelli default: 896f50f2127SFlorian Fainelli WARN_ON(1); 897f50f2127SFlorian Fainelli } 898f50f2127SFlorian Fainelli 899f50f2127SFlorian Fainelli kfree(mall_tc_entry); 900f50f2127SFlorian Fainelli } 901f50f2127SFlorian Fainelli 9023fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 9036b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 9046b3eb752SJiri Pirko bool ingress) 905f50f2127SFlorian Fainelli { 9065fd9fc4eSJiri Pirko if (cls->common.chain_index) 907a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 908f50f2127SFlorian Fainelli 9093fbae382SJiri Pirko switch (cls->command) { 9103fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 9115fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 9123fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 9133fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 9143fbae382SJiri Pirko return 0; 9153fbae382SJiri Pirko default: 9163fbae382SJiri Pirko return -EOPNOTSUPP; 9173fbae382SJiri Pirko } 9183fbae382SJiri Pirko } 9193fbae382SJiri Pirko 9206b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 9216b3eb752SJiri Pirko void *cb_priv, bool ingress) 9226b3eb752SJiri Pirko { 9236b3eb752SJiri Pirko struct net_device *dev = cb_priv; 9246b3eb752SJiri Pirko 92544ae12a7SJiri Pirko if (!tc_can_offload(dev)) 92644ae12a7SJiri Pirko return -EOPNOTSUPP; 92744ae12a7SJiri Pirko 9286b3eb752SJiri Pirko switch (type) { 9296b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 9306b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 9316b3eb752SJiri Pirko default: 9326b3eb752SJiri Pirko return -EOPNOTSUPP; 9336b3eb752SJiri Pirko } 9346b3eb752SJiri Pirko } 9356b3eb752SJiri Pirko 9366b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 9376b3eb752SJiri Pirko void *type_data, void *cb_priv) 9386b3eb752SJiri Pirko { 9396b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 9406b3eb752SJiri Pirko } 9416b3eb752SJiri Pirko 9426b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 9436b3eb752SJiri Pirko void *type_data, void *cb_priv) 9446b3eb752SJiri Pirko { 9456b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 9466b3eb752SJiri Pirko } 9476b3eb752SJiri Pirko 948955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 949955bcb6eSPablo Neira Ayuso 9506b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 951955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 9526b3eb752SJiri Pirko { 953955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 954*a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 9556b3eb752SJiri Pirko 95632f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 9576b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 95832f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 9596b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 9606b3eb752SJiri Pirko else 9616b3eb752SJiri Pirko return -EOPNOTSUPP; 9626b3eb752SJiri Pirko 963955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 964955bcb6eSPablo Neira Ayuso 9656b3eb752SJiri Pirko switch (f->command) { 9669c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 9670d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 9680d4fd02eSPablo Neira Ayuso return -EBUSY; 9690d4fd02eSPablo Neira Ayuso 9700c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 971955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 972955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 973955bcb6eSPablo Neira Ayuso 974955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 975955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 976955bcb6eSPablo Neira Ayuso return 0; 9779c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 978955bcb6eSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f, cb, dev); 979955bcb6eSPablo Neira Ayuso if (!block_cb) 980955bcb6eSPablo Neira Ayuso return -ENOENT; 981955bcb6eSPablo Neira Ayuso 982955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 983955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 9846b3eb752SJiri Pirko return 0; 9856b3eb752SJiri Pirko default: 9866b3eb752SJiri Pirko return -EOPNOTSUPP; 9876b3eb752SJiri Pirko } 9886b3eb752SJiri Pirko } 9896b3eb752SJiri Pirko 9903fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 991de4784caSJiri Pirko void *type_data) 9923fbae382SJiri Pirko { 9932572ac53SJiri Pirko switch (type) { 9946b3eb752SJiri Pirko case TC_SETUP_BLOCK: 9956b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 996f50f2127SFlorian Fainelli default: 997a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 998f50f2127SFlorian Fainelli } 999f50f2127SFlorian Fainelli } 1000f50f2127SFlorian Fainelli 1001f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev, 1002f613ed66SFlorian Fainelli struct rtnl_link_stats64 *stats) 1003f613ed66SFlorian Fainelli { 1004f613ed66SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 10055f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 1006f613ed66SFlorian Fainelli unsigned int start; 10075f6b4e14SFlorian Fainelli int i; 1008f613ed66SFlorian Fainelli 1009f613ed66SFlorian Fainelli netdev_stats_to_stats64(stats, &dev->stats); 10105f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 10115f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 10125f6b4e14SFlorian Fainelli 10135f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 1014f613ed66SFlorian Fainelli do { 10155f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 10165f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 10175f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 10185f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 10195f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 10205f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 10215f6b4e14SFlorian Fainelli 10225f6b4e14SFlorian Fainelli stats->tx_packets += tx_packets; 10235f6b4e14SFlorian Fainelli stats->tx_bytes += tx_bytes; 10245f6b4e14SFlorian Fainelli stats->rx_packets += rx_packets; 10255f6b4e14SFlorian Fainelli stats->rx_bytes += rx_bytes; 10265f6b4e14SFlorian Fainelli } 1027f613ed66SFlorian Fainelli } 1028f613ed66SFlorian Fainelli 1029bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1030bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1031bf9f2648SFlorian Fainelli { 1032d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1033d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1034bf9f2648SFlorian Fainelli 1035bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1036bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1037bf9f2648SFlorian Fainelli 1038d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1039bf9f2648SFlorian Fainelli } 1040bf9f2648SFlorian Fainelli 1041bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1042bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1043bf9f2648SFlorian Fainelli { 1044d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1045d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1046bf9f2648SFlorian Fainelli 1047bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1048bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1049bf9f2648SFlorian Fainelli 1050d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1051bf9f2648SFlorian Fainelli } 1052bf9f2648SFlorian Fainelli 10530336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 10540336369dSBrandon Streiff struct ethtool_ts_info *ts) 10550336369dSBrandon Streiff { 10560336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 10570336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 10580336369dSBrandon Streiff 10590336369dSBrandon Streiff if (!ds->ops->get_ts_info) 10600336369dSBrandon Streiff return -EOPNOTSUPP; 10610336369dSBrandon Streiff 10620336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 10630336369dSBrandon Streiff } 10640336369dSBrandon Streiff 1065061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1066061f6a50SFlorian Fainelli u16 vid) 1067061f6a50SFlorian Fainelli { 1068061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1069061f6a50SFlorian Fainelli struct bridge_vlan_info info; 1070061f6a50SFlorian Fainelli int ret; 1071061f6a50SFlorian Fainelli 1072061f6a50SFlorian Fainelli /* Check for a possible bridge VLAN entry now since there is no 1073061f6a50SFlorian Fainelli * need to emulate the switchdev prepare + commit phase. 1074061f6a50SFlorian Fainelli */ 1075061f6a50SFlorian Fainelli if (dp->bridge_dev) { 1076061f6a50SFlorian Fainelli /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 1077061f6a50SFlorian Fainelli * device, respectively the VID is not found, returning 1078061f6a50SFlorian Fainelli * 0 means success, which is a failure for us here. 1079061f6a50SFlorian Fainelli */ 1080061f6a50SFlorian Fainelli ret = br_vlan_get_info(dp->bridge_dev, vid, &info); 1081061f6a50SFlorian Fainelli if (ret == 0) 1082061f6a50SFlorian Fainelli return -EBUSY; 1083061f6a50SFlorian Fainelli } 1084061f6a50SFlorian Fainelli 1085314f76d7SVladimir Oltean /* This API only allows programming tagged, non-PVID VIDs */ 1086314f76d7SVladimir Oltean return dsa_port_vid_add(dp, vid, 0); 1087061f6a50SFlorian Fainelli } 1088061f6a50SFlorian Fainelli 1089061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1090061f6a50SFlorian Fainelli u16 vid) 1091061f6a50SFlorian Fainelli { 1092061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1093061f6a50SFlorian Fainelli struct bridge_vlan_info info; 1094061f6a50SFlorian Fainelli int ret; 1095061f6a50SFlorian Fainelli 1096061f6a50SFlorian Fainelli /* Check for a possible bridge VLAN entry now since there is no 1097061f6a50SFlorian Fainelli * need to emulate the switchdev prepare + commit phase. 1098061f6a50SFlorian Fainelli */ 1099061f6a50SFlorian Fainelli if (dp->bridge_dev) { 1100061f6a50SFlorian Fainelli /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 1101061f6a50SFlorian Fainelli * device, respectively the VID is not found, returning 1102061f6a50SFlorian Fainelli * 0 means success, which is a failure for us here. 1103061f6a50SFlorian Fainelli */ 1104061f6a50SFlorian Fainelli ret = br_vlan_get_info(dp->bridge_dev, vid, &info); 1105061f6a50SFlorian Fainelli if (ret == 0) 1106061f6a50SFlorian Fainelli return -EBUSY; 1107061f6a50SFlorian Fainelli } 1108061f6a50SFlorian Fainelli 1109314f76d7SVladimir Oltean ret = dsa_port_vid_del(dp, vid); 1110061f6a50SFlorian Fainelli if (ret == -EOPNOTSUPP) 1111061f6a50SFlorian Fainelli ret = 0; 1112061f6a50SFlorian Fainelli 1113061f6a50SFlorian Fainelli return ret; 1114061f6a50SFlorian Fainelli } 1115061f6a50SFlorian Fainelli 111691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 111791da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 11183d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 11193d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 1120aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 1121c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 11226793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 11236793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 11246793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 112591da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 112691da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 112791da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 112819e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 112919e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 11307905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 11317905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 1132aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 1133aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 1134bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 1135bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 11360336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 113791da11f8SLennert Buytenhek }; 113891da11f8SLennert Buytenhek 11392a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/ 11402a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 11412a93c1a3SFlorian Fainelli struct net_device *dev, 11422a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid, 114387b0984eSPetr Machata u16 flags, 114487b0984eSPetr Machata struct netlink_ext_ack *extack) 11452a93c1a3SFlorian Fainelli { 11462a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 11472a93c1a3SFlorian Fainelli 11482a93c1a3SFlorian Fainelli return dsa_port_fdb_add(dp, addr, vid); 11492a93c1a3SFlorian Fainelli } 11502a93c1a3SFlorian Fainelli 11512a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 11522a93c1a3SFlorian Fainelli struct net_device *dev, 11532a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid) 11542a93c1a3SFlorian Fainelli { 11552a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 11562a93c1a3SFlorian Fainelli 11572a93c1a3SFlorian Fainelli return dsa_port_fdb_del(dp, addr, vid); 11582a93c1a3SFlorian Fainelli } 11592a93c1a3SFlorian Fainelli 1160716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 1161716efee2SJiri Pirko { 1162716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 1163716efee2SJiri Pirko 1164716efee2SJiri Pirko return dp->ds->devlink ? &dp->devlink_port : NULL; 1165716efee2SJiri Pirko } 1166716efee2SJiri Pirko 11673e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 1168d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 1169d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 11703e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 1171d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1172d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1173d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 117437b8da1aSArkadi Sharshevsky .ndo_fdb_add = dsa_legacy_fdb_add, 117537b8da1aSArkadi Sharshevsky .ndo_fdb_del = dsa_legacy_fdb_del, 11762bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 1177d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 1178abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 117904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 118004ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 118104ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 118204ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 118304ff53f9SFlorian Fainelli #endif 118444bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1185f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 1186f613ed66SFlorian Fainelli .ndo_get_stats64 = dsa_slave_get_stats64, 1187929d6c14SFlorian Fainelli .ndo_get_port_parent_id = dsa_slave_get_port_parent_id, 1188061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 1189061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 1190716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 119198237d43SScott Feldman }; 119298237d43SScott Feldman 1193f37db85dSFlorian Fainelli static struct device_type dsa_type = { 1194f37db85dSFlorian Fainelli .name = "dsa", 1195f37db85dSFlorian Fainelli }; 1196f37db85dSFlorian Fainelli 1197aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1198aab9c406SFlorian Fainelli { 1199aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 1200aab9c406SFlorian Fainelli 1201aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 1202aab9c406SFlorian Fainelli } 1203aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 1204aab9c406SFlorian Fainelli 1205aab9c406SFlorian Fainelli static void dsa_slave_phylink_fixed_state(struct net_device *dev, 1206aab9c406SFlorian Fainelli struct phylink_link_state *state) 1207aab9c406SFlorian Fainelli { 1208aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1209aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 1210aab9c406SFlorian Fainelli 1211aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 1212aab9c406SFlorian Fainelli * not be called if it was not. 1213aab9c406SFlorian Fainelli */ 1214aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 1215ce31b31cSFlorian Fainelli } 1216ce31b31cSFlorian Fainelli 121791da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 12184fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1219c305c165SFlorian Fainelli { 1220d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1221d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1222c305c165SFlorian Fainelli 12230115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 12240115dcd1SVivien Didelot if (!slave_dev->phydev) { 1225d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1226c305c165SFlorian Fainelli return -ENODEV; 1227d25b8e74SRussell King } 1228c305c165SFlorian Fainelli 1229aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 1230c305c165SFlorian Fainelli } 1231c305c165SFlorian Fainelli 12324fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 12330d8bcdd3SFlorian Fainelli { 1234d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1235d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 1236d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 12376819563eSFlorian Fainelli u32 phy_flags = 0; 123819334920SGuenter Roeck int mode, ret; 12390d8bcdd3SFlorian Fainelli 124019334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 124119334920SGuenter Roeck if (mode < 0) 124219334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 12430d8bcdd3SFlorian Fainelli 124444cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 124544cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 124644cc27e4SIoana Ciornei 124744cc27e4SIoana Ciornei dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 124877373d49SIoana Ciornei &dsa_port_phylink_mac_ops); 1249aab9c406SFlorian Fainelli if (IS_ERR(dp->pl)) { 1250aab9c406SFlorian Fainelli netdev_err(slave_dev, 1251aab9c406SFlorian Fainelli "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1252aab9c406SFlorian Fainelli return PTR_ERR(dp->pl); 1253aab9c406SFlorian Fainelli } 1254aab9c406SFlorian Fainelli 1255aab9c406SFlorian Fainelli /* Register only if the switch provides such a callback, since this 1256aab9c406SFlorian Fainelli * callback takes precedence over polling the link GPIO in PHYLINK 1257aab9c406SFlorian Fainelli * (see phylink_get_fixed_state). 12580d8bcdd3SFlorian Fainelli */ 1259aab9c406SFlorian Fainelli if (ds->ops->phylink_fixed_state) 1260aab9c406SFlorian Fainelli phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state); 12610d8bcdd3SFlorian Fainelli 12629d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1263d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 12646819563eSFlorian Fainelli 1265aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 12666146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 12676146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 12686146dd45SVladimir Oltean * use the switch internal MDIO bus instead 12690d8bcdd3SFlorian Fainelli */ 1270d945097bSVivien Didelot ret = dsa_slave_phy_connect(slave_dev, dp->index); 1271d25b8e74SRussell King if (ret) { 1272aab9c406SFlorian Fainelli netdev_err(slave_dev, 1273aab9c406SFlorian Fainelli "failed to connect to port %d: %d\n", 1274d945097bSVivien Didelot dp->index, ret); 1275aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 1276c305c165SFlorian Fainelli return ret; 1277d25b8e74SRussell King } 12780d8bcdd3SFlorian Fainelli } 12799697f1cdSFlorian Fainelli 12806146dd45SVladimir Oltean return ret; 1281b31f65fbSAndrew Lunn } 12820d8bcdd3SFlorian Fainelli 1283448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1284448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1285448b4482SAndrew Lunn struct netdev_queue *txq, 1286448b4482SAndrew Lunn void *_unused) 1287448b4482SAndrew Lunn { 1288448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 1289448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 1290448b4482SAndrew Lunn } 1291448b4482SAndrew Lunn 129224462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 129324462549SFlorian Fainelli { 1294aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 129524462549SFlorian Fainelli 1296a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1297a94c689eSFlorian Fainelli return 0; 1298a94c689eSFlorian Fainelli 129997a69a0dSVladimir Oltean cancel_work_sync(&dp->xmit_work); 130097a69a0dSVladimir Oltean skb_queue_purge(&dp->xmit_queue); 130197a69a0dSVladimir Oltean 1302f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1303f154be24SFlorian Fainelli 1304aab9c406SFlorian Fainelli rtnl_lock(); 1305aab9c406SFlorian Fainelli phylink_stop(dp->pl); 1306aab9c406SFlorian Fainelli rtnl_unlock(); 130724462549SFlorian Fainelli 130824462549SFlorian Fainelli return 0; 130924462549SFlorian Fainelli } 131024462549SFlorian Fainelli 131124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 131224462549SFlorian Fainelli { 1313aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1314aab9c406SFlorian Fainelli 1315a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1316a94c689eSFlorian Fainelli return 0; 1317a94c689eSFlorian Fainelli 131824462549SFlorian Fainelli netif_device_attach(slave_dev); 131924462549SFlorian Fainelli 1320aab9c406SFlorian Fainelli rtnl_lock(); 1321aab9c406SFlorian Fainelli phylink_start(dp->pl); 1322aab9c406SFlorian Fainelli rtnl_unlock(); 132324462549SFlorian Fainelli 132424462549SFlorian Fainelli return 0; 132524462549SFlorian Fainelli } 132624462549SFlorian Fainelli 13276158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val) 13286158eaa7SVivien Didelot { 1329d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 1330d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 13316158eaa7SVivien Didelot struct dsa_notifier_register_info rinfo = { 13326158eaa7SVivien Didelot .switch_number = dp->ds->index, 13336158eaa7SVivien Didelot .port_number = dp->index, 13346158eaa7SVivien Didelot .master = master, 13356158eaa7SVivien Didelot .info.dev = dev, 13366158eaa7SVivien Didelot }; 13376158eaa7SVivien Didelot 13386158eaa7SVivien Didelot call_dsa_notifiers(val, dev, &rinfo.info); 13396158eaa7SVivien Didelot } 13406158eaa7SVivien Didelot 1341951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 134291da11f8SLennert Buytenhek { 134324a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 1344f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 13454cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 1346951259aaSVivien Didelot const char *name = port->name; 134791da11f8SLennert Buytenhek struct net_device *slave_dev; 134891da11f8SLennert Buytenhek struct dsa_slave_priv *p; 134991da11f8SLennert Buytenhek int ret; 135091da11f8SLennert Buytenhek 135155199df6SFlorian Fainelli if (!ds->num_tx_queues) 135255199df6SFlorian Fainelli ds->num_tx_queues = 1; 135355199df6SFlorian Fainelli 135455199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 135555199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 135655199df6SFlorian Fainelli ds->num_tx_queues, 1); 135791da11f8SLennert Buytenhek if (slave_dev == NULL) 1358d87d6f44SGuenter Roeck return -ENOMEM; 135991da11f8SLennert Buytenhek 1360061f6a50SFlorian Fainelli slave_dev->features = master->vlan_features | NETIF_F_HW_TC | 1361061f6a50SFlorian Fainelli NETIF_F_HW_VLAN_CTAG_FILTER; 1362f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 13637ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 13644974f9b7SPetr Štetiar if (!IS_ERR_OR_NULL(port->mac)) 1365a2c7023fSXiaofei Shen ether_addr_copy(slave_dev->dev_addr, port->mac); 1366a2c7023fSXiaofei Shen else 13672fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 13680a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 13693e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 13708b1efc0fSJarod Wilson slave_dev->min_mtu = 0; 13718b1efc0fSJarod Wilson slave_dev->max_mtu = ETH_MAX_MTU; 1372f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1373d442ad4aSStephen Hemminger 1374448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1375448b4482SAndrew Lunn NULL); 1376448b4482SAndrew Lunn 13774cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 13784cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 137991da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 138091da11f8SLennert Buytenhek 138191da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 13825f6b4e14SFlorian Fainelli p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 13835f6b4e14SFlorian Fainelli if (!p->stats64) { 13845f6b4e14SFlorian Fainelli free_netdev(slave_dev); 13855f6b4e14SFlorian Fainelli return -ENOMEM; 13865f6b4e14SFlorian Fainelli } 13874cfbf09cSVivien Didelot p->dp = port; 1388f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 138997a69a0dSVladimir Oltean INIT_WORK(&port->xmit_work, dsa_port_xmit_work); 139097a69a0dSVladimir Oltean skb_queue_head_init(&port->xmit_queue); 139115240248SVivien Didelot p->xmit = cpu_dp->tag_ops->xmit; 1392f8b8b1cdSVivien Didelot port->slave = slave_dev; 139391da11f8SLennert Buytenhek 139491da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 139591da11f8SLennert Buytenhek 13964fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 13970071f56eSAndrew Lunn if (ret) { 13980071f56eSAndrew Lunn netdev_err(master, "error %d setting up slave phy\n", ret); 1399e804441cSFlorian Fainelli goto out_free; 1400e804441cSFlorian Fainelli } 1401e804441cSFlorian Fainelli 14026158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_REGISTER); 140360724d4bSFlorian Fainelli 1404e804441cSFlorian Fainelli ret = register_netdev(slave_dev); 1405e804441cSFlorian Fainelli if (ret) { 1406e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 1407e804441cSFlorian Fainelli ret, slave_dev->name); 1408e804441cSFlorian Fainelli goto out_phy; 14090071f56eSAndrew Lunn } 14100071f56eSAndrew Lunn 1411d87d6f44SGuenter Roeck return 0; 1412e804441cSFlorian Fainelli 1413e804441cSFlorian Fainelli out_phy: 1414aab9c406SFlorian Fainelli rtnl_lock(); 1415aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 1416aab9c406SFlorian Fainelli rtnl_unlock(); 1417aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 1418e804441cSFlorian Fainelli out_free: 1419e804441cSFlorian Fainelli free_percpu(p->stats64); 1420e804441cSFlorian Fainelli free_netdev(slave_dev); 1421f8b8b1cdSVivien Didelot port->slave = NULL; 1422e804441cSFlorian Fainelli return ret; 142391da11f8SLennert Buytenhek } 1424b73adef6SFlorian Fainelli 1425cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1426cda5c15bSNeil Armstrong { 1427d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1428cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1429cda5c15bSNeil Armstrong 1430cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 1431aab9c406SFlorian Fainelli rtnl_lock(); 1432aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 1433aab9c406SFlorian Fainelli rtnl_unlock(); 1434881eadabSJohan Hovold 14356158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); 1436cda5c15bSNeil Armstrong unregister_netdev(slave_dev); 1437aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 14385f6b4e14SFlorian Fainelli free_percpu(p->stats64); 1439cda5c15bSNeil Armstrong free_netdev(slave_dev); 1440cda5c15bSNeil Armstrong } 1441cda5c15bSNeil Armstrong 1442f3b78049SVivien Didelot static bool dsa_slave_dev_check(const struct net_device *dev) 1443b73adef6SFlorian Fainelli { 1444b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1445b73adef6SFlorian Fainelli } 1446b73adef6SFlorian Fainelli 14478e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 14488e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1449b73adef6SFlorian Fainelli { 1450d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 14518e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1452b73adef6SFlorian Fainelli 14538e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 14548e92ab3aSVivien Didelot if (info->linking) { 145517d7802bSVivien Didelot err = dsa_port_bridge_join(dp, info->upper_dev); 14568e92ab3aSVivien Didelot err = notifier_from_errno(err); 14578e92ab3aSVivien Didelot } else { 145817d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 14598e92ab3aSVivien Didelot err = NOTIFY_OK; 14608e92ab3aSVivien Didelot } 14616debb68aSVivien Didelot } 1462b73adef6SFlorian Fainelli 14638e92ab3aSVivien Didelot return err; 1464b73adef6SFlorian Fainelli } 1465b73adef6SFlorian Fainelli 1466cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev, 1467cc1d5bdaSFlorian Fainelli struct netdev_notifier_changeupper_info * 1468cc1d5bdaSFlorian Fainelli info) 1469cc1d5bdaSFlorian Fainelli { 1470cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 1471cc1d5bdaSFlorian Fainelli struct net_device *slave; 1472cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 1473cc1d5bdaSFlorian Fainelli 1474cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 1475cc1d5bdaSFlorian Fainelli 1476cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 1477cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1478cc1d5bdaSFlorian Fainelli 1479cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 1480cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 1481cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1482cc1d5bdaSFlorian Fainelli 1483cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 1484cc1d5bdaSFlorian Fainelli if (!dp->bridge_dev) 1485cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1486cc1d5bdaSFlorian Fainelli 1487cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 1488cc1d5bdaSFlorian Fainelli if (br_vlan_enabled(dp->bridge_dev) && 1489cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 1490cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 1491cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 1492cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 1493cc1d5bdaSFlorian Fainelli } 1494cc1d5bdaSFlorian Fainelli 1495cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1496cc1d5bdaSFlorian Fainelli } 1497cc1d5bdaSFlorian Fainelli 149888e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 1499b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1500b73adef6SFlorian Fainelli { 15016debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1502b73adef6SFlorian Fainelli 1503cc1d5bdaSFlorian Fainelli if (event == NETDEV_CHANGEUPPER) { 150453bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 1505cc1d5bdaSFlorian Fainelli return dsa_slave_upper_vlan_check(dev, ptr); 15068e92ab3aSVivien Didelot 15078e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 1508cc1d5bdaSFlorian Fainelli } 1509b73adef6SFlorian Fainelli 1510b73adef6SFlorian Fainelli return NOTIFY_DONE; 1511b73adef6SFlorian Fainelli } 151288e4f0caSVivien Didelot 1513c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work { 1514c9eb3e0fSArkadi Sharshevsky struct work_struct work; 1515c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info fdb_info; 1516c9eb3e0fSArkadi Sharshevsky struct net_device *dev; 1517c9eb3e0fSArkadi Sharshevsky unsigned long event; 1518c9eb3e0fSArkadi Sharshevsky }; 1519c9eb3e0fSArkadi Sharshevsky 1520c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 1521c9eb3e0fSArkadi Sharshevsky { 1522c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 1523c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 1524c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_work->dev; 1525c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info *fdb_info; 1526d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1527c9eb3e0fSArkadi Sharshevsky int err; 1528c9eb3e0fSArkadi Sharshevsky 1529c9eb3e0fSArkadi Sharshevsky rtnl_lock(); 1530c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 1531c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 1532c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1533a37fb855SVivien Didelot if (!fdb_info->added_by_user) 1534a37fb855SVivien Didelot break; 1535a37fb855SVivien Didelot 1536d945097bSVivien Didelot err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid); 1537c9eb3e0fSArkadi Sharshevsky if (err) { 1538c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb add failed err=%d\n", err); 1539c9eb3e0fSArkadi Sharshevsky break; 1540c9eb3e0fSArkadi Sharshevsky } 1541e9ba0fbcSIdo Schimmel fdb_info->offloaded = true; 1542c9eb3e0fSArkadi Sharshevsky call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 15436685987cSPetr Machata &fdb_info->info, NULL); 1544c9eb3e0fSArkadi Sharshevsky break; 1545c9eb3e0fSArkadi Sharshevsky 1546c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1547c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1548a37fb855SVivien Didelot if (!fdb_info->added_by_user) 1549a37fb855SVivien Didelot break; 1550a37fb855SVivien Didelot 1551d945097bSVivien Didelot err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid); 1552c9eb3e0fSArkadi Sharshevsky if (err) { 1553c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb del failed err=%d\n", err); 1554c9eb3e0fSArkadi Sharshevsky dev_close(dev); 1555c9eb3e0fSArkadi Sharshevsky } 1556c9eb3e0fSArkadi Sharshevsky break; 1557c9eb3e0fSArkadi Sharshevsky } 1558c9eb3e0fSArkadi Sharshevsky rtnl_unlock(); 1559c9eb3e0fSArkadi Sharshevsky 1560c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work->fdb_info.addr); 1561c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1562c9eb3e0fSArkadi Sharshevsky dev_put(dev); 1563c9eb3e0fSArkadi Sharshevsky } 1564c9eb3e0fSArkadi Sharshevsky 1565c9eb3e0fSArkadi Sharshevsky static int 1566c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * 1567c9eb3e0fSArkadi Sharshevsky switchdev_work, 1568c9eb3e0fSArkadi Sharshevsky const struct switchdev_notifier_fdb_info * 1569c9eb3e0fSArkadi Sharshevsky fdb_info) 1570c9eb3e0fSArkadi Sharshevsky { 1571c9eb3e0fSArkadi Sharshevsky memcpy(&switchdev_work->fdb_info, fdb_info, 1572c9eb3e0fSArkadi Sharshevsky sizeof(switchdev_work->fdb_info)); 1573c9eb3e0fSArkadi Sharshevsky switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 1574c9eb3e0fSArkadi Sharshevsky if (!switchdev_work->fdb_info.addr) 1575c9eb3e0fSArkadi Sharshevsky return -ENOMEM; 1576c9eb3e0fSArkadi Sharshevsky ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 1577c9eb3e0fSArkadi Sharshevsky fdb_info->addr); 1578c9eb3e0fSArkadi Sharshevsky return 0; 1579c9eb3e0fSArkadi Sharshevsky } 1580c9eb3e0fSArkadi Sharshevsky 1581c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 1582c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 1583c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 1584c9eb3e0fSArkadi Sharshevsky { 1585c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1586c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work; 158779b139f4SVivien Didelot int err; 158879b139f4SVivien Didelot 158979b139f4SVivien Didelot if (event == SWITCHDEV_PORT_ATTR_SET) { 159079b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 159179b139f4SVivien Didelot dsa_slave_dev_check, 159279b139f4SVivien Didelot dsa_slave_port_attr_set); 159379b139f4SVivien Didelot return notifier_from_errno(err); 159479b139f4SVivien Didelot } 1595c9eb3e0fSArkadi Sharshevsky 1596c9eb3e0fSArkadi Sharshevsky if (!dsa_slave_dev_check(dev)) 1597c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1598c9eb3e0fSArkadi Sharshevsky 1599c9eb3e0fSArkadi Sharshevsky switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 1600c9eb3e0fSArkadi Sharshevsky if (!switchdev_work) 1601c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1602c9eb3e0fSArkadi Sharshevsky 1603c9eb3e0fSArkadi Sharshevsky INIT_WORK(&switchdev_work->work, 1604c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_event_work); 1605c9eb3e0fSArkadi Sharshevsky switchdev_work->dev = dev; 1606c9eb3e0fSArkadi Sharshevsky switchdev_work->event = event; 1607c9eb3e0fSArkadi Sharshevsky 1608c9eb3e0fSArkadi Sharshevsky switch (event) { 1609c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ 1610c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1611a37fb855SVivien Didelot if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr)) 1612c9eb3e0fSArkadi Sharshevsky goto err_fdb_work_init; 1613c9eb3e0fSArkadi Sharshevsky dev_hold(dev); 1614c9eb3e0fSArkadi Sharshevsky break; 1615c9eb3e0fSArkadi Sharshevsky default: 1616c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1617c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1618c9eb3e0fSArkadi Sharshevsky } 1619c9eb3e0fSArkadi Sharshevsky 1620c9eb3e0fSArkadi Sharshevsky dsa_schedule_work(&switchdev_work->work); 1621c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 1622c9eb3e0fSArkadi Sharshevsky 1623c9eb3e0fSArkadi Sharshevsky err_fdb_work_init: 1624c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1625c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1626c9eb3e0fSArkadi Sharshevsky } 1627c9eb3e0fSArkadi Sharshevsky 16282b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 16292b239f67SPetr Machata unsigned long event, void *ptr) 16302b239f67SPetr Machata { 16312b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 163279b139f4SVivien Didelot int err; 16332b239f67SPetr Machata 16342b239f67SPetr Machata switch (event) { 163579b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 163679b139f4SVivien Didelot err = switchdev_handle_port_obj_add(dev, ptr, 163779b139f4SVivien Didelot dsa_slave_dev_check, 163879b139f4SVivien Didelot dsa_slave_port_obj_add); 163979b139f4SVivien Didelot return notifier_from_errno(err); 16402b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 164179b139f4SVivien Didelot err = switchdev_handle_port_obj_del(dev, ptr, 164279b139f4SVivien Didelot dsa_slave_dev_check, 164379b139f4SVivien Didelot dsa_slave_port_obj_del); 164479b139f4SVivien Didelot return notifier_from_errno(err); 16459ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 164679b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 164779b139f4SVivien Didelot dsa_slave_dev_check, 164879b139f4SVivien Didelot dsa_slave_port_attr_set); 164979b139f4SVivien Didelot return notifier_from_errno(err); 16502b239f67SPetr Machata } 16512b239f67SPetr Machata 16522b239f67SPetr Machata return NOTIFY_DONE; 16532b239f67SPetr Machata } 16542b239f67SPetr Machata 165588e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 165688e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 165788e4f0caSVivien Didelot }; 165888e4f0caSVivien Didelot 1659c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = { 1660c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 1661c9eb3e0fSArkadi Sharshevsky }; 1662c9eb3e0fSArkadi Sharshevsky 16632b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = { 16642b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 16652b239f67SPetr Machata }; 16662b239f67SPetr Machata 166788e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 166888e4f0caSVivien Didelot { 16692b239f67SPetr Machata struct notifier_block *nb; 1670c9eb3e0fSArkadi Sharshevsky int err; 1671c9eb3e0fSArkadi Sharshevsky 1672c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 1673c9eb3e0fSArkadi Sharshevsky if (err) 1674c9eb3e0fSArkadi Sharshevsky return err; 1675c9eb3e0fSArkadi Sharshevsky 1676c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 1677c9eb3e0fSArkadi Sharshevsky if (err) 1678c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 1679c9eb3e0fSArkadi Sharshevsky 16802b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 16812b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 16822b239f67SPetr Machata if (err) 16832b239f67SPetr Machata goto err_switchdev_blocking_nb; 16842b239f67SPetr Machata 1685c9eb3e0fSArkadi Sharshevsky return 0; 1686c9eb3e0fSArkadi Sharshevsky 16872b239f67SPetr Machata err_switchdev_blocking_nb: 16882b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 1689c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 1690c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 1691c9eb3e0fSArkadi Sharshevsky return err; 169288e4f0caSVivien Didelot } 169388e4f0caSVivien Didelot 169488e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 169588e4f0caSVivien Didelot { 16962b239f67SPetr Machata struct notifier_block *nb; 169788e4f0caSVivien Didelot int err; 169888e4f0caSVivien Didelot 16992b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 17002b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 17012b239f67SPetr Machata if (err) 17022b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 17032b239f67SPetr Machata 1704c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 1705c9eb3e0fSArkadi Sharshevsky if (err) 1706c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 1707c9eb3e0fSArkadi Sharshevsky 170888e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 170988e4f0caSVivien Didelot if (err) 171088e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 171188e4f0caSVivien Didelot } 1712