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> 1391da11f8SLennert Buytenhek #include <linux/phy.h> 14a2820543SFlorian Fainelli #include <linux/phy_fixed.h> 150d8bcdd3SFlorian Fainelli #include <linux/of_net.h> 160d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h> 1791da11f8SLennert Buytenhek #include "dsa_priv.h" 1891da11f8SLennert Buytenhek 1991da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/ 2091da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) 2191da11f8SLennert Buytenhek { 2291da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 2391da11f8SLennert Buytenhek 240d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 2591da11f8SLennert Buytenhek return ds->drv->phy_read(ds, addr, reg); 2691da11f8SLennert Buytenhek 2791da11f8SLennert Buytenhek return 0xffff; 2891da11f8SLennert Buytenhek } 2991da11f8SLennert Buytenhek 3091da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) 3191da11f8SLennert Buytenhek { 3291da11f8SLennert Buytenhek struct dsa_switch *ds = bus->priv; 3391da11f8SLennert Buytenhek 340d8bcdd3SFlorian Fainelli if (ds->phys_mii_mask & (1 << addr)) 3591da11f8SLennert Buytenhek return ds->drv->phy_write(ds, addr, reg, val); 3691da11f8SLennert Buytenhek 3791da11f8SLennert Buytenhek return 0; 3891da11f8SLennert Buytenhek } 3991da11f8SLennert Buytenhek 4091da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds) 4191da11f8SLennert Buytenhek { 4291da11f8SLennert Buytenhek ds->slave_mii_bus->priv = (void *)ds; 4391da11f8SLennert Buytenhek ds->slave_mii_bus->name = "dsa slave smi"; 4491da11f8SLennert Buytenhek ds->slave_mii_bus->read = dsa_slave_phy_read; 4591da11f8SLennert Buytenhek ds->slave_mii_bus->write = dsa_slave_phy_write; 46f490be04SFlorian Fainelli snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", 47f490be04SFlorian Fainelli ds->index, ds->pd->sw_addr); 48b4d2394dSAlexander Duyck ds->slave_mii_bus->parent = ds->master_dev; 49*24df8986SVivien Didelot ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 5091da11f8SLennert Buytenhek } 5191da11f8SLennert Buytenhek 5291da11f8SLennert Buytenhek 5391da11f8SLennert Buytenhek /* slave device handling ****************************************************/ 54c0840801SLennert Buytenhek static int dsa_slave_init(struct net_device *dev) 55c0840801SLennert Buytenhek { 56c0840801SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 57c0840801SLennert Buytenhek 58e84665c9SLennert Buytenhek dev->iflink = p->parent->dst->master_netdev->ifindex; 59c0840801SLennert Buytenhek 60c0840801SLennert Buytenhek return 0; 61c0840801SLennert Buytenhek } 62c0840801SLennert Buytenhek 6391da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev) 6491da11f8SLennert Buytenhek { 65df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 66e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 67b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 68df02c6ffSLennert Buytenhek int err; 69df02c6ffSLennert Buytenhek 70df02c6ffSLennert Buytenhek if (!(master->flags & IFF_UP)) 71df02c6ffSLennert Buytenhek return -ENETDOWN; 72df02c6ffSLennert Buytenhek 738feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { 74a748ee24SJiri Pirko err = dev_uc_add(master, dev->dev_addr); 75df02c6ffSLennert Buytenhek if (err < 0) 76df02c6ffSLennert Buytenhek goto out; 77df02c6ffSLennert Buytenhek } 78df02c6ffSLennert Buytenhek 79df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) { 80df02c6ffSLennert Buytenhek err = dev_set_allmulti(master, 1); 81df02c6ffSLennert Buytenhek if (err < 0) 82df02c6ffSLennert Buytenhek goto del_unicast; 83df02c6ffSLennert Buytenhek } 84df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) { 85df02c6ffSLennert Buytenhek err = dev_set_promiscuity(master, 1); 86df02c6ffSLennert Buytenhek if (err < 0) 87df02c6ffSLennert Buytenhek goto clear_allmulti; 88df02c6ffSLennert Buytenhek } 89df02c6ffSLennert Buytenhek 90b2f2af21SFlorian Fainelli if (ds->drv->port_enable) { 91b2f2af21SFlorian Fainelli err = ds->drv->port_enable(ds, p->port, p->phy); 92b2f2af21SFlorian Fainelli if (err) 93b2f2af21SFlorian Fainelli goto clear_promisc; 94b2f2af21SFlorian Fainelli } 95b2f2af21SFlorian Fainelli 96f7f1de51SFlorian Fainelli if (p->phy) 97f7f1de51SFlorian Fainelli phy_start(p->phy); 98f7f1de51SFlorian Fainelli 9991da11f8SLennert Buytenhek return 0; 100df02c6ffSLennert Buytenhek 101b2f2af21SFlorian Fainelli clear_promisc: 102b2f2af21SFlorian Fainelli if (dev->flags & IFF_PROMISC) 103b2f2af21SFlorian Fainelli dev_set_promiscuity(master, 0); 104df02c6ffSLennert Buytenhek clear_allmulti: 105df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 106df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 107df02c6ffSLennert Buytenhek del_unicast: 1088feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 109a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 110df02c6ffSLennert Buytenhek out: 111df02c6ffSLennert Buytenhek return err; 11291da11f8SLennert Buytenhek } 11391da11f8SLennert Buytenhek 11491da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev) 11591da11f8SLennert Buytenhek { 116df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 117e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 118b2f2af21SFlorian Fainelli struct dsa_switch *ds = p->parent; 119df02c6ffSLennert Buytenhek 120f7f1de51SFlorian Fainelli if (p->phy) 121f7f1de51SFlorian Fainelli phy_stop(p->phy); 122f7f1de51SFlorian Fainelli 123df02c6ffSLennert Buytenhek dev_mc_unsync(master, dev); 124a748ee24SJiri Pirko dev_uc_unsync(master, dev); 125df02c6ffSLennert Buytenhek if (dev->flags & IFF_ALLMULTI) 126df02c6ffSLennert Buytenhek dev_set_allmulti(master, -1); 127df02c6ffSLennert Buytenhek if (dev->flags & IFF_PROMISC) 128df02c6ffSLennert Buytenhek dev_set_promiscuity(master, -1); 129df02c6ffSLennert Buytenhek 1308feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 131a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 132df02c6ffSLennert Buytenhek 133b2f2af21SFlorian Fainelli if (ds->drv->port_disable) 134b2f2af21SFlorian Fainelli ds->drv->port_disable(ds, p->port, p->phy); 135b2f2af21SFlorian Fainelli 13691da11f8SLennert Buytenhek return 0; 13791da11f8SLennert Buytenhek } 13891da11f8SLennert Buytenhek 13991da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change) 14091da11f8SLennert Buytenhek { 14191da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 142e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 14391da11f8SLennert Buytenhek 14491da11f8SLennert Buytenhek if (change & IFF_ALLMULTI) 14591da11f8SLennert Buytenhek dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); 14691da11f8SLennert Buytenhek if (change & IFF_PROMISC) 14791da11f8SLennert Buytenhek dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); 14891da11f8SLennert Buytenhek } 14991da11f8SLennert Buytenhek 15091da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev) 15191da11f8SLennert Buytenhek { 15291da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 153e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 15491da11f8SLennert Buytenhek 15591da11f8SLennert Buytenhek dev_mc_sync(master, dev); 156a748ee24SJiri Pirko dev_uc_sync(master, dev); 15791da11f8SLennert Buytenhek } 15891da11f8SLennert Buytenhek 159df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a) 16091da11f8SLennert Buytenhek { 161df02c6ffSLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 162e84665c9SLennert Buytenhek struct net_device *master = p->parent->dst->master_netdev; 163df02c6ffSLennert Buytenhek struct sockaddr *addr = a; 164df02c6ffSLennert Buytenhek int err; 165df02c6ffSLennert Buytenhek 166df02c6ffSLennert Buytenhek if (!is_valid_ether_addr(addr->sa_data)) 167df02c6ffSLennert Buytenhek return -EADDRNOTAVAIL; 168df02c6ffSLennert Buytenhek 169df02c6ffSLennert Buytenhek if (!(dev->flags & IFF_UP)) 170df02c6ffSLennert Buytenhek goto out; 171df02c6ffSLennert Buytenhek 1728feedbb4SJoe Perches if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { 173a748ee24SJiri Pirko err = dev_uc_add(master, addr->sa_data); 174df02c6ffSLennert Buytenhek if (err < 0) 175df02c6ffSLennert Buytenhek return err; 176df02c6ffSLennert Buytenhek } 177df02c6ffSLennert Buytenhek 1788feedbb4SJoe Perches if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) 179a748ee24SJiri Pirko dev_uc_del(master, dev->dev_addr); 180df02c6ffSLennert Buytenhek 181df02c6ffSLennert Buytenhek out: 182d08f161aSJoe Perches ether_addr_copy(dev->dev_addr, addr->sa_data); 18391da11f8SLennert Buytenhek 18491da11f8SLennert Buytenhek return 0; 18591da11f8SLennert Buytenhek } 18691da11f8SLennert Buytenhek 18791da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 18891da11f8SLennert Buytenhek { 18991da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 19091da11f8SLennert Buytenhek 19191da11f8SLennert Buytenhek if (p->phy != NULL) 19228b04113SRichard Cochran return phy_mii_ioctl(p->phy, ifr, cmd); 19391da11f8SLennert Buytenhek 19491da11f8SLennert Buytenhek return -EOPNOTSUPP; 19591da11f8SLennert Buytenhek } 19691da11f8SLennert Buytenhek 1973e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 1983e8a72d1SFlorian Fainelli { 1993e8a72d1SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 2003e8a72d1SFlorian Fainelli 2015075314eSAlexander Duyck return p->xmit(skb, dev); 2023e8a72d1SFlorian Fainelli } 2033e8a72d1SFlorian Fainelli 2045aed85ceSFlorian Fainelli static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb, 2055aed85ceSFlorian Fainelli struct net_device *dev) 2065aed85ceSFlorian Fainelli { 2075aed85ceSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 2085aed85ceSFlorian Fainelli 2095aed85ceSFlorian Fainelli skb->dev = p->parent->dst->master_netdev; 2105aed85ceSFlorian Fainelli dev_queue_xmit(skb); 2115aed85ceSFlorian Fainelli 2125aed85ceSFlorian Fainelli return NETDEV_TX_OK; 2135aed85ceSFlorian Fainelli } 2145aed85ceSFlorian Fainelli 21591da11f8SLennert Buytenhek 21691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/ 21791da11f8SLennert Buytenhek static int 21891da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 21991da11f8SLennert Buytenhek { 22091da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 22191da11f8SLennert Buytenhek int err; 22291da11f8SLennert Buytenhek 22391da11f8SLennert Buytenhek err = -EOPNOTSUPP; 22491da11f8SLennert Buytenhek if (p->phy != NULL) { 22591da11f8SLennert Buytenhek err = phy_read_status(p->phy); 22691da11f8SLennert Buytenhek if (err == 0) 22791da11f8SLennert Buytenhek err = phy_ethtool_gset(p->phy, cmd); 22891da11f8SLennert Buytenhek } 22991da11f8SLennert Buytenhek 23091da11f8SLennert Buytenhek return err; 23191da11f8SLennert Buytenhek } 23291da11f8SLennert Buytenhek 23391da11f8SLennert Buytenhek static int 23491da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 23591da11f8SLennert Buytenhek { 23691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 23791da11f8SLennert Buytenhek 23891da11f8SLennert Buytenhek if (p->phy != NULL) 23991da11f8SLennert Buytenhek return phy_ethtool_sset(p->phy, cmd); 24091da11f8SLennert Buytenhek 24191da11f8SLennert Buytenhek return -EOPNOTSUPP; 24291da11f8SLennert Buytenhek } 24391da11f8SLennert Buytenhek 24491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev, 24591da11f8SLennert Buytenhek struct ethtool_drvinfo *drvinfo) 24691da11f8SLennert Buytenhek { 2477826d43fSJiri Pirko strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); 2487826d43fSJiri Pirko strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); 2497826d43fSJiri Pirko strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 2507826d43fSJiri Pirko strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 25191da11f8SLennert Buytenhek } 25291da11f8SLennert Buytenhek 2533d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev) 2543d762a0fSGuenter Roeck { 2553d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 2563d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 2573d762a0fSGuenter Roeck 2583d762a0fSGuenter Roeck if (ds->drv->get_regs_len) 2593d762a0fSGuenter Roeck return ds->drv->get_regs_len(ds, p->port); 2603d762a0fSGuenter Roeck 2613d762a0fSGuenter Roeck return -EOPNOTSUPP; 2623d762a0fSGuenter Roeck } 2633d762a0fSGuenter Roeck 2643d762a0fSGuenter Roeck static void 2653d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) 2663d762a0fSGuenter Roeck { 2673d762a0fSGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 2683d762a0fSGuenter Roeck struct dsa_switch *ds = p->parent; 2693d762a0fSGuenter Roeck 2703d762a0fSGuenter Roeck if (ds->drv->get_regs) 2713d762a0fSGuenter Roeck ds->drv->get_regs(ds, p->port, regs, _p); 2723d762a0fSGuenter Roeck } 2733d762a0fSGuenter Roeck 27491da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev) 27591da11f8SLennert Buytenhek { 27691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 27791da11f8SLennert Buytenhek 27891da11f8SLennert Buytenhek if (p->phy != NULL) 27991da11f8SLennert Buytenhek return genphy_restart_aneg(p->phy); 28091da11f8SLennert Buytenhek 28191da11f8SLennert Buytenhek return -EOPNOTSUPP; 28291da11f8SLennert Buytenhek } 28391da11f8SLennert Buytenhek 28491da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev) 28591da11f8SLennert Buytenhek { 28691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 28791da11f8SLennert Buytenhek 28891da11f8SLennert Buytenhek if (p->phy != NULL) { 28991da11f8SLennert Buytenhek genphy_update_link(p->phy); 29091da11f8SLennert Buytenhek return p->phy->link; 29191da11f8SLennert Buytenhek } 29291da11f8SLennert Buytenhek 29391da11f8SLennert Buytenhek return -EOPNOTSUPP; 29491da11f8SLennert Buytenhek } 29591da11f8SLennert Buytenhek 2966793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev) 2976793abb4SGuenter Roeck { 2986793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 2996793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 3006793abb4SGuenter Roeck 3016793abb4SGuenter Roeck if (ds->pd->eeprom_len) 3026793abb4SGuenter Roeck return ds->pd->eeprom_len; 3036793abb4SGuenter Roeck 3046793abb4SGuenter Roeck if (ds->drv->get_eeprom_len) 3056793abb4SGuenter Roeck return ds->drv->get_eeprom_len(ds); 3066793abb4SGuenter Roeck 3076793abb4SGuenter Roeck return 0; 3086793abb4SGuenter Roeck } 3096793abb4SGuenter Roeck 3106793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev, 3116793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 3126793abb4SGuenter Roeck { 3136793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 3146793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 3156793abb4SGuenter Roeck 3166793abb4SGuenter Roeck if (ds->drv->get_eeprom) 3176793abb4SGuenter Roeck return ds->drv->get_eeprom(ds, eeprom, data); 3186793abb4SGuenter Roeck 3196793abb4SGuenter Roeck return -EOPNOTSUPP; 3206793abb4SGuenter Roeck } 3216793abb4SGuenter Roeck 3226793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev, 3236793abb4SGuenter Roeck struct ethtool_eeprom *eeprom, u8 *data) 3246793abb4SGuenter Roeck { 3256793abb4SGuenter Roeck struct dsa_slave_priv *p = netdev_priv(dev); 3266793abb4SGuenter Roeck struct dsa_switch *ds = p->parent; 3276793abb4SGuenter Roeck 3286793abb4SGuenter Roeck if (ds->drv->set_eeprom) 3296793abb4SGuenter Roeck return ds->drv->set_eeprom(ds, eeprom, data); 3306793abb4SGuenter Roeck 3316793abb4SGuenter Roeck return -EOPNOTSUPP; 3326793abb4SGuenter Roeck } 3336793abb4SGuenter Roeck 33491da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev, 33591da11f8SLennert Buytenhek uint32_t stringset, uint8_t *data) 33691da11f8SLennert Buytenhek { 33791da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 33891da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 33991da11f8SLennert Buytenhek 34091da11f8SLennert Buytenhek if (stringset == ETH_SS_STATS) { 34191da11f8SLennert Buytenhek int len = ETH_GSTRING_LEN; 34291da11f8SLennert Buytenhek 34391da11f8SLennert Buytenhek strncpy(data, "tx_packets", len); 34491da11f8SLennert Buytenhek strncpy(data + len, "tx_bytes", len); 34591da11f8SLennert Buytenhek strncpy(data + 2 * len, "rx_packets", len); 34691da11f8SLennert Buytenhek strncpy(data + 3 * len, "rx_bytes", len); 34791da11f8SLennert Buytenhek if (ds->drv->get_strings != NULL) 34891da11f8SLennert Buytenhek ds->drv->get_strings(ds, p->port, data + 4 * len); 34991da11f8SLennert Buytenhek } 35091da11f8SLennert Buytenhek } 35191da11f8SLennert Buytenhek 35291da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev, 35391da11f8SLennert Buytenhek struct ethtool_stats *stats, 35491da11f8SLennert Buytenhek uint64_t *data) 35591da11f8SLennert Buytenhek { 35691da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 35791da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 35891da11f8SLennert Buytenhek 35991da11f8SLennert Buytenhek data[0] = p->dev->stats.tx_packets; 36091da11f8SLennert Buytenhek data[1] = p->dev->stats.tx_bytes; 36191da11f8SLennert Buytenhek data[2] = p->dev->stats.rx_packets; 36291da11f8SLennert Buytenhek data[3] = p->dev->stats.rx_bytes; 36391da11f8SLennert Buytenhek if (ds->drv->get_ethtool_stats != NULL) 36491da11f8SLennert Buytenhek ds->drv->get_ethtool_stats(ds, p->port, data + 4); 36591da11f8SLennert Buytenhek } 36691da11f8SLennert Buytenhek 36791da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset) 36891da11f8SLennert Buytenhek { 36991da11f8SLennert Buytenhek struct dsa_slave_priv *p = netdev_priv(dev); 37091da11f8SLennert Buytenhek struct dsa_switch *ds = p->parent; 37191da11f8SLennert Buytenhek 37291da11f8SLennert Buytenhek if (sset == ETH_SS_STATS) { 37391da11f8SLennert Buytenhek int count; 37491da11f8SLennert Buytenhek 37591da11f8SLennert Buytenhek count = 4; 37691da11f8SLennert Buytenhek if (ds->drv->get_sset_count != NULL) 37791da11f8SLennert Buytenhek count += ds->drv->get_sset_count(ds); 37891da11f8SLennert Buytenhek 37991da11f8SLennert Buytenhek return count; 38091da11f8SLennert Buytenhek } 38191da11f8SLennert Buytenhek 38291da11f8SLennert Buytenhek return -EOPNOTSUPP; 38391da11f8SLennert Buytenhek } 38491da11f8SLennert Buytenhek 38519e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 38619e57c4eSFlorian Fainelli { 38719e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 38819e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 38919e57c4eSFlorian Fainelli 39019e57c4eSFlorian Fainelli if (ds->drv->get_wol) 39119e57c4eSFlorian Fainelli ds->drv->get_wol(ds, p->port, w); 39219e57c4eSFlorian Fainelli } 39319e57c4eSFlorian Fainelli 39419e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 39519e57c4eSFlorian Fainelli { 39619e57c4eSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 39719e57c4eSFlorian Fainelli struct dsa_switch *ds = p->parent; 39819e57c4eSFlorian Fainelli int ret = -EOPNOTSUPP; 39919e57c4eSFlorian Fainelli 40019e57c4eSFlorian Fainelli if (ds->drv->set_wol) 40119e57c4eSFlorian Fainelli ret = ds->drv->set_wol(ds, p->port, w); 40219e57c4eSFlorian Fainelli 40319e57c4eSFlorian Fainelli return ret; 40419e57c4eSFlorian Fainelli } 40519e57c4eSFlorian Fainelli 4067905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) 4077905288fSFlorian Fainelli { 4087905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 4097905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 4107905288fSFlorian Fainelli int ret; 4117905288fSFlorian Fainelli 4127905288fSFlorian Fainelli if (!ds->drv->set_eee) 4137905288fSFlorian Fainelli return -EOPNOTSUPP; 4147905288fSFlorian Fainelli 4157905288fSFlorian Fainelli ret = ds->drv->set_eee(ds, p->port, p->phy, e); 4167905288fSFlorian Fainelli if (ret) 4177905288fSFlorian Fainelli return ret; 4187905288fSFlorian Fainelli 4197905288fSFlorian Fainelli if (p->phy) 4207905288fSFlorian Fainelli ret = phy_ethtool_set_eee(p->phy, e); 4217905288fSFlorian Fainelli 4227905288fSFlorian Fainelli return ret; 4237905288fSFlorian Fainelli } 4247905288fSFlorian Fainelli 4257905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 4267905288fSFlorian Fainelli { 4277905288fSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 4287905288fSFlorian Fainelli struct dsa_switch *ds = p->parent; 4297905288fSFlorian Fainelli int ret; 4307905288fSFlorian Fainelli 4317905288fSFlorian Fainelli if (!ds->drv->get_eee) 4327905288fSFlorian Fainelli return -EOPNOTSUPP; 4337905288fSFlorian Fainelli 4347905288fSFlorian Fainelli ret = ds->drv->get_eee(ds, p->port, e); 4357905288fSFlorian Fainelli if (ret) 4367905288fSFlorian Fainelli return ret; 4377905288fSFlorian Fainelli 4387905288fSFlorian Fainelli if (p->phy) 4397905288fSFlorian Fainelli ret = phy_ethtool_get_eee(p->phy, e); 4407905288fSFlorian Fainelli 4417905288fSFlorian Fainelli return ret; 4427905288fSFlorian Fainelli } 4437905288fSFlorian Fainelli 44491da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = { 44591da11f8SLennert Buytenhek .get_settings = dsa_slave_get_settings, 44691da11f8SLennert Buytenhek .set_settings = dsa_slave_set_settings, 44791da11f8SLennert Buytenhek .get_drvinfo = dsa_slave_get_drvinfo, 4483d762a0fSGuenter Roeck .get_regs_len = dsa_slave_get_regs_len, 4493d762a0fSGuenter Roeck .get_regs = dsa_slave_get_regs, 45091da11f8SLennert Buytenhek .nway_reset = dsa_slave_nway_reset, 45191da11f8SLennert Buytenhek .get_link = dsa_slave_get_link, 4526793abb4SGuenter Roeck .get_eeprom_len = dsa_slave_get_eeprom_len, 4536793abb4SGuenter Roeck .get_eeprom = dsa_slave_get_eeprom, 4546793abb4SGuenter Roeck .set_eeprom = dsa_slave_set_eeprom, 45591da11f8SLennert Buytenhek .get_strings = dsa_slave_get_strings, 45691da11f8SLennert Buytenhek .get_ethtool_stats = dsa_slave_get_ethtool_stats, 45791da11f8SLennert Buytenhek .get_sset_count = dsa_slave_get_sset_count, 45819e57c4eSFlorian Fainelli .set_wol = dsa_slave_set_wol, 45919e57c4eSFlorian Fainelli .get_wol = dsa_slave_get_wol, 4607905288fSFlorian Fainelli .set_eee = dsa_slave_set_eee, 4617905288fSFlorian Fainelli .get_eee = dsa_slave_get_eee, 46291da11f8SLennert Buytenhek }; 46391da11f8SLennert Buytenhek 4643e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = { 465c0840801SLennert Buytenhek .ndo_init = dsa_slave_init, 466d442ad4aSStephen Hemminger .ndo_open = dsa_slave_open, 467d442ad4aSStephen Hemminger .ndo_stop = dsa_slave_close, 4683e8a72d1SFlorian Fainelli .ndo_start_xmit = dsa_slave_xmit, 469d442ad4aSStephen Hemminger .ndo_change_rx_flags = dsa_slave_change_rx_flags, 470d442ad4aSStephen Hemminger .ndo_set_rx_mode = dsa_slave_set_rx_mode, 471d442ad4aSStephen Hemminger .ndo_set_mac_address = dsa_slave_set_mac_address, 472d442ad4aSStephen Hemminger .ndo_do_ioctl = dsa_slave_ioctl, 473d442ad4aSStephen Hemminger }; 47491da11f8SLennert Buytenhek 4750d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev) 4760d8bcdd3SFlorian Fainelli { 4770d8bcdd3SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 478ec9436baSFlorian Fainelli struct dsa_switch *ds = p->parent; 4790d8bcdd3SFlorian Fainelli unsigned int status_changed = 0; 4800d8bcdd3SFlorian Fainelli 4810d8bcdd3SFlorian Fainelli if (p->old_link != p->phy->link) { 4820d8bcdd3SFlorian Fainelli status_changed = 1; 4830d8bcdd3SFlorian Fainelli p->old_link = p->phy->link; 4840d8bcdd3SFlorian Fainelli } 4850d8bcdd3SFlorian Fainelli 4860d8bcdd3SFlorian Fainelli if (p->old_duplex != p->phy->duplex) { 4870d8bcdd3SFlorian Fainelli status_changed = 1; 4880d8bcdd3SFlorian Fainelli p->old_duplex = p->phy->duplex; 4890d8bcdd3SFlorian Fainelli } 4900d8bcdd3SFlorian Fainelli 4910d8bcdd3SFlorian Fainelli if (p->old_pause != p->phy->pause) { 4920d8bcdd3SFlorian Fainelli status_changed = 1; 4930d8bcdd3SFlorian Fainelli p->old_pause = p->phy->pause; 4940d8bcdd3SFlorian Fainelli } 4950d8bcdd3SFlorian Fainelli 496ec9436baSFlorian Fainelli if (ds->drv->adjust_link && status_changed) 497ec9436baSFlorian Fainelli ds->drv->adjust_link(ds, p->port, p->phy); 498ec9436baSFlorian Fainelli 4990d8bcdd3SFlorian Fainelli if (status_changed) 5000d8bcdd3SFlorian Fainelli phy_print_status(p->phy); 5010d8bcdd3SFlorian Fainelli } 5020d8bcdd3SFlorian Fainelli 503ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev, 504ce31b31cSFlorian Fainelli struct fixed_phy_status *status) 505ce31b31cSFlorian Fainelli { 506ce31b31cSFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(dev); 507ce31b31cSFlorian Fainelli struct dsa_switch *ds = p->parent; 508ce31b31cSFlorian Fainelli 509ce31b31cSFlorian Fainelli if (ds->drv->fixed_link_update) 510ce31b31cSFlorian Fainelli ds->drv->fixed_link_update(ds, p->port, status); 511ce31b31cSFlorian Fainelli 512ce31b31cSFlorian Fainelli return 0; 513ce31b31cSFlorian Fainelli } 514ce31b31cSFlorian Fainelli 51591da11f8SLennert Buytenhek /* slave device setup *******************************************************/ 5169697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 5170d8bcdd3SFlorian Fainelli struct net_device *slave_dev) 5180d8bcdd3SFlorian Fainelli { 5190d8bcdd3SFlorian Fainelli struct dsa_switch *ds = p->parent; 5200d8bcdd3SFlorian Fainelli struct dsa_chip_data *cd = ds->pd; 5210d8bcdd3SFlorian Fainelli struct device_node *phy_dn, *port_dn; 522ce31b31cSFlorian Fainelli bool phy_is_fixed = false; 5236819563eSFlorian Fainelli u32 phy_flags = 0; 5240d8bcdd3SFlorian Fainelli int ret; 5250d8bcdd3SFlorian Fainelli 5260d8bcdd3SFlorian Fainelli port_dn = cd->port_dn[p->port]; 5270d8bcdd3SFlorian Fainelli p->phy_interface = of_get_phy_mode(port_dn); 5280d8bcdd3SFlorian Fainelli 5290d8bcdd3SFlorian Fainelli phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 5300d8bcdd3SFlorian Fainelli if (of_phy_is_fixed_link(port_dn)) { 5310d8bcdd3SFlorian Fainelli /* In the case of a fixed PHY, the DT node associated 5320d8bcdd3SFlorian Fainelli * to the fixed PHY is the Port DT node 5330d8bcdd3SFlorian Fainelli */ 5340d8bcdd3SFlorian Fainelli ret = of_phy_register_fixed_link(port_dn); 5350d8bcdd3SFlorian Fainelli if (ret) { 536a2ae6007SJoe Perches netdev_err(slave_dev, "failed to register fixed PHY\n"); 5379697f1cdSFlorian Fainelli return ret; 5380d8bcdd3SFlorian Fainelli } 539ce31b31cSFlorian Fainelli phy_is_fixed = true; 5400d8bcdd3SFlorian Fainelli phy_dn = port_dn; 5410d8bcdd3SFlorian Fainelli } 5420d8bcdd3SFlorian Fainelli 5436819563eSFlorian Fainelli if (ds->drv->get_phy_flags) 5446819563eSFlorian Fainelli phy_flags = ds->drv->get_phy_flags(ds, p->port); 5456819563eSFlorian Fainelli 5460d8bcdd3SFlorian Fainelli if (phy_dn) 5470d8bcdd3SFlorian Fainelli p->phy = of_phy_connect(slave_dev, phy_dn, 5486819563eSFlorian Fainelli dsa_slave_adjust_link, phy_flags, 5490d8bcdd3SFlorian Fainelli p->phy_interface); 5500d8bcdd3SFlorian Fainelli 551ce31b31cSFlorian Fainelli if (p->phy && phy_is_fixed) 552ce31b31cSFlorian Fainelli fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 553ce31b31cSFlorian Fainelli 5540d8bcdd3SFlorian Fainelli /* We could not connect to a designated PHY, so use the switch internal 5550d8bcdd3SFlorian Fainelli * MDIO bus instead 5560d8bcdd3SFlorian Fainelli */ 557b31f65fbSAndrew Lunn if (!p->phy) { 5580d8bcdd3SFlorian Fainelli p->phy = ds->slave_mii_bus->phy_map[p->port]; 55953013c77SFlorian Fainelli if (!p->phy) 5609697f1cdSFlorian Fainelli return -ENODEV; 56153013c77SFlorian Fainelli 562b31f65fbSAndrew Lunn phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, 563b31f65fbSAndrew Lunn p->phy_interface); 564b31f65fbSAndrew Lunn } else { 565a2ae6007SJoe Perches netdev_info(slave_dev, "attached PHY at address %d [%s]\n", 5660d8bcdd3SFlorian Fainelli p->phy->addr, p->phy->drv->name); 5670d8bcdd3SFlorian Fainelli } 5689697f1cdSFlorian Fainelli 5699697f1cdSFlorian Fainelli return 0; 570b31f65fbSAndrew Lunn } 5710d8bcdd3SFlorian Fainelli 57224462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev) 57324462549SFlorian Fainelli { 57424462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 57524462549SFlorian Fainelli 57624462549SFlorian Fainelli netif_device_detach(slave_dev); 57724462549SFlorian Fainelli 57824462549SFlorian Fainelli if (p->phy) { 57924462549SFlorian Fainelli phy_stop(p->phy); 58024462549SFlorian Fainelli p->old_pause = -1; 58124462549SFlorian Fainelli p->old_link = -1; 58224462549SFlorian Fainelli p->old_duplex = -1; 58324462549SFlorian Fainelli phy_suspend(p->phy); 58424462549SFlorian Fainelli } 58524462549SFlorian Fainelli 58624462549SFlorian Fainelli return 0; 58724462549SFlorian Fainelli } 58824462549SFlorian Fainelli 58924462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev) 59024462549SFlorian Fainelli { 59124462549SFlorian Fainelli struct dsa_slave_priv *p = netdev_priv(slave_dev); 59224462549SFlorian Fainelli 59324462549SFlorian Fainelli netif_device_attach(slave_dev); 59424462549SFlorian Fainelli 59524462549SFlorian Fainelli if (p->phy) { 59624462549SFlorian Fainelli phy_resume(p->phy); 59724462549SFlorian Fainelli phy_start(p->phy); 59824462549SFlorian Fainelli } 59924462549SFlorian Fainelli 60024462549SFlorian Fainelli return 0; 60124462549SFlorian Fainelli } 60224462549SFlorian Fainelli 60391da11f8SLennert Buytenhek struct net_device * 60491da11f8SLennert Buytenhek dsa_slave_create(struct dsa_switch *ds, struct device *parent, 60591da11f8SLennert Buytenhek int port, char *name) 60691da11f8SLennert Buytenhek { 607e84665c9SLennert Buytenhek struct net_device *master = ds->dst->master_netdev; 60891da11f8SLennert Buytenhek struct net_device *slave_dev; 60991da11f8SLennert Buytenhek struct dsa_slave_priv *p; 61091da11f8SLennert Buytenhek int ret; 61191da11f8SLennert Buytenhek 612c835a677STom Gundersen slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 613c835a677STom Gundersen NET_NAME_UNKNOWN, ether_setup); 61491da11f8SLennert Buytenhek if (slave_dev == NULL) 61591da11f8SLennert Buytenhek return slave_dev; 61691da11f8SLennert Buytenhek 61791da11f8SLennert Buytenhek slave_dev->features = master->vlan_features; 6187ad24ea4SWilfried Klaebe slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 6192fcc8005SBjørn Mork eth_hw_addr_inherit(slave_dev, master); 62091da11f8SLennert Buytenhek slave_dev->tx_queue_len = 0; 6213e8a72d1SFlorian Fainelli slave_dev->netdev_ops = &dsa_slave_netdev_ops; 622d442ad4aSStephen Hemminger 62391da11f8SLennert Buytenhek SET_NETDEV_DEV(slave_dev, parent); 624bd47497aSFlorian Fainelli slave_dev->dev.of_node = ds->pd->port_dn[port]; 62591da11f8SLennert Buytenhek slave_dev->vlan_features = master->vlan_features; 62691da11f8SLennert Buytenhek 62791da11f8SLennert Buytenhek p = netdev_priv(slave_dev); 62891da11f8SLennert Buytenhek p->dev = slave_dev; 62991da11f8SLennert Buytenhek p->parent = ds; 63091da11f8SLennert Buytenhek p->port = port; 6310d8bcdd3SFlorian Fainelli 6325075314eSAlexander Duyck switch (ds->dst->tag_protocol) { 6335075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA 6345075314eSAlexander Duyck case DSA_TAG_PROTO_DSA: 6355075314eSAlexander Duyck p->xmit = dsa_netdev_ops.xmit; 6365075314eSAlexander Duyck break; 6375075314eSAlexander Duyck #endif 6385075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA 6395075314eSAlexander Duyck case DSA_TAG_PROTO_EDSA: 6405075314eSAlexander Duyck p->xmit = edsa_netdev_ops.xmit; 6415075314eSAlexander Duyck break; 6425075314eSAlexander Duyck #endif 6435075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER 6445075314eSAlexander Duyck case DSA_TAG_PROTO_TRAILER: 6455075314eSAlexander Duyck p->xmit = trailer_netdev_ops.xmit; 6465075314eSAlexander Duyck break; 6475075314eSAlexander Duyck #endif 6485075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM 6495075314eSAlexander Duyck case DSA_TAG_PROTO_BRCM: 6505075314eSAlexander Duyck p->xmit = brcm_netdev_ops.xmit; 6515075314eSAlexander Duyck break; 6525075314eSAlexander Duyck #endif 6535075314eSAlexander Duyck default: 6545075314eSAlexander Duyck p->xmit = dsa_slave_notag_xmit; 6555075314eSAlexander Duyck break; 6565075314eSAlexander Duyck } 6575075314eSAlexander Duyck 6580d8bcdd3SFlorian Fainelli p->old_pause = -1; 6590d8bcdd3SFlorian Fainelli p->old_link = -1; 6600d8bcdd3SFlorian Fainelli p->old_duplex = -1; 6610d8bcdd3SFlorian Fainelli 6629697f1cdSFlorian Fainelli ret = dsa_slave_phy_setup(p, slave_dev); 6639697f1cdSFlorian Fainelli if (ret) { 6649697f1cdSFlorian Fainelli free_netdev(slave_dev); 6659697f1cdSFlorian Fainelli return NULL; 6669697f1cdSFlorian Fainelli } 66791da11f8SLennert Buytenhek 66891da11f8SLennert Buytenhek ret = register_netdev(slave_dev); 66991da11f8SLennert Buytenhek if (ret) { 670a2ae6007SJoe Perches netdev_err(master, "error %d registering interface %s\n", 671a2ae6007SJoe Perches ret, slave_dev->name); 6729697f1cdSFlorian Fainelli phy_disconnect(p->phy); 67391da11f8SLennert Buytenhek free_netdev(slave_dev); 67491da11f8SLennert Buytenhek return NULL; 67591da11f8SLennert Buytenhek } 67691da11f8SLennert Buytenhek 67791da11f8SLennert Buytenhek netif_carrier_off(slave_dev); 67891da11f8SLennert Buytenhek 67991da11f8SLennert Buytenhek if (p->phy != NULL) { 680228b16cbSAndrew Lunn if (ds->drv->get_phy_flags) 6816819563eSFlorian Fainelli p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port); 6826819563eSFlorian Fainelli 683fb28ad35SKay Sievers phy_attach(slave_dev, dev_name(&p->phy->dev), 684f9a8f83bSFlorian Fainelli PHY_INTERFACE_MODE_GMII); 68591da11f8SLennert Buytenhek 68691da11f8SLennert Buytenhek p->phy->autoneg = AUTONEG_ENABLE; 68791da11f8SLennert Buytenhek p->phy->speed = 0; 68891da11f8SLennert Buytenhek p->phy->duplex = 0; 68991da11f8SLennert Buytenhek p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg; 69091da11f8SLennert Buytenhek } 69191da11f8SLennert Buytenhek 69291da11f8SLennert Buytenhek return slave_dev; 69391da11f8SLennert Buytenhek } 694