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