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++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 225 | if (!dsa_is_port_initialized(ds, port)) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 226 | continue; |
| 227 | |
| 228 | p = netdev_priv(ds->ports[port]); |
| 229 | |
| 230 | if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT && |
| 231 | p->bridge_dev == bridge) |
| 232 | mask |= 1 << port; |
| 233 | } |
| 234 | |
| 235 | return mask; |
| 236 | } |
| 237 | |
| 238 | static int dsa_slave_stp_update(struct net_device *dev, u8 state) |
| 239 | { |
| 240 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 241 | struct dsa_switch *ds = p->parent; |
| 242 | int ret = -EOPNOTSUPP; |
| 243 | |
| 244 | if (ds->drv->port_stp_update) |
| 245 | ret = ds->drv->port_stp_update(ds, p->port, state); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static int dsa_slave_bridge_port_join(struct net_device *dev, |
| 251 | struct net_device *br) |
| 252 | { |
| 253 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 254 | struct dsa_switch *ds = p->parent; |
| 255 | int ret = -EOPNOTSUPP; |
| 256 | |
| 257 | p->bridge_dev = br; |
| 258 | |
| 259 | if (ds->drv->port_join_bridge) |
| 260 | ret = ds->drv->port_join_bridge(ds, p->port, |
| 261 | dsa_slave_br_port_mask(ds, br)); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int dsa_slave_bridge_port_leave(struct net_device *dev) |
| 267 | { |
| 268 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 269 | struct dsa_switch *ds = p->parent; |
| 270 | int ret = -EOPNOTSUPP; |
| 271 | |
| 272 | |
| 273 | if (ds->drv->port_leave_bridge) |
| 274 | ret = ds->drv->port_leave_bridge(ds, p->port, |
| 275 | dsa_slave_br_port_mask(ds, p->bridge_dev)); |
| 276 | |
| 277 | p->bridge_dev = NULL; |
| 278 | |
| 279 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 280 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 281 | */ |
| 282 | dsa_slave_stp_update(dev, BR_STATE_FORWARDING); |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | static int dsa_slave_parent_id_get(struct net_device *dev, |
| 288 | struct netdev_phys_item_id *psid) |
| 289 | { |
| 290 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 291 | struct dsa_switch *ds = p->parent; |
| 292 | |
| 293 | psid->id_len = sizeof(ds->index); |
| 294 | memcpy(&psid->id, &ds->index, psid->id_len); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 299 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 300 | { |
| 301 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 302 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 303 | return p->xmit(skb, dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 306 | static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb, |
| 307 | struct net_device *dev) |
| 308 | { |
| 309 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 310 | |
| 311 | skb->dev = p->parent->dst->master_netdev; |
| 312 | dev_queue_xmit(skb); |
| 313 | |
| 314 | return NETDEV_TX_OK; |
| 315 | } |
| 316 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 317 | |
| 318 | /* ethtool operations *******************************************************/ |
| 319 | static int |
| 320 | dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 321 | { |
| 322 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 323 | int err; |
| 324 | |
| 325 | err = -EOPNOTSUPP; |
| 326 | if (p->phy != NULL) { |
| 327 | err = phy_read_status(p->phy); |
| 328 | if (err == 0) |
| 329 | err = phy_ethtool_gset(p->phy, cmd); |
| 330 | } |
| 331 | |
| 332 | return err; |
| 333 | } |
| 334 | |
| 335 | static int |
| 336 | dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 337 | { |
| 338 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 339 | |
| 340 | if (p->phy != NULL) |
| 341 | return phy_ethtool_sset(p->phy, cmd); |
| 342 | |
| 343 | return -EOPNOTSUPP; |
| 344 | } |
| 345 | |
| 346 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 347 | struct ethtool_drvinfo *drvinfo) |
| 348 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 349 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
| 350 | strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); |
| 351 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 352 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 355 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 356 | { |
| 357 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 358 | struct dsa_switch *ds = p->parent; |
| 359 | |
| 360 | if (ds->drv->get_regs_len) |
| 361 | return ds->drv->get_regs_len(ds, p->port); |
| 362 | |
| 363 | return -EOPNOTSUPP; |
| 364 | } |
| 365 | |
| 366 | static void |
| 367 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 368 | { |
| 369 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 370 | struct dsa_switch *ds = p->parent; |
| 371 | |
| 372 | if (ds->drv->get_regs) |
| 373 | ds->drv->get_regs(ds, p->port, regs, _p); |
| 374 | } |
| 375 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 376 | static int dsa_slave_nway_reset(struct net_device *dev) |
| 377 | { |
| 378 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 379 | |
| 380 | if (p->phy != NULL) |
| 381 | return genphy_restart_aneg(p->phy); |
| 382 | |
| 383 | return -EOPNOTSUPP; |
| 384 | } |
| 385 | |
| 386 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 387 | { |
| 388 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 389 | |
| 390 | if (p->phy != NULL) { |
| 391 | genphy_update_link(p->phy); |
| 392 | return p->phy->link; |
| 393 | } |
| 394 | |
| 395 | return -EOPNOTSUPP; |
| 396 | } |
| 397 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 398 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 399 | { |
| 400 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 401 | struct dsa_switch *ds = p->parent; |
| 402 | |
| 403 | if (ds->pd->eeprom_len) |
| 404 | return ds->pd->eeprom_len; |
| 405 | |
| 406 | if (ds->drv->get_eeprom_len) |
| 407 | return ds->drv->get_eeprom_len(ds); |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 413 | struct ethtool_eeprom *eeprom, u8 *data) |
| 414 | { |
| 415 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 416 | struct dsa_switch *ds = p->parent; |
| 417 | |
| 418 | if (ds->drv->get_eeprom) |
| 419 | return ds->drv->get_eeprom(ds, eeprom, data); |
| 420 | |
| 421 | return -EOPNOTSUPP; |
| 422 | } |
| 423 | |
| 424 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 425 | struct ethtool_eeprom *eeprom, u8 *data) |
| 426 | { |
| 427 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 428 | struct dsa_switch *ds = p->parent; |
| 429 | |
| 430 | if (ds->drv->set_eeprom) |
| 431 | return ds->drv->set_eeprom(ds, eeprom, data); |
| 432 | |
| 433 | return -EOPNOTSUPP; |
| 434 | } |
| 435 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 436 | static void dsa_slave_get_strings(struct net_device *dev, |
| 437 | uint32_t stringset, uint8_t *data) |
| 438 | { |
| 439 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 440 | struct dsa_switch *ds = p->parent; |
| 441 | |
| 442 | if (stringset == ETH_SS_STATS) { |
| 443 | int len = ETH_GSTRING_LEN; |
| 444 | |
| 445 | strncpy(data, "tx_packets", len); |
| 446 | strncpy(data + len, "tx_bytes", len); |
| 447 | strncpy(data + 2 * len, "rx_packets", len); |
| 448 | strncpy(data + 3 * len, "rx_bytes", len); |
| 449 | if (ds->drv->get_strings != NULL) |
| 450 | ds->drv->get_strings(ds, p->port, data + 4 * len); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 455 | struct ethtool_stats *stats, |
| 456 | uint64_t *data) |
| 457 | { |
| 458 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 459 | struct dsa_switch *ds = p->parent; |
| 460 | |
| 461 | data[0] = p->dev->stats.tx_packets; |
| 462 | data[1] = p->dev->stats.tx_bytes; |
| 463 | data[2] = p->dev->stats.rx_packets; |
| 464 | data[3] = p->dev->stats.rx_bytes; |
| 465 | if (ds->drv->get_ethtool_stats != NULL) |
| 466 | ds->drv->get_ethtool_stats(ds, p->port, data + 4); |
| 467 | } |
| 468 | |
| 469 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 470 | { |
| 471 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 472 | struct dsa_switch *ds = p->parent; |
| 473 | |
| 474 | if (sset == ETH_SS_STATS) { |
| 475 | int count; |
| 476 | |
| 477 | count = 4; |
| 478 | if (ds->drv->get_sset_count != NULL) |
| 479 | count += ds->drv->get_sset_count(ds); |
| 480 | |
| 481 | return count; |
| 482 | } |
| 483 | |
| 484 | return -EOPNOTSUPP; |
| 485 | } |
| 486 | |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 487 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 488 | { |
| 489 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 490 | struct dsa_switch *ds = p->parent; |
| 491 | |
| 492 | if (ds->drv->get_wol) |
| 493 | ds->drv->get_wol(ds, p->port, w); |
| 494 | } |
| 495 | |
| 496 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 497 | { |
| 498 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 499 | struct dsa_switch *ds = p->parent; |
| 500 | int ret = -EOPNOTSUPP; |
| 501 | |
| 502 | if (ds->drv->set_wol) |
| 503 | ret = ds->drv->set_wol(ds, p->port, w); |
| 504 | |
| 505 | return ret; |
| 506 | } |
| 507 | |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 508 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 509 | { |
| 510 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 511 | struct dsa_switch *ds = p->parent; |
| 512 | int ret; |
| 513 | |
| 514 | if (!ds->drv->set_eee) |
| 515 | return -EOPNOTSUPP; |
| 516 | |
| 517 | ret = ds->drv->set_eee(ds, p->port, p->phy, e); |
| 518 | if (ret) |
| 519 | return ret; |
| 520 | |
| 521 | if (p->phy) |
| 522 | ret = phy_ethtool_set_eee(p->phy, e); |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 528 | { |
| 529 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 530 | struct dsa_switch *ds = p->parent; |
| 531 | int ret; |
| 532 | |
| 533 | if (!ds->drv->get_eee) |
| 534 | return -EOPNOTSUPP; |
| 535 | |
| 536 | ret = ds->drv->get_eee(ds, p->port, e); |
| 537 | if (ret) |
| 538 | return ret; |
| 539 | |
| 540 | if (p->phy) |
| 541 | ret = phy_ethtool_get_eee(p->phy, e); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 546 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
| 547 | .get_settings = dsa_slave_get_settings, |
| 548 | .set_settings = dsa_slave_set_settings, |
| 549 | .get_drvinfo = dsa_slave_get_drvinfo, |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 550 | .get_regs_len = dsa_slave_get_regs_len, |
| 551 | .get_regs = dsa_slave_get_regs, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 552 | .nway_reset = dsa_slave_nway_reset, |
| 553 | .get_link = dsa_slave_get_link, |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 554 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 555 | .get_eeprom = dsa_slave_get_eeprom, |
| 556 | .set_eeprom = dsa_slave_set_eeprom, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 557 | .get_strings = dsa_slave_get_strings, |
| 558 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 559 | .get_sset_count = dsa_slave_get_sset_count, |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 560 | .set_wol = dsa_slave_set_wol, |
| 561 | .get_wol = dsa_slave_get_wol, |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 562 | .set_eee = dsa_slave_set_eee, |
| 563 | .get_eee = dsa_slave_get_eee, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 564 | }; |
| 565 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 566 | static const struct net_device_ops dsa_slave_netdev_ops = { |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 567 | .ndo_init = dsa_slave_init, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 568 | .ndo_open = dsa_slave_open, |
| 569 | .ndo_stop = dsa_slave_close, |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 570 | .ndo_start_xmit = dsa_slave_xmit, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 571 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 572 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 573 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
| 574 | .ndo_do_ioctl = dsa_slave_ioctl, |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 575 | .ndo_switch_parent_id_get = dsa_slave_parent_id_get, |
| 576 | .ndo_switch_port_stp_update = dsa_slave_stp_update, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 577 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 578 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 579 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 580 | { |
| 581 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 582 | struct dsa_switch *ds = p->parent; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 583 | unsigned int status_changed = 0; |
| 584 | |
| 585 | if (p->old_link != p->phy->link) { |
| 586 | status_changed = 1; |
| 587 | p->old_link = p->phy->link; |
| 588 | } |
| 589 | |
| 590 | if (p->old_duplex != p->phy->duplex) { |
| 591 | status_changed = 1; |
| 592 | p->old_duplex = p->phy->duplex; |
| 593 | } |
| 594 | |
| 595 | if (p->old_pause != p->phy->pause) { |
| 596 | status_changed = 1; |
| 597 | p->old_pause = p->phy->pause; |
| 598 | } |
| 599 | |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 600 | if (ds->drv->adjust_link && status_changed) |
| 601 | ds->drv->adjust_link(ds, p->port, p->phy); |
| 602 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 603 | if (status_changed) |
| 604 | phy_print_status(p->phy); |
| 605 | } |
| 606 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 607 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 608 | struct fixed_phy_status *status) |
| 609 | { |
| 610 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 611 | struct dsa_switch *ds = p->parent; |
| 612 | |
| 613 | if (ds->drv->fixed_link_update) |
| 614 | ds->drv->fixed_link_update(ds, p->port, status); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 619 | /* slave device setup *******************************************************/ |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 620 | static int dsa_slave_phy_connect(struct dsa_slave_priv *p, |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 621 | struct net_device *slave_dev, |
| 622 | int addr) |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 623 | { |
| 624 | struct dsa_switch *ds = p->parent; |
| 625 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 626 | p->phy = ds->slave_mii_bus->phy_map[addr]; |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 627 | if (!p->phy) |
| 628 | return -ENODEV; |
| 629 | |
| 630 | /* Use already configured phy mode */ |
| 631 | p->phy_interface = p->phy->interface; |
| 632 | phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
| 633 | p->phy_interface); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 638 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 639 | struct net_device *slave_dev) |
| 640 | { |
| 641 | struct dsa_switch *ds = p->parent; |
| 642 | struct dsa_chip_data *cd = ds->pd; |
| 643 | struct device_node *phy_dn, *port_dn; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 644 | bool phy_is_fixed = false; |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 645 | u32 phy_flags = 0; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 646 | int mode, ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 647 | |
| 648 | port_dn = cd->port_dn[p->port]; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 649 | mode = of_get_phy_mode(port_dn); |
| 650 | if (mode < 0) |
| 651 | mode = PHY_INTERFACE_MODE_NA; |
| 652 | p->phy_interface = mode; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 653 | |
| 654 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
| 655 | if (of_phy_is_fixed_link(port_dn)) { |
| 656 | /* In the case of a fixed PHY, the DT node associated |
| 657 | * to the fixed PHY is the Port DT node |
| 658 | */ |
| 659 | ret = of_phy_register_fixed_link(port_dn); |
| 660 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 661 | netdev_err(slave_dev, "failed to register fixed PHY\n"); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 662 | return ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 663 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 664 | phy_is_fixed = true; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 665 | phy_dn = port_dn; |
| 666 | } |
| 667 | |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 668 | if (ds->drv->get_phy_flags) |
| 669 | phy_flags = ds->drv->get_phy_flags(ds, p->port); |
| 670 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 671 | if (phy_dn) { |
| 672 | ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn); |
| 673 | /* If this PHY address is part of phys_mii_mask, which means |
| 674 | * that we need to divert reads and writes to/from it, then we |
| 675 | * want to bind this device using the slave MII bus created by |
| 676 | * DSA to make that happen. |
| 677 | */ |
Florian Fainelli | 96026d0 | 2015-03-14 13:21:59 -0700 | [diff] [blame^] | 678 | if (!phy_is_fixed && ret >= 0 && |
| 679 | (ds->phys_mii_mask & (1 << ret))) { |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 680 | ret = dsa_slave_phy_connect(p, slave_dev, ret); |
| 681 | if (ret) |
| 682 | return ret; |
| 683 | } else { |
| 684 | p->phy = of_phy_connect(slave_dev, phy_dn, |
| 685 | dsa_slave_adjust_link, |
| 686 | phy_flags, |
| 687 | p->phy_interface); |
| 688 | } |
| 689 | } |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 690 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 691 | if (p->phy && phy_is_fixed) |
| 692 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); |
| 693 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 694 | /* We could not connect to a designated PHY, so use the switch internal |
| 695 | * MDIO bus instead |
| 696 | */ |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 697 | if (!p->phy) { |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 698 | ret = dsa_slave_phy_connect(p, slave_dev, p->port); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 699 | if (ret) |
| 700 | return ret; |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 701 | } else { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 702 | netdev_info(slave_dev, "attached PHY at address %d [%s]\n", |
| 703 | p->phy->addr, p->phy->drv->name); |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 704 | } |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 705 | |
| 706 | return 0; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 709 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 710 | { |
| 711 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 712 | |
| 713 | netif_device_detach(slave_dev); |
| 714 | |
| 715 | if (p->phy) { |
| 716 | phy_stop(p->phy); |
| 717 | p->old_pause = -1; |
| 718 | p->old_link = -1; |
| 719 | p->old_duplex = -1; |
| 720 | phy_suspend(p->phy); |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | int dsa_slave_resume(struct net_device *slave_dev) |
| 727 | { |
| 728 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 729 | |
| 730 | netif_device_attach(slave_dev); |
| 731 | |
| 732 | if (p->phy) { |
| 733 | phy_resume(p->phy); |
| 734 | phy_start(p->phy); |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 740 | int dsa_slave_create(struct dsa_switch *ds, struct device *parent, |
| 741 | int port, char *name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 742 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 743 | struct net_device *master = ds->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 744 | struct net_device *slave_dev; |
| 745 | struct dsa_slave_priv *p; |
| 746 | int ret; |
| 747 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 748 | slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, |
| 749 | NET_NAME_UNKNOWN, ether_setup); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 750 | if (slave_dev == NULL) |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 751 | return -ENOMEM; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 752 | |
| 753 | slave_dev->features = master->vlan_features; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 754 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
Bjørn Mork | 2fcc800 | 2013-08-30 18:08:46 +0200 | [diff] [blame] | 755 | eth_hw_addr_inherit(slave_dev, master); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 756 | slave_dev->tx_queue_len = 0; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 757 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 758 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 759 | SET_NETDEV_DEV(slave_dev, parent); |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 760 | slave_dev->dev.of_node = ds->pd->port_dn[port]; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 761 | slave_dev->vlan_features = master->vlan_features; |
| 762 | |
| 763 | p = netdev_priv(slave_dev); |
| 764 | p->dev = slave_dev; |
| 765 | p->parent = ds; |
| 766 | p->port = port; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 767 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 768 | switch (ds->dst->tag_protocol) { |
| 769 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 770 | case DSA_TAG_PROTO_DSA: |
| 771 | p->xmit = dsa_netdev_ops.xmit; |
| 772 | break; |
| 773 | #endif |
| 774 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 775 | case DSA_TAG_PROTO_EDSA: |
| 776 | p->xmit = edsa_netdev_ops.xmit; |
| 777 | break; |
| 778 | #endif |
| 779 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 780 | case DSA_TAG_PROTO_TRAILER: |
| 781 | p->xmit = trailer_netdev_ops.xmit; |
| 782 | break; |
| 783 | #endif |
| 784 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 785 | case DSA_TAG_PROTO_BRCM: |
| 786 | p->xmit = brcm_netdev_ops.xmit; |
| 787 | break; |
| 788 | #endif |
| 789 | default: |
| 790 | p->xmit = dsa_slave_notag_xmit; |
| 791 | break; |
| 792 | } |
| 793 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 794 | p->old_pause = -1; |
| 795 | p->old_link = -1; |
| 796 | p->old_duplex = -1; |
| 797 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 798 | ret = dsa_slave_phy_setup(p, slave_dev); |
| 799 | if (ret) { |
| 800 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 801 | return ret; |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 802 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 803 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 804 | ds->ports[port] = slave_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 805 | ret = register_netdev(slave_dev); |
| 806 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 807 | netdev_err(master, "error %d registering interface %s\n", |
| 808 | ret, slave_dev->name); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 809 | phy_disconnect(p->phy); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 810 | ds->ports[port] = NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 811 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 812 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | netif_carrier_off(slave_dev); |
| 816 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 817 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 818 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 819 | |
| 820 | static bool dsa_slave_dev_check(struct net_device *dev) |
| 821 | { |
| 822 | return dev->netdev_ops == &dsa_slave_netdev_ops; |
| 823 | } |
| 824 | |
| 825 | static int dsa_slave_master_changed(struct net_device *dev) |
| 826 | { |
| 827 | struct net_device *master = netdev_master_upper_dev_get(dev); |
| 828 | int err = 0; |
| 829 | |
| 830 | if (master && master->rtnl_link_ops && |
| 831 | !strcmp(master->rtnl_link_ops->kind, "bridge")) |
| 832 | err = dsa_slave_bridge_port_join(dev, master); |
| 833 | else |
| 834 | err = dsa_slave_bridge_port_leave(dev); |
| 835 | |
| 836 | return err; |
| 837 | } |
| 838 | |
| 839 | int dsa_slave_netdevice_event(struct notifier_block *unused, |
| 840 | unsigned long event, void *ptr) |
| 841 | { |
| 842 | struct net_device *dev; |
| 843 | int err = 0; |
| 844 | |
| 845 | switch (event) { |
| 846 | case NETDEV_CHANGEUPPER: |
| 847 | dev = netdev_notifier_info_to_dev(ptr); |
| 848 | if (!dsa_slave_dev_check(dev)) |
| 849 | goto out; |
| 850 | |
| 851 | err = dsa_slave_master_changed(dev); |
| 852 | if (err) |
| 853 | netdev_warn(dev, "failed to reflect master change\n"); |
| 854 | |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | out: |
| 859 | return NOTIFY_DONE; |
| 860 | } |