blob: a95a55f7913746bab3aa7a993265885ece25f35a [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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 Buytenhekdf02c6f2008-11-10 21:53:12 -080012#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080013#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070015#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070016#include <linux/of_net.h>
17#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010018#include <linux/mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080019#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080020#include <net/pkt_cls.h>
21#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080022#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070023#include <linux/netpoll.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040024
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include "dsa_priv.h"
26
Florian Fainellif50f2122017-01-30 12:41:40 -080027static bool dsa_slave_dev_check(struct net_device *dev);
28
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029/* slave mii_bus handling ***************************************************/
30static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
31{
32 struct dsa_switch *ds = bus->priv;
33
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070034 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040035 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000036
37 return 0xffff;
38}
39
40static 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 Fainelli0d8bcdd2014-08-27 17:04:51 -070044 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040045 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000046
47 return 0;
48}
49
50void 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 Fainelli0b7b4982016-06-07 16:32:38 -070056 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
Vivien Didelot49463b72017-11-03 19:05:21 -040057 ds->dst->index, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020058 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050059 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060}
61
62
63/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020064static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000065{
Vivien Didelotd0006b02017-10-16 11:12:16 -040066 return dsa_slave_to_master(dev)->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000067}
68
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000069static int dsa_slave_open(struct net_device *dev)
70{
Vivien Didelotd0006b02017-10-16 11:12:16 -040071 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -040072 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080073 int err;
74
75 if (!(master->flags & IFF_UP))
76 return -ENETDOWN;
77
Joe Perches8feedbb2012-05-08 18:56:57 +000078 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000079 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080080 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 Didelot0115dcd2017-09-26 17:15:32 -040095 err = dsa_port_enable(dp, dev->phydev);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040096 if (err)
97 goto clear_promisc;
Florian Fainellib73adef2015-02-24 13:15:33 -080098
Vivien Didelot0115dcd2017-09-26 17:15:32 -040099 if (dev->phydev)
100 phy_start(dev->phydev);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700101
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000102 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800103
Florian Fainellib2f2af22014-09-24 17:05:18 -0700104clear_promisc:
105 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300106 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800107clear_allmulti:
108 if (dev->flags & IFF_ALLMULTI)
109 dev_set_allmulti(master, -1);
110del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000111 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000112 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800113out:
114 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000115}
116
117static int dsa_slave_close(struct net_device *dev)
118{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400119 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400120 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800121
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400122 if (dev->phydev)
123 phy_stop(dev->phydev);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700124
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400125 dsa_port_disable(dp, dev->phydev);
Vivien Didelot6457edf2017-09-22 19:01:55 -0400126
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800127 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000128 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800129 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 Perches8feedbb2012-05-08 18:56:57 +0000134 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000135 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800136
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000137 return 0;
138}
139
140static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
141{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400142 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143
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
150static void dsa_slave_set_rx_mode(struct net_device *dev)
151{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400152 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000153
154 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000155 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000156}
157
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800158static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400160 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800161 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 Perches8feedbb2012-05-08 18:56:57 +0000170 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000171 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800172 if (err < 0)
173 return err;
174 }
175
Joe Perches8feedbb2012-05-08 18:56:57 +0000176 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000177 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800178
179out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800180 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181
182 return 0;
183}
184
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300185struct dsa_slave_dump_ctx {
186 struct net_device *dev;
187 struct sk_buff *skb;
188 struct netlink_callback *cb;
189 int idx;
190};
191
192static int
193dsa_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
227skip:
228 dump->idx++;
229 return 0;
230
231nla_put_failure:
232 nlmsg_cancel(dump->skb, nlh);
233 return -EMSGSIZE;
234}
235
236static int
237dsa_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 Didelotd9450972017-10-16 11:12:15 -0400241 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300242 struct dsa_slave_dump_ctx dump = {
243 .dev = dev,
244 .skb = skb,
245 .cb = cb,
246 .idx = *idx,
247 };
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300248 int err;
249
Vivien Didelotde40fc52017-09-20 19:32:14 -0400250 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300251 *idx = dump.idx;
Vivien Didelotde40fc52017-09-20 19:32:14 -0400252
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300253 return err;
254}
255
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
257{
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400258 if (!dev->phydev)
Vivien Didelotf4344e02017-09-26 17:15:31 -0400259 return -ENODEV;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000260
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400261 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000262}
263
Scott Feldman35636062015-05-10 09:47:51 -0700264static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200265 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200266 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700267{
Vivien Didelotd9450972017-10-16 11:12:15 -0400268 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400269 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700270
271 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200272 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400273 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700274 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500275 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400276 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
277 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500278 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400279 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400280 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400281 break;
Scott Feldman35636062015-05-10 09:47:51 -0700282 default:
283 ret = -EOPNOTSUPP;
284 break;
285 }
286
287 return ret;
288}
289
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400290static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200291 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200292 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400293{
Vivien Didelotd9450972017-10-16 11:12:15 -0400294 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400295 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 Pirko9e8f4a52015-10-01 11:03:46 +0200302 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400303 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400304 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400305 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100306 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 Pirko57d80832015-10-01 11:03:41 +0200313 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d122017-05-19 17:00:41 -0400314 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
315 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400316 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400317 default:
318 err = -EOPNOTSUPP;
319 break;
320 }
321
322 return err;
323}
324
325static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200326 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400327{
Vivien Didelotd9450972017-10-16 11:12:15 -0400328 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400329 int err;
330
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200331 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400332 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400333 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400334 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100335 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 Pirko57d80832015-10-01 11:03:41 +0200341 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d122017-05-19 17:00:41 -0400342 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400343 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400344 default:
345 err = -EOPNOTSUPP;
346 break;
347 }
348
349 return err;
350}
351
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700352static int dsa_slave_port_attr_get(struct net_device *dev,
353 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800354{
Vivien Didelotd9450972017-10-16 11:12:15 -0400355 struct dsa_port *dp = dsa_slave_to_port(dev);
356 struct dsa_switch *ds = dp->ds;
Andrew Lunna42c8e32017-11-09 22:29:51 +0100357 struct dsa_switch_tree *dst = ds->dst;
Florian Fainellib73adef2015-02-24 13:15:33 -0800358
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700359 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200360 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Andrew Lunna42c8e32017-11-09 22:29:51 +0100361 attr->u.ppid.id_len = sizeof(dst->index);
362 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700363 break;
Arkadi Sharshevskyc9e21052017-08-06 16:15:44 +0300364 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
365 attr->u.brport_flags_support = 0;
366 break;
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700367 default:
368 return -EOPNOTSUPP;
369 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800370
371 return 0;
372}
373
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400374static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
375 struct sk_buff *skb)
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700376{
377#ifdef CONFIG_NET_POLL_CONTROLLER
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400378 struct dsa_slave_priv *p = netdev_priv(dev);
379
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700380 if (p->netpoll)
381 netpoll_send_skb(p->netpoll, skb);
382#else
383 BUG();
384#endif
385 return NETDEV_TX_OK;
386}
387
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700388static 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 Fainelli5f6b4e142017-08-03 21:33:27 -0700391 struct pcpu_sw_netstats *s;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700392 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700393
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700394 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 Fainelli3e8a72d2014-08-27 17:04:46 -0700399
Vivien Didelotfe47d562017-06-01 16:07:15 -0400400 /* 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 Fainelli4ed70ce2015-07-31 11:42:56 -0700403 nskb = p->xmit(skb, dev);
Vivien Didelotfe47d562017-06-01 16:07:15 -0400404 if (!nskb) {
405 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700406 return NETDEV_TX_OK;
Vivien Didelotfe47d562017-06-01 16:07:15 -0400407 }
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700408
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700409 /* 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 Didelot4fa7b712017-09-20 19:31:57 -0400413 return dsa_slave_netpoll_send_skb(dev, nskb);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700414
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700415 /* Queue the SKB for transmission on the parent interface, but
416 * do not modify its EtherType
417 */
Vivien Didelotd0006b02017-10-16 11:12:16 -0400418 nskb->dev = dsa_slave_to_master(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700419 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700420
421 return NETDEV_TX_OK;
422}
423
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000424/* ethtool operations *******************************************************/
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000425
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000426static void dsa_slave_get_drvinfo(struct net_device *dev,
427 struct ethtool_drvinfo *drvinfo)
428{
Jiri Pirko7826d432013-01-06 00:44:26 +0000429 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000430 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
431 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000432}
433
Guenter Roeck3d762a02014-10-29 10:45:04 -0700434static int dsa_slave_get_regs_len(struct net_device *dev)
435{
Vivien Didelotd9450972017-10-16 11:12:15 -0400436 struct dsa_port *dp = dsa_slave_to_port(dev);
437 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700438
Vivien Didelot9d490b42016-08-23 12:38:56 -0400439 if (ds->ops->get_regs_len)
Vivien Didelotd9450972017-10-16 11:12:15 -0400440 return ds->ops->get_regs_len(ds, dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700441
442 return -EOPNOTSUPP;
443}
444
445static void
446dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
447{
Vivien Didelotd9450972017-10-16 11:12:15 -0400448 struct dsa_port *dp = dsa_slave_to_port(dev);
449 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700450
Vivien Didelot9d490b42016-08-23 12:38:56 -0400451 if (ds->ops->get_regs)
Vivien Didelotd9450972017-10-16 11:12:15 -0400452 ds->ops->get_regs(ds, dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700453}
454
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000455static u32 dsa_slave_get_link(struct net_device *dev)
456{
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400457 if (!dev->phydev)
Vivien Didelotf4344e02017-09-26 17:15:31 -0400458 return -ENODEV;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000459
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400460 genphy_update_link(dev->phydev);
Vivien Didelotf4344e02017-09-26 17:15:31 -0400461
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400462 return dev->phydev->link;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000463}
464
Guenter Roeck6793abb2014-10-29 10:45:01 -0700465static int dsa_slave_get_eeprom_len(struct net_device *dev)
466{
Vivien Didelotd9450972017-10-16 11:12:15 -0400467 struct dsa_port *dp = dsa_slave_to_port(dev);
468 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700469
Andrew Lunn0e576042016-06-04 21:16:52 +0200470 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200471 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700472
Vivien Didelot9d490b42016-08-23 12:38:56 -0400473 if (ds->ops->get_eeprom_len)
474 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700475
476 return 0;
477}
478
479static int dsa_slave_get_eeprom(struct net_device *dev,
480 struct ethtool_eeprom *eeprom, u8 *data)
481{
Vivien Didelotd9450972017-10-16 11:12:15 -0400482 struct dsa_port *dp = dsa_slave_to_port(dev);
483 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700484
Vivien Didelot9d490b42016-08-23 12:38:56 -0400485 if (ds->ops->get_eeprom)
486 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700487
488 return -EOPNOTSUPP;
489}
490
491static int dsa_slave_set_eeprom(struct net_device *dev,
492 struct ethtool_eeprom *eeprom, u8 *data)
493{
Vivien Didelotd9450972017-10-16 11:12:15 -0400494 struct dsa_port *dp = dsa_slave_to_port(dev);
495 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700496
Vivien Didelot9d490b42016-08-23 12:38:56 -0400497 if (ds->ops->set_eeprom)
498 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700499
500 return -EOPNOTSUPP;
501}
502
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000503static void dsa_slave_get_strings(struct net_device *dev,
504 uint32_t stringset, uint8_t *data)
505{
Vivien Didelotd9450972017-10-16 11:12:15 -0400506 struct dsa_port *dp = dsa_slave_to_port(dev);
507 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000508
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 Didelot9d490b42016-08-23 12:38:56 -0400516 if (ds->ops->get_strings)
Vivien Didelotd9450972017-10-16 11:12:15 -0400517 ds->ops->get_strings(ds, dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000518 }
519}
520
521static void dsa_slave_get_ethtool_stats(struct net_device *dev,
522 struct ethtool_stats *stats,
523 uint64_t *data)
524{
Vivien Didelotd9450972017-10-16 11:12:15 -0400525 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000526 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400527 struct dsa_switch *ds = dp->ds;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700528 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700529 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700530 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000531
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700532 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 Didelot9d490b42016-08-23 12:38:56 -0400548 if (ds->ops->get_ethtool_stats)
Vivien Didelotd9450972017-10-16 11:12:15 -0400549 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000550}
551
552static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
553{
Vivien Didelotd9450972017-10-16 11:12:15 -0400554 struct dsa_port *dp = dsa_slave_to_port(dev);
555 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000556
557 if (sset == ETH_SS_STATS) {
558 int count;
559
560 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400561 if (ds->ops->get_sset_count)
562 count += ds->ops->get_sset_count(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000563
564 return count;
565 }
566
567 return -EOPNOTSUPP;
568}
569
Florian Fainelli19e57c42014-09-18 17:31:24 -0700570static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
571{
Vivien Didelotd9450972017-10-16 11:12:15 -0400572 struct dsa_port *dp = dsa_slave_to_port(dev);
573 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700574
Vivien Didelot9d490b42016-08-23 12:38:56 -0400575 if (ds->ops->get_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400576 ds->ops->get_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700577}
578
579static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
580{
Vivien Didelotd9450972017-10-16 11:12:15 -0400581 struct dsa_port *dp = dsa_slave_to_port(dev);
582 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700583 int ret = -EOPNOTSUPP;
584
Vivien Didelot9d490b42016-08-23 12:38:56 -0400585 if (ds->ops->set_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400586 ret = ds->ops->set_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700587
588 return ret;
589}
590
Florian Fainelli79052882014-09-24 17:05:21 -0700591static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
592{
Vivien Didelotd9450972017-10-16 11:12:15 -0400593 struct dsa_port *dp = dsa_slave_to_port(dev);
594 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700595 int ret;
596
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400597 /* Port's PHY and MAC both need to be EEE capable */
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400598 if (!dev->phydev)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400599 return -ENODEV;
600
Vivien Didelot08f50062017-08-01 16:32:41 -0400601 if (!ds->ops->set_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700602 return -EOPNOTSUPP;
603
Vivien Didelotd9450972017-10-16 11:12:15 -0400604 ret = ds->ops->set_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700605 if (ret)
606 return ret;
607
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400608 if (e->eee_enabled) {
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400609 ret = phy_init_eee(dev->phydev, 0);
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400610 if (ret)
611 return ret;
612 }
613
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400614 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700615}
616
617static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
618{
Vivien Didelotd9450972017-10-16 11:12:15 -0400619 struct dsa_port *dp = dsa_slave_to_port(dev);
620 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700621 int ret;
622
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400623 /* Port's PHY and MAC both need to be EEE capable */
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400624 if (!dev->phydev)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400625 return -ENODEV;
626
Vivien Didelot08f50062017-08-01 16:32:41 -0400627 if (!ds->ops->get_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700628 return -EOPNOTSUPP;
629
Vivien Didelotd9450972017-10-16 11:12:15 -0400630 ret = ds->ops->get_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700631 if (ret)
632 return ret;
633
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400634 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700635}
636
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700637#ifdef CONFIG_NET_POLL_CONTROLLER
638static int dsa_slave_netpoll_setup(struct net_device *dev,
639 struct netpoll_info *ni)
640{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400641 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700642 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700643 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;
657out:
658 return err;
659}
660
661static 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
674static void dsa_slave_poll_controller(struct net_device *dev)
675{
676}
677#endif
678
Florian Fainelli44bb7652017-01-10 12:32:36 -0800679static int dsa_slave_get_phys_port_name(struct net_device *dev,
680 char *name, size_t len)
681{
Vivien Didelotd9450972017-10-16 11:12:15 -0400682 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli44bb7652017-01-10 12:32:36 -0800683
Vivien Didelotd9450972017-10-16 11:12:15 -0400684 if (snprintf(name, len, "p%d", dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800685 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800686
687 return 0;
688}
689
Florian Fainellif50f2122017-01-30 12:41:40 -0800690static struct dsa_mall_tc_entry *
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400691dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
Florian Fainellif50f2122017-01-30 12:41:40 -0800692{
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400693 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800694 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
703static int dsa_slave_add_cls_matchall(struct net_device *dev,
Florian Fainellif50f2122017-01-30 12:41:40 -0800704 struct tc_cls_matchall_offload *cls,
705 bool ingress)
706{
Vivien Didelotd9450972017-10-16 11:12:15 -0400707 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800708 struct dsa_slave_priv *p = netdev_priv(dev);
709 struct dsa_mall_tc_entry *mall_tc_entry;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200710 __be16 protocol = cls->common.protocol;
Florian Fainellif50f2122017-01-30 12:41:40 -0800711 struct net *net = dev_net(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400712 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800713 struct net_device *to_dev;
714 const struct tc_action *a;
Vivien Didelotd9450972017-10-16 11:12:15 -0400715 struct dsa_port *to_dp;
Florian Fainellif50f2122017-01-30 12:41:40 -0800716 int err = -EOPNOTSUPP;
717 LIST_HEAD(actions);
718 int ifindex;
719
720 if (!ds->ops->port_mirror_add)
721 return err;
722
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200723 if (!tcf_exts_has_one_action(cls->exts))
Florian Fainellif50f2122017-01-30 12:41:40 -0800724 return err;
725
726 tcf_exts_to_list(cls->exts, &actions);
727 a = list_first_entry(&actions, struct tc_action, list);
728
729 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
730 struct dsa_mall_mirror_tc_entry *mirror;
731
732 ifindex = tcf_mirred_ifindex(a);
733 to_dev = __dev_get_by_index(net, ifindex);
734 if (!to_dev)
735 return -EINVAL;
736
737 if (!dsa_slave_dev_check(to_dev))
738 return -EOPNOTSUPP;
739
740 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
741 if (!mall_tc_entry)
742 return -ENOMEM;
743
744 mall_tc_entry->cookie = cls->cookie;
745 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
746 mirror = &mall_tc_entry->mirror;
747
Vivien Didelotd9450972017-10-16 11:12:15 -0400748 to_dp = dsa_slave_to_port(to_dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800749
Vivien Didelotd9450972017-10-16 11:12:15 -0400750 mirror->to_local_port = to_dp->index;
Florian Fainellif50f2122017-01-30 12:41:40 -0800751 mirror->ingress = ingress;
752
Vivien Didelotd9450972017-10-16 11:12:15 -0400753 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
Florian Fainellif50f2122017-01-30 12:41:40 -0800754 if (err) {
755 kfree(mall_tc_entry);
756 return err;
757 }
758
759 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
760 }
761
762 return 0;
763}
764
765static void dsa_slave_del_cls_matchall(struct net_device *dev,
766 struct tc_cls_matchall_offload *cls)
767{
Vivien Didelotd9450972017-10-16 11:12:15 -0400768 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800769 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -0400770 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800771
772 if (!ds->ops->port_mirror_del)
773 return;
774
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400775 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
Florian Fainellif50f2122017-01-30 12:41:40 -0800776 if (!mall_tc_entry)
777 return;
778
779 list_del(&mall_tc_entry->list);
780
781 switch (mall_tc_entry->type) {
782 case DSA_PORT_MALL_MIRROR:
Vivien Didelotd9450972017-10-16 11:12:15 -0400783 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
Florian Fainellif50f2122017-01-30 12:41:40 -0800784 break;
785 default:
786 WARN_ON(1);
787 }
788
789 kfree(mall_tc_entry);
790}
791
Jiri Pirko3fbae382017-08-07 10:15:26 +0200792static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200793 struct tc_cls_matchall_offload *cls,
794 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -0800795{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200796 if (cls->common.chain_index)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200797 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800798
Jiri Pirko3fbae382017-08-07 10:15:26 +0200799 switch (cls->command) {
800 case TC_CLSMATCHALL_REPLACE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200801 return dsa_slave_add_cls_matchall(dev, cls, ingress);
Jiri Pirko3fbae382017-08-07 10:15:26 +0200802 case TC_CLSMATCHALL_DESTROY:
803 dsa_slave_del_cls_matchall(dev, cls);
804 return 0;
805 default:
806 return -EOPNOTSUPP;
807 }
808}
809
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200810static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
811 void *cb_priv, bool ingress)
812{
813 struct net_device *dev = cb_priv;
814
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100815 if (!tc_can_offload(dev))
816 return -EOPNOTSUPP;
817
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200818 switch (type) {
819 case TC_SETUP_CLSMATCHALL:
820 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
821 default:
822 return -EOPNOTSUPP;
823 }
824}
825
826static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
827 void *type_data, void *cb_priv)
828{
829 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
830}
831
832static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
833 void *type_data, void *cb_priv)
834{
835 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
836}
837
838static int dsa_slave_setup_tc_block(struct net_device *dev,
839 struct tc_block_offload *f)
840{
841 tc_setup_cb_t *cb;
842
843 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
844 cb = dsa_slave_setup_tc_block_cb_ig;
845 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
846 cb = dsa_slave_setup_tc_block_cb_eg;
847 else
848 return -EOPNOTSUPP;
849
850 switch (f->command) {
851 case TC_BLOCK_BIND:
852 return tcf_block_cb_register(f->block, cb, dev, dev);
853 case TC_BLOCK_UNBIND:
854 tcf_block_cb_unregister(f->block, cb, dev);
855 return 0;
856 default:
857 return -EOPNOTSUPP;
858 }
859}
860
Jiri Pirko3fbae382017-08-07 10:15:26 +0200861static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200862 void *type_data)
Jiri Pirko3fbae382017-08-07 10:15:26 +0200863{
Jiri Pirko2572ac52017-08-07 10:15:17 +0200864 switch (type) {
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200865 case TC_SETUP_BLOCK:
866 return dsa_slave_setup_tc_block(dev, type_data);
Florian Fainellif50f2122017-01-30 12:41:40 -0800867 default:
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200868 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800869 }
Florian Fainellif50f2122017-01-30 12:41:40 -0800870}
871
Florian Fainellif613ed62017-08-01 15:00:36 -0700872static void dsa_slave_get_stats64(struct net_device *dev,
873 struct rtnl_link_stats64 *stats)
874{
875 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700876 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700877 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700878 int i;
Florian Fainellif613ed62017-08-01 15:00:36 -0700879
880 netdev_stats_to_stats64(stats, &dev->stats);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700881 for_each_possible_cpu(i) {
882 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
883
884 s = per_cpu_ptr(p->stats64, i);
885 do {
886 start = u64_stats_fetch_begin_irq(&s->syncp);
887 tx_packets = s->tx_packets;
888 tx_bytes = s->tx_bytes;
889 rx_packets = s->rx_packets;
890 rx_bytes = s->rx_bytes;
891 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
892
893 stats->tx_packets += tx_packets;
894 stats->tx_bytes += tx_bytes;
895 stats->rx_packets += rx_packets;
896 stats->rx_bytes += rx_bytes;
897 }
Florian Fainellif613ed62017-08-01 15:00:36 -0700898}
899
Florian Fainellibf9f2642017-01-30 09:48:40 -0800900static int dsa_slave_get_rxnfc(struct net_device *dev,
901 struct ethtool_rxnfc *nfc, u32 *rule_locs)
902{
Vivien Didelotd9450972017-10-16 11:12:15 -0400903 struct dsa_port *dp = dsa_slave_to_port(dev);
904 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800905
906 if (!ds->ops->get_rxnfc)
907 return -EOPNOTSUPP;
908
Vivien Didelotd9450972017-10-16 11:12:15 -0400909 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800910}
911
912static int dsa_slave_set_rxnfc(struct net_device *dev,
913 struct ethtool_rxnfc *nfc)
914{
Vivien Didelotd9450972017-10-16 11:12:15 -0400915 struct dsa_port *dp = dsa_slave_to_port(dev);
916 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800917
918 if (!ds->ops->set_rxnfc)
919 return -EOPNOTSUPP;
920
Vivien Didelotd9450972017-10-16 11:12:15 -0400921 return ds->ops->set_rxnfc(ds, dp->index, nfc);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800922}
923
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000924static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000925 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700926 .get_regs_len = dsa_slave_get_regs_len,
927 .get_regs = dsa_slave_get_regs,
Vivien Didelot69b2c1622017-09-26 17:15:35 -0400928 .nway_reset = phy_ethtool_nway_reset,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000929 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700930 .get_eeprom_len = dsa_slave_get_eeprom_len,
931 .get_eeprom = dsa_slave_get_eeprom,
932 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000933 .get_strings = dsa_slave_get_strings,
934 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
935 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700936 .set_wol = dsa_slave_set_wol,
937 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700938 .set_eee = dsa_slave_set_eee,
939 .get_eee = dsa_slave_get_eee,
Vivien Didelot771df312017-09-26 17:15:33 -0400940 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Vivien Didelotaa62a8c2017-09-26 17:15:34 -0400941 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -0800942 .get_rxnfc = dsa_slave_get_rxnfc,
943 .set_rxnfc = dsa_slave_set_rxnfc,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000944};
945
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700946static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800947 .ndo_open = dsa_slave_open,
948 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700949 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800950 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
951 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800952 .ndo_set_mac_address = dsa_slave_set_mac_address,
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +0300953 .ndo_fdb_add = dsa_legacy_fdb_add,
954 .ndo_fdb_del = dsa_legacy_fdb_del,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300955 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800956 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +0200957 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700958#ifdef CONFIG_NET_POLL_CONTROLLER
959 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
960 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
961 .ndo_poll_controller = dsa_slave_poll_controller,
962#endif
Florian Fainelli44bb7652017-01-10 12:32:36 -0800963 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -0800964 .ndo_setup_tc = dsa_slave_setup_tc,
Florian Fainellif613ed62017-08-01 15:00:36 -0700965 .ndo_get_stats64 = dsa_slave_get_stats64,
Scott Feldman98237d42015-03-15 21:07:15 -0700966};
967
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700968static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700969 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -0700970 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400971 .switchdev_port_obj_add = dsa_slave_port_obj_add,
972 .switchdev_port_obj_del = dsa_slave_port_obj_del,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -0800973};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000974
Florian Fainellif37db852015-09-23 18:19:58 -0700975static struct device_type dsa_type = {
976 .name = "dsa",
977};
978
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700979static void dsa_slave_adjust_link(struct net_device *dev)
980{
Vivien Didelotd9450972017-10-16 11:12:15 -0400981 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700982 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400983 struct dsa_switch *ds = dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700984 unsigned int status_changed = 0;
985
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400986 if (p->old_link != dev->phydev->link) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700987 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400988 p->old_link = dev->phydev->link;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700989 }
990
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400991 if (p->old_duplex != dev->phydev->duplex) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700992 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400993 p->old_duplex = dev->phydev->duplex;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700994 }
995
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400996 if (p->old_pause != dev->phydev->pause) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700997 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400998 p->old_pause = dev->phydev->pause;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700999 }
1000
Vivien Didelot9d490b42016-08-23 12:38:56 -04001001 if (ds->ops->adjust_link && status_changed)
Vivien Didelotd9450972017-10-16 11:12:15 -04001002 ds->ops->adjust_link(ds, dp->index, dev->phydev);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001003
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001004 if (status_changed)
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001005 phy_print_status(dev->phydev);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001006}
1007
Florian Fainellice31b312014-08-27 17:04:54 -07001008static int dsa_slave_fixed_link_update(struct net_device *dev,
1009 struct fixed_phy_status *status)
1010{
Andrew Lunnb71be352016-03-12 00:01:37 +01001011 struct dsa_switch *ds;
Vivien Didelotd9450972017-10-16 11:12:15 -04001012 struct dsa_port *dp;
Florian Fainellice31b312014-08-27 17:04:54 -07001013
Andrew Lunnb71be352016-03-12 00:01:37 +01001014 if (dev) {
Vivien Didelotd9450972017-10-16 11:12:15 -04001015 dp = dsa_slave_to_port(dev);
1016 ds = dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001017 if (ds->ops->fixed_link_update)
Vivien Didelotd9450972017-10-16 11:12:15 -04001018 ds->ops->fixed_link_update(ds, dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001019 }
Florian Fainellice31b312014-08-27 17:04:54 -07001020
1021 return 0;
1022}
1023
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001024/* slave device setup *******************************************************/
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001025static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001026{
Vivien Didelotd9450972017-10-16 11:12:15 -04001027 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001028 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001029 struct dsa_switch *ds = dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001030
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001031 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1032 if (!slave_dev->phydev) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001033 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001034 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001035 }
Florian Fainellic305c162015-03-10 16:57:12 -07001036
1037 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001038 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001039 p->phy_interface = slave_dev->phydev->interface;
1040
1041 return phy_connect_direct(slave_dev, slave_dev->phydev,
1042 dsa_slave_adjust_link, p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001043}
1044
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001045static int dsa_slave_phy_setup(struct net_device *slave_dev)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001046{
Vivien Didelotd9450972017-10-16 11:12:15 -04001047 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001048 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001049 struct device_node *port_dn = dp->dn;
1050 struct dsa_switch *ds = dp->ds;
1051 struct device_node *phy_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001052 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001053 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001054 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001055
Guenter Roeck19334922015-02-16 21:23:51 -08001056 mode = of_get_phy_mode(port_dn);
1057 if (mode < 0)
1058 mode = PHY_INTERFACE_MODE_NA;
1059 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001060
1061 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001062 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001063 /* In the case of a fixed PHY, the DT node associated
1064 * to the fixed PHY is the Port DT node
1065 */
1066 ret = of_phy_register_fixed_link(port_dn);
1067 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001068 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001069 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001070 }
Florian Fainellice31b312014-08-27 17:04:54 -07001071 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001072 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001073 }
1074
Vivien Didelot9d490b42016-08-23 12:38:56 -04001075 if (ds->ops->get_phy_flags)
Vivien Didelotd9450972017-10-16 11:12:15 -04001076 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001077
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001078 if (phy_dn) {
Florian Fainelli399ba772017-10-25 17:32:05 -07001079 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1080 dsa_slave_adjust_link,
1081 phy_flags,
1082 p->phy_interface);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001083 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001084 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001085
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001086 if (slave_dev->phydev && phy_is_fixed)
1087 fixed_phy_set_link_update(slave_dev->phydev,
1088 dsa_slave_fixed_link_update);
Florian Fainellice31b312014-08-27 17:04:54 -07001089
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001090 /* We could not connect to a designated PHY, so use the switch internal
1091 * MDIO bus instead
1092 */
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001093 if (!slave_dev->phydev) {
Vivien Didelotd9450972017-10-16 11:12:15 -04001094 ret = dsa_slave_phy_connect(slave_dev, dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001095 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001096 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
Vivien Didelotd9450972017-10-16 11:12:15 -04001097 dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001098 if (phy_is_fixed)
1099 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001100 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001101 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001102 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001103
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001104 phy_attached_info(slave_dev->phydev);
Andrew Lunn22209432016-01-06 20:11:13 +01001105
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001106 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001107}
1108
Andrew Lunn448b4482015-05-06 01:09:56 +02001109static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1110static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1111 struct netdev_queue *txq,
1112 void *_unused)
1113{
1114 lockdep_set_class(&txq->_xmit_lock,
1115 &dsa_slave_netdev_xmit_lock_key);
1116}
1117
Florian Fainelli24462542014-09-18 17:31:22 -07001118int dsa_slave_suspend(struct net_device *slave_dev)
1119{
1120 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1121
Florian Fainellif154be22017-01-25 09:10:41 -08001122 netif_device_detach(slave_dev);
1123
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001124 if (slave_dev->phydev) {
1125 phy_stop(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001126 p->old_pause = -1;
1127 p->old_link = -1;
1128 p->old_duplex = -1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001129 phy_suspend(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001130 }
1131
1132 return 0;
1133}
1134
1135int dsa_slave_resume(struct net_device *slave_dev)
1136{
Florian Fainelli24462542014-09-18 17:31:22 -07001137 netif_device_attach(slave_dev);
1138
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001139 if (slave_dev->phydev) {
1140 phy_resume(slave_dev->phydev);
1141 phy_start(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001142 }
1143
1144 return 0;
1145}
1146
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001147static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1148{
Vivien Didelotd0006b02017-10-16 11:12:16 -04001149 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001150 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001151 struct dsa_notifier_register_info rinfo = {
1152 .switch_number = dp->ds->index,
1153 .port_number = dp->index,
1154 .master = master,
1155 .info.dev = dev,
1156 };
1157
1158 call_dsa_notifiers(val, dev, &rinfo.info);
1159}
1160
Vivien Didelot951259aa2017-10-27 15:55:19 -04001161int dsa_slave_create(struct dsa_port *port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001162{
Vivien Didelot24a93322017-11-06 16:11:43 -05001163 const struct dsa_port *cpu_dp = port->cpu_dp;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001164 struct net_device *master = cpu_dp->master;
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001165 struct dsa_switch *ds = port->ds;
Vivien Didelot951259aa2017-10-27 15:55:19 -04001166 const char *name = port->name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001167 struct net_device *slave_dev;
1168 struct dsa_slave_priv *p;
1169 int ret;
1170
Florian Fainelli55199df2017-09-03 20:27:00 -07001171 if (!ds->num_tx_queues)
1172 ds->num_tx_queues = 1;
1173
1174 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1175 NET_NAME_UNKNOWN, ether_setup,
1176 ds->num_tx_queues, 1);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001177 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001178 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001179
Florian Fainellif50f2122017-01-30 12:41:40 -08001180 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1181 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001182 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001183 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001184 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001185 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001186 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001187 slave_dev->min_mtu = 0;
1188 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001189 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001190
Andrew Lunn448b4482015-05-06 01:09:56 +02001191 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1192 NULL);
1193
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001194 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1195 slave_dev->dev.of_node = port->dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001196 slave_dev->vlan_features = master->vlan_features;
1197
1198 p = netdev_priv(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001199 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1200 if (!p->stats64) {
1201 free_netdev(slave_dev);
1202 return -ENOMEM;
1203 }
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001204 p->dp = port;
Florian Fainellif50f2122017-01-30 12:41:40 -08001205 INIT_LIST_HEAD(&p->mall_tc_list);
Vivien Didelot15240242017-09-29 17:19:18 -04001206 p->xmit = cpu_dp->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001207
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001208 p->old_pause = -1;
1209 p->old_link = -1;
1210 p->old_duplex = -1;
1211
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001212 port->slave = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001213
1214 netif_carrier_off(slave_dev);
1215
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001216 ret = dsa_slave_phy_setup(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001217 if (ret) {
1218 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainellie8044412017-09-25 15:55:53 -07001219 goto out_free;
1220 }
1221
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001222 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
Florian Fainelli60724d42017-10-11 10:57:48 -07001223
Florian Fainellie8044412017-09-25 15:55:53 -07001224 ret = register_netdev(slave_dev);
1225 if (ret) {
1226 netdev_err(master, "error %d registering interface %s\n",
1227 ret, slave_dev->name);
1228 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001229 }
1230
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001231 return 0;
Florian Fainellie8044412017-09-25 15:55:53 -07001232
1233out_phy:
David S. Miller53954cf2017-10-05 17:57:03 -07001234 phy_disconnect(slave_dev->phydev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001235 if (of_phy_is_fixed_link(port->dn))
1236 of_phy_deregister_fixed_link(port->dn);
Florian Fainellie8044412017-09-25 15:55:53 -07001237out_free:
1238 free_percpu(p->stats64);
1239 free_netdev(slave_dev);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001240 port->slave = NULL;
Florian Fainellie8044412017-09-25 15:55:53 -07001241 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001242}
Florian Fainellib73adef2015-02-24 13:15:33 -08001243
Neil Armstrongcda5c152015-12-07 13:57:35 +01001244void dsa_slave_destroy(struct net_device *slave_dev)
1245{
Vivien Didelotd9450972017-10-16 11:12:15 -04001246 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001247 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001248 struct device_node *port_dn = dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001249
1250 netif_carrier_off(slave_dev);
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001251 if (slave_dev->phydev) {
1252 phy_disconnect(slave_dev->phydev);
Johan Hovold881eada2016-11-28 19:25:09 +01001253
1254 if (of_phy_is_fixed_link(port_dn))
1255 of_phy_deregister_fixed_link(port_dn);
1256 }
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001257 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001258 unregister_netdev(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001259 free_percpu(p->stats64);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001260 free_netdev(slave_dev);
1261}
1262
Florian Fainellib73adef2015-02-24 13:15:33 -08001263static bool dsa_slave_dev_check(struct net_device *dev)
1264{
1265 return dev->netdev_ops == &dsa_slave_netdev_ops;
1266}
1267
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001268static int dsa_slave_changeupper(struct net_device *dev,
1269 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001270{
Vivien Didelotd9450972017-10-16 11:12:15 -04001271 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001272 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001273
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001274 if (netif_is_bridge_master(info->upper_dev)) {
1275 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001276 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001277 err = notifier_from_errno(err);
1278 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001279 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001280 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001281 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001282 }
1283
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001284 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001285}
1286
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001287static int dsa_slave_netdevice_event(struct notifier_block *nb,
1288 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001289{
Vivien Didelot6debb682016-03-13 16:21:34 -04001290 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001291
Florian Fainelli53bade82017-09-19 18:00:37 -07001292 if (!dsa_slave_dev_check(dev))
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001293 return NOTIFY_DONE;
1294
1295 if (event == NETDEV_CHANGEUPPER)
1296 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001297
Florian Fainellib73adef2015-02-24 13:15:33 -08001298 return NOTIFY_DONE;
1299}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001300
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001301struct dsa_switchdev_event_work {
1302 struct work_struct work;
1303 struct switchdev_notifier_fdb_info fdb_info;
1304 struct net_device *dev;
1305 unsigned long event;
1306};
1307
1308static void dsa_slave_switchdev_event_work(struct work_struct *work)
1309{
1310 struct dsa_switchdev_event_work *switchdev_work =
1311 container_of(work, struct dsa_switchdev_event_work, work);
1312 struct net_device *dev = switchdev_work->dev;
1313 struct switchdev_notifier_fdb_info *fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001314 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001315 int err;
1316
1317 rtnl_lock();
1318 switch (switchdev_work->event) {
1319 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1320 fdb_info = &switchdev_work->fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001321 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001322 if (err) {
1323 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1324 break;
1325 }
1326 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1327 &fdb_info->info);
1328 break;
1329
1330 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1331 fdb_info = &switchdev_work->fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001332 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001333 if (err) {
1334 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1335 dev_close(dev);
1336 }
1337 break;
1338 }
1339 rtnl_unlock();
1340
1341 kfree(switchdev_work->fdb_info.addr);
1342 kfree(switchdev_work);
1343 dev_put(dev);
1344}
1345
1346static int
1347dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1348 switchdev_work,
1349 const struct switchdev_notifier_fdb_info *
1350 fdb_info)
1351{
1352 memcpy(&switchdev_work->fdb_info, fdb_info,
1353 sizeof(switchdev_work->fdb_info));
1354 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1355 if (!switchdev_work->fdb_info.addr)
1356 return -ENOMEM;
1357 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1358 fdb_info->addr);
1359 return 0;
1360}
1361
1362/* Called under rcu_read_lock() */
1363static int dsa_slave_switchdev_event(struct notifier_block *unused,
1364 unsigned long event, void *ptr)
1365{
1366 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1367 struct dsa_switchdev_event_work *switchdev_work;
1368
1369 if (!dsa_slave_dev_check(dev))
1370 return NOTIFY_DONE;
1371
1372 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1373 if (!switchdev_work)
1374 return NOTIFY_BAD;
1375
1376 INIT_WORK(&switchdev_work->work,
1377 dsa_slave_switchdev_event_work);
1378 switchdev_work->dev = dev;
1379 switchdev_work->event = event;
1380
1381 switch (event) {
1382 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1383 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1384 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1385 ptr))
1386 goto err_fdb_work_init;
1387 dev_hold(dev);
1388 break;
1389 default:
1390 kfree(switchdev_work);
1391 return NOTIFY_DONE;
1392 }
1393
1394 dsa_schedule_work(&switchdev_work->work);
1395 return NOTIFY_OK;
1396
1397err_fdb_work_init:
1398 kfree(switchdev_work);
1399 return NOTIFY_BAD;
1400}
1401
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001402static struct notifier_block dsa_slave_nb __read_mostly = {
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001403 .notifier_call = dsa_slave_netdevice_event,
1404};
1405
1406static struct notifier_block dsa_slave_switchdev_notifier = {
1407 .notifier_call = dsa_slave_switchdev_event,
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001408};
1409
1410int dsa_slave_register_notifier(void)
1411{
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001412 int err;
1413
1414 err = register_netdevice_notifier(&dsa_slave_nb);
1415 if (err)
1416 return err;
1417
1418 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1419 if (err)
1420 goto err_switchdev_nb;
1421
1422 return 0;
1423
1424err_switchdev_nb:
1425 unregister_netdevice_notifier(&dsa_slave_nb);
1426 return err;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001427}
1428
1429void dsa_slave_unregister_notifier(void)
1430{
1431 int err;
1432
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001433 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1434 if (err)
1435 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1436
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001437 err = unregister_netdevice_notifier(&dsa_slave_nb);
1438 if (err)
1439 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1440}