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> 19f50f2127SFlorian Fainelli #include <linux/list.h> 20b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 21f50f2127SFlorian Fainelli #include <net/pkt_cls.h> 22f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h> 23b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2404ff53f9SFlorian Fainelli #include <linux/netpoll.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", 580b7b498dSFlorian Fainelli ds->dst->tree, 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 { 67c0840801SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 68c0840801SLennert Buytenhek 69afdcf151SVivien Didelot return p->dp->ds->dst->master_netdev->ifindex; 70c0840801SLennert Buytenhek } 71c0840801SLennert Buytenhek 7291da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 7391da11f8SLennert Buytenhek { 74df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 75f3d736c4SVivien Didelot struct dsa_port *dp = p->dp; 76f3d736c4SVivien Didelot struct dsa_switch *ds = dp->ds; 77f3d736c4SVivien Didelot struct net_device *master = ds->dst->master_netdev; 78f3d736c4SVivien Didelot u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING; 79df02c6ffSLennert Buytenhek int err; 80df02c6ffSLennert Buytenhek 81df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 82df02c6ffSLennert Buytenhek return -ENETDOWN; 83df02c6ffSLennert Buytenhek 848feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 85a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 86df02c6ffSLennert Buytenhek if (err < 0) 87df02c6ffSLennert Buytenhek goto out; 88df02c6ffSLennert Buytenhek } 89df02c6ffSLennert Buytenhek 90df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 91df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 92df02c6ffSLennert Buytenhek if (err < 0) 93df02c6ffSLennert Buytenhek goto del_unicast; 94df02c6ffSLennert Buytenhek } 95df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 96df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 97df02c6ffSLennert Buytenhek if (err < 0) 98df02c6ffSLennert Buytenhek goto clear_allmulti; 99df02c6ffSLennert Buytenhek } 100df02c6ffSLennert Buytenhek 1019d490b4eSVivien Didelot if (ds->ops->port_enable) { 102afdcf151SVivien Didelot err = ds->ops->port_enable(ds, p->dp->index, p->phy); 103b2f2af21SFlorian Fainelli if (err) 104b2f2af21SFlorian Fainelli goto clear_promisc; 105b2f2af21SFlorian Fainelli } 106b2f2af21SFlorian Fainelli 107fd364541SVivien Didelot dsa_port_set_state_now(p->dp, stp_state); 108b73adef6SFlorian Fainelli 109f7f1de51SFlorian Fainelli if (p->phy) 110f7f1de51SFlorian Fainelli phy_start(p->phy); 111f7f1de51SFlorian Fainelli 11291da11f8SLennert Buytenhek return 0; 113df02c6ffSLennert Buytenhek 114b2f2af21SFlorian Fainelli clear_promisc: 115b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 1164fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 117df02c6ffSLennert Buytenhek clear_allmulti: 118df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 119df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 120df02c6ffSLennert Buytenhek del_unicast: 1218feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 122a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 123df02c6ffSLennert Buytenhek out: 124df02c6ffSLennert Buytenhek return err; 12591da11f8SLennert Buytenhek } 12691da11f8SLennert Buytenhek 12791da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 12891da11f8SLennert Buytenhek { 129df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 130afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 131afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 132df02c6ffSLennert Buytenhek 133f7f1de51SFlorian Fainelli if (p->phy) 134f7f1de51SFlorian Fainelli phy_stop(p->phy); 135f7f1de51SFlorian Fainelli 136df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 137a748ee24SJiri Pirko dev_uc_unsync(master, dev); 138df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 139df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 140df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 141df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 142df02c6ffSLennert Buytenhek 1438feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 144a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 145df02c6ffSLennert Buytenhek 1469d490b4eSVivien Didelot if (ds->ops->port_disable) 147afdcf151SVivien Didelot ds->ops->port_disable(ds, p->dp->index, p->phy); 148b2f2af21SFlorian Fainelli 149fd364541SVivien Didelot dsa_port_set_state_now(p->dp, BR_STATE_DISABLED); 150b73adef6SFlorian Fainelli 15191da11f8SLennert Buytenhek return 0; 15291da11f8SLennert Buytenhek } 15391da11f8SLennert Buytenhek 15491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 15591da11f8SLennert Buytenhek { 15691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 157afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 15891da11f8SLennert Buytenhek 15991da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 16091da11f8SLennert Buytenhek dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 16191da11f8SLennert Buytenhek if (change & IFF_PROMISC) 16291da11f8SLennert Buytenhek dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 16391da11f8SLennert Buytenhek } 16491da11f8SLennert Buytenhek 16591da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 16691da11f8SLennert Buytenhek { 16791da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 168afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 16991da11f8SLennert Buytenhek 17091da11f8SLennert Buytenhek dev_mc_sync(master, dev); 171a748ee24SJiri Pirko dev_uc_sync(master, dev); 17291da11f8SLennert Buytenhek } 17391da11f8SLennert Buytenhek 174df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 17591da11f8SLennert Buytenhek { 176df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 177afdcf151SVivien Didelot struct net_device *master = p->dp->ds->dst->master_netdev; 178df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 179df02c6ffSLennert Buytenhek int err; 180df02c6ffSLennert Buytenhek 181df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 182df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 183df02c6ffSLennert Buytenhek 184df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 185df02c6ffSLennert Buytenhek goto out; 186df02c6ffSLennert Buytenhek 1878feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 188a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 189df02c6ffSLennert Buytenhek if (err < 0) 190df02c6ffSLennert Buytenhek return err; 191df02c6ffSLennert Buytenhek } 192df02c6ffSLennert Buytenhek 1938feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 194a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 195df02c6ffSLennert Buytenhek 196df02c6ffSLennert Buytenhek out: 197d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 19891da11f8SLennert Buytenhek 19991da11f8SLennert Buytenhek return 0; 20091da11f8SLennert Buytenhek } 20191da11f8SLennert Buytenhek 20291da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 20391da11f8SLennert Buytenhek { 20491da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 20591da11f8SLennert Buytenhek 20691da11f8SLennert Buytenhek if (p->phy != NULL) 20728b04113SRichard Cochran return phy_mii_ioctl(p->phy, ifr, cmd); 20891da11f8SLennert Buytenhek 20991da11f8SLennert Buytenhek return -EOPNOTSUPP; 21091da11f8SLennert Buytenhek } 21191da11f8SLennert Buytenhek 21235636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 213f7fadf30SJiri Pirko const struct switchdev_attr *attr, 2147ea6eb3fSJiri Pirko struct switchdev_trans *trans) 21535636062SScott Feldman { 216fd364541SVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 217fd364541SVivien Didelot struct dsa_port *dp = p->dp; 218b8d866acSVivien Didelot int ret; 21935636062SScott Feldman 22035636062SScott Feldman switch (attr->id) { 2211f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 222fd364541SVivien Didelot ret = dsa_port_set_state(dp, attr->u.stp_state, trans); 22335636062SScott Feldman break; 224fb2dabadSVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 225c02c4175SVivien Didelot ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, 226c02c4175SVivien Didelot trans); 227fb2dabadSVivien Didelot break; 22834a79f63SVivien Didelot case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 229072bb190SVivien Didelot ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); 23034a79f63SVivien Didelot break; 23135636062SScott Feldman default: 23235636062SScott Feldman ret = -EOPNOTSUPP; 23335636062SScott Feldman break; 23435636062SScott Feldman } 23535636062SScott Feldman 23635636062SScott Feldman return ret; 23735636062SScott Feldman } 23835636062SScott Feldman 239ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev, 240648b4a99SJiri Pirko const struct switchdev_obj *obj, 2417ea6eb3fSJiri Pirko struct switchdev_trans *trans) 242ba14d9ebSVivien Didelot { 2433fdb023bSVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 2443fdb023bSVivien Didelot struct dsa_port *dp = p->dp; 245ba14d9ebSVivien Didelot int err; 246ba14d9ebSVivien Didelot 247ba14d9ebSVivien Didelot /* For the prepare phase, ensure the full set of changes is feasable in 248ba14d9ebSVivien Didelot * one go in order to signal a failure properly. If an operation is not 249ba14d9ebSVivien Didelot * supported, return -EOPNOTSUPP. 250ba14d9ebSVivien Didelot */ 251ba14d9ebSVivien Didelot 2529e8f4a54SJiri Pirko switch (obj->id) { 25357d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 2543fdb023bSVivien Didelot err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans); 255ba14d9ebSVivien Didelot break; 2568df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 257bcebb976SVivien Didelot err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); 2588df30255SVivien Didelot break; 25957d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 26001676d12SVivien Didelot err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), 261648b4a99SJiri Pirko trans); 26211149536SVivien Didelot break; 263ba14d9ebSVivien Didelot default: 264ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 265ba14d9ebSVivien Didelot break; 266ba14d9ebSVivien Didelot } 267ba14d9ebSVivien Didelot 268ba14d9ebSVivien Didelot return err; 269ba14d9ebSVivien Didelot } 270ba14d9ebSVivien Didelot 271ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev, 272648b4a99SJiri Pirko const struct switchdev_obj *obj) 273ba14d9ebSVivien Didelot { 2743fdb023bSVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 2753fdb023bSVivien Didelot struct dsa_port *dp = p->dp; 276ba14d9ebSVivien Didelot int err; 277ba14d9ebSVivien Didelot 2789e8f4a54SJiri Pirko switch (obj->id) { 27957d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 2803fdb023bSVivien Didelot err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj)); 281ba14d9ebSVivien Didelot break; 2828df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 283bcebb976SVivien Didelot err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); 2848df30255SVivien Didelot break; 28557d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 28601676d12SVivien Didelot err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); 28711149536SVivien Didelot break; 288ba14d9ebSVivien Didelot default: 289ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 290ba14d9ebSVivien Didelot break; 291ba14d9ebSVivien Didelot } 292ba14d9ebSVivien Didelot 293ba14d9ebSVivien Didelot return err; 294ba14d9ebSVivien Didelot } 295ba14d9ebSVivien Didelot 296ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev, 297648b4a99SJiri Pirko struct switchdev_obj *obj, 298648b4a99SJiri Pirko switchdev_obj_dump_cb_t *cb) 299ba14d9ebSVivien Didelot { 3003fdb023bSVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 3013fdb023bSVivien Didelot struct dsa_port *dp = p->dp; 302ba14d9ebSVivien Didelot int err; 303ba14d9ebSVivien Didelot 3049e8f4a54SJiri Pirko switch (obj->id) { 30557d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_FDB: 3063fdb023bSVivien Didelot err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb); 307ba14d9ebSVivien Didelot break; 3088df30255SVivien Didelot case SWITCHDEV_OBJ_ID_PORT_MDB: 309bcebb976SVivien Didelot err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb); 3108df30255SVivien Didelot break; 31157d80838SJiri Pirko case SWITCHDEV_OBJ_ID_PORT_VLAN: 31201676d12SVivien Didelot err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb); 31311149536SVivien Didelot break; 314ba14d9ebSVivien Didelot default: 315ba14d9ebSVivien Didelot err = -EOPNOTSUPP; 316ba14d9ebSVivien Didelot break; 317ba14d9ebSVivien Didelot } 318ba14d9ebSVivien Didelot 319ba14d9ebSVivien Didelot return err; 320ba14d9ebSVivien Didelot } 321ba14d9ebSVivien Didelot 322f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev, 323f8e20a9fSScott Feldman struct switchdev_attr *attr) 324b73adef6SFlorian Fainelli { 325b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 326afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 327b73adef6SFlorian Fainelli 328f8e20a9fSScott Feldman switch (attr->id) { 3291f868398SJiri Pirko case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 33042275bd8SScott Feldman attr->u.ppid.id_len = sizeof(ds->index); 33142275bd8SScott Feldman memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); 332f8e20a9fSScott Feldman break; 333f8e20a9fSScott Feldman default: 334f8e20a9fSScott Feldman return -EOPNOTSUPP; 335f8e20a9fSScott Feldman } 336b73adef6SFlorian Fainelli 337b73adef6SFlorian Fainelli return 0; 338b73adef6SFlorian Fainelli } 339b73adef6SFlorian Fainelli 34004ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, 34104ff53f9SFlorian Fainelli struct sk_buff *skb) 34204ff53f9SFlorian Fainelli { 34304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 34404ff53f9SFlorian Fainelli if (p->netpoll) 34504ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 34604ff53f9SFlorian Fainelli #else 34704ff53f9SFlorian Fainelli BUG(); 34804ff53f9SFlorian Fainelli #endif 34904ff53f9SFlorian Fainelli return NETDEV_TX_OK; 35004ff53f9SFlorian Fainelli } 35104ff53f9SFlorian Fainelli 3523e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 3533e8a72d1SFlorian Fainelli { 3543e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 3554ed70ce9SFlorian Fainelli struct sk_buff *nskb; 3563e8a72d1SFlorian Fainelli 3574ed70ce9SFlorian Fainelli dev->stats.tx_packets++; 3584ed70ce9SFlorian Fainelli dev->stats.tx_bytes += skb->len; 3593e8a72d1SFlorian Fainelli 360fe47d563SVivien Didelot /* Transmit function may have to reallocate the original SKB, 361fe47d563SVivien Didelot * in which case it must have freed it. Only free it here on error. 362fe47d563SVivien Didelot */ 3634ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 364fe47d563SVivien Didelot if (!nskb) { 365fe47d563SVivien Didelot kfree_skb(skb); 3664ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 367fe47d563SVivien Didelot } 3685aed85ceSFlorian Fainelli 36904ff53f9SFlorian Fainelli /* SKB for netpoll still need to be mangled with the protocol-specific 37004ff53f9SFlorian Fainelli * tag to be successfully transmitted 37104ff53f9SFlorian Fainelli */ 37204ff53f9SFlorian Fainelli if (unlikely(netpoll_tx_running(dev))) 37304ff53f9SFlorian Fainelli return dsa_netpoll_send_skb(p, nskb); 37404ff53f9SFlorian Fainelli 3754ed70ce9SFlorian Fainelli /* Queue the SKB for transmission on the parent interface, but 3764ed70ce9SFlorian Fainelli * do not modify its EtherType 3774ed70ce9SFlorian Fainelli */ 378afdcf151SVivien Didelot nskb->dev = p->dp->ds->dst->master_netdev; 3794ed70ce9SFlorian Fainelli dev_queue_xmit(nskb); 3805aed85ceSFlorian Fainelli 3815aed85ceSFlorian Fainelli return NETDEV_TX_OK; 3825aed85ceSFlorian Fainelli } 3835aed85ceSFlorian Fainelli 38491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 38591da11f8SLennert Buytenhek static int 386bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev, 387bb10bb3eSPhilippe Reynes struct ethtool_link_ksettings *cmd) 38891da11f8SLennert Buytenhek { 38991da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 39091da11f8SLennert Buytenhek 391*5514174fSyuval.shaia@oracle.com if (!p->phy) 392*5514174fSyuval.shaia@oracle.com return -EOPNOTSUPP; 39391da11f8SLennert Buytenhek 394*5514174fSyuval.shaia@oracle.com phy_ethtool_ksettings_get(p->phy, cmd); 395*5514174fSyuval.shaia@oracle.com 396*5514174fSyuval.shaia@oracle.com return 0; 39791da11f8SLennert Buytenhek } 39891da11f8SLennert Buytenhek 39991da11f8SLennert Buytenhek static int 400bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev, 401bb10bb3eSPhilippe Reynes const struct ethtool_link_ksettings *cmd) 40291da11f8SLennert Buytenhek { 40391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 40491da11f8SLennert Buytenhek 40591da11f8SLennert Buytenhek if (p->phy != NULL) 406bb10bb3eSPhilippe Reynes return phy_ethtool_ksettings_set(p->phy, cmd); 40791da11f8SLennert Buytenhek 40891da11f8SLennert Buytenhek return -EOPNOTSUPP; 40991da11f8SLennert Buytenhek } 41091da11f8SLennert Buytenhek 41191da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 41291da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 41391da11f8SLennert Buytenhek { 4147826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 4157826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 4167826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 41791da11f8SLennert Buytenhek } 41891da11f8SLennert Buytenhek 4193d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 4203d762a0fSGuenter Roeck { 4213d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 422afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 4233d762a0fSGuenter Roeck 4249d490b4eSVivien Didelot if (ds->ops->get_regs_len) 425afdcf151SVivien Didelot return ds->ops->get_regs_len(ds, p->dp->index); 4263d762a0fSGuenter Roeck 4273d762a0fSGuenter Roeck return -EOPNOTSUPP; 4283d762a0fSGuenter Roeck } 4293d762a0fSGuenter Roeck 4303d762a0fSGuenter Roeck static void 4313d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 4323d762a0fSGuenter Roeck { 4333d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 434afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 4353d762a0fSGuenter Roeck 4369d490b4eSVivien Didelot if (ds->ops->get_regs) 437afdcf151SVivien Didelot ds->ops->get_regs(ds, p->dp->index, regs, _p); 4383d762a0fSGuenter Roeck } 4393d762a0fSGuenter Roeck 44091da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev) 44191da11f8SLennert Buytenhek { 44291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 44391da11f8SLennert Buytenhek 44491da11f8SLennert Buytenhek if (p->phy != NULL) 44591da11f8SLennert Buytenhek return genphy_restart_aneg(p->phy); 44691da11f8SLennert Buytenhek 44791da11f8SLennert Buytenhek return -EOPNOTSUPP; 44891da11f8SLennert Buytenhek } 44991da11f8SLennert Buytenhek 45091da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 45191da11f8SLennert Buytenhek { 45291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 45391da11f8SLennert Buytenhek 45491da11f8SLennert Buytenhek if (p->phy != NULL) { 45591da11f8SLennert Buytenhek genphy_update_link(p->phy); 45691da11f8SLennert Buytenhek return p->phy->link; 45791da11f8SLennert Buytenhek } 45891da11f8SLennert Buytenhek 45991da11f8SLennert Buytenhek return -EOPNOTSUPP; 46091da11f8SLennert Buytenhek } 46191da11f8SLennert Buytenhek 4626793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 4636793abb4SGuenter Roeck { 4646793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 465afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 4666793abb4SGuenter Roeck 4670e576044SAndrew Lunn if (ds->cd && ds->cd->eeprom_len) 468ff04955cSAndrew Lunn return ds->cd->eeprom_len; 4696793abb4SGuenter Roeck 4709d490b4eSVivien Didelot if (ds->ops->get_eeprom_len) 4719d490b4eSVivien Didelot return ds->ops->get_eeprom_len(ds); 4726793abb4SGuenter Roeck 4736793abb4SGuenter Roeck return 0; 4746793abb4SGuenter Roeck } 4756793abb4SGuenter Roeck 4766793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 4776793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 4786793abb4SGuenter Roeck { 4796793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 480afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 4816793abb4SGuenter Roeck 4829d490b4eSVivien Didelot if (ds->ops->get_eeprom) 4839d490b4eSVivien Didelot return ds->ops->get_eeprom(ds, eeprom, data); 4846793abb4SGuenter Roeck 4856793abb4SGuenter Roeck return -EOPNOTSUPP; 4866793abb4SGuenter Roeck } 4876793abb4SGuenter Roeck 4886793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 4896793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 4906793abb4SGuenter Roeck { 4916793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 492afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 4936793abb4SGuenter Roeck 4949d490b4eSVivien Didelot if (ds->ops->set_eeprom) 4959d490b4eSVivien Didelot return ds->ops->set_eeprom(ds, eeprom, data); 4966793abb4SGuenter Roeck 4976793abb4SGuenter Roeck return -EOPNOTSUPP; 4986793abb4SGuenter Roeck } 4996793abb4SGuenter Roeck 50091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 50191da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 50291da11f8SLennert Buytenhek { 50391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 504afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 50591da11f8SLennert Buytenhek 50691da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 50791da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 50891da11f8SLennert Buytenhek 50991da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 51091da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 51191da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 51291da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 5139d490b4eSVivien Didelot if (ds->ops->get_strings) 514afdcf151SVivien Didelot ds->ops->get_strings(ds, p->dp->index, data + 4 * len); 51591da11f8SLennert Buytenhek } 51691da11f8SLennert Buytenhek } 51791da11f8SLennert Buytenhek 518badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev, 519badf3adaSFlorian Fainelli struct ethtool_stats *stats, 520badf3adaSFlorian Fainelli uint64_t *data) 521badf3adaSFlorian Fainelli { 522badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 5238b0d3ea5SVivien Didelot struct dsa_switch *ds = dst->cpu_dp->ds; 5248b0d3ea5SVivien Didelot s8 cpu_port = dst->cpu_dp->index; 525badf3adaSFlorian Fainelli int count = 0; 526badf3adaSFlorian Fainelli 527badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) { 528badf3adaSFlorian Fainelli count = dst->master_ethtool_ops.get_sset_count(dev, 529badf3adaSFlorian Fainelli ETH_SS_STATS); 530badf3adaSFlorian Fainelli dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data); 531badf3adaSFlorian Fainelli } 532badf3adaSFlorian Fainelli 5339d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 5349d490b4eSVivien Didelot ds->ops->get_ethtool_stats(ds, cpu_port, data + count); 535badf3adaSFlorian Fainelli } 536badf3adaSFlorian Fainelli 537badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset) 538badf3adaSFlorian Fainelli { 539badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 5408b0d3ea5SVivien Didelot struct dsa_switch *ds = dst->cpu_dp->ds; 541badf3adaSFlorian Fainelli int count = 0; 542badf3adaSFlorian Fainelli 543badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) 544badf3adaSFlorian Fainelli count += dst->master_ethtool_ops.get_sset_count(dev, sset); 545badf3adaSFlorian Fainelli 5469d490b4eSVivien Didelot if (sset == ETH_SS_STATS && ds->ops->get_sset_count) 5479d490b4eSVivien Didelot count += ds->ops->get_sset_count(ds); 548badf3adaSFlorian Fainelli 549badf3adaSFlorian Fainelli return count; 550badf3adaSFlorian Fainelli } 551badf3adaSFlorian Fainelli 552badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev, 553badf3adaSFlorian Fainelli uint32_t stringset, uint8_t *data) 554badf3adaSFlorian Fainelli { 555badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = dev->dsa_ptr; 5568b0d3ea5SVivien Didelot struct dsa_switch *ds = dst->cpu_dp->ds; 5578b0d3ea5SVivien Didelot s8 cpu_port = dst->cpu_dp->index; 558badf3adaSFlorian Fainelli int len = ETH_GSTRING_LEN; 559badf3adaSFlorian Fainelli int mcount = 0, count; 560badf3adaSFlorian Fainelli unsigned int i; 561badf3adaSFlorian Fainelli uint8_t pfx[4]; 562badf3adaSFlorian Fainelli uint8_t *ndata; 563badf3adaSFlorian Fainelli 564badf3adaSFlorian Fainelli snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port); 565badf3adaSFlorian Fainelli /* We do not want to be NULL-terminated, since this is a prefix */ 566badf3adaSFlorian Fainelli pfx[sizeof(pfx) - 1] = '_'; 567badf3adaSFlorian Fainelli 568badf3adaSFlorian Fainelli if (dst->master_ethtool_ops.get_sset_count) { 569badf3adaSFlorian Fainelli mcount = dst->master_ethtool_ops.get_sset_count(dev, 570badf3adaSFlorian Fainelli ETH_SS_STATS); 571badf3adaSFlorian Fainelli dst->master_ethtool_ops.get_strings(dev, stringset, data); 572badf3adaSFlorian Fainelli } 573badf3adaSFlorian Fainelli 5749d490b4eSVivien Didelot if (stringset == ETH_SS_STATS && ds->ops->get_strings) { 575badf3adaSFlorian Fainelli ndata = data + mcount * len; 576badf3adaSFlorian Fainelli /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 577badf3adaSFlorian Fainelli * the output after to prepend our CPU port prefix we 578badf3adaSFlorian Fainelli * constructed earlier 579badf3adaSFlorian Fainelli */ 5809d490b4eSVivien Didelot ds->ops->get_strings(ds, cpu_port, ndata); 5819d490b4eSVivien Didelot count = ds->ops->get_sset_count(ds); 582badf3adaSFlorian Fainelli for (i = 0; i < count; i++) { 583badf3adaSFlorian Fainelli memmove(ndata + (i * len + sizeof(pfx)), 584badf3adaSFlorian Fainelli ndata + i * len, len - sizeof(pfx)); 585badf3adaSFlorian Fainelli memcpy(ndata + i * len, pfx, sizeof(pfx)); 586badf3adaSFlorian Fainelli } 587badf3adaSFlorian Fainelli } 588badf3adaSFlorian Fainelli } 589badf3adaSFlorian Fainelli 59091da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 59191da11f8SLennert Buytenhek struct ethtool_stats *stats, 59291da11f8SLennert Buytenhek uint64_t *data) 59391da11f8SLennert Buytenhek { 59491da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 595afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 59691da11f8SLennert Buytenhek 59746e7b8d8SVivien Didelot data[0] = dev->stats.tx_packets; 59846e7b8d8SVivien Didelot data[1] = dev->stats.tx_bytes; 59946e7b8d8SVivien Didelot data[2] = dev->stats.rx_packets; 60046e7b8d8SVivien Didelot data[3] = dev->stats.rx_bytes; 6019d490b4eSVivien Didelot if (ds->ops->get_ethtool_stats) 602afdcf151SVivien Didelot ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4); 60391da11f8SLennert Buytenhek } 60491da11f8SLennert Buytenhek 60591da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 60691da11f8SLennert Buytenhek { 60791da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 608afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 60991da11f8SLennert Buytenhek 61091da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 61191da11f8SLennert Buytenhek int count; 61291da11f8SLennert Buytenhek 61391da11f8SLennert Buytenhek count = 4; 6149d490b4eSVivien Didelot if (ds->ops->get_sset_count) 6159d490b4eSVivien Didelot count += ds->ops->get_sset_count(ds); 61691da11f8SLennert Buytenhek 61791da11f8SLennert Buytenhek return count; 61891da11f8SLennert Buytenhek } 61991da11f8SLennert Buytenhek 62091da11f8SLennert Buytenhek return -EOPNOTSUPP; 62191da11f8SLennert Buytenhek } 62291da11f8SLennert Buytenhek 62319e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 62419e57c4eSFlorian Fainelli { 62519e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 626afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 62719e57c4eSFlorian Fainelli 6289d490b4eSVivien Didelot if (ds->ops->get_wol) 629afdcf151SVivien Didelot ds->ops->get_wol(ds, p->dp->index, w); 63019e57c4eSFlorian Fainelli } 63119e57c4eSFlorian Fainelli 63219e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 63319e57c4eSFlorian Fainelli { 63419e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 635afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 63619e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 63719e57c4eSFlorian Fainelli 6389d490b4eSVivien Didelot if (ds->ops->set_wol) 639afdcf151SVivien Didelot ret = ds->ops->set_wol(ds, p->dp->index, w); 64019e57c4eSFlorian Fainelli 64119e57c4eSFlorian Fainelli return ret; 64219e57c4eSFlorian Fainelli } 64319e57c4eSFlorian Fainelli 6447905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 6457905288fSFlorian Fainelli { 6467905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 647afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 6487905288fSFlorian Fainelli int ret; 6497905288fSFlorian Fainelli 6509d490b4eSVivien Didelot if (!ds->ops->set_eee) 6517905288fSFlorian Fainelli return -EOPNOTSUPP; 6527905288fSFlorian Fainelli 653afdcf151SVivien Didelot ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e); 6547905288fSFlorian Fainelli if (ret) 6557905288fSFlorian Fainelli return ret; 6567905288fSFlorian Fainelli 6577905288fSFlorian Fainelli if (p->phy) 6587905288fSFlorian Fainelli ret = phy_ethtool_set_eee(p->phy, e); 6597905288fSFlorian Fainelli 6607905288fSFlorian Fainelli return ret; 6617905288fSFlorian Fainelli } 6627905288fSFlorian Fainelli 6637905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 6647905288fSFlorian Fainelli { 6657905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 666afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 6677905288fSFlorian Fainelli int ret; 6687905288fSFlorian Fainelli 6699d490b4eSVivien Didelot if (!ds->ops->get_eee) 6707905288fSFlorian Fainelli return -EOPNOTSUPP; 6717905288fSFlorian Fainelli 672afdcf151SVivien Didelot ret = ds->ops->get_eee(ds, p->dp->index, e); 6737905288fSFlorian Fainelli if (ret) 6747905288fSFlorian Fainelli return ret; 6757905288fSFlorian Fainelli 6767905288fSFlorian Fainelli if (p->phy) 6777905288fSFlorian Fainelli ret = phy_ethtool_get_eee(p->phy, e); 6787905288fSFlorian Fainelli 6797905288fSFlorian Fainelli return ret; 6807905288fSFlorian Fainelli } 6817905288fSFlorian Fainelli 68204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 68304ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 68404ff53f9SFlorian Fainelli struct netpoll_info *ni) 68504ff53f9SFlorian Fainelli { 68604ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 687afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 68804ff53f9SFlorian Fainelli struct net_device *master = ds->dst->master_netdev; 68904ff53f9SFlorian Fainelli struct netpoll *netpoll; 69004ff53f9SFlorian Fainelli int err = 0; 69104ff53f9SFlorian Fainelli 69204ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 69304ff53f9SFlorian Fainelli if (!netpoll) 69404ff53f9SFlorian Fainelli return -ENOMEM; 69504ff53f9SFlorian Fainelli 69604ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 69704ff53f9SFlorian Fainelli if (err) { 69804ff53f9SFlorian Fainelli kfree(netpoll); 69904ff53f9SFlorian Fainelli goto out; 70004ff53f9SFlorian Fainelli } 70104ff53f9SFlorian Fainelli 70204ff53f9SFlorian Fainelli p->netpoll = netpoll; 70304ff53f9SFlorian Fainelli out: 70404ff53f9SFlorian Fainelli return err; 70504ff53f9SFlorian Fainelli } 70604ff53f9SFlorian Fainelli 70704ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 70804ff53f9SFlorian Fainelli { 70904ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 71004ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 71104ff53f9SFlorian Fainelli 71204ff53f9SFlorian Fainelli if (!netpoll) 71304ff53f9SFlorian Fainelli return; 71404ff53f9SFlorian Fainelli 71504ff53f9SFlorian Fainelli p->netpoll = NULL; 71604ff53f9SFlorian Fainelli 71704ff53f9SFlorian Fainelli __netpoll_free_async(netpoll); 71804ff53f9SFlorian Fainelli } 71904ff53f9SFlorian Fainelli 72004ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 72104ff53f9SFlorian Fainelli { 72204ff53f9SFlorian Fainelli } 72304ff53f9SFlorian Fainelli #endif 72404ff53f9SFlorian Fainelli 72544bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev, 72644bb765cSFlorian Fainelli char *name, size_t len) 72744bb765cSFlorian Fainelli { 72844bb765cSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 72944bb765cSFlorian Fainelli 730afdcf151SVivien Didelot if (snprintf(name, len, "p%d", p->dp->index) >= len) 73144bb765cSFlorian Fainelli return -EINVAL; 7323a543ef4SFlorian Fainelli 7333a543ef4SFlorian Fainelli return 0; 7343a543ef4SFlorian Fainelli } 7353a543ef4SFlorian Fainelli 736f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry * 737f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p, 738f50f2127SFlorian Fainelli unsigned long cookie) 739f50f2127SFlorian Fainelli { 740f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 741f50f2127SFlorian Fainelli 742f50f2127SFlorian Fainelli list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) 743f50f2127SFlorian Fainelli if (mall_tc_entry->cookie == cookie) 744f50f2127SFlorian Fainelli return mall_tc_entry; 745f50f2127SFlorian Fainelli 746f50f2127SFlorian Fainelli return NULL; 747f50f2127SFlorian Fainelli } 748f50f2127SFlorian Fainelli 749f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev, 750f50f2127SFlorian Fainelli __be16 protocol, 751f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls, 752f50f2127SFlorian Fainelli bool ingress) 753f50f2127SFlorian Fainelli { 754f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 755f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 756f50f2127SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 757f50f2127SFlorian Fainelli struct net *net = dev_net(dev); 758f50f2127SFlorian Fainelli struct dsa_slave_priv *to_p; 759f50f2127SFlorian Fainelli struct net_device *to_dev; 760f50f2127SFlorian Fainelli const struct tc_action *a; 761f50f2127SFlorian Fainelli int err = -EOPNOTSUPP; 762f50f2127SFlorian Fainelli LIST_HEAD(actions); 763f50f2127SFlorian Fainelli int ifindex; 764f50f2127SFlorian Fainelli 765f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_add) 766f50f2127SFlorian Fainelli return err; 767f50f2127SFlorian Fainelli 768f50f2127SFlorian Fainelli if (!tc_single_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 777f50f2127SFlorian Fainelli ifindex = tcf_mirred_ifindex(a); 778f50f2127SFlorian Fainelli to_dev = __dev_get_by_index(net, ifindex); 779f50f2127SFlorian Fainelli if (!to_dev) 780f50f2127SFlorian Fainelli return -EINVAL; 781f50f2127SFlorian Fainelli 782f50f2127SFlorian Fainelli if (!dsa_slave_dev_check(to_dev)) 783f50f2127SFlorian Fainelli return -EOPNOTSUPP; 784f50f2127SFlorian Fainelli 785f50f2127SFlorian Fainelli mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); 786f50f2127SFlorian Fainelli if (!mall_tc_entry) 787f50f2127SFlorian Fainelli return -ENOMEM; 788f50f2127SFlorian Fainelli 789f50f2127SFlorian Fainelli mall_tc_entry->cookie = cls->cookie; 790f50f2127SFlorian Fainelli mall_tc_entry->type = DSA_PORT_MALL_MIRROR; 791f50f2127SFlorian Fainelli mirror = &mall_tc_entry->mirror; 792f50f2127SFlorian Fainelli 793f50f2127SFlorian Fainelli to_p = netdev_priv(to_dev); 794f50f2127SFlorian Fainelli 795f50f2127SFlorian Fainelli mirror->to_local_port = to_p->dp->index; 796f50f2127SFlorian Fainelli mirror->ingress = ingress; 797f50f2127SFlorian Fainelli 798f50f2127SFlorian Fainelli err = ds->ops->port_mirror_add(ds, p->dp->index, mirror, 799f50f2127SFlorian Fainelli ingress); 800f50f2127SFlorian Fainelli if (err) { 801f50f2127SFlorian Fainelli kfree(mall_tc_entry); 802f50f2127SFlorian Fainelli return err; 803f50f2127SFlorian Fainelli } 804f50f2127SFlorian Fainelli 805f50f2127SFlorian Fainelli list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); 806f50f2127SFlorian Fainelli } 807f50f2127SFlorian Fainelli 808f50f2127SFlorian Fainelli return 0; 809f50f2127SFlorian Fainelli } 810f50f2127SFlorian Fainelli 811f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev, 812f50f2127SFlorian Fainelli struct tc_cls_matchall_offload *cls) 813f50f2127SFlorian Fainelli { 814f50f2127SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 815f50f2127SFlorian Fainelli struct dsa_mall_tc_entry *mall_tc_entry; 816f50f2127SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 817f50f2127SFlorian Fainelli 818f50f2127SFlorian Fainelli if (!ds->ops->port_mirror_del) 819f50f2127SFlorian Fainelli return; 820f50f2127SFlorian Fainelli 821f50f2127SFlorian Fainelli mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie); 822f50f2127SFlorian Fainelli if (!mall_tc_entry) 823f50f2127SFlorian Fainelli return; 824f50f2127SFlorian Fainelli 825f50f2127SFlorian Fainelli list_del(&mall_tc_entry->list); 826f50f2127SFlorian Fainelli 827f50f2127SFlorian Fainelli switch (mall_tc_entry->type) { 828f50f2127SFlorian Fainelli case DSA_PORT_MALL_MIRROR: 829f50f2127SFlorian Fainelli ds->ops->port_mirror_del(ds, p->dp->index, 830f50f2127SFlorian Fainelli &mall_tc_entry->mirror); 831f50f2127SFlorian Fainelli break; 832f50f2127SFlorian Fainelli default: 833f50f2127SFlorian Fainelli WARN_ON(1); 834f50f2127SFlorian Fainelli } 835f50f2127SFlorian Fainelli 836f50f2127SFlorian Fainelli kfree(mall_tc_entry); 837f50f2127SFlorian Fainelli } 838f50f2127SFlorian Fainelli 839f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle, 840a5fcf8a6SJiri Pirko u32 chain_index, __be16 protocol, 841a5fcf8a6SJiri Pirko struct tc_to_netdev *tc) 842f50f2127SFlorian Fainelli { 843f50f2127SFlorian Fainelli bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS); 844a5fcf8a6SJiri Pirko 845a5fcf8a6SJiri Pirko if (chain_index) 846a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 847f50f2127SFlorian Fainelli 848f50f2127SFlorian Fainelli switch (tc->type) { 849f50f2127SFlorian Fainelli case TC_SETUP_MATCHALL: 850f50f2127SFlorian Fainelli switch (tc->cls_mall->command) { 851f50f2127SFlorian Fainelli case TC_CLSMATCHALL_REPLACE: 852f50f2127SFlorian Fainelli return dsa_slave_add_cls_matchall(dev, protocol, 853f50f2127SFlorian Fainelli tc->cls_mall, 854f50f2127SFlorian Fainelli ingress); 855f50f2127SFlorian Fainelli case TC_CLSMATCHALL_DESTROY: 856f50f2127SFlorian Fainelli dsa_slave_del_cls_matchall(dev, tc->cls_mall); 857f50f2127SFlorian Fainelli return 0; 858f50f2127SFlorian Fainelli } 859f50f2127SFlorian Fainelli default: 860a5fcf8a6SJiri Pirko return -EOPNOTSUPP; 861f50f2127SFlorian Fainelli } 862f50f2127SFlorian Fainelli } 863f50f2127SFlorian Fainelli 864af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops) 865af42192cSFlorian Fainelli { 866af42192cSFlorian Fainelli ops->get_sset_count = dsa_cpu_port_get_sset_count; 867af42192cSFlorian Fainelli ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats; 868af42192cSFlorian Fainelli ops->get_strings = dsa_cpu_port_get_strings; 869af42192cSFlorian Fainelli } 870af42192cSFlorian Fainelli 871bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev, 872bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc, u32 *rule_locs) 873bf9f2648SFlorian Fainelli { 874bf9f2648SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 875bf9f2648SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 876bf9f2648SFlorian Fainelli 877bf9f2648SFlorian Fainelli if (!ds->ops->get_rxnfc) 878bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 879bf9f2648SFlorian Fainelli 880bf9f2648SFlorian Fainelli return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs); 881bf9f2648SFlorian Fainelli } 882bf9f2648SFlorian Fainelli 883bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev, 884bf9f2648SFlorian Fainelli struct ethtool_rxnfc *nfc) 885bf9f2648SFlorian Fainelli { 886bf9f2648SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 887bf9f2648SFlorian Fainelli struct dsa_switch *ds = p->dp->ds; 888bf9f2648SFlorian Fainelli 889bf9f2648SFlorian Fainelli if (!ds->ops->set_rxnfc) 890bf9f2648SFlorian Fainelli return -EOPNOTSUPP; 891bf9f2648SFlorian Fainelli 892bf9f2648SFlorian Fainelli return ds->ops->set_rxnfc(ds, p->dp->index, nfc); 893bf9f2648SFlorian Fainelli } 894bf9f2648SFlorian Fainelli 89591da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 89691da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 8973d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 8983d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 89991da11f8SLennert Buytenhek .nway_reset = dsa_slave_nway_reset, 90091da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 9016793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 9026793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 9036793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 90491da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 90591da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 90691da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 90719e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 90819e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 9097905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 9107905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 911bb10bb3eSPhilippe Reynes .get_link_ksettings = dsa_slave_get_link_ksettings, 912bb10bb3eSPhilippe Reynes .set_link_ksettings = dsa_slave_set_link_ksettings, 913bf9f2648SFlorian Fainelli .get_rxnfc = dsa_slave_get_rxnfc, 914bf9f2648SFlorian Fainelli .set_rxnfc = dsa_slave_set_rxnfc, 91591da11f8SLennert Buytenhek }; 91691da11f8SLennert Buytenhek 9173e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 918d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 919d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 9203e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 921d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 922d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 923d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 924ba14d9ebSVivien Didelot .ndo_fdb_add = switchdev_port_fdb_add, 925ba14d9ebSVivien Didelot .ndo_fdb_del = switchdev_port_fdb_del, 926ba14d9ebSVivien Didelot .ndo_fdb_dump = switchdev_port_fdb_dump, 927d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 928abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 92904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 93004ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 93104ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 93204ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 93304ff53f9SFlorian Fainelli #endif 93411149536SVivien Didelot .ndo_bridge_getlink = switchdev_port_bridge_getlink, 93511149536SVivien Didelot .ndo_bridge_setlink = switchdev_port_bridge_setlink, 93611149536SVivien Didelot .ndo_bridge_dellink = switchdev_port_bridge_dellink, 93744bb765cSFlorian Fainelli .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, 938f50f2127SFlorian Fainelli .ndo_setup_tc = dsa_slave_setup_tc, 93998237d43SScott Feldman }; 94098237d43SScott Feldman 9419d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = { 942f8e20a9fSScott Feldman .switchdev_port_attr_get = dsa_slave_port_attr_get, 94335636062SScott Feldman .switchdev_port_attr_set = dsa_slave_port_attr_set, 944ba14d9ebSVivien Didelot .switchdev_port_obj_add = dsa_slave_port_obj_add, 945ba14d9ebSVivien Didelot .switchdev_port_obj_del = dsa_slave_port_obj_del, 946ba14d9ebSVivien Didelot .switchdev_port_obj_dump = dsa_slave_port_obj_dump, 947d442ad4aSStephen Hemminger }; 94891da11f8SLennert Buytenhek 949f37db85dSFlorian Fainelli static struct device_type dsa_type = { 950f37db85dSFlorian Fainelli .name = "dsa", 951f37db85dSFlorian Fainelli }; 952f37db85dSFlorian Fainelli 9530d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 9540d8bcdd3SFlorian Fainelli { 9550d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 956afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 9570d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 9580d8bcdd3SFlorian Fainelli 9590d8bcdd3SFlorian Fainelli if (p->old_link != p->phy->link) { 9600d8bcdd3SFlorian Fainelli status_changed = 1; 9610d8bcdd3SFlorian Fainelli p->old_link = p->phy->link; 9620d8bcdd3SFlorian Fainelli } 9630d8bcdd3SFlorian Fainelli 9640d8bcdd3SFlorian Fainelli if (p->old_duplex != p->phy->duplex) { 9650d8bcdd3SFlorian Fainelli status_changed = 1; 9660d8bcdd3SFlorian Fainelli p->old_duplex = p->phy->duplex; 9670d8bcdd3SFlorian Fainelli } 9680d8bcdd3SFlorian Fainelli 9690d8bcdd3SFlorian Fainelli if (p->old_pause != p->phy->pause) { 9700d8bcdd3SFlorian Fainelli status_changed = 1; 9710d8bcdd3SFlorian Fainelli p->old_pause = p->phy->pause; 9720d8bcdd3SFlorian Fainelli } 9730d8bcdd3SFlorian Fainelli 9749d490b4eSVivien Didelot if (ds->ops->adjust_link && status_changed) 975afdcf151SVivien Didelot ds->ops->adjust_link(ds, p->dp->index, p->phy); 976ec9436baSFlorian Fainelli 9770d8bcdd3SFlorian Fainelli if (status_changed) 9780d8bcdd3SFlorian Fainelli phy_print_status(p->phy); 9790d8bcdd3SFlorian Fainelli } 9800d8bcdd3SFlorian Fainelli 981ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 982ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 983ce31b31cSFlorian Fainelli { 984b71be352SAndrew Lunn struct dsa_slave_priv *p; 985b71be352SAndrew Lunn struct dsa_switch *ds; 986ce31b31cSFlorian Fainelli 987b71be352SAndrew Lunn if (dev) { 988b71be352SAndrew Lunn p = netdev_priv(dev); 989afdcf151SVivien Didelot ds = p->dp->ds; 9909d490b4eSVivien Didelot if (ds->ops->fixed_link_update) 991afdcf151SVivien Didelot ds->ops->fixed_link_update(ds, p->dp->index, status); 992b71be352SAndrew Lunn } 993ce31b31cSFlorian Fainelli 994ce31b31cSFlorian Fainelli return 0; 995ce31b31cSFlorian Fainelli } 996ce31b31cSFlorian Fainelli 99791da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 998c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p, 999cd28a1a9SFlorian Fainelli struct net_device *slave_dev, 1000cd28a1a9SFlorian Fainelli int addr) 1001c305c165SFlorian Fainelli { 1002afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 1003c305c165SFlorian Fainelli 10047f854420SAndrew Lunn p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr); 1005d25b8e74SRussell King if (!p->phy) { 1006d25b8e74SRussell King netdev_err(slave_dev, "no phy at %d\n", addr); 1007c305c165SFlorian Fainelli return -ENODEV; 1008d25b8e74SRussell King } 1009c305c165SFlorian Fainelli 1010c305c165SFlorian Fainelli /* Use already configured phy mode */ 1011211c504aSFlorian Fainelli if (p->phy_interface == PHY_INTERFACE_MODE_NA) 1012c305c165SFlorian Fainelli p->phy_interface = p->phy->interface; 10134078b76cSFlorian Fainelli return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 1014c305c165SFlorian Fainelli p->phy_interface); 1015c305c165SFlorian Fainelli } 1016c305c165SFlorian Fainelli 10179697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 10180d8bcdd3SFlorian Fainelli struct net_device *slave_dev) 10190d8bcdd3SFlorian Fainelli { 1020afdcf151SVivien Didelot struct dsa_switch *ds = p->dp->ds; 10210d8bcdd3SFlorian Fainelli struct device_node *phy_dn, *port_dn; 1022ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 10236819563eSFlorian Fainelli u32 phy_flags = 0; 102419334920SGuenter Roeck int mode, ret; 10250d8bcdd3SFlorian Fainelli 1026afdcf151SVivien Didelot port_dn = p->dp->dn; 102719334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 102819334920SGuenter Roeck if (mode < 0) 102919334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 103019334920SGuenter Roeck p->phy_interface = mode; 10310d8bcdd3SFlorian Fainelli 10320d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 10330d8f3c67SJohan Hovold if (!phy_dn && of_phy_is_fixed_link(port_dn)) { 10340d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 10350d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 10360d8bcdd3SFlorian Fainelli */ 10370d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 10380d8bcdd3SFlorian Fainelli if (ret) { 1039d25b8e74SRussell King netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); 10409697f1cdSFlorian Fainelli return ret; 10410d8bcdd3SFlorian Fainelli } 1042ce31b31cSFlorian Fainelli phy_is_fixed = true; 10430d8f3c67SJohan Hovold phy_dn = of_node_get(port_dn); 10440d8bcdd3SFlorian Fainelli } 10450d8bcdd3SFlorian Fainelli 10469d490b4eSVivien Didelot if (ds->ops->get_phy_flags) 1047afdcf151SVivien Didelot phy_flags = ds->ops->get_phy_flags(ds, p->dp->index); 10486819563eSFlorian Fainelli 1049cd28a1a9SFlorian Fainelli if (phy_dn) { 1050d25b8e74SRussell King int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn); 1051d25b8e74SRussell King 1052cd28a1a9SFlorian Fainelli /* If this PHY address is part of phys_mii_mask, which means 1053cd28a1a9SFlorian Fainelli * that we need to divert reads and writes to/from it, then we 1054cd28a1a9SFlorian Fainelli * want to bind this device using the slave MII bus created by 1055cd28a1a9SFlorian Fainelli * DSA to make that happen. 1056cd28a1a9SFlorian Fainelli */ 1057d25b8e74SRussell King if (!phy_is_fixed && phy_id >= 0 && 1058d25b8e74SRussell King (ds->phys_mii_mask & (1 << phy_id))) { 1059d25b8e74SRussell King ret = dsa_slave_phy_connect(p, slave_dev, phy_id); 1060d25b8e74SRussell King if (ret) { 1061d25b8e74SRussell King netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret); 10620d8f3c67SJohan Hovold of_node_put(phy_dn); 1063cd28a1a9SFlorian Fainelli return ret; 1064d25b8e74SRussell King } 1065cd28a1a9SFlorian Fainelli } else { 10660d8bcdd3SFlorian Fainelli p->phy = of_phy_connect(slave_dev, phy_dn, 1067cd28a1a9SFlorian Fainelli dsa_slave_adjust_link, 1068cd28a1a9SFlorian Fainelli phy_flags, 10690d8bcdd3SFlorian Fainelli p->phy_interface); 1070cd28a1a9SFlorian Fainelli } 10710d8f3c67SJohan Hovold 10720d8f3c67SJohan Hovold of_node_put(phy_dn); 1073cd28a1a9SFlorian Fainelli } 10740d8bcdd3SFlorian Fainelli 1075ce31b31cSFlorian Fainelli if (p->phy && phy_is_fixed) 1076ce31b31cSFlorian Fainelli fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 1077ce31b31cSFlorian Fainelli 10780d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 10790d8bcdd3SFlorian Fainelli * MDIO bus instead 10800d8bcdd3SFlorian Fainelli */ 1081b31f65fbSAndrew Lunn if (!p->phy) { 1082afdcf151SVivien Didelot ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index); 1083d25b8e74SRussell King if (ret) { 1084afdcf151SVivien Didelot netdev_err(slave_dev, "failed to connect to port %d: %d\n", 1085afdcf151SVivien Didelot p->dp->index, ret); 1086881eadabSJohan Hovold if (phy_is_fixed) 1087881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1088c305c165SFlorian Fainelli return ret; 1089d25b8e74SRussell King } 10900d8bcdd3SFlorian Fainelli } 10919697f1cdSFlorian Fainelli 10922220943aSAndrew Lunn phy_attached_info(p->phy); 10932220943aSAndrew Lunn 10949697f1cdSFlorian Fainelli return 0; 1095b31f65fbSAndrew Lunn } 10960d8bcdd3SFlorian Fainelli 1097448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 1098448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 1099448b4482SAndrew Lunn struct netdev_queue *txq, 1100448b4482SAndrew Lunn void *_unused) 1101448b4482SAndrew Lunn { 1102448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 1103448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 1104448b4482SAndrew Lunn } 1105448b4482SAndrew Lunn 110624462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 110724462549SFlorian Fainelli { 110824462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 110924462549SFlorian Fainelli 1110f154be24SFlorian Fainelli netif_device_detach(slave_dev); 1111f154be24SFlorian Fainelli 111224462549SFlorian Fainelli if (p->phy) { 111324462549SFlorian Fainelli phy_stop(p->phy); 111424462549SFlorian Fainelli p->old_pause = -1; 111524462549SFlorian Fainelli p->old_link = -1; 111624462549SFlorian Fainelli p->old_duplex = -1; 111724462549SFlorian Fainelli phy_suspend(p->phy); 111824462549SFlorian Fainelli } 111924462549SFlorian Fainelli 112024462549SFlorian Fainelli return 0; 112124462549SFlorian Fainelli } 112224462549SFlorian Fainelli 112324462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 112424462549SFlorian Fainelli { 112524462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 112624462549SFlorian Fainelli 112724462549SFlorian Fainelli netif_device_attach(slave_dev); 112824462549SFlorian Fainelli 112924462549SFlorian Fainelli if (p->phy) { 113024462549SFlorian Fainelli phy_resume(p->phy); 113124462549SFlorian Fainelli phy_start(p->phy); 113224462549SFlorian Fainelli } 113324462549SFlorian Fainelli 113424462549SFlorian Fainelli return 0; 113524462549SFlorian Fainelli } 113624462549SFlorian Fainelli 1137d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 113883c0afaeSAndrew Lunn int port, const char *name) 113991da11f8SLennert Buytenhek { 1140badf3adaSFlorian Fainelli struct dsa_switch_tree *dst = ds->dst; 114183c0afaeSAndrew Lunn struct net_device *master; 114291da11f8SLennert Buytenhek struct net_device *slave_dev; 114391da11f8SLennert Buytenhek struct dsa_slave_priv *p; 114491da11f8SLennert Buytenhek int ret; 114591da11f8SLennert Buytenhek 114683c0afaeSAndrew Lunn master = ds->dst->master_netdev; 114783c0afaeSAndrew Lunn if (ds->master_netdev) 114883c0afaeSAndrew Lunn master = ds->master_netdev; 114983c0afaeSAndrew Lunn 1150c835a677STom Gundersen slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 1151c835a677STom Gundersen NET_NAME_UNKNOWN, ether_setup); 115291da11f8SLennert Buytenhek if (slave_dev == NULL) 1153d87d6f44SGuenter Roeck return -ENOMEM; 115491da11f8SLennert Buytenhek 1155f50f2127SFlorian Fainelli slave_dev->features = master->vlan_features | NETIF_F_HW_TC; 1156f50f2127SFlorian Fainelli slave_dev->hw_features |= NETIF_F_HW_TC; 11577ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 11582fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 11590a5f107bSPhil Sutter slave_dev->priv_flags |= IFF_NO_QUEUE; 11603e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 11619d47c0a2SJiri Pirko slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 11628b1efc0fSJarod Wilson slave_dev->min_mtu = 0; 11638b1efc0fSJarod Wilson slave_dev->max_mtu = ETH_MAX_MTU; 1164f37db85dSFlorian Fainelli SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); 1165d442ad4aSStephen Hemminger 1166448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 1167448b4482SAndrew Lunn NULL); 1168448b4482SAndrew Lunn 116991da11f8SLennert Buytenhek SET_NETDEV_DEV(slave_dev, parent); 1170189b0d93SAndrew Lunn slave_dev->dev.of_node = ds->ports[port].dn; 117191da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 117291da11f8SLennert Buytenhek 117391da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 1174afdcf151SVivien Didelot p->dp = &ds->ports[port]; 1175f50f2127SFlorian Fainelli INIT_LIST_HEAD(&p->mall_tc_list); 117639a7f2a4SAndrew Lunn p->xmit = dst->tag_ops->xmit; 11775075314eSAlexander Duyck 11780d8bcdd3SFlorian Fainelli p->old_pause = -1; 11790d8bcdd3SFlorian Fainelli p->old_link = -1; 11800d8bcdd3SFlorian Fainelli p->old_duplex = -1; 11810d8bcdd3SFlorian Fainelli 1182c8b09808SAndrew Lunn ds->ports[port].netdev = slave_dev; 118391da11f8SLennert Buytenhek ret = register_netdev(slave_dev); 118491da11f8SLennert Buytenhek if (ret) { 1185a2ae6007SJoe Perches netdev_err(master, "error %d registering interface %s\n", 1186a2ae6007SJoe Perches ret, slave_dev->name); 1187c8b09808SAndrew Lunn ds->ports[port].netdev = NULL; 118891da11f8SLennert Buytenhek free_netdev(slave_dev); 1189d87d6f44SGuenter Roeck return ret; 119091da11f8SLennert Buytenhek } 119191da11f8SLennert Buytenhek 119291da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 119391da11f8SLennert Buytenhek 11940071f56eSAndrew Lunn ret = dsa_slave_phy_setup(p, slave_dev); 11950071f56eSAndrew Lunn if (ret) { 11960071f56eSAndrew Lunn netdev_err(master, "error %d setting up slave phy\n", ret); 119773dcb556SFlorian Fainelli unregister_netdev(slave_dev); 11980071f56eSAndrew Lunn free_netdev(slave_dev); 11990071f56eSAndrew Lunn return ret; 12000071f56eSAndrew Lunn } 12010071f56eSAndrew Lunn 1202d87d6f44SGuenter Roeck return 0; 120391da11f8SLennert Buytenhek } 1204b73adef6SFlorian Fainelli 1205cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev) 1206cda5c15bSNeil Armstrong { 1207cda5c15bSNeil Armstrong struct dsa_slave_priv *p = netdev_priv(slave_dev); 1208881eadabSJohan Hovold struct device_node *port_dn; 1209881eadabSJohan Hovold 1210afdcf151SVivien Didelot port_dn = p->dp->dn; 1211cda5c15bSNeil Armstrong 1212cda5c15bSNeil Armstrong netif_carrier_off(slave_dev); 1213881eadabSJohan Hovold if (p->phy) { 1214cda5c15bSNeil Armstrong phy_disconnect(p->phy); 1215881eadabSJohan Hovold 1216881eadabSJohan Hovold if (of_phy_is_fixed_link(port_dn)) 1217881eadabSJohan Hovold of_phy_deregister_fixed_link(port_dn); 1218881eadabSJohan Hovold } 1219cda5c15bSNeil Armstrong unregister_netdev(slave_dev); 1220cda5c15bSNeil Armstrong free_netdev(slave_dev); 1221cda5c15bSNeil Armstrong } 1222cda5c15bSNeil Armstrong 1223b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev) 1224b73adef6SFlorian Fainelli { 1225b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1226b73adef6SFlorian Fainelli } 1227b73adef6SFlorian Fainelli 12288e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev, 12298e92ab3aSVivien Didelot struct netdev_notifier_changeupper_info *info) 1230b73adef6SFlorian Fainelli { 123117d7802bSVivien Didelot struct dsa_slave_priv *p = netdev_priv(dev); 123217d7802bSVivien Didelot struct dsa_port *dp = p->dp; 12338e92ab3aSVivien Didelot int err = NOTIFY_DONE; 1234b73adef6SFlorian Fainelli 12358e92ab3aSVivien Didelot if (netif_is_bridge_master(info->upper_dev)) { 12368e92ab3aSVivien Didelot if (info->linking) { 123717d7802bSVivien Didelot err = dsa_port_bridge_join(dp, info->upper_dev); 12388e92ab3aSVivien Didelot err = notifier_from_errno(err); 12398e92ab3aSVivien Didelot } else { 124017d7802bSVivien Didelot dsa_port_bridge_leave(dp, info->upper_dev); 12418e92ab3aSVivien Didelot err = NOTIFY_OK; 12428e92ab3aSVivien Didelot } 12436debb68aSVivien Didelot } 1244b73adef6SFlorian Fainelli 12458e92ab3aSVivien Didelot return err; 1246b73adef6SFlorian Fainelli } 1247b73adef6SFlorian Fainelli 124888e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb, 1249b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1250b73adef6SFlorian Fainelli { 12516debb68aSVivien Didelot struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1252b73adef6SFlorian Fainelli 12538e92ab3aSVivien Didelot if (dev->netdev_ops != &dsa_slave_netdev_ops) 12548e92ab3aSVivien Didelot return NOTIFY_DONE; 12558e92ab3aSVivien Didelot 12568e92ab3aSVivien Didelot if (event == NETDEV_CHANGEUPPER) 12578e92ab3aSVivien Didelot return dsa_slave_changeupper(dev, ptr); 1258b73adef6SFlorian Fainelli 1259b73adef6SFlorian Fainelli return NOTIFY_DONE; 1260b73adef6SFlorian Fainelli } 126188e4f0caSVivien Didelot 126288e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = { 126388e4f0caSVivien Didelot .notifier_call = dsa_slave_netdevice_event, 126488e4f0caSVivien Didelot }; 126588e4f0caSVivien Didelot 126688e4f0caSVivien Didelot int dsa_slave_register_notifier(void) 126788e4f0caSVivien Didelot { 126888e4f0caSVivien Didelot return register_netdevice_notifier(&dsa_slave_nb); 126988e4f0caSVivien Didelot } 127088e4f0caSVivien Didelot 127188e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void) 127288e4f0caSVivien Didelot { 127388e4f0caSVivien Didelot int err; 127488e4f0caSVivien Didelot 127588e4f0caSVivien Didelot err = unregister_netdevice_notifier(&dsa_slave_nb); 127688e4f0caSVivien Didelot if (err) 127788e4f0caSVivien Didelot pr_err("DSA: failed to unregister slave notifier (%d)\n", err); 127888e4f0caSVivien Didelot } 1279