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