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