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