blob: d5680a98a7f021c408d51c057ad4ee5f4cea8307 [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 Fainelliaab9c402018-05-10 13:17:36 -070016#include <linux/phylink.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070017#include <linux/of_net.h>
18#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010019#include <linux/mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080020#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080021#include <net/pkt_cls.h>
22#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080023#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070024#include <linux/netpoll.h>
Brandon Streiff90af1052018-02-14 01:07:49 +010025#include <linux/ptp_classify.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040026
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include "dsa_priv.h"
28
Florian Fainellif50f2122017-01-30 12:41:40 -080029static bool dsa_slave_dev_check(struct net_device *dev);
30
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000031/* slave mii_bus handling ***************************************************/
32static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
33{
34 struct dsa_switch *ds = bus->priv;
35
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070036 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040037 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000038
39 return 0xffff;
40}
41
42static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
43{
44 struct dsa_switch *ds = bus->priv;
45
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070046 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040047 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000048
49 return 0;
50}
51
52void dsa_slave_mii_bus_init(struct dsa_switch *ds)
53{
54 ds->slave_mii_bus->priv = (void *)ds;
55 ds->slave_mii_bus->name = "dsa slave smi";
56 ds->slave_mii_bus->read = dsa_slave_phy_read;
57 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070058 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
Vivien Didelot49463b72017-11-03 19:05:21 -040059 ds->dst->index, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020060 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050061 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000062}
63
64
65/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020066static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000067{
Vivien Didelotd0006b02017-10-16 11:12:16 -040068 return dsa_slave_to_master(dev)->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000069}
70
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071static int dsa_slave_open(struct net_device *dev)
72{
Vivien Didelotd0006b02017-10-16 11:12:16 -040073 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -040074 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080075 int err;
76
77 if (!(master->flags & IFF_UP))
78 return -ENETDOWN;
79
Joe Perches8feedbb2012-05-08 18:56:57 +000080 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000081 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080082 if (err < 0)
83 goto out;
84 }
85
86 if (dev->flags & IFF_ALLMULTI) {
87 err = dev_set_allmulti(master, 1);
88 if (err < 0)
89 goto del_unicast;
90 }
91 if (dev->flags & IFF_PROMISC) {
92 err = dev_set_promiscuity(master, 1);
93 if (err < 0)
94 goto clear_allmulti;
95 }
96
Vivien Didelot0115dcd2017-09-26 17:15:32 -040097 err = dsa_port_enable(dp, dev->phydev);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040098 if (err)
99 goto clear_promisc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800100
Florian Fainelliaab9c402018-05-10 13:17:36 -0700101 phylink_start(dp->pl);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700102
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000103 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800104
Florian Fainellib2f2af22014-09-24 17:05:18 -0700105clear_promisc:
106 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300107 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800108clear_allmulti:
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
111del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000113 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800114out:
115 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000116}
117
118static int dsa_slave_close(struct net_device *dev)
119{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400120 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400121 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800122
Florian Fainelliaab9c402018-05-10 13:17:36 -0700123 phylink_stop(dp->pl);
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{
Brandon Streiff03363692018-02-14 01:07:48 +0100258 struct dsa_slave_priv *p = netdev_priv(dev);
259 struct dsa_switch *ds = p->dp->ds;
260 int port = p->dp->index;
261
262 /* Pass through to switch driver if it supports timestamping */
263 switch (cmd) {
264 case SIOCGHWTSTAMP:
265 if (ds->ops->port_hwtstamp_get)
266 return ds->ops->port_hwtstamp_get(ds, port, ifr);
267 break;
268 case SIOCSHWTSTAMP:
269 if (ds->ops->port_hwtstamp_set)
270 return ds->ops->port_hwtstamp_set(ds, port, ifr);
271 break;
272 }
273
Florian Fainelliaab9c402018-05-10 13:17:36 -0700274 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000275}
276
Scott Feldman35636062015-05-10 09:47:51 -0700277static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200278 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200279 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700280{
Vivien Didelotd9450972017-10-16 11:12:15 -0400281 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400282 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700283
284 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200285 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400286 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700287 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500288 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400289 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
290 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500291 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400292 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400293 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400294 break;
Scott Feldman35636062015-05-10 09:47:51 -0700295 default:
296 ret = -EOPNOTSUPP;
297 break;
298 }
299
300 return ret;
301}
302
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400303static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200304 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200305 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400306{
Vivien Didelotd9450972017-10-16 11:12:15 -0400307 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400308 int err;
309
310 /* For the prepare phase, ensure the full set of changes is feasable in
311 * one go in order to signal a failure properly. If an operation is not
312 * supported, return -EOPNOTSUPP.
313 */
314
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200315 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400316 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400317 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400318 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100319 case SWITCHDEV_OBJ_ID_HOST_MDB:
320 /* DSA can directly translate this to a normal MDB add,
321 * but on the CPU port.
322 */
323 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
324 trans);
325 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200326 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d122017-05-19 17:00:41 -0400327 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
328 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400329 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400330 default:
331 err = -EOPNOTSUPP;
332 break;
333 }
334
335 return err;
336}
337
338static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200339 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400340{
Vivien Didelotd9450972017-10-16 11:12:15 -0400341 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400342 int err;
343
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200344 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400345 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400346 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400347 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100348 case SWITCHDEV_OBJ_ID_HOST_MDB:
349 /* DSA can directly translate this to a normal MDB add,
350 * but on the CPU port.
351 */
352 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
353 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200354 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d122017-05-19 17:00:41 -0400355 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400356 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400357 default:
358 err = -EOPNOTSUPP;
359 break;
360 }
361
362 return err;
363}
364
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700365static int dsa_slave_port_attr_get(struct net_device *dev,
366 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800367{
Vivien Didelotd9450972017-10-16 11:12:15 -0400368 struct dsa_port *dp = dsa_slave_to_port(dev);
369 struct dsa_switch *ds = dp->ds;
Andrew Lunna42c8e32017-11-09 22:29:51 +0100370 struct dsa_switch_tree *dst = ds->dst;
Florian Fainellib73adef2015-02-24 13:15:33 -0800371
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700372 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200373 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Andrew Lunna42c8e32017-11-09 22:29:51 +0100374 attr->u.ppid.id_len = sizeof(dst->index);
375 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700376 break;
Arkadi Sharshevskyc9e21052017-08-06 16:15:44 +0300377 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
378 attr->u.brport_flags_support = 0;
379 break;
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700380 default:
381 return -EOPNOTSUPP;
382 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800383
384 return 0;
385}
386
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400387static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
388 struct sk_buff *skb)
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700389{
390#ifdef CONFIG_NET_POLL_CONTROLLER
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400391 struct dsa_slave_priv *p = netdev_priv(dev);
392
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700393 if (p->netpoll)
394 netpoll_send_skb(p->netpoll, skb);
395#else
396 BUG();
397#endif
398 return NETDEV_TX_OK;
399}
400
Brandon Streiff90af1052018-02-14 01:07:49 +0100401static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
402 struct sk_buff *skb)
403{
404 struct dsa_switch *ds = p->dp->ds;
405 struct sk_buff *clone;
406 unsigned int type;
407
408 type = ptp_classify_raw(skb);
409 if (type == PTP_CLASS_NONE)
410 return;
411
412 if (!ds->ops->port_txtstamp)
413 return;
414
415 clone = skb_clone_sk(skb);
416 if (!clone)
417 return;
418
419 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
420 return;
421
422 kfree_skb(clone);
423}
424
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700425static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
426{
427 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700428 struct pcpu_sw_netstats *s;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700429 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700430
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700431 s = this_cpu_ptr(p->stats64);
432 u64_stats_update_begin(&s->syncp);
433 s->tx_packets++;
434 s->tx_bytes += skb->len;
435 u64_stats_update_end(&s->syncp);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700436
Brandon Streiff90af1052018-02-14 01:07:49 +0100437 /* Identify PTP protocol packets, clone them, and pass them to the
438 * switch driver
439 */
440 dsa_skb_tx_timestamp(p, skb);
441
Vivien Didelotfe47d562017-06-01 16:07:15 -0400442 /* Transmit function may have to reallocate the original SKB,
443 * in which case it must have freed it. Only free it here on error.
444 */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700445 nskb = p->xmit(skb, dev);
Vivien Didelotfe47d562017-06-01 16:07:15 -0400446 if (!nskb) {
447 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700448 return NETDEV_TX_OK;
Vivien Didelotfe47d562017-06-01 16:07:15 -0400449 }
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700450
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700451 /* SKB for netpoll still need to be mangled with the protocol-specific
452 * tag to be successfully transmitted
453 */
454 if (unlikely(netpoll_tx_running(dev)))
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400455 return dsa_slave_netpoll_send_skb(dev, nskb);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700456
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700457 /* Queue the SKB for transmission on the parent interface, but
458 * do not modify its EtherType
459 */
Vivien Didelotd0006b02017-10-16 11:12:16 -0400460 nskb->dev = dsa_slave_to_master(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700461 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700462
463 return NETDEV_TX_OK;
464}
465
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000466/* ethtool operations *******************************************************/
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000467
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000468static void dsa_slave_get_drvinfo(struct net_device *dev,
469 struct ethtool_drvinfo *drvinfo)
470{
Jiri Pirko7826d432013-01-06 00:44:26 +0000471 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000472 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
473 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000474}
475
Guenter Roeck3d762a02014-10-29 10:45:04 -0700476static int dsa_slave_get_regs_len(struct net_device *dev)
477{
Vivien Didelotd9450972017-10-16 11:12:15 -0400478 struct dsa_port *dp = dsa_slave_to_port(dev);
479 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700480
Vivien Didelot9d490b42016-08-23 12:38:56 -0400481 if (ds->ops->get_regs_len)
Vivien Didelotd9450972017-10-16 11:12:15 -0400482 return ds->ops->get_regs_len(ds, dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700483
484 return -EOPNOTSUPP;
485}
486
487static void
488dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
489{
Vivien Didelotd9450972017-10-16 11:12:15 -0400490 struct dsa_port *dp = dsa_slave_to_port(dev);
491 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700492
Vivien Didelot9d490b42016-08-23 12:38:56 -0400493 if (ds->ops->get_regs)
Vivien Didelotd9450972017-10-16 11:12:15 -0400494 ds->ops->get_regs(ds, dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700495}
496
Florian Fainelliaab9c402018-05-10 13:17:36 -0700497static int dsa_slave_nway_reset(struct net_device *dev)
498{
499 struct dsa_port *dp = dsa_slave_to_port(dev);
500
501 return phylink_ethtool_nway_reset(dp->pl);
502}
503
Guenter Roeck6793abb2014-10-29 10:45:01 -0700504static int dsa_slave_get_eeprom_len(struct net_device *dev)
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;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700508
Andrew Lunn0e576042016-06-04 21:16:52 +0200509 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200510 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700511
Vivien Didelot9d490b42016-08-23 12:38:56 -0400512 if (ds->ops->get_eeprom_len)
513 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700514
515 return 0;
516}
517
518static int dsa_slave_get_eeprom(struct net_device *dev,
519 struct ethtool_eeprom *eeprom, u8 *data)
520{
Vivien Didelotd9450972017-10-16 11:12:15 -0400521 struct dsa_port *dp = dsa_slave_to_port(dev);
522 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700523
Vivien Didelot9d490b42016-08-23 12:38:56 -0400524 if (ds->ops->get_eeprom)
525 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700526
527 return -EOPNOTSUPP;
528}
529
530static int dsa_slave_set_eeprom(struct net_device *dev,
531 struct ethtool_eeprom *eeprom, u8 *data)
532{
Vivien Didelotd9450972017-10-16 11:12:15 -0400533 struct dsa_port *dp = dsa_slave_to_port(dev);
534 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700535
Vivien Didelot9d490b42016-08-23 12:38:56 -0400536 if (ds->ops->set_eeprom)
537 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700538
539 return -EOPNOTSUPP;
540}
541
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000542static void dsa_slave_get_strings(struct net_device *dev,
543 uint32_t stringset, uint8_t *data)
544{
Vivien Didelotd9450972017-10-16 11:12:15 -0400545 struct dsa_port *dp = dsa_slave_to_port(dev);
546 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000547
548 if (stringset == ETH_SS_STATS) {
549 int len = ETH_GSTRING_LEN;
550
551 strncpy(data, "tx_packets", len);
552 strncpy(data + len, "tx_bytes", len);
553 strncpy(data + 2 * len, "rx_packets", len);
554 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400555 if (ds->ops->get_strings)
Florian Fainelli89f09042018-04-25 12:12:50 -0700556 ds->ops->get_strings(ds, dp->index, stringset,
557 data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000558 }
559}
560
561static void dsa_slave_get_ethtool_stats(struct net_device *dev,
562 struct ethtool_stats *stats,
563 uint64_t *data)
564{
Vivien Didelotd9450972017-10-16 11:12:15 -0400565 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000566 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400567 struct dsa_switch *ds = dp->ds;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700568 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700569 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700570 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000571
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700572 for_each_possible_cpu(i) {
573 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
574
575 s = per_cpu_ptr(p->stats64, i);
576 do {
577 start = u64_stats_fetch_begin_irq(&s->syncp);
578 tx_packets = s->tx_packets;
579 tx_bytes = s->tx_bytes;
580 rx_packets = s->rx_packets;
581 rx_bytes = s->rx_bytes;
582 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
583 data[0] += tx_packets;
584 data[1] += tx_bytes;
585 data[2] += rx_packets;
586 data[3] += rx_bytes;
587 }
Vivien Didelot9d490b42016-08-23 12:38:56 -0400588 if (ds->ops->get_ethtool_stats)
Vivien Didelotd9450972017-10-16 11:12:15 -0400589 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590}
591
592static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
593{
Vivien Didelotd9450972017-10-16 11:12:15 -0400594 struct dsa_port *dp = dsa_slave_to_port(dev);
595 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596
597 if (sset == ETH_SS_STATS) {
598 int count;
599
600 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400601 if (ds->ops->get_sset_count)
Florian Fainelli89f09042018-04-25 12:12:50 -0700602 count += ds->ops->get_sset_count(ds, dp->index, sset);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603
604 return count;
605 }
606
607 return -EOPNOTSUPP;
608}
609
Florian Fainelli19e57c42014-09-18 17:31:24 -0700610static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
611{
Vivien Didelotd9450972017-10-16 11:12:15 -0400612 struct dsa_port *dp = dsa_slave_to_port(dev);
613 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700614
Florian Fainelliaab9c402018-05-10 13:17:36 -0700615 phylink_ethtool_get_wol(dp->pl, w);
616
Vivien Didelot9d490b42016-08-23 12:38:56 -0400617 if (ds->ops->get_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400618 ds->ops->get_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700619}
620
621static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
622{
Vivien Didelotd9450972017-10-16 11:12:15 -0400623 struct dsa_port *dp = dsa_slave_to_port(dev);
624 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700625 int ret = -EOPNOTSUPP;
626
Florian Fainelliaab9c402018-05-10 13:17:36 -0700627 phylink_ethtool_set_wol(dp->pl, w);
628
Vivien Didelot9d490b42016-08-23 12:38:56 -0400629 if (ds->ops->set_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400630 ret = ds->ops->set_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700631
632 return ret;
633}
634
Florian Fainelli79052882014-09-24 17:05:21 -0700635static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
636{
Vivien Didelotd9450972017-10-16 11:12:15 -0400637 struct dsa_port *dp = dsa_slave_to_port(dev);
638 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700639 int ret;
640
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400641 /* Port's PHY and MAC both need to be EEE capable */
Andrew Lunn1be52e92018-08-08 20:56:40 +0200642 if (!dev->phydev && !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400643 return -ENODEV;
644
Vivien Didelot08f50062017-08-01 16:32:41 -0400645 if (!ds->ops->set_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700646 return -EOPNOTSUPP;
647
Vivien Didelotd9450972017-10-16 11:12:15 -0400648 ret = ds->ops->set_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700649 if (ret)
650 return ret;
651
Florian Fainelliaab9c402018-05-10 13:17:36 -0700652 return phylink_ethtool_set_eee(dp->pl, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700653}
654
655static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
656{
Vivien Didelotd9450972017-10-16 11:12:15 -0400657 struct dsa_port *dp = dsa_slave_to_port(dev);
658 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700659 int ret;
660
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400661 /* Port's PHY and MAC both need to be EEE capable */
Andrew Lunn1be52e92018-08-08 20:56:40 +0200662 if (!dev->phydev && !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400663 return -ENODEV;
664
Vivien Didelot08f50062017-08-01 16:32:41 -0400665 if (!ds->ops->get_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700666 return -EOPNOTSUPP;
667
Vivien Didelotd9450972017-10-16 11:12:15 -0400668 ret = ds->ops->get_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700669 if (ret)
670 return ret;
671
Florian Fainelliaab9c402018-05-10 13:17:36 -0700672 return phylink_ethtool_get_eee(dp->pl, e);
673}
674
675static int dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
677{
678 struct dsa_port *dp = dsa_slave_to_port(dev);
679
680 return phylink_ethtool_ksettings_get(dp->pl, cmd);
681}
682
683static int dsa_slave_set_link_ksettings(struct net_device *dev,
684 const struct ethtool_link_ksettings *cmd)
685{
686 struct dsa_port *dp = dsa_slave_to_port(dev);
687
688 return phylink_ethtool_ksettings_set(dp->pl, cmd);
Florian Fainelli79052882014-09-24 17:05:21 -0700689}
690
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700691#ifdef CONFIG_NET_POLL_CONTROLLER
692static int dsa_slave_netpoll_setup(struct net_device *dev,
693 struct netpoll_info *ni)
694{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400695 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700696 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700697 struct netpoll *netpoll;
698 int err = 0;
699
700 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
701 if (!netpoll)
702 return -ENOMEM;
703
704 err = __netpoll_setup(netpoll, master);
705 if (err) {
706 kfree(netpoll);
707 goto out;
708 }
709
710 p->netpoll = netpoll;
711out:
712 return err;
713}
714
715static void dsa_slave_netpoll_cleanup(struct net_device *dev)
716{
717 struct dsa_slave_priv *p = netdev_priv(dev);
718 struct netpoll *netpoll = p->netpoll;
719
720 if (!netpoll)
721 return;
722
723 p->netpoll = NULL;
724
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400725 __netpoll_free(netpoll);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700726}
727
728static void dsa_slave_poll_controller(struct net_device *dev)
729{
730}
731#endif
732
Florian Fainelli44bb7652017-01-10 12:32:36 -0800733static int dsa_slave_get_phys_port_name(struct net_device *dev,
734 char *name, size_t len)
735{
Vivien Didelotd9450972017-10-16 11:12:15 -0400736 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli44bb7652017-01-10 12:32:36 -0800737
Vivien Didelotd9450972017-10-16 11:12:15 -0400738 if (snprintf(name, len, "p%d", dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800739 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800740
741 return 0;
742}
743
Florian Fainellif50f2122017-01-30 12:41:40 -0800744static struct dsa_mall_tc_entry *
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400745dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
Florian Fainellif50f2122017-01-30 12:41:40 -0800746{
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400747 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800748 struct dsa_mall_tc_entry *mall_tc_entry;
749
750 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
751 if (mall_tc_entry->cookie == cookie)
752 return mall_tc_entry;
753
754 return NULL;
755}
756
757static int dsa_slave_add_cls_matchall(struct net_device *dev,
Florian Fainellif50f2122017-01-30 12:41:40 -0800758 struct tc_cls_matchall_offload *cls,
759 bool ingress)
760{
Vivien Didelotd9450972017-10-16 11:12:15 -0400761 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800762 struct dsa_slave_priv *p = netdev_priv(dev);
763 struct dsa_mall_tc_entry *mall_tc_entry;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200764 __be16 protocol = cls->common.protocol;
Vivien Didelotd9450972017-10-16 11:12:15 -0400765 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800766 struct net_device *to_dev;
767 const struct tc_action *a;
Vivien Didelotd9450972017-10-16 11:12:15 -0400768 struct dsa_port *to_dp;
Florian Fainellif50f2122017-01-30 12:41:40 -0800769 int err = -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800770
771 if (!ds->ops->port_mirror_add)
772 return err;
773
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200774 if (!tcf_exts_has_one_action(cls->exts))
Florian Fainellif50f2122017-01-30 12:41:40 -0800775 return err;
776
Cong Wang244cd962018-08-19 12:22:09 -0700777 a = tcf_exts_first_action(cls->exts);
Florian Fainellif50f2122017-01-30 12:41:40 -0800778
779 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
780 struct dsa_mall_mirror_tc_entry *mirror;
781
Cong Wang9f8a7392017-12-05 16:17:26 -0800782 to_dev = tcf_mirred_dev(a);
Florian Fainellif50f2122017-01-30 12:41:40 -0800783 if (!to_dev)
784 return -EINVAL;
785
786 if (!dsa_slave_dev_check(to_dev))
787 return -EOPNOTSUPP;
788
789 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
790 if (!mall_tc_entry)
791 return -ENOMEM;
792
793 mall_tc_entry->cookie = cls->cookie;
794 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
795 mirror = &mall_tc_entry->mirror;
796
Vivien Didelotd9450972017-10-16 11:12:15 -0400797 to_dp = dsa_slave_to_port(to_dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800798
Vivien Didelotd9450972017-10-16 11:12:15 -0400799 mirror->to_local_port = to_dp->index;
Florian Fainellif50f2122017-01-30 12:41:40 -0800800 mirror->ingress = ingress;
801
Vivien Didelotd9450972017-10-16 11:12:15 -0400802 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
Florian Fainellif50f2122017-01-30 12:41:40 -0800803 if (err) {
804 kfree(mall_tc_entry);
805 return err;
806 }
807
808 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
809 }
810
811 return 0;
812}
813
814static void dsa_slave_del_cls_matchall(struct net_device *dev,
815 struct tc_cls_matchall_offload *cls)
816{
Vivien Didelotd9450972017-10-16 11:12:15 -0400817 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800818 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -0400819 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800820
821 if (!ds->ops->port_mirror_del)
822 return;
823
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400824 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
Florian Fainellif50f2122017-01-30 12:41:40 -0800825 if (!mall_tc_entry)
826 return;
827
828 list_del(&mall_tc_entry->list);
829
830 switch (mall_tc_entry->type) {
831 case DSA_PORT_MALL_MIRROR:
Vivien Didelotd9450972017-10-16 11:12:15 -0400832 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
Florian Fainellif50f2122017-01-30 12:41:40 -0800833 break;
834 default:
835 WARN_ON(1);
836 }
837
838 kfree(mall_tc_entry);
839}
840
Jiri Pirko3fbae382017-08-07 10:15:26 +0200841static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200842 struct tc_cls_matchall_offload *cls,
843 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -0800844{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200845 if (cls->common.chain_index)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200846 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800847
Jiri Pirko3fbae382017-08-07 10:15:26 +0200848 switch (cls->command) {
849 case TC_CLSMATCHALL_REPLACE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200850 return dsa_slave_add_cls_matchall(dev, cls, ingress);
Jiri Pirko3fbae382017-08-07 10:15:26 +0200851 case TC_CLSMATCHALL_DESTROY:
852 dsa_slave_del_cls_matchall(dev, cls);
853 return 0;
854 default:
855 return -EOPNOTSUPP;
856 }
857}
858
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200859static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
860 void *cb_priv, bool ingress)
861{
862 struct net_device *dev = cb_priv;
863
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100864 if (!tc_can_offload(dev))
865 return -EOPNOTSUPP;
866
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200867 switch (type) {
868 case TC_SETUP_CLSMATCHALL:
869 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
870 default:
871 return -EOPNOTSUPP;
872 }
873}
874
875static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
876 void *type_data, void *cb_priv)
877{
878 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
879}
880
881static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
882 void *type_data, void *cb_priv)
883{
884 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
885}
886
887static int dsa_slave_setup_tc_block(struct net_device *dev,
888 struct tc_block_offload *f)
889{
890 tc_setup_cb_t *cb;
891
892 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
893 cb = dsa_slave_setup_tc_block_cb_ig;
894 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
895 cb = dsa_slave_setup_tc_block_cb_eg;
896 else
897 return -EOPNOTSUPP;
898
899 switch (f->command) {
900 case TC_BLOCK_BIND:
John Hurley60513bd2018-06-25 14:30:04 -0700901 return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200902 case TC_BLOCK_UNBIND:
903 tcf_block_cb_unregister(f->block, cb, dev);
904 return 0;
905 default:
906 return -EOPNOTSUPP;
907 }
908}
909
Jiri Pirko3fbae382017-08-07 10:15:26 +0200910static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200911 void *type_data)
Jiri Pirko3fbae382017-08-07 10:15:26 +0200912{
Jiri Pirko2572ac52017-08-07 10:15:17 +0200913 switch (type) {
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200914 case TC_SETUP_BLOCK:
915 return dsa_slave_setup_tc_block(dev, type_data);
Florian Fainellif50f2122017-01-30 12:41:40 -0800916 default:
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200917 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800918 }
Florian Fainellif50f2122017-01-30 12:41:40 -0800919}
920
Florian Fainellif613ed62017-08-01 15:00:36 -0700921static void dsa_slave_get_stats64(struct net_device *dev,
922 struct rtnl_link_stats64 *stats)
923{
924 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700925 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700926 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700927 int i;
Florian Fainellif613ed62017-08-01 15:00:36 -0700928
929 netdev_stats_to_stats64(stats, &dev->stats);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700930 for_each_possible_cpu(i) {
931 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
932
933 s = per_cpu_ptr(p->stats64, i);
934 do {
935 start = u64_stats_fetch_begin_irq(&s->syncp);
936 tx_packets = s->tx_packets;
937 tx_bytes = s->tx_bytes;
938 rx_packets = s->rx_packets;
939 rx_bytes = s->rx_bytes;
940 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
941
942 stats->tx_packets += tx_packets;
943 stats->tx_bytes += tx_bytes;
944 stats->rx_packets += rx_packets;
945 stats->rx_bytes += rx_bytes;
946 }
Florian Fainellif613ed62017-08-01 15:00:36 -0700947}
948
Florian Fainellibf9f2642017-01-30 09:48:40 -0800949static int dsa_slave_get_rxnfc(struct net_device *dev,
950 struct ethtool_rxnfc *nfc, u32 *rule_locs)
951{
Vivien Didelotd9450972017-10-16 11:12:15 -0400952 struct dsa_port *dp = dsa_slave_to_port(dev);
953 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800954
955 if (!ds->ops->get_rxnfc)
956 return -EOPNOTSUPP;
957
Vivien Didelotd9450972017-10-16 11:12:15 -0400958 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800959}
960
961static int dsa_slave_set_rxnfc(struct net_device *dev,
962 struct ethtool_rxnfc *nfc)
963{
Vivien Didelotd9450972017-10-16 11:12:15 -0400964 struct dsa_port *dp = dsa_slave_to_port(dev);
965 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800966
967 if (!ds->ops->set_rxnfc)
968 return -EOPNOTSUPP;
969
Vivien Didelotd9450972017-10-16 11:12:15 -0400970 return ds->ops->set_rxnfc(ds, dp->index, nfc);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800971}
972
Brandon Streiff03363692018-02-14 01:07:48 +0100973static int dsa_slave_get_ts_info(struct net_device *dev,
974 struct ethtool_ts_info *ts)
975{
976 struct dsa_slave_priv *p = netdev_priv(dev);
977 struct dsa_switch *ds = p->dp->ds;
978
979 if (!ds->ops->get_ts_info)
980 return -EOPNOTSUPP;
981
982 return ds->ops->get_ts_info(ds, p->dp->index, ts);
983}
984
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000985static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000986 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700987 .get_regs_len = dsa_slave_get_regs_len,
988 .get_regs = dsa_slave_get_regs,
Florian Fainelliaab9c402018-05-10 13:17:36 -0700989 .nway_reset = dsa_slave_nway_reset,
Florian Fainellic4aef9f2018-05-10 13:17:34 -0700990 .get_link = ethtool_op_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700991 .get_eeprom_len = dsa_slave_get_eeprom_len,
992 .get_eeprom = dsa_slave_get_eeprom,
993 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000994 .get_strings = dsa_slave_get_strings,
995 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
996 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700997 .set_wol = dsa_slave_set_wol,
998 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700999 .set_eee = dsa_slave_set_eee,
1000 .get_eee = dsa_slave_get_eee,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001001 .get_link_ksettings = dsa_slave_get_link_ksettings,
1002 .set_link_ksettings = dsa_slave_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001003 .get_rxnfc = dsa_slave_get_rxnfc,
1004 .set_rxnfc = dsa_slave_set_rxnfc,
Brandon Streiff03363692018-02-14 01:07:48 +01001005 .get_ts_info = dsa_slave_get_ts_info,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001006};
1007
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001008/* legacy way, bypassing the bridge *****************************************/
1009int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1010 struct net_device *dev,
1011 const unsigned char *addr, u16 vid,
Petr Machata87b09842019-01-16 23:06:50 +00001012 u16 flags,
1013 struct netlink_ext_ack *extack)
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001014{
1015 struct dsa_port *dp = dsa_slave_to_port(dev);
1016
1017 return dsa_port_fdb_add(dp, addr, vid);
1018}
1019
1020int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1021 struct net_device *dev,
1022 const unsigned char *addr, u16 vid)
1023{
1024 struct dsa_port *dp = dsa_slave_to_port(dev);
1025
1026 return dsa_port_fdb_del(dp, addr, vid);
1027}
1028
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001029static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001030 .ndo_open = dsa_slave_open,
1031 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001032 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001033 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1034 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001035 .ndo_set_mac_address = dsa_slave_set_mac_address,
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +03001036 .ndo_fdb_add = dsa_legacy_fdb_add,
1037 .ndo_fdb_del = dsa_legacy_fdb_del,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001038 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001039 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001040 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001041#ifdef CONFIG_NET_POLL_CONTROLLER
1042 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1043 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1044 .ndo_poll_controller = dsa_slave_poll_controller,
1045#endif
Florian Fainelli44bb7652017-01-10 12:32:36 -08001046 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001047 .ndo_setup_tc = dsa_slave_setup_tc,
Florian Fainellif613ed62017-08-01 15:00:36 -07001048 .ndo_get_stats64 = dsa_slave_get_stats64,
Scott Feldman98237d42015-03-15 21:07:15 -07001049};
1050
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001051static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001052 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001053 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001054};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001055
Florian Fainellif37db852015-09-23 18:19:58 -07001056static struct device_type dsa_type = {
1057 .name = "dsa",
1058};
1059
Florian Fainelliaab9c402018-05-10 13:17:36 -07001060static void dsa_slave_phylink_validate(struct net_device *dev,
1061 unsigned long *supported,
1062 struct phylink_link_state *state)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001063{
Vivien Didelotd9450972017-10-16 11:12:15 -04001064 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001065 struct dsa_switch *ds = dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001066
Florian Fainelliaab9c402018-05-10 13:17:36 -07001067 if (!ds->ops->phylink_validate)
1068 return;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001069
Florian Fainelliaab9c402018-05-10 13:17:36 -07001070 ds->ops->phylink_validate(ds, dp->index, supported, state);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001071}
1072
Florian Fainelliaab9c402018-05-10 13:17:36 -07001073static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1074 struct phylink_link_state *state)
Florian Fainellice31b312014-08-27 17:04:54 -07001075{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001076 struct dsa_port *dp = dsa_slave_to_port(dev);
1077 struct dsa_switch *ds = dp->ds;
Florian Fainellice31b312014-08-27 17:04:54 -07001078
Florian Fainelliaab9c402018-05-10 13:17:36 -07001079 /* Only called for SGMII and 802.3z */
1080 if (!ds->ops->phylink_mac_link_state)
1081 return -EOPNOTSUPP;
1082
1083 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1084}
1085
1086static void dsa_slave_phylink_mac_config(struct net_device *dev,
1087 unsigned int mode,
1088 const struct phylink_link_state *state)
1089{
1090 struct dsa_port *dp = dsa_slave_to_port(dev);
1091 struct dsa_switch *ds = dp->ds;
1092
1093 if (!ds->ops->phylink_mac_config)
1094 return;
1095
1096 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1097}
1098
1099static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1100{
1101 struct dsa_port *dp = dsa_slave_to_port(dev);
1102 struct dsa_switch *ds = dp->ds;
1103
1104 if (!ds->ops->phylink_mac_an_restart)
1105 return;
1106
1107 ds->ops->phylink_mac_an_restart(ds, dp->index);
1108}
1109
1110static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1111 unsigned int mode,
1112 phy_interface_t interface)
1113{
1114 struct dsa_port *dp = dsa_slave_to_port(dev);
1115 struct dsa_switch *ds = dp->ds;
1116
1117 if (!ds->ops->phylink_mac_link_down) {
1118 if (ds->ops->adjust_link && dev->phydev)
1119 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1120 return;
Andrew Lunnb71be352016-03-12 00:01:37 +01001121 }
Florian Fainellice31b312014-08-27 17:04:54 -07001122
Florian Fainelliaab9c402018-05-10 13:17:36 -07001123 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1124}
1125
1126static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1127 unsigned int mode,
1128 phy_interface_t interface,
1129 struct phy_device *phydev)
1130{
1131 struct dsa_port *dp = dsa_slave_to_port(dev);
1132 struct dsa_switch *ds = dp->ds;
1133
1134 if (!ds->ops->phylink_mac_link_up) {
1135 if (ds->ops->adjust_link && dev->phydev)
1136 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1137 return;
1138 }
1139
1140 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1141}
1142
1143static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1144 .validate = dsa_slave_phylink_validate,
1145 .mac_link_state = dsa_slave_phylink_mac_link_state,
1146 .mac_config = dsa_slave_phylink_mac_config,
1147 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1148 .mac_link_down = dsa_slave_phylink_mac_link_down,
1149 .mac_link_up = dsa_slave_phylink_mac_link_up,
1150};
1151
1152void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1153{
1154 const struct dsa_port *dp = dsa_to_port(ds, port);
1155
1156 phylink_mac_change(dp->pl, up);
1157}
1158EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1159
1160static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1161 struct phylink_link_state *state)
1162{
1163 struct dsa_port *dp = dsa_slave_to_port(dev);
1164 struct dsa_switch *ds = dp->ds;
1165
1166 /* No need to check that this operation is valid, the callback would
1167 * not be called if it was not.
1168 */
1169 ds->ops->phylink_fixed_state(ds, dp->index, state);
Florian Fainellice31b312014-08-27 17:04:54 -07001170}
1171
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001172/* slave device setup *******************************************************/
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001173static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001174{
Vivien Didelotd9450972017-10-16 11:12:15 -04001175 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001176 struct dsa_switch *ds = dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001177
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001178 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1179 if (!slave_dev->phydev) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001180 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001181 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001182 }
Florian Fainellic305c162015-03-10 16:57:12 -07001183
Florian Fainelliaab9c402018-05-10 13:17:36 -07001184 return phylink_connect_phy(dp->pl, slave_dev->phydev);
Florian Fainellic305c162015-03-10 16:57:12 -07001185}
1186
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001187static int dsa_slave_phy_setup(struct net_device *slave_dev)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001188{
Vivien Didelotd9450972017-10-16 11:12:15 -04001189 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001190 struct device_node *port_dn = dp->dn;
1191 struct dsa_switch *ds = dp->ds;
Florian Fainelli68195632014-09-19 13:07:54 -07001192 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001193 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001194
Guenter Roeck19334922015-02-16 21:23:51 -08001195 mode = of_get_phy_mode(port_dn);
1196 if (mode < 0)
1197 mode = PHY_INTERFACE_MODE_NA;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001198
Florian Fainelliaab9c402018-05-10 13:17:36 -07001199 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1200 &dsa_slave_phylink_mac_ops);
1201 if (IS_ERR(dp->pl)) {
1202 netdev_err(slave_dev,
1203 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1204 return PTR_ERR(dp->pl);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001205 }
1206
Florian Fainelliaab9c402018-05-10 13:17:36 -07001207 /* Register only if the switch provides such a callback, since this
1208 * callback takes precedence over polling the link GPIO in PHYLINK
1209 * (see phylink_get_fixed_state).
1210 */
1211 if (ds->ops->phylink_fixed_state)
1212 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1213
Vivien Didelot9d490b42016-08-23 12:38:56 -04001214 if (ds->ops->get_phy_flags)
Vivien Didelotd9450972017-10-16 11:12:15 -04001215 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001216
Florian Fainelliaab9c402018-05-10 13:17:36 -07001217 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1218 if (ret == -ENODEV) {
1219 /* We could not connect to a designated PHY or SFP, so use the
1220 * switch internal MDIO bus instead
1221 */
Vivien Didelotd9450972017-10-16 11:12:15 -04001222 ret = dsa_slave_phy_connect(slave_dev, dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001223 if (ret) {
Florian Fainelliaab9c402018-05-10 13:17:36 -07001224 netdev_err(slave_dev,
1225 "failed to connect to port %d: %d\n",
Vivien Didelotd9450972017-10-16 11:12:15 -04001226 dp->index, ret);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001227 phylink_destroy(dp->pl);
Florian Fainellic305c162015-03-10 16:57:12 -07001228 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001229 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001230 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001231
1232 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001233}
1234
Andrew Lunn448b4482015-05-06 01:09:56 +02001235static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1236static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1237 struct netdev_queue *txq,
1238 void *_unused)
1239{
1240 lockdep_set_class(&txq->_xmit_lock,
1241 &dsa_slave_netdev_xmit_lock_key);
1242}
1243
Florian Fainelli24462542014-09-18 17:31:22 -07001244int dsa_slave_suspend(struct net_device *slave_dev)
1245{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001246 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Florian Fainelli24462542014-09-18 17:31:22 -07001247
Florian Fainellia94c6892018-07-31 17:12:52 -07001248 if (!netif_running(slave_dev))
1249 return 0;
1250
Florian Fainellif154be22017-01-25 09:10:41 -08001251 netif_device_detach(slave_dev);
1252
Florian Fainelliaab9c402018-05-10 13:17:36 -07001253 rtnl_lock();
1254 phylink_stop(dp->pl);
1255 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001256
1257 return 0;
1258}
1259
1260int dsa_slave_resume(struct net_device *slave_dev)
1261{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001262 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1263
Florian Fainellia94c6892018-07-31 17:12:52 -07001264 if (!netif_running(slave_dev))
1265 return 0;
1266
Florian Fainelli24462542014-09-18 17:31:22 -07001267 netif_device_attach(slave_dev);
1268
Florian Fainelliaab9c402018-05-10 13:17:36 -07001269 rtnl_lock();
1270 phylink_start(dp->pl);
1271 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001272
1273 return 0;
1274}
1275
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001276static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1277{
Vivien Didelotd0006b02017-10-16 11:12:16 -04001278 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001279 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001280 struct dsa_notifier_register_info rinfo = {
1281 .switch_number = dp->ds->index,
1282 .port_number = dp->index,
1283 .master = master,
1284 .info.dev = dev,
1285 };
1286
1287 call_dsa_notifiers(val, dev, &rinfo.info);
1288}
1289
Vivien Didelot951259aa2017-10-27 15:55:19 -04001290int dsa_slave_create(struct dsa_port *port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001291{
Vivien Didelot24a93322017-11-06 16:11:43 -05001292 const struct dsa_port *cpu_dp = port->cpu_dp;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001293 struct net_device *master = cpu_dp->master;
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001294 struct dsa_switch *ds = port->ds;
Vivien Didelot951259aa2017-10-27 15:55:19 -04001295 const char *name = port->name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001296 struct net_device *slave_dev;
1297 struct dsa_slave_priv *p;
1298 int ret;
1299
Florian Fainelli55199df2017-09-03 20:27:00 -07001300 if (!ds->num_tx_queues)
1301 ds->num_tx_queues = 1;
1302
1303 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1304 NET_NAME_UNKNOWN, ether_setup,
1305 ds->num_tx_queues, 1);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001306 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001307 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001308
Florian Fainellif50f2122017-01-30 12:41:40 -08001309 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1310 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001311 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001312 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001313 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001314 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001315 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001316 slave_dev->min_mtu = 0;
1317 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001318 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001319
Andrew Lunn448b4482015-05-06 01:09:56 +02001320 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1321 NULL);
1322
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001323 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1324 slave_dev->dev.of_node = port->dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001325 slave_dev->vlan_features = master->vlan_features;
1326
1327 p = netdev_priv(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001328 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1329 if (!p->stats64) {
1330 free_netdev(slave_dev);
1331 return -ENOMEM;
1332 }
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001333 p->dp = port;
Florian Fainellif50f2122017-01-30 12:41:40 -08001334 INIT_LIST_HEAD(&p->mall_tc_list);
Vivien Didelot15240242017-09-29 17:19:18 -04001335 p->xmit = cpu_dp->tag_ops->xmit;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001336 port->slave = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001337
1338 netif_carrier_off(slave_dev);
1339
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001340 ret = dsa_slave_phy_setup(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001341 if (ret) {
1342 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainellie8044412017-09-25 15:55:53 -07001343 goto out_free;
1344 }
1345
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001346 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
Florian Fainelli60724d42017-10-11 10:57:48 -07001347
Florian Fainellie8044412017-09-25 15:55:53 -07001348 ret = register_netdev(slave_dev);
1349 if (ret) {
1350 netdev_err(master, "error %d registering interface %s\n",
1351 ret, slave_dev->name);
1352 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001353 }
1354
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001355 return 0;
Florian Fainellie8044412017-09-25 15:55:53 -07001356
1357out_phy:
Florian Fainelliaab9c402018-05-10 13:17:36 -07001358 rtnl_lock();
1359 phylink_disconnect_phy(p->dp->pl);
1360 rtnl_unlock();
1361 phylink_destroy(p->dp->pl);
Florian Fainellie8044412017-09-25 15:55:53 -07001362out_free:
1363 free_percpu(p->stats64);
1364 free_netdev(slave_dev);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001365 port->slave = NULL;
Florian Fainellie8044412017-09-25 15:55:53 -07001366 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001367}
Florian Fainellib73adef2015-02-24 13:15:33 -08001368
Neil Armstrongcda5c152015-12-07 13:57:35 +01001369void dsa_slave_destroy(struct net_device *slave_dev)
1370{
Vivien Didelotd9450972017-10-16 11:12:15 -04001371 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001372 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1373
1374 netif_carrier_off(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001375 rtnl_lock();
1376 phylink_disconnect_phy(dp->pl);
1377 rtnl_unlock();
Johan Hovold881eada2016-11-28 19:25:09 +01001378
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001379 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001380 unregister_netdev(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001381 phylink_destroy(dp->pl);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001382 free_percpu(p->stats64);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001383 free_netdev(slave_dev);
1384}
1385
Florian Fainellib73adef2015-02-24 13:15:33 -08001386static bool dsa_slave_dev_check(struct net_device *dev)
1387{
1388 return dev->netdev_ops == &dsa_slave_netdev_ops;
1389}
1390
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001391static int dsa_slave_changeupper(struct net_device *dev,
1392 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001393{
Vivien Didelotd9450972017-10-16 11:12:15 -04001394 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001395 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001396
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001397 if (netif_is_bridge_master(info->upper_dev)) {
1398 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001399 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001400 err = notifier_from_errno(err);
1401 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001402 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001403 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001404 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001405 }
1406
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001407 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001408}
1409
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001410static int dsa_slave_netdevice_event(struct notifier_block *nb,
1411 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001412{
Vivien Didelot6debb682016-03-13 16:21:34 -04001413 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001414
Florian Fainelli53bade82017-09-19 18:00:37 -07001415 if (!dsa_slave_dev_check(dev))
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001416 return NOTIFY_DONE;
1417
1418 if (event == NETDEV_CHANGEUPPER)
1419 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001420
Florian Fainellib73adef2015-02-24 13:15:33 -08001421 return NOTIFY_DONE;
1422}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001423
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001424struct dsa_switchdev_event_work {
1425 struct work_struct work;
1426 struct switchdev_notifier_fdb_info fdb_info;
1427 struct net_device *dev;
1428 unsigned long event;
1429};
1430
1431static void dsa_slave_switchdev_event_work(struct work_struct *work)
1432{
1433 struct dsa_switchdev_event_work *switchdev_work =
1434 container_of(work, struct dsa_switchdev_event_work, work);
1435 struct net_device *dev = switchdev_work->dev;
1436 struct switchdev_notifier_fdb_info *fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001437 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001438 int err;
1439
1440 rtnl_lock();
1441 switch (switchdev_work->event) {
1442 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1443 fdb_info = &switchdev_work->fdb_info;
Vivien Didelota37fb852018-05-08 23:03:12 -04001444 if (!fdb_info->added_by_user)
1445 break;
1446
Vivien Didelotd9450972017-10-16 11:12:15 -04001447 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001448 if (err) {
1449 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1450 break;
1451 }
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001452 fdb_info->offloaded = true;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001453 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1454 &fdb_info->info);
1455 break;
1456
1457 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1458 fdb_info = &switchdev_work->fdb_info;
Vivien Didelota37fb852018-05-08 23:03:12 -04001459 if (!fdb_info->added_by_user)
1460 break;
1461
Vivien Didelotd9450972017-10-16 11:12:15 -04001462 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001463 if (err) {
1464 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1465 dev_close(dev);
1466 }
1467 break;
1468 }
1469 rtnl_unlock();
1470
1471 kfree(switchdev_work->fdb_info.addr);
1472 kfree(switchdev_work);
1473 dev_put(dev);
1474}
1475
1476static int
1477dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1478 switchdev_work,
1479 const struct switchdev_notifier_fdb_info *
1480 fdb_info)
1481{
1482 memcpy(&switchdev_work->fdb_info, fdb_info,
1483 sizeof(switchdev_work->fdb_info));
1484 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1485 if (!switchdev_work->fdb_info.addr)
1486 return -ENOMEM;
1487 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1488 fdb_info->addr);
1489 return 0;
1490}
1491
1492/* Called under rcu_read_lock() */
1493static int dsa_slave_switchdev_event(struct notifier_block *unused,
1494 unsigned long event, void *ptr)
1495{
1496 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1497 struct dsa_switchdev_event_work *switchdev_work;
1498
1499 if (!dsa_slave_dev_check(dev))
1500 return NOTIFY_DONE;
1501
1502 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1503 if (!switchdev_work)
1504 return NOTIFY_BAD;
1505
1506 INIT_WORK(&switchdev_work->work,
1507 dsa_slave_switchdev_event_work);
1508 switchdev_work->dev = dev;
1509 switchdev_work->event = event;
1510
1511 switch (event) {
1512 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1513 case SWITCHDEV_FDB_DEL_TO_DEVICE:
Vivien Didelota37fb852018-05-08 23:03:12 -04001514 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001515 goto err_fdb_work_init;
1516 dev_hold(dev);
1517 break;
1518 default:
1519 kfree(switchdev_work);
1520 return NOTIFY_DONE;
1521 }
1522
1523 dsa_schedule_work(&switchdev_work->work);
1524 return NOTIFY_OK;
1525
1526err_fdb_work_init:
1527 kfree(switchdev_work);
1528 return NOTIFY_BAD;
1529}
1530
Petr Machata2b239f62018-11-22 23:29:05 +00001531static int
1532dsa_slave_switchdev_port_obj_event(unsigned long event,
1533 struct net_device *netdev,
1534 struct switchdev_notifier_port_obj_info *port_obj_info)
1535{
1536 int err = -EOPNOTSUPP;
1537
1538 switch (event) {
1539 case SWITCHDEV_PORT_OBJ_ADD:
1540 err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
1541 port_obj_info->trans);
1542 break;
1543 case SWITCHDEV_PORT_OBJ_DEL:
1544 err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
1545 break;
1546 }
1547
1548 port_obj_info->handled = true;
1549 return notifier_from_errno(err);
1550}
1551
1552static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1553 unsigned long event, void *ptr)
1554{
1555 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1556
1557 if (!dsa_slave_dev_check(dev))
1558 return NOTIFY_DONE;
1559
1560 switch (event) {
1561 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1562 case SWITCHDEV_PORT_OBJ_DEL:
1563 return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
1564 }
1565
1566 return NOTIFY_DONE;
1567}
1568
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001569static struct notifier_block dsa_slave_nb __read_mostly = {
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001570 .notifier_call = dsa_slave_netdevice_event,
1571};
1572
1573static struct notifier_block dsa_slave_switchdev_notifier = {
1574 .notifier_call = dsa_slave_switchdev_event,
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001575};
1576
Petr Machata2b239f62018-11-22 23:29:05 +00001577static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1578 .notifier_call = dsa_slave_switchdev_blocking_event,
1579};
1580
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001581int dsa_slave_register_notifier(void)
1582{
Petr Machata2b239f62018-11-22 23:29:05 +00001583 struct notifier_block *nb;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001584 int err;
1585
1586 err = register_netdevice_notifier(&dsa_slave_nb);
1587 if (err)
1588 return err;
1589
1590 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1591 if (err)
1592 goto err_switchdev_nb;
1593
Petr Machata2b239f62018-11-22 23:29:05 +00001594 nb = &dsa_slave_switchdev_blocking_notifier;
1595 err = register_switchdev_blocking_notifier(nb);
1596 if (err)
1597 goto err_switchdev_blocking_nb;
1598
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001599 return 0;
1600
Petr Machata2b239f62018-11-22 23:29:05 +00001601err_switchdev_blocking_nb:
1602 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001603err_switchdev_nb:
1604 unregister_netdevice_notifier(&dsa_slave_nb);
1605 return err;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001606}
1607
1608void dsa_slave_unregister_notifier(void)
1609{
Petr Machata2b239f62018-11-22 23:29:05 +00001610 struct notifier_block *nb;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001611 int err;
1612
Petr Machata2b239f62018-11-22 23:29:05 +00001613 nb = &dsa_slave_switchdev_blocking_notifier;
1614 err = unregister_switchdev_blocking_notifier(nb);
1615 if (err)
1616 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1617
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001618 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1619 if (err)
1620 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1621
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001622 err = unregister_netdevice_notifier(&dsa_slave_nb);
1623 if (err)
1624 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1625}