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