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