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