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 2591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2791da11f8SLennert Buytenhek { 2891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 2991da11f8SLennert Buytenhek 300d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 319d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 3291da11f8SLennert Buytenhek 3391da11f8SLennert Buytenhek return 0xffff; 3491da11f8SLennert Buytenhek } 3591da11f8SLennert Buytenhek 3691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3791da11f8SLennert Buytenhek { 3891da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3991da11f8SLennert Buytenhek 400d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 419d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 4291da11f8SLennert Buytenhek 4391da11f8SLennert Buytenhek return 0; 4491da11f8SLennert Buytenhek } 4591da11f8SLennert Buytenhek 4691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4791da11f8SLennert Buytenhek { 4891da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 4991da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 5091da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 5191da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 520b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 5349463b7fSVivien Didelot ds->dst->index, ds->index); 54c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 5524df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5691da11f8SLennert Buytenhek } 5791da11f8SLennert Buytenhek 5891da11f8SLennert Buytenhek 5991da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 60abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 61c0840801SLennert Buytenhek { 62d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 63c0840801SLennert Buytenhek } 64c0840801SLennert Buytenhek 6591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 6691da11f8SLennert Buytenhek { 67d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 68d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 69df02c6ffSLennert Buytenhek int err; 70df02c6ffSLennert Buytenhek 71df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 72df02c6ffSLennert Buytenhek return -ENETDOWN; 73df02c6ffSLennert Buytenhek 748feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 75a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 76df02c6ffSLennert Buytenhek if (err < 0) 77df02c6ffSLennert Buytenhek goto out; 78df02c6ffSLennert Buytenhek } 79df02c6ffSLennert Buytenhek 80df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 81df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 82df02c6ffSLennert Buytenhek if (err < 0) 83df02c6ffSLennert Buytenhek goto del_unicast; 84df02c6ffSLennert Buytenhek } 85df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 86df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 87df02c6ffSLennert Buytenhek if (err < 0) 88df02c6ffSLennert Buytenhek goto clear_allmulti; 89df02c6ffSLennert Buytenhek } 90df02c6ffSLennert Buytenhek 918640f8dcSRussell King err = dsa_port_enable_rt(dp, dev->phydev); 92b2f2af21SFlorian Fainelli if (err) 93b2f2af21SFlorian Fainelli goto clear_promisc; 94b73adef6SFlorian Fainelli 9591da11f8SLennert Buytenhek return 0; 96df02c6ffSLennert Buytenhek 97b2f2af21SFlorian Fainelli clear_promisc: 98b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 994fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 100df02c6ffSLennert Buytenhek clear_allmulti: 101df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 102df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 103df02c6ffSLennert Buytenhek del_unicast: 1048feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 105a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 106df02c6ffSLennert Buytenhek out: 107df02c6ffSLennert Buytenhek return err; 10891da11f8SLennert Buytenhek } 10991da11f8SLennert Buytenhek 11091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 11191da11f8SLennert Buytenhek { 112d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 113d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 114df02c6ffSLennert Buytenhek 1158640f8dcSRussell King dsa_port_disable_rt(dp); 1166457edfeSVivien Didelot 117df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 118a748ee24SJiri Pirko dev_uc_unsync(master, dev); 119df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 120df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 121df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 122df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 123df02c6ffSLennert Buytenhek 1248feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 125a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 126df02c6ffSLennert Buytenhek 12791da11f8SLennert Buytenhek return 0; 12891da11f8SLennert Buytenhek } 12991da11f8SLennert Buytenhek 13091da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 13191da11f8SLennert Buytenhek { 132d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 13317ab4f61SRundong Ge if (dev->flags & IFF_UP) { 13491da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 13517ab4f61SRundong Ge dev_set_allmulti(master, 13617ab4f61SRundong Ge dev->flags & IFF_ALLMULTI ? 1 : -1); 13791da11f8SLennert Buytenhek if (change & IFF_PROMISC) 13817ab4f61SRundong Ge dev_set_promiscuity(master, 13917ab4f61SRundong Ge dev->flags & IFF_PROMISC ? 1 : -1); 14017ab4f61SRundong Ge } 14191da11f8SLennert Buytenhek } 14291da11f8SLennert Buytenhek 14391da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 14491da11f8SLennert Buytenhek { 145d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 14691da11f8SLennert Buytenhek 14791da11f8SLennert Buytenhek dev_mc_sync(master, dev); 148a748ee24SJiri Pirko dev_uc_sync(master, dev); 14991da11f8SLennert Buytenhek } 15091da11f8SLennert Buytenhek 151df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 15291da11f8SLennert Buytenhek { 153d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 154df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 155df02c6ffSLennert Buytenhek int err; 156df02c6ffSLennert Buytenhek 157df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 158df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 159df02c6ffSLennert Buytenhek 160df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 161df02c6ffSLennert Buytenhek goto out; 162df02c6ffSLennert Buytenhek 1638feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 164a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 165df02c6ffSLennert Buytenhek if (err < 0) 166df02c6ffSLennert Buytenhek return err; 167df02c6ffSLennert Buytenhek } 168df02c6ffSLennert Buytenhek 1698feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 170a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 171df02c6ffSLennert Buytenhek 172df02c6ffSLennert Buytenhek out: 173d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 17491da11f8SLennert Buytenhek 17591da11f8SLennert Buytenhek return 0; 17691da11f8SLennert Buytenhek } 17791da11f8SLennert Buytenhek 1782bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx { 1792bedde1aSArkadi Sharshevsky struct net_device *dev; 1802bedde1aSArkadi Sharshevsky struct sk_buff *skb; 1812bedde1aSArkadi Sharshevsky struct netlink_callback *cb; 1822bedde1aSArkadi Sharshevsky int idx; 1832bedde1aSArkadi Sharshevsky }; 1842bedde1aSArkadi Sharshevsky 1852bedde1aSArkadi Sharshevsky static int 1862bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1872bedde1aSArkadi Sharshevsky bool is_static, void *data) 1882bedde1aSArkadi Sharshevsky { 1892bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx *dump = data; 1902bedde1aSArkadi Sharshevsky u32 portid = NETLINK_CB(dump->cb->skb).portid; 1912bedde1aSArkadi Sharshevsky u32 seq = dump->cb->nlh->nlmsg_seq; 1922bedde1aSArkadi Sharshevsky struct nlmsghdr *nlh; 1932bedde1aSArkadi Sharshevsky struct ndmsg *ndm; 1942bedde1aSArkadi Sharshevsky 1952bedde1aSArkadi Sharshevsky if (dump->idx < dump->cb->args[2]) 1962bedde1aSArkadi Sharshevsky goto skip; 1972bedde1aSArkadi Sharshevsky 1982bedde1aSArkadi Sharshevsky nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 1992bedde1aSArkadi Sharshevsky sizeof(*ndm), NLM_F_MULTI); 2002bedde1aSArkadi Sharshevsky if (!nlh) 2012bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2022bedde1aSArkadi Sharshevsky 2032bedde1aSArkadi Sharshevsky ndm = nlmsg_data(nlh); 2042bedde1aSArkadi Sharshevsky ndm->ndm_family = AF_BRIDGE; 2052bedde1aSArkadi Sharshevsky ndm->ndm_pad1 = 0; 2062bedde1aSArkadi Sharshevsky ndm->ndm_pad2 = 0; 2072bedde1aSArkadi Sharshevsky ndm->ndm_flags = NTF_SELF; 2082bedde1aSArkadi Sharshevsky ndm->ndm_type = 0; 2092bedde1aSArkadi Sharshevsky ndm->ndm_ifindex = dump->dev->ifindex; 2102bedde1aSArkadi Sharshevsky ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 2112bedde1aSArkadi Sharshevsky 2122bedde1aSArkadi Sharshevsky if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 2132bedde1aSArkadi Sharshevsky goto nla_put_failure; 2142bedde1aSArkadi Sharshevsky 2152bedde1aSArkadi Sharshevsky if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 2162bedde1aSArkadi Sharshevsky goto nla_put_failure; 2172bedde1aSArkadi Sharshevsky 2182bedde1aSArkadi Sharshevsky nlmsg_end(dump->skb, nlh); 2192bedde1aSArkadi Sharshevsky 2202bedde1aSArkadi Sharshevsky skip: 2212bedde1aSArkadi Sharshevsky dump->idx++; 2222bedde1aSArkadi Sharshevsky return 0; 2232bedde1aSArkadi Sharshevsky 2242bedde1aSArkadi Sharshevsky nla_put_failure: 2252bedde1aSArkadi Sharshevsky nlmsg_cancel(dump->skb, nlh); 2262bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2272bedde1aSArkadi Sharshevsky } 2282bedde1aSArkadi Sharshevsky 2292bedde1aSArkadi Sharshevsky static int 2302bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 2312bedde1aSArkadi Sharshevsky struct net_device *dev, struct net_device *filter_dev, 2322bedde1aSArkadi Sharshevsky int *idx) 2332bedde1aSArkadi Sharshevsky { 234d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 2352bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx dump = { 2362bedde1aSArkadi Sharshevsky .dev = dev, 2372bedde1aSArkadi Sharshevsky .skb = skb, 2382bedde1aSArkadi Sharshevsky .cb = cb, 2392bedde1aSArkadi Sharshevsky .idx = *idx, 2402bedde1aSArkadi Sharshevsky }; 2412bedde1aSArkadi Sharshevsky int err; 2422bedde1aSArkadi Sharshevsky 243de40fc5dSVivien Didelot err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 2442bedde1aSArkadi Sharshevsky *idx = dump.idx; 245de40fc5dSVivien Didelot 2462bedde1aSArkadi Sharshevsky return err; 2472bedde1aSArkadi Sharshevsky } 2482bedde1aSArkadi Sharshevsky 24991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 25091da11f8SLennert Buytenhek { 2510336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 2520336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 2530336369dSBrandon Streiff int port = p->dp->index; 2540336369dSBrandon Streiff 2550336369dSBrandon Streiff /* Pass through to switch driver if it supports timestamping */ 2560336369dSBrandon Streiff switch (cmd) { 2570336369dSBrandon Streiff case SIOCGHWTSTAMP: 2580336369dSBrandon Streiff if (ds->ops->port_hwtstamp_get) 2590336369dSBrandon Streiff return ds->ops->port_hwtstamp_get(ds, port, ifr); 2600336369dSBrandon Streiff break; 2610336369dSBrandon Streiff case SIOCSHWTSTAMP: 2620336369dSBrandon Streiff if (ds->ops->port_hwtstamp_set) 2630336369dSBrandon Streiff return ds->ops->port_hwtstamp_set(ds, port, ifr); 2640336369dSBrandon Streiff break; 2650336369dSBrandon Streiff } 2660336369dSBrandon Streiff 267aab9c406SFlorian Fainelli return phylink_mii_ioctl(p->dp->pl, ifr, cmd); 26891da11f8SLennert Buytenhek } 26991da11f8SLennert Buytenhek 27035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 271f7fadf30SJiri Pirko const struct switchdev_attr *attr, 2727ea6eb3fSJiri Pirko struct switchdev_trans *trans) 27335636062SScott Feldman { 274d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 275b8d866acSVivien Didelot int ret; 27635636062SScott Feldman 27735636062SScott Feldman switch (attr->id) { 2781f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 279fd364541SVivien Didelot ret = dsa_port_set_state(dp, attr->u.stp_state, trans); 28035636062SScott Feldman break; 281fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 282c02c4175SVivien Didelot ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 283c02c4175SVivien Didelot trans); 284fb2dabadSVivien Didelot break; 28534a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 286072bb190SVivien Didelot ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); 28734a79f63SVivien Didelot break; 288ea87005aSFlorian Fainelli case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 289ea87005aSFlorian Fainelli ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags, 290ea87005aSFlorian Fainelli trans); 291ea87005aSFlorian Fainelli break; 29257652796SRussell King case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 29357652796SRussell King ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans); 29457652796SRussell King break; 29508cc83ccSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER: 29608cc83ccSVivien Didelot ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans); 29708cc83ccSVivien Didelot break; 29835636062SScott Feldman default: 29935636062SScott Feldman ret = -EOPNOTSUPP; 30035636062SScott Feldman break; 30135636062SScott Feldman } 30235636062SScott Feldman 30335636062SScott Feldman return ret; 30435636062SScott Feldman } 30535636062SScott Feldman 306bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev, 307bdcff080SVivien Didelot const struct switchdev_obj *obj, 308bdcff080SVivien Didelot struct switchdev_trans *trans) 309bdcff080SVivien Didelot { 310bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 311bdcff080SVivien Didelot struct switchdev_obj_port_vlan vlan; 312bdcff080SVivien Didelot int err; 313bdcff080SVivien Didelot 314bdcff080SVivien Didelot if (obj->orig_dev != dev) 315bdcff080SVivien Didelot return -EOPNOTSUPP; 316bdcff080SVivien Didelot 317c5335d73SVivien Didelot if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev)) 318c5335d73SVivien Didelot return 0; 319c5335d73SVivien Didelot 320bdcff080SVivien Didelot vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj); 321bdcff080SVivien Didelot 322bdcff080SVivien Didelot err = dsa_port_vlan_add(dp, &vlan, trans); 323bdcff080SVivien Didelot if (err) 324bdcff080SVivien Didelot return err; 325bdcff080SVivien Didelot 326b9499904SVivien Didelot /* We need the dedicated CPU port to be a member of the VLAN as well. 327b9499904SVivien Didelot * Even though drivers often handle CPU membership in special ways, 328b9499904SVivien Didelot * it doesn't make sense to program a PVID, so clear this flag. 329b9499904SVivien Didelot */ 330b9499904SVivien Didelot vlan.flags &= ~BRIDGE_VLAN_INFO_PVID; 331b9499904SVivien Didelot 3327e1741b4SVivien Didelot err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans); 3337e1741b4SVivien Didelot if (err) 3347e1741b4SVivien Didelot return err; 3357e1741b4SVivien Didelot 336bdcff080SVivien Didelot return 0; 337bdcff080SVivien Didelot } 338bdcff080SVivien Didelot 339ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 340648b4a99SJiri Pirko const struct switchdev_obj *obj, 34179b139f4SVivien Didelot struct switchdev_trans *trans, 34279b139f4SVivien Didelot struct netlink_ext_ack *extack) 343ba14d9ebSVivien Didelot { 344d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 345ba14d9ebSVivien Didelot int err; 346ba14d9ebSVivien Didelot 347ba14d9ebSVivien Didelot /* For the prepare phase, ensure the full set of changes is feasable in 348ba14d9ebSVivien Didelot * one go in order to signal a failure properly. If an operation is not 349ba14d9ebSVivien Didelot * supported, return -EOPNOTSUPP. 350ba14d9ebSVivien Didelot */ 351ba14d9ebSVivien Didelot 3529e8f4a54SJiri Pirko switch (obj->id) { 3538df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 35479b139f4SVivien Didelot if (obj->orig_dev != dev) 35579b139f4SVivien Didelot return -EOPNOTSUPP; 356bcebb976SVivien Didelot err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); 3578df30255SVivien Didelot break; 3585f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3595f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3605f4dbc50SAndrew Lunn * but on the CPU port. 3615f4dbc50SAndrew Lunn */ 3625f4dbc50SAndrew Lunn err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj), 3635f4dbc50SAndrew Lunn trans); 3645f4dbc50SAndrew Lunn break; 36557d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 366bdcff080SVivien Didelot err = dsa_slave_vlan_add(dev, obj, trans); 36711149536SVivien Didelot break; 368ba14d9ebSVivien Didelot default: 369ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 370ba14d9ebSVivien Didelot break; 371ba14d9ebSVivien Didelot } 372ba14d9ebSVivien Didelot 373ba14d9ebSVivien Didelot return err; 374ba14d9ebSVivien Didelot } 375ba14d9ebSVivien Didelot 376bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev, 377bdcff080SVivien Didelot const struct switchdev_obj *obj) 378bdcff080SVivien Didelot { 379bdcff080SVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 380bdcff080SVivien Didelot 381bdcff080SVivien Didelot if (obj->orig_dev != dev) 382bdcff080SVivien Didelot return -EOPNOTSUPP; 383bdcff080SVivien Didelot 384c5335d73SVivien Didelot if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev)) 385c5335d73SVivien Didelot return 0; 386c5335d73SVivien Didelot 3877e1741b4SVivien Didelot /* Do not deprogram the CPU port as it may be shared with other user 3887e1741b4SVivien Didelot * ports which can be members of this VLAN as well. 3897e1741b4SVivien Didelot */ 390bdcff080SVivien Didelot return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); 391bdcff080SVivien Didelot } 392bdcff080SVivien Didelot 393ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 394648b4a99SJiri Pirko const struct switchdev_obj *obj) 395ba14d9ebSVivien Didelot { 396d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 397ba14d9ebSVivien Didelot int err; 398ba14d9ebSVivien Didelot 3999e8f4a54SJiri Pirko switch (obj->id) { 4008df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 40179b139f4SVivien Didelot if (obj->orig_dev != dev) 40279b139f4SVivien Didelot return -EOPNOTSUPP; 403bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 4048df30255SVivien Didelot break; 4055f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 4065f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 4075f4dbc50SAndrew Lunn * but on the CPU port. 4085f4dbc50SAndrew Lunn */ 4095f4dbc50SAndrew Lunn err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 4105f4dbc50SAndrew Lunn break; 41157d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 412bdcff080SVivien Didelot err = dsa_slave_vlan_del(dev, obj); 41311149536SVivien Didelot break; 414ba14d9ebSVivien Didelot default: 415ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 416ba14d9ebSVivien Didelot break; 417ba14d9ebSVivien Didelot } 418ba14d9ebSVivien Didelot 419ba14d9ebSVivien Didelot return err; 420ba14d9ebSVivien Didelot } 421ba14d9ebSVivien Didelot 422929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev, 423929d6c14SFlorian Fainelli struct netdev_phys_item_id *ppid) 424b73adef6SFlorian Fainelli { 425d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 426d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 427a42c8e33SAndrew Lunn struct dsa_switch_tree *dst = ds->dst; 428b73adef6SFlorian Fainelli 42915b04aceSJiri Pirko /* For non-legacy ports, devlink is used and it takes 43015b04aceSJiri Pirko * care of the name generation. This ndo implementation 43115b04aceSJiri Pirko * should be removed with legacy support. 43215b04aceSJiri Pirko */ 43315b04aceSJiri Pirko if (dp->ds->devlink) 43415b04aceSJiri Pirko return -EOPNOTSUPP; 43515b04aceSJiri Pirko 436929d6c14SFlorian Fainelli ppid->id_len = sizeof(dst->index); 437929d6c14SFlorian Fainelli memcpy(&ppid->id, &dst->index, ppid->id_len); 438929d6c14SFlorian Fainelli 439929d6c14SFlorian Fainelli return 0; 440929d6c14SFlorian Fainelli } 441929d6c14SFlorian Fainelli 4424fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 44304ff53f9SFlorian Fainelli struct sk_buff *skb) 44404ff53f9SFlorian Fainelli { 44504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 4464fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 4474fa7b718SVivien Didelot 44804ff53f9SFlorian Fainelli if (p->netpoll) 44904ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 45004ff53f9SFlorian Fainelli #else 45104ff53f9SFlorian Fainelli BUG(); 45204ff53f9SFlorian Fainelli #endif 45304ff53f9SFlorian Fainelli return NETDEV_TX_OK; 45404ff53f9SFlorian Fainelli } 45504ff53f9SFlorian Fainelli 45690af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 45790af1059SBrandon Streiff struct sk_buff *skb) 45890af1059SBrandon Streiff { 45990af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 46090af1059SBrandon Streiff struct sk_buff *clone; 46190af1059SBrandon Streiff unsigned int type; 46290af1059SBrandon Streiff 46390af1059SBrandon Streiff type = ptp_classify_raw(skb); 46490af1059SBrandon Streiff if (type == PTP_CLASS_NONE) 46590af1059SBrandon Streiff return; 46690af1059SBrandon Streiff 46790af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 46890af1059SBrandon Streiff return; 46990af1059SBrandon Streiff 47090af1059SBrandon Streiff clone = skb_clone_sk(skb); 47190af1059SBrandon Streiff if (!clone) 47290af1059SBrandon Streiff return; 47390af1059SBrandon Streiff 474146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = clone; 475146d442cSVladimir Oltean 47690af1059SBrandon Streiff if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) 47790af1059SBrandon Streiff return; 47890af1059SBrandon Streiff 47990af1059SBrandon Streiff kfree_skb(clone); 48090af1059SBrandon Streiff } 48190af1059SBrandon Streiff 48297a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev) 48397a69a0dSVladimir Oltean { 48497a69a0dSVladimir Oltean /* SKB for netpoll still need to be mangled with the protocol-specific 48597a69a0dSVladimir Oltean * tag to be successfully transmitted 48697a69a0dSVladimir Oltean */ 48797a69a0dSVladimir Oltean if (unlikely(netpoll_tx_running(dev))) 48897a69a0dSVladimir Oltean return dsa_slave_netpoll_send_skb(dev, skb); 48997a69a0dSVladimir Oltean 49097a69a0dSVladimir Oltean /* Queue the SKB for transmission on the parent interface, but 49197a69a0dSVladimir Oltean * do not modify its EtherType 49297a69a0dSVladimir Oltean */ 49397a69a0dSVladimir Oltean skb->dev = dsa_slave_to_master(dev); 49497a69a0dSVladimir Oltean dev_queue_xmit(skb); 49597a69a0dSVladimir Oltean 49697a69a0dSVladimir Oltean return NETDEV_TX_OK; 49797a69a0dSVladimir Oltean } 49897a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb); 49997a69a0dSVladimir Oltean 5003e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 5013e8a72d1SFlorian Fainelli { 5023e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 5035f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 5044ed70ce9SFlorian Fainelli struct sk_buff *nskb; 5053e8a72d1SFlorian Fainelli 5065f6b4e14SFlorian Fainelli s = this_cpu_ptr(p->stats64); 5075f6b4e14SFlorian Fainelli u64_stats_update_begin(&s->syncp); 5085f6b4e14SFlorian Fainelli s->tx_packets++; 5095f6b4e14SFlorian Fainelli s->tx_bytes += skb->len; 5105f6b4e14SFlorian Fainelli u64_stats_update_end(&s->syncp); 5113e8a72d1SFlorian Fainelli 512146d442cSVladimir Oltean DSA_SKB_CB(skb)->clone = NULL; 51387671375SVladimir Oltean 51490af1059SBrandon Streiff /* Identify PTP protocol packets, clone them, and pass them to the 51590af1059SBrandon Streiff * switch driver 51690af1059SBrandon Streiff */ 51790af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 51890af1059SBrandon Streiff 519fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 520fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 521fe47d563SVivien Didelot */ 5224ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 523fe47d563SVivien Didelot if (!nskb) { 524fe47d563SVivien Didelot kfree_skb(skb); 5254ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 526fe47d563SVivien Didelot } 5275aed85ceSFlorian Fainelli 52897a69a0dSVladimir Oltean return dsa_enqueue_skb(nskb, dev); 52997a69a0dSVladimir Oltean } 53004ff53f9SFlorian Fainelli 53191da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 53291da11f8SLennert Buytenhek 53391da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 53491da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 53591da11f8SLennert Buytenhek { 5367826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 5377826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 5387826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 53991da11f8SLennert Buytenhek } 54091da11f8SLennert Buytenhek 5413d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 5423d762a0fSGuenter Roeck { 543d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 544d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5453d762a0fSGuenter Roeck 5469d490b4eSVivien Didelot if (ds->ops->get_regs_len) 547d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 5483d762a0fSGuenter Roeck 5493d762a0fSGuenter Roeck return -EOPNOTSUPP; 5503d762a0fSGuenter Roeck } 5513d762a0fSGuenter Roeck 5523d762a0fSGuenter Roeck static void 5533d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 5543d762a0fSGuenter Roeck { 555d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 556d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5573d762a0fSGuenter Roeck 5589d490b4eSVivien Didelot if (ds->ops->get_regs) 559d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 5603d762a0fSGuenter Roeck } 5613d762a0fSGuenter Roeck 562aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev) 563aab9c406SFlorian Fainelli { 564aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 565aab9c406SFlorian Fainelli 566aab9c406SFlorian Fainelli return phylink_ethtool_nway_reset(dp->pl); 567aab9c406SFlorian Fainelli } 568aab9c406SFlorian Fainelli 5696793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 5706793abb4SGuenter Roeck { 571d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 572d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5736793abb4SGuenter Roeck 5740e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 575ff04955cSAndrew Lunn return ds->cd->eeprom_len; 5766793abb4SGuenter Roeck 5779d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 5789d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 5796793abb4SGuenter Roeck 5806793abb4SGuenter Roeck return 0; 5816793abb4SGuenter Roeck } 5826793abb4SGuenter Roeck 5836793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 5846793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5856793abb4SGuenter Roeck { 586d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 587d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5886793abb4SGuenter Roeck 5899d490b4eSVivien Didelot if (ds->ops->get_eeprom) 5909d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 5916793abb4SGuenter Roeck 5926793abb4SGuenter Roeck return -EOPNOTSUPP; 5936793abb4SGuenter Roeck } 5946793abb4SGuenter Roeck 5956793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 5966793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5976793abb4SGuenter Roeck { 598d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 599d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6006793abb4SGuenter Roeck 6019d490b4eSVivien Didelot if (ds->ops->set_eeprom) 6029d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 6036793abb4SGuenter Roeck 6046793abb4SGuenter Roeck return -EOPNOTSUPP; 6056793abb4SGuenter Roeck } 6066793abb4SGuenter Roeck 60791da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 60891da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 60991da11f8SLennert Buytenhek { 610d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 611d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 61291da11f8SLennert Buytenhek 61391da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 61491da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 61591da11f8SLennert Buytenhek 61691da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 61791da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 61891da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 61991da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 6209d490b4eSVivien Didelot if (ds->ops->get_strings) 62189f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 62289f09048SFlorian Fainelli data + 4 * len); 62391da11f8SLennert Buytenhek } 62491da11f8SLennert Buytenhek } 62591da11f8SLennert Buytenhek 62691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 62791da11f8SLennert Buytenhek struct ethtool_stats *stats, 62891da11f8SLennert Buytenhek uint64_t *data) 62991da11f8SLennert Buytenhek { 630d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 63191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 632d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6335f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 634f613ed66SFlorian Fainelli unsigned int start; 6355f6b4e14SFlorian Fainelli int i; 63691da11f8SLennert Buytenhek 6375f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 6385f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 6395f6b4e14SFlorian Fainelli 6405f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 641f613ed66SFlorian Fainelli do { 6425f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 6435f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 6445f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 6455f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 6465f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 6475f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 6485f6b4e14SFlorian Fainelli data[0] += tx_packets; 6495f6b4e14SFlorian Fainelli data[1] += tx_bytes; 6505f6b4e14SFlorian Fainelli data[2] += rx_packets; 6515f6b4e14SFlorian Fainelli data[3] += rx_bytes; 6525f6b4e14SFlorian Fainelli } 6539d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 654d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 65591da11f8SLennert Buytenhek } 65691da11f8SLennert Buytenhek 65791da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 65891da11f8SLennert Buytenhek { 659d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 660d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 66191da11f8SLennert Buytenhek 66291da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 66391da11f8SLennert Buytenhek int count; 66491da11f8SLennert Buytenhek 66591da11f8SLennert Buytenhek count = 4; 6669d490b4eSVivien Didelot if (ds->ops->get_sset_count) 66789f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, dp->index, sset); 66891da11f8SLennert Buytenhek 66991da11f8SLennert Buytenhek return count; 67091da11f8SLennert Buytenhek } 67191da11f8SLennert Buytenhek 67291da11f8SLennert Buytenhek return -EOPNOTSUPP; 67391da11f8SLennert Buytenhek } 67491da11f8SLennert Buytenhek 67519e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 67619e57c4eSFlorian Fainelli { 677d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 678d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 67919e57c4eSFlorian Fainelli 680aab9c406SFlorian Fainelli phylink_ethtool_get_wol(dp->pl, w); 681aab9c406SFlorian Fainelli 6829d490b4eSVivien Didelot if (ds->ops->get_wol) 683d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 68419e57c4eSFlorian Fainelli } 68519e57c4eSFlorian Fainelli 68619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 68719e57c4eSFlorian Fainelli { 688d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 689d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 69019e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 69119e57c4eSFlorian Fainelli 692aab9c406SFlorian Fainelli phylink_ethtool_set_wol(dp->pl, w); 693aab9c406SFlorian Fainelli 6949d490b4eSVivien Didelot if (ds->ops->set_wol) 695d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 69619e57c4eSFlorian Fainelli 69719e57c4eSFlorian Fainelli return ret; 69819e57c4eSFlorian Fainelli } 69919e57c4eSFlorian Fainelli 7007905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 7017905288fSFlorian Fainelli { 702d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 703d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7047905288fSFlorian Fainelli int ret; 7057905288fSFlorian Fainelli 7067b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 70700670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 7087b9cc738SVivien Didelot return -ENODEV; 7097b9cc738SVivien Didelot 71008f50061SVivien Didelot if (!ds->ops->set_mac_eee) 7117905288fSFlorian Fainelli return -EOPNOTSUPP; 7127905288fSFlorian Fainelli 713d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 7147905288fSFlorian Fainelli if (ret) 7157905288fSFlorian Fainelli return ret; 7167905288fSFlorian Fainelli 717aab9c406SFlorian Fainelli return phylink_ethtool_set_eee(dp->pl, e); 7187905288fSFlorian Fainelli } 7197905288fSFlorian Fainelli 7207905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 7217905288fSFlorian Fainelli { 722d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 723d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 7247905288fSFlorian Fainelli int ret; 7257905288fSFlorian Fainelli 7267b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 72700670cb8SDan Carpenter if (!dev->phydev || !dp->pl) 7287b9cc738SVivien Didelot return -ENODEV; 7297b9cc738SVivien Didelot 73008f50061SVivien Didelot if (!ds->ops->get_mac_eee) 7317905288fSFlorian Fainelli return -EOPNOTSUPP; 7327905288fSFlorian Fainelli 733d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 7347905288fSFlorian Fainelli if (ret) 7357905288fSFlorian Fainelli return ret; 7367905288fSFlorian Fainelli 737aab9c406SFlorian Fainelli return phylink_ethtool_get_eee(dp->pl, e); 738aab9c406SFlorian Fainelli } 739aab9c406SFlorian Fainelli 740aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev, 741aab9c406SFlorian Fainelli struct ethtool_link_ksettings *cmd) 742aab9c406SFlorian Fainelli { 743aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 744aab9c406SFlorian Fainelli 745aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_get(dp->pl, cmd); 746aab9c406SFlorian Fainelli } 747aab9c406SFlorian Fainelli 748aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev, 749aab9c406SFlorian Fainelli const struct ethtool_link_ksettings *cmd) 750aab9c406SFlorian Fainelli { 751aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 752aab9c406SFlorian Fainelli 753aab9c406SFlorian Fainelli return phylink_ethtool_ksettings_set(dp->pl, cmd); 7547905288fSFlorian Fainelli } 7557905288fSFlorian Fainelli 756a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev, 757a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 758a2a1a13bSHeiner Kallweit { 759a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 760a2a1a13bSHeiner Kallweit 761a2a1a13bSHeiner Kallweit phylink_ethtool_get_pauseparam(dp->pl, pause); 762a2a1a13bSHeiner Kallweit } 763a2a1a13bSHeiner Kallweit 764a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev, 765a2a1a13bSHeiner Kallweit struct ethtool_pauseparam *pause) 766a2a1a13bSHeiner Kallweit { 767a2a1a13bSHeiner Kallweit struct dsa_port *dp = dsa_slave_to_port(dev); 768a2a1a13bSHeiner Kallweit 769a2a1a13bSHeiner Kallweit return phylink_ethtool_set_pauseparam(dp->pl, pause); 770a2a1a13bSHeiner Kallweit } 771a2a1a13bSHeiner Kallweit 77204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 77304ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 77404ff53f9SFlorian Fainelli struct netpoll_info *ni) 77504ff53f9SFlorian Fainelli { 776d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 77704ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 77804ff53f9SFlorian Fainelli struct netpoll *netpoll; 77904ff53f9SFlorian Fainelli int err = 0; 78004ff53f9SFlorian Fainelli 78104ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 78204ff53f9SFlorian Fainelli if (!netpoll) 78304ff53f9SFlorian Fainelli return -ENOMEM; 78404ff53f9SFlorian Fainelli 78504ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 78604ff53f9SFlorian Fainelli if (err) { 78704ff53f9SFlorian Fainelli kfree(netpoll); 78804ff53f9SFlorian Fainelli goto out; 78904ff53f9SFlorian Fainelli } 79004ff53f9SFlorian Fainelli 79104ff53f9SFlorian Fainelli p->netpoll = netpoll; 79204ff53f9SFlorian Fainelli out: 79304ff53f9SFlorian Fainelli return err; 79404ff53f9SFlorian Fainelli } 79504ff53f9SFlorian Fainelli 79604ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 79704ff53f9SFlorian Fainelli { 79804ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 79904ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 80004ff53f9SFlorian Fainelli 80104ff53f9SFlorian Fainelli if (!netpoll) 80204ff53f9SFlorian Fainelli return; 80304ff53f9SFlorian Fainelli 80404ff53f9SFlorian Fainelli p->netpoll = NULL; 80504ff53f9SFlorian Fainelli 806c9fbd71fSDebabrata Banerjee __netpoll_free(netpoll); 80704ff53f9SFlorian Fainelli } 80804ff53f9SFlorian Fainelli 80904ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 81004ff53f9SFlorian Fainelli { 81104ff53f9SFlorian Fainelli } 81204ff53f9SFlorian Fainelli #endif 81304ff53f9SFlorian Fainelli 81444bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 81544bb765cSFlorian Fainelli char *name, size_t len) 81644bb765cSFlorian Fainelli { 817d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 81844bb765cSFlorian Fainelli 819d484210bSJiri Pirko /* For non-legacy ports, devlink is used and it takes 820d484210bSJiri Pirko * care of the name generation. This ndo implementation 821d484210bSJiri Pirko * should be removed with legacy support. 822d484210bSJiri Pirko */ 823d484210bSJiri Pirko if (dp->ds->devlink) 824d484210bSJiri Pirko return -EOPNOTSUPP; 825d484210bSJiri Pirko 826d945097bSVivien Didelot if (snprintf(name, len, "p%d", dp->index) >= len) 82744bb765cSFlorian Fainelli return -EINVAL; 8283a543ef4SFlorian Fainelli 8293a543ef4SFlorian Fainelli return 0; 8303a543ef4SFlorian Fainelli } 8313a543ef4SFlorian Fainelli 832f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 8334fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 834f50f2127SFlorian Fainelli { 8354fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 836f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 837f50f2127SFlorian Fainelli 838f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 839f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 840f50f2127SFlorian Fainelli return mall_tc_entry; 841f50f2127SFlorian Fainelli 842f50f2127SFlorian Fainelli return NULL; 843f50f2127SFlorian Fainelli } 844f50f2127SFlorian Fainelli 845f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev, 846f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 847f50f2127SFlorian Fainelli bool ingress) 848f50f2127SFlorian Fainelli { 849d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 850f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 851f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 8525fd9fc4eSJiri Pirko __be16 protocol = cls->common.protocol; 853d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 8549681e8b3SPieter Jansen van Vuuren struct flow_action_entry *act; 855d945097bSVivien Didelot struct dsa_port *to_dp; 856f50f2127SFlorian Fainelli int err = -EOPNOTSUPP; 857f50f2127SFlorian Fainelli 858f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 859f50f2127SFlorian Fainelli return err; 860f50f2127SFlorian Fainelli 8619681e8b3SPieter Jansen van Vuuren if (!flow_offload_has_one_action(&cls->rule->action)) 862f50f2127SFlorian Fainelli return err; 863f50f2127SFlorian Fainelli 86453eca1f3SJakub Kicinski if (!flow_action_basic_hw_stats_check(&cls->rule->action, 865319a1d19SJiri Pirko cls->common.extack)) 866319a1d19SJiri Pirko return err; 867319a1d19SJiri Pirko 8689681e8b3SPieter Jansen van Vuuren act = &cls->rule->action.entries[0]; 869f50f2127SFlorian Fainelli 8709681e8b3SPieter Jansen van Vuuren if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) { 871f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror; 872f50f2127SFlorian Fainelli 8739681e8b3SPieter Jansen van Vuuren if (!act->dev) 874f50f2127SFlorian Fainelli return -EINVAL; 875f50f2127SFlorian Fainelli 8769681e8b3SPieter Jansen van Vuuren if (!dsa_slave_dev_check(act->dev)) 877f50f2127SFlorian Fainelli return -EOPNOTSUPP; 878f50f2127SFlorian Fainelli 879f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 880f50f2127SFlorian Fainelli if (!mall_tc_entry) 881f50f2127SFlorian Fainelli return -ENOMEM; 882f50f2127SFlorian Fainelli 883f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 884f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 885f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 886f50f2127SFlorian Fainelli 8879681e8b3SPieter Jansen van Vuuren to_dp = dsa_slave_to_port(act->dev); 888f50f2127SFlorian Fainelli 889d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 890f50f2127SFlorian Fainelli mirror->ingress = ingress; 891f50f2127SFlorian Fainelli 892d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 893f50f2127SFlorian Fainelli if (err) { 894f50f2127SFlorian Fainelli kfree(mall_tc_entry); 895f50f2127SFlorian Fainelli return err; 896f50f2127SFlorian Fainelli } 897f50f2127SFlorian Fainelli 898f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 899f50f2127SFlorian Fainelli } 900f50f2127SFlorian Fainelli 901f50f2127SFlorian Fainelli return 0; 902f50f2127SFlorian Fainelli } 903f50f2127SFlorian Fainelli 904f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 905f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 906f50f2127SFlorian Fainelli { 907d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 908f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 909d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 910f50f2127SFlorian Fainelli 911f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_del) 912f50f2127SFlorian Fainelli return; 913f50f2127SFlorian Fainelli 9144fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 915f50f2127SFlorian Fainelli if (!mall_tc_entry) 916f50f2127SFlorian Fainelli return; 917f50f2127SFlorian Fainelli 918f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 919f50f2127SFlorian Fainelli 920f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 921f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 922d945097bSVivien Didelot ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror); 923f50f2127SFlorian Fainelli break; 924f50f2127SFlorian Fainelli default: 925f50f2127SFlorian Fainelli WARN_ON(1); 926f50f2127SFlorian Fainelli } 927f50f2127SFlorian Fainelli 928f50f2127SFlorian Fainelli kfree(mall_tc_entry); 929f50f2127SFlorian Fainelli } 930f50f2127SFlorian Fainelli 9313fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 9326b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 9336b3eb752SJiri Pirko bool ingress) 934f50f2127SFlorian Fainelli { 9355fd9fc4eSJiri Pirko if (cls->common.chain_index) 936a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 937f50f2127SFlorian Fainelli 9383fbae382SJiri Pirko switch (cls->command) { 9393fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 9405fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 9413fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 9423fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 9433fbae382SJiri Pirko return 0; 9443fbae382SJiri Pirko default: 9453fbae382SJiri Pirko return -EOPNOTSUPP; 9463fbae382SJiri Pirko } 9473fbae382SJiri Pirko } 9483fbae382SJiri Pirko 949ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev, 950ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 951ed11bb1fSVladimir Oltean bool ingress) 952ed11bb1fSVladimir Oltean { 953ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 954ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 955ed11bb1fSVladimir Oltean int port = dp->index; 956ed11bb1fSVladimir Oltean 957ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_add) 958ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 959ed11bb1fSVladimir Oltean 960ed11bb1fSVladimir Oltean return ds->ops->cls_flower_add(ds, port, cls, ingress); 961ed11bb1fSVladimir Oltean } 962ed11bb1fSVladimir Oltean 963ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev, 964ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 965ed11bb1fSVladimir Oltean bool ingress) 966ed11bb1fSVladimir Oltean { 967ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 968ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 969ed11bb1fSVladimir Oltean int port = dp->index; 970ed11bb1fSVladimir Oltean 971ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_del) 972ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 973ed11bb1fSVladimir Oltean 974ed11bb1fSVladimir Oltean return ds->ops->cls_flower_del(ds, port, cls, ingress); 975ed11bb1fSVladimir Oltean } 976ed11bb1fSVladimir Oltean 977ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev, 978ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 979ed11bb1fSVladimir Oltean bool ingress) 980ed11bb1fSVladimir Oltean { 981ed11bb1fSVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 982ed11bb1fSVladimir Oltean struct dsa_switch *ds = dp->ds; 983ed11bb1fSVladimir Oltean int port = dp->index; 984ed11bb1fSVladimir Oltean 985ed11bb1fSVladimir Oltean if (!ds->ops->cls_flower_stats) 986ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 987ed11bb1fSVladimir Oltean 988ed11bb1fSVladimir Oltean return ds->ops->cls_flower_stats(ds, port, cls, ingress); 989ed11bb1fSVladimir Oltean } 990ed11bb1fSVladimir Oltean 991ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev, 992ed11bb1fSVladimir Oltean struct flow_cls_offload *cls, 993ed11bb1fSVladimir Oltean bool ingress) 994ed11bb1fSVladimir Oltean { 995ed11bb1fSVladimir Oltean switch (cls->command) { 996ed11bb1fSVladimir Oltean case FLOW_CLS_REPLACE: 997ed11bb1fSVladimir Oltean return dsa_slave_add_cls_flower(dev, cls, ingress); 998ed11bb1fSVladimir Oltean case FLOW_CLS_DESTROY: 999ed11bb1fSVladimir Oltean return dsa_slave_del_cls_flower(dev, cls, ingress); 1000ed11bb1fSVladimir Oltean case FLOW_CLS_STATS: 1001ed11bb1fSVladimir Oltean return dsa_slave_stats_cls_flower(dev, cls, ingress); 1002ed11bb1fSVladimir Oltean default: 1003ed11bb1fSVladimir Oltean return -EOPNOTSUPP; 1004ed11bb1fSVladimir Oltean } 1005ed11bb1fSVladimir Oltean } 1006ed11bb1fSVladimir Oltean 10076b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 10086b3eb752SJiri Pirko void *cb_priv, bool ingress) 10096b3eb752SJiri Pirko { 10106b3eb752SJiri Pirko struct net_device *dev = cb_priv; 10116b3eb752SJiri Pirko 101244ae12a7SJiri Pirko if (!tc_can_offload(dev)) 101344ae12a7SJiri Pirko return -EOPNOTSUPP; 101444ae12a7SJiri Pirko 10156b3eb752SJiri Pirko switch (type) { 10166b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 10176b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 1018ed11bb1fSVladimir Oltean case TC_SETUP_CLSFLOWER: 1019ed11bb1fSVladimir Oltean return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress); 10206b3eb752SJiri Pirko default: 10216b3eb752SJiri Pirko return -EOPNOTSUPP; 10226b3eb752SJiri Pirko } 10236b3eb752SJiri Pirko } 10246b3eb752SJiri Pirko 10256b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 10266b3eb752SJiri Pirko void *type_data, void *cb_priv) 10276b3eb752SJiri Pirko { 10286b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 10296b3eb752SJiri Pirko } 10306b3eb752SJiri Pirko 10316b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 10326b3eb752SJiri Pirko void *type_data, void *cb_priv) 10336b3eb752SJiri Pirko { 10346b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 10356b3eb752SJiri Pirko } 10366b3eb752SJiri Pirko 1037955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list); 1038955bcb6eSPablo Neira Ayuso 10396b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 1040955bcb6eSPablo Neira Ayuso struct flow_block_offload *f) 10416b3eb752SJiri Pirko { 1042955bcb6eSPablo Neira Ayuso struct flow_block_cb *block_cb; 1043a7323311SPablo Neira Ayuso flow_setup_cb_t *cb; 10446b3eb752SJiri Pirko 104532f8c409SPablo Neira Ayuso if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 10466b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 104732f8c409SPablo Neira Ayuso else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 10486b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 10496b3eb752SJiri Pirko else 10506b3eb752SJiri Pirko return -EOPNOTSUPP; 10516b3eb752SJiri Pirko 1052955bcb6eSPablo Neira Ayuso f->driver_block_list = &dsa_slave_block_cb_list; 1053955bcb6eSPablo Neira Ayuso 10546b3eb752SJiri Pirko switch (f->command) { 10559c0e189eSPablo Neira Ayuso case FLOW_BLOCK_BIND: 10560d4fd02eSPablo Neira Ayuso if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list)) 10570d4fd02eSPablo Neira Ayuso return -EBUSY; 10580d4fd02eSPablo Neira Ayuso 10590c7294ddSPablo Neira Ayuso block_cb = flow_block_cb_alloc(cb, dev, dev, NULL); 1060955bcb6eSPablo Neira Ayuso if (IS_ERR(block_cb)) 1061955bcb6eSPablo Neira Ayuso return PTR_ERR(block_cb); 1062955bcb6eSPablo Neira Ayuso 1063955bcb6eSPablo Neira Ayuso flow_block_cb_add(block_cb, f); 1064955bcb6eSPablo Neira Ayuso list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list); 1065955bcb6eSPablo Neira Ayuso return 0; 10669c0e189eSPablo Neira Ayuso case FLOW_BLOCK_UNBIND: 106714bfb13fSPablo Neira Ayuso block_cb = flow_block_cb_lookup(f->block, cb, dev); 1068955bcb6eSPablo Neira Ayuso if (!block_cb) 1069955bcb6eSPablo Neira Ayuso return -ENOENT; 1070955bcb6eSPablo Neira Ayuso 1071955bcb6eSPablo Neira Ayuso flow_block_cb_remove(block_cb, f); 1072955bcb6eSPablo Neira Ayuso list_del(&block_cb->driver_list); 10736b3eb752SJiri Pirko return 0; 10746b3eb752SJiri Pirko default: 10756b3eb752SJiri Pirko return -EOPNOTSUPP; 10766b3eb752SJiri Pirko } 10776b3eb752SJiri Pirko } 10786b3eb752SJiri Pirko 10793fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 1080de4784caSJiri Pirko void *type_data) 10813fbae382SJiri Pirko { 108247d23af2SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 108347d23af2SVladimir Oltean struct dsa_switch *ds = dp->ds; 108447d23af2SVladimir Oltean 108547d23af2SVladimir Oltean if (type == TC_SETUP_BLOCK) 10866b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 108747d23af2SVladimir Oltean 108847d23af2SVladimir Oltean if (!ds->ops->port_setup_tc) 1089a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 109047d23af2SVladimir Oltean 109147d23af2SVladimir Oltean return ds->ops->port_setup_tc(ds, dp->index, type, type_data); 1092f50f2127SFlorian Fainelli } 1093f50f2127SFlorian Fainelli 1094f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev, 1095f613ed66SFlorian Fainelli struct rtnl_link_stats64 *stats) 1096f613ed66SFlorian Fainelli { 1097f613ed66SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 10985f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 1099f613ed66SFlorian Fainelli unsigned int start; 11005f6b4e14SFlorian Fainelli int i; 1101f613ed66SFlorian Fainelli 1102f613ed66SFlorian Fainelli netdev_stats_to_stats64(stats, &dev->stats); 11035f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 11045f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 11055f6b4e14SFlorian Fainelli 11065f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 1107f613ed66SFlorian Fainelli do { 11085f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 11095f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 11105f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 11115f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 11125f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 11135f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 11145f6b4e14SFlorian Fainelli 11155f6b4e14SFlorian Fainelli stats->tx_packets += tx_packets; 11165f6b4e14SFlorian Fainelli stats->tx_bytes += tx_bytes; 11175f6b4e14SFlorian Fainelli stats->rx_packets += rx_packets; 11185f6b4e14SFlorian Fainelli stats->rx_bytes += rx_bytes; 11195f6b4e14SFlorian Fainelli } 1120f613ed66SFlorian Fainelli } 1121f613ed66SFlorian Fainelli 1122bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 1123bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 1124bf9f2648SFlorian Fainelli { 1125d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1126d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1127bf9f2648SFlorian Fainelli 1128bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 1129bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1130bf9f2648SFlorian Fainelli 1131d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 1132bf9f2648SFlorian Fainelli } 1133bf9f2648SFlorian Fainelli 1134bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 1135bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 1136bf9f2648SFlorian Fainelli { 1137d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1138d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1139bf9f2648SFlorian Fainelli 1140bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 1141bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 1142bf9f2648SFlorian Fainelli 1143d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 1144bf9f2648SFlorian Fainelli } 1145bf9f2648SFlorian Fainelli 11460336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 11470336369dSBrandon Streiff struct ethtool_ts_info *ts) 11480336369dSBrandon Streiff { 11490336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 11500336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 11510336369dSBrandon Streiff 11520336369dSBrandon Streiff if (!ds->ops->get_ts_info) 11530336369dSBrandon Streiff return -EOPNOTSUPP; 11540336369dSBrandon Streiff 11550336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 11560336369dSBrandon Streiff } 11570336369dSBrandon Streiff 1158061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 1159061f6a50SFlorian Fainelli u16 vid) 1160061f6a50SFlorian Fainelli { 1161061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1162061f6a50SFlorian Fainelli struct bridge_vlan_info info; 1163061f6a50SFlorian Fainelli int ret; 1164061f6a50SFlorian Fainelli 1165061f6a50SFlorian Fainelli /* Check for a possible bridge VLAN entry now since there is no 1166061f6a50SFlorian Fainelli * need to emulate the switchdev prepare + commit phase. 1167061f6a50SFlorian Fainelli */ 1168061f6a50SFlorian Fainelli if (dp->bridge_dev) { 1169c5335d73SVivien Didelot if (!br_vlan_enabled(dp->bridge_dev)) 1170c5335d73SVivien Didelot return 0; 1171c5335d73SVivien Didelot 1172061f6a50SFlorian Fainelli /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 1173061f6a50SFlorian Fainelli * device, respectively the VID is not found, returning 1174061f6a50SFlorian Fainelli * 0 means success, which is a failure for us here. 1175061f6a50SFlorian Fainelli */ 1176061f6a50SFlorian Fainelli ret = br_vlan_get_info(dp->bridge_dev, vid, &info); 1177061f6a50SFlorian Fainelli if (ret == 0) 1178061f6a50SFlorian Fainelli return -EBUSY; 1179061f6a50SFlorian Fainelli } 1180061f6a50SFlorian Fainelli 1181cf360866SVivien Didelot ret = dsa_port_vid_add(dp, vid, 0); 11829b236d2aSVladimir Oltean if (ret) 1183cf360866SVivien Didelot return ret; 1184cf360866SVivien Didelot 11857e1741b4SVivien Didelot ret = dsa_port_vid_add(dp->cpu_dp, vid, 0); 11869b236d2aSVladimir Oltean if (ret) 11877e1741b4SVivien Didelot return ret; 11887e1741b4SVivien Didelot 1189cf360866SVivien Didelot return 0; 1190061f6a50SFlorian Fainelli } 1191061f6a50SFlorian Fainelli 1192061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 1193061f6a50SFlorian Fainelli u16 vid) 1194061f6a50SFlorian Fainelli { 1195061f6a50SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1196061f6a50SFlorian Fainelli struct bridge_vlan_info info; 1197061f6a50SFlorian Fainelli int ret; 1198061f6a50SFlorian Fainelli 1199061f6a50SFlorian Fainelli /* Check for a possible bridge VLAN entry now since there is no 1200061f6a50SFlorian Fainelli * need to emulate the switchdev prepare + commit phase. 1201061f6a50SFlorian Fainelli */ 1202061f6a50SFlorian Fainelli if (dp->bridge_dev) { 1203c5335d73SVivien Didelot if (!br_vlan_enabled(dp->bridge_dev)) 1204c5335d73SVivien Didelot return 0; 1205c5335d73SVivien Didelot 1206061f6a50SFlorian Fainelli /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 1207061f6a50SFlorian Fainelli * device, respectively the VID is not found, returning 1208061f6a50SFlorian Fainelli * 0 means success, which is a failure for us here. 1209061f6a50SFlorian Fainelli */ 1210061f6a50SFlorian Fainelli ret = br_vlan_get_info(dp->bridge_dev, vid, &info); 1211061f6a50SFlorian Fainelli if (ret == 0) 1212061f6a50SFlorian Fainelli return -EBUSY; 1213061f6a50SFlorian Fainelli } 1214061f6a50SFlorian Fainelli 12157e1741b4SVivien Didelot /* Do not deprogram the CPU port as it may be shared with other user 12167e1741b4SVivien Didelot * ports which can be members of this VLAN as well. 12177e1741b4SVivien Didelot */ 12189b236d2aSVladimir Oltean return dsa_port_vid_del(dp, vid); 1219061f6a50SFlorian Fainelli } 1220061f6a50SFlorian Fainelli 1221*bff33f7eSVladimir Oltean struct dsa_hw_port { 1222*bff33f7eSVladimir Oltean struct list_head list; 1223*bff33f7eSVladimir Oltean struct net_device *dev; 1224*bff33f7eSVladimir Oltean int old_mtu; 1225*bff33f7eSVladimir Oltean }; 1226*bff33f7eSVladimir Oltean 1227*bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu) 1228*bff33f7eSVladimir Oltean { 1229*bff33f7eSVladimir Oltean const struct dsa_hw_port *p; 1230*bff33f7eSVladimir Oltean int err; 1231*bff33f7eSVladimir Oltean 1232*bff33f7eSVladimir Oltean list_for_each_entry(p, hw_port_list, list) { 1233*bff33f7eSVladimir Oltean if (p->dev->mtu == mtu) 1234*bff33f7eSVladimir Oltean continue; 1235*bff33f7eSVladimir Oltean 1236*bff33f7eSVladimir Oltean err = dev_set_mtu(p->dev, mtu); 1237*bff33f7eSVladimir Oltean if (err) 1238*bff33f7eSVladimir Oltean goto rollback; 1239*bff33f7eSVladimir Oltean } 1240*bff33f7eSVladimir Oltean 1241*bff33f7eSVladimir Oltean return 0; 1242*bff33f7eSVladimir Oltean 1243*bff33f7eSVladimir Oltean rollback: 1244*bff33f7eSVladimir Oltean list_for_each_entry_continue_reverse(p, hw_port_list, list) { 1245*bff33f7eSVladimir Oltean if (p->dev->mtu == p->old_mtu) 1246*bff33f7eSVladimir Oltean continue; 1247*bff33f7eSVladimir Oltean 1248*bff33f7eSVladimir Oltean if (dev_set_mtu(p->dev, p->old_mtu)) 1249*bff33f7eSVladimir Oltean netdev_err(p->dev, "Failed to restore MTU\n"); 1250*bff33f7eSVladimir Oltean } 1251*bff33f7eSVladimir Oltean 1252*bff33f7eSVladimir Oltean return err; 1253*bff33f7eSVladimir Oltean } 1254*bff33f7eSVladimir Oltean 1255*bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list) 1256*bff33f7eSVladimir Oltean { 1257*bff33f7eSVladimir Oltean struct dsa_hw_port *p, *n; 1258*bff33f7eSVladimir Oltean 1259*bff33f7eSVladimir Oltean list_for_each_entry_safe(p, n, hw_port_list, list) 1260*bff33f7eSVladimir Oltean kfree(p); 1261*bff33f7eSVladimir Oltean } 1262*bff33f7eSVladimir Oltean 1263*bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */ 1264*bff33f7eSVladimir Oltean void dsa_bridge_mtu_normalization(struct dsa_port *dp) 1265*bff33f7eSVladimir Oltean { 1266*bff33f7eSVladimir Oltean struct list_head hw_port_list; 1267*bff33f7eSVladimir Oltean struct dsa_switch_tree *dst; 1268*bff33f7eSVladimir Oltean int min_mtu = ETH_MAX_MTU; 1269*bff33f7eSVladimir Oltean struct dsa_port *other_dp; 1270*bff33f7eSVladimir Oltean int err; 1271*bff33f7eSVladimir Oltean 1272*bff33f7eSVladimir Oltean if (!dp->ds->mtu_enforcement_ingress) 1273*bff33f7eSVladimir Oltean return; 1274*bff33f7eSVladimir Oltean 1275*bff33f7eSVladimir Oltean if (!dp->bridge_dev) 1276*bff33f7eSVladimir Oltean return; 1277*bff33f7eSVladimir Oltean 1278*bff33f7eSVladimir Oltean INIT_LIST_HEAD(&hw_port_list); 1279*bff33f7eSVladimir Oltean 1280*bff33f7eSVladimir Oltean /* Populate the list of ports that are part of the same bridge 1281*bff33f7eSVladimir Oltean * as the newly added/modified port 1282*bff33f7eSVladimir Oltean */ 1283*bff33f7eSVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) { 1284*bff33f7eSVladimir Oltean list_for_each_entry(other_dp, &dst->ports, list) { 1285*bff33f7eSVladimir Oltean struct dsa_hw_port *hw_port; 1286*bff33f7eSVladimir Oltean struct net_device *slave; 1287*bff33f7eSVladimir Oltean 1288*bff33f7eSVladimir Oltean if (other_dp->type != DSA_PORT_TYPE_USER) 1289*bff33f7eSVladimir Oltean continue; 1290*bff33f7eSVladimir Oltean 1291*bff33f7eSVladimir Oltean if (other_dp->bridge_dev != dp->bridge_dev) 1292*bff33f7eSVladimir Oltean continue; 1293*bff33f7eSVladimir Oltean 1294*bff33f7eSVladimir Oltean if (!other_dp->ds->mtu_enforcement_ingress) 1295*bff33f7eSVladimir Oltean continue; 1296*bff33f7eSVladimir Oltean 1297*bff33f7eSVladimir Oltean slave = other_dp->slave; 1298*bff33f7eSVladimir Oltean 1299*bff33f7eSVladimir Oltean if (min_mtu > slave->mtu) 1300*bff33f7eSVladimir Oltean min_mtu = slave->mtu; 1301*bff33f7eSVladimir Oltean 1302*bff33f7eSVladimir Oltean hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL); 1303*bff33f7eSVladimir Oltean if (!hw_port) 1304*bff33f7eSVladimir Oltean goto out; 1305*bff33f7eSVladimir Oltean 1306*bff33f7eSVladimir Oltean hw_port->dev = slave; 1307*bff33f7eSVladimir Oltean hw_port->old_mtu = slave->mtu; 1308*bff33f7eSVladimir Oltean 1309*bff33f7eSVladimir Oltean list_add(&hw_port->list, &hw_port_list); 1310*bff33f7eSVladimir Oltean } 1311*bff33f7eSVladimir Oltean } 1312*bff33f7eSVladimir Oltean 1313*bff33f7eSVladimir Oltean /* Attempt to configure the entire hardware bridge to the newly added 1314*bff33f7eSVladimir Oltean * interface's MTU first, regardless of whether the intention of the 1315*bff33f7eSVladimir Oltean * user was to raise or lower it. 1316*bff33f7eSVladimir Oltean */ 1317*bff33f7eSVladimir Oltean err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu); 1318*bff33f7eSVladimir Oltean if (!err) 1319*bff33f7eSVladimir Oltean goto out; 1320*bff33f7eSVladimir Oltean 1321*bff33f7eSVladimir Oltean /* Clearly that didn't work out so well, so just set the minimum MTU on 1322*bff33f7eSVladimir Oltean * all hardware bridge ports now. If this fails too, then all ports will 1323*bff33f7eSVladimir Oltean * still have their old MTU rolled back anyway. 1324*bff33f7eSVladimir Oltean */ 1325*bff33f7eSVladimir Oltean dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu); 1326*bff33f7eSVladimir Oltean 1327*bff33f7eSVladimir Oltean out: 1328*bff33f7eSVladimir Oltean dsa_hw_port_list_free(&hw_port_list); 1329*bff33f7eSVladimir Oltean } 1330*bff33f7eSVladimir Oltean 1331bfcb8132SVladimir Oltean static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu) 1332bfcb8132SVladimir Oltean { 1333bfcb8132SVladimir Oltean struct net_device *master = dsa_slave_to_master(dev); 1334bfcb8132SVladimir Oltean struct dsa_port *dp = dsa_slave_to_port(dev); 1335bfcb8132SVladimir Oltean struct dsa_slave_priv *p = netdev_priv(dev); 1336bfcb8132SVladimir Oltean struct dsa_switch *ds = p->dp->ds; 1337bfcb8132SVladimir Oltean struct dsa_port *cpu_dp; 1338bfcb8132SVladimir Oltean int port = p->dp->index; 1339bfcb8132SVladimir Oltean int largest_mtu = 0; 1340bfcb8132SVladimir Oltean int new_master_mtu; 1341bfcb8132SVladimir Oltean int old_master_mtu; 1342bfcb8132SVladimir Oltean int mtu_limit; 1343bfcb8132SVladimir Oltean int cpu_mtu; 1344bfcb8132SVladimir Oltean int err, i; 1345bfcb8132SVladimir Oltean 1346bfcb8132SVladimir Oltean if (!ds->ops->port_change_mtu) 1347bfcb8132SVladimir Oltean return -EOPNOTSUPP; 1348bfcb8132SVladimir Oltean 1349bfcb8132SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1350bfcb8132SVladimir Oltean int slave_mtu; 1351bfcb8132SVladimir Oltean 1352bfcb8132SVladimir Oltean if (!dsa_is_user_port(ds, i)) 1353bfcb8132SVladimir Oltean continue; 1354bfcb8132SVladimir Oltean 1355bfcb8132SVladimir Oltean /* During probe, this function will be called for each slave 1356bfcb8132SVladimir Oltean * device, while not all of them have been allocated. That's 1357bfcb8132SVladimir Oltean * ok, it doesn't change what the maximum is, so ignore it. 1358bfcb8132SVladimir Oltean */ 1359bfcb8132SVladimir Oltean if (!dsa_to_port(ds, i)->slave) 1360bfcb8132SVladimir Oltean continue; 1361bfcb8132SVladimir Oltean 1362bfcb8132SVladimir Oltean /* Pretend that we already applied the setting, which we 1363bfcb8132SVladimir Oltean * actually haven't (still haven't done all integrity checks) 1364bfcb8132SVladimir Oltean */ 1365bfcb8132SVladimir Oltean if (i == port) 1366bfcb8132SVladimir Oltean slave_mtu = new_mtu; 1367bfcb8132SVladimir Oltean else 1368bfcb8132SVladimir Oltean slave_mtu = dsa_to_port(ds, i)->slave->mtu; 1369bfcb8132SVladimir Oltean 1370bfcb8132SVladimir Oltean if (largest_mtu < slave_mtu) 1371bfcb8132SVladimir Oltean largest_mtu = slave_mtu; 1372bfcb8132SVladimir Oltean } 1373bfcb8132SVladimir Oltean 1374bfcb8132SVladimir Oltean cpu_dp = dsa_to_port(ds, port)->cpu_dp; 1375bfcb8132SVladimir Oltean 1376bfcb8132SVladimir Oltean mtu_limit = min_t(int, master->max_mtu, dev->max_mtu); 1377bfcb8132SVladimir Oltean old_master_mtu = master->mtu; 1378bfcb8132SVladimir Oltean new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead; 1379bfcb8132SVladimir Oltean if (new_master_mtu > mtu_limit) 1380bfcb8132SVladimir Oltean return -ERANGE; 1381bfcb8132SVladimir Oltean 1382bfcb8132SVladimir Oltean /* If the master MTU isn't over limit, there's no need to check the CPU 1383bfcb8132SVladimir Oltean * MTU, since that surely isn't either. 1384bfcb8132SVladimir Oltean */ 1385bfcb8132SVladimir Oltean cpu_mtu = largest_mtu; 1386bfcb8132SVladimir Oltean 1387bfcb8132SVladimir Oltean /* Start applying stuff */ 1388bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) { 1389bfcb8132SVladimir Oltean err = dev_set_mtu(master, new_master_mtu); 1390bfcb8132SVladimir Oltean if (err < 0) 1391bfcb8132SVladimir Oltean goto out_master_failed; 1392bfcb8132SVladimir Oltean 1393bfcb8132SVladimir Oltean /* We only need to propagate the MTU of the CPU port to 1394bfcb8132SVladimir Oltean * upstream switches. 1395bfcb8132SVladimir Oltean */ 1396bfcb8132SVladimir Oltean err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true); 1397bfcb8132SVladimir Oltean if (err) 1398bfcb8132SVladimir Oltean goto out_cpu_failed; 1399bfcb8132SVladimir Oltean } 1400bfcb8132SVladimir Oltean 1401bfcb8132SVladimir Oltean err = dsa_port_mtu_change(dp, new_mtu, false); 1402bfcb8132SVladimir Oltean if (err) 1403bfcb8132SVladimir Oltean goto out_port_failed; 1404bfcb8132SVladimir Oltean 1405bfcb8132SVladimir Oltean dev->mtu = new_mtu; 1406bfcb8132SVladimir Oltean 1407*bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 1408*bff33f7eSVladimir Oltean 1409bfcb8132SVladimir Oltean return 0; 1410bfcb8132SVladimir Oltean 1411bfcb8132SVladimir Oltean out_port_failed: 1412bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1413bfcb8132SVladimir Oltean dsa_port_mtu_change(cpu_dp, old_master_mtu - 1414bfcb8132SVladimir Oltean cpu_dp->tag_ops->overhead, 1415bfcb8132SVladimir Oltean true); 1416bfcb8132SVladimir Oltean out_cpu_failed: 1417bfcb8132SVladimir Oltean if (new_master_mtu != old_master_mtu) 1418bfcb8132SVladimir Oltean dev_set_mtu(master, old_master_mtu); 1419bfcb8132SVladimir Oltean out_master_failed: 1420bfcb8132SVladimir Oltean return err; 1421bfcb8132SVladimir Oltean } 1422bfcb8132SVladimir Oltean 142391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 142491da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 14253d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 14263d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 1427aab9c406SFlorian Fainelli .nway_reset = dsa_slave_nway_reset, 1428c4aef9fcSFlorian Fainelli .get_link = ethtool_op_get_link, 14296793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 14306793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 14316793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 143291da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 143391da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 143491da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 143519e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 143619e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 14377905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 14387905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 1439aab9c406SFlorian Fainelli .get_link_ksettings = dsa_slave_get_link_ksettings, 1440aab9c406SFlorian Fainelli .set_link_ksettings = dsa_slave_set_link_ksettings, 1441a2a1a13bSHeiner Kallweit .get_pauseparam = dsa_slave_get_pauseparam, 1442a2a1a13bSHeiner Kallweit .set_pauseparam = dsa_slave_set_pauseparam, 1443bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 1444bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 14450336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 144691da11f8SLennert Buytenhek }; 144791da11f8SLennert Buytenhek 14482a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/ 14492a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 14502a93c1a3SFlorian Fainelli struct net_device *dev, 14512a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid, 145287b0984eSPetr Machata u16 flags, 145387b0984eSPetr Machata struct netlink_ext_ack *extack) 14542a93c1a3SFlorian Fainelli { 14552a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 14562a93c1a3SFlorian Fainelli 14572a93c1a3SFlorian Fainelli return dsa_port_fdb_add(dp, addr, vid); 14582a93c1a3SFlorian Fainelli } 14592a93c1a3SFlorian Fainelli 14602a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 14612a93c1a3SFlorian Fainelli struct net_device *dev, 14622a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid) 14632a93c1a3SFlorian Fainelli { 14642a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 14652a93c1a3SFlorian Fainelli 14662a93c1a3SFlorian Fainelli return dsa_port_fdb_del(dp, addr, vid); 14672a93c1a3SFlorian Fainelli } 14682a93c1a3SFlorian Fainelli 1469716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev) 1470716efee2SJiri Pirko { 1471716efee2SJiri Pirko struct dsa_port *dp = dsa_slave_to_port(dev); 1472716efee2SJiri Pirko 1473716efee2SJiri Pirko return dp->ds->devlink ? &dp->devlink_port : NULL; 1474716efee2SJiri Pirko } 1475716efee2SJiri Pirko 14763e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 1477d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 1478d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 14793e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 1480d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1481d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1482d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 148337b8da1aSArkadi Sharshevsky .ndo_fdb_add = dsa_legacy_fdb_add, 148437b8da1aSArkadi Sharshevsky .ndo_fdb_del = dsa_legacy_fdb_del, 14852bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 1486d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 1487abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 148804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 148904ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 149004ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 149104ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 149204ff53f9SFlorian Fainelli #endif 149344bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1494f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 1495f613ed66SFlorian Fainelli .ndo_get_stats64 = dsa_slave_get_stats64, 1496929d6c14SFlorian Fainelli .ndo_get_port_parent_id = dsa_slave_get_port_parent_id, 1497061f6a50SFlorian Fainelli .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid, 1498061f6a50SFlorian Fainelli .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid, 1499716efee2SJiri Pirko .ndo_get_devlink_port = dsa_slave_get_devlink_port, 1500bfcb8132SVladimir Oltean .ndo_change_mtu = dsa_slave_change_mtu, 150198237d43SScott Feldman }; 150298237d43SScott Feldman 1503f37db85dSFlorian Fainelli static struct device_type dsa_type = { 1504f37db85dSFlorian Fainelli .name = "dsa", 1505f37db85dSFlorian Fainelli }; 1506f37db85dSFlorian Fainelli 1507aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1508aab9c406SFlorian Fainelli { 1509aab9c406SFlorian Fainelli const struct dsa_port *dp = dsa_to_port(ds, port); 1510aab9c406SFlorian Fainelli 1511aab9c406SFlorian Fainelli phylink_mac_change(dp->pl, up); 1512aab9c406SFlorian Fainelli } 1513aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change); 1514aab9c406SFlorian Fainelli 1515aab9c406SFlorian Fainelli static void dsa_slave_phylink_fixed_state(struct net_device *dev, 1516aab9c406SFlorian Fainelli struct phylink_link_state *state) 1517aab9c406SFlorian Fainelli { 1518aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 1519aab9c406SFlorian Fainelli struct dsa_switch *ds = dp->ds; 1520aab9c406SFlorian Fainelli 1521aab9c406SFlorian Fainelli /* No need to check that this operation is valid, the callback would 1522aab9c406SFlorian Fainelli * not be called if it was not. 1523aab9c406SFlorian Fainelli */ 1524aab9c406SFlorian Fainelli ds->ops->phylink_fixed_state(ds, dp->index, state); 1525ce31b31cSFlorian Fainelli } 1526ce31b31cSFlorian Fainelli 152791da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 15284fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1529c305c165SFlorian Fainelli { 1530d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1531d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1532c305c165SFlorian Fainelli 15330115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 15340115dcd1SVivien Didelot if (!slave_dev->phydev) { 1535d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1536c305c165SFlorian Fainelli return -ENODEV; 1537d25b8e74SRussell King } 1538c305c165SFlorian Fainelli 1539aab9c406SFlorian Fainelli return phylink_connect_phy(dp->pl, slave_dev->phydev); 1540c305c165SFlorian Fainelli } 1541c305c165SFlorian Fainelli 15424fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 15430d8bcdd3SFlorian Fainelli { 1544d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1545d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 1546d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 15470c65b2b9SAndrew Lunn phy_interface_t mode; 15486819563eSFlorian Fainelli u32 phy_flags = 0; 15490c65b2b9SAndrew Lunn int ret; 15500d8bcdd3SFlorian Fainelli 15510c65b2b9SAndrew Lunn ret = of_get_phy_mode(port_dn, &mode); 15520c65b2b9SAndrew Lunn if (ret) 155319334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 15540d8bcdd3SFlorian Fainelli 155544cc27e4SIoana Ciornei dp->pl_config.dev = &slave_dev->dev; 155644cc27e4SIoana Ciornei dp->pl_config.type = PHYLINK_NETDEV; 155744cc27e4SIoana Ciornei 155844cc27e4SIoana Ciornei dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 155977373d49SIoana Ciornei &dsa_port_phylink_mac_ops); 1560aab9c406SFlorian Fainelli if (IS_ERR(dp->pl)) { 1561aab9c406SFlorian Fainelli netdev_err(slave_dev, 1562aab9c406SFlorian Fainelli "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1563aab9c406SFlorian Fainelli return PTR_ERR(dp->pl); 1564aab9c406SFlorian Fainelli } 1565aab9c406SFlorian Fainelli 1566aab9c406SFlorian Fainelli /* Register only if the switch provides such a callback, since this 1567aab9c406SFlorian Fainelli * callback takes precedence over polling the link GPIO in PHYLINK 1568aab9c406SFlorian Fainelli * (see phylink_get_fixed_state). 15690d8bcdd3SFlorian Fainelli */ 1570aab9c406SFlorian Fainelli if (ds->ops->phylink_fixed_state) 1571aab9c406SFlorian Fainelli phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state); 15720d8bcdd3SFlorian Fainelli 15739d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1574d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 15756819563eSFlorian Fainelli 1576aab9c406SFlorian Fainelli ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags); 15776146dd45SVladimir Oltean if (ret == -ENODEV && ds->slave_mii_bus) { 15786146dd45SVladimir Oltean /* We could not connect to a designated PHY or SFP, so try to 15796146dd45SVladimir Oltean * use the switch internal MDIO bus instead 15800d8bcdd3SFlorian Fainelli */ 1581d945097bSVivien Didelot ret = dsa_slave_phy_connect(slave_dev, dp->index); 1582d25b8e74SRussell King if (ret) { 1583aab9c406SFlorian Fainelli netdev_err(slave_dev, 1584aab9c406SFlorian Fainelli "failed to connect to port %d: %d\n", 1585d945097bSVivien Didelot dp->index, ret); 1586aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 1587c305c165SFlorian Fainelli return ret; 1588d25b8e74SRussell King } 15890d8bcdd3SFlorian Fainelli } 15909697f1cdSFlorian Fainelli 15916146dd45SVladimir Oltean return ret; 1592b31f65fbSAndrew Lunn } 15930d8bcdd3SFlorian Fainelli 159424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 159524462549SFlorian Fainelli { 1596aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 159724462549SFlorian Fainelli 1598a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1599a94c689eSFlorian Fainelli return 0; 1600a94c689eSFlorian Fainelli 1601f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1602f154be24SFlorian Fainelli 1603aab9c406SFlorian Fainelli rtnl_lock(); 1604aab9c406SFlorian Fainelli phylink_stop(dp->pl); 1605aab9c406SFlorian Fainelli rtnl_unlock(); 160624462549SFlorian Fainelli 160724462549SFlorian Fainelli return 0; 160824462549SFlorian Fainelli } 160924462549SFlorian Fainelli 161024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 161124462549SFlorian Fainelli { 1612aab9c406SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1613aab9c406SFlorian Fainelli 1614a94c689eSFlorian Fainelli if (!netif_running(slave_dev)) 1615a94c689eSFlorian Fainelli return 0; 1616a94c689eSFlorian Fainelli 161724462549SFlorian Fainelli netif_device_attach(slave_dev); 161824462549SFlorian Fainelli 1619aab9c406SFlorian Fainelli rtnl_lock(); 1620aab9c406SFlorian Fainelli phylink_start(dp->pl); 1621aab9c406SFlorian Fainelli rtnl_unlock(); 162224462549SFlorian Fainelli 162324462549SFlorian Fainelli return 0; 162424462549SFlorian Fainelli } 162524462549SFlorian Fainelli 16266158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val) 16276158eaa7SVivien Didelot { 1628d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 1629d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 16306158eaa7SVivien Didelot struct dsa_notifier_register_info rinfo = { 16316158eaa7SVivien Didelot .switch_number = dp->ds->index, 16326158eaa7SVivien Didelot .port_number = dp->index, 16336158eaa7SVivien Didelot .master = master, 16346158eaa7SVivien Didelot .info.dev = dev, 16356158eaa7SVivien Didelot }; 16366158eaa7SVivien Didelot 16376158eaa7SVivien Didelot call_dsa_notifiers(val, dev, &rinfo.info); 16386158eaa7SVivien Didelot } 16396158eaa7SVivien Didelot 1640951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 164191da11f8SLennert Buytenhek { 164224a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 1643f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 16444cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 1645951259aaSVivien Didelot const char *name = port->name; 164691da11f8SLennert Buytenhek struct net_device *slave_dev; 164791da11f8SLennert Buytenhek struct dsa_slave_priv *p; 164891da11f8SLennert Buytenhek int ret; 164991da11f8SLennert Buytenhek 165055199df6SFlorian Fainelli if (!ds->num_tx_queues) 165155199df6SFlorian Fainelli ds->num_tx_queues = 1; 165255199df6SFlorian Fainelli 165355199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 165455199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 165555199df6SFlorian Fainelli ds->num_tx_queues, 1); 165691da11f8SLennert Buytenhek if (slave_dev == NULL) 1657d87d6f44SGuenter Roeck return -ENOMEM; 165891da11f8SLennert Buytenhek 16599b236d2aSVladimir Oltean slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 16609b236d2aSVladimir Oltean if (ds->ops->port_vlan_add && ds->ops->port_vlan_del) 16619b236d2aSVladimir Oltean slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1662f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 16637ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 16644974f9b7SPetr Štetiar if (!IS_ERR_OR_NULL(port->mac)) 1665a2c7023fSXiaofei Shen ether_addr_copy(slave_dev->dev_addr, port->mac); 1666a2c7023fSXiaofei Shen else 16672fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 16680a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 16693e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 16708b1efc0fSJarod Wilson slave_dev->min_mtu = 0; 1671bfcb8132SVladimir Oltean if (ds->ops->port_max_mtu) 1672bfcb8132SVladimir Oltean slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index); 1673bfcb8132SVladimir Oltean else 16748b1efc0fSJarod Wilson slave_dev->max_mtu = ETH_MAX_MTU; 1675f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1676d442ad4aSStephen Hemminger 16774cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 16784cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 167991da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 168091da11f8SLennert Buytenhek 168191da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 16825f6b4e14SFlorian Fainelli p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 16835f6b4e14SFlorian Fainelli if (!p->stats64) { 16845f6b4e14SFlorian Fainelli free_netdev(slave_dev); 16855f6b4e14SFlorian Fainelli return -ENOMEM; 16865f6b4e14SFlorian Fainelli } 16874cfbf09cSVivien Didelot p->dp = port; 1688f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 168915240248SVivien Didelot p->xmit = cpu_dp->tag_ops->xmit; 1690f8b8b1cdSVivien Didelot port->slave = slave_dev; 169191da11f8SLennert Buytenhek 1692bfcb8132SVladimir Oltean rtnl_lock(); 1693bfcb8132SVladimir Oltean ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN); 1694bfcb8132SVladimir Oltean rtnl_unlock(); 1695bfcb8132SVladimir Oltean if (ret && ret != -EOPNOTSUPP) { 1696bfcb8132SVladimir Oltean dev_err(ds->dev, "error %d setting MTU on port %d\n", 1697bfcb8132SVladimir Oltean ret, port->index); 1698bfcb8132SVladimir Oltean goto out_free; 1699bfcb8132SVladimir Oltean } 1700bfcb8132SVladimir Oltean 170191da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 170291da11f8SLennert Buytenhek 17034fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 17040071f56eSAndrew Lunn if (ret) { 17050071f56eSAndrew Lunn netdev_err(master, "error %d setting up slave phy\n", ret); 1706e804441cSFlorian Fainelli goto out_free; 1707e804441cSFlorian Fainelli } 1708e804441cSFlorian Fainelli 17096158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_REGISTER); 171060724d4bSFlorian Fainelli 1711e804441cSFlorian Fainelli ret = register_netdev(slave_dev); 1712e804441cSFlorian Fainelli if (ret) { 1713e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 1714e804441cSFlorian Fainelli ret, slave_dev->name); 1715e804441cSFlorian Fainelli goto out_phy; 17160071f56eSAndrew Lunn } 17170071f56eSAndrew Lunn 1718d87d6f44SGuenter Roeck return 0; 1719e804441cSFlorian Fainelli 1720e804441cSFlorian Fainelli out_phy: 1721aab9c406SFlorian Fainelli rtnl_lock(); 1722aab9c406SFlorian Fainelli phylink_disconnect_phy(p->dp->pl); 1723aab9c406SFlorian Fainelli rtnl_unlock(); 1724aab9c406SFlorian Fainelli phylink_destroy(p->dp->pl); 1725e804441cSFlorian Fainelli out_free: 1726e804441cSFlorian Fainelli free_percpu(p->stats64); 1727e804441cSFlorian Fainelli free_netdev(slave_dev); 1728f8b8b1cdSVivien Didelot port->slave = NULL; 1729e804441cSFlorian Fainelli return ret; 173091da11f8SLennert Buytenhek } 1731b73adef6SFlorian Fainelli 1732cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1733cda5c15bSNeil Armstrong { 1734d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1735cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1736cda5c15bSNeil Armstrong 1737cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 1738aab9c406SFlorian Fainelli rtnl_lock(); 1739aab9c406SFlorian Fainelli phylink_disconnect_phy(dp->pl); 1740aab9c406SFlorian Fainelli rtnl_unlock(); 1741881eadabSJohan Hovold 17426158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); 1743cda5c15bSNeil Armstrong unregister_netdev(slave_dev); 1744aab9c406SFlorian Fainelli phylink_destroy(dp->pl); 17455f6b4e14SFlorian Fainelli free_percpu(p->stats64); 1746cda5c15bSNeil Armstrong free_netdev(slave_dev); 1747cda5c15bSNeil Armstrong } 1748cda5c15bSNeil Armstrong 17494d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev) 1750b73adef6SFlorian Fainelli { 1751b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1752b73adef6SFlorian Fainelli } 1753b73adef6SFlorian Fainelli 17548e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 17558e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1756b73adef6SFlorian Fainelli { 1757d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 17588e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1759b73adef6SFlorian Fainelli 17608e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 17618e92ab3aSVivien Didelot if (info->linking) { 176217d7802bSVivien Didelot err = dsa_port_bridge_join(dp, info->upper_dev); 1763*bff33f7eSVladimir Oltean if (!err) 1764*bff33f7eSVladimir Oltean dsa_bridge_mtu_normalization(dp); 17658e92ab3aSVivien Didelot err = notifier_from_errno(err); 17668e92ab3aSVivien Didelot } else { 176717d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 17688e92ab3aSVivien Didelot err = NOTIFY_OK; 17698e92ab3aSVivien Didelot } 17706debb68aSVivien Didelot } 1771b73adef6SFlorian Fainelli 17728e92ab3aSVivien Didelot return err; 1773b73adef6SFlorian Fainelli } 1774b73adef6SFlorian Fainelli 1775cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev, 1776cc1d5bdaSFlorian Fainelli struct netdev_notifier_changeupper_info * 1777cc1d5bdaSFlorian Fainelli info) 1778cc1d5bdaSFlorian Fainelli { 1779cc1d5bdaSFlorian Fainelli struct netlink_ext_ack *ext_ack; 1780cc1d5bdaSFlorian Fainelli struct net_device *slave; 1781cc1d5bdaSFlorian Fainelli struct dsa_port *dp; 1782cc1d5bdaSFlorian Fainelli 1783cc1d5bdaSFlorian Fainelli ext_ack = netdev_notifier_info_to_extack(&info->info); 1784cc1d5bdaSFlorian Fainelli 1785cc1d5bdaSFlorian Fainelli if (!is_vlan_dev(dev)) 1786cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1787cc1d5bdaSFlorian Fainelli 1788cc1d5bdaSFlorian Fainelli slave = vlan_dev_real_dev(dev); 1789cc1d5bdaSFlorian Fainelli if (!dsa_slave_dev_check(slave)) 1790cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1791cc1d5bdaSFlorian Fainelli 1792cc1d5bdaSFlorian Fainelli dp = dsa_slave_to_port(slave); 1793cc1d5bdaSFlorian Fainelli if (!dp->bridge_dev) 1794cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1795cc1d5bdaSFlorian Fainelli 1796cc1d5bdaSFlorian Fainelli /* Deny enslaving a VLAN device into a VLAN-aware bridge */ 1797cc1d5bdaSFlorian Fainelli if (br_vlan_enabled(dp->bridge_dev) && 1798cc1d5bdaSFlorian Fainelli netif_is_bridge_master(info->upper_dev) && info->linking) { 1799cc1d5bdaSFlorian Fainelli NL_SET_ERR_MSG_MOD(ext_ack, 1800cc1d5bdaSFlorian Fainelli "Cannot enslave VLAN device into VLAN aware bridge"); 1801cc1d5bdaSFlorian Fainelli return notifier_from_errno(-EINVAL); 1802cc1d5bdaSFlorian Fainelli } 1803cc1d5bdaSFlorian Fainelli 1804cc1d5bdaSFlorian Fainelli return NOTIFY_DONE; 1805cc1d5bdaSFlorian Fainelli } 1806cc1d5bdaSFlorian Fainelli 180788e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 1808b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1809b73adef6SFlorian Fainelli { 18106debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1811b73adef6SFlorian Fainelli 1812cc1d5bdaSFlorian Fainelli if (event == NETDEV_CHANGEUPPER) { 181353bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 1814cc1d5bdaSFlorian Fainelli return dsa_slave_upper_vlan_check(dev, ptr); 18158e92ab3aSVivien Didelot 18168e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 1817cc1d5bdaSFlorian Fainelli } 1818b73adef6SFlorian Fainelli 1819b73adef6SFlorian Fainelli return NOTIFY_DONE; 1820b73adef6SFlorian Fainelli } 182188e4f0caSVivien Didelot 1822c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work { 1823c9eb3e0fSArkadi Sharshevsky struct work_struct work; 1824c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info fdb_info; 1825c9eb3e0fSArkadi Sharshevsky struct net_device *dev; 1826c9eb3e0fSArkadi Sharshevsky unsigned long event; 1827c9eb3e0fSArkadi Sharshevsky }; 1828c9eb3e0fSArkadi Sharshevsky 1829c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 1830c9eb3e0fSArkadi Sharshevsky { 1831c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 1832c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 1833c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_work->dev; 1834c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info *fdb_info; 1835d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1836c9eb3e0fSArkadi Sharshevsky int err; 1837c9eb3e0fSArkadi Sharshevsky 1838c9eb3e0fSArkadi Sharshevsky rtnl_lock(); 1839c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 1840c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 1841c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1842a37fb855SVivien Didelot if (!fdb_info->added_by_user) 1843a37fb855SVivien Didelot break; 1844a37fb855SVivien Didelot 1845d945097bSVivien Didelot err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid); 1846c9eb3e0fSArkadi Sharshevsky if (err) { 1847c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb add failed err=%d\n", err); 1848c9eb3e0fSArkadi Sharshevsky break; 1849c9eb3e0fSArkadi Sharshevsky } 1850e9ba0fbcSIdo Schimmel fdb_info->offloaded = true; 1851c9eb3e0fSArkadi Sharshevsky call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 18526685987cSPetr Machata &fdb_info->info, NULL); 1853c9eb3e0fSArkadi Sharshevsky break; 1854c9eb3e0fSArkadi Sharshevsky 1855c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1856c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1857a37fb855SVivien Didelot if (!fdb_info->added_by_user) 1858a37fb855SVivien Didelot break; 1859a37fb855SVivien Didelot 1860d945097bSVivien Didelot err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid); 1861c9eb3e0fSArkadi Sharshevsky if (err) { 1862c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb del failed err=%d\n", err); 1863c9eb3e0fSArkadi Sharshevsky dev_close(dev); 1864c9eb3e0fSArkadi Sharshevsky } 1865c9eb3e0fSArkadi Sharshevsky break; 1866c9eb3e0fSArkadi Sharshevsky } 1867c9eb3e0fSArkadi Sharshevsky rtnl_unlock(); 1868c9eb3e0fSArkadi Sharshevsky 1869c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work->fdb_info.addr); 1870c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1871c9eb3e0fSArkadi Sharshevsky dev_put(dev); 1872c9eb3e0fSArkadi Sharshevsky } 1873c9eb3e0fSArkadi Sharshevsky 1874c9eb3e0fSArkadi Sharshevsky static int 1875c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * 1876c9eb3e0fSArkadi Sharshevsky switchdev_work, 1877c9eb3e0fSArkadi Sharshevsky const struct switchdev_notifier_fdb_info * 1878c9eb3e0fSArkadi Sharshevsky fdb_info) 1879c9eb3e0fSArkadi Sharshevsky { 1880c9eb3e0fSArkadi Sharshevsky memcpy(&switchdev_work->fdb_info, fdb_info, 1881c9eb3e0fSArkadi Sharshevsky sizeof(switchdev_work->fdb_info)); 1882c9eb3e0fSArkadi Sharshevsky switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 1883c9eb3e0fSArkadi Sharshevsky if (!switchdev_work->fdb_info.addr) 1884c9eb3e0fSArkadi Sharshevsky return -ENOMEM; 1885c9eb3e0fSArkadi Sharshevsky ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 1886c9eb3e0fSArkadi Sharshevsky fdb_info->addr); 1887c9eb3e0fSArkadi Sharshevsky return 0; 1888c9eb3e0fSArkadi Sharshevsky } 1889c9eb3e0fSArkadi Sharshevsky 1890c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 1891c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 1892c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 1893c9eb3e0fSArkadi Sharshevsky { 1894c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1895c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work; 189679b139f4SVivien Didelot int err; 189779b139f4SVivien Didelot 189879b139f4SVivien Didelot if (event == SWITCHDEV_PORT_ATTR_SET) { 189979b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 190079b139f4SVivien Didelot dsa_slave_dev_check, 190179b139f4SVivien Didelot dsa_slave_port_attr_set); 190279b139f4SVivien Didelot return notifier_from_errno(err); 190379b139f4SVivien Didelot } 1904c9eb3e0fSArkadi Sharshevsky 1905c9eb3e0fSArkadi Sharshevsky if (!dsa_slave_dev_check(dev)) 1906c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1907c9eb3e0fSArkadi Sharshevsky 1908c9eb3e0fSArkadi Sharshevsky switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 1909c9eb3e0fSArkadi Sharshevsky if (!switchdev_work) 1910c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1911c9eb3e0fSArkadi Sharshevsky 1912c9eb3e0fSArkadi Sharshevsky INIT_WORK(&switchdev_work->work, 1913c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_event_work); 1914c9eb3e0fSArkadi Sharshevsky switchdev_work->dev = dev; 1915c9eb3e0fSArkadi Sharshevsky switchdev_work->event = event; 1916c9eb3e0fSArkadi Sharshevsky 1917c9eb3e0fSArkadi Sharshevsky switch (event) { 1918c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ 1919c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1920a37fb855SVivien Didelot if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr)) 1921c9eb3e0fSArkadi Sharshevsky goto err_fdb_work_init; 1922c9eb3e0fSArkadi Sharshevsky dev_hold(dev); 1923c9eb3e0fSArkadi Sharshevsky break; 1924c9eb3e0fSArkadi Sharshevsky default: 1925c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1926c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1927c9eb3e0fSArkadi Sharshevsky } 1928c9eb3e0fSArkadi Sharshevsky 1929c9eb3e0fSArkadi Sharshevsky dsa_schedule_work(&switchdev_work->work); 1930c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 1931c9eb3e0fSArkadi Sharshevsky 1932c9eb3e0fSArkadi Sharshevsky err_fdb_work_init: 1933c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1934c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1935c9eb3e0fSArkadi Sharshevsky } 1936c9eb3e0fSArkadi Sharshevsky 19372b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused, 19382b239f67SPetr Machata unsigned long event, void *ptr) 19392b239f67SPetr Machata { 19402b239f67SPetr Machata struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 194179b139f4SVivien Didelot int err; 19422b239f67SPetr Machata 19432b239f67SPetr Machata switch (event) { 194479b139f4SVivien Didelot case SWITCHDEV_PORT_OBJ_ADD: 194579b139f4SVivien Didelot err = switchdev_handle_port_obj_add(dev, ptr, 194679b139f4SVivien Didelot dsa_slave_dev_check, 194779b139f4SVivien Didelot dsa_slave_port_obj_add); 194879b139f4SVivien Didelot return notifier_from_errno(err); 19492b239f67SPetr Machata case SWITCHDEV_PORT_OBJ_DEL: 195079b139f4SVivien Didelot err = switchdev_handle_port_obj_del(dev, ptr, 195179b139f4SVivien Didelot dsa_slave_dev_check, 195279b139f4SVivien Didelot dsa_slave_port_obj_del); 195379b139f4SVivien Didelot return notifier_from_errno(err); 19549ed1ecedSFlorian Fainelli case SWITCHDEV_PORT_ATTR_SET: 195579b139f4SVivien Didelot err = switchdev_handle_port_attr_set(dev, ptr, 195679b139f4SVivien Didelot dsa_slave_dev_check, 195779b139f4SVivien Didelot dsa_slave_port_attr_set); 195879b139f4SVivien Didelot return notifier_from_errno(err); 19592b239f67SPetr Machata } 19602b239f67SPetr Machata 19612b239f67SPetr Machata return NOTIFY_DONE; 19622b239f67SPetr Machata } 19632b239f67SPetr Machata 196488e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 196588e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 196688e4f0caSVivien Didelot }; 196788e4f0caSVivien Didelot 1968c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = { 1969c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 1970c9eb3e0fSArkadi Sharshevsky }; 1971c9eb3e0fSArkadi Sharshevsky 19722b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = { 19732b239f67SPetr Machata .notifier_call = dsa_slave_switchdev_blocking_event, 19742b239f67SPetr Machata }; 19752b239f67SPetr Machata 197688e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 197788e4f0caSVivien Didelot { 19782b239f67SPetr Machata struct notifier_block *nb; 1979c9eb3e0fSArkadi Sharshevsky int err; 1980c9eb3e0fSArkadi Sharshevsky 1981c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 1982c9eb3e0fSArkadi Sharshevsky if (err) 1983c9eb3e0fSArkadi Sharshevsky return err; 1984c9eb3e0fSArkadi Sharshevsky 1985c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 1986c9eb3e0fSArkadi Sharshevsky if (err) 1987c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 1988c9eb3e0fSArkadi Sharshevsky 19892b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 19902b239f67SPetr Machata err = register_switchdev_blocking_notifier(nb); 19912b239f67SPetr Machata if (err) 19922b239f67SPetr Machata goto err_switchdev_blocking_nb; 19932b239f67SPetr Machata 1994c9eb3e0fSArkadi Sharshevsky return 0; 1995c9eb3e0fSArkadi Sharshevsky 19962b239f67SPetr Machata err_switchdev_blocking_nb: 19972b239f67SPetr Machata unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 1998c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 1999c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 2000c9eb3e0fSArkadi Sharshevsky return err; 200188e4f0caSVivien Didelot } 200288e4f0caSVivien Didelot 200388e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 200488e4f0caSVivien Didelot { 20052b239f67SPetr Machata struct notifier_block *nb; 200688e4f0caSVivien Didelot int err; 200788e4f0caSVivien Didelot 20082b239f67SPetr Machata nb = &dsa_slave_switchdev_blocking_notifier; 20092b239f67SPetr Machata err = unregister_switchdev_blocking_notifier(nb); 20102b239f67SPetr Machata if (err) 20112b239f67SPetr Machata pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err); 20122b239f67SPetr Machata 2013c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 2014c9eb3e0fSArkadi Sharshevsky if (err) 2015c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 2016c9eb3e0fSArkadi Sharshevsky 201788e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 201888e4f0caSVivien Didelot if (err) 201988e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 202088e4f0caSVivien Didelot } 2021