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> 2191da11f8SLennert Buytenhek #include "dsa_priv.h" 2291da11f8SLennert Buytenhek 2391da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2491da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2591da11f8SLennert Buytenhek { 2691da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 2791da11f8SLennert Buytenhek 280d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 2991da11f8SLennert Buytenhek return ds->drv->phy_read(ds, addr, reg); 3091da11f8SLennert Buytenhek 3191da11f8SLennert Buytenhek return 0xffff; 3291da11f8SLennert Buytenhek } 3391da11f8SLennert Buytenhek 3491da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3591da11f8SLennert Buytenhek { 3691da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3791da11f8SLennert Buytenhek 380d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 3991da11f8SLennert Buytenhek return ds->drv->phy_write(ds, addr, reg, val); 4091da11f8SLennert Buytenhek 4191da11f8SLennert Buytenhek return 0; 4291da11f8SLennert Buytenhek } 4391da11f8SLennert Buytenhek 4491da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4591da11f8SLennert Buytenhek { 4691da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 4791da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 4891da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 4991da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 50f490be04SFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", 51f490be04SFlorian Fainelli ds->index, ds->pd->sw_addr); 52b4d2394dSAlexander Duyck ds->slave_mii_bus->parent = ds->master_dev; 5324df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5491da11f8SLennert Buytenhek } 5591da11f8SLennert Buytenhek 5691da11f8SLennert Buytenhek 5791da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 58abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev) 59c0840801SLennert Buytenhek { 60c0840801SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 61c0840801SLennert Buytenhek 62abd2be00SNicolas Dichtel return p->parent->dst->master_netdev->ifindex; 63c0840801SLennert Buytenhek } 64c0840801SLennert Buytenhek 65b73adef6SFlorian Fainelli static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p) 66b73adef6SFlorian Fainelli { 67b73adef6SFlorian Fainelli return !!p->bridge_dev; 68b73adef6SFlorian Fainelli } 69b73adef6SFlorian Fainelli 7091da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 7191da11f8SLennert Buytenhek { 72df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 73e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 74b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 75b73adef6SFlorian Fainelli u8 stp_state = dsa_port_is_bridged(p) ? 76b73adef6SFlorian Fainelli BR_STATE_BLOCKING : BR_STATE_FORWARDING; 77df02c6ffSLennert Buytenhek int err; 78df02c6ffSLennert Buytenhek 79df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 80df02c6ffSLennert Buytenhek return -ENETDOWN; 81df02c6ffSLennert Buytenhek 828feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 83a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 84df02c6ffSLennert Buytenhek if (err < 0) 85df02c6ffSLennert Buytenhek goto out; 86df02c6ffSLennert Buytenhek } 87df02c6ffSLennert Buytenhek 88df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 89df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 90df02c6ffSLennert Buytenhek if (err < 0) 91df02c6ffSLennert Buytenhek goto del_unicast; 92df02c6ffSLennert Buytenhek } 93df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 94df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 95df02c6ffSLennert Buytenhek if (err < 0) 96df02c6ffSLennert Buytenhek goto clear_allmulti; 97df02c6ffSLennert Buytenhek } 98df02c6ffSLennert Buytenhek 99b2f2af21SFlorian Fainelli if (ds->drv->port_enable) { 100b2f2af21SFlorian Fainelli err = ds->drv->port_enable(ds, p->port, p->phy); 101b2f2af21SFlorian Fainelli if (err) 102b2f2af21SFlorian Fainelli goto clear_promisc; 103b2f2af21SFlorian Fainelli } 104b2f2af21SFlorian Fainelli 105b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 106b73adef6SFlorian Fainelli ds->drv->port_stp_update(ds, p->port, stp_state); 107b73adef6SFlorian Fainelli 108f7f1de51SFlorian Fainelli if (p->phy) 109f7f1de51SFlorian Fainelli phy_start(p->phy); 110f7f1de51SFlorian Fainelli 11191da11f8SLennert Buytenhek return 0; 112df02c6ffSLennert Buytenhek 113b2f2af21SFlorian Fainelli clear_promisc: 114b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 1154fdeddfeSGilad Ben-Yossef dev_set_promiscuity(master, -1); 116df02c6ffSLennert Buytenhek clear_allmulti: 117df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 118df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 119df02c6ffSLennert Buytenhek del_unicast: 1208feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 121a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 122df02c6ffSLennert Buytenhek out: 123df02c6ffSLennert Buytenhek return err; 12491da11f8SLennert Buytenhek } 12591da11f8SLennert Buytenhek 12691da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 12791da11f8SLennert Buytenhek { 128df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 129e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 130b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 131df02c6ffSLennert Buytenhek 132f7f1de51SFlorian Fainelli if (p->phy) 133f7f1de51SFlorian Fainelli phy_stop(p->phy); 134f7f1de51SFlorian Fainelli 135df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 136a748ee24SJiri Pirko dev_uc_unsync(master, dev); 137df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 138df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 139df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 140df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 141df02c6ffSLennert Buytenhek 1428feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 143a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 144df02c6ffSLennert Buytenhek 145b2f2af21SFlorian Fainelli if (ds->drv->port_disable) 146b2f2af21SFlorian Fainelli ds->drv->port_disable(ds, p->port, p->phy); 147b2f2af21SFlorian Fainelli 148b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 149b73adef6SFlorian Fainelli ds->drv->port_stp_update(ds, p->port, 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); 157e84665c9SLennert Buytenhek struct net_device *master = p->parent->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); 168e84665c9SLennert Buytenhek struct net_device *master = p->parent->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); 177e84665c9SLennert Buytenhek struct net_device *master = p->parent->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 202339d8262SGuenter Roeck static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 203339d8262SGuenter Roeck struct net_device *dev, 204339d8262SGuenter Roeck const unsigned char *addr, u16 vid, u16 nlm_flags) 205339d8262SGuenter Roeck { 206339d8262SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 207339d8262SGuenter Roeck struct dsa_switch *ds = p->parent; 208339d8262SGuenter Roeck int ret = -EOPNOTSUPP; 209339d8262SGuenter Roeck 210339d8262SGuenter Roeck if (ds->drv->fdb_add) 211339d8262SGuenter Roeck ret = ds->drv->fdb_add(ds, p->port, addr, vid); 212339d8262SGuenter Roeck 213339d8262SGuenter Roeck return ret; 214339d8262SGuenter Roeck } 215339d8262SGuenter Roeck 216339d8262SGuenter Roeck static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 217339d8262SGuenter Roeck struct net_device *dev, 218339d8262SGuenter Roeck const unsigned char *addr, u16 vid) 219339d8262SGuenter Roeck { 220339d8262SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 221339d8262SGuenter Roeck struct dsa_switch *ds = p->parent; 222339d8262SGuenter Roeck int ret = -EOPNOTSUPP; 223339d8262SGuenter Roeck 224339d8262SGuenter Roeck if (ds->drv->fdb_del) 225339d8262SGuenter Roeck ret = ds->drv->fdb_del(ds, p->port, addr, vid); 226339d8262SGuenter Roeck 227339d8262SGuenter Roeck return ret; 228339d8262SGuenter Roeck } 229339d8262SGuenter Roeck 230339d8262SGuenter Roeck static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb, 231339d8262SGuenter Roeck const unsigned char *addr, u16 vid, 232339d8262SGuenter Roeck bool is_static, 233339d8262SGuenter Roeck u32 portid, u32 seq, int type, 234339d8262SGuenter Roeck unsigned int flags) 235339d8262SGuenter Roeck { 236339d8262SGuenter Roeck struct nlmsghdr *nlh; 237339d8262SGuenter Roeck struct ndmsg *ndm; 238339d8262SGuenter Roeck 239339d8262SGuenter Roeck nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); 240339d8262SGuenter Roeck if (!nlh) 241339d8262SGuenter Roeck return -EMSGSIZE; 242339d8262SGuenter Roeck 243339d8262SGuenter Roeck ndm = nlmsg_data(nlh); 244339d8262SGuenter Roeck ndm->ndm_family = AF_BRIDGE; 245339d8262SGuenter Roeck ndm->ndm_pad1 = 0; 246339d8262SGuenter Roeck ndm->ndm_pad2 = 0; 247339d8262SGuenter Roeck ndm->ndm_flags = NTF_EXT_LEARNED; 248339d8262SGuenter Roeck ndm->ndm_type = 0; 249339d8262SGuenter Roeck ndm->ndm_ifindex = dev->ifindex; 250339d8262SGuenter Roeck ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 251339d8262SGuenter Roeck 252339d8262SGuenter Roeck if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 253339d8262SGuenter Roeck goto nla_put_failure; 254339d8262SGuenter Roeck 255339d8262SGuenter Roeck if (vid && nla_put_u16(skb, NDA_VLAN, vid)) 256339d8262SGuenter Roeck goto nla_put_failure; 257339d8262SGuenter Roeck 258339d8262SGuenter Roeck nlmsg_end(skb, nlh); 259339d8262SGuenter Roeck return 0; 260339d8262SGuenter Roeck 261339d8262SGuenter Roeck nla_put_failure: 262339d8262SGuenter Roeck nlmsg_cancel(skb, nlh); 263339d8262SGuenter Roeck return -EMSGSIZE; 264339d8262SGuenter Roeck } 265339d8262SGuenter Roeck 266339d8262SGuenter Roeck /* Dump information about entries, in response to GETNEIGH */ 267339d8262SGuenter Roeck static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 268339d8262SGuenter Roeck struct net_device *dev, 269339d8262SGuenter Roeck struct net_device *filter_dev, int idx) 270339d8262SGuenter Roeck { 271339d8262SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 272339d8262SGuenter Roeck struct dsa_switch *ds = p->parent; 273339d8262SGuenter Roeck unsigned char addr[ETH_ALEN] = { 0 }; 274339d8262SGuenter Roeck int ret; 275339d8262SGuenter Roeck 276339d8262SGuenter Roeck if (!ds->drv->fdb_getnext) 277339d8262SGuenter Roeck return -EOPNOTSUPP; 278339d8262SGuenter Roeck 279339d8262SGuenter Roeck for (; ; idx++) { 280339d8262SGuenter Roeck bool is_static; 281339d8262SGuenter Roeck 282339d8262SGuenter Roeck ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static); 283339d8262SGuenter Roeck if (ret < 0) 284339d8262SGuenter Roeck break; 285339d8262SGuenter Roeck 286339d8262SGuenter Roeck if (idx < cb->args[0]) 287339d8262SGuenter Roeck continue; 288339d8262SGuenter Roeck 289339d8262SGuenter Roeck ret = dsa_slave_fill_info(dev, skb, addr, 0, 290339d8262SGuenter Roeck is_static, 291339d8262SGuenter Roeck NETLINK_CB(cb->skb).portid, 292339d8262SGuenter Roeck cb->nlh->nlmsg_seq, 293339d8262SGuenter Roeck RTM_NEWNEIGH, NLM_F_MULTI); 294339d8262SGuenter Roeck if (ret < 0) 295339d8262SGuenter Roeck break; 296339d8262SGuenter Roeck } 297339d8262SGuenter Roeck 298339d8262SGuenter Roeck return idx; 299339d8262SGuenter Roeck } 300339d8262SGuenter Roeck 30191da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 30291da11f8SLennert Buytenhek { 30391da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 30491da11f8SLennert Buytenhek 30591da11f8SLennert Buytenhek if (p->phy != NULL) 30628b04113SRichard Cochran return phy_mii_ioctl(p->phy, ifr, cmd); 30791da11f8SLennert Buytenhek 30891da11f8SLennert Buytenhek return -EOPNOTSUPP; 30991da11f8SLennert Buytenhek } 31091da11f8SLennert Buytenhek 311b73adef6SFlorian Fainelli /* Return a bitmask of all ports being currently bridged within a given bridge 312b73adef6SFlorian Fainelli * device. Note that on leave, the mask will still return the bitmask of ports 313b73adef6SFlorian Fainelli * currently bridged, prior to port removal, and this is exactly what we want. 314b73adef6SFlorian Fainelli */ 315b73adef6SFlorian Fainelli static u32 dsa_slave_br_port_mask(struct dsa_switch *ds, 316b73adef6SFlorian Fainelli struct net_device *bridge) 317b73adef6SFlorian Fainelli { 318b73adef6SFlorian Fainelli struct dsa_slave_priv *p; 319b73adef6SFlorian Fainelli unsigned int port; 320b73adef6SFlorian Fainelli u32 mask = 0; 321b73adef6SFlorian Fainelli 322b73adef6SFlorian Fainelli for (port = 0; port < DSA_MAX_PORTS; port++) { 323d79d2107SGuenter Roeck if (!dsa_is_port_initialized(ds, port)) 324b73adef6SFlorian Fainelli continue; 325b73adef6SFlorian Fainelli 326b73adef6SFlorian Fainelli p = netdev_priv(ds->ports[port]); 327b73adef6SFlorian Fainelli 328b73adef6SFlorian Fainelli if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT && 329b73adef6SFlorian Fainelli p->bridge_dev == bridge) 330b73adef6SFlorian Fainelli mask |= 1 << port; 331b73adef6SFlorian Fainelli } 332b73adef6SFlorian Fainelli 333b73adef6SFlorian Fainelli return mask; 334b73adef6SFlorian Fainelli } 335b73adef6SFlorian Fainelli 336b73adef6SFlorian Fainelli static int dsa_slave_stp_update(struct net_device *dev, u8 state) 337b73adef6SFlorian Fainelli { 338b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 339b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 340b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 341b73adef6SFlorian Fainelli 342b73adef6SFlorian Fainelli if (ds->drv->port_stp_update) 343b73adef6SFlorian Fainelli ret = ds->drv->port_stp_update(ds, p->port, state); 344b73adef6SFlorian Fainelli 345b73adef6SFlorian Fainelli return ret; 346b73adef6SFlorian Fainelli } 347b73adef6SFlorian Fainelli 34835636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev, 34935636062SScott Feldman struct switchdev_attr *attr) 35035636062SScott Feldman { 35135636062SScott Feldman int ret = 0; 35235636062SScott Feldman 35335636062SScott Feldman switch (attr->id) { 35435636062SScott Feldman case SWITCHDEV_ATTR_PORT_STP_STATE: 35535636062SScott Feldman if (attr->trans == SWITCHDEV_TRANS_COMMIT) 35642275bd8SScott Feldman ret = dsa_slave_stp_update(dev, attr->u.stp_state); 35735636062SScott Feldman break; 35835636062SScott Feldman default: 35935636062SScott Feldman ret = -EOPNOTSUPP; 36035636062SScott Feldman break; 36135636062SScott Feldman } 36235636062SScott Feldman 36335636062SScott Feldman return ret; 36435636062SScott Feldman } 36535636062SScott Feldman 366b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev, 367b73adef6SFlorian Fainelli struct net_device *br) 368b73adef6SFlorian Fainelli { 369b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 370b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 371b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 372b73adef6SFlorian Fainelli 373b73adef6SFlorian Fainelli p->bridge_dev = br; 374b73adef6SFlorian Fainelli 375b73adef6SFlorian Fainelli if (ds->drv->port_join_bridge) 376b73adef6SFlorian Fainelli ret = ds->drv->port_join_bridge(ds, p->port, 377b73adef6SFlorian Fainelli dsa_slave_br_port_mask(ds, br)); 378b73adef6SFlorian Fainelli 379b73adef6SFlorian Fainelli return ret; 380b73adef6SFlorian Fainelli } 381b73adef6SFlorian Fainelli 382b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_leave(struct net_device *dev) 383b73adef6SFlorian Fainelli { 384b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 385b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 386b73adef6SFlorian Fainelli int ret = -EOPNOTSUPP; 387b73adef6SFlorian Fainelli 388b73adef6SFlorian Fainelli 389b73adef6SFlorian Fainelli if (ds->drv->port_leave_bridge) 390b73adef6SFlorian Fainelli ret = ds->drv->port_leave_bridge(ds, p->port, 391b73adef6SFlorian Fainelli dsa_slave_br_port_mask(ds, p->bridge_dev)); 392b73adef6SFlorian Fainelli 393b73adef6SFlorian Fainelli p->bridge_dev = NULL; 394b73adef6SFlorian Fainelli 395b73adef6SFlorian Fainelli /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, 396b73adef6SFlorian Fainelli * so allow it to be in BR_STATE_FORWARDING to be kept functional 397b73adef6SFlorian Fainelli */ 398b73adef6SFlorian Fainelli dsa_slave_stp_update(dev, BR_STATE_FORWARDING); 399b73adef6SFlorian Fainelli 400b73adef6SFlorian Fainelli return ret; 401b73adef6SFlorian Fainelli } 402b73adef6SFlorian Fainelli 403f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev, 404f8e20a9fSScott Feldman struct switchdev_attr *attr) 405b73adef6SFlorian Fainelli { 406b73adef6SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 407b73adef6SFlorian Fainelli struct dsa_switch *ds = p->parent; 408b73adef6SFlorian Fainelli 409f8e20a9fSScott Feldman switch (attr->id) { 410f8e20a9fSScott Feldman case SWITCHDEV_ATTR_PORT_PARENT_ID: 41142275bd8SScott Feldman attr->u.ppid.id_len = sizeof(ds->index); 41242275bd8SScott Feldman memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); 413f8e20a9fSScott Feldman break; 414f8e20a9fSScott Feldman default: 415f8e20a9fSScott Feldman return -EOPNOTSUPP; 416f8e20a9fSScott Feldman } 417b73adef6SFlorian Fainelli 418b73adef6SFlorian Fainelli return 0; 419b73adef6SFlorian Fainelli } 420b73adef6SFlorian Fainelli 4213e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 4223e8a72d1SFlorian Fainelli { 4233e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 424*4ed70ce9SFlorian Fainelli struct sk_buff *nskb; 4253e8a72d1SFlorian Fainelli 426*4ed70ce9SFlorian Fainelli dev->stats.tx_packets++; 427*4ed70ce9SFlorian Fainelli dev->stats.tx_bytes += skb->len; 4283e8a72d1SFlorian Fainelli 429*4ed70ce9SFlorian Fainelli /* Transmit function may have to reallocate the original SKB */ 430*4ed70ce9SFlorian Fainelli nskb = p->xmit(skb, dev); 431*4ed70ce9SFlorian Fainelli if (!nskb) 432*4ed70ce9SFlorian Fainelli return NETDEV_TX_OK; 4335aed85ceSFlorian Fainelli 434*4ed70ce9SFlorian Fainelli /* Queue the SKB for transmission on the parent interface, but 435*4ed70ce9SFlorian Fainelli * do not modify its EtherType 436*4ed70ce9SFlorian Fainelli */ 437*4ed70ce9SFlorian Fainelli nskb->dev = p->parent->dst->master_netdev; 438*4ed70ce9SFlorian Fainelli dev_queue_xmit(nskb); 4395aed85ceSFlorian Fainelli 4405aed85ceSFlorian Fainelli return NETDEV_TX_OK; 4415aed85ceSFlorian Fainelli } 4425aed85ceSFlorian Fainelli 443*4ed70ce9SFlorian Fainelli static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, 444*4ed70ce9SFlorian Fainelli struct net_device *dev) 445*4ed70ce9SFlorian Fainelli { 446*4ed70ce9SFlorian Fainelli /* Just return the original SKB */ 447*4ed70ce9SFlorian Fainelli return skb; 448*4ed70ce9SFlorian Fainelli } 449*4ed70ce9SFlorian Fainelli 45091da11f8SLennert Buytenhek 45191da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 45291da11f8SLennert Buytenhek static int 45391da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 45491da11f8SLennert Buytenhek { 45591da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 45691da11f8SLennert Buytenhek int err; 45791da11f8SLennert Buytenhek 45891da11f8SLennert Buytenhek err = -EOPNOTSUPP; 45991da11f8SLennert Buytenhek if (p->phy != NULL) { 46091da11f8SLennert Buytenhek err = phy_read_status(p->phy); 46191da11f8SLennert Buytenhek if (err == 0) 46291da11f8SLennert Buytenhek err = phy_ethtool_gset(p->phy, cmd); 46391da11f8SLennert Buytenhek } 46491da11f8SLennert Buytenhek 46591da11f8SLennert Buytenhek return err; 46691da11f8SLennert Buytenhek } 46791da11f8SLennert Buytenhek 46891da11f8SLennert Buytenhek static int 46991da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 47091da11f8SLennert Buytenhek { 47191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 47291da11f8SLennert Buytenhek 47391da11f8SLennert Buytenhek if (p->phy != NULL) 47491da11f8SLennert Buytenhek return phy_ethtool_sset(p->phy, cmd); 47591da11f8SLennert Buytenhek 47691da11f8SLennert Buytenhek return -EOPNOTSUPP; 47791da11f8SLennert Buytenhek } 47891da11f8SLennert Buytenhek 47991da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 48091da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 48191da11f8SLennert Buytenhek { 4827826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 4837826d43fSJiri Pirko strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); 4847826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 4857826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 48691da11f8SLennert Buytenhek } 48791da11f8SLennert Buytenhek 4883d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 4893d762a0fSGuenter Roeck { 4903d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 4913d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 4923d762a0fSGuenter Roeck 4933d762a0fSGuenter Roeck if (ds->drv->get_regs_len) 4943d762a0fSGuenter Roeck return ds->drv->get_regs_len(ds, p->port); 4953d762a0fSGuenter Roeck 4963d762a0fSGuenter Roeck return -EOPNOTSUPP; 4973d762a0fSGuenter Roeck } 4983d762a0fSGuenter Roeck 4993d762a0fSGuenter Roeck static void 5003d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 5013d762a0fSGuenter Roeck { 5023d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5033d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 5043d762a0fSGuenter Roeck 5053d762a0fSGuenter Roeck if (ds->drv->get_regs) 5063d762a0fSGuenter Roeck ds->drv->get_regs(ds, p->port, regs, _p); 5073d762a0fSGuenter Roeck } 5083d762a0fSGuenter Roeck 50991da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev) 51091da11f8SLennert Buytenhek { 51191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 51291da11f8SLennert Buytenhek 51391da11f8SLennert Buytenhek if (p->phy != NULL) 51491da11f8SLennert Buytenhek return genphy_restart_aneg(p->phy); 51591da11f8SLennert Buytenhek 51691da11f8SLennert Buytenhek return -EOPNOTSUPP; 51791da11f8SLennert Buytenhek } 51891da11f8SLennert Buytenhek 51991da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 52091da11f8SLennert Buytenhek { 52191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 52291da11f8SLennert Buytenhek 52391da11f8SLennert Buytenhek if (p->phy != NULL) { 52491da11f8SLennert Buytenhek genphy_update_link(p->phy); 52591da11f8SLennert Buytenhek return p->phy->link; 52691da11f8SLennert Buytenhek } 52791da11f8SLennert Buytenhek 52891da11f8SLennert Buytenhek return -EOPNOTSUPP; 52991da11f8SLennert Buytenhek } 53091da11f8SLennert Buytenhek 5316793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 5326793abb4SGuenter Roeck { 5336793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5346793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5356793abb4SGuenter Roeck 5366793abb4SGuenter Roeck if (ds->pd->eeprom_len) 5376793abb4SGuenter Roeck return ds->pd->eeprom_len; 5386793abb4SGuenter Roeck 5396793abb4SGuenter Roeck if (ds->drv->get_eeprom_len) 5406793abb4SGuenter Roeck return ds->drv->get_eeprom_len(ds); 5416793abb4SGuenter Roeck 5426793abb4SGuenter Roeck return 0; 5436793abb4SGuenter Roeck } 5446793abb4SGuenter Roeck 5456793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 5466793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5476793abb4SGuenter Roeck { 5486793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5496793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5506793abb4SGuenter Roeck 5516793abb4SGuenter Roeck if (ds->drv->get_eeprom) 5526793abb4SGuenter Roeck return ds->drv->get_eeprom(ds, eeprom, data); 5536793abb4SGuenter Roeck 5546793abb4SGuenter Roeck return -EOPNOTSUPP; 5556793abb4SGuenter Roeck } 5566793abb4SGuenter Roeck 5576793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 5586793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 5596793abb4SGuenter Roeck { 5606793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 5616793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 5626793abb4SGuenter Roeck 5636793abb4SGuenter Roeck if (ds->drv->set_eeprom) 5646793abb4SGuenter Roeck return ds->drv->set_eeprom(ds, eeprom, data); 5656793abb4SGuenter Roeck 5666793abb4SGuenter Roeck return -EOPNOTSUPP; 5676793abb4SGuenter Roeck } 5686793abb4SGuenter Roeck 56991da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 57091da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 57191da11f8SLennert Buytenhek { 57291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 57391da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 57491da11f8SLennert Buytenhek 57591da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 57691da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 57791da11f8SLennert Buytenhek 57891da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 57991da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 58091da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 58191da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 58291da11f8SLennert Buytenhek if (ds->drv->get_strings != NULL) 58391da11f8SLennert Buytenhek ds->drv->get_strings(ds, p->port, data + 4 * len); 58491da11f8SLennert Buytenhek } 58591da11f8SLennert Buytenhek } 58691da11f8SLennert Buytenhek 58791da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 58891da11f8SLennert Buytenhek struct ethtool_stats *stats, 58991da11f8SLennert Buytenhek uint64_t *data) 59091da11f8SLennert Buytenhek { 59191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 59291da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 59391da11f8SLennert Buytenhek 59491da11f8SLennert Buytenhek data[0] = p->dev->stats.tx_packets; 59591da11f8SLennert Buytenhek data[1] = p->dev->stats.tx_bytes; 59691da11f8SLennert Buytenhek data[2] = p->dev->stats.rx_packets; 59791da11f8SLennert Buytenhek data[3] = p->dev->stats.rx_bytes; 59891da11f8SLennert Buytenhek if (ds->drv->get_ethtool_stats != NULL) 59991da11f8SLennert Buytenhek ds->drv->get_ethtool_stats(ds, p->port, data + 4); 60091da11f8SLennert Buytenhek } 60191da11f8SLennert Buytenhek 60291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 60391da11f8SLennert Buytenhek { 60491da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 60591da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 60691da11f8SLennert Buytenhek 60791da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 60891da11f8SLennert Buytenhek int count; 60991da11f8SLennert Buytenhek 61091da11f8SLennert Buytenhek count = 4; 61191da11f8SLennert Buytenhek if (ds->drv->get_sset_count != NULL) 61291da11f8SLennert Buytenhek count += ds->drv->get_sset_count(ds); 61391da11f8SLennert Buytenhek 61491da11f8SLennert Buytenhek return count; 61591da11f8SLennert Buytenhek } 61691da11f8SLennert Buytenhek 61791da11f8SLennert Buytenhek return -EOPNOTSUPP; 61891da11f8SLennert Buytenhek } 61991da11f8SLennert Buytenhek 62019e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 62119e57c4eSFlorian Fainelli { 62219e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 62319e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 62419e57c4eSFlorian Fainelli 62519e57c4eSFlorian Fainelli if (ds->drv->get_wol) 62619e57c4eSFlorian Fainelli ds->drv->get_wol(ds, p->port, w); 62719e57c4eSFlorian Fainelli } 62819e57c4eSFlorian Fainelli 62919e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 63019e57c4eSFlorian Fainelli { 63119e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 63219e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 63319e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 63419e57c4eSFlorian Fainelli 63519e57c4eSFlorian Fainelli if (ds->drv->set_wol) 63619e57c4eSFlorian Fainelli ret = ds->drv->set_wol(ds, p->port, w); 63719e57c4eSFlorian Fainelli 63819e57c4eSFlorian Fainelli return ret; 63919e57c4eSFlorian Fainelli } 64019e57c4eSFlorian Fainelli 6417905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 6427905288fSFlorian Fainelli { 6437905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 6447905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 6457905288fSFlorian Fainelli int ret; 6467905288fSFlorian Fainelli 6477905288fSFlorian Fainelli if (!ds->drv->set_eee) 6487905288fSFlorian Fainelli return -EOPNOTSUPP; 6497905288fSFlorian Fainelli 6507905288fSFlorian Fainelli ret = ds->drv->set_eee(ds, p->port, p->phy, e); 6517905288fSFlorian Fainelli if (ret) 6527905288fSFlorian Fainelli return ret; 6537905288fSFlorian Fainelli 6547905288fSFlorian Fainelli if (p->phy) 6557905288fSFlorian Fainelli ret = phy_ethtool_set_eee(p->phy, e); 6567905288fSFlorian Fainelli 6577905288fSFlorian Fainelli return ret; 6587905288fSFlorian Fainelli } 6597905288fSFlorian Fainelli 6607905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 6617905288fSFlorian Fainelli { 6627905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 6637905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 6647905288fSFlorian Fainelli int ret; 6657905288fSFlorian Fainelli 6667905288fSFlorian Fainelli if (!ds->drv->get_eee) 6677905288fSFlorian Fainelli return -EOPNOTSUPP; 6687905288fSFlorian Fainelli 6697905288fSFlorian Fainelli ret = ds->drv->get_eee(ds, p->port, e); 6707905288fSFlorian Fainelli if (ret) 6717905288fSFlorian Fainelli return ret; 6727905288fSFlorian Fainelli 6737905288fSFlorian Fainelli if (p->phy) 6747905288fSFlorian Fainelli ret = phy_ethtool_get_eee(p->phy, e); 6757905288fSFlorian Fainelli 6767905288fSFlorian Fainelli return ret; 6777905288fSFlorian Fainelli } 6787905288fSFlorian Fainelli 67991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 68091da11f8SLennert Buytenhek .get_settings = dsa_slave_get_settings, 68191da11f8SLennert Buytenhek .set_settings = dsa_slave_set_settings, 68291da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 6833d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 6843d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 68591da11f8SLennert Buytenhek .nway_reset = dsa_slave_nway_reset, 68691da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 6876793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 6886793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 6896793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 69091da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 69191da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 69291da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 69319e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 69419e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 6957905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 6967905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 69791da11f8SLennert Buytenhek }; 69891da11f8SLennert Buytenhek 6993e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 700d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 701d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 7023e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 703d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 704d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 705d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 706339d8262SGuenter Roeck .ndo_fdb_add = dsa_slave_fdb_add, 707339d8262SGuenter Roeck .ndo_fdb_del = dsa_slave_fdb_del, 708339d8262SGuenter Roeck .ndo_fdb_dump = dsa_slave_fdb_dump, 709d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 710abd2be00SNicolas Dichtel .ndo_get_iflink = dsa_slave_get_iflink, 71198237d43SScott Feldman }; 71298237d43SScott Feldman 7139d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = { 714f8e20a9fSScott Feldman .switchdev_port_attr_get = dsa_slave_port_attr_get, 71535636062SScott Feldman .switchdev_port_attr_set = dsa_slave_port_attr_set, 716d442ad4aSStephen Hemminger }; 71791da11f8SLennert Buytenhek 7180d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 7190d8bcdd3SFlorian Fainelli { 7200d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 721ec9436baSFlorian Fainelli struct dsa_switch *ds = p->parent; 7220d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 7230d8bcdd3SFlorian Fainelli 7240d8bcdd3SFlorian Fainelli if (p->old_link != p->phy->link) { 7250d8bcdd3SFlorian Fainelli status_changed = 1; 7260d8bcdd3SFlorian Fainelli p->old_link = p->phy->link; 7270d8bcdd3SFlorian Fainelli } 7280d8bcdd3SFlorian Fainelli 7290d8bcdd3SFlorian Fainelli if (p->old_duplex != p->phy->duplex) { 7300d8bcdd3SFlorian Fainelli status_changed = 1; 7310d8bcdd3SFlorian Fainelli p->old_duplex = p->phy->duplex; 7320d8bcdd3SFlorian Fainelli } 7330d8bcdd3SFlorian Fainelli 7340d8bcdd3SFlorian Fainelli if (p->old_pause != p->phy->pause) { 7350d8bcdd3SFlorian Fainelli status_changed = 1; 7360d8bcdd3SFlorian Fainelli p->old_pause = p->phy->pause; 7370d8bcdd3SFlorian Fainelli } 7380d8bcdd3SFlorian Fainelli 739ec9436baSFlorian Fainelli if (ds->drv->adjust_link && status_changed) 740ec9436baSFlorian Fainelli ds->drv->adjust_link(ds, p->port, p->phy); 741ec9436baSFlorian Fainelli 7420d8bcdd3SFlorian Fainelli if (status_changed) 7430d8bcdd3SFlorian Fainelli phy_print_status(p->phy); 7440d8bcdd3SFlorian Fainelli } 7450d8bcdd3SFlorian Fainelli 746ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 747ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 748ce31b31cSFlorian Fainelli { 749ce31b31cSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 750ce31b31cSFlorian Fainelli struct dsa_switch *ds = p->parent; 751ce31b31cSFlorian Fainelli 752ce31b31cSFlorian Fainelli if (ds->drv->fixed_link_update) 753ce31b31cSFlorian Fainelli ds->drv->fixed_link_update(ds, p->port, status); 754ce31b31cSFlorian Fainelli 755ce31b31cSFlorian Fainelli return 0; 756ce31b31cSFlorian Fainelli } 757ce31b31cSFlorian Fainelli 75891da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 759c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p, 760cd28a1a9SFlorian Fainelli struct net_device *slave_dev, 761cd28a1a9SFlorian Fainelli int addr) 762c305c165SFlorian Fainelli { 763c305c165SFlorian Fainelli struct dsa_switch *ds = p->parent; 764c305c165SFlorian Fainelli 765cd28a1a9SFlorian Fainelli p->phy = ds->slave_mii_bus->phy_map[addr]; 766c305c165SFlorian Fainelli if (!p->phy) 767c305c165SFlorian Fainelli return -ENODEV; 768c305c165SFlorian Fainelli 769c305c165SFlorian Fainelli /* Use already configured phy mode */ 770c305c165SFlorian Fainelli p->phy_interface = p->phy->interface; 771c305c165SFlorian Fainelli phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 772c305c165SFlorian Fainelli p->phy_interface); 773c305c165SFlorian Fainelli 774c305c165SFlorian Fainelli return 0; 775c305c165SFlorian Fainelli } 776c305c165SFlorian Fainelli 7779697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 7780d8bcdd3SFlorian Fainelli struct net_device *slave_dev) 7790d8bcdd3SFlorian Fainelli { 7800d8bcdd3SFlorian Fainelli struct dsa_switch *ds = p->parent; 7810d8bcdd3SFlorian Fainelli struct dsa_chip_data *cd = ds->pd; 7820d8bcdd3SFlorian Fainelli struct device_node *phy_dn, *port_dn; 783ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 7846819563eSFlorian Fainelli u32 phy_flags = 0; 78519334920SGuenter Roeck int mode, ret; 7860d8bcdd3SFlorian Fainelli 7870d8bcdd3SFlorian Fainelli port_dn = cd->port_dn[p->port]; 78819334920SGuenter Roeck mode = of_get_phy_mode(port_dn); 78919334920SGuenter Roeck if (mode < 0) 79019334920SGuenter Roeck mode = PHY_INTERFACE_MODE_NA; 79119334920SGuenter Roeck p->phy_interface = mode; 7920d8bcdd3SFlorian Fainelli 7930d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 7940d8bcdd3SFlorian Fainelli if (of_phy_is_fixed_link(port_dn)) { 7950d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 7960d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 7970d8bcdd3SFlorian Fainelli */ 7980d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 7990d8bcdd3SFlorian Fainelli if (ret) { 800a2ae6007SJoe Perches netdev_err(slave_dev, "failed to register fixed PHY\n"); 8019697f1cdSFlorian Fainelli return ret; 8020d8bcdd3SFlorian Fainelli } 803ce31b31cSFlorian Fainelli phy_is_fixed = true; 8040d8bcdd3SFlorian Fainelli phy_dn = port_dn; 8050d8bcdd3SFlorian Fainelli } 8060d8bcdd3SFlorian Fainelli 8076819563eSFlorian Fainelli if (ds->drv->get_phy_flags) 8086819563eSFlorian Fainelli phy_flags = ds->drv->get_phy_flags(ds, p->port); 8096819563eSFlorian Fainelli 810cd28a1a9SFlorian Fainelli if (phy_dn) { 811cd28a1a9SFlorian Fainelli ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn); 812cd28a1a9SFlorian Fainelli /* If this PHY address is part of phys_mii_mask, which means 813cd28a1a9SFlorian Fainelli * that we need to divert reads and writes to/from it, then we 814cd28a1a9SFlorian Fainelli * want to bind this device using the slave MII bus created by 815cd28a1a9SFlorian Fainelli * DSA to make that happen. 816cd28a1a9SFlorian Fainelli */ 81796026d05SFlorian Fainelli if (!phy_is_fixed && ret >= 0 && 81896026d05SFlorian Fainelli (ds->phys_mii_mask & (1 << ret))) { 819cd28a1a9SFlorian Fainelli ret = dsa_slave_phy_connect(p, slave_dev, ret); 820cd28a1a9SFlorian Fainelli if (ret) 821cd28a1a9SFlorian Fainelli return ret; 822cd28a1a9SFlorian Fainelli } else { 8230d8bcdd3SFlorian Fainelli p->phy = of_phy_connect(slave_dev, phy_dn, 824cd28a1a9SFlorian Fainelli dsa_slave_adjust_link, 825cd28a1a9SFlorian Fainelli phy_flags, 8260d8bcdd3SFlorian Fainelli p->phy_interface); 827cd28a1a9SFlorian Fainelli } 828cd28a1a9SFlorian Fainelli } 8290d8bcdd3SFlorian Fainelli 830ce31b31cSFlorian Fainelli if (p->phy && phy_is_fixed) 831ce31b31cSFlorian Fainelli fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 832ce31b31cSFlorian Fainelli 8330d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 8340d8bcdd3SFlorian Fainelli * MDIO bus instead 8350d8bcdd3SFlorian Fainelli */ 836b31f65fbSAndrew Lunn if (!p->phy) { 837cd28a1a9SFlorian Fainelli ret = dsa_slave_phy_connect(p, slave_dev, p->port); 838c305c165SFlorian Fainelli if (ret) 839c305c165SFlorian Fainelli return ret; 840b31f65fbSAndrew Lunn } else { 841a2ae6007SJoe Perches netdev_info(slave_dev, "attached PHY at address %d [%s]\n", 8420d8bcdd3SFlorian Fainelli p->phy->addr, p->phy->drv->name); 8430d8bcdd3SFlorian Fainelli } 8449697f1cdSFlorian Fainelli 8459697f1cdSFlorian Fainelli return 0; 846b31f65fbSAndrew Lunn } 8470d8bcdd3SFlorian Fainelli 848448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key; 849448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev, 850448b4482SAndrew Lunn struct netdev_queue *txq, 851448b4482SAndrew Lunn void *_unused) 852448b4482SAndrew Lunn { 853448b4482SAndrew Lunn lockdep_set_class(&txq->_xmit_lock, 854448b4482SAndrew Lunn &dsa_slave_netdev_xmit_lock_key); 855448b4482SAndrew Lunn } 856448b4482SAndrew Lunn 85724462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 85824462549SFlorian Fainelli { 85924462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 86024462549SFlorian Fainelli 86124462549SFlorian Fainelli if (p->phy) { 86224462549SFlorian Fainelli phy_stop(p->phy); 86324462549SFlorian Fainelli p->old_pause = -1; 86424462549SFlorian Fainelli p->old_link = -1; 86524462549SFlorian Fainelli p->old_duplex = -1; 86624462549SFlorian Fainelli phy_suspend(p->phy); 86724462549SFlorian Fainelli } 86824462549SFlorian Fainelli 86924462549SFlorian Fainelli return 0; 87024462549SFlorian Fainelli } 87124462549SFlorian Fainelli 87224462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 87324462549SFlorian Fainelli { 87424462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 87524462549SFlorian Fainelli 87624462549SFlorian Fainelli netif_device_attach(slave_dev); 87724462549SFlorian Fainelli 87824462549SFlorian Fainelli if (p->phy) { 87924462549SFlorian Fainelli phy_resume(p->phy); 88024462549SFlorian Fainelli phy_start(p->phy); 88124462549SFlorian Fainelli } 88224462549SFlorian Fainelli 88324462549SFlorian Fainelli return 0; 88424462549SFlorian Fainelli } 88524462549SFlorian Fainelli 886d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent, 88791da11f8SLennert Buytenhek int port, char *name) 88891da11f8SLennert Buytenhek { 889e84665c9SLennert Buytenhek struct net_device *master = ds->dst->master_netdev; 89091da11f8SLennert Buytenhek struct net_device *slave_dev; 89191da11f8SLennert Buytenhek struct dsa_slave_priv *p; 89291da11f8SLennert Buytenhek int ret; 89391da11f8SLennert Buytenhek 894c835a677STom Gundersen slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 895c835a677STom Gundersen NET_NAME_UNKNOWN, ether_setup); 89691da11f8SLennert Buytenhek if (slave_dev == NULL) 897d87d6f44SGuenter Roeck return -ENOMEM; 89891da11f8SLennert Buytenhek 89991da11f8SLennert Buytenhek slave_dev->features = master->vlan_features; 9007ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 9012fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 90291da11f8SLennert Buytenhek slave_dev->tx_queue_len = 0; 9033e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 9049d47c0a2SJiri Pirko slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; 905d442ad4aSStephen Hemminger 906448b4482SAndrew Lunn netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, 907448b4482SAndrew Lunn NULL); 908448b4482SAndrew Lunn 90991da11f8SLennert Buytenhek SET_NETDEV_DEV(slave_dev, parent); 910bd47497aSFlorian Fainelli slave_dev->dev.of_node = ds->pd->port_dn[port]; 91191da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 91291da11f8SLennert Buytenhek 91391da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 91491da11f8SLennert Buytenhek p->dev = slave_dev; 91591da11f8SLennert Buytenhek p->parent = ds; 91691da11f8SLennert Buytenhek p->port = port; 9170d8bcdd3SFlorian Fainelli 9185075314eSAlexander Duyck switch (ds->dst->tag_protocol) { 9195075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA 9205075314eSAlexander Duyck case DSA_TAG_PROTO_DSA: 9215075314eSAlexander Duyck p->xmit = dsa_netdev_ops.xmit; 9225075314eSAlexander Duyck break; 9235075314eSAlexander Duyck #endif 9245075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA 9255075314eSAlexander Duyck case DSA_TAG_PROTO_EDSA: 9265075314eSAlexander Duyck p->xmit = edsa_netdev_ops.xmit; 9275075314eSAlexander Duyck break; 9285075314eSAlexander Duyck #endif 9295075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER 9305075314eSAlexander Duyck case DSA_TAG_PROTO_TRAILER: 9315075314eSAlexander Duyck p->xmit = trailer_netdev_ops.xmit; 9325075314eSAlexander Duyck break; 9335075314eSAlexander Duyck #endif 9345075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM 9355075314eSAlexander Duyck case DSA_TAG_PROTO_BRCM: 9365075314eSAlexander Duyck p->xmit = brcm_netdev_ops.xmit; 9375075314eSAlexander Duyck break; 9385075314eSAlexander Duyck #endif 9395075314eSAlexander Duyck default: 9405075314eSAlexander Duyck p->xmit = dsa_slave_notag_xmit; 9415075314eSAlexander Duyck break; 9425075314eSAlexander Duyck } 9435075314eSAlexander Duyck 9440d8bcdd3SFlorian Fainelli p->old_pause = -1; 9450d8bcdd3SFlorian Fainelli p->old_link = -1; 9460d8bcdd3SFlorian Fainelli p->old_duplex = -1; 9470d8bcdd3SFlorian Fainelli 9489697f1cdSFlorian Fainelli ret = dsa_slave_phy_setup(p, slave_dev); 9499697f1cdSFlorian Fainelli if (ret) { 9509697f1cdSFlorian Fainelli free_netdev(slave_dev); 951d87d6f44SGuenter Roeck return ret; 9529697f1cdSFlorian Fainelli } 95391da11f8SLennert Buytenhek 954d87d6f44SGuenter Roeck ds->ports[port] = slave_dev; 95591da11f8SLennert Buytenhek ret = register_netdev(slave_dev); 95691da11f8SLennert Buytenhek if (ret) { 957a2ae6007SJoe Perches netdev_err(master, "error %d registering interface %s\n", 958a2ae6007SJoe Perches ret, slave_dev->name); 9599697f1cdSFlorian Fainelli phy_disconnect(p->phy); 960d87d6f44SGuenter Roeck ds->ports[port] = NULL; 96191da11f8SLennert Buytenhek free_netdev(slave_dev); 962d87d6f44SGuenter Roeck return ret; 96391da11f8SLennert Buytenhek } 96491da11f8SLennert Buytenhek 96591da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 96691da11f8SLennert Buytenhek 967d87d6f44SGuenter Roeck return 0; 96891da11f8SLennert Buytenhek } 969b73adef6SFlorian Fainelli 970b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev) 971b73adef6SFlorian Fainelli { 972b73adef6SFlorian Fainelli return dev->netdev_ops == &dsa_slave_netdev_ops; 973b73adef6SFlorian Fainelli } 974b73adef6SFlorian Fainelli 975b73adef6SFlorian Fainelli static int dsa_slave_master_changed(struct net_device *dev) 976b73adef6SFlorian Fainelli { 977b73adef6SFlorian Fainelli struct net_device *master = netdev_master_upper_dev_get(dev); 978b06b107aSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 979b73adef6SFlorian Fainelli int err = 0; 980b73adef6SFlorian Fainelli 981b73adef6SFlorian Fainelli if (master && master->rtnl_link_ops && 982b73adef6SFlorian Fainelli !strcmp(master->rtnl_link_ops->kind, "bridge")) 983b73adef6SFlorian Fainelli err = dsa_slave_bridge_port_join(dev, master); 984b06b107aSGuenter Roeck else if (dsa_port_is_bridged(p)) 985b73adef6SFlorian Fainelli err = dsa_slave_bridge_port_leave(dev); 986b73adef6SFlorian Fainelli 987b73adef6SFlorian Fainelli return err; 988b73adef6SFlorian Fainelli } 989b73adef6SFlorian Fainelli 990b73adef6SFlorian Fainelli int dsa_slave_netdevice_event(struct notifier_block *unused, 991b73adef6SFlorian Fainelli unsigned long event, void *ptr) 992b73adef6SFlorian Fainelli { 993b73adef6SFlorian Fainelli struct net_device *dev; 994b73adef6SFlorian Fainelli int err = 0; 995b73adef6SFlorian Fainelli 996b73adef6SFlorian Fainelli switch (event) { 997b73adef6SFlorian Fainelli case NETDEV_CHANGEUPPER: 998b73adef6SFlorian Fainelli dev = netdev_notifier_info_to_dev(ptr); 999b73adef6SFlorian Fainelli if (!dsa_slave_dev_check(dev)) 1000b73adef6SFlorian Fainelli goto out; 1001b73adef6SFlorian Fainelli 1002b73adef6SFlorian Fainelli err = dsa_slave_master_changed(dev); 1003b73adef6SFlorian Fainelli if (err) 1004b73adef6SFlorian Fainelli netdev_warn(dev, "failed to reflect master change\n"); 1005b73adef6SFlorian Fainelli 1006b73adef6SFlorian Fainelli break; 1007b73adef6SFlorian Fainelli } 1008b73adef6SFlorian Fainelli 1009b73adef6SFlorian Fainelli out: 1010b73adef6SFlorian Fainelli return NOTIFY_DONE; 1011b73adef6SFlorian Fainelli } 1012