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> |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 21 | #include <linux/netpoll.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 22 | #include "dsa_priv.h" |
| 23 | |
| 24 | /* slave mii_bus handling ***************************************************/ |
| 25 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) |
| 26 | { |
| 27 | struct dsa_switch *ds = bus->priv; |
| 28 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 29 | if (ds->phys_mii_mask & (1 << addr)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 30 | return ds->drv->phy_read(ds, addr, reg); |
| 31 | |
| 32 | return 0xffff; |
| 33 | } |
| 34 | |
| 35 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) |
| 36 | { |
| 37 | struct dsa_switch *ds = bus->priv; |
| 38 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 39 | if (ds->phys_mii_mask & (1 << addr)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 40 | return ds->drv->phy_write(ds, addr, reg, val); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) |
| 46 | { |
| 47 | ds->slave_mii_bus->priv = (void *)ds; |
| 48 | ds->slave_mii_bus->name = "dsa slave smi"; |
| 49 | ds->slave_mii_bus->read = dsa_slave_phy_read; |
| 50 | ds->slave_mii_bus->write = dsa_slave_phy_write; |
Florian Fainelli | f490be0 | 2013-01-21 09:58:50 +0000 | [diff] [blame] | 51 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x", |
| 52 | ds->index, ds->pd->sw_addr); |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 53 | ds->slave_mii_bus->parent = ds->master_dev; |
Vivien Didelot | 24df898 | 2015-01-20 19:13:32 -0500 | [diff] [blame] | 54 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | /* slave device handling ****************************************************/ |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 59 | static int dsa_slave_get_iflink(const struct net_device *dev) |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 60 | { |
| 61 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 62 | |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 63 | return p->parent->dst->master_netdev->ifindex; |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 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) |
Gilad Ben-Yossef | 4fdeddf | 2015-06-25 16:50:13 +0300 | [diff] [blame] | 116 | dev_set_promiscuity(master, -1); |
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 | |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 203 | static int dsa_bridge_check_vlan_range(struct dsa_switch *ds, |
| 204 | const struct net_device *bridge, |
| 205 | u16 vid_begin, u16 vid_end) |
| 206 | { |
| 207 | struct dsa_slave_priv *p; |
| 208 | struct net_device *dev, *vlan_br; |
| 209 | DECLARE_BITMAP(members, DSA_MAX_PORTS); |
| 210 | DECLARE_BITMAP(untagged, DSA_MAX_PORTS); |
| 211 | u16 vid; |
| 212 | int member, err; |
| 213 | |
| 214 | if (!ds->drv->vlan_getnext || !vid_begin) |
| 215 | return -EOPNOTSUPP; |
| 216 | |
| 217 | vid = vid_begin - 1; |
| 218 | |
| 219 | do { |
| 220 | err = ds->drv->vlan_getnext(ds, &vid, members, untagged); |
| 221 | if (err) |
| 222 | break; |
| 223 | |
| 224 | if (vid > vid_end) |
| 225 | break; |
| 226 | |
| 227 | member = find_first_bit(members, DSA_MAX_PORTS); |
| 228 | if (member == DSA_MAX_PORTS) |
| 229 | continue; |
| 230 | |
| 231 | dev = ds->ports[member]; |
| 232 | p = netdev_priv(dev); |
| 233 | vlan_br = p->bridge_dev; |
| 234 | if (vlan_br == bridge) |
| 235 | continue; |
| 236 | |
| 237 | netdev_dbg(vlan_br, "hardware VLAN %d already in use\n", vid); |
| 238 | return -EOPNOTSUPP; |
| 239 | } while (vid < vid_end); |
| 240 | |
| 241 | return err == -ENOENT ? 0 : err; |
| 242 | } |
| 243 | |
| 244 | static int dsa_slave_port_vlan_add(struct net_device *dev, |
Jiri Pirko | f8db834 | 2015-09-24 10:02:42 +0200 | [diff] [blame] | 245 | struct switchdev_obj *obj, |
| 246 | struct switchdev_trans *trans) |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 247 | { |
| 248 | struct switchdev_obj_vlan *vlan = &obj->u.vlan; |
| 249 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 250 | struct dsa_switch *ds = p->parent; |
| 251 | u16 vid; |
| 252 | int err; |
| 253 | |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame^] | 254 | if (switchdev_trans_ph_prepare(trans)) { |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 255 | if (!ds->drv->port_vlan_add || !ds->drv->port_pvid_set) |
| 256 | return -EOPNOTSUPP; |
| 257 | |
| 258 | /* If the requested port doesn't belong to the same bridge as |
| 259 | * the VLAN members, fallback to software VLAN (hopefully). |
| 260 | */ |
| 261 | err = dsa_bridge_check_vlan_range(ds, p->bridge_dev, |
| 262 | vlan->vid_begin, |
| 263 | vlan->vid_end); |
| 264 | if (err) |
| 265 | return err; |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame^] | 266 | } else { |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 267 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 268 | err = ds->drv->port_vlan_add(ds, p->port, vid, |
| 269 | vlan->flags & |
| 270 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 271 | if (!err && vlan->flags & BRIDGE_VLAN_INFO_PVID) |
| 272 | err = ds->drv->port_pvid_set(ds, p->port, vid); |
| 273 | if (err) |
| 274 | return err; |
| 275 | } |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int dsa_slave_port_vlan_del(struct net_device *dev, |
| 282 | struct switchdev_obj *obj) |
| 283 | { |
| 284 | struct switchdev_obj_vlan *vlan = &obj->u.vlan; |
| 285 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 286 | struct dsa_switch *ds = p->parent; |
| 287 | u16 vid; |
| 288 | int err; |
| 289 | |
| 290 | if (!ds->drv->port_vlan_del) |
| 291 | return -EOPNOTSUPP; |
| 292 | |
| 293 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 294 | err = ds->drv->port_vlan_del(ds, p->port, vid); |
| 295 | if (err) |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int dsa_slave_port_vlan_dump(struct net_device *dev, |
| 303 | struct switchdev_obj *obj) |
| 304 | { |
| 305 | struct switchdev_obj_vlan *vlan = &obj->u.vlan; |
| 306 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 307 | struct dsa_switch *ds = p->parent; |
| 308 | DECLARE_BITMAP(members, DSA_MAX_PORTS); |
| 309 | DECLARE_BITMAP(untagged, DSA_MAX_PORTS); |
| 310 | u16 pvid, vid = 0; |
| 311 | int err; |
| 312 | |
| 313 | if (!ds->drv->vlan_getnext || !ds->drv->port_pvid_get) |
| 314 | return -EOPNOTSUPP; |
| 315 | |
| 316 | err = ds->drv->port_pvid_get(ds, p->port, &pvid); |
| 317 | if (err) |
| 318 | return err; |
| 319 | |
| 320 | for (;;) { |
| 321 | err = ds->drv->vlan_getnext(ds, &vid, members, untagged); |
| 322 | if (err) |
| 323 | break; |
| 324 | |
| 325 | if (!test_bit(p->port, members)) |
| 326 | continue; |
| 327 | |
| 328 | memset(vlan, 0, sizeof(*vlan)); |
| 329 | vlan->vid_begin = vlan->vid_end = vid; |
| 330 | |
| 331 | if (vid == pvid) |
| 332 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; |
| 333 | |
| 334 | if (test_bit(p->port, untagged)) |
| 335 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 336 | |
| 337 | err = obj->cb(dev, obj); |
| 338 | if (err) |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | return err == -ENOENT ? 0 : err; |
| 343 | } |
| 344 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 345 | static int dsa_slave_port_fdb_add(struct net_device *dev, |
Jiri Pirko | f8db834 | 2015-09-24 10:02:42 +0200 | [diff] [blame] | 346 | struct switchdev_obj *obj, |
| 347 | struct switchdev_trans *trans) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 348 | { |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 349 | struct switchdev_obj_fdb *fdb = &obj->u.fdb; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 350 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 351 | struct dsa_switch *ds = p->parent; |
| 352 | int ret = -EOPNOTSUPP; |
| 353 | |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame^] | 354 | if (switchdev_trans_ph_prepare(trans)) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 355 | ret = ds->drv->port_fdb_add ? 0 : -EOPNOTSUPP; |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame^] | 356 | else |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 357 | ret = ds->drv->port_fdb_add(ds, p->port, fdb->addr, fdb->vid); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 362 | static int dsa_slave_port_fdb_del(struct net_device *dev, |
| 363 | struct switchdev_obj *obj) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 364 | { |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 365 | struct switchdev_obj_fdb *fdb = &obj->u.fdb; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 366 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 367 | struct dsa_switch *ds = p->parent; |
| 368 | int ret = -EOPNOTSUPP; |
| 369 | |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 370 | if (ds->drv->port_fdb_del) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 371 | ret = ds->drv->port_fdb_del(ds, p->port, fdb->addr, fdb->vid); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 376 | static int dsa_slave_port_fdb_dump(struct net_device *dev, |
| 377 | struct switchdev_obj *obj) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 378 | { |
| 379 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 380 | struct dsa_switch *ds = p->parent; |
| 381 | unsigned char addr[ETH_ALEN] = { 0 }; |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 382 | u16 vid = 0; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 383 | int ret; |
| 384 | |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 385 | if (!ds->drv->port_fdb_getnext) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 386 | return -EOPNOTSUPP; |
| 387 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 388 | for (;;) { |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 389 | bool is_static; |
| 390 | |
Vivien Didelot | 2a778e1 | 2015-08-10 09:09:49 -0400 | [diff] [blame] | 391 | ret = ds->drv->port_fdb_getnext(ds, p->port, addr, &vid, |
| 392 | &is_static); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 393 | if (ret < 0) |
| 394 | break; |
| 395 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 396 | obj->u.fdb.addr = addr; |
| 397 | obj->u.fdb.vid = vid; |
| 398 | obj->u.fdb.ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 399 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 400 | ret = obj->cb(dev, obj); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 401 | if (ret < 0) |
| 402 | break; |
| 403 | } |
| 404 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 405 | return ret == -ENOENT ? 0 : ret; |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 408 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 409 | { |
| 410 | struct dsa_slave_priv *p = netdev_priv(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 411 | |
| 412 | if (p->phy != NULL) |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 413 | return phy_mii_ioctl(p->phy, ifr, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 414 | |
| 415 | return -EOPNOTSUPP; |
| 416 | } |
| 417 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 418 | /* Return a bitmask of all ports being currently bridged within a given bridge |
| 419 | * device. Note that on leave, the mask will still return the bitmask of ports |
| 420 | * currently bridged, prior to port removal, and this is exactly what we want. |
| 421 | */ |
| 422 | static u32 dsa_slave_br_port_mask(struct dsa_switch *ds, |
| 423 | struct net_device *bridge) |
| 424 | { |
| 425 | struct dsa_slave_priv *p; |
| 426 | unsigned int port; |
| 427 | u32 mask = 0; |
| 428 | |
| 429 | for (port = 0; port < DSA_MAX_PORTS; port++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 430 | if (!dsa_is_port_initialized(ds, port)) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 431 | continue; |
| 432 | |
| 433 | p = netdev_priv(ds->ports[port]); |
| 434 | |
| 435 | if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT && |
| 436 | p->bridge_dev == bridge) |
| 437 | mask |= 1 << port; |
| 438 | } |
| 439 | |
| 440 | return mask; |
| 441 | } |
| 442 | |
| 443 | static int dsa_slave_stp_update(struct net_device *dev, u8 state) |
| 444 | { |
| 445 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 446 | struct dsa_switch *ds = p->parent; |
| 447 | int ret = -EOPNOTSUPP; |
| 448 | |
| 449 | if (ds->drv->port_stp_update) |
| 450 | ret = ds->drv->port_stp_update(ds, p->port, state); |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 455 | static int dsa_slave_port_attr_set(struct net_device *dev, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 456 | struct switchdev_attr *attr, |
| 457 | struct switchdev_trans *trans) |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 458 | { |
| 459 | int ret = 0; |
| 460 | |
| 461 | switch (attr->id) { |
| 462 | case SWITCHDEV_ATTR_PORT_STP_STATE: |
Jiri Pirko | 79a62eb | 2015-09-24 10:02:48 +0200 | [diff] [blame^] | 463 | if (switchdev_trans_ph_commit(trans)) |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 464 | ret = dsa_slave_stp_update(dev, attr->u.stp_state); |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 465 | break; |
| 466 | default: |
| 467 | ret = -EOPNOTSUPP; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 474 | static int dsa_slave_port_obj_add(struct net_device *dev, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 475 | struct switchdev_obj *obj, |
| 476 | struct switchdev_trans *trans) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 477 | { |
| 478 | int err; |
| 479 | |
| 480 | /* For the prepare phase, ensure the full set of changes is feasable in |
| 481 | * one go in order to signal a failure properly. If an operation is not |
| 482 | * supported, return -EOPNOTSUPP. |
| 483 | */ |
| 484 | |
| 485 | switch (obj->id) { |
| 486 | case SWITCHDEV_OBJ_PORT_FDB: |
Jiri Pirko | f8db834 | 2015-09-24 10:02:42 +0200 | [diff] [blame] | 487 | err = dsa_slave_port_fdb_add(dev, obj, trans); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 488 | break; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 489 | case SWITCHDEV_OBJ_PORT_VLAN: |
Jiri Pirko | f8db834 | 2015-09-24 10:02:42 +0200 | [diff] [blame] | 490 | err = dsa_slave_port_vlan_add(dev, obj, trans); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 491 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 492 | default: |
| 493 | err = -EOPNOTSUPP; |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | return err; |
| 498 | } |
| 499 | |
| 500 | static int dsa_slave_port_obj_del(struct net_device *dev, |
| 501 | struct switchdev_obj *obj) |
| 502 | { |
| 503 | int err; |
| 504 | |
| 505 | switch (obj->id) { |
| 506 | case SWITCHDEV_OBJ_PORT_FDB: |
| 507 | err = dsa_slave_port_fdb_del(dev, obj); |
| 508 | break; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 509 | case SWITCHDEV_OBJ_PORT_VLAN: |
| 510 | err = dsa_slave_port_vlan_del(dev, obj); |
| 511 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 512 | default: |
| 513 | err = -EOPNOTSUPP; |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | return err; |
| 518 | } |
| 519 | |
| 520 | static int dsa_slave_port_obj_dump(struct net_device *dev, |
| 521 | struct switchdev_obj *obj) |
| 522 | { |
| 523 | int err; |
| 524 | |
| 525 | switch (obj->id) { |
| 526 | case SWITCHDEV_OBJ_PORT_FDB: |
| 527 | err = dsa_slave_port_fdb_dump(dev, obj); |
| 528 | break; |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 529 | case SWITCHDEV_OBJ_PORT_VLAN: |
| 530 | err = dsa_slave_port_vlan_dump(dev, obj); |
| 531 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 532 | default: |
| 533 | err = -EOPNOTSUPP; |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | return err; |
| 538 | } |
| 539 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 540 | static int dsa_slave_bridge_port_join(struct net_device *dev, |
| 541 | struct net_device *br) |
| 542 | { |
| 543 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 544 | struct dsa_switch *ds = p->parent; |
| 545 | int ret = -EOPNOTSUPP; |
| 546 | |
| 547 | p->bridge_dev = br; |
| 548 | |
| 549 | if (ds->drv->port_join_bridge) |
| 550 | ret = ds->drv->port_join_bridge(ds, p->port, |
| 551 | dsa_slave_br_port_mask(ds, br)); |
| 552 | |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | static int dsa_slave_bridge_port_leave(struct net_device *dev) |
| 557 | { |
| 558 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 559 | struct dsa_switch *ds = p->parent; |
| 560 | int ret = -EOPNOTSUPP; |
| 561 | |
| 562 | |
| 563 | if (ds->drv->port_leave_bridge) |
| 564 | ret = ds->drv->port_leave_bridge(ds, p->port, |
| 565 | dsa_slave_br_port_mask(ds, p->bridge_dev)); |
| 566 | |
| 567 | p->bridge_dev = NULL; |
| 568 | |
| 569 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 570 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 571 | */ |
| 572 | dsa_slave_stp_update(dev, BR_STATE_FORWARDING); |
| 573 | |
| 574 | return ret; |
| 575 | } |
| 576 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 577 | static int dsa_slave_port_attr_get(struct net_device *dev, |
| 578 | struct switchdev_attr *attr) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 579 | { |
| 580 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 581 | struct dsa_switch *ds = p->parent; |
| 582 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 583 | switch (attr->id) { |
| 584 | case SWITCHDEV_ATTR_PORT_PARENT_ID: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 585 | attr->u.ppid.id_len = sizeof(ds->index); |
| 586 | memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 587 | break; |
| 588 | default: |
| 589 | return -EOPNOTSUPP; |
| 590 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 595 | static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, |
| 596 | struct sk_buff *skb) |
| 597 | { |
| 598 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 599 | if (p->netpoll) |
| 600 | netpoll_send_skb(p->netpoll, skb); |
| 601 | #else |
| 602 | BUG(); |
| 603 | #endif |
| 604 | return NETDEV_TX_OK; |
| 605 | } |
| 606 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 607 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 608 | { |
| 609 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 610 | struct sk_buff *nskb; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 611 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 612 | dev->stats.tx_packets++; |
| 613 | dev->stats.tx_bytes += skb->len; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 614 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 615 | /* Transmit function may have to reallocate the original SKB */ |
| 616 | nskb = p->xmit(skb, dev); |
| 617 | if (!nskb) |
| 618 | return NETDEV_TX_OK; |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 619 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 620 | /* SKB for netpoll still need to be mangled with the protocol-specific |
| 621 | * tag to be successfully transmitted |
| 622 | */ |
| 623 | if (unlikely(netpoll_tx_running(dev))) |
| 624 | return dsa_netpoll_send_skb(p, nskb); |
| 625 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 626 | /* Queue the SKB for transmission on the parent interface, but |
| 627 | * do not modify its EtherType |
| 628 | */ |
| 629 | nskb->dev = p->parent->dst->master_netdev; |
| 630 | dev_queue_xmit(nskb); |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 631 | |
| 632 | return NETDEV_TX_OK; |
| 633 | } |
| 634 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 635 | static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, |
| 636 | struct net_device *dev) |
| 637 | { |
| 638 | /* Just return the original SKB */ |
| 639 | return skb; |
| 640 | } |
| 641 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 642 | |
| 643 | /* ethtool operations *******************************************************/ |
| 644 | static int |
| 645 | dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 646 | { |
| 647 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 648 | int err; |
| 649 | |
| 650 | err = -EOPNOTSUPP; |
| 651 | if (p->phy != NULL) { |
| 652 | err = phy_read_status(p->phy); |
| 653 | if (err == 0) |
| 654 | err = phy_ethtool_gset(p->phy, cmd); |
| 655 | } |
| 656 | |
| 657 | return err; |
| 658 | } |
| 659 | |
| 660 | static int |
| 661 | dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 662 | { |
| 663 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 664 | |
| 665 | if (p->phy != NULL) |
| 666 | return phy_ethtool_sset(p->phy, cmd); |
| 667 | |
| 668 | return -EOPNOTSUPP; |
| 669 | } |
| 670 | |
| 671 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 672 | struct ethtool_drvinfo *drvinfo) |
| 673 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 674 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
| 675 | strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version)); |
| 676 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 677 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 680 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 681 | { |
| 682 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 683 | struct dsa_switch *ds = p->parent; |
| 684 | |
| 685 | if (ds->drv->get_regs_len) |
| 686 | return ds->drv->get_regs_len(ds, p->port); |
| 687 | |
| 688 | return -EOPNOTSUPP; |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 693 | { |
| 694 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 695 | struct dsa_switch *ds = p->parent; |
| 696 | |
| 697 | if (ds->drv->get_regs) |
| 698 | ds->drv->get_regs(ds, p->port, regs, _p); |
| 699 | } |
| 700 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 701 | static int dsa_slave_nway_reset(struct net_device *dev) |
| 702 | { |
| 703 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 704 | |
| 705 | if (p->phy != NULL) |
| 706 | return genphy_restart_aneg(p->phy); |
| 707 | |
| 708 | return -EOPNOTSUPP; |
| 709 | } |
| 710 | |
| 711 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 712 | { |
| 713 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 714 | |
| 715 | if (p->phy != NULL) { |
| 716 | genphy_update_link(p->phy); |
| 717 | return p->phy->link; |
| 718 | } |
| 719 | |
| 720 | return -EOPNOTSUPP; |
| 721 | } |
| 722 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 723 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 724 | { |
| 725 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 726 | struct dsa_switch *ds = p->parent; |
| 727 | |
| 728 | if (ds->pd->eeprom_len) |
| 729 | return ds->pd->eeprom_len; |
| 730 | |
| 731 | if (ds->drv->get_eeprom_len) |
| 732 | return ds->drv->get_eeprom_len(ds); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 738 | struct ethtool_eeprom *eeprom, u8 *data) |
| 739 | { |
| 740 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 741 | struct dsa_switch *ds = p->parent; |
| 742 | |
| 743 | if (ds->drv->get_eeprom) |
| 744 | return ds->drv->get_eeprom(ds, eeprom, data); |
| 745 | |
| 746 | return -EOPNOTSUPP; |
| 747 | } |
| 748 | |
| 749 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 750 | struct ethtool_eeprom *eeprom, u8 *data) |
| 751 | { |
| 752 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 753 | struct dsa_switch *ds = p->parent; |
| 754 | |
| 755 | if (ds->drv->set_eeprom) |
| 756 | return ds->drv->set_eeprom(ds, eeprom, data); |
| 757 | |
| 758 | return -EOPNOTSUPP; |
| 759 | } |
| 760 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 761 | static void dsa_slave_get_strings(struct net_device *dev, |
| 762 | uint32_t stringset, uint8_t *data) |
| 763 | { |
| 764 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 765 | struct dsa_switch *ds = p->parent; |
| 766 | |
| 767 | if (stringset == ETH_SS_STATS) { |
| 768 | int len = ETH_GSTRING_LEN; |
| 769 | |
| 770 | strncpy(data, "tx_packets", len); |
| 771 | strncpy(data + len, "tx_bytes", len); |
| 772 | strncpy(data + 2 * len, "rx_packets", len); |
| 773 | strncpy(data + 3 * len, "rx_bytes", len); |
| 774 | if (ds->drv->get_strings != NULL) |
| 775 | ds->drv->get_strings(ds, p->port, data + 4 * len); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 780 | struct ethtool_stats *stats, |
| 781 | uint64_t *data) |
| 782 | { |
| 783 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 784 | struct dsa_switch *ds = p->parent; |
| 785 | |
| 786 | data[0] = p->dev->stats.tx_packets; |
| 787 | data[1] = p->dev->stats.tx_bytes; |
| 788 | data[2] = p->dev->stats.rx_packets; |
| 789 | data[3] = p->dev->stats.rx_bytes; |
| 790 | if (ds->drv->get_ethtool_stats != NULL) |
| 791 | ds->drv->get_ethtool_stats(ds, p->port, data + 4); |
| 792 | } |
| 793 | |
| 794 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 795 | { |
| 796 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 797 | struct dsa_switch *ds = p->parent; |
| 798 | |
| 799 | if (sset == ETH_SS_STATS) { |
| 800 | int count; |
| 801 | |
| 802 | count = 4; |
| 803 | if (ds->drv->get_sset_count != NULL) |
| 804 | count += ds->drv->get_sset_count(ds); |
| 805 | |
| 806 | return count; |
| 807 | } |
| 808 | |
| 809 | return -EOPNOTSUPP; |
| 810 | } |
| 811 | |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 812 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 813 | { |
| 814 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 815 | struct dsa_switch *ds = p->parent; |
| 816 | |
| 817 | if (ds->drv->get_wol) |
| 818 | ds->drv->get_wol(ds, p->port, w); |
| 819 | } |
| 820 | |
| 821 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 822 | { |
| 823 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 824 | struct dsa_switch *ds = p->parent; |
| 825 | int ret = -EOPNOTSUPP; |
| 826 | |
| 827 | if (ds->drv->set_wol) |
| 828 | ret = ds->drv->set_wol(ds, p->port, w); |
| 829 | |
| 830 | return ret; |
| 831 | } |
| 832 | |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 833 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 834 | { |
| 835 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 836 | struct dsa_switch *ds = p->parent; |
| 837 | int ret; |
| 838 | |
| 839 | if (!ds->drv->set_eee) |
| 840 | return -EOPNOTSUPP; |
| 841 | |
| 842 | ret = ds->drv->set_eee(ds, p->port, p->phy, e); |
| 843 | if (ret) |
| 844 | return ret; |
| 845 | |
| 846 | if (p->phy) |
| 847 | ret = phy_ethtool_set_eee(p->phy, e); |
| 848 | |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 853 | { |
| 854 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 855 | struct dsa_switch *ds = p->parent; |
| 856 | int ret; |
| 857 | |
| 858 | if (!ds->drv->get_eee) |
| 859 | return -EOPNOTSUPP; |
| 860 | |
| 861 | ret = ds->drv->get_eee(ds, p->port, e); |
| 862 | if (ret) |
| 863 | return ret; |
| 864 | |
| 865 | if (p->phy) |
| 866 | ret = phy_ethtool_get_eee(p->phy, e); |
| 867 | |
| 868 | return ret; |
| 869 | } |
| 870 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 871 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 872 | static int dsa_slave_netpoll_setup(struct net_device *dev, |
| 873 | struct netpoll_info *ni) |
| 874 | { |
| 875 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 876 | struct dsa_switch *ds = p->parent; |
| 877 | struct net_device *master = ds->dst->master_netdev; |
| 878 | struct netpoll *netpoll; |
| 879 | int err = 0; |
| 880 | |
| 881 | netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); |
| 882 | if (!netpoll) |
| 883 | return -ENOMEM; |
| 884 | |
| 885 | err = __netpoll_setup(netpoll, master); |
| 886 | if (err) { |
| 887 | kfree(netpoll); |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | p->netpoll = netpoll; |
| 892 | out: |
| 893 | return err; |
| 894 | } |
| 895 | |
| 896 | static void dsa_slave_netpoll_cleanup(struct net_device *dev) |
| 897 | { |
| 898 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 899 | struct netpoll *netpoll = p->netpoll; |
| 900 | |
| 901 | if (!netpoll) |
| 902 | return; |
| 903 | |
| 904 | p->netpoll = NULL; |
| 905 | |
| 906 | __netpoll_free_async(netpoll); |
| 907 | } |
| 908 | |
| 909 | static void dsa_slave_poll_controller(struct net_device *dev) |
| 910 | { |
| 911 | } |
| 912 | #endif |
| 913 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 914 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
| 915 | .get_settings = dsa_slave_get_settings, |
| 916 | .set_settings = dsa_slave_set_settings, |
| 917 | .get_drvinfo = dsa_slave_get_drvinfo, |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 918 | .get_regs_len = dsa_slave_get_regs_len, |
| 919 | .get_regs = dsa_slave_get_regs, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 920 | .nway_reset = dsa_slave_nway_reset, |
| 921 | .get_link = dsa_slave_get_link, |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 922 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 923 | .get_eeprom = dsa_slave_get_eeprom, |
| 924 | .set_eeprom = dsa_slave_set_eeprom, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 925 | .get_strings = dsa_slave_get_strings, |
| 926 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 927 | .get_sset_count = dsa_slave_get_sset_count, |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 928 | .set_wol = dsa_slave_set_wol, |
| 929 | .get_wol = dsa_slave_get_wol, |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 930 | .set_eee = dsa_slave_set_eee, |
| 931 | .get_eee = dsa_slave_get_eee, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 932 | }; |
| 933 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 934 | static const struct net_device_ops dsa_slave_netdev_ops = { |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 935 | .ndo_open = dsa_slave_open, |
| 936 | .ndo_stop = dsa_slave_close, |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 937 | .ndo_start_xmit = dsa_slave_xmit, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 938 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 939 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 940 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 941 | .ndo_fdb_add = switchdev_port_fdb_add, |
| 942 | .ndo_fdb_del = switchdev_port_fdb_del, |
| 943 | .ndo_fdb_dump = switchdev_port_fdb_dump, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 944 | .ndo_do_ioctl = dsa_slave_ioctl, |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 945 | .ndo_get_iflink = dsa_slave_get_iflink, |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 946 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 947 | .ndo_netpoll_setup = dsa_slave_netpoll_setup, |
| 948 | .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, |
| 949 | .ndo_poll_controller = dsa_slave_poll_controller, |
| 950 | #endif |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 951 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
| 952 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, |
| 953 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 954 | }; |
| 955 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 956 | static const struct switchdev_ops dsa_slave_switchdev_ops = { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 957 | .switchdev_port_attr_get = dsa_slave_port_attr_get, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 958 | .switchdev_port_attr_set = dsa_slave_port_attr_set, |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 959 | .switchdev_port_obj_add = dsa_slave_port_obj_add, |
| 960 | .switchdev_port_obj_del = dsa_slave_port_obj_del, |
| 961 | .switchdev_port_obj_dump = dsa_slave_port_obj_dump, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 962 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 963 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 964 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 965 | { |
| 966 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 967 | struct dsa_switch *ds = p->parent; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 968 | unsigned int status_changed = 0; |
| 969 | |
| 970 | if (p->old_link != p->phy->link) { |
| 971 | status_changed = 1; |
| 972 | p->old_link = p->phy->link; |
| 973 | } |
| 974 | |
| 975 | if (p->old_duplex != p->phy->duplex) { |
| 976 | status_changed = 1; |
| 977 | p->old_duplex = p->phy->duplex; |
| 978 | } |
| 979 | |
| 980 | if (p->old_pause != p->phy->pause) { |
| 981 | status_changed = 1; |
| 982 | p->old_pause = p->phy->pause; |
| 983 | } |
| 984 | |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 985 | if (ds->drv->adjust_link && status_changed) |
| 986 | ds->drv->adjust_link(ds, p->port, p->phy); |
| 987 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 988 | if (status_changed) |
| 989 | phy_print_status(p->phy); |
| 990 | } |
| 991 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 992 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 993 | struct fixed_phy_status *status) |
| 994 | { |
| 995 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 996 | struct dsa_switch *ds = p->parent; |
| 997 | |
| 998 | if (ds->drv->fixed_link_update) |
| 999 | ds->drv->fixed_link_update(ds, p->port, status); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1004 | /* slave device setup *******************************************************/ |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1005 | static int dsa_slave_phy_connect(struct dsa_slave_priv *p, |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1006 | struct net_device *slave_dev, |
| 1007 | int addr) |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1008 | { |
| 1009 | struct dsa_switch *ds = p->parent; |
| 1010 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1011 | p->phy = ds->slave_mii_bus->phy_map[addr]; |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1012 | if (!p->phy) |
| 1013 | return -ENODEV; |
| 1014 | |
| 1015 | /* Use already configured phy mode */ |
Florian Fainelli | 211c504 | 2015-08-08 12:58:57 -0700 | [diff] [blame] | 1016 | if (p->phy_interface == PHY_INTERFACE_MODE_NA) |
| 1017 | p->phy_interface = p->phy->interface; |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1018 | phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
| 1019 | p->phy_interface); |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1024 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1025 | struct net_device *slave_dev) |
| 1026 | { |
| 1027 | struct dsa_switch *ds = p->parent; |
| 1028 | struct dsa_chip_data *cd = ds->pd; |
| 1029 | struct device_node *phy_dn, *port_dn; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1030 | bool phy_is_fixed = false; |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1031 | u32 phy_flags = 0; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1032 | int mode, ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1033 | |
| 1034 | port_dn = cd->port_dn[p->port]; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1035 | mode = of_get_phy_mode(port_dn); |
| 1036 | if (mode < 0) |
| 1037 | mode = PHY_INTERFACE_MODE_NA; |
| 1038 | p->phy_interface = mode; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1039 | |
| 1040 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
| 1041 | if (of_phy_is_fixed_link(port_dn)) { |
| 1042 | /* In the case of a fixed PHY, the DT node associated |
| 1043 | * to the fixed PHY is the Port DT node |
| 1044 | */ |
| 1045 | ret = of_phy_register_fixed_link(port_dn); |
| 1046 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 1047 | netdev_err(slave_dev, "failed to register fixed PHY\n"); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1048 | return ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1049 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1050 | phy_is_fixed = true; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1051 | phy_dn = port_dn; |
| 1052 | } |
| 1053 | |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1054 | if (ds->drv->get_phy_flags) |
| 1055 | phy_flags = ds->drv->get_phy_flags(ds, p->port); |
| 1056 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1057 | if (phy_dn) { |
| 1058 | ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn); |
| 1059 | /* If this PHY address is part of phys_mii_mask, which means |
| 1060 | * that we need to divert reads and writes to/from it, then we |
| 1061 | * want to bind this device using the slave MII bus created by |
| 1062 | * DSA to make that happen. |
| 1063 | */ |
Florian Fainelli | 96026d0 | 2015-03-14 13:21:59 -0700 | [diff] [blame] | 1064 | if (!phy_is_fixed && ret >= 0 && |
| 1065 | (ds->phys_mii_mask & (1 << ret))) { |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1066 | ret = dsa_slave_phy_connect(p, slave_dev, ret); |
| 1067 | if (ret) |
| 1068 | return ret; |
| 1069 | } else { |
| 1070 | p->phy = of_phy_connect(slave_dev, phy_dn, |
| 1071 | dsa_slave_adjust_link, |
| 1072 | phy_flags, |
| 1073 | p->phy_interface); |
| 1074 | } |
| 1075 | } |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1076 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1077 | if (p->phy && phy_is_fixed) |
| 1078 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); |
| 1079 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1080 | /* We could not connect to a designated PHY, so use the switch internal |
| 1081 | * MDIO bus instead |
| 1082 | */ |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1083 | if (!p->phy) { |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1084 | ret = dsa_slave_phy_connect(p, slave_dev, p->port); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1085 | if (ret) |
| 1086 | return ret; |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1087 | } else { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 1088 | netdev_info(slave_dev, "attached PHY at address %d [%s]\n", |
| 1089 | p->phy->addr, p->phy->drv->name); |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1090 | } |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1091 | |
| 1092 | return 0; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1095 | static struct lock_class_key dsa_slave_netdev_xmit_lock_key; |
| 1096 | static void dsa_slave_set_lockdep_class_one(struct net_device *dev, |
| 1097 | struct netdev_queue *txq, |
| 1098 | void *_unused) |
| 1099 | { |
| 1100 | lockdep_set_class(&txq->_xmit_lock, |
| 1101 | &dsa_slave_netdev_xmit_lock_key); |
| 1102 | } |
| 1103 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1104 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 1105 | { |
| 1106 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1107 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1108 | if (p->phy) { |
| 1109 | phy_stop(p->phy); |
| 1110 | p->old_pause = -1; |
| 1111 | p->old_link = -1; |
| 1112 | p->old_duplex = -1; |
| 1113 | phy_suspend(p->phy); |
| 1114 | } |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | int dsa_slave_resume(struct net_device *slave_dev) |
| 1120 | { |
| 1121 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1122 | |
| 1123 | netif_device_attach(slave_dev); |
| 1124 | |
| 1125 | if (p->phy) { |
| 1126 | phy_resume(p->phy); |
| 1127 | phy_start(p->phy); |
| 1128 | } |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1133 | int dsa_slave_create(struct dsa_switch *ds, struct device *parent, |
| 1134 | int port, char *name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1135 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 1136 | struct net_device *master = ds->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1137 | struct net_device *slave_dev; |
| 1138 | struct dsa_slave_priv *p; |
| 1139 | int ret; |
| 1140 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1141 | slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, |
| 1142 | NET_NAME_UNKNOWN, ether_setup); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1143 | if (slave_dev == NULL) |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1144 | return -ENOMEM; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1145 | |
| 1146 | slave_dev->features = master->vlan_features; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1147 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
Bjørn Mork | 2fcc800 | 2013-08-30 18:08:46 +0200 | [diff] [blame] | 1148 | eth_hw_addr_inherit(slave_dev, master); |
Phil Sutter | 0a5f107 | 2015-08-18 10:30:41 +0200 | [diff] [blame] | 1149 | slave_dev->priv_flags |= IFF_NO_QUEUE; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 1150 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1151 | slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1152 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1153 | netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, |
| 1154 | NULL); |
| 1155 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1156 | SET_NETDEV_DEV(slave_dev, parent); |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 1157 | slave_dev->dev.of_node = ds->pd->port_dn[port]; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1158 | slave_dev->vlan_features = master->vlan_features; |
| 1159 | |
| 1160 | p = netdev_priv(slave_dev); |
| 1161 | p->dev = slave_dev; |
| 1162 | p->parent = ds; |
| 1163 | p->port = port; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1164 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 1165 | switch (ds->dst->tag_protocol) { |
| 1166 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 1167 | case DSA_TAG_PROTO_DSA: |
| 1168 | p->xmit = dsa_netdev_ops.xmit; |
| 1169 | break; |
| 1170 | #endif |
| 1171 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 1172 | case DSA_TAG_PROTO_EDSA: |
| 1173 | p->xmit = edsa_netdev_ops.xmit; |
| 1174 | break; |
| 1175 | #endif |
| 1176 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 1177 | case DSA_TAG_PROTO_TRAILER: |
| 1178 | p->xmit = trailer_netdev_ops.xmit; |
| 1179 | break; |
| 1180 | #endif |
| 1181 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 1182 | case DSA_TAG_PROTO_BRCM: |
| 1183 | p->xmit = brcm_netdev_ops.xmit; |
| 1184 | break; |
| 1185 | #endif |
| 1186 | default: |
| 1187 | p->xmit = dsa_slave_notag_xmit; |
| 1188 | break; |
| 1189 | } |
| 1190 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1191 | p->old_pause = -1; |
| 1192 | p->old_link = -1; |
| 1193 | p->old_duplex = -1; |
| 1194 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1195 | ret = dsa_slave_phy_setup(p, slave_dev); |
| 1196 | if (ret) { |
| 1197 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1198 | return ret; |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1199 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1200 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1201 | ds->ports[port] = slave_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1202 | ret = register_netdev(slave_dev); |
| 1203 | if (ret) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 1204 | netdev_err(master, "error %d registering interface %s\n", |
| 1205 | ret, slave_dev->name); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1206 | phy_disconnect(p->phy); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1207 | ds->ports[port] = NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1208 | free_netdev(slave_dev); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1209 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | netif_carrier_off(slave_dev); |
| 1213 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1214 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1215 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1216 | |
| 1217 | static bool dsa_slave_dev_check(struct net_device *dev) |
| 1218 | { |
| 1219 | return dev->netdev_ops == &dsa_slave_netdev_ops; |
| 1220 | } |
| 1221 | |
| 1222 | static int dsa_slave_master_changed(struct net_device *dev) |
| 1223 | { |
| 1224 | struct net_device *master = netdev_master_upper_dev_get(dev); |
Guenter Roeck | b06b107 | 2015-03-25 08:08:37 -0700 | [diff] [blame] | 1225 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1226 | int err = 0; |
| 1227 | |
| 1228 | if (master && master->rtnl_link_ops && |
| 1229 | !strcmp(master->rtnl_link_ops->kind, "bridge")) |
| 1230 | err = dsa_slave_bridge_port_join(dev, master); |
Guenter Roeck | b06b107 | 2015-03-25 08:08:37 -0700 | [diff] [blame] | 1231 | else if (dsa_port_is_bridged(p)) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1232 | err = dsa_slave_bridge_port_leave(dev); |
| 1233 | |
| 1234 | return err; |
| 1235 | } |
| 1236 | |
| 1237 | int dsa_slave_netdevice_event(struct notifier_block *unused, |
| 1238 | unsigned long event, void *ptr) |
| 1239 | { |
| 1240 | struct net_device *dev; |
| 1241 | int err = 0; |
| 1242 | |
| 1243 | switch (event) { |
| 1244 | case NETDEV_CHANGEUPPER: |
| 1245 | dev = netdev_notifier_info_to_dev(ptr); |
| 1246 | if (!dsa_slave_dev_check(dev)) |
| 1247 | goto out; |
| 1248 | |
| 1249 | err = dsa_slave_master_changed(dev); |
| 1250 | if (err) |
| 1251 | netdev_warn(dev, "failed to reflect master change\n"); |
| 1252 | |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | out: |
| 1257 | return NOTIFY_DONE; |
| 1258 | } |