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> 18b73adef6SFlorian Fainelli #include <net/rtnetlink.h> 1998237d43SScott Feldman #include <net/switchdev.h> 20b73adef6SFlorian Fainelli #include <linux/if_bridge.h> 2104ff53f9SFlorian Fainelli #include <linux/netpoll.h> 2291da11f8SLennert Buytenhek #include "dsa_priv.h" 2391da11f8SLennert Buytenhek 2491da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2591da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2691da11f8SLennert Buytenhek { 2791da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 2891da11f8SLennert Buytenhek 290d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 3091da11f8SLennert Buytenhek return ds->drv->phy_read(ds, addr, reg); 3191da11f8SLennert Buytenhek 3291da11f8SLennert Buytenhek return 0xffff; 3391da11f8SLennert Buytenhek } 3491da11f8SLennert Buytenhek 3591da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3691da11f8SLennert Buytenhek { 3791da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3891da11f8SLennert Buytenhek 390d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 4091da11f8SLennert Buytenhek return ds->drv->phy_write(ds, addr, reg, val); 4191da11f8SLennert Buytenhek 4291da11f8SLennert Buytenhek return 0; 4391da11f8SLennert Buytenhek } 4491da11f8SLennert Buytenhek 4591da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4691da11f8SLennert Buytenhek { 4791da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 4891da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 4991da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 5091da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 51f490be04SFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", 52f490be04SFlorian Fainelli ds->index, ds->pd->sw_addr); 53b4d2394dSAlexander Duyck ds->slave_mii_bus->parent = ds->master_dev; 5424df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5591da11f8SLennert Buytenhek } 5691da11f8SLennert Buytenhek 5791da11f8SLennert Buytenhek 5891da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 59abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 60c0840801SLennert Buytenhek { 61c0840801SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 62c0840801SLennert Buytenhek 63abd2be00SNicolas Dichtel return p->parent->dst->master_netdev->ifindex; 64c0840801SLennert Buytenhek } 65c0840801SLennert Buytenhek 66b73adef6SFlorian Fainelli static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p) 67b73adef6SFlorian Fainelli { 68b73adef6SFlorian Fainelli return !!p->bridge_dev; 69b73adef6SFlorian Fainelli } 70b73adef6SFlorian Fainelli 7191da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 7291da11f8SLennert Buytenhek { 73df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 74e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 75b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 76b73adef6SFlorian Fainelli u8 stp_state = dsa_port_is_bridged(p) ? 77b73adef6SFlorian Fainelli BR_STATE_BLOCKING : BR_STATE_FORWARDING; 78df02c6ffSLennert Buytenhek int err; 79df02c6ffSLennert Buytenhek 80df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 81df02c6ffSLennert Buytenhek return -ENETDOWN; 82df02c6ffSLennert Buytenhek 838feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 84a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 85df02c6ffSLennert Buytenhek if (err < 0) 86df02c6ffSLennert Buytenhek goto out; 87df02c6ffSLennert Buytenhek } 88df02c6ffSLennert Buytenhek 89df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 90df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 91df02c6ffSLennert Buytenhek if (err < 0) 92df02c6ffSLennert Buytenhek goto del_unicast; 93df02c6ffSLennert Buytenhek } 94df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 95df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 96df02c6ffSLennert Buytenhek if (err < 0) 97df02c6ffSLennert Buytenhek goto clear_allmulti; 98df02c6ffSLennert Buytenhek } 99df02c6ffSLennert Buytenhek 100b2f2af21SFlorian Fainelli if (ds->drv->port_enable) { 101b2f2af21SFlorian Fainelli err = ds->drv->port_enable(ds, p->port, p->phy); 102b2f2af21SFlorian Fainelli if (err) 103b2f2af21SFlorian Fainelli goto clear_promisc; 104b2f2af21SFlorian Fainelli } 105b2f2af21SFlorian Fainelli 106b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 107b73adef6SFlorian Fainelli ds->drv->port_stp_update(ds, p->port, 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); 130e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 131b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 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 146b2f2af21SFlorian Fainelli if (ds->drv->port_disable) 147b2f2af21SFlorian Fainelli ds->drv->port_disable(ds, p->port, p->phy); 148b2f2af21SFlorian Fainelli 149b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 150b73adef6SFlorian Fainelli ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED); 151b73adef6SFlorian Fainelli 15291da11f8SLennert Buytenhek return 0; 15391da11f8SLennert Buytenhek } 15491da11f8SLennert Buytenhek 15591da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 15691da11f8SLennert Buytenhek { 15791da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 158e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 15991da11f8SLennert Buytenhek 16091da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 16191da11f8SLennert Buytenhek dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 16291da11f8SLennert Buytenhek if (change & IFF_PROMISC) 16391da11f8SLennert Buytenhek dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 16491da11f8SLennert Buytenhek } 16591da11f8SLennert Buytenhek 16691da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 16791da11f8SLennert Buytenhek { 16891da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 169e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 17091da11f8SLennert Buytenhek 17191da11f8SLennert Buytenhek dev_mc_sync(master, dev); 172a748ee24SJiri Pirko dev_uc_sync(master, dev); 17391da11f8SLennert Buytenhek } 17491da11f8SLennert Buytenhek 175df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 17691da11f8SLennert Buytenhek { 177df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 178e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 179df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 180df02c6ffSLennert Buytenhek int err; 181df02c6ffSLennert Buytenhek 182df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 183df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 184df02c6ffSLennert Buytenhek 185df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 186df02c6ffSLennert Buytenhek goto out; 187df02c6ffSLennert Buytenhek 1888feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 189a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 190df02c6ffSLennert Buytenhek if (err < 0) 191df02c6ffSLennert Buytenhek return err; 192df02c6ffSLennert Buytenhek } 193df02c6ffSLennert Buytenhek 1948feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 195a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 196df02c6ffSLennert Buytenhek 197df02c6ffSLennert Buytenhek out: 198d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 19991da11f8SLennert Buytenhek 20091da11f8SLennert Buytenhek return 0; 20191da11f8SLennert Buytenhek } 20291da11f8SLennert Buytenhek 203cdf09697SDavid S. Miller static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 204cdf09697SDavid S. Miller struct net_device *dev, 205cdf09697SDavid S. Miller const unsigned char *addr, u16 vid, u16 nlm_flags) 206cdf09697SDavid S. Miller { 207cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 208cdf09697SDavid S. Miller struct dsa_switch *ds = p->parent; 209cdf09697SDavid S. Miller int ret = -EOPNOTSUPP; 210cdf09697SDavid S. Miller 211*2a778e1bSVivien Didelot if (ds->drv->port_fdb_add) 212*2a778e1bSVivien Didelot ret = ds->drv->port_fdb_add(ds, p->port, addr, vid); 213cdf09697SDavid S. Miller 214cdf09697SDavid S. Miller return ret; 215cdf09697SDavid S. Miller } 216cdf09697SDavid S. Miller 217cdf09697SDavid S. Miller static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 218cdf09697SDavid S. Miller struct net_device *dev, 219cdf09697SDavid S. Miller const unsigned char *addr, u16 vid) 220cdf09697SDavid S. Miller { 221cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 222cdf09697SDavid S. Miller struct dsa_switch *ds = p->parent; 223cdf09697SDavid S. Miller int ret = -EOPNOTSUPP; 224cdf09697SDavid S. Miller 225*2a778e1bSVivien Didelot if (ds->drv->port_fdb_del) 226*2a778e1bSVivien Didelot ret = ds->drv->port_fdb_del(ds, p->port, addr, vid); 227cdf09697SDavid S. Miller 228cdf09697SDavid S. Miller return ret; 229cdf09697SDavid S. Miller } 230cdf09697SDavid S. Miller 231cdf09697SDavid S. Miller static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb, 232cdf09697SDavid S. Miller const unsigned char *addr, u16 vid, 233cdf09697SDavid S. Miller bool is_static, 234cdf09697SDavid S. Miller u32 portid, u32 seq, int type, 235cdf09697SDavid S. Miller unsigned int flags) 236cdf09697SDavid S. Miller { 237cdf09697SDavid S. Miller struct nlmsghdr *nlh; 238cdf09697SDavid S. Miller struct ndmsg *ndm; 239cdf09697SDavid S. Miller 240cdf09697SDavid S. Miller nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); 241cdf09697SDavid S. Miller if (!nlh) 242cdf09697SDavid S. Miller return -EMSGSIZE; 243cdf09697SDavid S. Miller 244cdf09697SDavid S. Miller ndm = nlmsg_data(nlh); 245cdf09697SDavid S. Miller ndm->ndm_family = AF_BRIDGE; 246cdf09697SDavid S. Miller ndm->ndm_pad1 = 0; 247cdf09697SDavid S. Miller ndm->ndm_pad2 = 0; 248cdf09697SDavid S. Miller ndm->ndm_flags = NTF_EXT_LEARNED; 249cdf09697SDavid S. Miller ndm->ndm_type = 0; 250cdf09697SDavid S. Miller ndm->ndm_ifindex = dev->ifindex; 251cdf09697SDavid S. Miller ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 252cdf09697SDavid S. Miller 253cdf09697SDavid S. Miller if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 254cdf09697SDavid S. Miller goto nla_put_failure; 255cdf09697SDavid S. Miller 256cdf09697SDavid S. Miller if (vid && nla_put_u16(skb, NDA_VLAN, vid)) 257cdf09697SDavid S. Miller goto nla_put_failure; 258cdf09697SDavid S. Miller 259cdf09697SDavid S. Miller nlmsg_end(skb, nlh); 260cdf09697SDavid S. Miller return 0; 261cdf09697SDavid S. Miller 262cdf09697SDavid S. Miller nla_put_failure: 263cdf09697SDavid S. Miller nlmsg_cancel(skb, nlh); 264cdf09697SDavid S. Miller return -EMSGSIZE; 265cdf09697SDavid S. Miller } 266cdf09697SDavid S. Miller 267cdf09697SDavid S. Miller /* Dump information about entries, in response to GETNEIGH */ 268cdf09697SDavid S. Miller static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 269cdf09697SDavid S. Miller struct net_device *dev, 270cdf09697SDavid S. Miller struct net_device *filter_dev, int idx) 271cdf09697SDavid S. Miller { 272cdf09697SDavid S. Miller struct dsa_slave_priv *p = netdev_priv(dev); 273cdf09697SDavid S. Miller struct dsa_switch *ds = p->parent; 274cdf09697SDavid S. Miller unsigned char addr[ETH_ALEN] = { 0 }; 275*2a778e1bSVivien Didelot u16 vid = 0; 276cdf09697SDavid S. Miller int ret; 277cdf09697SDavid S. Miller 278*2a778e1bSVivien Didelot if (!ds->drv->port_fdb_getnext) 279cdf09697SDavid S. Miller return -EOPNOTSUPP; 280cdf09697SDavid S. Miller 281cdf09697SDavid S. Miller for (; ; idx++) { 282cdf09697SDavid S. Miller bool is_static; 283cdf09697SDavid S. Miller 284*2a778e1bSVivien Didelot ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid, 285*2a778e1bSVivien Didelot &is_static); 286cdf09697SDavid S. Miller if (ret < 0) 287cdf09697SDavid S. Miller break; 288cdf09697SDavid S. Miller 289cdf09697SDavid S. Miller if (idx < cb->args[0]) 290cdf09697SDavid S. Miller continue; 291cdf09697SDavid S. Miller 292*2a778e1bSVivien Didelot ret = dsa_slave_fill_info(dev, skb, addr, vid, 293cdf09697SDavid S. Miller is_static, 294cdf09697SDavid S. Miller NETLINK_CB(cb->skb).portid, 295cdf09697SDavid S. Miller cb->nlh->nlmsg_seq, 296cdf09697SDavid S. Miller RTM_NEWNEIGH, NLM_F_MULTI); 297cdf09697SDavid S. Miller if (ret < 0) 298cdf09697SDavid S. Miller break; 299cdf09697SDavid S. Miller } 300cdf09697SDavid S. Miller 301cdf09697SDavid S. Miller return idx; 302cdf09697SDavid S. Miller } 303cdf09697SDavid S. Miller 30491da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 30591da11f8SLennert Buytenhek { 30691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 30791da11f8SLennert Buytenhek 30891da11f8SLennert Buytenhek if (p->phy != NULL) 30928b04113SRichard Cochran return phy_mii_ioctl(p->phy, ifr, cmd); 31091da11f8SLennert Buytenhek 31191da11f8SLennert Buytenhek return -EOPNOTSUPP; 31291da11f8SLennert Buytenhek } 31391da11f8SLennert Buytenhek 314b73adef6SFlorian Fainelli /* Return a bitmask of all ports being currently bridged within a given bridge 315b73adef6SFlorian Fainelli * device. Note that on leave, the mask will still return the bitmask of ports 316b73adef6SFlorian Fainelli * currently bridged, prior to port removal, and this is exactly what we want. 317b73adef6SFlorian Fainelli */ 318b73adef6SFlorian Fainelli static u32 dsa_slave_br_port_mask(struct dsa_switch *ds, 319b73adef6SFlorian Fainelli struct net_device *bridge) 320b73adef6SFlorian Fainelli { 321b73adef6SFlorian Fainelli struct dsa_slave_priv *p; 322b73adef6SFlorian Fainelli unsigned int port; 323b73adef6SFlorian Fainelli u32 mask = 0; 324b73adef6SFlorian Fainelli 325b73adef6SFlorian Fainelli for (port = 0; port < DSA_MAX_PORTS; port++) { 326d79d2107SGuenter Roeck if (!dsa_is_port_initialized(ds, port)) 327b73adef6SFlorian Fainelli continue; 328b73adef6SFlorian Fainelli 329b73adef6SFlorian Fainelli p = netdev_priv(ds->ports[port]); 330b73adef6SFlorian Fainelli 331b73adef6SFlorian Fainelli if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT && 332b73adef6SFlorian Fainelli p->bridge_dev == bridge) 333b73adef6SFlorian Fainelli mask |= 1 << port; 334b73adef6SFlorian Fainelli } 335b73adef6SFlorian Fainelli 336b73adef6SFlorian Fainelli return mask; 337b73adef6SFlorian Fainelli } 338b73adef6SFlorian Fainelli 339b73adef6SFlorian Fainelli static int dsa_slave_stp_update(struct net_device *dev, u8 state) 340b73adef6SFlorian Fainelli { 341b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 342b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 343b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 344b73adef6SFlorian Fainelli 345b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 346b73adef6SFlorian Fainelli ret = ds->drv->port_stp_update(ds, p->port, state); 347b73adef6SFlorian Fainelli 348b73adef6SFlorian Fainelli return ret; 349b73adef6SFlorian Fainelli } 350b73adef6SFlorian Fainelli 35135636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 35235636062SScott Feldman struct switchdev_attr *attr) 35335636062SScott Feldman { 35435636062SScott Feldman int ret = 0; 35535636062SScott Feldman 35635636062SScott Feldman switch (attr->id) { 35735636062SScott Feldman case SWITCHDEV_ATTR_PORT_STP_STATE: 35835636062SScott Feldman if (attr->trans == SWITCHDEV_TRANS_COMMIT) 35942275bd8SScott Feldman ret = dsa_slave_stp_update(dev, attr->u.stp_state); 36035636062SScott Feldman break; 36135636062SScott Feldman default: 36235636062SScott Feldman ret = -EOPNOTSUPP; 36335636062SScott Feldman break; 36435636062SScott Feldman } 36535636062SScott Feldman 36635636062SScott Feldman return ret; 36735636062SScott Feldman } 36835636062SScott Feldman 369b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev, 370b73adef6SFlorian Fainelli struct net_device *br) 371b73adef6SFlorian Fainelli { 372b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 373b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 374b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 375b73adef6SFlorian Fainelli 376b73adef6SFlorian Fainelli p->bridge_dev = br; 377b73adef6SFlorian Fainelli 378b73adef6SFlorian Fainelli if (ds->drv->port_join_bridge) 379b73adef6SFlorian Fainelli ret = ds->drv->port_join_bridge(ds, p->port, 380b73adef6SFlorian Fainelli dsa_slave_br_port_mask(ds, br)); 381b73adef6SFlorian Fainelli 382b73adef6SFlorian Fainelli return ret; 383b73adef6SFlorian Fainelli } 384b73adef6SFlorian Fainelli 385b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_leave(struct net_device *dev) 386b73adef6SFlorian Fainelli { 387b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 388b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 389b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 390b73adef6SFlorian Fainelli 391b73adef6SFlorian Fainelli 392b73adef6SFlorian Fainelli if (ds->drv->port_leave_bridge) 393b73adef6SFlorian Fainelli ret = ds->drv->port_leave_bridge(ds, p->port, 394b73adef6SFlorian Fainelli dsa_slave_br_port_mask(ds, p->bridge_dev)); 395b73adef6SFlorian Fainelli 396b73adef6SFlorian Fainelli p->bridge_dev = NULL; 397b73adef6SFlorian Fainelli 398b73adef6SFlorian Fainelli /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, 399b73adef6SFlorian Fainelli * so allow it to be in BR_STATE_FORWARDING to be kept functional 400b73adef6SFlorian Fainelli */ 401b73adef6SFlorian Fainelli dsa_slave_stp_update(dev, BR_STATE_FORWARDING); 402b73adef6SFlorian Fainelli 403b73adef6SFlorian Fainelli return ret; 404b73adef6SFlorian Fainelli } 405b73adef6SFlorian Fainelli 406f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev, 407f8e20a9fSScott Feldman struct switchdev_attr *attr) 408b73adef6SFlorian Fainelli { 409b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 410b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 411b73adef6SFlorian Fainelli 412f8e20a9fSScott Feldman switch (attr->id) { 413f8e20a9fSScott Feldman case SWITCHDEV_ATTR_PORT_PARENT_ID: 41442275bd8SScott Feldman attr->u.ppid.id_len = sizeof(ds->index); 41542275bd8SScott Feldman memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); 416f8e20a9fSScott Feldman break; 417f8e20a9fSScott Feldman default: 418f8e20a9fSScott Feldman return -EOPNOTSUPP; 419f8e20a9fSScott Feldman } 420b73adef6SFlorian Fainelli 421b73adef6SFlorian Fainelli return 0; 422b73adef6SFlorian Fainelli } 423b73adef6SFlorian Fainelli 42404ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, 42504ff53f9SFlorian Fainelli struct sk_buff *skb) 42604ff53f9SFlorian Fainelli { 42704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 42804ff53f9SFlorian Fainelli if (p->netpoll) 42904ff53f9SFlorian Fainelli netpoll_send_skb(p->netpoll, skb); 43004ff53f9SFlorian Fainelli #else 43104ff53f9SFlorian Fainelli BUG(); 43204ff53f9SFlorian Fainelli #endif 43304ff53f9SFlorian Fainelli return NETDEV_TX_OK; 43404ff53f9SFlorian Fainelli } 43504ff53f9SFlorian Fainelli 4363e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 4373e8a72d1SFlorian Fainelli { 4383e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 4394ed70ce9SFlorian Fainelli struct sk_buff *nskb; 4403e8a72d1SFlorian Fainelli 4414ed70ce9SFlorian Fainelli dev->stats.tx_packets++; 4424ed70ce9SFlorian Fainelli dev->stats.tx_bytes += skb->len; 4433e8a72d1SFlorian Fainelli 4444ed70ce9SFlorian Fainelli /* Transmit function may have to reallocate the original SKB */ 4454ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 4464ed70ce9SFlorian Fainelli if (!nskb) 4474ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 4485aed85ceSFlorian Fainelli 44904ff53f9SFlorian Fainelli /* SKB for netpoll still need to be mangled with the protocol-specific 45004ff53f9SFlorian Fainelli * tag to be successfully transmitted 45104ff53f9SFlorian Fainelli */ 45204ff53f9SFlorian Fainelli if (unlikely(netpoll_tx_running(dev))) 45304ff53f9SFlorian Fainelli return dsa_netpoll_send_skb(p, nskb); 45404ff53f9SFlorian Fainelli 4554ed70ce9SFlorian Fainelli /* Queue the SKB for transmission on the parent interface, but 4564ed70ce9SFlorian Fainelli * do not modify its EtherType 4574ed70ce9SFlorian Fainelli */ 4584ed70ce9SFlorian Fainelli nskb->dev = p->parent->dst->master_netdev; 4594ed70ce9SFlorian Fainelli dev_queue_xmit(nskb); 4605aed85ceSFlorian Fainelli 4615aed85ceSFlorian Fainelli return NETDEV_TX_OK; 4625aed85ceSFlorian Fainelli } 4635aed85ceSFlorian Fainelli 4644ed70ce9SFlorian Fainelli static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, 4654ed70ce9SFlorian Fainelli struct net_device *dev) 4664ed70ce9SFlorian Fainelli { 4674ed70ce9SFlorian Fainelli /* Just return the original SKB */ 4684ed70ce9SFlorian Fainelli return skb; 4694ed70ce9SFlorian Fainelli } 4704ed70ce9SFlorian Fainelli 47191da11f8SLennert Buytenhek 47291da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 47391da11f8SLennert Buytenhek static int 47491da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 47591da11f8SLennert Buytenhek { 47691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 47791da11f8SLennert Buytenhek int err; 47891da11f8SLennert Buytenhek 47991da11f8SLennert Buytenhek err = -EOPNOTSUPP; 48091da11f8SLennert Buytenhek if (p->phy != NULL) { 48191da11f8SLennert Buytenhek err = phy_read_status(p->phy); 48291da11f8SLennert Buytenhek if (err == 0) 48391da11f8SLennert Buytenhek err = phy_ethtool_gset(p->phy, cmd); 48491da11f8SLennert Buytenhek } 48591da11f8SLennert Buytenhek 48691da11f8SLennert Buytenhek return err; 48791da11f8SLennert Buytenhek } 48891da11f8SLennert Buytenhek 48991da11f8SLennert Buytenhek static int 49091da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 49191da11f8SLennert Buytenhek { 49291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 49391da11f8SLennert Buytenhek 49491da11f8SLennert Buytenhek if (p->phy != NULL) 49591da11f8SLennert Buytenhek return phy_ethtool_sset(p->phy, cmd); 49691da11f8SLennert Buytenhek 49791da11f8SLennert Buytenhek return -EOPNOTSUPP; 49891da11f8SLennert Buytenhek } 49991da11f8SLennert Buytenhek 50091da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 50191da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 50291da11f8SLennert Buytenhek { 5037826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 5047826d43fSJiri Pirko strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); 5057826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 5067826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 50791da11f8SLennert Buytenhek } 50891da11f8SLennert Buytenhek 5093d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 5103d762a0fSGuenter Roeck { 5113d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5123d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 5133d762a0fSGuenter Roeck 5143d762a0fSGuenter Roeck if (ds->drv->get_regs_len) 5153d762a0fSGuenter Roeck return ds->drv->get_regs_len(ds, p->port); 5163d762a0fSGuenter Roeck 5173d762a0fSGuenter Roeck return -EOPNOTSUPP; 5183d762a0fSGuenter Roeck } 5193d762a0fSGuenter Roeck 5203d762a0fSGuenter Roeck static void 5213d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 5223d762a0fSGuenter Roeck { 5233d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5243d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 5253d762a0fSGuenter Roeck 5263d762a0fSGuenter Roeck if (ds->drv->get_regs) 5273d762a0fSGuenter Roeck ds->drv->get_regs(ds, p->port, regs, _p); 5283d762a0fSGuenter Roeck } 5293d762a0fSGuenter Roeck 53091da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev) 53191da11f8SLennert Buytenhek { 53291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 53391da11f8SLennert Buytenhek 53491da11f8SLennert Buytenhek if (p->phy != NULL) 53591da11f8SLennert Buytenhek return genphy_restart_aneg(p->phy); 53691da11f8SLennert Buytenhek 53791da11f8SLennert Buytenhek return -EOPNOTSUPP; 53891da11f8SLennert Buytenhek } 53991da11f8SLennert Buytenhek 54091da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 54191da11f8SLennert Buytenhek { 54291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 54391da11f8SLennert Buytenhek 54491da11f8SLennert Buytenhek if (p->phy != NULL) { 54591da11f8SLennert Buytenhek genphy_update_link(p->phy); 54691da11f8SLennert Buytenhek return p->phy->link; 54791da11f8SLennert Buytenhek } 54891da11f8SLennert Buytenhek 54991da11f8SLennert Buytenhek return -EOPNOTSUPP; 55091da11f8SLennert Buytenhek } 55191da11f8SLennert Buytenhek 5526793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 5536793abb4SGuenter Roeck { 5546793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5556793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5566793abb4SGuenter Roeck 5576793abb4SGuenter Roeck if (ds->pd->eeprom_len) 5586793abb4SGuenter Roeck return ds->pd->eeprom_len; 5596793abb4SGuenter Roeck 5606793abb4SGuenter Roeck if (ds->drv->get_eeprom_len) 5616793abb4SGuenter Roeck return ds->drv->get_eeprom_len(ds); 5626793abb4SGuenter Roeck 5636793abb4SGuenter Roeck return 0; 5646793abb4SGuenter Roeck } 5656793abb4SGuenter Roeck 5666793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 5676793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5686793abb4SGuenter Roeck { 5696793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5706793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5716793abb4SGuenter Roeck 5726793abb4SGuenter Roeck if (ds->drv->get_eeprom) 5736793abb4SGuenter Roeck return ds->drv->get_eeprom(ds, eeprom, data); 5746793abb4SGuenter Roeck 5756793abb4SGuenter Roeck return -EOPNOTSUPP; 5766793abb4SGuenter Roeck } 5776793abb4SGuenter Roeck 5786793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 5796793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5806793abb4SGuenter Roeck { 5816793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5826793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5836793abb4SGuenter Roeck 5846793abb4SGuenter Roeck if (ds->drv->set_eeprom) 5856793abb4SGuenter Roeck return ds->drv->set_eeprom(ds, eeprom, data); 5866793abb4SGuenter Roeck 5876793abb4SGuenter Roeck return -EOPNOTSUPP; 5886793abb4SGuenter Roeck } 5896793abb4SGuenter Roeck 59091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 59191da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 59291da11f8SLennert Buytenhek { 59391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 59491da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 59591da11f8SLennert Buytenhek 59691da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 59791da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 59891da11f8SLennert Buytenhek 59991da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 60091da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 60191da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 60291da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 60391da11f8SLennert Buytenhek if (ds->drv->get_strings != NULL) 60491da11f8SLennert Buytenhek ds->drv->get_strings(ds, p->port, data + 4 * len); 60591da11f8SLennert Buytenhek } 60691da11f8SLennert Buytenhek } 60791da11f8SLennert Buytenhek 60891da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 60991da11f8SLennert Buytenhek struct ethtool_stats *stats, 61091da11f8SLennert Buytenhek uint64_t *data) 61191da11f8SLennert Buytenhek { 61291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 61391da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 61491da11f8SLennert Buytenhek 61591da11f8SLennert Buytenhek data[0] = p->dev->stats.tx_packets; 61691da11f8SLennert Buytenhek data[1] = p->dev->stats.tx_bytes; 61791da11f8SLennert Buytenhek data[2] = p->dev->stats.rx_packets; 61891da11f8SLennert Buytenhek data[3] = p->dev->stats.rx_bytes; 61991da11f8SLennert Buytenhek if (ds->drv->get_ethtool_stats != NULL) 62091da11f8SLennert Buytenhek ds->drv->get_ethtool_stats(ds, p->port, data + 4); 62191da11f8SLennert Buytenhek } 62291da11f8SLennert Buytenhek 62391da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 62491da11f8SLennert Buytenhek { 62591da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 62691da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 62791da11f8SLennert Buytenhek 62891da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 62991da11f8SLennert Buytenhek int count; 63091da11f8SLennert Buytenhek 63191da11f8SLennert Buytenhek count = 4; 63291da11f8SLennert Buytenhek if (ds->drv->get_sset_count != NULL) 63391da11f8SLennert Buytenhek count += ds->drv->get_sset_count(ds); 63491da11f8SLennert Buytenhek 63591da11f8SLennert Buytenhek return count; 63691da11f8SLennert Buytenhek } 63791da11f8SLennert Buytenhek 63891da11f8SLennert Buytenhek return -EOPNOTSUPP; 63991da11f8SLennert Buytenhek } 64091da11f8SLennert Buytenhek 64119e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 64219e57c4eSFlorian Fainelli { 64319e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 64419e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 64519e57c4eSFlorian Fainelli 64619e57c4eSFlorian Fainelli if (ds->drv->get_wol) 64719e57c4eSFlorian Fainelli ds->drv->get_wol(ds, p->port, w); 64819e57c4eSFlorian Fainelli } 64919e57c4eSFlorian Fainelli 65019e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 65119e57c4eSFlorian Fainelli { 65219e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 65319e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 65419e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 65519e57c4eSFlorian Fainelli 65619e57c4eSFlorian Fainelli if (ds->drv->set_wol) 65719e57c4eSFlorian Fainelli ret = ds->drv->set_wol(ds, p->port, w); 65819e57c4eSFlorian Fainelli 65919e57c4eSFlorian Fainelli return ret; 66019e57c4eSFlorian Fainelli } 66119e57c4eSFlorian Fainelli 6627905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 6637905288fSFlorian Fainelli { 6647905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 6657905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 6667905288fSFlorian Fainelli int ret; 6677905288fSFlorian Fainelli 6687905288fSFlorian Fainelli if (!ds->drv->set_eee) 6697905288fSFlorian Fainelli return -EOPNOTSUPP; 6707905288fSFlorian Fainelli 6717905288fSFlorian Fainelli ret = ds->drv->set_eee(ds, p->port, p->phy, e); 6727905288fSFlorian Fainelli if (ret) 6737905288fSFlorian Fainelli return ret; 6747905288fSFlorian Fainelli 6757905288fSFlorian Fainelli if (p->phy) 6767905288fSFlorian Fainelli ret = phy_ethtool_set_eee(p->phy, e); 6777905288fSFlorian Fainelli 6787905288fSFlorian Fainelli return ret; 6797905288fSFlorian Fainelli } 6807905288fSFlorian Fainelli 6817905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 6827905288fSFlorian Fainelli { 6837905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 6847905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 6857905288fSFlorian Fainelli int ret; 6867905288fSFlorian Fainelli 6877905288fSFlorian Fainelli if (!ds->drv->get_eee) 6887905288fSFlorian Fainelli return -EOPNOTSUPP; 6897905288fSFlorian Fainelli 6907905288fSFlorian Fainelli ret = ds->drv->get_eee(ds, p->port, e); 6917905288fSFlorian Fainelli if (ret) 6927905288fSFlorian Fainelli return ret; 6937905288fSFlorian Fainelli 6947905288fSFlorian Fainelli if (p->phy) 6957905288fSFlorian Fainelli ret = phy_ethtool_get_eee(p->phy, e); 6967905288fSFlorian Fainelli 6977905288fSFlorian Fainelli return ret; 6987905288fSFlorian Fainelli } 6997905288fSFlorian Fainelli 70004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 70104ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev, 70204ff53f9SFlorian Fainelli struct netpoll_info *ni) 70304ff53f9SFlorian Fainelli { 70404ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 70504ff53f9SFlorian Fainelli struct dsa_switch *ds = p->parent; 70604ff53f9SFlorian Fainelli struct net_device *master = ds->dst->master_netdev; 70704ff53f9SFlorian Fainelli struct netpoll *netpoll; 70804ff53f9SFlorian Fainelli int err = 0; 70904ff53f9SFlorian Fainelli 71004ff53f9SFlorian Fainelli netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 71104ff53f9SFlorian Fainelli if (!netpoll) 71204ff53f9SFlorian Fainelli return -ENOMEM; 71304ff53f9SFlorian Fainelli 71404ff53f9SFlorian Fainelli err = __netpoll_setup(netpoll, master); 71504ff53f9SFlorian Fainelli if (err) { 71604ff53f9SFlorian Fainelli kfree(netpoll); 71704ff53f9SFlorian Fainelli goto out; 71804ff53f9SFlorian Fainelli } 71904ff53f9SFlorian Fainelli 72004ff53f9SFlorian Fainelli p->netpoll = netpoll; 72104ff53f9SFlorian Fainelli out: 72204ff53f9SFlorian Fainelli return err; 72304ff53f9SFlorian Fainelli } 72404ff53f9SFlorian Fainelli 72504ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev) 72604ff53f9SFlorian Fainelli { 72704ff53f9SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 72804ff53f9SFlorian Fainelli struct netpoll *netpoll = p->netpoll; 72904ff53f9SFlorian Fainelli 73004ff53f9SFlorian Fainelli if (!netpoll) 73104ff53f9SFlorian Fainelli return; 73204ff53f9SFlorian Fainelli 73304ff53f9SFlorian Fainelli p->netpoll = NULL; 73404ff53f9SFlorian Fainelli 73504ff53f9SFlorian Fainelli __netpoll_free_async(netpoll); 73604ff53f9SFlorian Fainelli } 73704ff53f9SFlorian Fainelli 73804ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev) 73904ff53f9SFlorian Fainelli { 74004ff53f9SFlorian Fainelli } 74104ff53f9SFlorian Fainelli #endif 74204ff53f9SFlorian Fainelli 74391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 74491da11f8SLennert Buytenhek .get_settings = dsa_slave_get_settings, 74591da11f8SLennert Buytenhek .set_settings = dsa_slave_set_settings, 74691da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 7473d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 7483d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 74991da11f8SLennert Buytenhek .nway_reset = dsa_slave_nway_reset, 75091da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 7516793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 7526793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 7536793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 75491da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 75591da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 75691da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 75719e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 75819e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 7597905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 7607905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 76191da11f8SLennert Buytenhek }; 76291da11f8SLennert Buytenhek 7633e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 764d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 765d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 7663e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 767d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 768d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 769d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 770cdf09697SDavid S. Miller .ndo_fdb_add = dsa_slave_fdb_add, 771cdf09697SDavid S. Miller .ndo_fdb_del = dsa_slave_fdb_del, 772cdf09697SDavid S. Miller .ndo_fdb_dump = dsa_slave_fdb_dump, 773d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 774abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 77504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER 77604ff53f9SFlorian Fainelli .ndo_netpoll_setup = dsa_slave_netpoll_setup, 77704ff53f9SFlorian Fainelli .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, 77804ff53f9SFlorian Fainelli .ndo_poll_controller = dsa_slave_poll_controller, 77904ff53f9SFlorian Fainelli #endif 78098237d43SScott Feldman }; 78198237d43SScott Feldman 7829d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = { 783f8e20a9fSScott Feldman .switchdev_port_attr_get = dsa_slave_port_attr_get, 78435636062SScott Feldman .switchdev_port_attr_set = dsa_slave_port_attr_set, 785d442ad4aSStephen Hemminger }; 78691da11f8SLennert Buytenhek 7870d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 7880d8bcdd3SFlorian Fainelli { 7890d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 790ec9436baSFlorian Fainelli struct dsa_switch *ds = p->parent; 7910d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 7920d8bcdd3SFlorian Fainelli 7930d8bcdd3SFlorian Fainelli if (p->old_link != p->phy->link) { 7940d8bcdd3SFlorian Fainelli status_changed = 1; 7950d8bcdd3SFlorian Fainelli p->old_link = p->phy->link; 7960d8bcdd3SFlorian Fainelli } 7970d8bcdd3SFlorian Fainelli 7980d8bcdd3SFlorian Fainelli if (p->old_duplex != p->phy->duplex) { 7990d8bcdd3SFlorian Fainelli status_changed = 1; 8000d8bcdd3SFlorian Fainelli p->old_duplex = p->phy->duplex; 8010d8bcdd3SFlorian Fainelli } 8020d8bcdd3SFlorian Fainelli 8030d8bcdd3SFlorian Fainelli if (p->old_pause != p->phy->pause) { 8040d8bcdd3SFlorian Fainelli status_changed = 1; 8050d8bcdd3SFlorian Fainelli p->old_pause = p->phy->pause; 8060d8bcdd3SFlorian Fainelli } 8070d8bcdd3SFlorian Fainelli 808ec9436baSFlorian Fainelli if (ds->drv->adjust_link && status_changed) 809ec9436baSFlorian Fainelli ds->drv->adjust_link(ds, p->port, p->phy); 810ec9436baSFlorian Fainelli 8110d8bcdd3SFlorian Fainelli if (status_changed) 8120d8bcdd3SFlorian Fainelli phy_print_status(p->phy); 8130d8bcdd3SFlorian Fainelli } 8140d8bcdd3SFlorian Fainelli 815ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 816ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 817ce31b31cSFlorian Fainelli { 818ce31b31cSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 819ce31b31cSFlorian Fainelli struct dsa_switch *ds = p->parent; 820ce31b31cSFlorian Fainelli 821ce31b31cSFlorian Fainelli if (ds->drv->fixed_link_update) 822ce31b31cSFlorian Fainelli ds->drv->fixed_link_update(ds, p->port, status); 823ce31b31cSFlorian Fainelli 824ce31b31cSFlorian Fainelli return 0; 825ce31b31cSFlorian Fainelli } 826ce31b31cSFlorian Fainelli 82791da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 828c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p, 829cd28a1a9SFlorian Fainelli struct net_device *slave_dev, 830cd28a1a9SFlorian Fainelli int addr) 831c305c165SFlorian Fainelli { 832c305c165SFlorian Fainelli struct dsa_switch *ds = p->parent; 833c305c165SFlorian Fainelli 834cd28a1a9SFlorian Fainelli p->phy = ds->slave_mii_bus->phy_map[addr]; 835c305c165SFlorian Fainelli if (!p->phy) 836c305c165SFlorian Fainelli return -ENODEV; 837c305c165SFlorian Fainelli 838c305c165SFlorian Fainelli /* Use already configured phy mode */ 839c305c165SFlorian Fainelli p->phy_interface = p->phy->interface; 840c305c165SFlorian Fainelli phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 841c305c165SFlorian Fainelli p->phy_interface); 842c305c165SFlorian Fainelli 843c305c165SFlorian Fainelli return 0; 844c305c165SFlorian Fainelli } 845c305c165SFlorian Fainelli 8469697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 8470d8bcdd3SFlorian Fainelli struct net_device *slave_dev) 8480d8bcdd3SFlorian Fainelli { 8490d8bcdd3SFlorian Fainelli struct dsa_switch *ds = p->parent; 8500d8bcdd3SFlorian Fainelli struct dsa_chip_data *cd = ds->pd; 8510d8bcdd3SFlorian Fainelli struct device_node *phy_dn, *port_dn; 852ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 8536819563eSFlorian Fainelli u32 phy_flags = 0; 85419334920SGuenter Roeck int mode, ret; 8550d8bcdd3SFlorian Fainelli 8560d8bcdd3SFlorian Fainelli port_dn = cd->port_dn[p->port]; 85719334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 85819334920SGuenter Roeck if (mode < 0) 85919334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 86019334920SGuenter Roeck p->phy_interface = mode; 8610d8bcdd3SFlorian Fainelli 8620d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 8630d8bcdd3SFlorian Fainelli if (of_phy_is_fixed_link(port_dn)) { 8640d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 8650d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 8660d8bcdd3SFlorian Fainelli */ 8670d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 8680d8bcdd3SFlorian Fainelli if (ret) { 869a2ae6007SJoe Perches netdev_err(slave_dev, "failed to register fixed PHY\n"); 8709697f1cdSFlorian Fainelli return ret; 8710d8bcdd3SFlorian Fainelli } 872ce31b31cSFlorian Fainelli phy_is_fixed = true; 8730d8bcdd3SFlorian Fainelli phy_dn = port_dn; 8740d8bcdd3SFlorian Fainelli } 8750d8bcdd3SFlorian Fainelli 8766819563eSFlorian Fainelli if (ds->drv->get_phy_flags) 8776819563eSFlorian Fainelli phy_flags = ds->drv->get_phy_flags(ds, p->port); 8786819563eSFlorian Fainelli 879cd28a1a9SFlorian Fainelli if (phy_dn) { 880cd28a1a9SFlorian Fainelli ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn); 881cd28a1a9SFlorian Fainelli /* If this PHY address is part of phys_mii_mask, which means 882cd28a1a9SFlorian Fainelli * that we need to divert reads and writes to/from it, then we 883cd28a1a9SFlorian Fainelli * want to bind this device using the slave MII bus created by 884cd28a1a9SFlorian Fainelli * DSA to make that happen. 885cd28a1a9SFlorian Fainelli */ 88696026d05SFlorian Fainelli if (!phy_is_fixed && ret >= 0 && 88796026d05SFlorian Fainelli (ds->phys_mii_mask & (1 << ret))) { 888cd28a1a9SFlorian Fainelli ret = dsa_slave_phy_connect(p, slave_dev, ret); 889cd28a1a9SFlorian Fainelli if (ret) 890cd28a1a9SFlorian Fainelli return ret; 891cd28a1a9SFlorian Fainelli } else { 8920d8bcdd3SFlorian Fainelli p->phy = of_phy_connect(slave_dev, phy_dn, 893cd28a1a9SFlorian Fainelli dsa_slave_adjust_link, 894cd28a1a9SFlorian Fainelli phy_flags, 8950d8bcdd3SFlorian Fainelli p->phy_interface); 896cd28a1a9SFlorian Fainelli } 897cd28a1a9SFlorian Fainelli } 8980d8bcdd3SFlorian Fainelli 899ce31b31cSFlorian Fainelli if (p->phy && phy_is_fixed) 900ce31b31cSFlorian Fainelli fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 901ce31b31cSFlorian Fainelli 9020d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 9030d8bcdd3SFlorian Fainelli * MDIO bus instead 9040d8bcdd3SFlorian Fainelli */ 905b31f65fbSAndrew Lunn if (!p->phy) { 906cd28a1a9SFlorian Fainelli ret = dsa_slave_phy_connect(p, slave_dev, p->port); 907c305c165SFlorian Fainelli if (ret) 908c305c165SFlorian Fainelli return ret; 909b31f65fbSAndrew Lunn } else { 910a2ae6007SJoe Perches netdev_info(slave_dev, "attached PHY at address %d [%s]\n", 9110d8bcdd3SFlorian Fainelli p->phy->addr, p->phy->drv->name); 9120d8bcdd3SFlorian Fainelli } 9139697f1cdSFlorian Fainelli 9149697f1cdSFlorian Fainelli return 0; 915b31f65fbSAndrew Lunn } 9160d8bcdd3SFlorian Fainelli 917448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 918448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 919448b4482SAndrew Lunn struct netdev_queue *txq, 920448b4482SAndrew Lunn void *_unused) 921448b4482SAndrew Lunn { 922448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 923448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 924448b4482SAndrew Lunn } 925448b4482SAndrew Lunn 92624462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 92724462549SFlorian Fainelli { 92824462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 92924462549SFlorian Fainelli 93024462549SFlorian Fainelli if (p->phy) { 93124462549SFlorian Fainelli phy_stop(p->phy); 93224462549SFlorian Fainelli p->old_pause = -1; 93324462549SFlorian Fainelli p->old_link = -1; 93424462549SFlorian Fainelli p->old_duplex = -1; 93524462549SFlorian Fainelli phy_suspend(p->phy); 93624462549SFlorian Fainelli } 93724462549SFlorian Fainelli 93824462549SFlorian Fainelli return 0; 93924462549SFlorian Fainelli } 94024462549SFlorian Fainelli 94124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 94224462549SFlorian Fainelli { 94324462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 94424462549SFlorian Fainelli 94524462549SFlorian Fainelli netif_device_attach(slave_dev); 94624462549SFlorian Fainelli 94724462549SFlorian Fainelli if (p->phy) { 94824462549SFlorian Fainelli phy_resume(p->phy); 94924462549SFlorian Fainelli phy_start(p->phy); 95024462549SFlorian Fainelli } 95124462549SFlorian Fainelli 95224462549SFlorian Fainelli return 0; 95324462549SFlorian Fainelli } 95424462549SFlorian Fainelli 955d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 95691da11f8SLennert Buytenhek int port, char *name) 95791da11f8SLennert Buytenhek { 958e84665c9SLennert Buytenhek struct net_device *master = ds->dst->master_netdev; 95991da11f8SLennert Buytenhek struct net_device *slave_dev; 96091da11f8SLennert Buytenhek struct dsa_slave_priv *p; 96191da11f8SLennert Buytenhek int ret; 96291da11f8SLennert Buytenhek 963c835a677STom Gundersen slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 964c835a677STom Gundersen NET_NAME_UNKNOWN, ether_setup); 96591da11f8SLennert Buytenhek if (slave_dev == NULL) 966d87d6f44SGuenter Roeck return -ENOMEM; 96791da11f8SLennert Buytenhek 96891da11f8SLennert Buytenhek slave_dev->features = master->vlan_features; 9697ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 9702fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 97191da11f8SLennert Buytenhek slave_dev->tx_queue_len = 0; 9723e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 9739d47c0a2SJiri Pirko slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 974d442ad4aSStephen Hemminger 975448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 976448b4482SAndrew Lunn NULL); 977448b4482SAndrew Lunn 97891da11f8SLennert Buytenhek SET_NETDEV_DEV(slave_dev, parent); 979bd47497aSFlorian Fainelli slave_dev->dev.of_node = ds->pd->port_dn[port]; 98091da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 98191da11f8SLennert Buytenhek 98291da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 98391da11f8SLennert Buytenhek p->dev = slave_dev; 98491da11f8SLennert Buytenhek p->parent = ds; 98591da11f8SLennert Buytenhek p->port = port; 9860d8bcdd3SFlorian Fainelli 9875075314eSAlexander Duyck switch (ds->dst->tag_protocol) { 9885075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA 9895075314eSAlexander Duyck case DSA_TAG_PROTO_DSA: 9905075314eSAlexander Duyck p->xmit = dsa_netdev_ops.xmit; 9915075314eSAlexander Duyck break; 9925075314eSAlexander Duyck #endif 9935075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA 9945075314eSAlexander Duyck case DSA_TAG_PROTO_EDSA: 9955075314eSAlexander Duyck p->xmit = edsa_netdev_ops.xmit; 9965075314eSAlexander Duyck break; 9975075314eSAlexander Duyck #endif 9985075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER 9995075314eSAlexander Duyck case DSA_TAG_PROTO_TRAILER: 10005075314eSAlexander Duyck p->xmit = trailer_netdev_ops.xmit; 10015075314eSAlexander Duyck break; 10025075314eSAlexander Duyck #endif 10035075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM 10045075314eSAlexander Duyck case DSA_TAG_PROTO_BRCM: 10055075314eSAlexander Duyck p->xmit = brcm_netdev_ops.xmit; 10065075314eSAlexander Duyck break; 10075075314eSAlexander Duyck #endif 10085075314eSAlexander Duyck default: 10095075314eSAlexander Duyck p->xmit = dsa_slave_notag_xmit; 10105075314eSAlexander Duyck break; 10115075314eSAlexander Duyck } 10125075314eSAlexander Duyck 10130d8bcdd3SFlorian Fainelli p->old_pause = -1; 10140d8bcdd3SFlorian Fainelli p->old_link = -1; 10150d8bcdd3SFlorian Fainelli p->old_duplex = -1; 10160d8bcdd3SFlorian Fainelli 10179697f1cdSFlorian Fainelli ret = dsa_slave_phy_setup(p, slave_dev); 10189697f1cdSFlorian Fainelli if (ret) { 10199697f1cdSFlorian Fainelli free_netdev(slave_dev); 1020d87d6f44SGuenter Roeck return ret; 10219697f1cdSFlorian Fainelli } 102291da11f8SLennert Buytenhek 1023d87d6f44SGuenter Roeck ds->ports[port] = slave_dev; 102491da11f8SLennert Buytenhek ret = register_netdev(slave_dev); 102591da11f8SLennert Buytenhek if (ret) { 1026a2ae6007SJoe Perches netdev_err(master, "error %d registering interface %s\n", 1027a2ae6007SJoe Perches ret, slave_dev->name); 10289697f1cdSFlorian Fainelli phy_disconnect(p->phy); 1029d87d6f44SGuenter Roeck ds->ports[port] = NULL; 103091da11f8SLennert Buytenhek free_netdev(slave_dev); 1031d87d6f44SGuenter Roeck return ret; 103291da11f8SLennert Buytenhek } 103391da11f8SLennert Buytenhek 103491da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 103591da11f8SLennert Buytenhek 1036d87d6f44SGuenter Roeck return 0; 103791da11f8SLennert Buytenhek } 1038b73adef6SFlorian Fainelli 1039b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev) 1040b73adef6SFlorian Fainelli { 1041b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 1042b73adef6SFlorian Fainelli } 1043b73adef6SFlorian Fainelli 1044b73adef6SFlorian Fainelli static int dsa_slave_master_changed(struct net_device *dev) 1045b73adef6SFlorian Fainelli { 1046b73adef6SFlorian Fainelli struct net_device *master = netdev_master_upper_dev_get(dev); 1047b06b107aSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 1048b73adef6SFlorian Fainelli int err = 0; 1049b73adef6SFlorian Fainelli 1050b73adef6SFlorian Fainelli if (master && master->rtnl_link_ops && 1051b73adef6SFlorian Fainelli !strcmp(master->rtnl_link_ops->kind, "bridge")) 1052b73adef6SFlorian Fainelli err = dsa_slave_bridge_port_join(dev, master); 1053b06b107aSGuenter Roeck else if (dsa_port_is_bridged(p)) 1054b73adef6SFlorian Fainelli err = dsa_slave_bridge_port_leave(dev); 1055b73adef6SFlorian Fainelli 1056b73adef6SFlorian Fainelli return err; 1057b73adef6SFlorian Fainelli } 1058b73adef6SFlorian Fainelli 1059b73adef6SFlorian Fainelli int dsa_slave_netdevice_event(struct notifier_block *unused, 1060b73adef6SFlorian Fainelli unsigned long event, void *ptr) 1061b73adef6SFlorian Fainelli { 1062b73adef6SFlorian Fainelli struct net_device *dev; 1063b73adef6SFlorian Fainelli int err = 0; 1064b73adef6SFlorian Fainelli 1065b73adef6SFlorian Fainelli switch (event) { 1066b73adef6SFlorian Fainelli case NETDEV_CHANGEUPPER: 1067b73adef6SFlorian Fainelli dev = netdev_notifier_info_to_dev(ptr); 1068b73adef6SFlorian Fainelli if (!dsa_slave_dev_check(dev)) 1069b73adef6SFlorian Fainelli goto out; 1070b73adef6SFlorian Fainelli 1071b73adef6SFlorian Fainelli err = dsa_slave_master_changed(dev); 1072b73adef6SFlorian Fainelli if (err) 1073b73adef6SFlorian Fainelli netdev_warn(dev, "failed to reflect master change\n"); 1074b73adef6SFlorian Fainelli 1075b73adef6SFlorian Fainelli break; 1076b73adef6SFlorian Fainelli } 1077b73adef6SFlorian Fainelli 1078b73adef6SFlorian Fainelli out: 1079b73adef6SFlorian Fainelli return NOTIFY_DONE; 1080b73adef6SFlorian Fainelli } 1081