blob: fca9bfa8437e41e016c0118b0eb6939d26719cf2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00002/*
3 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00004 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 */
6
7#include <linux/list.h>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -08008#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -08009#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000010#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070011#include <linux/phy_fixed.h>
Florian Fainelliaab9c402018-05-10 13:17:36 -070012#include <linux/phylink.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070013#include <linux/of_net.h>
14#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010015#include <linux/mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080016#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080017#include <net/pkt_cls.h>
18#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080019#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070020#include <linux/netpoll.h>
Brandon Streiff90af1052018-02-14 01:07:49 +010021#include <linux/ptp_classify.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040022
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023#include "dsa_priv.h"
24
25/* slave mii_bus handling ***************************************************/
26static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27{
28 struct dsa_switch *ds = bus->priv;
29
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070030 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040031 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032
33 return 0xffff;
34}
35
36static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37{
38 struct dsa_switch *ds = bus->priv;
39
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070040 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040041 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042
43 return 0;
44}
45
46void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47{
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070052 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
Vivien Didelot49463b72017-11-03 19:05:21 -040053 ds->dst->index, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020054 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050055 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056}
57
58
59/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020060static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000061{
Vivien Didelotd0006b02017-10-16 11:12:16 -040062 return dsa_slave_to_master(dev)->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000063}
64
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065static int dsa_slave_open(struct net_device *dev)
66{
Vivien Didelotd0006b02017-10-16 11:12:16 -040067 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -040068 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080069 int err;
70
71 if (!(master->flags & IFF_UP))
72 return -ENETDOWN;
73
Joe Perches8feedbb2012-05-08 18:56:57 +000074 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000075 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080076 if (err < 0)
77 goto out;
78 }
79
80 if (dev->flags & IFF_ALLMULTI) {
81 err = dev_set_allmulti(master, 1);
82 if (err < 0)
83 goto del_unicast;
84 }
85 if (dev->flags & IFF_PROMISC) {
86 err = dev_set_promiscuity(master, 1);
87 if (err < 0)
88 goto clear_allmulti;
89 }
90
Vivien Didelot0115dcd2017-09-26 17:15:32 -040091 err = dsa_port_enable(dp, dev->phydev);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040092 if (err)
93 goto clear_promisc;
Florian Fainellib73adef2015-02-24 13:15:33 -080094
Florian Fainelliaab9c402018-05-10 13:17:36 -070095 phylink_start(dp->pl);
Florian Fainellif7f1de52014-09-24 17:05:17 -070096
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080098
Florian Fainellib2f2af22014-09-24 17:05:18 -070099clear_promisc:
100 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300101 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800102clear_allmulti:
103 if (dev->flags & IFF_ALLMULTI)
104 dev_set_allmulti(master, -1);
105del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000106 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000107 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800108out:
109 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110}
111
112static int dsa_slave_close(struct net_device *dev)
113{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400114 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400115 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800116
Florian Fainelliaab9c402018-05-10 13:17:36 -0700117 phylink_stop(dp->pl);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700118
Andrew Lunn75104db2019-02-24 20:44:43 +0100119 dsa_port_disable(dp);
Vivien Didelot6457edf2017-09-22 19:01:55 -0400120
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800121 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000122 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800123 if (dev->flags & IFF_ALLMULTI)
124 dev_set_allmulti(master, -1);
125 if (dev->flags & IFF_PROMISC)
126 dev_set_promiscuity(master, -1);
127
Joe Perches8feedbb2012-05-08 18:56:57 +0000128 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000129 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800130
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000131 return 0;
132}
133
134static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
135{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400136 struct net_device *master = dsa_slave_to_master(dev);
Rundong Ge17ab4f62019-02-02 14:29:35 +0000137 if (dev->flags & IFF_UP) {
138 if (change & IFF_ALLMULTI)
139 dev_set_allmulti(master,
140 dev->flags & IFF_ALLMULTI ? 1 : -1);
141 if (change & IFF_PROMISC)
142 dev_set_promiscuity(master,
143 dev->flags & IFF_PROMISC ? 1 : -1);
144 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000145}
146
147static void dsa_slave_set_rx_mode(struct net_device *dev)
148{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400149 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000150
151 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000152 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000153}
154
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800155static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000156{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400157 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800158 struct sockaddr *addr = a;
159 int err;
160
161 if (!is_valid_ether_addr(addr->sa_data))
162 return -EADDRNOTAVAIL;
163
164 if (!(dev->flags & IFF_UP))
165 goto out;
166
Joe Perches8feedbb2012-05-08 18:56:57 +0000167 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000168 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800169 if (err < 0)
170 return err;
171 }
172
Joe Perches8feedbb2012-05-08 18:56:57 +0000173 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000174 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800175
176out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800177 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000178
179 return 0;
180}
181
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300182struct dsa_slave_dump_ctx {
183 struct net_device *dev;
184 struct sk_buff *skb;
185 struct netlink_callback *cb;
186 int idx;
187};
188
189static int
190dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
191 bool is_static, void *data)
192{
193 struct dsa_slave_dump_ctx *dump = data;
194 u32 portid = NETLINK_CB(dump->cb->skb).portid;
195 u32 seq = dump->cb->nlh->nlmsg_seq;
196 struct nlmsghdr *nlh;
197 struct ndmsg *ndm;
198
199 if (dump->idx < dump->cb->args[2])
200 goto skip;
201
202 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
203 sizeof(*ndm), NLM_F_MULTI);
204 if (!nlh)
205 return -EMSGSIZE;
206
207 ndm = nlmsg_data(nlh);
208 ndm->ndm_family = AF_BRIDGE;
209 ndm->ndm_pad1 = 0;
210 ndm->ndm_pad2 = 0;
211 ndm->ndm_flags = NTF_SELF;
212 ndm->ndm_type = 0;
213 ndm->ndm_ifindex = dump->dev->ifindex;
214 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
215
216 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
217 goto nla_put_failure;
218
219 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
220 goto nla_put_failure;
221
222 nlmsg_end(dump->skb, nlh);
223
224skip:
225 dump->idx++;
226 return 0;
227
228nla_put_failure:
229 nlmsg_cancel(dump->skb, nlh);
230 return -EMSGSIZE;
231}
232
233static int
234dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
235 struct net_device *dev, struct net_device *filter_dev,
236 int *idx)
237{
Vivien Didelotd9450972017-10-16 11:12:15 -0400238 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300239 struct dsa_slave_dump_ctx dump = {
240 .dev = dev,
241 .skb = skb,
242 .cb = cb,
243 .idx = *idx,
244 };
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300245 int err;
246
Vivien Didelotde40fc52017-09-20 19:32:14 -0400247 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300248 *idx = dump.idx;
Vivien Didelotde40fc52017-09-20 19:32:14 -0400249
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300250 return err;
251}
252
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000253static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
254{
Brandon Streiff03363692018-02-14 01:07:48 +0100255 struct dsa_slave_priv *p = netdev_priv(dev);
256 struct dsa_switch *ds = p->dp->ds;
257 int port = p->dp->index;
258
259 /* Pass through to switch driver if it supports timestamping */
260 switch (cmd) {
261 case SIOCGHWTSTAMP:
262 if (ds->ops->port_hwtstamp_get)
263 return ds->ops->port_hwtstamp_get(ds, port, ifr);
264 break;
265 case SIOCSHWTSTAMP:
266 if (ds->ops->port_hwtstamp_set)
267 return ds->ops->port_hwtstamp_set(ds, port, ifr);
268 break;
269 }
270
Florian Fainelliaab9c402018-05-10 13:17:36 -0700271 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000272}
273
Scott Feldman35636062015-05-10 09:47:51 -0700274static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200275 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200276 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700277{
Vivien Didelotd9450972017-10-16 11:12:15 -0400278 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400279 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700280
281 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200282 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400283 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700284 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500285 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400286 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
287 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500288 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400289 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400290 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400291 break;
Florian Fainelliea870052019-02-20 16:58:22 -0800292 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
293 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
294 trans);
295 break;
Russell King57652792019-02-20 15:35:04 -0800296 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
297 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
298 break;
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400299 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
300 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
301 break;
Scott Feldman35636062015-05-10 09:47:51 -0700302 default:
303 ret = -EOPNOTSUPP;
304 break;
305 }
306
307 return ret;
308}
309
Vivien Didelotbdcff082019-08-25 13:25:17 -0400310static int dsa_slave_vlan_add(struct net_device *dev,
311 const struct switchdev_obj *obj,
312 struct switchdev_trans *trans)
313{
314 struct dsa_port *dp = dsa_slave_to_port(dev);
315 struct switchdev_obj_port_vlan vlan;
316 int err;
317
318 if (obj->orig_dev != dev)
319 return -EOPNOTSUPP;
320
Vivien Didelotc5335d72019-08-25 13:25:18 -0400321 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
322 return 0;
323
Vivien Didelotbdcff082019-08-25 13:25:17 -0400324 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
325
326 err = dsa_port_vlan_add(dp, &vlan, trans);
327 if (err)
328 return err;
329
Vivien Didelotb9499902019-08-25 13:25:20 -0400330 /* We need the dedicated CPU port to be a member of the VLAN as well.
331 * Even though drivers often handle CPU membership in special ways,
332 * it doesn't make sense to program a PVID, so clear this flag.
333 */
334 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
335
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400336 err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
337 if (err)
338 return err;
339
Vivien Didelotbdcff082019-08-25 13:25:17 -0400340 return 0;
341}
342
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400343static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200344 const struct switchdev_obj *obj,
Vivien Didelot79b139f2019-06-14 13:49:22 -0400345 struct switchdev_trans *trans,
346 struct netlink_ext_ack *extack)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400347{
Vivien Didelotd9450972017-10-16 11:12:15 -0400348 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400349 int err;
350
351 /* For the prepare phase, ensure the full set of changes is feasable in
352 * one go in order to signal a failure properly. If an operation is not
353 * supported, return -EOPNOTSUPP.
354 */
355
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200356 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400357 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelot79b139f2019-06-14 13:49:22 -0400358 if (obj->orig_dev != dev)
359 return -EOPNOTSUPP;
Vivien Didelotbcebb972017-05-19 17:00:40 -0400360 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400361 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100362 case SWITCHDEV_OBJ_ID_HOST_MDB:
363 /* DSA can directly translate this to a normal MDB add,
364 * but on the CPU port.
365 */
366 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
367 trans);
368 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200369 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelotbdcff082019-08-25 13:25:17 -0400370 err = dsa_slave_vlan_add(dev, obj, trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400371 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400372 default:
373 err = -EOPNOTSUPP;
374 break;
375 }
376
377 return err;
378}
379
Vivien Didelotbdcff082019-08-25 13:25:17 -0400380static int dsa_slave_vlan_del(struct net_device *dev,
381 const struct switchdev_obj *obj)
382{
383 struct dsa_port *dp = dsa_slave_to_port(dev);
384
385 if (obj->orig_dev != dev)
386 return -EOPNOTSUPP;
387
Vivien Didelotc5335d72019-08-25 13:25:18 -0400388 if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
389 return 0;
390
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400391 /* Do not deprogram the CPU port as it may be shared with other user
392 * ports which can be members of this VLAN as well.
393 */
Vivien Didelotbdcff082019-08-25 13:25:17 -0400394 return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
395}
396
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400397static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200398 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400399{
Vivien Didelotd9450972017-10-16 11:12:15 -0400400 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400401 int err;
402
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200403 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400404 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelot79b139f2019-06-14 13:49:22 -0400405 if (obj->orig_dev != dev)
406 return -EOPNOTSUPP;
Vivien Didelotbcebb972017-05-19 17:00:40 -0400407 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400408 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100409 case SWITCHDEV_OBJ_ID_HOST_MDB:
410 /* DSA can directly translate this to a normal MDB add,
411 * but on the CPU port.
412 */
413 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
414 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200415 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelotbdcff082019-08-25 13:25:17 -0400416 err = dsa_slave_vlan_del(dev, obj);
Vivien Didelot11149532015-08-13 12:52:17 -0400417 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400418 default:
419 err = -EOPNOTSUPP;
420 break;
421 }
422
423 return err;
424}
425
Florian Fainelli929d6c12019-02-06 09:45:45 -0800426static int dsa_slave_get_port_parent_id(struct net_device *dev,
427 struct netdev_phys_item_id *ppid)
Florian Fainellib73adef2015-02-24 13:15:33 -0800428{
Vivien Didelotd9450972017-10-16 11:12:15 -0400429 struct dsa_port *dp = dsa_slave_to_port(dev);
430 struct dsa_switch *ds = dp->ds;
Andrew Lunna42c8e32017-11-09 22:29:51 +0100431 struct dsa_switch_tree *dst = ds->dst;
Florian Fainellib73adef2015-02-24 13:15:33 -0800432
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200433 /* For non-legacy ports, devlink is used and it takes
434 * care of the name generation. This ndo implementation
435 * should be removed with legacy support.
436 */
437 if (dp->ds->devlink)
438 return -EOPNOTSUPP;
439
Florian Fainelli929d6c12019-02-06 09:45:45 -0800440 ppid->id_len = sizeof(dst->index);
441 memcpy(&ppid->id, &dst->index, ppid->id_len);
442
443 return 0;
444}
445
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400446static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
447 struct sk_buff *skb)
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700448{
449#ifdef CONFIG_NET_POLL_CONTROLLER
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400450 struct dsa_slave_priv *p = netdev_priv(dev);
451
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700452 if (p->netpoll)
453 netpoll_send_skb(p->netpoll, skb);
454#else
455 BUG();
456#endif
457 return NETDEV_TX_OK;
458}
459
Brandon Streiff90af1052018-02-14 01:07:49 +0100460static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
461 struct sk_buff *skb)
462{
463 struct dsa_switch *ds = p->dp->ds;
464 struct sk_buff *clone;
465 unsigned int type;
466
467 type = ptp_classify_raw(skb);
468 if (type == PTP_CLASS_NONE)
469 return;
470
471 if (!ds->ops->port_txtstamp)
472 return;
473
474 clone = skb_clone_sk(skb);
475 if (!clone)
476 return;
477
Vladimir Oltean146d4422019-06-08 15:04:27 +0300478 DSA_SKB_CB(skb)->clone = clone;
479
Brandon Streiff90af1052018-02-14 01:07:49 +0100480 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
481 return;
482
483 kfree_skb(clone);
484}
485
Vladimir Oltean97a69a02019-05-05 13:19:25 +0300486netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
487{
488 /* SKB for netpoll still need to be mangled with the protocol-specific
489 * tag to be successfully transmitted
490 */
491 if (unlikely(netpoll_tx_running(dev)))
492 return dsa_slave_netpoll_send_skb(dev, skb);
493
494 /* Queue the SKB for transmission on the parent interface, but
495 * do not modify its EtherType
496 */
497 skb->dev = dsa_slave_to_master(dev);
498 dev_queue_xmit(skb);
499
500 return NETDEV_TX_OK;
501}
502EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
503
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700504static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
505{
506 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700507 struct pcpu_sw_netstats *s;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700508 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700509
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700510 s = this_cpu_ptr(p->stats64);
511 u64_stats_update_begin(&s->syncp);
512 s->tx_packets++;
513 s->tx_bytes += skb->len;
514 u64_stats_update_end(&s->syncp);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700515
Vladimir Oltean146d4422019-06-08 15:04:27 +0300516 DSA_SKB_CB(skb)->clone = NULL;
Vladimir Oltean87671372019-05-11 23:14:45 +0300517
Brandon Streiff90af1052018-02-14 01:07:49 +0100518 /* Identify PTP protocol packets, clone them, and pass them to the
519 * switch driver
520 */
521 dsa_skb_tx_timestamp(p, skb);
522
Vivien Didelotfe47d562017-06-01 16:07:15 -0400523 /* Transmit function may have to reallocate the original SKB,
524 * in which case it must have freed it. Only free it here on error.
525 */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700526 nskb = p->xmit(skb, dev);
Vivien Didelotfe47d562017-06-01 16:07:15 -0400527 if (!nskb) {
Vladimir Olteana68578c22020-01-04 02:37:10 +0200528 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700529 return NETDEV_TX_OK;
Vivien Didelotfe47d562017-06-01 16:07:15 -0400530 }
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700531
Vladimir Oltean97a69a02019-05-05 13:19:25 +0300532 return dsa_enqueue_skb(nskb, dev);
533}
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700534
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000535/* ethtool operations *******************************************************/
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000536
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000537static void dsa_slave_get_drvinfo(struct net_device *dev,
538 struct ethtool_drvinfo *drvinfo)
539{
Jiri Pirko7826d432013-01-06 00:44:26 +0000540 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000541 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
542 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000543}
544
Guenter Roeck3d762a02014-10-29 10:45:04 -0700545static int dsa_slave_get_regs_len(struct net_device *dev)
546{
Vivien Didelotd9450972017-10-16 11:12:15 -0400547 struct dsa_port *dp = dsa_slave_to_port(dev);
548 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700549
Vivien Didelot9d490b42016-08-23 12:38:56 -0400550 if (ds->ops->get_regs_len)
Vivien Didelotd9450972017-10-16 11:12:15 -0400551 return ds->ops->get_regs_len(ds, dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700552
553 return -EOPNOTSUPP;
554}
555
556static void
557dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
558{
Vivien Didelotd9450972017-10-16 11:12:15 -0400559 struct dsa_port *dp = dsa_slave_to_port(dev);
560 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700561
Vivien Didelot9d490b42016-08-23 12:38:56 -0400562 if (ds->ops->get_regs)
Vivien Didelotd9450972017-10-16 11:12:15 -0400563 ds->ops->get_regs(ds, dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700564}
565
Florian Fainelliaab9c402018-05-10 13:17:36 -0700566static int dsa_slave_nway_reset(struct net_device *dev)
567{
568 struct dsa_port *dp = dsa_slave_to_port(dev);
569
570 return phylink_ethtool_nway_reset(dp->pl);
571}
572
Guenter Roeck6793abb2014-10-29 10:45:01 -0700573static int dsa_slave_get_eeprom_len(struct net_device *dev)
574{
Vivien Didelotd9450972017-10-16 11:12:15 -0400575 struct dsa_port *dp = dsa_slave_to_port(dev);
576 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700577
Andrew Lunn0e576042016-06-04 21:16:52 +0200578 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200579 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700580
Vivien Didelot9d490b42016-08-23 12:38:56 -0400581 if (ds->ops->get_eeprom_len)
582 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700583
584 return 0;
585}
586
587static int dsa_slave_get_eeprom(struct net_device *dev,
588 struct ethtool_eeprom *eeprom, u8 *data)
589{
Vivien Didelotd9450972017-10-16 11:12:15 -0400590 struct dsa_port *dp = dsa_slave_to_port(dev);
591 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700592
Vivien Didelot9d490b42016-08-23 12:38:56 -0400593 if (ds->ops->get_eeprom)
594 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700595
596 return -EOPNOTSUPP;
597}
598
599static int dsa_slave_set_eeprom(struct net_device *dev,
600 struct ethtool_eeprom *eeprom, u8 *data)
601{
Vivien Didelotd9450972017-10-16 11:12:15 -0400602 struct dsa_port *dp = dsa_slave_to_port(dev);
603 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700604
Vivien Didelot9d490b42016-08-23 12:38:56 -0400605 if (ds->ops->set_eeprom)
606 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700607
608 return -EOPNOTSUPP;
609}
610
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000611static void dsa_slave_get_strings(struct net_device *dev,
612 uint32_t stringset, uint8_t *data)
613{
Vivien Didelotd9450972017-10-16 11:12:15 -0400614 struct dsa_port *dp = dsa_slave_to_port(dev);
615 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000616
617 if (stringset == ETH_SS_STATS) {
618 int len = ETH_GSTRING_LEN;
619
620 strncpy(data, "tx_packets", len);
621 strncpy(data + len, "tx_bytes", len);
622 strncpy(data + 2 * len, "rx_packets", len);
623 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400624 if (ds->ops->get_strings)
Florian Fainelli89f09042018-04-25 12:12:50 -0700625 ds->ops->get_strings(ds, dp->index, stringset,
626 data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000627 }
628}
629
630static void dsa_slave_get_ethtool_stats(struct net_device *dev,
631 struct ethtool_stats *stats,
632 uint64_t *data)
633{
Vivien Didelotd9450972017-10-16 11:12:15 -0400634 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000635 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400636 struct dsa_switch *ds = dp->ds;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700637 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700638 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700639 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000640
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700641 for_each_possible_cpu(i) {
642 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
643
644 s = per_cpu_ptr(p->stats64, i);
645 do {
646 start = u64_stats_fetch_begin_irq(&s->syncp);
647 tx_packets = s->tx_packets;
648 tx_bytes = s->tx_bytes;
649 rx_packets = s->rx_packets;
650 rx_bytes = s->rx_bytes;
651 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
652 data[0] += tx_packets;
653 data[1] += tx_bytes;
654 data[2] += rx_packets;
655 data[3] += rx_bytes;
656 }
Vivien Didelot9d490b42016-08-23 12:38:56 -0400657 if (ds->ops->get_ethtool_stats)
Vivien Didelotd9450972017-10-16 11:12:15 -0400658 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000659}
660
661static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
662{
Vivien Didelotd9450972017-10-16 11:12:15 -0400663 struct dsa_port *dp = dsa_slave_to_port(dev);
664 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000665
666 if (sset == ETH_SS_STATS) {
667 int count;
668
669 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400670 if (ds->ops->get_sset_count)
Florian Fainelli89f09042018-04-25 12:12:50 -0700671 count += ds->ops->get_sset_count(ds, dp->index, sset);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000672
673 return count;
674 }
675
676 return -EOPNOTSUPP;
677}
678
Florian Fainelli19e57c42014-09-18 17:31:24 -0700679static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
680{
Vivien Didelotd9450972017-10-16 11:12:15 -0400681 struct dsa_port *dp = dsa_slave_to_port(dev);
682 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700683
Florian Fainelliaab9c402018-05-10 13:17:36 -0700684 phylink_ethtool_get_wol(dp->pl, w);
685
Vivien Didelot9d490b42016-08-23 12:38:56 -0400686 if (ds->ops->get_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400687 ds->ops->get_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700688}
689
690static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
691{
Vivien Didelotd9450972017-10-16 11:12:15 -0400692 struct dsa_port *dp = dsa_slave_to_port(dev);
693 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700694 int ret = -EOPNOTSUPP;
695
Florian Fainelliaab9c402018-05-10 13:17:36 -0700696 phylink_ethtool_set_wol(dp->pl, w);
697
Vivien Didelot9d490b42016-08-23 12:38:56 -0400698 if (ds->ops->set_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400699 ret = ds->ops->set_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700700
701 return ret;
702}
703
Florian Fainelli79052882014-09-24 17:05:21 -0700704static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
705{
Vivien Didelotd9450972017-10-16 11:12:15 -0400706 struct dsa_port *dp = dsa_slave_to_port(dev);
707 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700708 int ret;
709
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400710 /* Port's PHY and MAC both need to be EEE capable */
Dan Carpenter00670cb2019-02-06 18:35:15 +0300711 if (!dev->phydev || !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400712 return -ENODEV;
713
Vivien Didelot08f50062017-08-01 16:32:41 -0400714 if (!ds->ops->set_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700715 return -EOPNOTSUPP;
716
Vivien Didelotd9450972017-10-16 11:12:15 -0400717 ret = ds->ops->set_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700718 if (ret)
719 return ret;
720
Florian Fainelliaab9c402018-05-10 13:17:36 -0700721 return phylink_ethtool_set_eee(dp->pl, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700722}
723
724static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
725{
Vivien Didelotd9450972017-10-16 11:12:15 -0400726 struct dsa_port *dp = dsa_slave_to_port(dev);
727 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700728 int ret;
729
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400730 /* Port's PHY and MAC both need to be EEE capable */
Dan Carpenter00670cb2019-02-06 18:35:15 +0300731 if (!dev->phydev || !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400732 return -ENODEV;
733
Vivien Didelot08f50062017-08-01 16:32:41 -0400734 if (!ds->ops->get_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700735 return -EOPNOTSUPP;
736
Vivien Didelotd9450972017-10-16 11:12:15 -0400737 ret = ds->ops->get_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700738 if (ret)
739 return ret;
740
Florian Fainelliaab9c402018-05-10 13:17:36 -0700741 return phylink_ethtool_get_eee(dp->pl, e);
742}
743
744static int dsa_slave_get_link_ksettings(struct net_device *dev,
745 struct ethtool_link_ksettings *cmd)
746{
747 struct dsa_port *dp = dsa_slave_to_port(dev);
748
749 return phylink_ethtool_ksettings_get(dp->pl, cmd);
750}
751
752static int dsa_slave_set_link_ksettings(struct net_device *dev,
753 const struct ethtool_link_ksettings *cmd)
754{
755 struct dsa_port *dp = dsa_slave_to_port(dev);
756
757 return phylink_ethtool_ksettings_set(dp->pl, cmd);
Florian Fainelli79052882014-09-24 17:05:21 -0700758}
759
Heiner Kallweita2a1a132019-10-29 22:32:48 +0100760static void dsa_slave_get_pauseparam(struct net_device *dev,
761 struct ethtool_pauseparam *pause)
762{
763 struct dsa_port *dp = dsa_slave_to_port(dev);
764
765 phylink_ethtool_get_pauseparam(dp->pl, pause);
766}
767
768static int dsa_slave_set_pauseparam(struct net_device *dev,
769 struct ethtool_pauseparam *pause)
770{
771 struct dsa_port *dp = dsa_slave_to_port(dev);
772
773 return phylink_ethtool_set_pauseparam(dp->pl, pause);
774}
775
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700776#ifdef CONFIG_NET_POLL_CONTROLLER
777static int dsa_slave_netpoll_setup(struct net_device *dev,
778 struct netpoll_info *ni)
779{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400780 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700781 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700782 struct netpoll *netpoll;
783 int err = 0;
784
785 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
786 if (!netpoll)
787 return -ENOMEM;
788
789 err = __netpoll_setup(netpoll, master);
790 if (err) {
791 kfree(netpoll);
792 goto out;
793 }
794
795 p->netpoll = netpoll;
796out:
797 return err;
798}
799
800static void dsa_slave_netpoll_cleanup(struct net_device *dev)
801{
802 struct dsa_slave_priv *p = netdev_priv(dev);
803 struct netpoll *netpoll = p->netpoll;
804
805 if (!netpoll)
806 return;
807
808 p->netpoll = NULL;
809
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400810 __netpoll_free(netpoll);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700811}
812
813static void dsa_slave_poll_controller(struct net_device *dev)
814{
815}
816#endif
817
Florian Fainelli44bb7652017-01-10 12:32:36 -0800818static int dsa_slave_get_phys_port_name(struct net_device *dev,
819 char *name, size_t len)
820{
Vivien Didelotd9450972017-10-16 11:12:15 -0400821 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli44bb7652017-01-10 12:32:36 -0800822
Jiri Pirkod4842102019-03-28 13:56:44 +0100823 /* For non-legacy ports, devlink is used and it takes
824 * care of the name generation. This ndo implementation
825 * should be removed with legacy support.
826 */
827 if (dp->ds->devlink)
828 return -EOPNOTSUPP;
829
Vivien Didelotd9450972017-10-16 11:12:15 -0400830 if (snprintf(name, len, "p%d", dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800831 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800832
833 return 0;
834}
835
Florian Fainellif50f2122017-01-30 12:41:40 -0800836static struct dsa_mall_tc_entry *
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400837dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
Florian Fainellif50f2122017-01-30 12:41:40 -0800838{
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400839 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800840 struct dsa_mall_tc_entry *mall_tc_entry;
841
842 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
843 if (mall_tc_entry->cookie == cookie)
844 return mall_tc_entry;
845
846 return NULL;
847}
848
849static int dsa_slave_add_cls_matchall(struct net_device *dev,
Florian Fainellif50f2122017-01-30 12:41:40 -0800850 struct tc_cls_matchall_offload *cls,
851 bool ingress)
852{
Vivien Didelotd9450972017-10-16 11:12:15 -0400853 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800854 struct dsa_slave_priv *p = netdev_priv(dev);
855 struct dsa_mall_tc_entry *mall_tc_entry;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200856 __be16 protocol = cls->common.protocol;
Vivien Didelotd9450972017-10-16 11:12:15 -0400857 struct dsa_switch *ds = dp->ds;
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700858 struct flow_action_entry *act;
Vivien Didelotd9450972017-10-16 11:12:15 -0400859 struct dsa_port *to_dp;
Florian Fainellif50f2122017-01-30 12:41:40 -0800860 int err = -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800861
862 if (!ds->ops->port_mirror_add)
863 return err;
864
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700865 if (!flow_offload_has_one_action(&cls->rule->action))
Florian Fainellif50f2122017-01-30 12:41:40 -0800866 return err;
867
Jiri Pirko319a1d12020-03-07 12:40:13 +0100868 if (!flow_action_basic_hw_stats_types_check(&cls->rule->action,
869 cls->common.extack))
870 return err;
871
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700872 act = &cls->rule->action.entries[0];
Florian Fainellif50f2122017-01-30 12:41:40 -0800873
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700874 if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
Florian Fainellif50f2122017-01-30 12:41:40 -0800875 struct dsa_mall_mirror_tc_entry *mirror;
876
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700877 if (!act->dev)
Florian Fainellif50f2122017-01-30 12:41:40 -0800878 return -EINVAL;
879
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700880 if (!dsa_slave_dev_check(act->dev))
Florian Fainellif50f2122017-01-30 12:41:40 -0800881 return -EOPNOTSUPP;
882
883 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
884 if (!mall_tc_entry)
885 return -ENOMEM;
886
887 mall_tc_entry->cookie = cls->cookie;
888 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
889 mirror = &mall_tc_entry->mirror;
890
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700891 to_dp = dsa_slave_to_port(act->dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800892
Vivien Didelotd9450972017-10-16 11:12:15 -0400893 mirror->to_local_port = to_dp->index;
Florian Fainellif50f2122017-01-30 12:41:40 -0800894 mirror->ingress = ingress;
895
Vivien Didelotd9450972017-10-16 11:12:15 -0400896 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
Florian Fainellif50f2122017-01-30 12:41:40 -0800897 if (err) {
898 kfree(mall_tc_entry);
899 return err;
900 }
901
902 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
903 }
904
905 return 0;
906}
907
908static void dsa_slave_del_cls_matchall(struct net_device *dev,
909 struct tc_cls_matchall_offload *cls)
910{
Vivien Didelotd9450972017-10-16 11:12:15 -0400911 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800912 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -0400913 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800914
915 if (!ds->ops->port_mirror_del)
916 return;
917
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400918 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
Florian Fainellif50f2122017-01-30 12:41:40 -0800919 if (!mall_tc_entry)
920 return;
921
922 list_del(&mall_tc_entry->list);
923
924 switch (mall_tc_entry->type) {
925 case DSA_PORT_MALL_MIRROR:
Vivien Didelotd9450972017-10-16 11:12:15 -0400926 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
Florian Fainellif50f2122017-01-30 12:41:40 -0800927 break;
928 default:
929 WARN_ON(1);
930 }
931
932 kfree(mall_tc_entry);
933}
934
Jiri Pirko3fbae382017-08-07 10:15:26 +0200935static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200936 struct tc_cls_matchall_offload *cls,
937 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -0800938{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200939 if (cls->common.chain_index)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200940 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800941
Jiri Pirko3fbae382017-08-07 10:15:26 +0200942 switch (cls->command) {
943 case TC_CLSMATCHALL_REPLACE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200944 return dsa_slave_add_cls_matchall(dev, cls, ingress);
Jiri Pirko3fbae382017-08-07 10:15:26 +0200945 case TC_CLSMATCHALL_DESTROY:
946 dsa_slave_del_cls_matchall(dev, cls);
947 return 0;
948 default:
949 return -EOPNOTSUPP;
950 }
951}
952
Vladimir Olteaned11bb12020-02-29 16:31:13 +0200953static int dsa_slave_add_cls_flower(struct net_device *dev,
954 struct flow_cls_offload *cls,
955 bool ingress)
956{
957 struct dsa_port *dp = dsa_slave_to_port(dev);
958 struct dsa_switch *ds = dp->ds;
959 int port = dp->index;
960
961 if (!ds->ops->cls_flower_add)
962 return -EOPNOTSUPP;
963
964 return ds->ops->cls_flower_add(ds, port, cls, ingress);
965}
966
967static int dsa_slave_del_cls_flower(struct net_device *dev,
968 struct flow_cls_offload *cls,
969 bool ingress)
970{
971 struct dsa_port *dp = dsa_slave_to_port(dev);
972 struct dsa_switch *ds = dp->ds;
973 int port = dp->index;
974
975 if (!ds->ops->cls_flower_del)
976 return -EOPNOTSUPP;
977
978 return ds->ops->cls_flower_del(ds, port, cls, ingress);
979}
980
981static int dsa_slave_stats_cls_flower(struct net_device *dev,
982 struct flow_cls_offload *cls,
983 bool ingress)
984{
985 struct dsa_port *dp = dsa_slave_to_port(dev);
986 struct dsa_switch *ds = dp->ds;
987 int port = dp->index;
988
989 if (!ds->ops->cls_flower_stats)
990 return -EOPNOTSUPP;
991
992 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
993}
994
995static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
996 struct flow_cls_offload *cls,
997 bool ingress)
998{
999 switch (cls->command) {
1000 case FLOW_CLS_REPLACE:
1001 return dsa_slave_add_cls_flower(dev, cls, ingress);
1002 case FLOW_CLS_DESTROY:
1003 return dsa_slave_del_cls_flower(dev, cls, ingress);
1004 case FLOW_CLS_STATS:
1005 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1006 default:
1007 return -EOPNOTSUPP;
1008 }
1009}
1010
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001011static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1012 void *cb_priv, bool ingress)
1013{
1014 struct net_device *dev = cb_priv;
1015
Jiri Pirko44ae12a2017-11-01 11:47:39 +01001016 if (!tc_can_offload(dev))
1017 return -EOPNOTSUPP;
1018
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001019 switch (type) {
1020 case TC_SETUP_CLSMATCHALL:
1021 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
Vladimir Olteaned11bb12020-02-29 16:31:13 +02001022 case TC_SETUP_CLSFLOWER:
1023 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001024 default:
1025 return -EOPNOTSUPP;
1026 }
1027}
1028
1029static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1030 void *type_data, void *cb_priv)
1031{
1032 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1033}
1034
1035static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1036 void *type_data, void *cb_priv)
1037{
1038 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1039}
1040
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001041static LIST_HEAD(dsa_slave_block_cb_list);
1042
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001043static int dsa_slave_setup_tc_block(struct net_device *dev,
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001044 struct flow_block_offload *f)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001045{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001046 struct flow_block_cb *block_cb;
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02001047 flow_setup_cb_t *cb;
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001048
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001049 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001050 cb = dsa_slave_setup_tc_block_cb_ig;
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001051 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001052 cb = dsa_slave_setup_tc_block_cb_eg;
1053 else
1054 return -EOPNOTSUPP;
1055
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001056 f->driver_block_list = &dsa_slave_block_cb_list;
1057
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001058 switch (f->command) {
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +02001059 case FLOW_BLOCK_BIND:
Pablo Neira Ayuso0d4fd022019-07-09 22:55:48 +02001060 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1061 return -EBUSY;
1062
Pablo Neira Ayuso0c7294d2019-07-19 18:20:14 +02001063 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001064 if (IS_ERR(block_cb))
1065 return PTR_ERR(block_cb);
1066
1067 flow_block_cb_add(block_cb, f);
1068 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1069 return 0;
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +02001070 case FLOW_BLOCK_UNBIND:
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +02001071 block_cb = flow_block_cb_lookup(f->block, cb, dev);
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001072 if (!block_cb)
1073 return -ENOENT;
1074
1075 flow_block_cb_remove(block_cb, f);
1076 list_del(&block_cb->driver_list);
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001077 return 0;
1078 default:
1079 return -EOPNOTSUPP;
1080 }
1081}
1082
Jiri Pirko3fbae382017-08-07 10:15:26 +02001083static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02001084 void *type_data)
Jiri Pirko3fbae382017-08-07 10:15:26 +02001085{
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001086 struct dsa_port *dp = dsa_slave_to_port(dev);
1087 struct dsa_switch *ds = dp->ds;
1088
1089 if (type == TC_SETUP_BLOCK)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001090 return dsa_slave_setup_tc_block(dev, type_data);
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001091
1092 if (!ds->ops->port_setup_tc)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02001093 return -EOPNOTSUPP;
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001094
1095 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
Florian Fainellif50f2122017-01-30 12:41:40 -08001096}
1097
Florian Fainellif613ed62017-08-01 15:00:36 -07001098static void dsa_slave_get_stats64(struct net_device *dev,
1099 struct rtnl_link_stats64 *stats)
1100{
1101 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001102 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -07001103 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001104 int i;
Florian Fainellif613ed62017-08-01 15:00:36 -07001105
1106 netdev_stats_to_stats64(stats, &dev->stats);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001107 for_each_possible_cpu(i) {
1108 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1109
1110 s = per_cpu_ptr(p->stats64, i);
1111 do {
1112 start = u64_stats_fetch_begin_irq(&s->syncp);
1113 tx_packets = s->tx_packets;
1114 tx_bytes = s->tx_bytes;
1115 rx_packets = s->rx_packets;
1116 rx_bytes = s->rx_bytes;
1117 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
1118
1119 stats->tx_packets += tx_packets;
1120 stats->tx_bytes += tx_bytes;
1121 stats->rx_packets += rx_packets;
1122 stats->rx_bytes += rx_bytes;
1123 }
Florian Fainellif613ed62017-08-01 15:00:36 -07001124}
1125
Florian Fainellibf9f2642017-01-30 09:48:40 -08001126static int dsa_slave_get_rxnfc(struct net_device *dev,
1127 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1128{
Vivien Didelotd9450972017-10-16 11:12:15 -04001129 struct dsa_port *dp = dsa_slave_to_port(dev);
1130 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -08001131
1132 if (!ds->ops->get_rxnfc)
1133 return -EOPNOTSUPP;
1134
Vivien Didelotd9450972017-10-16 11:12:15 -04001135 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
Florian Fainellibf9f2642017-01-30 09:48:40 -08001136}
1137
1138static int dsa_slave_set_rxnfc(struct net_device *dev,
1139 struct ethtool_rxnfc *nfc)
1140{
Vivien Didelotd9450972017-10-16 11:12:15 -04001141 struct dsa_port *dp = dsa_slave_to_port(dev);
1142 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -08001143
1144 if (!ds->ops->set_rxnfc)
1145 return -EOPNOTSUPP;
1146
Vivien Didelotd9450972017-10-16 11:12:15 -04001147 return ds->ops->set_rxnfc(ds, dp->index, nfc);
Florian Fainellibf9f2642017-01-30 09:48:40 -08001148}
1149
Brandon Streiff03363692018-02-14 01:07:48 +01001150static int dsa_slave_get_ts_info(struct net_device *dev,
1151 struct ethtool_ts_info *ts)
1152{
1153 struct dsa_slave_priv *p = netdev_priv(dev);
1154 struct dsa_switch *ds = p->dp->ds;
1155
1156 if (!ds->ops->get_ts_info)
1157 return -EOPNOTSUPP;
1158
1159 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1160}
1161
Florian Fainelli061f6a52019-02-20 14:35:39 -08001162static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1163 u16 vid)
1164{
1165 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001166 struct bridge_vlan_info info;
1167 int ret;
1168
1169 /* Check for a possible bridge VLAN entry now since there is no
1170 * need to emulate the switchdev prepare + commit phase.
1171 */
1172 if (dp->bridge_dev) {
Vivien Didelotc5335d72019-08-25 13:25:18 -04001173 if (!br_vlan_enabled(dp->bridge_dev))
1174 return 0;
1175
Florian Fainelli061f6a52019-02-20 14:35:39 -08001176 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1177 * device, respectively the VID is not found, returning
1178 * 0 means success, which is a failure for us here.
1179 */
1180 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1181 if (ret == 0)
1182 return -EBUSY;
1183 }
1184
Vivien Didelotcf360862019-08-25 13:25:16 -04001185 ret = dsa_port_vid_add(dp, vid, 0);
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001186 if (ret)
Vivien Didelotcf360862019-08-25 13:25:16 -04001187 return ret;
1188
Vivien Didelot7e1741b2019-08-25 13:25:19 -04001189 ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001190 if (ret)
Vivien Didelot7e1741b2019-08-25 13:25:19 -04001191 return ret;
1192
Vivien Didelotcf360862019-08-25 13:25:16 -04001193 return 0;
Florian Fainelli061f6a52019-02-20 14:35:39 -08001194}
1195
1196static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1197 u16 vid)
1198{
1199 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001200 struct bridge_vlan_info info;
1201 int ret;
1202
1203 /* Check for a possible bridge VLAN entry now since there is no
1204 * need to emulate the switchdev prepare + commit phase.
1205 */
1206 if (dp->bridge_dev) {
Vivien Didelotc5335d72019-08-25 13:25:18 -04001207 if (!br_vlan_enabled(dp->bridge_dev))
1208 return 0;
1209
Florian Fainelli061f6a52019-02-20 14:35:39 -08001210 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1211 * device, respectively the VID is not found, returning
1212 * 0 means success, which is a failure for us here.
1213 */
1214 ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1215 if (ret == 0)
1216 return -EBUSY;
1217 }
1218
Vivien Didelot7e1741b2019-08-25 13:25:19 -04001219 /* Do not deprogram the CPU port as it may be shared with other user
1220 * ports which can be members of this VLAN as well.
1221 */
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001222 return dsa_port_vid_del(dp, vid);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001223}
1224
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001225static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001226 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001227 .get_regs_len = dsa_slave_get_regs_len,
1228 .get_regs = dsa_slave_get_regs,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001229 .nway_reset = dsa_slave_nway_reset,
Florian Fainellic4aef9f2018-05-10 13:17:34 -07001230 .get_link = ethtool_op_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001231 .get_eeprom_len = dsa_slave_get_eeprom_len,
1232 .get_eeprom = dsa_slave_get_eeprom,
1233 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001234 .get_strings = dsa_slave_get_strings,
1235 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1236 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001237 .set_wol = dsa_slave_set_wol,
1238 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001239 .set_eee = dsa_slave_set_eee,
1240 .get_eee = dsa_slave_get_eee,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001241 .get_link_ksettings = dsa_slave_get_link_ksettings,
1242 .set_link_ksettings = dsa_slave_set_link_ksettings,
Heiner Kallweita2a1a132019-10-29 22:32:48 +01001243 .get_pauseparam = dsa_slave_get_pauseparam,
1244 .set_pauseparam = dsa_slave_set_pauseparam,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001245 .get_rxnfc = dsa_slave_get_rxnfc,
1246 .set_rxnfc = dsa_slave_set_rxnfc,
Brandon Streiff03363692018-02-14 01:07:48 +01001247 .get_ts_info = dsa_slave_get_ts_info,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001248};
1249
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001250/* legacy way, bypassing the bridge *****************************************/
1251int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1252 struct net_device *dev,
1253 const unsigned char *addr, u16 vid,
Petr Machata87b09842019-01-16 23:06:50 +00001254 u16 flags,
1255 struct netlink_ext_ack *extack)
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001256{
1257 struct dsa_port *dp = dsa_slave_to_port(dev);
1258
1259 return dsa_port_fdb_add(dp, addr, vid);
1260}
1261
1262int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1263 struct net_device *dev,
1264 const unsigned char *addr, u16 vid)
1265{
1266 struct dsa_port *dp = dsa_slave_to_port(dev);
1267
1268 return dsa_port_fdb_del(dp, addr, vid);
1269}
1270
Jiri Pirko716efee2019-03-28 13:56:43 +01001271static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1272{
1273 struct dsa_port *dp = dsa_slave_to_port(dev);
1274
1275 return dp->ds->devlink ? &dp->devlink_port : NULL;
1276}
1277
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001278static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001279 .ndo_open = dsa_slave_open,
1280 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001281 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001282 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1283 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001284 .ndo_set_mac_address = dsa_slave_set_mac_address,
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +03001285 .ndo_fdb_add = dsa_legacy_fdb_add,
1286 .ndo_fdb_del = dsa_legacy_fdb_del,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001287 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001288 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001289 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001290#ifdef CONFIG_NET_POLL_CONTROLLER
1291 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1292 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1293 .ndo_poll_controller = dsa_slave_poll_controller,
1294#endif
Florian Fainelli44bb7652017-01-10 12:32:36 -08001295 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001296 .ndo_setup_tc = dsa_slave_setup_tc,
Florian Fainellif613ed62017-08-01 15:00:36 -07001297 .ndo_get_stats64 = dsa_slave_get_stats64,
Florian Fainelli929d6c12019-02-06 09:45:45 -08001298 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
Florian Fainelli061f6a52019-02-20 14:35:39 -08001299 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1300 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
Jiri Pirko716efee2019-03-28 13:56:43 +01001301 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
Scott Feldman98237d42015-03-15 21:07:15 -07001302};
1303
Florian Fainellif37db852015-09-23 18:19:58 -07001304static struct device_type dsa_type = {
1305 .name = "dsa",
1306};
1307
Florian Fainelliaab9c402018-05-10 13:17:36 -07001308void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1309{
1310 const struct dsa_port *dp = dsa_to_port(ds, port);
1311
1312 phylink_mac_change(dp->pl, up);
1313}
1314EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1315
1316static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1317 struct phylink_link_state *state)
1318{
1319 struct dsa_port *dp = dsa_slave_to_port(dev);
1320 struct dsa_switch *ds = dp->ds;
1321
1322 /* No need to check that this operation is valid, the callback would
1323 * not be called if it was not.
1324 */
1325 ds->ops->phylink_fixed_state(ds, dp->index, state);
Florian Fainellice31b312014-08-27 17:04:54 -07001326}
1327
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001328/* slave device setup *******************************************************/
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001329static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001330{
Vivien Didelotd9450972017-10-16 11:12:15 -04001331 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001332 struct dsa_switch *ds = dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001333
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001334 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1335 if (!slave_dev->phydev) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001336 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001337 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001338 }
Florian Fainellic305c162015-03-10 16:57:12 -07001339
Florian Fainelliaab9c402018-05-10 13:17:36 -07001340 return phylink_connect_phy(dp->pl, slave_dev->phydev);
Florian Fainellic305c162015-03-10 16:57:12 -07001341}
1342
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001343static int dsa_slave_phy_setup(struct net_device *slave_dev)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001344{
Vivien Didelotd9450972017-10-16 11:12:15 -04001345 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001346 struct device_node *port_dn = dp->dn;
1347 struct dsa_switch *ds = dp->ds;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001348 phy_interface_t mode;
Florian Fainelli68195632014-09-19 13:07:54 -07001349 u32 phy_flags = 0;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001350 int ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001351
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001352 ret = of_get_phy_mode(port_dn, &mode);
1353 if (ret)
Guenter Roeck19334922015-02-16 21:23:51 -08001354 mode = PHY_INTERFACE_MODE_NA;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001355
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03001356 dp->pl_config.dev = &slave_dev->dev;
1357 dp->pl_config.type = PHYLINK_NETDEV;
1358
1359 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
Ioana Ciornei77373d42019-05-28 20:38:15 +03001360 &dsa_port_phylink_mac_ops);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001361 if (IS_ERR(dp->pl)) {
1362 netdev_err(slave_dev,
1363 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1364 return PTR_ERR(dp->pl);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001365 }
1366
Florian Fainelliaab9c402018-05-10 13:17:36 -07001367 /* Register only if the switch provides such a callback, since this
1368 * callback takes precedence over polling the link GPIO in PHYLINK
1369 * (see phylink_get_fixed_state).
1370 */
1371 if (ds->ops->phylink_fixed_state)
1372 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1373
Vivien Didelot9d490b42016-08-23 12:38:56 -04001374 if (ds->ops->get_phy_flags)
Vivien Didelotd9450972017-10-16 11:12:15 -04001375 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001376
Florian Fainelliaab9c402018-05-10 13:17:36 -07001377 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
Vladimir Oltean6146dd42019-03-24 01:24:07 +02001378 if (ret == -ENODEV && ds->slave_mii_bus) {
1379 /* We could not connect to a designated PHY or SFP, so try to
1380 * use the switch internal MDIO bus instead
Florian Fainelliaab9c402018-05-10 13:17:36 -07001381 */
Vivien Didelotd9450972017-10-16 11:12:15 -04001382 ret = dsa_slave_phy_connect(slave_dev, dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001383 if (ret) {
Florian Fainelliaab9c402018-05-10 13:17:36 -07001384 netdev_err(slave_dev,
1385 "failed to connect to port %d: %d\n",
Vivien Didelotd9450972017-10-16 11:12:15 -04001386 dp->index, ret);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001387 phylink_destroy(dp->pl);
Florian Fainellic305c162015-03-10 16:57:12 -07001388 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001389 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001390 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001391
Vladimir Oltean6146dd42019-03-24 01:24:07 +02001392 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001393}
1394
Florian Fainelli24462542014-09-18 17:31:22 -07001395int dsa_slave_suspend(struct net_device *slave_dev)
1396{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001397 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Florian Fainelli24462542014-09-18 17:31:22 -07001398
Florian Fainellia94c6892018-07-31 17:12:52 -07001399 if (!netif_running(slave_dev))
1400 return 0;
1401
Florian Fainellif154be22017-01-25 09:10:41 -08001402 netif_device_detach(slave_dev);
1403
Florian Fainelliaab9c402018-05-10 13:17:36 -07001404 rtnl_lock();
1405 phylink_stop(dp->pl);
1406 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001407
1408 return 0;
1409}
1410
1411int dsa_slave_resume(struct net_device *slave_dev)
1412{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001413 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1414
Florian Fainellia94c6892018-07-31 17:12:52 -07001415 if (!netif_running(slave_dev))
1416 return 0;
1417
Florian Fainelli24462542014-09-18 17:31:22 -07001418 netif_device_attach(slave_dev);
1419
Florian Fainelliaab9c402018-05-10 13:17:36 -07001420 rtnl_lock();
1421 phylink_start(dp->pl);
1422 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001423
1424 return 0;
1425}
1426
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001427static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1428{
Vivien Didelotd0006b02017-10-16 11:12:16 -04001429 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001430 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001431 struct dsa_notifier_register_info rinfo = {
1432 .switch_number = dp->ds->index,
1433 .port_number = dp->index,
1434 .master = master,
1435 .info.dev = dev,
1436 };
1437
1438 call_dsa_notifiers(val, dev, &rinfo.info);
1439}
1440
Vivien Didelot951259aa2017-10-27 15:55:19 -04001441int dsa_slave_create(struct dsa_port *port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001442{
Vivien Didelot24a93322017-11-06 16:11:43 -05001443 const struct dsa_port *cpu_dp = port->cpu_dp;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001444 struct net_device *master = cpu_dp->master;
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001445 struct dsa_switch *ds = port->ds;
Vivien Didelot951259aa2017-10-27 15:55:19 -04001446 const char *name = port->name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001447 struct net_device *slave_dev;
1448 struct dsa_slave_priv *p;
1449 int ret;
1450
Florian Fainelli55199df2017-09-03 20:27:00 -07001451 if (!ds->num_tx_queues)
1452 ds->num_tx_queues = 1;
1453
1454 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1455 NET_NAME_UNKNOWN, ether_setup,
1456 ds->num_tx_queues, 1);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001457 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001458 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001459
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001460 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1461 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1462 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Florian Fainellif50f2122017-01-30 12:41:40 -08001463 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001464 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Petr Å tetiar4974f9b2019-05-06 23:24:45 +02001465 if (!IS_ERR_OR_NULL(port->mac))
Xiaofei Shena2c70232019-03-29 11:04:58 +05301466 ether_addr_copy(slave_dev->dev_addr, port->mac);
1467 else
1468 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001469 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001470 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001471 slave_dev->min_mtu = 0;
1472 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001473 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001474
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001475 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1476 slave_dev->dev.of_node = port->dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001477 slave_dev->vlan_features = master->vlan_features;
1478
1479 p = netdev_priv(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001480 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1481 if (!p->stats64) {
1482 free_netdev(slave_dev);
1483 return -ENOMEM;
1484 }
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001485 p->dp = port;
Florian Fainellif50f2122017-01-30 12:41:40 -08001486 INIT_LIST_HEAD(&p->mall_tc_list);
Vivien Didelot15240242017-09-29 17:19:18 -04001487 p->xmit = cpu_dp->tag_ops->xmit;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001488 port->slave = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001489
1490 netif_carrier_off(slave_dev);
1491
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001492 ret = dsa_slave_phy_setup(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001493 if (ret) {
1494 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainellie8044412017-09-25 15:55:53 -07001495 goto out_free;
1496 }
1497
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001498 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
Florian Fainelli60724d42017-10-11 10:57:48 -07001499
Florian Fainellie8044412017-09-25 15:55:53 -07001500 ret = register_netdev(slave_dev);
1501 if (ret) {
1502 netdev_err(master, "error %d registering interface %s\n",
1503 ret, slave_dev->name);
1504 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001505 }
1506
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001507 return 0;
Florian Fainellie8044412017-09-25 15:55:53 -07001508
1509out_phy:
Florian Fainelliaab9c402018-05-10 13:17:36 -07001510 rtnl_lock();
1511 phylink_disconnect_phy(p->dp->pl);
1512 rtnl_unlock();
1513 phylink_destroy(p->dp->pl);
Florian Fainellie8044412017-09-25 15:55:53 -07001514out_free:
1515 free_percpu(p->stats64);
1516 free_netdev(slave_dev);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001517 port->slave = NULL;
Florian Fainellie8044412017-09-25 15:55:53 -07001518 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001519}
Florian Fainellib73adef2015-02-24 13:15:33 -08001520
Neil Armstrongcda5c152015-12-07 13:57:35 +01001521void dsa_slave_destroy(struct net_device *slave_dev)
1522{
Vivien Didelotd9450972017-10-16 11:12:15 -04001523 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001524 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1525
1526 netif_carrier_off(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001527 rtnl_lock();
1528 phylink_disconnect_phy(dp->pl);
1529 rtnl_unlock();
Johan Hovold881eada2016-11-28 19:25:09 +01001530
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001531 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001532 unregister_netdev(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001533 phylink_destroy(dp->pl);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001534 free_percpu(p->stats64);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001535 free_netdev(slave_dev);
1536}
1537
Florian Fainelli4d776482020-01-07 21:06:05 -08001538bool dsa_slave_dev_check(const struct net_device *dev)
Florian Fainellib73adef2015-02-24 13:15:33 -08001539{
1540 return dev->netdev_ops == &dsa_slave_netdev_ops;
1541}
1542
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001543static int dsa_slave_changeupper(struct net_device *dev,
1544 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001545{
Vivien Didelotd9450972017-10-16 11:12:15 -04001546 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001547 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001548
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001549 if (netif_is_bridge_master(info->upper_dev)) {
1550 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001551 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001552 err = notifier_from_errno(err);
1553 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001554 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001555 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001556 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001557 }
1558
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001559 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001560}
1561
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08001562static int dsa_slave_upper_vlan_check(struct net_device *dev,
1563 struct netdev_notifier_changeupper_info *
1564 info)
1565{
1566 struct netlink_ext_ack *ext_ack;
1567 struct net_device *slave;
1568 struct dsa_port *dp;
1569
1570 ext_ack = netdev_notifier_info_to_extack(&info->info);
1571
1572 if (!is_vlan_dev(dev))
1573 return NOTIFY_DONE;
1574
1575 slave = vlan_dev_real_dev(dev);
1576 if (!dsa_slave_dev_check(slave))
1577 return NOTIFY_DONE;
1578
1579 dp = dsa_slave_to_port(slave);
1580 if (!dp->bridge_dev)
1581 return NOTIFY_DONE;
1582
1583 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1584 if (br_vlan_enabled(dp->bridge_dev) &&
1585 netif_is_bridge_master(info->upper_dev) && info->linking) {
1586 NL_SET_ERR_MSG_MOD(ext_ack,
1587 "Cannot enslave VLAN device into VLAN aware bridge");
1588 return notifier_from_errno(-EINVAL);
1589 }
1590
1591 return NOTIFY_DONE;
1592}
1593
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001594static int dsa_slave_netdevice_event(struct notifier_block *nb,
1595 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001596{
Vivien Didelot6debb682016-03-13 16:21:34 -04001597 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001598
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08001599 if (event == NETDEV_CHANGEUPPER) {
1600 if (!dsa_slave_dev_check(dev))
1601 return dsa_slave_upper_vlan_check(dev, ptr);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001602
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001603 return dsa_slave_changeupper(dev, ptr);
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08001604 }
Florian Fainellib73adef2015-02-24 13:15:33 -08001605
Florian Fainellib73adef2015-02-24 13:15:33 -08001606 return NOTIFY_DONE;
1607}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001608
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001609struct dsa_switchdev_event_work {
1610 struct work_struct work;
1611 struct switchdev_notifier_fdb_info fdb_info;
1612 struct net_device *dev;
1613 unsigned long event;
1614};
1615
1616static void dsa_slave_switchdev_event_work(struct work_struct *work)
1617{
1618 struct dsa_switchdev_event_work *switchdev_work =
1619 container_of(work, struct dsa_switchdev_event_work, work);
1620 struct net_device *dev = switchdev_work->dev;
1621 struct switchdev_notifier_fdb_info *fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001622 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001623 int err;
1624
1625 rtnl_lock();
1626 switch (switchdev_work->event) {
1627 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1628 fdb_info = &switchdev_work->fdb_info;
Vivien Didelota37fb852018-05-08 23:03:12 -04001629 if (!fdb_info->added_by_user)
1630 break;
1631
Vivien Didelotd9450972017-10-16 11:12:15 -04001632 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001633 if (err) {
1634 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1635 break;
1636 }
Ido Schimmele9ba0fb2018-10-17 08:53:29 +00001637 fdb_info->offloaded = true;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001638 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
Petr Machata66859872019-01-16 23:06:56 +00001639 &fdb_info->info, NULL);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001640 break;
1641
1642 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1643 fdb_info = &switchdev_work->fdb_info;
Vivien Didelota37fb852018-05-08 23:03:12 -04001644 if (!fdb_info->added_by_user)
1645 break;
1646
Vivien Didelotd9450972017-10-16 11:12:15 -04001647 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001648 if (err) {
1649 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1650 dev_close(dev);
1651 }
1652 break;
1653 }
1654 rtnl_unlock();
1655
1656 kfree(switchdev_work->fdb_info.addr);
1657 kfree(switchdev_work);
1658 dev_put(dev);
1659}
1660
1661static int
1662dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1663 switchdev_work,
1664 const struct switchdev_notifier_fdb_info *
1665 fdb_info)
1666{
1667 memcpy(&switchdev_work->fdb_info, fdb_info,
1668 sizeof(switchdev_work->fdb_info));
1669 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1670 if (!switchdev_work->fdb_info.addr)
1671 return -ENOMEM;
1672 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1673 fdb_info->addr);
1674 return 0;
1675}
1676
1677/* Called under rcu_read_lock() */
1678static int dsa_slave_switchdev_event(struct notifier_block *unused,
1679 unsigned long event, void *ptr)
1680{
1681 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1682 struct dsa_switchdev_event_work *switchdev_work;
Vivien Didelot79b139f2019-06-14 13:49:22 -04001683 int err;
1684
1685 if (event == SWITCHDEV_PORT_ATTR_SET) {
1686 err = switchdev_handle_port_attr_set(dev, ptr,
1687 dsa_slave_dev_check,
1688 dsa_slave_port_attr_set);
1689 return notifier_from_errno(err);
1690 }
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001691
1692 if (!dsa_slave_dev_check(dev))
1693 return NOTIFY_DONE;
1694
1695 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1696 if (!switchdev_work)
1697 return NOTIFY_BAD;
1698
1699 INIT_WORK(&switchdev_work->work,
1700 dsa_slave_switchdev_event_work);
1701 switchdev_work->dev = dev;
1702 switchdev_work->event = event;
1703
1704 switch (event) {
1705 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1706 case SWITCHDEV_FDB_DEL_TO_DEVICE:
Vivien Didelota37fb852018-05-08 23:03:12 -04001707 if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001708 goto err_fdb_work_init;
1709 dev_hold(dev);
1710 break;
1711 default:
1712 kfree(switchdev_work);
1713 return NOTIFY_DONE;
1714 }
1715
1716 dsa_schedule_work(&switchdev_work->work);
1717 return NOTIFY_OK;
1718
1719err_fdb_work_init:
1720 kfree(switchdev_work);
1721 return NOTIFY_BAD;
1722}
1723
Petr Machata2b239f62018-11-22 23:29:05 +00001724static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
1725 unsigned long event, void *ptr)
1726{
1727 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Vivien Didelot79b139f2019-06-14 13:49:22 -04001728 int err;
Petr Machata2b239f62018-11-22 23:29:05 +00001729
1730 switch (event) {
Vivien Didelot79b139f2019-06-14 13:49:22 -04001731 case SWITCHDEV_PORT_OBJ_ADD:
1732 err = switchdev_handle_port_obj_add(dev, ptr,
1733 dsa_slave_dev_check,
1734 dsa_slave_port_obj_add);
1735 return notifier_from_errno(err);
Petr Machata2b239f62018-11-22 23:29:05 +00001736 case SWITCHDEV_PORT_OBJ_DEL:
Vivien Didelot79b139f2019-06-14 13:49:22 -04001737 err = switchdev_handle_port_obj_del(dev, ptr,
1738 dsa_slave_dev_check,
1739 dsa_slave_port_obj_del);
1740 return notifier_from_errno(err);
Florian Fainelli9ed1ece2019-02-27 11:44:27 -08001741 case SWITCHDEV_PORT_ATTR_SET:
Vivien Didelot79b139f2019-06-14 13:49:22 -04001742 err = switchdev_handle_port_attr_set(dev, ptr,
1743 dsa_slave_dev_check,
1744 dsa_slave_port_attr_set);
1745 return notifier_from_errno(err);
Petr Machata2b239f62018-11-22 23:29:05 +00001746 }
1747
1748 return NOTIFY_DONE;
1749}
1750
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001751static struct notifier_block dsa_slave_nb __read_mostly = {
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001752 .notifier_call = dsa_slave_netdevice_event,
1753};
1754
1755static struct notifier_block dsa_slave_switchdev_notifier = {
1756 .notifier_call = dsa_slave_switchdev_event,
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001757};
1758
Petr Machata2b239f62018-11-22 23:29:05 +00001759static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
1760 .notifier_call = dsa_slave_switchdev_blocking_event,
1761};
1762
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001763int dsa_slave_register_notifier(void)
1764{
Petr Machata2b239f62018-11-22 23:29:05 +00001765 struct notifier_block *nb;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001766 int err;
1767
1768 err = register_netdevice_notifier(&dsa_slave_nb);
1769 if (err)
1770 return err;
1771
1772 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1773 if (err)
1774 goto err_switchdev_nb;
1775
Petr Machata2b239f62018-11-22 23:29:05 +00001776 nb = &dsa_slave_switchdev_blocking_notifier;
1777 err = register_switchdev_blocking_notifier(nb);
1778 if (err)
1779 goto err_switchdev_blocking_nb;
1780
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001781 return 0;
1782
Petr Machata2b239f62018-11-22 23:29:05 +00001783err_switchdev_blocking_nb:
1784 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001785err_switchdev_nb:
1786 unregister_netdevice_notifier(&dsa_slave_nb);
1787 return err;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001788}
1789
1790void dsa_slave_unregister_notifier(void)
1791{
Petr Machata2b239f62018-11-22 23:29:05 +00001792 struct notifier_block *nb;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001793 int err;
1794
Petr Machata2b239f62018-11-22 23:29:05 +00001795 nb = &dsa_slave_switchdev_blocking_notifier;
1796 err = unregister_switchdev_blocking_notifier(nb);
1797 if (err)
1798 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
1799
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001800 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1801 if (err)
1802 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1803
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001804 err = unregister_netdevice_notifier(&dsa_slave_nb);
1805 if (err)
1806 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1807}