Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/slave.c - Slave device handling |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/list.h> |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 12 | #include <linux/etherdevice.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 13 | #include <linux/phy.h> |
Florian Fainelli | a282054 | 2014-10-17 16:02:13 -0700 | [diff] [blame] | 14 | #include <linux/phy_fixed.h> |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 15 | #include <linux/of_net.h> |
| 16 | #include <linux/of_mdio.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 17 | #include "dsa_priv.h" |
| 18 | |
| 19 | /* slave mii_bus handling ***************************************************/ |
| 20 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) |
| 21 | { |
| 22 | struct dsa_switch *ds = bus->priv; |
| 23 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 24 | if (ds->phys_mii_mask & (1 << addr)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 25 | return ds->drv->phy_read(ds, addr, reg); |
| 26 | |
| 27 | return 0xffff; |
| 28 | } |
| 29 | |
| 30 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) |
| 31 | { |
| 32 | struct dsa_switch *ds = bus->priv; |
| 33 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 34 | if (ds->phys_mii_mask & (1 << addr)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 35 | return ds->drv->phy_write(ds, addr, reg, val); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) |
| 41 | { |
| 42 | ds->slave_mii_bus->priv = (void *)ds; |
| 43 | ds->slave_mii_bus->name = "dsa slave smi"; |
| 44 | ds->slave_mii_bus->read = dsa_slave_phy_read; |
| 45 | ds->slave_mii_bus->write = dsa_slave_phy_write; |
Florian Fainelli | f490be0 | 2013-01-21 09:58:50 +0000 | [diff] [blame] | 46 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", |
| 47 | ds->index, ds->pd->sw_addr); |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 48 | ds->slave_mii_bus->parent = ds->master_dev; |
Vivien Didelot | 24df898 | 2015-01-20 19:13:32 -0500 | [diff] [blame] | 49 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | /* slave device handling ****************************************************/ |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 54 | static int dsa_slave_init(struct net_device *dev) |
| 55 | { |
| 56 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 57 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 58 | dev->iflink = p->parent->dst->master_netdev->ifindex; |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 63 | static int dsa_slave_open(struct net_device *dev) |
| 64 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 65 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 66 | struct net_device *master = p->parent->dst->master_netdev; |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 67 | struct dsa_switch *ds = p->parent; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 68 | int err; |
| 69 | |
| 70 | if (!(master->flags & IFF_UP)) |
| 71 | return -ENETDOWN; |
| 72 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 73 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 74 | err = dev_uc_add(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 75 | if (err < 0) |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | if (dev->flags & IFF_ALLMULTI) { |
| 80 | err = dev_set_allmulti(master, 1); |
| 81 | if (err < 0) |
| 82 | goto del_unicast; |
| 83 | } |
| 84 | if (dev->flags & IFF_PROMISC) { |
| 85 | err = dev_set_promiscuity(master, 1); |
| 86 | if (err < 0) |
| 87 | goto clear_allmulti; |
| 88 | } |
| 89 | |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 90 | if (ds->drv->port_enable) { |
| 91 | err = ds->drv->port_enable(ds, p->port, p->phy); |
| 92 | if (err) |
| 93 | goto clear_promisc; |
| 94 | } |
| 95 | |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 96 | if (p->phy) |
| 97 | phy_start(p->phy); |
| 98 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 99 | return 0; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 100 | |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 101 | clear_promisc: |
| 102 | if (dev->flags & IFF_PROMISC) |
| 103 | dev_set_promiscuity(master, 0); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 104 | clear_allmulti: |
| 105 | if (dev->flags & IFF_ALLMULTI) |
| 106 | dev_set_allmulti(master, -1); |
| 107 | del_unicast: |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 108 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 109 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 110 | out: |
| 111 | return err; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int dsa_slave_close(struct net_device *dev) |
| 115 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 116 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 117 | struct net_device *master = p->parent->dst->master_netdev; |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 118 | struct dsa_switch *ds = p->parent; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 119 | |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 120 | if (p->phy) |
| 121 | phy_stop(p->phy); |
| 122 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 123 | dev_mc_unsync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 124 | dev_uc_unsync(master, dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 125 | if (dev->flags & IFF_ALLMULTI) |
| 126 | dev_set_allmulti(master, -1); |
| 127 | if (dev->flags & IFF_PROMISC) |
| 128 | dev_set_promiscuity(master, -1); |
| 129 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 130 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 131 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 132 | |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 133 | if (ds->drv->port_disable) |
| 134 | ds->drv->port_disable(ds, p->port, p->phy); |
| 135 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static void dsa_slave_change_rx_flags(struct net_device *dev, int change) |
| 140 | { |
| 141 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 142 | struct net_device *master = p->parent->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 143 | |
| 144 | if (change & IFF_ALLMULTI) |
| 145 | dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 146 | if (change & IFF_PROMISC) |
| 147 | dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); |
| 148 | } |
| 149 | |
| 150 | static void dsa_slave_set_rx_mode(struct net_device *dev) |
| 151 | { |
| 152 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 153 | struct net_device *master = p->parent->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 154 | |
| 155 | dev_mc_sync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 156 | dev_uc_sync(master, dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 159 | static int dsa_slave_set_mac_address(struct net_device *dev, void *a) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 160 | { |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 161 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 162 | struct net_device *master = p->parent->dst->master_netdev; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 163 | struct sockaddr *addr = a; |
| 164 | int err; |
| 165 | |
| 166 | if (!is_valid_ether_addr(addr->sa_data)) |
| 167 | return -EADDRNOTAVAIL; |
| 168 | |
| 169 | if (!(dev->flags & IFF_UP)) |
| 170 | goto out; |
| 171 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 172 | if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 173 | err = dev_uc_add(master, addr->sa_data); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 174 | if (err < 0) |
| 175 | return err; |
| 176 | } |
| 177 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 178 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 179 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 180 | |
| 181 | out: |
Joe Perches | d08f161 | 2014-01-20 09:52:20 -0800 | [diff] [blame] | 182 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 188 | { |
| 189 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 190 | |
| 191 | if (p->phy != NULL) |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 192 | return phy_mii_ioctl(p->phy, ifr, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 193 | |
| 194 | return -EOPNOTSUPP; |
| 195 | } |
| 196 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 197 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 198 | { |
| 199 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 200 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 201 | return p->xmit(skb, dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 204 | static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb, |
| 205 | struct net_device *dev) |
| 206 | { |
| 207 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 208 | |
| 209 | skb->dev = p->parent->dst->master_netdev; |
| 210 | dev_queue_xmit(skb); |
| 211 | |
| 212 | return NETDEV_TX_OK; |
| 213 | } |
| 214 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 215 | |
| 216 | /* ethtool operations *******************************************************/ |
| 217 | static int |
| 218 | dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 219 | { |
| 220 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 221 | int err; |
| 222 | |
| 223 | err = -EOPNOTSUPP; |
| 224 | if (p->phy != NULL) { |
| 225 | err = phy_read_status(p->phy); |
| 226 | if (err == 0) |
| 227 | err = phy_ethtool_gset(p->phy, cmd); |
| 228 | } |
| 229 | |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 235 | { |
| 236 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 237 | |
| 238 | if (p->phy != NULL) |
| 239 | return phy_ethtool_sset(p->phy, cmd); |
| 240 | |
| 241 | return -EOPNOTSUPP; |
| 242 | } |
| 243 | |
| 244 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 245 | struct ethtool_drvinfo *drvinfo) |
| 246 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 247 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
| 248 | strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); |
| 249 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 250 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 253 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 254 | { |
| 255 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 256 | struct dsa_switch *ds = p->parent; |
| 257 | |
| 258 | if (ds->drv->get_regs_len) |
| 259 | return ds->drv->get_regs_len(ds, p->port); |
| 260 | |
| 261 | return -EOPNOTSUPP; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 266 | { |
| 267 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 268 | struct dsa_switch *ds = p->parent; |
| 269 | |
| 270 | if (ds->drv->get_regs) |
| 271 | ds->drv->get_regs(ds, p->port, regs, _p); |
| 272 | } |
| 273 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 274 | static int dsa_slave_nway_reset(struct net_device *dev) |
| 275 | { |
| 276 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 277 | |
| 278 | if (p->phy != NULL) |
| 279 | return genphy_restart_aneg(p->phy); |
| 280 | |
| 281 | return -EOPNOTSUPP; |
| 282 | } |
| 283 | |
| 284 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 285 | { |
| 286 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 287 | |
| 288 | if (p->phy != NULL) { |
| 289 | genphy_update_link(p->phy); |
| 290 | return p->phy->link; |
| 291 | } |
| 292 | |
| 293 | return -EOPNOTSUPP; |
| 294 | } |
| 295 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 296 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 297 | { |
| 298 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 299 | struct dsa_switch *ds = p->parent; |
| 300 | |
| 301 | if (ds->pd->eeprom_len) |
| 302 | return ds->pd->eeprom_len; |
| 303 | |
| 304 | if (ds->drv->get_eeprom_len) |
| 305 | return ds->drv->get_eeprom_len(ds); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 311 | struct ethtool_eeprom *eeprom, u8 *data) |
| 312 | { |
| 313 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 314 | struct dsa_switch *ds = p->parent; |
| 315 | |
| 316 | if (ds->drv->get_eeprom) |
| 317 | return ds->drv->get_eeprom(ds, eeprom, data); |
| 318 | |
| 319 | return -EOPNOTSUPP; |
| 320 | } |
| 321 | |
| 322 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 323 | struct ethtool_eeprom *eeprom, u8 *data) |
| 324 | { |
| 325 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 326 | struct dsa_switch *ds = p->parent; |
| 327 | |
| 328 | if (ds->drv->set_eeprom) |
| 329 | return ds->drv->set_eeprom(ds, eeprom, data); |
| 330 | |
| 331 | return -EOPNOTSUPP; |
| 332 | } |
| 333 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 334 | static void dsa_slave_get_strings(struct net_device *dev, |
| 335 | uint32_t stringset, uint8_t *data) |
| 336 | { |
| 337 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 338 | struct dsa_switch *ds = p->parent; |
| 339 | |
| 340 | if (stringset == ETH_SS_STATS) { |
| 341 | int len = ETH_GSTRING_LEN; |
| 342 | |
| 343 | strncpy(data, "tx_packets", len); |
| 344 | strncpy(data + len, "tx_bytes", len); |
| 345 | strncpy(data + 2 * len, "rx_packets", len); |
| 346 | strncpy(data + 3 * len, "rx_bytes", len); |
| 347 | if (ds->drv->get_strings != NULL) |
| 348 | ds->drv->get_strings(ds, p->port, data + 4 * len); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 353 | struct ethtool_stats *stats, |
| 354 | uint64_t *data) |
| 355 | { |
| 356 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 357 | struct dsa_switch *ds = p->parent; |
| 358 | |
| 359 | data[0] = p->dev->stats.tx_packets; |
| 360 | data[1] = p->dev->stats.tx_bytes; |
| 361 | data[2] = p->dev->stats.rx_packets; |
| 362 | data[3] = p->dev->stats.rx_bytes; |
| 363 | if (ds->drv->get_ethtool_stats != NULL) |
| 364 | ds->drv->get_ethtool_stats(ds, p->port, data + 4); |
| 365 | } |
| 366 | |
| 367 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 368 | { |
| 369 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 370 | struct dsa_switch *ds = p->parent; |
| 371 | |
| 372 | if (sset == ETH_SS_STATS) { |
| 373 | int count; |
| 374 | |
| 375 | count = 4; |
| 376 | if (ds->drv->get_sset_count != NULL) |
| 377 | count += ds->drv->get_sset_count(ds); |
| 378 | |
| 379 | return count; |
| 380 | } |
| 381 | |
| 382 | return -EOPNOTSUPP; |
| 383 | } |
| 384 | |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 385 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 386 | { |
| 387 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 388 | struct dsa_switch *ds = p->parent; |
| 389 | |
| 390 | if (ds->drv->get_wol) |
| 391 | ds->drv->get_wol(ds, p->port, w); |
| 392 | } |
| 393 | |
| 394 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 395 | { |
| 396 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 397 | struct dsa_switch *ds = p->parent; |
| 398 | int ret = -EOPNOTSUPP; |
| 399 | |
| 400 | if (ds->drv->set_wol) |
| 401 | ret = ds->drv->set_wol(ds, p->port, w); |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 406 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 407 | { |
| 408 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 409 | struct dsa_switch *ds = p->parent; |
| 410 | int ret; |
| 411 | |
| 412 | if (!ds->drv->set_eee) |
| 413 | return -EOPNOTSUPP; |
| 414 | |
| 415 | ret = ds->drv->set_eee(ds, p->port, p->phy, e); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
| 419 | if (p->phy) |
| 420 | ret = phy_ethtool_set_eee(p->phy, e); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 426 | { |
| 427 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 428 | struct dsa_switch *ds = p->parent; |
| 429 | int ret; |
| 430 | |
| 431 | if (!ds->drv->get_eee) |
| 432 | return -EOPNOTSUPP; |
| 433 | |
| 434 | ret = ds->drv->get_eee(ds, p->port, e); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | |
| 438 | if (p->phy) |
| 439 | ret = phy_ethtool_get_eee(p->phy, e); |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 444 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
| 445 | .get_settings = dsa_slave_get_settings, |
| 446 | .set_settings = dsa_slave_set_settings, |
| 447 | .get_drvinfo = dsa_slave_get_drvinfo, |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 448 | .get_regs_len = dsa_slave_get_regs_len, |
| 449 | .get_regs = dsa_slave_get_regs, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 450 | .nway_reset = dsa_slave_nway_reset, |
| 451 | .get_link = dsa_slave_get_link, |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 452 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 453 | .get_eeprom = dsa_slave_get_eeprom, |
| 454 | .set_eeprom = dsa_slave_set_eeprom, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 455 | .get_strings = dsa_slave_get_strings, |
| 456 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 457 | .get_sset_count = dsa_slave_get_sset_count, |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 458 | .set_wol = dsa_slave_set_wol, |
| 459 | .get_wol = dsa_slave_get_wol, |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 460 | .set_eee = dsa_slave_set_eee, |
| 461 | .get_eee = dsa_slave_get_eee, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 464 | static const struct net_device_ops dsa_slave_netdev_ops = { |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 465 | .ndo_init = dsa_slave_init, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 466 | .ndo_open = dsa_slave_open, |
| 467 | .ndo_stop = dsa_slave_close, |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 468 | .ndo_start_xmit = dsa_slave_xmit, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 469 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 470 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 471 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
| 472 | .ndo_do_ioctl = dsa_slave_ioctl, |
| 473 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 474 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 475 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 476 | { |
| 477 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 478 | struct dsa_switch *ds = p->parent; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 479 | unsigned int status_changed = 0; |
| 480 | |
| 481 | if (p->old_link != p->phy->link) { |
| 482 | status_changed = 1; |
| 483 | p->old_link = p->phy->link; |
| 484 | } |
| 485 | |
| 486 | if (p->old_duplex != p->phy->duplex) { |
| 487 | status_changed = 1; |
| 488 | p->old_duplex = p->phy->duplex; |
| 489 | } |
| 490 | |
| 491 | if (p->old_pause != p->phy->pause) { |
| 492 | status_changed = 1; |
| 493 | p->old_pause = p->phy->pause; |
| 494 | } |
| 495 | |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 496 | if (ds->drv->adjust_link && status_changed) |
| 497 | ds->drv->adjust_link(ds, p->port, p->phy); |
| 498 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 499 | if (status_changed) |
| 500 | phy_print_status(p->phy); |
| 501 | } |
| 502 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 503 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 504 | struct fixed_phy_status *status) |
| 505 | { |
| 506 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 507 | struct dsa_switch *ds = p->parent; |
| 508 | |
| 509 | if (ds->drv->fixed_link_update) |
| 510 | ds->drv->fixed_link_update(ds, p->port, status); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 515 | /* slave device setup *******************************************************/ |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 516 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 517 | struct net_device *slave_dev) |
| 518 | { |
| 519 | struct dsa_switch *ds = p->parent; |
| 520 | struct dsa_chip_data *cd = ds->pd; |
| 521 | struct device_node *phy_dn, *port_dn; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 522 | bool phy_is_fixed = false; |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 523 | u32 phy_flags = 0; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 524 | int mode, ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 525 | |
| 526 | port_dn = cd->port_dn[p->port]; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 527 | mode = of_get_phy_mode(port_dn); |
| 528 | if (mode < 0) |
| 529 | mode = PHY_INTERFACE_MODE_NA; |
| 530 | p->phy_interface = mode; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 531 | |
| 532 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
| 533 | if (of_phy_is_fixed_link(port_dn)) { |
| 534 | /* In the case of a fixed PHY, the DT node associated |
| 535 | * to the fixed PHY is the Port DT node |
| 536 | */ |
| 537 | ret = of_phy_register_fixed_link(port_dn); |
| 538 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 539 | netdev_err(slave_dev, "failed to register fixed PHY\n"); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 540 | return ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 541 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 542 | phy_is_fixed = true; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 543 | phy_dn = port_dn; |
| 544 | } |
| 545 | |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 546 | if (ds->drv->get_phy_flags) |
| 547 | phy_flags = ds->drv->get_phy_flags(ds, p->port); |
| 548 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 549 | if (phy_dn) |
| 550 | p->phy = of_phy_connect(slave_dev, phy_dn, |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 551 | dsa_slave_adjust_link, phy_flags, |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 552 | p->phy_interface); |
| 553 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 554 | if (p->phy && phy_is_fixed) |
| 555 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); |
| 556 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 557 | /* We could not connect to a designated PHY, so use the switch internal |
| 558 | * MDIO bus instead |
| 559 | */ |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 560 | if (!p->phy) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 561 | p->phy = ds->slave_mii_bus->phy_map[p->port]; |
Florian Fainelli | 53013c7 | 2014-12-11 12:49:15 -0800 | [diff] [blame] | 562 | if (!p->phy) |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 563 | return -ENODEV; |
Florian Fainelli | 53013c7 | 2014-12-11 12:49:15 -0800 | [diff] [blame] | 564 | |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 565 | /* Use already configured phy mode */ |
| 566 | p->phy_interface = p->phy->interface; |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 567 | phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
| 568 | p->phy_interface); |
| 569 | } else { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 570 | netdev_info(slave_dev, "attached PHY at address %d [%s]\n", |
| 571 | p->phy->addr, p->phy->drv->name); |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 572 | } |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 573 | |
| 574 | return 0; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 577 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 578 | { |
| 579 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 580 | |
| 581 | netif_device_detach(slave_dev); |
| 582 | |
| 583 | if (p->phy) { |
| 584 | phy_stop(p->phy); |
| 585 | p->old_pause = -1; |
| 586 | p->old_link = -1; |
| 587 | p->old_duplex = -1; |
| 588 | phy_suspend(p->phy); |
| 589 | } |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | int dsa_slave_resume(struct net_device *slave_dev) |
| 595 | { |
| 596 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 597 | |
| 598 | netif_device_attach(slave_dev); |
| 599 | |
| 600 | if (p->phy) { |
| 601 | phy_resume(p->phy); |
| 602 | phy_start(p->phy); |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 608 | int dsa_slave_create(struct dsa_switch *ds, struct device *parent, |
| 609 | int port, char *name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 610 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 611 | struct net_device *master = ds->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 612 | struct net_device *slave_dev; |
| 613 | struct dsa_slave_priv *p; |
| 614 | int ret; |
| 615 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 616 | slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, |
| 617 | NET_NAME_UNKNOWN, ether_setup); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 618 | if (slave_dev == NULL) |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 619 | return -ENOMEM; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 620 | |
| 621 | slave_dev->features = master->vlan_features; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 622 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
Bjørn Mork | 2fcc800 | 2013-08-30 18:08:46 +0200 | [diff] [blame] | 623 | eth_hw_addr_inherit(slave_dev, master); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 624 | slave_dev->tx_queue_len = 0; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 625 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 626 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 627 | SET_NETDEV_DEV(slave_dev, parent); |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 628 | slave_dev->dev.of_node = ds->pd->port_dn[port]; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 629 | slave_dev->vlan_features = master->vlan_features; |
| 630 | |
| 631 | p = netdev_priv(slave_dev); |
| 632 | p->dev = slave_dev; |
| 633 | p->parent = ds; |
| 634 | p->port = port; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 635 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 636 | switch (ds->dst->tag_protocol) { |
| 637 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 638 | case DSA_TAG_PROTO_DSA: |
| 639 | p->xmit = dsa_netdev_ops.xmit; |
| 640 | break; |
| 641 | #endif |
| 642 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 643 | case DSA_TAG_PROTO_EDSA: |
| 644 | p->xmit = edsa_netdev_ops.xmit; |
| 645 | break; |
| 646 | #endif |
| 647 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 648 | case DSA_TAG_PROTO_TRAILER: |
| 649 | p->xmit = trailer_netdev_ops.xmit; |
| 650 | break; |
| 651 | #endif |
| 652 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 653 | case DSA_TAG_PROTO_BRCM: |
| 654 | p->xmit = brcm_netdev_ops.xmit; |
| 655 | break; |
| 656 | #endif |
| 657 | default: |
| 658 | p->xmit = dsa_slave_notag_xmit; |
| 659 | break; |
| 660 | } |
| 661 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 662 | p->old_pause = -1; |
| 663 | p->old_link = -1; |
| 664 | p->old_duplex = -1; |
| 665 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 666 | ret = dsa_slave_phy_setup(p, slave_dev); |
| 667 | if (ret) { |
| 668 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 669 | return ret; |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 670 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 671 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 672 | ds->ports[port] = slave_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 673 | ret = register_netdev(slave_dev); |
| 674 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 675 | netdev_err(master, "error %d registering interface %s\n", |
| 676 | ret, slave_dev->name); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 677 | phy_disconnect(p->phy); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 678 | ds->ports[port] = NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 679 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 680 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | netif_carrier_off(slave_dev); |
| 684 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame^] | 685 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 686 | } |