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