191da11f8SLennert Buytenhek /* 291da11f8SLennert Buytenhek * net/dsa/slave.c - Slave device handling 3e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor 491da11f8SLennert Buytenhek * 591da11f8SLennert Buytenhek * This program is free software; you can redistribute it and/or modify 691da11f8SLennert Buytenhek * it under the terms of the GNU General Public License as published by 791da11f8SLennert Buytenhek * the Free Software Foundation; either version 2 of the License, or 891da11f8SLennert Buytenhek * (at your option) any later version. 991da11f8SLennert Buytenhek */ 1091da11f8SLennert Buytenhek 1191da11f8SLennert Buytenhek #include <linux/list.h> 12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h> 13b73adef6SFlorian Fainelli #include <linux/netdevice.h> 1491da11f8SLennert Buytenhek #include <linux/phy.h> 15a2820543SFlorian Fainelli #include <linux/phy_fixed.h> 160d8bcdd3SFlorian Fainelli #include <linux/of_net.h> 170d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h> 187f854420SAndrew Lunn #include <linux/mdio.h> 19b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 20f50f2127SFlorian Fainelli #include <net/pkt_cls.h> 21f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 22b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2304ff53f9SFlorian Fainelli #include <linux/netpoll.h> 2490af1059SBrandon Streiff #include <linux/ptp_classify.h> 25ea5dd34bSVivien Didelot 2691da11f8SLennert Buytenhek #include "dsa_priv.h" 2791da11f8SLennert Buytenhek 28f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev); 29f50f2127SFlorian Fainelli 3091da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 3191da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 3291da11f8SLennert Buytenhek { 3391da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3491da11f8SLennert Buytenhek 350d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 369d490b4eSVivien Didelot return ds->ops->phy_read(ds, addr, reg); 3791da11f8SLennert Buytenhek 3891da11f8SLennert Buytenhek return 0xffff; 3991da11f8SLennert Buytenhek } 4091da11f8SLennert Buytenhek 4191da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 4291da11f8SLennert Buytenhek { 4391da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 4491da11f8SLennert Buytenhek 450d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 469d490b4eSVivien Didelot return ds->ops->phy_write(ds, addr, reg, val); 4791da11f8SLennert Buytenhek 4891da11f8SLennert Buytenhek return 0; 4991da11f8SLennert Buytenhek } 5091da11f8SLennert Buytenhek 5191da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 5291da11f8SLennert Buytenhek { 5391da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 5491da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 5591da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 5691da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 570b7b498dSFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", 5849463b7fSVivien Didelot ds->dst->index, ds->index); 59c33063d6SAndrew Lunn ds->slave_mii_bus->parent = ds->dev; 6024df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 6191da11f8SLennert Buytenhek } 6291da11f8SLennert Buytenhek 6391da11f8SLennert Buytenhek 6491da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 65abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 66c0840801SLennert Buytenhek { 67d0006b00SVivien Didelot return dsa_slave_to_master(dev)->ifindex; 68c0840801SLennert Buytenhek } 69c0840801SLennert Buytenhek 7091da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 7191da11f8SLennert Buytenhek { 72d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 73d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 74df02c6ffSLennert Buytenhek int err; 75df02c6ffSLennert Buytenhek 76df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 77df02c6ffSLennert Buytenhek return -ENETDOWN; 78df02c6ffSLennert Buytenhek 798feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 80a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 81df02c6ffSLennert Buytenhek if (err < 0) 82df02c6ffSLennert Buytenhek goto out; 83df02c6ffSLennert Buytenhek } 84df02c6ffSLennert Buytenhek 85df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 86df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 87df02c6ffSLennert Buytenhek if (err < 0) 88df02c6ffSLennert Buytenhek goto del_unicast; 89df02c6ffSLennert Buytenhek } 90df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 91df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 92df02c6ffSLennert Buytenhek if (err < 0) 93df02c6ffSLennert Buytenhek goto clear_allmulti; 94df02c6ffSLennert Buytenhek } 95df02c6ffSLennert Buytenhek 960115dcd1SVivien Didelot err = dsa_port_enable(dp, dev->phydev); 97b2f2af21SFlorian Fainelli if (err) 98b2f2af21SFlorian Fainelli goto clear_promisc; 99b73adef6SFlorian Fainelli 1000115dcd1SVivien Didelot if (dev->phydev) 1010115dcd1SVivien Didelot phy_start(dev->phydev); 102f7f1de51SFlorian Fainelli 10391da11f8SLennert Buytenhek return 0; 104df02c6ffSLennert Buytenhek 105b2f2af21SFlorian Fainelli clear_promisc: 106b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 1074fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 108df02c6ffSLennert Buytenhek clear_allmulti: 109df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 110df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 111df02c6ffSLennert Buytenhek del_unicast: 1128feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 113a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 114df02c6ffSLennert Buytenhek out: 115df02c6ffSLennert Buytenhek return err; 11691da11f8SLennert Buytenhek } 11791da11f8SLennert Buytenhek 11891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 11991da11f8SLennert Buytenhek { 120d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 121d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 122df02c6ffSLennert Buytenhek 1230115dcd1SVivien Didelot if (dev->phydev) 1240115dcd1SVivien Didelot phy_stop(dev->phydev); 125f7f1de51SFlorian Fainelli 1260115dcd1SVivien Didelot dsa_port_disable(dp, dev->phydev); 1276457edfeSVivien Didelot 128df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 129a748ee24SJiri Pirko dev_uc_unsync(master, dev); 130df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 131df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 132df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 133df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 134df02c6ffSLennert Buytenhek 1358feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 136a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 137df02c6ffSLennert Buytenhek 13891da11f8SLennert Buytenhek return 0; 13991da11f8SLennert Buytenhek } 14091da11f8SLennert Buytenhek 14191da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 14291da11f8SLennert Buytenhek { 143d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 14491da11f8SLennert Buytenhek 14591da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 14691da11f8SLennert Buytenhek dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 14791da11f8SLennert Buytenhek if (change & IFF_PROMISC) 14891da11f8SLennert Buytenhek dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 14991da11f8SLennert Buytenhek } 15091da11f8SLennert Buytenhek 15191da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 15291da11f8SLennert Buytenhek { 153d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 15491da11f8SLennert Buytenhek 15591da11f8SLennert Buytenhek dev_mc_sync(master, dev); 156a748ee24SJiri Pirko dev_uc_sync(master, dev); 15791da11f8SLennert Buytenhek } 15891da11f8SLennert Buytenhek 159df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 16091da11f8SLennert Buytenhek { 161d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 162df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 163df02c6ffSLennert Buytenhek int err; 164df02c6ffSLennert Buytenhek 165df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 166df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 167df02c6ffSLennert Buytenhek 168df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 169df02c6ffSLennert Buytenhek goto out; 170df02c6ffSLennert Buytenhek 1718feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 172a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 173df02c6ffSLennert Buytenhek if (err < 0) 174df02c6ffSLennert Buytenhek return err; 175df02c6ffSLennert Buytenhek } 176df02c6ffSLennert Buytenhek 1778feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 178a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 179df02c6ffSLennert Buytenhek 180df02c6ffSLennert Buytenhek out: 181d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 18291da11f8SLennert Buytenhek 18391da11f8SLennert Buytenhek return 0; 18491da11f8SLennert Buytenhek } 18591da11f8SLennert Buytenhek 1862bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx { 1872bedde1aSArkadi Sharshevsky struct net_device *dev; 1882bedde1aSArkadi Sharshevsky struct sk_buff *skb; 1892bedde1aSArkadi Sharshevsky struct netlink_callback *cb; 1902bedde1aSArkadi Sharshevsky int idx; 1912bedde1aSArkadi Sharshevsky }; 1922bedde1aSArkadi Sharshevsky 1932bedde1aSArkadi Sharshevsky static int 1942bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, 1952bedde1aSArkadi Sharshevsky bool is_static, void *data) 1962bedde1aSArkadi Sharshevsky { 1972bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx *dump = data; 1982bedde1aSArkadi Sharshevsky u32 portid = NETLINK_CB(dump->cb->skb).portid; 1992bedde1aSArkadi Sharshevsky u32 seq = dump->cb->nlh->nlmsg_seq; 2002bedde1aSArkadi Sharshevsky struct nlmsghdr *nlh; 2012bedde1aSArkadi Sharshevsky struct ndmsg *ndm; 2022bedde1aSArkadi Sharshevsky 2032bedde1aSArkadi Sharshevsky if (dump->idx < dump->cb->args[2]) 2042bedde1aSArkadi Sharshevsky goto skip; 2052bedde1aSArkadi Sharshevsky 2062bedde1aSArkadi Sharshevsky nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 2072bedde1aSArkadi Sharshevsky sizeof(*ndm), NLM_F_MULTI); 2082bedde1aSArkadi Sharshevsky if (!nlh) 2092bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2102bedde1aSArkadi Sharshevsky 2112bedde1aSArkadi Sharshevsky ndm = nlmsg_data(nlh); 2122bedde1aSArkadi Sharshevsky ndm->ndm_family = AF_BRIDGE; 2132bedde1aSArkadi Sharshevsky ndm->ndm_pad1 = 0; 2142bedde1aSArkadi Sharshevsky ndm->ndm_pad2 = 0; 2152bedde1aSArkadi Sharshevsky ndm->ndm_flags = NTF_SELF; 2162bedde1aSArkadi Sharshevsky ndm->ndm_type = 0; 2172bedde1aSArkadi Sharshevsky ndm->ndm_ifindex = dump->dev->ifindex; 2182bedde1aSArkadi Sharshevsky ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 2192bedde1aSArkadi Sharshevsky 2202bedde1aSArkadi Sharshevsky if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 2212bedde1aSArkadi Sharshevsky goto nla_put_failure; 2222bedde1aSArkadi Sharshevsky 2232bedde1aSArkadi Sharshevsky if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 2242bedde1aSArkadi Sharshevsky goto nla_put_failure; 2252bedde1aSArkadi Sharshevsky 2262bedde1aSArkadi Sharshevsky nlmsg_end(dump->skb, nlh); 2272bedde1aSArkadi Sharshevsky 2282bedde1aSArkadi Sharshevsky skip: 2292bedde1aSArkadi Sharshevsky dump->idx++; 2302bedde1aSArkadi Sharshevsky return 0; 2312bedde1aSArkadi Sharshevsky 2322bedde1aSArkadi Sharshevsky nla_put_failure: 2332bedde1aSArkadi Sharshevsky nlmsg_cancel(dump->skb, nlh); 2342bedde1aSArkadi Sharshevsky return -EMSGSIZE; 2352bedde1aSArkadi Sharshevsky } 2362bedde1aSArkadi Sharshevsky 2372bedde1aSArkadi Sharshevsky static int 2382bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 2392bedde1aSArkadi Sharshevsky struct net_device *dev, struct net_device *filter_dev, 2402bedde1aSArkadi Sharshevsky int *idx) 2412bedde1aSArkadi Sharshevsky { 242d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 2432bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx dump = { 2442bedde1aSArkadi Sharshevsky .dev = dev, 2452bedde1aSArkadi Sharshevsky .skb = skb, 2462bedde1aSArkadi Sharshevsky .cb = cb, 2472bedde1aSArkadi Sharshevsky .idx = *idx, 2482bedde1aSArkadi Sharshevsky }; 2492bedde1aSArkadi Sharshevsky int err; 2502bedde1aSArkadi Sharshevsky 251de40fc5dSVivien Didelot err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); 2522bedde1aSArkadi Sharshevsky *idx = dump.idx; 253de40fc5dSVivien Didelot 2542bedde1aSArkadi Sharshevsky return err; 2552bedde1aSArkadi Sharshevsky } 2562bedde1aSArkadi Sharshevsky 25791da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 25891da11f8SLennert Buytenhek { 2590336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 2600336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 2610336369dSBrandon Streiff int port = p->dp->index; 2620336369dSBrandon Streiff 2630336369dSBrandon Streiff /* Pass through to switch driver if it supports timestamping */ 2640336369dSBrandon Streiff switch (cmd) { 2650336369dSBrandon Streiff case SIOCGHWTSTAMP: 2660336369dSBrandon Streiff if (ds->ops->port_hwtstamp_get) 2670336369dSBrandon Streiff return ds->ops->port_hwtstamp_get(ds, port, ifr); 2680336369dSBrandon Streiff break; 2690336369dSBrandon Streiff case SIOCSHWTSTAMP: 2700336369dSBrandon Streiff if (ds->ops->port_hwtstamp_set) 2710336369dSBrandon Streiff return ds->ops->port_hwtstamp_set(ds, port, ifr); 2720336369dSBrandon Streiff break; 2730336369dSBrandon Streiff } 2740336369dSBrandon Streiff 2750115dcd1SVivien Didelot if (!dev->phydev) 276f4344e0aSVivien Didelot return -ENODEV; 27791da11f8SLennert Buytenhek 2780115dcd1SVivien Didelot return phy_mii_ioctl(dev->phydev, ifr, cmd); 27991da11f8SLennert Buytenhek } 28091da11f8SLennert Buytenhek 28135636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 282f7fadf30SJiri Pirko const struct switchdev_attr *attr, 2837ea6eb3fSJiri Pirko struct switchdev_trans *trans) 28435636062SScott Feldman { 285d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 286b8d866acSVivien Didelot int ret; 28735636062SScott Feldman 28835636062SScott Feldman switch (attr->id) { 2891f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 290fd364541SVivien Didelot ret = dsa_port_set_state(dp, attr->u.stp_state, trans); 29135636062SScott Feldman break; 292fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 293c02c4175SVivien Didelot ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 294c02c4175SVivien Didelot trans); 295fb2dabadSVivien Didelot break; 29634a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 297072bb190SVivien Didelot ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); 29834a79f63SVivien Didelot break; 29935636062SScott Feldman default: 30035636062SScott Feldman ret = -EOPNOTSUPP; 30135636062SScott Feldman break; 30235636062SScott Feldman } 30335636062SScott Feldman 30435636062SScott Feldman return ret; 30535636062SScott Feldman } 30635636062SScott Feldman 307ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 308648b4a99SJiri Pirko const struct switchdev_obj *obj, 3097ea6eb3fSJiri Pirko struct switchdev_trans *trans) 310ba14d9ebSVivien Didelot { 311d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 312ba14d9ebSVivien Didelot int err; 313ba14d9ebSVivien Didelot 314ba14d9ebSVivien Didelot /* For the prepare phase, ensure the full set of changes is feasable in 315ba14d9ebSVivien Didelot * one go in order to signal a failure properly. If an operation is not 316ba14d9ebSVivien Didelot * supported, return -EOPNOTSUPP. 317ba14d9ebSVivien Didelot */ 318ba14d9ebSVivien Didelot 3199e8f4a54SJiri Pirko switch (obj->id) { 3208df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 321bcebb976SVivien Didelot err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); 3228df30255SVivien Didelot break; 3235f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3245f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3255f4dbc50SAndrew Lunn * but on the CPU port. 3265f4dbc50SAndrew Lunn */ 3275f4dbc50SAndrew Lunn err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj), 3285f4dbc50SAndrew Lunn trans); 3295f4dbc50SAndrew Lunn break; 33057d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 33101676d12SVivien Didelot err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), 332648b4a99SJiri Pirko trans); 33311149536SVivien Didelot break; 334ba14d9ebSVivien Didelot default: 335ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 336ba14d9ebSVivien Didelot break; 337ba14d9ebSVivien Didelot } 338ba14d9ebSVivien Didelot 339ba14d9ebSVivien Didelot return err; 340ba14d9ebSVivien Didelot } 341ba14d9ebSVivien Didelot 342ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 343648b4a99SJiri Pirko const struct switchdev_obj *obj) 344ba14d9ebSVivien Didelot { 345d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 346ba14d9ebSVivien Didelot int err; 347ba14d9ebSVivien Didelot 3489e8f4a54SJiri Pirko switch (obj->id) { 3498df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 350bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3518df30255SVivien Didelot break; 3525f4dbc50SAndrew Lunn case SWITCHDEV_OBJ_ID_HOST_MDB: 3535f4dbc50SAndrew Lunn /* DSA can directly translate this to a normal MDB add, 3545f4dbc50SAndrew Lunn * but on the CPU port. 3555f4dbc50SAndrew Lunn */ 3565f4dbc50SAndrew Lunn err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 3575f4dbc50SAndrew Lunn break; 35857d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 35901676d12SVivien Didelot err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); 36011149536SVivien Didelot break; 361ba14d9ebSVivien Didelot default: 362ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 363ba14d9ebSVivien Didelot break; 364ba14d9ebSVivien Didelot } 365ba14d9ebSVivien Didelot 366ba14d9ebSVivien Didelot return err; 367ba14d9ebSVivien Didelot } 368ba14d9ebSVivien Didelot 369f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev, 370f8e20a9fSScott Feldman struct switchdev_attr *attr) 371b73adef6SFlorian Fainelli { 372d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 373d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 374a42c8e33SAndrew Lunn struct dsa_switch_tree *dst = ds->dst; 375b73adef6SFlorian Fainelli 376f8e20a9fSScott Feldman switch (attr->id) { 3771f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 378a42c8e33SAndrew Lunn attr->u.ppid.id_len = sizeof(dst->index); 379a42c8e33SAndrew Lunn memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len); 380f8e20a9fSScott Feldman break; 381c9e2105eSArkadi Sharshevsky case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT: 382c9e2105eSArkadi Sharshevsky attr->u.brport_flags_support = 0; 383c9e2105eSArkadi Sharshevsky break; 384f8e20a9fSScott Feldman default: 385f8e20a9fSScott Feldman return -EOPNOTSUPP; 386f8e20a9fSScott Feldman } 387b73adef6SFlorian Fainelli 388b73adef6SFlorian Fainelli return 0; 389b73adef6SFlorian Fainelli } 390b73adef6SFlorian Fainelli 3914fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, 39204ff53f9SFlorian Fainelli struct sk_buff *skb) 39304ff53f9SFlorian Fainelli { 39404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 3954fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 3964fa7b718SVivien Didelot 39704ff53f9SFlorian Fainelli if (p->netpoll) 39804ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 39904ff53f9SFlorian Fainelli #else 40004ff53f9SFlorian Fainelli BUG(); 40104ff53f9SFlorian Fainelli #endif 40204ff53f9SFlorian Fainelli return NETDEV_TX_OK; 40304ff53f9SFlorian Fainelli } 40404ff53f9SFlorian Fainelli 40590af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p, 40690af1059SBrandon Streiff struct sk_buff *skb) 40790af1059SBrandon Streiff { 40890af1059SBrandon Streiff struct dsa_switch *ds = p->dp->ds; 40990af1059SBrandon Streiff struct sk_buff *clone; 41090af1059SBrandon Streiff unsigned int type; 41190af1059SBrandon Streiff 41290af1059SBrandon Streiff type = ptp_classify_raw(skb); 41390af1059SBrandon Streiff if (type == PTP_CLASS_NONE) 41490af1059SBrandon Streiff return; 41590af1059SBrandon Streiff 41690af1059SBrandon Streiff if (!ds->ops->port_txtstamp) 41790af1059SBrandon Streiff return; 41890af1059SBrandon Streiff 41990af1059SBrandon Streiff clone = skb_clone_sk(skb); 42090af1059SBrandon Streiff if (!clone) 42190af1059SBrandon Streiff return; 42290af1059SBrandon Streiff 42390af1059SBrandon Streiff if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) 42490af1059SBrandon Streiff return; 42590af1059SBrandon Streiff 42690af1059SBrandon Streiff kfree_skb(clone); 42790af1059SBrandon Streiff } 42890af1059SBrandon Streiff 4293e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 4303e8a72d1SFlorian Fainelli { 4313e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 4325f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 4334ed70ce9SFlorian Fainelli struct sk_buff *nskb; 4343e8a72d1SFlorian Fainelli 4355f6b4e14SFlorian Fainelli s = this_cpu_ptr(p->stats64); 4365f6b4e14SFlorian Fainelli u64_stats_update_begin(&s->syncp); 4375f6b4e14SFlorian Fainelli s->tx_packets++; 4385f6b4e14SFlorian Fainelli s->tx_bytes += skb->len; 4395f6b4e14SFlorian Fainelli u64_stats_update_end(&s->syncp); 4403e8a72d1SFlorian Fainelli 44190af1059SBrandon Streiff /* Identify PTP protocol packets, clone them, and pass them to the 44290af1059SBrandon Streiff * switch driver 44390af1059SBrandon Streiff */ 44490af1059SBrandon Streiff dsa_skb_tx_timestamp(p, skb); 44590af1059SBrandon Streiff 446fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 447fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 448fe47d563SVivien Didelot */ 4494ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 450fe47d563SVivien Didelot if (!nskb) { 451fe47d563SVivien Didelot kfree_skb(skb); 4524ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 453fe47d563SVivien Didelot } 4545aed85ceSFlorian Fainelli 45504ff53f9SFlorian Fainelli /* SKB for netpoll still need to be mangled with the protocol-specific 45604ff53f9SFlorian Fainelli * tag to be successfully transmitted 45704ff53f9SFlorian Fainelli */ 45804ff53f9SFlorian Fainelli if (unlikely(netpoll_tx_running(dev))) 4594fa7b718SVivien Didelot return dsa_slave_netpoll_send_skb(dev, nskb); 46004ff53f9SFlorian Fainelli 4614ed70ce9SFlorian Fainelli /* Queue the SKB for transmission on the parent interface, but 4624ed70ce9SFlorian Fainelli * do not modify its EtherType 4634ed70ce9SFlorian Fainelli */ 464d0006b00SVivien Didelot nskb->dev = dsa_slave_to_master(dev); 4654ed70ce9SFlorian Fainelli dev_queue_xmit(nskb); 4665aed85ceSFlorian Fainelli 4675aed85ceSFlorian Fainelli return NETDEV_TX_OK; 4685aed85ceSFlorian Fainelli } 4695aed85ceSFlorian Fainelli 47091da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 47191da11f8SLennert Buytenhek 47291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 47391da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 47491da11f8SLennert Buytenhek { 4757826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 4767826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 4777826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 47891da11f8SLennert Buytenhek } 47991da11f8SLennert Buytenhek 4803d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 4813d762a0fSGuenter Roeck { 482d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 483d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 4843d762a0fSGuenter Roeck 4859d490b4eSVivien Didelot if (ds->ops->get_regs_len) 486d945097bSVivien Didelot return ds->ops->get_regs_len(ds, dp->index); 4873d762a0fSGuenter Roeck 4883d762a0fSGuenter Roeck return -EOPNOTSUPP; 4893d762a0fSGuenter Roeck } 4903d762a0fSGuenter Roeck 4913d762a0fSGuenter Roeck static void 4923d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 4933d762a0fSGuenter Roeck { 494d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 495d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 4963d762a0fSGuenter Roeck 4979d490b4eSVivien Didelot if (ds->ops->get_regs) 498d945097bSVivien Didelot ds->ops->get_regs(ds, dp->index, regs, _p); 4993d762a0fSGuenter Roeck } 5003d762a0fSGuenter Roeck 50191da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 50291da11f8SLennert Buytenhek { 5030115dcd1SVivien Didelot if (!dev->phydev) 504f4344e0aSVivien Didelot return -ENODEV; 50591da11f8SLennert Buytenhek 5060115dcd1SVivien Didelot genphy_update_link(dev->phydev); 507f4344e0aSVivien Didelot 5080115dcd1SVivien Didelot return dev->phydev->link; 50991da11f8SLennert Buytenhek } 51091da11f8SLennert Buytenhek 5116793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 5126793abb4SGuenter Roeck { 513d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 514d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5156793abb4SGuenter Roeck 5160e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 517ff04955cSAndrew Lunn return ds->cd->eeprom_len; 5186793abb4SGuenter Roeck 5199d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 5209d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 5216793abb4SGuenter Roeck 5226793abb4SGuenter Roeck return 0; 5236793abb4SGuenter Roeck } 5246793abb4SGuenter Roeck 5256793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 5266793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5276793abb4SGuenter Roeck { 528d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 529d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5306793abb4SGuenter Roeck 5319d490b4eSVivien Didelot if (ds->ops->get_eeprom) 5329d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 5336793abb4SGuenter Roeck 5346793abb4SGuenter Roeck return -EOPNOTSUPP; 5356793abb4SGuenter Roeck } 5366793abb4SGuenter Roeck 5376793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 5386793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5396793abb4SGuenter Roeck { 540d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 541d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5426793abb4SGuenter Roeck 5439d490b4eSVivien Didelot if (ds->ops->set_eeprom) 5449d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 5456793abb4SGuenter Roeck 5466793abb4SGuenter Roeck return -EOPNOTSUPP; 5476793abb4SGuenter Roeck } 5486793abb4SGuenter Roeck 54991da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 55091da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 55191da11f8SLennert Buytenhek { 552d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 553d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 55491da11f8SLennert Buytenhek 55591da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 55691da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 55791da11f8SLennert Buytenhek 55891da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 55991da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 56091da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 56191da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 5629d490b4eSVivien Didelot if (ds->ops->get_strings) 563*89f09048SFlorian Fainelli ds->ops->get_strings(ds, dp->index, stringset, 564*89f09048SFlorian Fainelli data + 4 * len); 56591da11f8SLennert Buytenhek } 56691da11f8SLennert Buytenhek } 56791da11f8SLennert Buytenhek 56891da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 56991da11f8SLennert Buytenhek struct ethtool_stats *stats, 57091da11f8SLennert Buytenhek uint64_t *data) 57191da11f8SLennert Buytenhek { 572d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 57391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 574d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 5755f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 576f613ed66SFlorian Fainelli unsigned int start; 5775f6b4e14SFlorian Fainelli int i; 57891da11f8SLennert Buytenhek 5795f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 5805f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 5815f6b4e14SFlorian Fainelli 5825f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 583f613ed66SFlorian Fainelli do { 5845f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 5855f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 5865f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 5875f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 5885f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 5895f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 5905f6b4e14SFlorian Fainelli data[0] += tx_packets; 5915f6b4e14SFlorian Fainelli data[1] += tx_bytes; 5925f6b4e14SFlorian Fainelli data[2] += rx_packets; 5935f6b4e14SFlorian Fainelli data[3] += rx_bytes; 5945f6b4e14SFlorian Fainelli } 5959d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 596d945097bSVivien Didelot ds->ops->get_ethtool_stats(ds, dp->index, data + 4); 59791da11f8SLennert Buytenhek } 59891da11f8SLennert Buytenhek 59991da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 60091da11f8SLennert Buytenhek { 601d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 602d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 60391da11f8SLennert Buytenhek 60491da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 60591da11f8SLennert Buytenhek int count; 60691da11f8SLennert Buytenhek 60791da11f8SLennert Buytenhek count = 4; 6089d490b4eSVivien Didelot if (ds->ops->get_sset_count) 609*89f09048SFlorian Fainelli count += ds->ops->get_sset_count(ds, dp->index, sset); 61091da11f8SLennert Buytenhek 61191da11f8SLennert Buytenhek return count; 61291da11f8SLennert Buytenhek } 61391da11f8SLennert Buytenhek 61491da11f8SLennert Buytenhek return -EOPNOTSUPP; 61591da11f8SLennert Buytenhek } 61691da11f8SLennert Buytenhek 61719e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 61819e57c4eSFlorian Fainelli { 619d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 620d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 62119e57c4eSFlorian Fainelli 6229d490b4eSVivien Didelot if (ds->ops->get_wol) 623d945097bSVivien Didelot ds->ops->get_wol(ds, dp->index, w); 62419e57c4eSFlorian Fainelli } 62519e57c4eSFlorian Fainelli 62619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 62719e57c4eSFlorian Fainelli { 628d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 629d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 63019e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 63119e57c4eSFlorian Fainelli 6329d490b4eSVivien Didelot if (ds->ops->set_wol) 633d945097bSVivien Didelot ret = ds->ops->set_wol(ds, dp->index, w); 63419e57c4eSFlorian Fainelli 63519e57c4eSFlorian Fainelli return ret; 63619e57c4eSFlorian Fainelli } 63719e57c4eSFlorian Fainelli 6387905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 6397905288fSFlorian Fainelli { 640d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 641d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6427905288fSFlorian Fainelli int ret; 6437905288fSFlorian Fainelli 6447b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 6450115dcd1SVivien Didelot if (!dev->phydev) 6467b9cc738SVivien Didelot return -ENODEV; 6477b9cc738SVivien Didelot 64808f50061SVivien Didelot if (!ds->ops->set_mac_eee) 6497905288fSFlorian Fainelli return -EOPNOTSUPP; 6507905288fSFlorian Fainelli 651d945097bSVivien Didelot ret = ds->ops->set_mac_eee(ds, dp->index, e); 6527905288fSFlorian Fainelli if (ret) 6537905288fSFlorian Fainelli return ret; 6547905288fSFlorian Fainelli 655c48f7eb3SVivien Didelot if (e->eee_enabled) { 6560115dcd1SVivien Didelot ret = phy_init_eee(dev->phydev, 0); 657c48f7eb3SVivien Didelot if (ret) 658c48f7eb3SVivien Didelot return ret; 659c48f7eb3SVivien Didelot } 660c48f7eb3SVivien Didelot 6610115dcd1SVivien Didelot return phy_ethtool_set_eee(dev->phydev, e); 6627905288fSFlorian Fainelli } 6637905288fSFlorian Fainelli 6647905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 6657905288fSFlorian Fainelli { 666d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 667d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 6687905288fSFlorian Fainelli int ret; 6697905288fSFlorian Fainelli 6707b9cc738SVivien Didelot /* Port's PHY and MAC both need to be EEE capable */ 6710115dcd1SVivien Didelot if (!dev->phydev) 6727b9cc738SVivien Didelot return -ENODEV; 6737b9cc738SVivien Didelot 67408f50061SVivien Didelot if (!ds->ops->get_mac_eee) 6757905288fSFlorian Fainelli return -EOPNOTSUPP; 6767905288fSFlorian Fainelli 677d945097bSVivien Didelot ret = ds->ops->get_mac_eee(ds, dp->index, e); 6787905288fSFlorian Fainelli if (ret) 6797905288fSFlorian Fainelli return ret; 6807905288fSFlorian Fainelli 6810115dcd1SVivien Didelot return phy_ethtool_get_eee(dev->phydev, e); 6827905288fSFlorian Fainelli } 6837905288fSFlorian Fainelli 68404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 68504ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 68604ff53f9SFlorian Fainelli struct netpoll_info *ni) 68704ff53f9SFlorian Fainelli { 688d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 68904ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 69004ff53f9SFlorian Fainelli struct netpoll *netpoll; 69104ff53f9SFlorian Fainelli int err = 0; 69204ff53f9SFlorian Fainelli 69304ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 69404ff53f9SFlorian Fainelli if (!netpoll) 69504ff53f9SFlorian Fainelli return -ENOMEM; 69604ff53f9SFlorian Fainelli 69704ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 69804ff53f9SFlorian Fainelli if (err) { 69904ff53f9SFlorian Fainelli kfree(netpoll); 70004ff53f9SFlorian Fainelli goto out; 70104ff53f9SFlorian Fainelli } 70204ff53f9SFlorian Fainelli 70304ff53f9SFlorian Fainelli p->netpoll = netpoll; 70404ff53f9SFlorian Fainelli out: 70504ff53f9SFlorian Fainelli return err; 70604ff53f9SFlorian Fainelli } 70704ff53f9SFlorian Fainelli 70804ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 70904ff53f9SFlorian Fainelli { 71004ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 71104ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 71204ff53f9SFlorian Fainelli 71304ff53f9SFlorian Fainelli if (!netpoll) 71404ff53f9SFlorian Fainelli return; 71504ff53f9SFlorian Fainelli 71604ff53f9SFlorian Fainelli p->netpoll = NULL; 71704ff53f9SFlorian Fainelli 71804ff53f9SFlorian Fainelli __netpoll_free_async(netpoll); 71904ff53f9SFlorian Fainelli } 72004ff53f9SFlorian Fainelli 72104ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 72204ff53f9SFlorian Fainelli { 72304ff53f9SFlorian Fainelli } 72404ff53f9SFlorian Fainelli #endif 72504ff53f9SFlorian Fainelli 72644bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 72744bb765cSFlorian Fainelli char *name, size_t len) 72844bb765cSFlorian Fainelli { 729d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 73044bb765cSFlorian Fainelli 731d945097bSVivien Didelot if (snprintf(name, len, "p%d", dp->index) >= len) 73244bb765cSFlorian Fainelli return -EINVAL; 7333a543ef4SFlorian Fainelli 7343a543ef4SFlorian Fainelli return 0; 7353a543ef4SFlorian Fainelli } 7363a543ef4SFlorian Fainelli 737f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 7384fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) 739f50f2127SFlorian Fainelli { 7404fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 741f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 742f50f2127SFlorian Fainelli 743f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 744f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 745f50f2127SFlorian Fainelli return mall_tc_entry; 746f50f2127SFlorian Fainelli 747f50f2127SFlorian Fainelli return NULL; 748f50f2127SFlorian Fainelli } 749f50f2127SFlorian Fainelli 750f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev, 751f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 752f50f2127SFlorian Fainelli bool ingress) 753f50f2127SFlorian Fainelli { 754d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 755f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 756f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 7575fd9fc4eSJiri Pirko __be16 protocol = cls->common.protocol; 758d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 759f50f2127SFlorian Fainelli struct net_device *to_dev; 760f50f2127SFlorian Fainelli const struct tc_action *a; 761d945097bSVivien Didelot struct dsa_port *to_dp; 762f50f2127SFlorian Fainelli int err = -EOPNOTSUPP; 763f50f2127SFlorian Fainelli LIST_HEAD(actions); 764f50f2127SFlorian Fainelli 765f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 766f50f2127SFlorian Fainelli return err; 767f50f2127SFlorian Fainelli 7683bcc0cecSJiri Pirko if (!tcf_exts_has_one_action(cls->exts)) 769f50f2127SFlorian Fainelli return err; 770f50f2127SFlorian Fainelli 771f50f2127SFlorian Fainelli tcf_exts_to_list(cls->exts, &actions); 772f50f2127SFlorian Fainelli a = list_first_entry(&actions, struct tc_action, list); 773f50f2127SFlorian Fainelli 774f50f2127SFlorian Fainelli if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { 775f50f2127SFlorian Fainelli struct dsa_mall_mirror_tc_entry *mirror; 776f50f2127SFlorian Fainelli 7779f8a739eSCong Wang to_dev = tcf_mirred_dev(a); 778f50f2127SFlorian Fainelli if (!to_dev) 779f50f2127SFlorian Fainelli return -EINVAL; 780f50f2127SFlorian Fainelli 781f50f2127SFlorian Fainelli if (!dsa_slave_dev_check(to_dev)) 782f50f2127SFlorian Fainelli return -EOPNOTSUPP; 783f50f2127SFlorian Fainelli 784f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 785f50f2127SFlorian Fainelli if (!mall_tc_entry) 786f50f2127SFlorian Fainelli return -ENOMEM; 787f50f2127SFlorian Fainelli 788f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 789f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 790f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 791f50f2127SFlorian Fainelli 792d945097bSVivien Didelot to_dp = dsa_slave_to_port(to_dev); 793f50f2127SFlorian Fainelli 794d945097bSVivien Didelot mirror->to_local_port = to_dp->index; 795f50f2127SFlorian Fainelli mirror->ingress = ingress; 796f50f2127SFlorian Fainelli 797d945097bSVivien Didelot err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); 798f50f2127SFlorian Fainelli if (err) { 799f50f2127SFlorian Fainelli kfree(mall_tc_entry); 800f50f2127SFlorian Fainelli return err; 801f50f2127SFlorian Fainelli } 802f50f2127SFlorian Fainelli 803f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 804f50f2127SFlorian Fainelli } 805f50f2127SFlorian Fainelli 806f50f2127SFlorian Fainelli return 0; 807f50f2127SFlorian Fainelli } 808f50f2127SFlorian Fainelli 809f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 810f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 811f50f2127SFlorian Fainelli { 812d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 813f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 814d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 815f50f2127SFlorian Fainelli 816f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_del) 817f50f2127SFlorian Fainelli return; 818f50f2127SFlorian Fainelli 8194fa7b718SVivien Didelot mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); 820f50f2127SFlorian Fainelli if (!mall_tc_entry) 821f50f2127SFlorian Fainelli return; 822f50f2127SFlorian Fainelli 823f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 824f50f2127SFlorian Fainelli 825f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 826f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 827d945097bSVivien Didelot ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror); 828f50f2127SFlorian Fainelli break; 829f50f2127SFlorian Fainelli default: 830f50f2127SFlorian Fainelli WARN_ON(1); 831f50f2127SFlorian Fainelli } 832f50f2127SFlorian Fainelli 833f50f2127SFlorian Fainelli kfree(mall_tc_entry); 834f50f2127SFlorian Fainelli } 835f50f2127SFlorian Fainelli 8363fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, 8376b3eb752SJiri Pirko struct tc_cls_matchall_offload *cls, 8386b3eb752SJiri Pirko bool ingress) 839f50f2127SFlorian Fainelli { 8405fd9fc4eSJiri Pirko if (cls->common.chain_index) 841a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 842f50f2127SFlorian Fainelli 8433fbae382SJiri Pirko switch (cls->command) { 8443fbae382SJiri Pirko case TC_CLSMATCHALL_REPLACE: 8455fd9fc4eSJiri Pirko return dsa_slave_add_cls_matchall(dev, cls, ingress); 8463fbae382SJiri Pirko case TC_CLSMATCHALL_DESTROY: 8473fbae382SJiri Pirko dsa_slave_del_cls_matchall(dev, cls); 8483fbae382SJiri Pirko return 0; 8493fbae382SJiri Pirko default: 8503fbae382SJiri Pirko return -EOPNOTSUPP; 8513fbae382SJiri Pirko } 8523fbae382SJiri Pirko } 8533fbae382SJiri Pirko 8546b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 8556b3eb752SJiri Pirko void *cb_priv, bool ingress) 8566b3eb752SJiri Pirko { 8576b3eb752SJiri Pirko struct net_device *dev = cb_priv; 8586b3eb752SJiri Pirko 85944ae12a7SJiri Pirko if (!tc_can_offload(dev)) 86044ae12a7SJiri Pirko return -EOPNOTSUPP; 86144ae12a7SJiri Pirko 8626b3eb752SJiri Pirko switch (type) { 8636b3eb752SJiri Pirko case TC_SETUP_CLSMATCHALL: 8646b3eb752SJiri Pirko return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); 8656b3eb752SJiri Pirko default: 8666b3eb752SJiri Pirko return -EOPNOTSUPP; 8676b3eb752SJiri Pirko } 8686b3eb752SJiri Pirko } 8696b3eb752SJiri Pirko 8706b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, 8716b3eb752SJiri Pirko void *type_data, void *cb_priv) 8726b3eb752SJiri Pirko { 8736b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); 8746b3eb752SJiri Pirko } 8756b3eb752SJiri Pirko 8766b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, 8776b3eb752SJiri Pirko void *type_data, void *cb_priv) 8786b3eb752SJiri Pirko { 8796b3eb752SJiri Pirko return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); 8806b3eb752SJiri Pirko } 8816b3eb752SJiri Pirko 8826b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev, 8836b3eb752SJiri Pirko struct tc_block_offload *f) 8846b3eb752SJiri Pirko { 8856b3eb752SJiri Pirko tc_setup_cb_t *cb; 8866b3eb752SJiri Pirko 8876b3eb752SJiri Pirko if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 8886b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_ig; 8896b3eb752SJiri Pirko else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 8906b3eb752SJiri Pirko cb = dsa_slave_setup_tc_block_cb_eg; 8916b3eb752SJiri Pirko else 8926b3eb752SJiri Pirko return -EOPNOTSUPP; 8936b3eb752SJiri Pirko 8946b3eb752SJiri Pirko switch (f->command) { 8956b3eb752SJiri Pirko case TC_BLOCK_BIND: 8966b3eb752SJiri Pirko return tcf_block_cb_register(f->block, cb, dev, dev); 8976b3eb752SJiri Pirko case TC_BLOCK_UNBIND: 8986b3eb752SJiri Pirko tcf_block_cb_unregister(f->block, cb, dev); 8996b3eb752SJiri Pirko return 0; 9006b3eb752SJiri Pirko default: 9016b3eb752SJiri Pirko return -EOPNOTSUPP; 9026b3eb752SJiri Pirko } 9036b3eb752SJiri Pirko } 9046b3eb752SJiri Pirko 9053fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, 906de4784caSJiri Pirko void *type_data) 9073fbae382SJiri Pirko { 9082572ac53SJiri Pirko switch (type) { 9096b3eb752SJiri Pirko case TC_SETUP_BLOCK: 9106b3eb752SJiri Pirko return dsa_slave_setup_tc_block(dev, type_data); 911f50f2127SFlorian Fainelli default: 912a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 913f50f2127SFlorian Fainelli } 914f50f2127SFlorian Fainelli } 915f50f2127SFlorian Fainelli 916f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev, 917f613ed66SFlorian Fainelli struct rtnl_link_stats64 *stats) 918f613ed66SFlorian Fainelli { 919f613ed66SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 9205f6b4e14SFlorian Fainelli struct pcpu_sw_netstats *s; 921f613ed66SFlorian Fainelli unsigned int start; 9225f6b4e14SFlorian Fainelli int i; 923f613ed66SFlorian Fainelli 924f613ed66SFlorian Fainelli netdev_stats_to_stats64(stats, &dev->stats); 9255f6b4e14SFlorian Fainelli for_each_possible_cpu(i) { 9265f6b4e14SFlorian Fainelli u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 9275f6b4e14SFlorian Fainelli 9285f6b4e14SFlorian Fainelli s = per_cpu_ptr(p->stats64, i); 929f613ed66SFlorian Fainelli do { 9305f6b4e14SFlorian Fainelli start = u64_stats_fetch_begin_irq(&s->syncp); 9315f6b4e14SFlorian Fainelli tx_packets = s->tx_packets; 9325f6b4e14SFlorian Fainelli tx_bytes = s->tx_bytes; 9335f6b4e14SFlorian Fainelli rx_packets = s->rx_packets; 9345f6b4e14SFlorian Fainelli rx_bytes = s->rx_bytes; 9355f6b4e14SFlorian Fainelli } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 9365f6b4e14SFlorian Fainelli 9375f6b4e14SFlorian Fainelli stats->tx_packets += tx_packets; 9385f6b4e14SFlorian Fainelli stats->tx_bytes += tx_bytes; 9395f6b4e14SFlorian Fainelli stats->rx_packets += rx_packets; 9405f6b4e14SFlorian Fainelli stats->rx_bytes += rx_bytes; 9415f6b4e14SFlorian Fainelli } 942f613ed66SFlorian Fainelli } 943f613ed66SFlorian Fainelli 944bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 945bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 946bf9f2648SFlorian Fainelli { 947d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 948d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 949bf9f2648SFlorian Fainelli 950bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 951bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 952bf9f2648SFlorian Fainelli 953d945097bSVivien Didelot return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); 954bf9f2648SFlorian Fainelli } 955bf9f2648SFlorian Fainelli 956bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 957bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 958bf9f2648SFlorian Fainelli { 959d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 960d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 961bf9f2648SFlorian Fainelli 962bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 963bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 964bf9f2648SFlorian Fainelli 965d945097bSVivien Didelot return ds->ops->set_rxnfc(ds, dp->index, nfc); 966bf9f2648SFlorian Fainelli } 967bf9f2648SFlorian Fainelli 9680336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev, 9690336369dSBrandon Streiff struct ethtool_ts_info *ts) 9700336369dSBrandon Streiff { 9710336369dSBrandon Streiff struct dsa_slave_priv *p = netdev_priv(dev); 9720336369dSBrandon Streiff struct dsa_switch *ds = p->dp->ds; 9730336369dSBrandon Streiff 9740336369dSBrandon Streiff if (!ds->ops->get_ts_info) 9750336369dSBrandon Streiff return -EOPNOTSUPP; 9760336369dSBrandon Streiff 9770336369dSBrandon Streiff return ds->ops->get_ts_info(ds, p->dp->index, ts); 9780336369dSBrandon Streiff } 9790336369dSBrandon Streiff 98091da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 98191da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 9823d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 9833d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 98469b2c162SVivien Didelot .nway_reset = phy_ethtool_nway_reset, 98591da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 9866793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 9876793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 9886793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 98991da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 99091da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 99191da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 99219e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 99319e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 9947905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 9957905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 996771df31aSVivien Didelot .get_link_ksettings = phy_ethtool_get_link_ksettings, 997aa62a8caSVivien Didelot .set_link_ksettings = phy_ethtool_set_link_ksettings, 998bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 999bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 10000336369dSBrandon Streiff .get_ts_info = dsa_slave_get_ts_info, 100191da11f8SLennert Buytenhek }; 100291da11f8SLennert Buytenhek 10032a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/ 10042a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 10052a93c1a3SFlorian Fainelli struct net_device *dev, 10062a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid, 10072a93c1a3SFlorian Fainelli u16 flags) 10082a93c1a3SFlorian Fainelli { 10092a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 10102a93c1a3SFlorian Fainelli 10112a93c1a3SFlorian Fainelli return dsa_port_fdb_add(dp, addr, vid); 10122a93c1a3SFlorian Fainelli } 10132a93c1a3SFlorian Fainelli 10142a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 10152a93c1a3SFlorian Fainelli struct net_device *dev, 10162a93c1a3SFlorian Fainelli const unsigned char *addr, u16 vid) 10172a93c1a3SFlorian Fainelli { 10182a93c1a3SFlorian Fainelli struct dsa_port *dp = dsa_slave_to_port(dev); 10192a93c1a3SFlorian Fainelli 10202a93c1a3SFlorian Fainelli return dsa_port_fdb_del(dp, addr, vid); 10212a93c1a3SFlorian Fainelli } 10222a93c1a3SFlorian Fainelli 10233e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 1024d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 1025d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 10263e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 1027d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 1028d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 1029d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 103037b8da1aSArkadi Sharshevsky .ndo_fdb_add = dsa_legacy_fdb_add, 103137b8da1aSArkadi Sharshevsky .ndo_fdb_del = dsa_legacy_fdb_del, 10322bedde1aSArkadi Sharshevsky .ndo_fdb_dump = dsa_slave_fdb_dump, 1033d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 1034abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 103504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 103604ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 103704ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 103804ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 103904ff53f9SFlorian Fainelli #endif 104044bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 1041f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 1042f613ed66SFlorian Fainelli .ndo_get_stats64 = dsa_slave_get_stats64, 104398237d43SScott Feldman }; 104498237d43SScott Feldman 10459d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = { 1046f8e20a9fSScott Feldman .switchdev_port_attr_get = dsa_slave_port_attr_get, 104735636062SScott Feldman .switchdev_port_attr_set = dsa_slave_port_attr_set, 1048ba14d9ebSVivien Didelot .switchdev_port_obj_add = dsa_slave_port_obj_add, 1049ba14d9ebSVivien Didelot .switchdev_port_obj_del = dsa_slave_port_obj_del, 1050d442ad4aSStephen Hemminger }; 105191da11f8SLennert Buytenhek 1052f37db85dSFlorian Fainelli static struct device_type dsa_type = { 1053f37db85dSFlorian Fainelli .name = "dsa", 1054f37db85dSFlorian Fainelli }; 1055f37db85dSFlorian Fainelli 10560d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 10570d8bcdd3SFlorian Fainelli { 1058d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 10590d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 1060d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 10610d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 10620d8bcdd3SFlorian Fainelli 10630115dcd1SVivien Didelot if (p->old_link != dev->phydev->link) { 10640d8bcdd3SFlorian Fainelli status_changed = 1; 10650115dcd1SVivien Didelot p->old_link = dev->phydev->link; 10660d8bcdd3SFlorian Fainelli } 10670d8bcdd3SFlorian Fainelli 10680115dcd1SVivien Didelot if (p->old_duplex != dev->phydev->duplex) { 10690d8bcdd3SFlorian Fainelli status_changed = 1; 10700115dcd1SVivien Didelot p->old_duplex = dev->phydev->duplex; 10710d8bcdd3SFlorian Fainelli } 10720d8bcdd3SFlorian Fainelli 10730115dcd1SVivien Didelot if (p->old_pause != dev->phydev->pause) { 10740d8bcdd3SFlorian Fainelli status_changed = 1; 10750115dcd1SVivien Didelot p->old_pause = dev->phydev->pause; 10760d8bcdd3SFlorian Fainelli } 10770d8bcdd3SFlorian Fainelli 10789d490b4eSVivien Didelot if (ds->ops->adjust_link && status_changed) 1079d945097bSVivien Didelot ds->ops->adjust_link(ds, dp->index, dev->phydev); 1080ec9436baSFlorian Fainelli 10810d8bcdd3SFlorian Fainelli if (status_changed) 10820115dcd1SVivien Didelot phy_print_status(dev->phydev); 10830d8bcdd3SFlorian Fainelli } 10840d8bcdd3SFlorian Fainelli 1085ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 1086ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 1087ce31b31cSFlorian Fainelli { 1088b71be352SAndrew Lunn struct dsa_switch *ds; 1089d945097bSVivien Didelot struct dsa_port *dp; 1090ce31b31cSFlorian Fainelli 1091b71be352SAndrew Lunn if (dev) { 1092d945097bSVivien Didelot dp = dsa_slave_to_port(dev); 1093d945097bSVivien Didelot ds = dp->ds; 10949d490b4eSVivien Didelot if (ds->ops->fixed_link_update) 1095d945097bSVivien Didelot ds->ops->fixed_link_update(ds, dp->index, status); 1096b71be352SAndrew Lunn } 1097ce31b31cSFlorian Fainelli 1098ce31b31cSFlorian Fainelli return 0; 1099ce31b31cSFlorian Fainelli } 1100ce31b31cSFlorian Fainelli 110191da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 11024fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1103c305c165SFlorian Fainelli { 1104d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 11054fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(slave_dev); 1106d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1107c305c165SFlorian Fainelli 11080115dcd1SVivien Didelot slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 11090115dcd1SVivien Didelot if (!slave_dev->phydev) { 1110d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1111c305c165SFlorian Fainelli return -ENODEV; 1112d25b8e74SRussell King } 1113c305c165SFlorian Fainelli 1114c305c165SFlorian Fainelli /* Use already configured phy mode */ 1115211c504aSFlorian Fainelli if (p->phy_interface == PHY_INTERFACE_MODE_NA) 11160115dcd1SVivien Didelot p->phy_interface = slave_dev->phydev->interface; 11170115dcd1SVivien Didelot 11180115dcd1SVivien Didelot return phy_connect_direct(slave_dev, slave_dev->phydev, 11190115dcd1SVivien Didelot dsa_slave_adjust_link, p->phy_interface); 1120c305c165SFlorian Fainelli } 1121c305c165SFlorian Fainelli 11224fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev) 11230d8bcdd3SFlorian Fainelli { 1124d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 11254fa7b718SVivien Didelot struct dsa_slave_priv *p = netdev_priv(slave_dev); 1126d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 1127d945097bSVivien Didelot struct dsa_switch *ds = dp->ds; 1128d945097bSVivien Didelot struct device_node *phy_dn; 1129ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 11306819563eSFlorian Fainelli u32 phy_flags = 0; 113119334920SGuenter Roeck int mode, ret; 11320d8bcdd3SFlorian Fainelli 113319334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 113419334920SGuenter Roeck if (mode < 0) 113519334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 113619334920SGuenter Roeck p->phy_interface = mode; 11370d8bcdd3SFlorian Fainelli 11380d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 11390d8f3c67SJohan Hovold if (!phy_dn && of_phy_is_fixed_link(port_dn)) { 11400d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 11410d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 11420d8bcdd3SFlorian Fainelli */ 11430d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 11440d8bcdd3SFlorian Fainelli if (ret) { 1145d25b8e74SRussell King netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); 11469697f1cdSFlorian Fainelli return ret; 11470d8bcdd3SFlorian Fainelli } 1148ce31b31cSFlorian Fainelli phy_is_fixed = true; 11490d8f3c67SJohan Hovold phy_dn = of_node_get(port_dn); 11500d8bcdd3SFlorian Fainelli } 11510d8bcdd3SFlorian Fainelli 11529d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1153d945097bSVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, dp->index); 11546819563eSFlorian Fainelli 1155cd28a1a9SFlorian Fainelli if (phy_dn) { 11560115dcd1SVivien Didelot slave_dev->phydev = of_phy_connect(slave_dev, phy_dn, 1157cd28a1a9SFlorian Fainelli dsa_slave_adjust_link, 1158cd28a1a9SFlorian Fainelli phy_flags, 11590d8bcdd3SFlorian Fainelli p->phy_interface); 11600d8f3c67SJohan Hovold of_node_put(phy_dn); 1161cd28a1a9SFlorian Fainelli } 11620d8bcdd3SFlorian Fainelli 11630115dcd1SVivien Didelot if (slave_dev->phydev && phy_is_fixed) 11640115dcd1SVivien Didelot fixed_phy_set_link_update(slave_dev->phydev, 11650115dcd1SVivien Didelot dsa_slave_fixed_link_update); 1166ce31b31cSFlorian Fainelli 11670d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 11680d8bcdd3SFlorian Fainelli * MDIO bus instead 11690d8bcdd3SFlorian Fainelli */ 11700115dcd1SVivien Didelot if (!slave_dev->phydev) { 1171d945097bSVivien Didelot ret = dsa_slave_phy_connect(slave_dev, dp->index); 1172d25b8e74SRussell King if (ret) { 1173afdcf151SVivien Didelot netdev_err(slave_dev, "failed to connect to port %d: %d\n", 1174d945097bSVivien Didelot dp->index, ret); 1175881eadabSJohan Hovold if (phy_is_fixed) 1176881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1177c305c165SFlorian Fainelli return ret; 1178d25b8e74SRussell King } 11790d8bcdd3SFlorian Fainelli } 11809697f1cdSFlorian Fainelli 11810115dcd1SVivien Didelot phy_attached_info(slave_dev->phydev); 11822220943aSAndrew Lunn 11839697f1cdSFlorian Fainelli return 0; 1184b31f65fbSAndrew Lunn } 11850d8bcdd3SFlorian Fainelli 1186448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1187448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1188448b4482SAndrew Lunn struct netdev_queue *txq, 1189448b4482SAndrew Lunn void *_unused) 1190448b4482SAndrew Lunn { 1191448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 1192448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 1193448b4482SAndrew Lunn } 1194448b4482SAndrew Lunn 119524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 119624462549SFlorian Fainelli { 119724462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 119824462549SFlorian Fainelli 1199f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1200f154be24SFlorian Fainelli 12010115dcd1SVivien Didelot if (slave_dev->phydev) { 12020115dcd1SVivien Didelot phy_stop(slave_dev->phydev); 120324462549SFlorian Fainelli p->old_pause = -1; 120424462549SFlorian Fainelli p->old_link = -1; 120524462549SFlorian Fainelli p->old_duplex = -1; 12060115dcd1SVivien Didelot phy_suspend(slave_dev->phydev); 120724462549SFlorian Fainelli } 120824462549SFlorian Fainelli 120924462549SFlorian Fainelli return 0; 121024462549SFlorian Fainelli } 121124462549SFlorian Fainelli 121224462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 121324462549SFlorian Fainelli { 121424462549SFlorian Fainelli netif_device_attach(slave_dev); 121524462549SFlorian Fainelli 12160115dcd1SVivien Didelot if (slave_dev->phydev) { 12170115dcd1SVivien Didelot phy_resume(slave_dev->phydev); 12180115dcd1SVivien Didelot phy_start(slave_dev->phydev); 121924462549SFlorian Fainelli } 122024462549SFlorian Fainelli 122124462549SFlorian Fainelli return 0; 122224462549SFlorian Fainelli } 122324462549SFlorian Fainelli 12246158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val) 12256158eaa7SVivien Didelot { 1226d0006b00SVivien Didelot struct net_device *master = dsa_slave_to_master(dev); 1227d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 12286158eaa7SVivien Didelot struct dsa_notifier_register_info rinfo = { 12296158eaa7SVivien Didelot .switch_number = dp->ds->index, 12306158eaa7SVivien Didelot .port_number = dp->index, 12316158eaa7SVivien Didelot .master = master, 12326158eaa7SVivien Didelot .info.dev = dev, 12336158eaa7SVivien Didelot }; 12346158eaa7SVivien Didelot 12356158eaa7SVivien Didelot call_dsa_notifiers(val, dev, &rinfo.info); 12366158eaa7SVivien Didelot } 12376158eaa7SVivien Didelot 1238951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port) 123991da11f8SLennert Buytenhek { 124024a9332aSVivien Didelot const struct dsa_port *cpu_dp = port->cpu_dp; 1241f8b8b1cdSVivien Didelot struct net_device *master = cpu_dp->master; 12424cfbf09cSVivien Didelot struct dsa_switch *ds = port->ds; 1243951259aaSVivien Didelot const char *name = port->name; 124491da11f8SLennert Buytenhek struct net_device *slave_dev; 124591da11f8SLennert Buytenhek struct dsa_slave_priv *p; 124691da11f8SLennert Buytenhek int ret; 124791da11f8SLennert Buytenhek 124855199df6SFlorian Fainelli if (!ds->num_tx_queues) 124955199df6SFlorian Fainelli ds->num_tx_queues = 1; 125055199df6SFlorian Fainelli 125155199df6SFlorian Fainelli slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, 125255199df6SFlorian Fainelli NET_NAME_UNKNOWN, ether_setup, 125355199df6SFlorian Fainelli ds->num_tx_queues, 1); 125491da11f8SLennert Buytenhek if (slave_dev == NULL) 1255d87d6f44SGuenter Roeck return -ENOMEM; 125691da11f8SLennert Buytenhek 1257f50f2127SFlorian Fainelli slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1258f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 12597ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 12602fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 12610a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 12623e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 12639d47c0a2SJiri Pirko slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 12648b1efc0fSJarod Wilson slave_dev->min_mtu = 0; 12658b1efc0fSJarod Wilson slave_dev->max_mtu = ETH_MAX_MTU; 1266f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1267d442ad4aSStephen Hemminger 1268448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1269448b4482SAndrew Lunn NULL); 1270448b4482SAndrew Lunn 12714cfbf09cSVivien Didelot SET_NETDEV_DEV(slave_dev, port->ds->dev); 12724cfbf09cSVivien Didelot slave_dev->dev.of_node = port->dn; 127391da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 127491da11f8SLennert Buytenhek 127591da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 12765f6b4e14SFlorian Fainelli p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 12775f6b4e14SFlorian Fainelli if (!p->stats64) { 12785f6b4e14SFlorian Fainelli free_netdev(slave_dev); 12795f6b4e14SFlorian Fainelli return -ENOMEM; 12805f6b4e14SFlorian Fainelli } 12814cfbf09cSVivien Didelot p->dp = port; 1282f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 128315240248SVivien Didelot p->xmit = cpu_dp->tag_ops->xmit; 12845075314eSAlexander Duyck 12850d8bcdd3SFlorian Fainelli p->old_pause = -1; 12860d8bcdd3SFlorian Fainelli p->old_link = -1; 12870d8bcdd3SFlorian Fainelli p->old_duplex = -1; 12880d8bcdd3SFlorian Fainelli 1289f8b8b1cdSVivien Didelot port->slave = slave_dev; 129091da11f8SLennert Buytenhek 129191da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 129291da11f8SLennert Buytenhek 12934fa7b718SVivien Didelot ret = dsa_slave_phy_setup(slave_dev); 12940071f56eSAndrew Lunn if (ret) { 12950071f56eSAndrew Lunn netdev_err(master, "error %d setting up slave phy\n", ret); 1296e804441cSFlorian Fainelli goto out_free; 1297e804441cSFlorian Fainelli } 1298e804441cSFlorian Fainelli 12996158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_REGISTER); 130060724d4bSFlorian Fainelli 1301e804441cSFlorian Fainelli ret = register_netdev(slave_dev); 1302e804441cSFlorian Fainelli if (ret) { 1303e804441cSFlorian Fainelli netdev_err(master, "error %d registering interface %s\n", 1304e804441cSFlorian Fainelli ret, slave_dev->name); 1305e804441cSFlorian Fainelli goto out_phy; 13060071f56eSAndrew Lunn } 13070071f56eSAndrew Lunn 1308d87d6f44SGuenter Roeck return 0; 1309e804441cSFlorian Fainelli 1310e804441cSFlorian Fainelli out_phy: 131153954cf8SDavid S. Miller phy_disconnect(slave_dev->phydev); 1312d945097bSVivien Didelot if (of_phy_is_fixed_link(port->dn)) 1313d945097bSVivien Didelot of_phy_deregister_fixed_link(port->dn); 1314e804441cSFlorian Fainelli out_free: 1315e804441cSFlorian Fainelli free_percpu(p->stats64); 1316e804441cSFlorian Fainelli free_netdev(slave_dev); 1317f8b8b1cdSVivien Didelot port->slave = NULL; 1318e804441cSFlorian Fainelli return ret; 131991da11f8SLennert Buytenhek } 1320b73adef6SFlorian Fainelli 1321cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1322cda5c15bSNeil Armstrong { 1323d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1324cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1325d945097bSVivien Didelot struct device_node *port_dn = dp->dn; 1326cda5c15bSNeil Armstrong 1327cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 13280115dcd1SVivien Didelot if (slave_dev->phydev) { 13290115dcd1SVivien Didelot phy_disconnect(slave_dev->phydev); 1330881eadabSJohan Hovold 1331881eadabSJohan Hovold if (of_phy_is_fixed_link(port_dn)) 1332881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1333881eadabSJohan Hovold } 13346158eaa7SVivien Didelot dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); 1335cda5c15bSNeil Armstrong unregister_netdev(slave_dev); 13365f6b4e14SFlorian Fainelli free_percpu(p->stats64); 1337cda5c15bSNeil Armstrong free_netdev(slave_dev); 1338cda5c15bSNeil Armstrong } 1339cda5c15bSNeil Armstrong 1340b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev) 1341b73adef6SFlorian Fainelli { 1342b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1343b73adef6SFlorian Fainelli } 1344b73adef6SFlorian Fainelli 13458e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 13468e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1347b73adef6SFlorian Fainelli { 1348d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 13498e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1350b73adef6SFlorian Fainelli 13518e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 13528e92ab3aSVivien Didelot if (info->linking) { 135317d7802bSVivien Didelot err = dsa_port_bridge_join(dp, info->upper_dev); 13548e92ab3aSVivien Didelot err = notifier_from_errno(err); 13558e92ab3aSVivien Didelot } else { 135617d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 13578e92ab3aSVivien Didelot err = NOTIFY_OK; 13588e92ab3aSVivien Didelot } 13596debb68aSVivien Didelot } 1360b73adef6SFlorian Fainelli 13618e92ab3aSVivien Didelot return err; 1362b73adef6SFlorian Fainelli } 1363b73adef6SFlorian Fainelli 136488e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 1365b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1366b73adef6SFlorian Fainelli { 13676debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1368b73adef6SFlorian Fainelli 136953bade8aSFlorian Fainelli if (!dsa_slave_dev_check(dev)) 13708e92ab3aSVivien Didelot return NOTIFY_DONE; 13718e92ab3aSVivien Didelot 13728e92ab3aSVivien Didelot if (event == NETDEV_CHANGEUPPER) 13738e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 1374b73adef6SFlorian Fainelli 1375b73adef6SFlorian Fainelli return NOTIFY_DONE; 1376b73adef6SFlorian Fainelli } 137788e4f0caSVivien Didelot 1378c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work { 1379c9eb3e0fSArkadi Sharshevsky struct work_struct work; 1380c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info fdb_info; 1381c9eb3e0fSArkadi Sharshevsky struct net_device *dev; 1382c9eb3e0fSArkadi Sharshevsky unsigned long event; 1383c9eb3e0fSArkadi Sharshevsky }; 1384c9eb3e0fSArkadi Sharshevsky 1385c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work) 1386c9eb3e0fSArkadi Sharshevsky { 1387c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work = 1388c9eb3e0fSArkadi Sharshevsky container_of(work, struct dsa_switchdev_event_work, work); 1389c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_work->dev; 1390c9eb3e0fSArkadi Sharshevsky struct switchdev_notifier_fdb_info *fdb_info; 1391d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev); 1392c9eb3e0fSArkadi Sharshevsky int err; 1393c9eb3e0fSArkadi Sharshevsky 1394c9eb3e0fSArkadi Sharshevsky rtnl_lock(); 1395c9eb3e0fSArkadi Sharshevsky switch (switchdev_work->event) { 1396c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: 1397c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1398d945097bSVivien Didelot err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid); 1399c9eb3e0fSArkadi Sharshevsky if (err) { 1400c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb add failed err=%d\n", err); 1401c9eb3e0fSArkadi Sharshevsky break; 1402c9eb3e0fSArkadi Sharshevsky } 1403c9eb3e0fSArkadi Sharshevsky call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 1404c9eb3e0fSArkadi Sharshevsky &fdb_info->info); 1405c9eb3e0fSArkadi Sharshevsky break; 1406c9eb3e0fSArkadi Sharshevsky 1407c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1408c9eb3e0fSArkadi Sharshevsky fdb_info = &switchdev_work->fdb_info; 1409d945097bSVivien Didelot err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid); 1410c9eb3e0fSArkadi Sharshevsky if (err) { 1411c9eb3e0fSArkadi Sharshevsky netdev_dbg(dev, "fdb del failed err=%d\n", err); 1412c9eb3e0fSArkadi Sharshevsky dev_close(dev); 1413c9eb3e0fSArkadi Sharshevsky } 1414c9eb3e0fSArkadi Sharshevsky break; 1415c9eb3e0fSArkadi Sharshevsky } 1416c9eb3e0fSArkadi Sharshevsky rtnl_unlock(); 1417c9eb3e0fSArkadi Sharshevsky 1418c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work->fdb_info.addr); 1419c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1420c9eb3e0fSArkadi Sharshevsky dev_put(dev); 1421c9eb3e0fSArkadi Sharshevsky } 1422c9eb3e0fSArkadi Sharshevsky 1423c9eb3e0fSArkadi Sharshevsky static int 1424c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * 1425c9eb3e0fSArkadi Sharshevsky switchdev_work, 1426c9eb3e0fSArkadi Sharshevsky const struct switchdev_notifier_fdb_info * 1427c9eb3e0fSArkadi Sharshevsky fdb_info) 1428c9eb3e0fSArkadi Sharshevsky { 1429c9eb3e0fSArkadi Sharshevsky memcpy(&switchdev_work->fdb_info, fdb_info, 1430c9eb3e0fSArkadi Sharshevsky sizeof(switchdev_work->fdb_info)); 1431c9eb3e0fSArkadi Sharshevsky switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 1432c9eb3e0fSArkadi Sharshevsky if (!switchdev_work->fdb_info.addr) 1433c9eb3e0fSArkadi Sharshevsky return -ENOMEM; 1434c9eb3e0fSArkadi Sharshevsky ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 1435c9eb3e0fSArkadi Sharshevsky fdb_info->addr); 1436c9eb3e0fSArkadi Sharshevsky return 0; 1437c9eb3e0fSArkadi Sharshevsky } 1438c9eb3e0fSArkadi Sharshevsky 1439c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */ 1440c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused, 1441c9eb3e0fSArkadi Sharshevsky unsigned long event, void *ptr) 1442c9eb3e0fSArkadi Sharshevsky { 1443c9eb3e0fSArkadi Sharshevsky struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1444c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work *switchdev_work; 1445c9eb3e0fSArkadi Sharshevsky 1446c9eb3e0fSArkadi Sharshevsky if (!dsa_slave_dev_check(dev)) 1447c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1448c9eb3e0fSArkadi Sharshevsky 1449c9eb3e0fSArkadi Sharshevsky switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 1450c9eb3e0fSArkadi Sharshevsky if (!switchdev_work) 1451c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1452c9eb3e0fSArkadi Sharshevsky 1453c9eb3e0fSArkadi Sharshevsky INIT_WORK(&switchdev_work->work, 1454c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_event_work); 1455c9eb3e0fSArkadi Sharshevsky switchdev_work->dev = dev; 1456c9eb3e0fSArkadi Sharshevsky switchdev_work->event = event; 1457c9eb3e0fSArkadi Sharshevsky 1458c9eb3e0fSArkadi Sharshevsky switch (event) { 1459c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ 1460c9eb3e0fSArkadi Sharshevsky case SWITCHDEV_FDB_DEL_TO_DEVICE: 1461c9eb3e0fSArkadi Sharshevsky if (dsa_slave_switchdev_fdb_work_init(switchdev_work, 1462c9eb3e0fSArkadi Sharshevsky ptr)) 1463c9eb3e0fSArkadi Sharshevsky goto err_fdb_work_init; 1464c9eb3e0fSArkadi Sharshevsky dev_hold(dev); 1465c9eb3e0fSArkadi Sharshevsky break; 1466c9eb3e0fSArkadi Sharshevsky default: 1467c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1468c9eb3e0fSArkadi Sharshevsky return NOTIFY_DONE; 1469c9eb3e0fSArkadi Sharshevsky } 1470c9eb3e0fSArkadi Sharshevsky 1471c9eb3e0fSArkadi Sharshevsky dsa_schedule_work(&switchdev_work->work); 1472c9eb3e0fSArkadi Sharshevsky return NOTIFY_OK; 1473c9eb3e0fSArkadi Sharshevsky 1474c9eb3e0fSArkadi Sharshevsky err_fdb_work_init: 1475c9eb3e0fSArkadi Sharshevsky kfree(switchdev_work); 1476c9eb3e0fSArkadi Sharshevsky return NOTIFY_BAD; 1477c9eb3e0fSArkadi Sharshevsky } 1478c9eb3e0fSArkadi Sharshevsky 147988e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 148088e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 148188e4f0caSVivien Didelot }; 148288e4f0caSVivien Didelot 1483c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = { 1484c9eb3e0fSArkadi Sharshevsky .notifier_call = dsa_slave_switchdev_event, 1485c9eb3e0fSArkadi Sharshevsky }; 1486c9eb3e0fSArkadi Sharshevsky 148788e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 148888e4f0caSVivien Didelot { 1489c9eb3e0fSArkadi Sharshevsky int err; 1490c9eb3e0fSArkadi Sharshevsky 1491c9eb3e0fSArkadi Sharshevsky err = register_netdevice_notifier(&dsa_slave_nb); 1492c9eb3e0fSArkadi Sharshevsky if (err) 1493c9eb3e0fSArkadi Sharshevsky return err; 1494c9eb3e0fSArkadi Sharshevsky 1495c9eb3e0fSArkadi Sharshevsky err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); 1496c9eb3e0fSArkadi Sharshevsky if (err) 1497c9eb3e0fSArkadi Sharshevsky goto err_switchdev_nb; 1498c9eb3e0fSArkadi Sharshevsky 1499c9eb3e0fSArkadi Sharshevsky return 0; 1500c9eb3e0fSArkadi Sharshevsky 1501c9eb3e0fSArkadi Sharshevsky err_switchdev_nb: 1502c9eb3e0fSArkadi Sharshevsky unregister_netdevice_notifier(&dsa_slave_nb); 1503c9eb3e0fSArkadi Sharshevsky return err; 150488e4f0caSVivien Didelot } 150588e4f0caSVivien Didelot 150688e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 150788e4f0caSVivien Didelot { 150888e4f0caSVivien Didelot int err; 150988e4f0caSVivien Didelot 1510c9eb3e0fSArkadi Sharshevsky err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); 1511c9eb3e0fSArkadi Sharshevsky if (err) 1512c9eb3e0fSArkadi Sharshevsky pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); 1513c9eb3e0fSArkadi Sharshevsky 151488e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 151588e4f0caSVivien Didelot if (err) 151688e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 151788e4f0caSVivien Didelot } 1518