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> |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 18 | #include <linux/mdio.h> |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 19 | #include <net/rtnetlink.h> |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 20 | #include <net/pkt_cls.h> |
| 21 | #include <net/tc_act/tc_mirred.h> |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 22 | #include <linux/if_bridge.h> |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 23 | #include <linux/netpoll.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 24 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 25 | #include "dsa_priv.h" |
| 26 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 27 | static bool dsa_slave_dev_check(struct net_device *dev); |
| 28 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 29 | /* slave mii_bus handling ***************************************************/ |
| 30 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) |
| 31 | { |
| 32 | struct dsa_switch *ds = bus->priv; |
| 33 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 34 | if (ds->phys_mii_mask & (1 << addr)) |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 35 | return ds->ops->phy_read(ds, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 36 | |
| 37 | return 0xffff; |
| 38 | } |
| 39 | |
| 40 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) |
| 41 | { |
| 42 | struct dsa_switch *ds = bus->priv; |
| 43 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 44 | if (ds->phys_mii_mask & (1 << addr)) |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 45 | return ds->ops->phy_write(ds, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) |
| 51 | { |
| 52 | ds->slave_mii_bus->priv = (void *)ds; |
| 53 | ds->slave_mii_bus->name = "dsa slave smi"; |
| 54 | ds->slave_mii_bus->read = dsa_slave_phy_read; |
| 55 | ds->slave_mii_bus->write = dsa_slave_phy_write; |
Florian Fainelli | 0b7b498 | 2016-06-07 16:32:38 -0700 | [diff] [blame] | 56 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 57 | ds->dst->index, ds->index); |
Andrew Lunn | c33063d | 2016-05-10 23:27:23 +0200 | [diff] [blame] | 58 | ds->slave_mii_bus->parent = ds->dev; |
Vivien Didelot | 24df898 | 2015-01-20 19:13:32 -0500 | [diff] [blame] | 59 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | /* slave device handling ****************************************************/ |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 64 | static int dsa_slave_get_iflink(const struct net_device *dev) |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 65 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 66 | return dsa_slave_to_master(dev)->ifindex; |
Lennert Buytenhek | c084080 | 2009-03-20 09:49:49 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 69 | static int dsa_slave_open(struct net_device *dev) |
| 70 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 71 | struct net_device *master = dsa_slave_to_master(dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 72 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 73 | int err; |
| 74 | |
| 75 | if (!(master->flags & IFF_UP)) |
| 76 | return -ENETDOWN; |
| 77 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 78 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 79 | err = dev_uc_add(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 80 | if (err < 0) |
| 81 | goto out; |
| 82 | } |
| 83 | |
| 84 | if (dev->flags & IFF_ALLMULTI) { |
| 85 | err = dev_set_allmulti(master, 1); |
| 86 | if (err < 0) |
| 87 | goto del_unicast; |
| 88 | } |
| 89 | if (dev->flags & IFF_PROMISC) { |
| 90 | err = dev_set_promiscuity(master, 1); |
| 91 | if (err < 0) |
| 92 | goto clear_allmulti; |
| 93 | } |
| 94 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 95 | err = dsa_port_enable(dp, dev->phydev); |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame] | 96 | if (err) |
| 97 | goto clear_promisc; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 98 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 99 | if (dev->phydev) |
| 100 | phy_start(dev->phydev); |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 101 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 102 | return 0; |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 103 | |
Florian Fainelli | b2f2af2 | 2014-09-24 17:05:18 -0700 | [diff] [blame] | 104 | clear_promisc: |
| 105 | if (dev->flags & IFF_PROMISC) |
Gilad Ben-Yossef | 4fdeddf | 2015-06-25 16:50:13 +0300 | [diff] [blame] | 106 | dev_set_promiscuity(master, -1); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 107 | clear_allmulti: |
| 108 | if (dev->flags & IFF_ALLMULTI) |
| 109 | dev_set_allmulti(master, -1); |
| 110 | del_unicast: |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 111 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 112 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 113 | out: |
| 114 | return err; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int dsa_slave_close(struct net_device *dev) |
| 118 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 119 | struct net_device *master = dsa_slave_to_master(dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 120 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 121 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 122 | if (dev->phydev) |
| 123 | phy_stop(dev->phydev); |
Florian Fainelli | f7f1de5 | 2014-09-24 17:05:17 -0700 | [diff] [blame] | 124 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 125 | dsa_port_disable(dp, dev->phydev); |
Vivien Didelot | 6457edf | 2017-09-22 19:01:55 -0400 | [diff] [blame] | 126 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 127 | dev_mc_unsync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 128 | dev_uc_unsync(master, dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 129 | if (dev->flags & IFF_ALLMULTI) |
| 130 | dev_set_allmulti(master, -1); |
| 131 | if (dev->flags & IFF_PROMISC) |
| 132 | dev_set_promiscuity(master, -1); |
| 133 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 134 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 135 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 136 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static void dsa_slave_change_rx_flags(struct net_device *dev, int change) |
| 141 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 142 | struct net_device *master = dsa_slave_to_master(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 143 | |
| 144 | if (change & IFF_ALLMULTI) |
| 145 | dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 146 | if (change & IFF_PROMISC) |
| 147 | dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1); |
| 148 | } |
| 149 | |
| 150 | static void dsa_slave_set_rx_mode(struct net_device *dev) |
| 151 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 152 | struct net_device *master = dsa_slave_to_master(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 153 | |
| 154 | dev_mc_sync(master, dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 155 | dev_uc_sync(master, dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 158 | 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] | 159 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 160 | struct net_device *master = dsa_slave_to_master(dev); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 161 | struct sockaddr *addr = a; |
| 162 | int err; |
| 163 | |
| 164 | if (!is_valid_ether_addr(addr->sa_data)) |
| 165 | return -EADDRNOTAVAIL; |
| 166 | |
| 167 | if (!(dev->flags & IFF_UP)) |
| 168 | goto out; |
| 169 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 170 | if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 171 | err = dev_uc_add(master, addr->sa_data); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 172 | if (err < 0) |
| 173 | return err; |
| 174 | } |
| 175 | |
Joe Perches | 8feedbb | 2012-05-08 18:56:57 +0000 | [diff] [blame] | 176 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 177 | dev_uc_del(master, dev->dev_addr); |
Lennert Buytenhek | df02c6f | 2008-11-10 21:53:12 -0800 | [diff] [blame] | 178 | |
| 179 | out: |
Joe Perches | d08f161 | 2014-01-20 09:52:20 -0800 | [diff] [blame] | 180 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 185 | struct dsa_slave_dump_ctx { |
| 186 | struct net_device *dev; |
| 187 | struct sk_buff *skb; |
| 188 | struct netlink_callback *cb; |
| 189 | int idx; |
| 190 | }; |
| 191 | |
| 192 | static int |
| 193 | dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 194 | bool is_static, void *data) |
| 195 | { |
| 196 | struct dsa_slave_dump_ctx *dump = data; |
| 197 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 198 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 199 | struct nlmsghdr *nlh; |
| 200 | struct ndmsg *ndm; |
| 201 | |
| 202 | if (dump->idx < dump->cb->args[2]) |
| 203 | goto skip; |
| 204 | |
| 205 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 206 | sizeof(*ndm), NLM_F_MULTI); |
| 207 | if (!nlh) |
| 208 | return -EMSGSIZE; |
| 209 | |
| 210 | ndm = nlmsg_data(nlh); |
| 211 | ndm->ndm_family = AF_BRIDGE; |
| 212 | ndm->ndm_pad1 = 0; |
| 213 | ndm->ndm_pad2 = 0; |
| 214 | ndm->ndm_flags = NTF_SELF; |
| 215 | ndm->ndm_type = 0; |
| 216 | ndm->ndm_ifindex = dump->dev->ifindex; |
| 217 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
| 218 | |
| 219 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 220 | goto nla_put_failure; |
| 221 | |
| 222 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
| 223 | goto nla_put_failure; |
| 224 | |
| 225 | nlmsg_end(dump->skb, nlh); |
| 226 | |
| 227 | skip: |
| 228 | dump->idx++; |
| 229 | return 0; |
| 230 | |
| 231 | nla_put_failure: |
| 232 | nlmsg_cancel(dump->skb, nlh); |
| 233 | return -EMSGSIZE; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 238 | struct net_device *dev, struct net_device *filter_dev, |
| 239 | int *idx) |
| 240 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 241 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 242 | struct dsa_slave_dump_ctx dump = { |
| 243 | .dev = dev, |
| 244 | .skb = skb, |
| 245 | .cb = cb, |
| 246 | .idx = *idx, |
| 247 | }; |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 248 | int err; |
| 249 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 250 | err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump); |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 251 | *idx = dump.idx; |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 252 | |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 253 | return err; |
| 254 | } |
| 255 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 256 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 257 | { |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 258 | if (!dev->phydev) |
Vivien Didelot | f4344e0 | 2017-09-26 17:15:31 -0400 | [diff] [blame] | 259 | return -ENODEV; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 260 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 261 | return phy_mii_ioctl(dev->phydev, ifr, cmd); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 264 | static int dsa_slave_port_attr_set(struct net_device *dev, |
Jiri Pirko | f7fadf3 | 2015-10-14 19:40:49 +0200 | [diff] [blame] | 265 | const struct switchdev_attr *attr, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 266 | struct switchdev_trans *trans) |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 267 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 268 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Vivien Didelot | b8d866a | 2015-09-29 12:38:36 -0400 | [diff] [blame] | 269 | int ret; |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 270 | |
| 271 | switch (attr->id) { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 272 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
Vivien Didelot | fd36454 | 2017-05-19 17:00:36 -0400 | [diff] [blame] | 273 | ret = dsa_port_set_state(dp, attr->u.stp_state, trans); |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 274 | break; |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 275 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
Vivien Didelot | c02c417 | 2017-05-19 17:00:42 -0400 | [diff] [blame] | 276 | ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, |
| 277 | trans); |
Vivien Didelot | fb2daba | 2016-02-26 13:16:00 -0500 | [diff] [blame] | 278 | break; |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 279 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
Vivien Didelot | 072bb19 | 2017-05-19 17:00:43 -0400 | [diff] [blame] | 280 | ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); |
Vivien Didelot | 34a79f6 | 2016-07-18 20:45:38 -0400 | [diff] [blame] | 281 | break; |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 282 | default: |
| 283 | ret = -EOPNOTSUPP; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 290 | static int dsa_slave_port_obj_add(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 291 | const struct switchdev_obj *obj, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 292 | struct switchdev_trans *trans) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 293 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 294 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 295 | int err; |
| 296 | |
| 297 | /* For the prepare phase, ensure the full set of changes is feasable in |
| 298 | * one go in order to signal a failure properly. If an operation is not |
| 299 | * supported, return -EOPNOTSUPP. |
| 300 | */ |
| 301 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 302 | switch (obj->id) { |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 303 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 304 | err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 305 | break; |
Andrew Lunn | 5f4dbc5 | 2017-11-09 23:11:00 +0100 | [diff] [blame] | 306 | case SWITCHDEV_OBJ_ID_HOST_MDB: |
| 307 | /* DSA can directly translate this to a normal MDB add, |
| 308 | * but on the CPU port. |
| 309 | */ |
| 310 | err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj), |
| 311 | trans); |
| 312 | break; |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 313 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Vivien Didelot | 01676d12 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 314 | err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), |
| 315 | trans); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 316 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 317 | default: |
| 318 | err = -EOPNOTSUPP; |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | return err; |
| 323 | } |
| 324 | |
| 325 | static int dsa_slave_port_obj_del(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 326 | const struct switchdev_obj *obj) |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 327 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 328 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 329 | int err; |
| 330 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 331 | switch (obj->id) { |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 332 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
Vivien Didelot | bcebb97 | 2017-05-19 17:00:40 -0400 | [diff] [blame] | 333 | err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); |
Vivien Didelot | 8df3025 | 2016-08-31 11:50:03 -0400 | [diff] [blame] | 334 | break; |
Andrew Lunn | 5f4dbc5 | 2017-11-09 23:11:00 +0100 | [diff] [blame] | 335 | case SWITCHDEV_OBJ_ID_HOST_MDB: |
| 336 | /* DSA can directly translate this to a normal MDB add, |
| 337 | * but on the CPU port. |
| 338 | */ |
| 339 | err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj)); |
| 340 | break; |
Jiri Pirko | 57d8083 | 2015-10-01 11:03:41 +0200 | [diff] [blame] | 341 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Vivien Didelot | 01676d12 | 2017-05-19 17:00:41 -0400 | [diff] [blame] | 342 | err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); |
Vivien Didelot | 1114953 | 2015-08-13 12:52:17 -0400 | [diff] [blame] | 343 | break; |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 344 | default: |
| 345 | err = -EOPNOTSUPP; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | return err; |
| 350 | } |
| 351 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 352 | static int dsa_slave_port_attr_get(struct net_device *dev, |
| 353 | struct switchdev_attr *attr) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 354 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 355 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 356 | struct dsa_switch *ds = dp->ds; |
Andrew Lunn | a42c8e3 | 2017-11-09 22:29:51 +0100 | [diff] [blame] | 357 | struct dsa_switch_tree *dst = ds->dst; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 358 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 359 | switch (attr->id) { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 360 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
Andrew Lunn | a42c8e3 | 2017-11-09 22:29:51 +0100 | [diff] [blame] | 361 | attr->u.ppid.id_len = sizeof(dst->index); |
| 362 | memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len); |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 363 | break; |
Arkadi Sharshevsky | c9e2105 | 2017-08-06 16:15:44 +0300 | [diff] [blame] | 364 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT: |
| 365 | attr->u.brport_flags_support = 0; |
| 366 | break; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 367 | default: |
| 368 | return -EOPNOTSUPP; |
| 369 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 374 | static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev, |
| 375 | struct sk_buff *skb) |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 376 | { |
| 377 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 378 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 379 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 380 | if (p->netpoll) |
| 381 | netpoll_send_skb(p->netpoll, skb); |
| 382 | #else |
| 383 | BUG(); |
| 384 | #endif |
| 385 | return NETDEV_TX_OK; |
| 386 | } |
| 387 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 388 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 389 | { |
| 390 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 391 | struct pcpu_sw_netstats *s; |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 392 | struct sk_buff *nskb; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 393 | |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 394 | s = this_cpu_ptr(p->stats64); |
| 395 | u64_stats_update_begin(&s->syncp); |
| 396 | s->tx_packets++; |
| 397 | s->tx_bytes += skb->len; |
| 398 | u64_stats_update_end(&s->syncp); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 399 | |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 400 | /* Transmit function may have to reallocate the original SKB, |
| 401 | * in which case it must have freed it. Only free it here on error. |
| 402 | */ |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 403 | nskb = p->xmit(skb, dev); |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 404 | if (!nskb) { |
| 405 | kfree_skb(skb); |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 406 | return NETDEV_TX_OK; |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 407 | } |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 408 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 409 | /* SKB for netpoll still need to be mangled with the protocol-specific |
| 410 | * tag to be successfully transmitted |
| 411 | */ |
| 412 | if (unlikely(netpoll_tx_running(dev))) |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 413 | return dsa_slave_netpoll_send_skb(dev, nskb); |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 414 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 415 | /* Queue the SKB for transmission on the parent interface, but |
| 416 | * do not modify its EtherType |
| 417 | */ |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 418 | nskb->dev = dsa_slave_to_master(dev); |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 419 | dev_queue_xmit(nskb); |
Florian Fainelli | 5aed85c | 2014-08-27 17:04:52 -0700 | [diff] [blame] | 420 | |
| 421 | return NETDEV_TX_OK; |
| 422 | } |
| 423 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 424 | /* ethtool operations *******************************************************/ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 425 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 426 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 427 | struct ethtool_drvinfo *drvinfo) |
| 428 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 429 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 430 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 431 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 434 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 435 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 436 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 437 | struct dsa_switch *ds = dp->ds; |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 438 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 439 | if (ds->ops->get_regs_len) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 440 | return ds->ops->get_regs_len(ds, dp->index); |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 441 | |
| 442 | return -EOPNOTSUPP; |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 447 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 448 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 449 | struct dsa_switch *ds = dp->ds; |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 450 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 451 | if (ds->ops->get_regs) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 452 | ds->ops->get_regs(ds, dp->index, regs, _p); |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 455 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 456 | { |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 457 | if (!dev->phydev) |
Vivien Didelot | f4344e0 | 2017-09-26 17:15:31 -0400 | [diff] [blame] | 458 | return -ENODEV; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 459 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 460 | genphy_update_link(dev->phydev); |
Vivien Didelot | f4344e0 | 2017-09-26 17:15:31 -0400 | [diff] [blame] | 461 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 462 | return dev->phydev->link; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 465 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 466 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 467 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 468 | struct dsa_switch *ds = dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 469 | |
Andrew Lunn | 0e57604 | 2016-06-04 21:16:52 +0200 | [diff] [blame] | 470 | if (ds->cd && ds->cd->eeprom_len) |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 471 | return ds->cd->eeprom_len; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 472 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 473 | if (ds->ops->get_eeprom_len) |
| 474 | return ds->ops->get_eeprom_len(ds); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 480 | struct ethtool_eeprom *eeprom, u8 *data) |
| 481 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 482 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 483 | struct dsa_switch *ds = dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 484 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 485 | if (ds->ops->get_eeprom) |
| 486 | return ds->ops->get_eeprom(ds, eeprom, data); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 487 | |
| 488 | return -EOPNOTSUPP; |
| 489 | } |
| 490 | |
| 491 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 492 | struct ethtool_eeprom *eeprom, u8 *data) |
| 493 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 494 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 495 | struct dsa_switch *ds = dp->ds; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 496 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 497 | if (ds->ops->set_eeprom) |
| 498 | return ds->ops->set_eeprom(ds, eeprom, data); |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 499 | |
| 500 | return -EOPNOTSUPP; |
| 501 | } |
| 502 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 503 | static void dsa_slave_get_strings(struct net_device *dev, |
| 504 | uint32_t stringset, uint8_t *data) |
| 505 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 506 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 507 | struct dsa_switch *ds = dp->ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 508 | |
| 509 | if (stringset == ETH_SS_STATS) { |
| 510 | int len = ETH_GSTRING_LEN; |
| 511 | |
| 512 | strncpy(data, "tx_packets", len); |
| 513 | strncpy(data + len, "tx_bytes", len); |
| 514 | strncpy(data + 2 * len, "rx_packets", len); |
| 515 | strncpy(data + 3 * len, "rx_bytes", len); |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 516 | if (ds->ops->get_strings) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 517 | ds->ops->get_strings(ds, dp->index, data + 4 * len); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 522 | struct ethtool_stats *stats, |
| 523 | uint64_t *data) |
| 524 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 525 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 526 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 527 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 528 | struct pcpu_sw_netstats *s; |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 529 | unsigned int start; |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 530 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 531 | |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 532 | for_each_possible_cpu(i) { |
| 533 | u64 tx_packets, tx_bytes, rx_packets, rx_bytes; |
| 534 | |
| 535 | s = per_cpu_ptr(p->stats64, i); |
| 536 | do { |
| 537 | start = u64_stats_fetch_begin_irq(&s->syncp); |
| 538 | tx_packets = s->tx_packets; |
| 539 | tx_bytes = s->tx_bytes; |
| 540 | rx_packets = s->rx_packets; |
| 541 | rx_bytes = s->rx_bytes; |
| 542 | } while (u64_stats_fetch_retry_irq(&s->syncp, start)); |
| 543 | data[0] += tx_packets; |
| 544 | data[1] += tx_bytes; |
| 545 | data[2] += rx_packets; |
| 546 | data[3] += rx_bytes; |
| 547 | } |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 548 | if (ds->ops->get_ethtool_stats) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 549 | ds->ops->get_ethtool_stats(ds, dp->index, data + 4); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 553 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 554 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 555 | struct dsa_switch *ds = dp->ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 556 | |
| 557 | if (sset == ETH_SS_STATS) { |
| 558 | int count; |
| 559 | |
| 560 | count = 4; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 561 | if (ds->ops->get_sset_count) |
| 562 | count += ds->ops->get_sset_count(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 563 | |
| 564 | return count; |
| 565 | } |
| 566 | |
| 567 | return -EOPNOTSUPP; |
| 568 | } |
| 569 | |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 570 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 571 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 572 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 573 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 574 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 575 | if (ds->ops->get_wol) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 576 | ds->ops->get_wol(ds, dp->index, w); |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 580 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 581 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 582 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 583 | int ret = -EOPNOTSUPP; |
| 584 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 585 | if (ds->ops->set_wol) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 586 | ret = ds->ops->set_wol(ds, dp->index, w); |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 587 | |
| 588 | return ret; |
| 589 | } |
| 590 | |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 591 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 592 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 593 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 594 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 595 | int ret; |
| 596 | |
Vivien Didelot | 7b9cc73 | 2017-08-01 16:32:31 -0400 | [diff] [blame] | 597 | /* Port's PHY and MAC both need to be EEE capable */ |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 598 | if (!dev->phydev) |
Vivien Didelot | 7b9cc73 | 2017-08-01 16:32:31 -0400 | [diff] [blame] | 599 | return -ENODEV; |
| 600 | |
Vivien Didelot | 08f5006 | 2017-08-01 16:32:41 -0400 | [diff] [blame] | 601 | if (!ds->ops->set_mac_eee) |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 602 | return -EOPNOTSUPP; |
| 603 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 604 | ret = ds->ops->set_mac_eee(ds, dp->index, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 605 | if (ret) |
| 606 | return ret; |
| 607 | |
Vivien Didelot | c48f7eb | 2017-08-01 16:32:38 -0400 | [diff] [blame] | 608 | if (e->eee_enabled) { |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 609 | ret = phy_init_eee(dev->phydev, 0); |
Vivien Didelot | c48f7eb | 2017-08-01 16:32:38 -0400 | [diff] [blame] | 610 | if (ret) |
| 611 | return ret; |
| 612 | } |
| 613 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 614 | return phy_ethtool_set_eee(dev->phydev, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 618 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 619 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 620 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 621 | int ret; |
| 622 | |
Vivien Didelot | 7b9cc73 | 2017-08-01 16:32:31 -0400 | [diff] [blame] | 623 | /* Port's PHY and MAC both need to be EEE capable */ |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 624 | if (!dev->phydev) |
Vivien Didelot | 7b9cc73 | 2017-08-01 16:32:31 -0400 | [diff] [blame] | 625 | return -ENODEV; |
| 626 | |
Vivien Didelot | 08f5006 | 2017-08-01 16:32:41 -0400 | [diff] [blame] | 627 | if (!ds->ops->get_mac_eee) |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 628 | return -EOPNOTSUPP; |
| 629 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 630 | ret = ds->ops->get_mac_eee(ds, dp->index, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 631 | if (ret) |
| 632 | return ret; |
| 633 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 634 | return phy_ethtool_get_eee(dev->phydev, e); |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 637 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 638 | static int dsa_slave_netpoll_setup(struct net_device *dev, |
| 639 | struct netpoll_info *ni) |
| 640 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 641 | struct net_device *master = dsa_slave_to_master(dev); |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 642 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 643 | struct netpoll *netpoll; |
| 644 | int err = 0; |
| 645 | |
| 646 | netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); |
| 647 | if (!netpoll) |
| 648 | return -ENOMEM; |
| 649 | |
| 650 | err = __netpoll_setup(netpoll, master); |
| 651 | if (err) { |
| 652 | kfree(netpoll); |
| 653 | goto out; |
| 654 | } |
| 655 | |
| 656 | p->netpoll = netpoll; |
| 657 | out: |
| 658 | return err; |
| 659 | } |
| 660 | |
| 661 | static void dsa_slave_netpoll_cleanup(struct net_device *dev) |
| 662 | { |
| 663 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 664 | struct netpoll *netpoll = p->netpoll; |
| 665 | |
| 666 | if (!netpoll) |
| 667 | return; |
| 668 | |
| 669 | p->netpoll = NULL; |
| 670 | |
| 671 | __netpoll_free_async(netpoll); |
| 672 | } |
| 673 | |
| 674 | static void dsa_slave_poll_controller(struct net_device *dev) |
| 675 | { |
| 676 | } |
| 677 | #endif |
| 678 | |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 679 | static int dsa_slave_get_phys_port_name(struct net_device *dev, |
| 680 | char *name, size_t len) |
| 681 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 682 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 683 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 684 | if (snprintf(name, len, "p%d", dp->index) >= len) |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 685 | return -EINVAL; |
Florian Fainelli | 3a543ef | 2016-12-29 14:20:56 -0800 | [diff] [blame] | 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 690 | static struct dsa_mall_tc_entry * |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 691 | dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie) |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 692 | { |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 693 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 694 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 695 | |
| 696 | list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) |
| 697 | if (mall_tc_entry->cookie == cookie) |
| 698 | return mall_tc_entry; |
| 699 | |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | static int dsa_slave_add_cls_matchall(struct net_device *dev, |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 704 | struct tc_cls_matchall_offload *cls, |
| 705 | bool ingress) |
| 706 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 707 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 708 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 709 | struct dsa_mall_tc_entry *mall_tc_entry; |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame] | 710 | __be16 protocol = cls->common.protocol; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 711 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 712 | struct net_device *to_dev; |
| 713 | const struct tc_action *a; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 714 | struct dsa_port *to_dp; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 715 | int err = -EOPNOTSUPP; |
| 716 | LIST_HEAD(actions); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 717 | |
| 718 | if (!ds->ops->port_mirror_add) |
| 719 | return err; |
| 720 | |
Jiri Pirko | 3bcc0ce | 2017-08-04 14:28:58 +0200 | [diff] [blame] | 721 | if (!tcf_exts_has_one_action(cls->exts)) |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 722 | return err; |
| 723 | |
| 724 | tcf_exts_to_list(cls->exts, &actions); |
| 725 | a = list_first_entry(&actions, struct tc_action, list); |
| 726 | |
| 727 | if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { |
| 728 | struct dsa_mall_mirror_tc_entry *mirror; |
| 729 | |
Cong Wang | 9f8a739 | 2017-12-05 16:17:26 -0800 | [diff] [blame] | 730 | to_dev = tcf_mirred_dev(a); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 731 | if (!to_dev) |
| 732 | return -EINVAL; |
| 733 | |
| 734 | if (!dsa_slave_dev_check(to_dev)) |
| 735 | return -EOPNOTSUPP; |
| 736 | |
| 737 | mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); |
| 738 | if (!mall_tc_entry) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | mall_tc_entry->cookie = cls->cookie; |
| 742 | mall_tc_entry->type = DSA_PORT_MALL_MIRROR; |
| 743 | mirror = &mall_tc_entry->mirror; |
| 744 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 745 | to_dp = dsa_slave_to_port(to_dev); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 746 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 747 | mirror->to_local_port = to_dp->index; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 748 | mirror->ingress = ingress; |
| 749 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 750 | err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 751 | if (err) { |
| 752 | kfree(mall_tc_entry); |
| 753 | return err; |
| 754 | } |
| 755 | |
| 756 | list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static void dsa_slave_del_cls_matchall(struct net_device *dev, |
| 763 | struct tc_cls_matchall_offload *cls) |
| 764 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 765 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 766 | struct dsa_mall_tc_entry *mall_tc_entry; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 767 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 768 | |
| 769 | if (!ds->ops->port_mirror_del) |
| 770 | return; |
| 771 | |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 772 | mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 773 | if (!mall_tc_entry) |
| 774 | return; |
| 775 | |
| 776 | list_del(&mall_tc_entry->list); |
| 777 | |
| 778 | switch (mall_tc_entry->type) { |
| 779 | case DSA_PORT_MALL_MIRROR: |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 780 | ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 781 | break; |
| 782 | default: |
| 783 | WARN_ON(1); |
| 784 | } |
| 785 | |
| 786 | kfree(mall_tc_entry); |
| 787 | } |
| 788 | |
Jiri Pirko | 3fbae38 | 2017-08-07 10:15:26 +0200 | [diff] [blame] | 789 | static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, |
Jiri Pirko | 6b3eb75 | 2017-10-19 15:50:45 +0200 | [diff] [blame] | 790 | struct tc_cls_matchall_offload *cls, |
| 791 | bool ingress) |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 792 | { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame] | 793 | if (cls->common.chain_index) |
Jiri Pirko | a5fcf8a | 2017-06-06 17:00:16 +0200 | [diff] [blame] | 794 | return -EOPNOTSUPP; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 795 | |
Jiri Pirko | 3fbae38 | 2017-08-07 10:15:26 +0200 | [diff] [blame] | 796 | switch (cls->command) { |
| 797 | case TC_CLSMATCHALL_REPLACE: |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame] | 798 | return dsa_slave_add_cls_matchall(dev, cls, ingress); |
Jiri Pirko | 3fbae38 | 2017-08-07 10:15:26 +0200 | [diff] [blame] | 799 | case TC_CLSMATCHALL_DESTROY: |
| 800 | dsa_slave_del_cls_matchall(dev, cls); |
| 801 | return 0; |
| 802 | default: |
| 803 | return -EOPNOTSUPP; |
| 804 | } |
| 805 | } |
| 806 | |
Jiri Pirko | 6b3eb75 | 2017-10-19 15:50:45 +0200 | [diff] [blame] | 807 | static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data, |
| 808 | void *cb_priv, bool ingress) |
| 809 | { |
| 810 | struct net_device *dev = cb_priv; |
| 811 | |
Jiri Pirko | 44ae12a | 2017-11-01 11:47:39 +0100 | [diff] [blame] | 812 | if (!tc_can_offload(dev)) |
| 813 | return -EOPNOTSUPP; |
| 814 | |
Jiri Pirko | 6b3eb75 | 2017-10-19 15:50:45 +0200 | [diff] [blame] | 815 | switch (type) { |
| 816 | case TC_SETUP_CLSMATCHALL: |
| 817 | return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress); |
| 818 | default: |
| 819 | return -EOPNOTSUPP; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type, |
| 824 | void *type_data, void *cb_priv) |
| 825 | { |
| 826 | return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true); |
| 827 | } |
| 828 | |
| 829 | static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type, |
| 830 | void *type_data, void *cb_priv) |
| 831 | { |
| 832 | return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false); |
| 833 | } |
| 834 | |
| 835 | static int dsa_slave_setup_tc_block(struct net_device *dev, |
| 836 | struct tc_block_offload *f) |
| 837 | { |
| 838 | tc_setup_cb_t *cb; |
| 839 | |
| 840 | if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) |
| 841 | cb = dsa_slave_setup_tc_block_cb_ig; |
| 842 | else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
| 843 | cb = dsa_slave_setup_tc_block_cb_eg; |
| 844 | else |
| 845 | return -EOPNOTSUPP; |
| 846 | |
| 847 | switch (f->command) { |
| 848 | case TC_BLOCK_BIND: |
| 849 | return tcf_block_cb_register(f->block, cb, dev, dev); |
| 850 | case TC_BLOCK_UNBIND: |
| 851 | tcf_block_cb_unregister(f->block, cb, dev); |
| 852 | return 0; |
| 853 | default: |
| 854 | return -EOPNOTSUPP; |
| 855 | } |
| 856 | } |
| 857 | |
Jiri Pirko | 3fbae38 | 2017-08-07 10:15:26 +0200 | [diff] [blame] | 858 | static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 859 | void *type_data) |
Jiri Pirko | 3fbae38 | 2017-08-07 10:15:26 +0200 | [diff] [blame] | 860 | { |
Jiri Pirko | 2572ac5 | 2017-08-07 10:15:17 +0200 | [diff] [blame] | 861 | switch (type) { |
Jiri Pirko | 6b3eb75 | 2017-10-19 15:50:45 +0200 | [diff] [blame] | 862 | case TC_SETUP_BLOCK: |
| 863 | return dsa_slave_setup_tc_block(dev, type_data); |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 864 | default: |
Jiri Pirko | a5fcf8a | 2017-06-06 17:00:16 +0200 | [diff] [blame] | 865 | return -EOPNOTSUPP; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 866 | } |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 869 | static void dsa_slave_get_stats64(struct net_device *dev, |
| 870 | struct rtnl_link_stats64 *stats) |
| 871 | { |
| 872 | struct dsa_slave_priv *p = netdev_priv(dev); |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 873 | struct pcpu_sw_netstats *s; |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 874 | unsigned int start; |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 875 | int i; |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 876 | |
| 877 | netdev_stats_to_stats64(stats, &dev->stats); |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 878 | for_each_possible_cpu(i) { |
| 879 | u64 tx_packets, tx_bytes, rx_packets, rx_bytes; |
| 880 | |
| 881 | s = per_cpu_ptr(p->stats64, i); |
| 882 | do { |
| 883 | start = u64_stats_fetch_begin_irq(&s->syncp); |
| 884 | tx_packets = s->tx_packets; |
| 885 | tx_bytes = s->tx_bytes; |
| 886 | rx_packets = s->rx_packets; |
| 887 | rx_bytes = s->rx_bytes; |
| 888 | } while (u64_stats_fetch_retry_irq(&s->syncp, start)); |
| 889 | |
| 890 | stats->tx_packets += tx_packets; |
| 891 | stats->tx_bytes += tx_bytes; |
| 892 | stats->rx_packets += rx_packets; |
| 893 | stats->rx_bytes += rx_bytes; |
| 894 | } |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 897 | static int dsa_slave_get_rxnfc(struct net_device *dev, |
| 898 | struct ethtool_rxnfc *nfc, u32 *rule_locs) |
| 899 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 900 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 901 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 902 | |
| 903 | if (!ds->ops->get_rxnfc) |
| 904 | return -EOPNOTSUPP; |
| 905 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 906 | return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs); |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int dsa_slave_set_rxnfc(struct net_device *dev, |
| 910 | struct ethtool_rxnfc *nfc) |
| 911 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 912 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 913 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 914 | |
| 915 | if (!ds->ops->set_rxnfc) |
| 916 | return -EOPNOTSUPP; |
| 917 | |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 918 | return ds->ops->set_rxnfc(ds, dp->index, nfc); |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 921 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 922 | .get_drvinfo = dsa_slave_get_drvinfo, |
Guenter Roeck | 3d762a0 | 2014-10-29 10:45:04 -0700 | [diff] [blame] | 923 | .get_regs_len = dsa_slave_get_regs_len, |
| 924 | .get_regs = dsa_slave_get_regs, |
Vivien Didelot | 69b2c162 | 2017-09-26 17:15:35 -0400 | [diff] [blame] | 925 | .nway_reset = phy_ethtool_nway_reset, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 926 | .get_link = dsa_slave_get_link, |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 927 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 928 | .get_eeprom = dsa_slave_get_eeprom, |
| 929 | .set_eeprom = dsa_slave_set_eeprom, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 930 | .get_strings = dsa_slave_get_strings, |
| 931 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 932 | .get_sset_count = dsa_slave_get_sset_count, |
Florian Fainelli | 19e57c4 | 2014-09-18 17:31:24 -0700 | [diff] [blame] | 933 | .set_wol = dsa_slave_set_wol, |
| 934 | .get_wol = dsa_slave_get_wol, |
Florian Fainelli | 7905288 | 2014-09-24 17:05:21 -0700 | [diff] [blame] | 935 | .set_eee = dsa_slave_set_eee, |
| 936 | .get_eee = dsa_slave_get_eee, |
Vivien Didelot | 771df31 | 2017-09-26 17:15:33 -0400 | [diff] [blame] | 937 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
Vivien Didelot | aa62a8c | 2017-09-26 17:15:34 -0400 | [diff] [blame] | 938 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Florian Fainelli | bf9f264 | 2017-01-30 09:48:40 -0800 | [diff] [blame] | 939 | .get_rxnfc = dsa_slave_get_rxnfc, |
| 940 | .set_rxnfc = dsa_slave_set_rxnfc, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 941 | }; |
| 942 | |
Florian Fainelli | 2a93c1a | 2017-12-06 15:03:33 -0800 | [diff] [blame] | 943 | /* legacy way, bypassing the bridge *****************************************/ |
| 944 | int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 945 | struct net_device *dev, |
| 946 | const unsigned char *addr, u16 vid, |
| 947 | u16 flags) |
| 948 | { |
| 949 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 950 | |
| 951 | return dsa_port_fdb_add(dp, addr, vid); |
| 952 | } |
| 953 | |
| 954 | int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 955 | struct net_device *dev, |
| 956 | const unsigned char *addr, u16 vid) |
| 957 | { |
| 958 | struct dsa_port *dp = dsa_slave_to_port(dev); |
| 959 | |
| 960 | return dsa_port_fdb_del(dp, addr, vid); |
| 961 | } |
| 962 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 963 | static const struct net_device_ops dsa_slave_netdev_ops = { |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 964 | .ndo_open = dsa_slave_open, |
| 965 | .ndo_stop = dsa_slave_close, |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 966 | .ndo_start_xmit = dsa_slave_xmit, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 967 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 968 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 969 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
Arkadi Sharshevsky | 37b8da1 | 2017-08-06 16:15:43 +0300 | [diff] [blame] | 970 | .ndo_fdb_add = dsa_legacy_fdb_add, |
| 971 | .ndo_fdb_del = dsa_legacy_fdb_del, |
Arkadi Sharshevsky | 2bedde1 | 2017-08-06 16:15:49 +0300 | [diff] [blame] | 972 | .ndo_fdb_dump = dsa_slave_fdb_dump, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 973 | .ndo_do_ioctl = dsa_slave_ioctl, |
Nicolas Dichtel | abd2be0 | 2015-04-02 17:07:08 +0200 | [diff] [blame] | 974 | .ndo_get_iflink = dsa_slave_get_iflink, |
Florian Fainelli | 04ff53f | 2015-07-31 11:42:57 -0700 | [diff] [blame] | 975 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 976 | .ndo_netpoll_setup = dsa_slave_netpoll_setup, |
| 977 | .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, |
| 978 | .ndo_poll_controller = dsa_slave_poll_controller, |
| 979 | #endif |
Florian Fainelli | 44bb765 | 2017-01-10 12:32:36 -0800 | [diff] [blame] | 980 | .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 981 | .ndo_setup_tc = dsa_slave_setup_tc, |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 982 | .ndo_get_stats64 = dsa_slave_get_stats64, |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 983 | }; |
| 984 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 985 | static const struct switchdev_ops dsa_slave_switchdev_ops = { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 986 | .switchdev_port_attr_get = dsa_slave_port_attr_get, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 987 | .switchdev_port_attr_set = dsa_slave_port_attr_set, |
Vivien Didelot | ba14d9e | 2015-08-10 09:09:53 -0400 | [diff] [blame] | 988 | .switchdev_port_obj_add = dsa_slave_port_obj_add, |
| 989 | .switchdev_port_obj_del = dsa_slave_port_obj_del, |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 990 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 991 | |
Florian Fainelli | f37db85 | 2015-09-23 18:19:58 -0700 | [diff] [blame] | 992 | static struct device_type dsa_type = { |
| 993 | .name = "dsa", |
| 994 | }; |
| 995 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 996 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 997 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 998 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 999 | struct dsa_slave_priv *p = netdev_priv(dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1000 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1001 | unsigned int status_changed = 0; |
| 1002 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1003 | if (p->old_link != dev->phydev->link) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1004 | status_changed = 1; |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1005 | p->old_link = dev->phydev->link; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1008 | if (p->old_duplex != dev->phydev->duplex) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1009 | status_changed = 1; |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1010 | p->old_duplex = dev->phydev->duplex; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1013 | if (p->old_pause != dev->phydev->pause) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1014 | status_changed = 1; |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1015 | p->old_pause = dev->phydev->pause; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1018 | if (ds->ops->adjust_link && status_changed) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1019 | ds->ops->adjust_link(ds, dp->index, dev->phydev); |
Florian Fainelli | ec9436b | 2014-08-27 17:04:53 -0700 | [diff] [blame] | 1020 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1021 | if (status_changed) |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1022 | phy_print_status(dev->phydev); |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1025 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 1026 | struct fixed_phy_status *status) |
| 1027 | { |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1028 | struct dsa_switch *ds; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1029 | struct dsa_port *dp; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1030 | |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1031 | if (dev) { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1032 | dp = dsa_slave_to_port(dev); |
| 1033 | ds = dp->ds; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1034 | if (ds->ops->fixed_link_update) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1035 | ds->ops->fixed_link_update(ds, dp->index, status); |
Andrew Lunn | b71be35 | 2016-03-12 00:01:37 +0100 | [diff] [blame] | 1036 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1041 | /* slave device setup *******************************************************/ |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 1042 | static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1043 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1044 | struct dsa_port *dp = dsa_slave_to_port(slave_dev); |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 1045 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1046 | struct dsa_switch *ds = dp->ds; |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1047 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1048 | slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); |
| 1049 | if (!slave_dev->phydev) { |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1050 | netdev_err(slave_dev, "no phy at %d\n", addr); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1051 | return -ENODEV; |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1052 | } |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* Use already configured phy mode */ |
Florian Fainelli | 211c504 | 2015-08-08 12:58:57 -0700 | [diff] [blame] | 1055 | if (p->phy_interface == PHY_INTERFACE_MODE_NA) |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1056 | p->phy_interface = slave_dev->phydev->interface; |
| 1057 | |
| 1058 | return phy_connect_direct(slave_dev, slave_dev->phydev, |
| 1059 | dsa_slave_adjust_link, p->phy_interface); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 1062 | static int dsa_slave_phy_setup(struct net_device *slave_dev) |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1063 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1064 | struct dsa_port *dp = dsa_slave_to_port(slave_dev); |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 1065 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1066 | struct device_node *port_dn = dp->dn; |
| 1067 | struct dsa_switch *ds = dp->ds; |
| 1068 | struct device_node *phy_dn; |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1069 | bool phy_is_fixed = false; |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1070 | u32 phy_flags = 0; |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1071 | int mode, ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1072 | |
Guenter Roeck | 1933492 | 2015-02-16 21:23:51 -0800 | [diff] [blame] | 1073 | mode = of_get_phy_mode(port_dn); |
| 1074 | if (mode < 0) |
| 1075 | mode = PHY_INTERFACE_MODE_NA; |
| 1076 | p->phy_interface = mode; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1077 | |
| 1078 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1079 | if (!phy_dn && of_phy_is_fixed_link(port_dn)) { |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1080 | /* In the case of a fixed PHY, the DT node associated |
| 1081 | * to the fixed PHY is the Port DT node |
| 1082 | */ |
| 1083 | ret = of_phy_register_fixed_link(port_dn); |
| 1084 | if (ret) { |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1085 | netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1086 | return ret; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1087 | } |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1088 | phy_is_fixed = true; |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1089 | phy_dn = of_node_get(port_dn); |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 1092 | if (ds->ops->get_phy_flags) |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1093 | phy_flags = ds->ops->get_phy_flags(ds, dp->index); |
Florian Fainelli | 6819563 | 2014-09-19 13:07:54 -0700 | [diff] [blame] | 1094 | |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1095 | if (phy_dn) { |
Florian Fainelli | 399ba77 | 2017-10-25 17:32:05 -0700 | [diff] [blame] | 1096 | slave_dev->phydev = of_phy_connect(slave_dev, phy_dn, |
| 1097 | dsa_slave_adjust_link, |
| 1098 | phy_flags, |
| 1099 | p->phy_interface); |
Johan Hovold | 0d8f3c6 | 2016-11-28 19:24:54 +0100 | [diff] [blame] | 1100 | of_node_put(phy_dn); |
Florian Fainelli | cd28a1a | 2015-03-10 16:57:13 -0700 | [diff] [blame] | 1101 | } |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1102 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1103 | if (slave_dev->phydev && phy_is_fixed) |
| 1104 | fixed_phy_set_link_update(slave_dev->phydev, |
| 1105 | dsa_slave_fixed_link_update); |
Florian Fainelli | ce31b31 | 2014-08-27 17:04:54 -0700 | [diff] [blame] | 1106 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1107 | /* We could not connect to a designated PHY, so use the switch internal |
| 1108 | * MDIO bus instead |
| 1109 | */ |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1110 | if (!slave_dev->phydev) { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1111 | ret = dsa_slave_phy_connect(slave_dev, dp->index); |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1112 | if (ret) { |
Vivien Didelot | afdcf15 | 2017-01-27 15:29:39 -0500 | [diff] [blame] | 1113 | netdev_err(slave_dev, "failed to connect to port %d: %d\n", |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1114 | dp->index, ret); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1115 | if (phy_is_fixed) |
| 1116 | of_phy_deregister_fixed_link(port_dn); |
Florian Fainelli | c305c16 | 2015-03-10 16:57:12 -0700 | [diff] [blame] | 1117 | return ret; |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 1118 | } |
Andrew Lunn | b31f65f | 2014-11-05 19:47:28 +0100 | [diff] [blame] | 1119 | } |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1120 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1121 | phy_attached_info(slave_dev->phydev); |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1122 | |
Florian Fainelli | 9697f1c | 2014-12-11 12:49:16 -0800 | [diff] [blame] | 1123 | return 0; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1126 | static struct lock_class_key dsa_slave_netdev_xmit_lock_key; |
| 1127 | static void dsa_slave_set_lockdep_class_one(struct net_device *dev, |
| 1128 | struct netdev_queue *txq, |
| 1129 | void *_unused) |
| 1130 | { |
| 1131 | lockdep_set_class(&txq->_xmit_lock, |
| 1132 | &dsa_slave_netdev_xmit_lock_key); |
| 1133 | } |
| 1134 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1135 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 1136 | { |
| 1137 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1138 | |
Florian Fainelli | f154be2 | 2017-01-25 09:10:41 -0800 | [diff] [blame] | 1139 | netif_device_detach(slave_dev); |
| 1140 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1141 | if (slave_dev->phydev) { |
| 1142 | phy_stop(slave_dev->phydev); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1143 | p->old_pause = -1; |
| 1144 | p->old_link = -1; |
| 1145 | p->old_duplex = -1; |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1146 | phy_suspend(slave_dev->phydev); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | int dsa_slave_resume(struct net_device *slave_dev) |
| 1153 | { |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1154 | netif_device_attach(slave_dev); |
| 1155 | |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1156 | if (slave_dev->phydev) { |
| 1157 | phy_resume(slave_dev->phydev); |
| 1158 | phy_start(slave_dev->phydev); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Vivien Didelot | 6158eaa | 2017-10-16 11:12:14 -0400 | [diff] [blame] | 1164 | static void dsa_slave_notify(struct net_device *dev, unsigned long val) |
| 1165 | { |
Vivien Didelot | d0006b0 | 2017-10-16 11:12:16 -0400 | [diff] [blame] | 1166 | struct net_device *master = dsa_slave_to_master(dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1167 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Vivien Didelot | 6158eaa | 2017-10-16 11:12:14 -0400 | [diff] [blame] | 1168 | struct dsa_notifier_register_info rinfo = { |
| 1169 | .switch_number = dp->ds->index, |
| 1170 | .port_number = dp->index, |
| 1171 | .master = master, |
| 1172 | .info.dev = dev, |
| 1173 | }; |
| 1174 | |
| 1175 | call_dsa_notifiers(val, dev, &rinfo.info); |
| 1176 | } |
| 1177 | |
Vivien Didelot | 951259aa | 2017-10-27 15:55:19 -0400 | [diff] [blame] | 1178 | int dsa_slave_create(struct dsa_port *port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1179 | { |
Vivien Didelot | 24a9332 | 2017-11-06 16:11:43 -0500 | [diff] [blame] | 1180 | const struct dsa_port *cpu_dp = port->cpu_dp; |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 1181 | struct net_device *master = cpu_dp->master; |
Vivien Didelot | 4cfbf09 | 2017-08-05 16:20:19 -0400 | [diff] [blame] | 1182 | struct dsa_switch *ds = port->ds; |
Vivien Didelot | 951259aa | 2017-10-27 15:55:19 -0400 | [diff] [blame] | 1183 | const char *name = port->name; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1184 | struct net_device *slave_dev; |
| 1185 | struct dsa_slave_priv *p; |
| 1186 | int ret; |
| 1187 | |
Florian Fainelli | 55199df | 2017-09-03 20:27:00 -0700 | [diff] [blame] | 1188 | if (!ds->num_tx_queues) |
| 1189 | ds->num_tx_queues = 1; |
| 1190 | |
| 1191 | slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, |
| 1192 | NET_NAME_UNKNOWN, ether_setup, |
| 1193 | ds->num_tx_queues, 1); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1194 | if (slave_dev == NULL) |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1195 | return -ENOMEM; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1196 | |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1197 | slave_dev->features = master->vlan_features | NETIF_F_HW_TC; |
| 1198 | slave_dev->hw_features |= NETIF_F_HW_TC; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1199 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
Bjørn Mork | 2fcc800 | 2013-08-30 18:08:46 +0200 | [diff] [blame] | 1200 | eth_hw_addr_inherit(slave_dev, master); |
Phil Sutter | 0a5f107 | 2015-08-18 10:30:41 +0200 | [diff] [blame] | 1201 | slave_dev->priv_flags |= IFF_NO_QUEUE; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 1202 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1203 | slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; |
Jarod Wilson | 8b1efc0 | 2016-10-20 23:25:27 -0400 | [diff] [blame] | 1204 | slave_dev->min_mtu = 0; |
| 1205 | slave_dev->max_mtu = ETH_MAX_MTU; |
Florian Fainelli | f37db85 | 2015-09-23 18:19:58 -0700 | [diff] [blame] | 1206 | SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); |
Stephen Hemminger | d442ad4 | 2009-01-06 16:45:26 -0800 | [diff] [blame] | 1207 | |
Andrew Lunn | 448b448 | 2015-05-06 01:09:56 +0200 | [diff] [blame] | 1208 | netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, |
| 1209 | NULL); |
| 1210 | |
Vivien Didelot | 4cfbf09 | 2017-08-05 16:20:19 -0400 | [diff] [blame] | 1211 | SET_NETDEV_DEV(slave_dev, port->ds->dev); |
| 1212 | slave_dev->dev.of_node = port->dn; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1213 | slave_dev->vlan_features = master->vlan_features; |
| 1214 | |
| 1215 | p = netdev_priv(slave_dev); |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 1216 | p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1217 | if (!p->stats64) { |
| 1218 | free_netdev(slave_dev); |
| 1219 | return -ENOMEM; |
| 1220 | } |
Vivien Didelot | 4cfbf09 | 2017-08-05 16:20:19 -0400 | [diff] [blame] | 1221 | p->dp = port; |
Florian Fainelli | f50f212 | 2017-01-30 12:41:40 -0800 | [diff] [blame] | 1222 | INIT_LIST_HEAD(&p->mall_tc_list); |
Vivien Didelot | 1524024 | 2017-09-29 17:19:18 -0400 | [diff] [blame] | 1223 | p->xmit = cpu_dp->tag_ops->xmit; |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 1224 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 1225 | p->old_pause = -1; |
| 1226 | p->old_link = -1; |
| 1227 | p->old_duplex = -1; |
| 1228 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 1229 | port->slave = slave_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1230 | |
| 1231 | netif_carrier_off(slave_dev); |
| 1232 | |
Vivien Didelot | 4fa7b71 | 2017-09-20 19:31:57 -0400 | [diff] [blame] | 1233 | ret = dsa_slave_phy_setup(slave_dev); |
Andrew Lunn | 0071f56 | 2016-01-06 20:11:20 +0100 | [diff] [blame] | 1234 | if (ret) { |
| 1235 | netdev_err(master, "error %d setting up slave phy\n", ret); |
Florian Fainelli | e804441 | 2017-09-25 15:55:53 -0700 | [diff] [blame] | 1236 | goto out_free; |
| 1237 | } |
| 1238 | |
Vivien Didelot | 6158eaa | 2017-10-16 11:12:14 -0400 | [diff] [blame] | 1239 | dsa_slave_notify(slave_dev, DSA_PORT_REGISTER); |
Florian Fainelli | 60724d4 | 2017-10-11 10:57:48 -0700 | [diff] [blame] | 1240 | |
Florian Fainelli | e804441 | 2017-09-25 15:55:53 -0700 | [diff] [blame] | 1241 | ret = register_netdev(slave_dev); |
| 1242 | if (ret) { |
| 1243 | netdev_err(master, "error %d registering interface %s\n", |
| 1244 | ret, slave_dev->name); |
| 1245 | goto out_phy; |
Andrew Lunn | 0071f56 | 2016-01-06 20:11:20 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 1248 | return 0; |
Florian Fainelli | e804441 | 2017-09-25 15:55:53 -0700 | [diff] [blame] | 1249 | |
| 1250 | out_phy: |
David S. Miller | 53954cf | 2017-10-05 17:57:03 -0700 | [diff] [blame] | 1251 | phy_disconnect(slave_dev->phydev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1252 | if (of_phy_is_fixed_link(port->dn)) |
| 1253 | of_phy_deregister_fixed_link(port->dn); |
Florian Fainelli | e804441 | 2017-09-25 15:55:53 -0700 | [diff] [blame] | 1254 | out_free: |
| 1255 | free_percpu(p->stats64); |
| 1256 | free_netdev(slave_dev); |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 1257 | port->slave = NULL; |
Florian Fainelli | e804441 | 2017-09-25 15:55:53 -0700 | [diff] [blame] | 1258 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1259 | } |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1260 | |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1261 | void dsa_slave_destroy(struct net_device *slave_dev) |
| 1262 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1263 | struct dsa_port *dp = dsa_slave_to_port(slave_dev); |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1264 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1265 | struct device_node *port_dn = dp->dn; |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1266 | |
| 1267 | netif_carrier_off(slave_dev); |
Vivien Didelot | 0115dcd | 2017-09-26 17:15:32 -0400 | [diff] [blame] | 1268 | if (slave_dev->phydev) { |
| 1269 | phy_disconnect(slave_dev->phydev); |
Johan Hovold | 881eada | 2016-11-28 19:25:09 +0100 | [diff] [blame] | 1270 | |
| 1271 | if (of_phy_is_fixed_link(port_dn)) |
| 1272 | of_phy_deregister_fixed_link(port_dn); |
| 1273 | } |
Vivien Didelot | 6158eaa | 2017-10-16 11:12:14 -0400 | [diff] [blame] | 1274 | dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1275 | unregister_netdev(slave_dev); |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 1276 | free_percpu(p->stats64); |
Neil Armstrong | cda5c15 | 2015-12-07 13:57:35 +0100 | [diff] [blame] | 1277 | free_netdev(slave_dev); |
| 1278 | } |
| 1279 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1280 | static bool dsa_slave_dev_check(struct net_device *dev) |
| 1281 | { |
| 1282 | return dev->netdev_ops == &dsa_slave_netdev_ops; |
| 1283 | } |
| 1284 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1285 | static int dsa_slave_changeupper(struct net_device *dev, |
| 1286 | struct netdev_notifier_changeupper_info *info) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1287 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1288 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1289 | int err = NOTIFY_DONE; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1290 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1291 | if (netif_is_bridge_master(info->upper_dev)) { |
| 1292 | if (info->linking) { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 1293 | err = dsa_port_bridge_join(dp, info->upper_dev); |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1294 | err = notifier_from_errno(err); |
| 1295 | } else { |
Vivien Didelot | 17d7802 | 2017-05-19 17:00:38 -0400 | [diff] [blame] | 1296 | dsa_port_bridge_leave(dp, info->upper_dev); |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1297 | err = NOTIFY_OK; |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1298 | } |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1299 | } |
| 1300 | |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1301 | return err; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1304 | static int dsa_slave_netdevice_event(struct notifier_block *nb, |
| 1305 | unsigned long event, void *ptr) |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1306 | { |
Vivien Didelot | 6debb68 | 2016-03-13 16:21:34 -0400 | [diff] [blame] | 1307 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1308 | |
Florian Fainelli | 53bade8 | 2017-09-19 18:00:37 -0700 | [diff] [blame] | 1309 | if (!dsa_slave_dev_check(dev)) |
Vivien Didelot | 8e92ab3 | 2017-02-03 13:20:17 -0500 | [diff] [blame] | 1310 | return NOTIFY_DONE; |
| 1311 | |
| 1312 | if (event == NETDEV_CHANGEUPPER) |
| 1313 | return dsa_slave_changeupper(dev, ptr); |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1314 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 1315 | return NOTIFY_DONE; |
| 1316 | } |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1317 | |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1318 | struct dsa_switchdev_event_work { |
| 1319 | struct work_struct work; |
| 1320 | struct switchdev_notifier_fdb_info fdb_info; |
| 1321 | struct net_device *dev; |
| 1322 | unsigned long event; |
| 1323 | }; |
| 1324 | |
| 1325 | static void dsa_slave_switchdev_event_work(struct work_struct *work) |
| 1326 | { |
| 1327 | struct dsa_switchdev_event_work *switchdev_work = |
| 1328 | container_of(work, struct dsa_switchdev_event_work, work); |
| 1329 | struct net_device *dev = switchdev_work->dev; |
| 1330 | struct switchdev_notifier_fdb_info *fdb_info; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1331 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1332 | int err; |
| 1333 | |
| 1334 | rtnl_lock(); |
| 1335 | switch (switchdev_work->event) { |
| 1336 | case SWITCHDEV_FDB_ADD_TO_DEVICE: |
| 1337 | fdb_info = &switchdev_work->fdb_info; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1338 | err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid); |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1339 | if (err) { |
| 1340 | netdev_dbg(dev, "fdb add failed err=%d\n", err); |
| 1341 | break; |
| 1342 | } |
| 1343 | call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, |
| 1344 | &fdb_info->info); |
| 1345 | break; |
| 1346 | |
| 1347 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1348 | fdb_info = &switchdev_work->fdb_info; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 1349 | err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid); |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1350 | if (err) { |
| 1351 | netdev_dbg(dev, "fdb del failed err=%d\n", err); |
| 1352 | dev_close(dev); |
| 1353 | } |
| 1354 | break; |
| 1355 | } |
| 1356 | rtnl_unlock(); |
| 1357 | |
| 1358 | kfree(switchdev_work->fdb_info.addr); |
| 1359 | kfree(switchdev_work); |
| 1360 | dev_put(dev); |
| 1361 | } |
| 1362 | |
| 1363 | static int |
| 1364 | dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * |
| 1365 | switchdev_work, |
| 1366 | const struct switchdev_notifier_fdb_info * |
| 1367 | fdb_info) |
| 1368 | { |
| 1369 | memcpy(&switchdev_work->fdb_info, fdb_info, |
| 1370 | sizeof(switchdev_work->fdb_info)); |
| 1371 | switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); |
| 1372 | if (!switchdev_work->fdb_info.addr) |
| 1373 | return -ENOMEM; |
| 1374 | ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, |
| 1375 | fdb_info->addr); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | /* Called under rcu_read_lock() */ |
| 1380 | static int dsa_slave_switchdev_event(struct notifier_block *unused, |
| 1381 | unsigned long event, void *ptr) |
| 1382 | { |
| 1383 | struct net_device *dev = switchdev_notifier_info_to_dev(ptr); |
| 1384 | struct dsa_switchdev_event_work *switchdev_work; |
| 1385 | |
| 1386 | if (!dsa_slave_dev_check(dev)) |
| 1387 | return NOTIFY_DONE; |
| 1388 | |
| 1389 | switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); |
| 1390 | if (!switchdev_work) |
| 1391 | return NOTIFY_BAD; |
| 1392 | |
| 1393 | INIT_WORK(&switchdev_work->work, |
| 1394 | dsa_slave_switchdev_event_work); |
| 1395 | switchdev_work->dev = dev; |
| 1396 | switchdev_work->event = event; |
| 1397 | |
| 1398 | switch (event) { |
| 1399 | case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ |
| 1400 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1401 | if (dsa_slave_switchdev_fdb_work_init(switchdev_work, |
| 1402 | ptr)) |
| 1403 | goto err_fdb_work_init; |
| 1404 | dev_hold(dev); |
| 1405 | break; |
| 1406 | default: |
| 1407 | kfree(switchdev_work); |
| 1408 | return NOTIFY_DONE; |
| 1409 | } |
| 1410 | |
| 1411 | dsa_schedule_work(&switchdev_work->work); |
| 1412 | return NOTIFY_OK; |
| 1413 | |
| 1414 | err_fdb_work_init: |
| 1415 | kfree(switchdev_work); |
| 1416 | return NOTIFY_BAD; |
| 1417 | } |
| 1418 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1419 | static struct notifier_block dsa_slave_nb __read_mostly = { |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1420 | .notifier_call = dsa_slave_netdevice_event, |
| 1421 | }; |
| 1422 | |
| 1423 | static struct notifier_block dsa_slave_switchdev_notifier = { |
| 1424 | .notifier_call = dsa_slave_switchdev_event, |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1425 | }; |
| 1426 | |
| 1427 | int dsa_slave_register_notifier(void) |
| 1428 | { |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1429 | int err; |
| 1430 | |
| 1431 | err = register_netdevice_notifier(&dsa_slave_nb); |
| 1432 | if (err) |
| 1433 | return err; |
| 1434 | |
| 1435 | err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); |
| 1436 | if (err) |
| 1437 | goto err_switchdev_nb; |
| 1438 | |
| 1439 | return 0; |
| 1440 | |
| 1441 | err_switchdev_nb: |
| 1442 | unregister_netdevice_notifier(&dsa_slave_nb); |
| 1443 | return err; |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | void dsa_slave_unregister_notifier(void) |
| 1447 | { |
| 1448 | int err; |
| 1449 | |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 1450 | err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); |
| 1451 | if (err) |
| 1452 | pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); |
| 1453 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 1454 | err = unregister_netdevice_notifier(&dsa_slave_nb); |
| 1455 | if (err) |
| 1456 | pr_err("DSA: failed to unregister slave notifier (%d)\n", err); |
| 1457 | } |