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