xref: /openbmc/linux/net/dsa/slave.c (revision 2bedde1abbef5eec211308f0293dd7681b0513ec)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #include <linux/list.h>
12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
13b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1491da11f8SLennert Buytenhek #include <linux/phy.h>
15a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
160d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
170d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
187f854420SAndrew Lunn #include <linux/mdio.h>
19f50f2127SFlorian Fainelli #include <linux/list.h>
20b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
21f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
22f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
23b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2404ff53f9SFlorian Fainelli #include <linux/netpoll.h>
25ea5dd34bSVivien Didelot 
2691da11f8SLennert Buytenhek #include "dsa_priv.h"
2791da11f8SLennert Buytenhek 
28f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev);
29f50f2127SFlorian Fainelli 
3091da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
3191da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
3291da11f8SLennert Buytenhek {
3391da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3491da11f8SLennert Buytenhek 
350d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
369d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3791da11f8SLennert Buytenhek 
3891da11f8SLennert Buytenhek 	return 0xffff;
3991da11f8SLennert Buytenhek }
4091da11f8SLennert Buytenhek 
4191da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
4291da11f8SLennert Buytenhek {
4391da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4491da11f8SLennert Buytenhek 
450d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
469d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4791da11f8SLennert Buytenhek 
4891da11f8SLennert Buytenhek 	return 0;
4991da11f8SLennert Buytenhek }
5091da11f8SLennert Buytenhek 
5191da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
5291da11f8SLennert Buytenhek {
5391da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5491da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5591da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5691da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
570b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
580b7b498dSFlorian Fainelli 		 ds->dst->tree, ds->index);
59c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
6024df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
6191da11f8SLennert Buytenhek }
6291da11f8SLennert Buytenhek 
6391da11f8SLennert Buytenhek 
6491da11f8SLennert Buytenhek /* slave device handling ****************************************************/
65abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
66c0840801SLennert Buytenhek {
67c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
68c0840801SLennert Buytenhek 
696d3c8c0dSFlorian Fainelli 	return dsa_master_netdev(p)->ifindex;
70c0840801SLennert Buytenhek }
71c0840801SLennert Buytenhek 
7291da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
7391da11f8SLennert Buytenhek {
74df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
75f3d736c4SVivien Didelot 	struct dsa_port *dp = p->dp;
76f3d736c4SVivien Didelot 	struct dsa_switch *ds = dp->ds;
776d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
78f3d736c4SVivien Didelot 	u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING;
79df02c6ffSLennert Buytenhek 	int err;
80df02c6ffSLennert Buytenhek 
81df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
82df02c6ffSLennert Buytenhek 		return -ENETDOWN;
83df02c6ffSLennert Buytenhek 
848feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
85a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
86df02c6ffSLennert Buytenhek 		if (err < 0)
87df02c6ffSLennert Buytenhek 			goto out;
88df02c6ffSLennert Buytenhek 	}
89df02c6ffSLennert Buytenhek 
90df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
91df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
92df02c6ffSLennert Buytenhek 		if (err < 0)
93df02c6ffSLennert Buytenhek 			goto del_unicast;
94df02c6ffSLennert Buytenhek 	}
95df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
96df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
97df02c6ffSLennert Buytenhek 		if (err < 0)
98df02c6ffSLennert Buytenhek 			goto clear_allmulti;
99df02c6ffSLennert Buytenhek 	}
100df02c6ffSLennert Buytenhek 
1019d490b4eSVivien Didelot 	if (ds->ops->port_enable) {
102afdcf151SVivien Didelot 		err = ds->ops->port_enable(ds, p->dp->index, p->phy);
103b2f2af21SFlorian Fainelli 		if (err)
104b2f2af21SFlorian Fainelli 			goto clear_promisc;
105b2f2af21SFlorian Fainelli 	}
106b2f2af21SFlorian Fainelli 
107fd364541SVivien Didelot 	dsa_port_set_state_now(p->dp, stp_state);
108b73adef6SFlorian Fainelli 
109f7f1de51SFlorian Fainelli 	if (p->phy)
110f7f1de51SFlorian Fainelli 		phy_start(p->phy);
111f7f1de51SFlorian Fainelli 
11291da11f8SLennert Buytenhek 	return 0;
113df02c6ffSLennert Buytenhek 
114b2f2af21SFlorian Fainelli clear_promisc:
115b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1164fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
117df02c6ffSLennert Buytenhek clear_allmulti:
118df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
119df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
120df02c6ffSLennert Buytenhek del_unicast:
1218feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
122a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
123df02c6ffSLennert Buytenhek out:
124df02c6ffSLennert Buytenhek 	return err;
12591da11f8SLennert Buytenhek }
12691da11f8SLennert Buytenhek 
12791da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
12891da11f8SLennert Buytenhek {
129df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
1306d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
131afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
132df02c6ffSLennert Buytenhek 
133f7f1de51SFlorian Fainelli 	if (p->phy)
134f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
135f7f1de51SFlorian Fainelli 
136df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
137a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
138df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
139df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
140df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
141df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
142df02c6ffSLennert Buytenhek 
1438feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
144a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
145df02c6ffSLennert Buytenhek 
1469d490b4eSVivien Didelot 	if (ds->ops->port_disable)
147afdcf151SVivien Didelot 		ds->ops->port_disable(ds, p->dp->index, p->phy);
148b2f2af21SFlorian Fainelli 
149fd364541SVivien Didelot 	dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
150b73adef6SFlorian Fainelli 
15191da11f8SLennert Buytenhek 	return 0;
15291da11f8SLennert Buytenhek }
15391da11f8SLennert Buytenhek 
15491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
15591da11f8SLennert Buytenhek {
15691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
1576d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
15891da11f8SLennert Buytenhek 
15991da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
16091da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
16191da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
16291da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
16391da11f8SLennert Buytenhek }
16491da11f8SLennert Buytenhek 
16591da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
16691da11f8SLennert Buytenhek {
16791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
1686d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
16991da11f8SLennert Buytenhek 
17091da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
171a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
17291da11f8SLennert Buytenhek }
17391da11f8SLennert Buytenhek 
174df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
17591da11f8SLennert Buytenhek {
176df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
1776d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
178df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
179df02c6ffSLennert Buytenhek 	int err;
180df02c6ffSLennert Buytenhek 
181df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
182df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
183df02c6ffSLennert Buytenhek 
184df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
185df02c6ffSLennert Buytenhek 		goto out;
186df02c6ffSLennert Buytenhek 
1878feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
188a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
189df02c6ffSLennert Buytenhek 		if (err < 0)
190df02c6ffSLennert Buytenhek 			return err;
191df02c6ffSLennert Buytenhek 	}
192df02c6ffSLennert Buytenhek 
1938feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
194a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
195df02c6ffSLennert Buytenhek 
196df02c6ffSLennert Buytenhek out:
197d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
19891da11f8SLennert Buytenhek 
19991da11f8SLennert Buytenhek 	return 0;
20091da11f8SLennert Buytenhek }
20191da11f8SLennert Buytenhek 
202*2bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
203*2bedde1aSArkadi Sharshevsky 	struct net_device *dev;
204*2bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
205*2bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
206*2bedde1aSArkadi Sharshevsky 	int idx;
207*2bedde1aSArkadi Sharshevsky };
208*2bedde1aSArkadi Sharshevsky 
209*2bedde1aSArkadi Sharshevsky static int
210*2bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
211*2bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
212*2bedde1aSArkadi Sharshevsky {
213*2bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
214*2bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
215*2bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
216*2bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
217*2bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
218*2bedde1aSArkadi Sharshevsky 
219*2bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
220*2bedde1aSArkadi Sharshevsky 		goto skip;
221*2bedde1aSArkadi Sharshevsky 
222*2bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
223*2bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
224*2bedde1aSArkadi Sharshevsky 	if (!nlh)
225*2bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
226*2bedde1aSArkadi Sharshevsky 
227*2bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
228*2bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
229*2bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
230*2bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
231*2bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
232*2bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
233*2bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
234*2bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
235*2bedde1aSArkadi Sharshevsky 
236*2bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
237*2bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
238*2bedde1aSArkadi Sharshevsky 
239*2bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
240*2bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
241*2bedde1aSArkadi Sharshevsky 
242*2bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
243*2bedde1aSArkadi Sharshevsky 
244*2bedde1aSArkadi Sharshevsky skip:
245*2bedde1aSArkadi Sharshevsky 	dump->idx++;
246*2bedde1aSArkadi Sharshevsky 	return 0;
247*2bedde1aSArkadi Sharshevsky 
248*2bedde1aSArkadi Sharshevsky nla_put_failure:
249*2bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
250*2bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
251*2bedde1aSArkadi Sharshevsky }
252*2bedde1aSArkadi Sharshevsky 
253*2bedde1aSArkadi Sharshevsky static int
254*2bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
255*2bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
256*2bedde1aSArkadi Sharshevsky 		   int *idx)
257*2bedde1aSArkadi Sharshevsky {
258*2bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
259*2bedde1aSArkadi Sharshevsky 		.dev = dev,
260*2bedde1aSArkadi Sharshevsky 		.skb = skb,
261*2bedde1aSArkadi Sharshevsky 		.cb = cb,
262*2bedde1aSArkadi Sharshevsky 		.idx = *idx,
263*2bedde1aSArkadi Sharshevsky 	};
264*2bedde1aSArkadi Sharshevsky 	struct dsa_slave_priv *p = netdev_priv(dev);
265*2bedde1aSArkadi Sharshevsky 	struct dsa_port *dp = p->dp;
266*2bedde1aSArkadi Sharshevsky 	struct dsa_switch *ds = dp->ds;
267*2bedde1aSArkadi Sharshevsky 	int err;
268*2bedde1aSArkadi Sharshevsky 
269*2bedde1aSArkadi Sharshevsky 	if (!ds->ops->port_fdb_dump)
270*2bedde1aSArkadi Sharshevsky 		return -EOPNOTSUPP;
271*2bedde1aSArkadi Sharshevsky 
272*2bedde1aSArkadi Sharshevsky 	err = ds->ops->port_fdb_dump(ds, dp->index,
273*2bedde1aSArkadi Sharshevsky 				     dsa_slave_port_fdb_do_dump,
274*2bedde1aSArkadi Sharshevsky 				     &dump);
275*2bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
276*2bedde1aSArkadi Sharshevsky 	return err;
277*2bedde1aSArkadi Sharshevsky }
278*2bedde1aSArkadi Sharshevsky 
27991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
28091da11f8SLennert Buytenhek {
28191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
28291da11f8SLennert Buytenhek 
28391da11f8SLennert Buytenhek 	if (p->phy != NULL)
28428b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
28591da11f8SLennert Buytenhek 
28691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
28791da11f8SLennert Buytenhek }
28891da11f8SLennert Buytenhek 
28935636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
290f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
2917ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
29235636062SScott Feldman {
293fd364541SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
294fd364541SVivien Didelot 	struct dsa_port *dp = p->dp;
295b8d866acSVivien Didelot 	int ret;
29635636062SScott Feldman 
29735636062SScott Feldman 	switch (attr->id) {
2981f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
299fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
30035636062SScott Feldman 		break;
301fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
302c02c4175SVivien Didelot 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
303c02c4175SVivien Didelot 					      trans);
304fb2dabadSVivien Didelot 		break;
30534a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
306072bb190SVivien Didelot 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
30734a79f63SVivien Didelot 		break;
30835636062SScott Feldman 	default:
30935636062SScott Feldman 		ret = -EOPNOTSUPP;
31035636062SScott Feldman 		break;
31135636062SScott Feldman 	}
31235636062SScott Feldman 
31335636062SScott Feldman 	return ret;
31435636062SScott Feldman }
31535636062SScott Feldman 
316ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
317648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
3187ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
319ba14d9ebSVivien Didelot {
3203fdb023bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
3213fdb023bSVivien Didelot 	struct dsa_port *dp = p->dp;
322ba14d9ebSVivien Didelot 	int err;
323ba14d9ebSVivien Didelot 
324ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
325ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
326ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
327ba14d9ebSVivien Didelot 	 */
328ba14d9ebSVivien Didelot 
3299e8f4a54SJiri Pirko 	switch (obj->id) {
3308df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
331bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
3328df30255SVivien Didelot 		break;
33357d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
33401676d12SVivien Didelot 		err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
335648b4a99SJiri Pirko 					trans);
33611149536SVivien Didelot 		break;
337ba14d9ebSVivien Didelot 	default:
338ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
339ba14d9ebSVivien Didelot 		break;
340ba14d9ebSVivien Didelot 	}
341ba14d9ebSVivien Didelot 
342ba14d9ebSVivien Didelot 	return err;
343ba14d9ebSVivien Didelot }
344ba14d9ebSVivien Didelot 
345ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
346648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
347ba14d9ebSVivien Didelot {
3483fdb023bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
3493fdb023bSVivien Didelot 	struct dsa_port *dp = p->dp;
350ba14d9ebSVivien Didelot 	int err;
351ba14d9ebSVivien Didelot 
3529e8f4a54SJiri Pirko 	switch (obj->id) {
3538df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
354bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3558df30255SVivien Didelot 		break;
35657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
35701676d12SVivien Didelot 		err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
35811149536SVivien Didelot 		break;
359ba14d9ebSVivien Didelot 	default:
360ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
361ba14d9ebSVivien Didelot 		break;
362ba14d9ebSVivien Didelot 	}
363ba14d9ebSVivien Didelot 
364ba14d9ebSVivien Didelot 	return err;
365ba14d9ebSVivien Didelot }
366ba14d9ebSVivien Didelot 
367f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
368f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
369b73adef6SFlorian Fainelli {
370b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
371afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
372b73adef6SFlorian Fainelli 
373f8e20a9fSScott Feldman 	switch (attr->id) {
3741f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
37542275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
37642275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
377f8e20a9fSScott Feldman 		break;
378c9e2105eSArkadi Sharshevsky 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
379c9e2105eSArkadi Sharshevsky 		attr->u.brport_flags_support = 0;
380c9e2105eSArkadi Sharshevsky 		break;
381f8e20a9fSScott Feldman 	default:
382f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
383f8e20a9fSScott Feldman 	}
384b73adef6SFlorian Fainelli 
385b73adef6SFlorian Fainelli 	return 0;
386b73adef6SFlorian Fainelli }
387b73adef6SFlorian Fainelli 
38804ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
38904ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
39004ff53f9SFlorian Fainelli {
39104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
39204ff53f9SFlorian Fainelli 	if (p->netpoll)
39304ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
39404ff53f9SFlorian Fainelli #else
39504ff53f9SFlorian Fainelli 	BUG();
39604ff53f9SFlorian Fainelli #endif
39704ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
39804ff53f9SFlorian Fainelli }
39904ff53f9SFlorian Fainelli 
4003e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
4013e8a72d1SFlorian Fainelli {
4023e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4035f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
4044ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
4053e8a72d1SFlorian Fainelli 
4065f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
4075f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
4085f6b4e14SFlorian Fainelli 	s->tx_packets++;
4095f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
4105f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
4113e8a72d1SFlorian Fainelli 
412fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
413fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
414fe47d563SVivien Didelot 	 */
4154ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
416fe47d563SVivien Didelot 	if (!nskb) {
417fe47d563SVivien Didelot 		kfree_skb(skb);
4184ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
419fe47d563SVivien Didelot 	}
4205aed85ceSFlorian Fainelli 
42104ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
42204ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
42304ff53f9SFlorian Fainelli 	 */
42404ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
42504ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
42604ff53f9SFlorian Fainelli 
4274ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
4284ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
4294ed70ce9SFlorian Fainelli 	 */
4306d3c8c0dSFlorian Fainelli 	nskb->dev = dsa_master_netdev(p);
4314ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
4325aed85ceSFlorian Fainelli 
4335aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
4345aed85ceSFlorian Fainelli }
4355aed85ceSFlorian Fainelli 
43691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
43791da11f8SLennert Buytenhek static int
438bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
439bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
44091da11f8SLennert Buytenhek {
44191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
44291da11f8SLennert Buytenhek 
4435514174fSyuval.shaia@oracle.com 	if (!p->phy)
4445514174fSyuval.shaia@oracle.com 		return -EOPNOTSUPP;
44591da11f8SLennert Buytenhek 
4465514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(p->phy, cmd);
4475514174fSyuval.shaia@oracle.com 
4485514174fSyuval.shaia@oracle.com 	return 0;
44991da11f8SLennert Buytenhek }
45091da11f8SLennert Buytenhek 
45191da11f8SLennert Buytenhek static int
452bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
453bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
45491da11f8SLennert Buytenhek {
45591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
45691da11f8SLennert Buytenhek 
45791da11f8SLennert Buytenhek 	if (p->phy != NULL)
458bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
45991da11f8SLennert Buytenhek 
46091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
46191da11f8SLennert Buytenhek }
46291da11f8SLennert Buytenhek 
46391da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
46491da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
46591da11f8SLennert Buytenhek {
4667826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
4677826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
4687826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
46991da11f8SLennert Buytenhek }
47091da11f8SLennert Buytenhek 
4713d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
4723d762a0fSGuenter Roeck {
4733d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
474afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
4753d762a0fSGuenter Roeck 
4769d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
477afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
4783d762a0fSGuenter Roeck 
4793d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
4803d762a0fSGuenter Roeck }
4813d762a0fSGuenter Roeck 
4823d762a0fSGuenter Roeck static void
4833d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
4843d762a0fSGuenter Roeck {
4853d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
486afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
4873d762a0fSGuenter Roeck 
4889d490b4eSVivien Didelot 	if (ds->ops->get_regs)
489afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
4903d762a0fSGuenter Roeck }
4913d762a0fSGuenter Roeck 
49291da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
49391da11f8SLennert Buytenhek {
49491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
49591da11f8SLennert Buytenhek 
49691da11f8SLennert Buytenhek 	if (p->phy != NULL)
49791da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
49891da11f8SLennert Buytenhek 
49991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
50091da11f8SLennert Buytenhek }
50191da11f8SLennert Buytenhek 
50291da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
50391da11f8SLennert Buytenhek {
50491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
50591da11f8SLennert Buytenhek 
50691da11f8SLennert Buytenhek 	if (p->phy != NULL) {
50791da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
50891da11f8SLennert Buytenhek 		return p->phy->link;
50991da11f8SLennert Buytenhek 	}
51091da11f8SLennert Buytenhek 
51191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
51291da11f8SLennert Buytenhek }
51391da11f8SLennert Buytenhek 
5146793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5156793abb4SGuenter Roeck {
5166793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
517afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5186793abb4SGuenter Roeck 
5190e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
520ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5216793abb4SGuenter Roeck 
5229d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5239d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5246793abb4SGuenter Roeck 
5256793abb4SGuenter Roeck 	return 0;
5266793abb4SGuenter Roeck }
5276793abb4SGuenter Roeck 
5286793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5296793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5306793abb4SGuenter Roeck {
5316793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
532afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5336793abb4SGuenter Roeck 
5349d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5359d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5366793abb4SGuenter Roeck 
5376793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5386793abb4SGuenter Roeck }
5396793abb4SGuenter Roeck 
5406793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5416793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5426793abb4SGuenter Roeck {
5436793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
544afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5456793abb4SGuenter Roeck 
5469d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
5479d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
5486793abb4SGuenter Roeck 
5496793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5506793abb4SGuenter Roeck }
5516793abb4SGuenter Roeck 
55291da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
55391da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
55491da11f8SLennert Buytenhek {
55591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
556afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
55791da11f8SLennert Buytenhek 
55891da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
55991da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
56091da11f8SLennert Buytenhek 
56191da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
56291da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
56391da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
56491da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
5659d490b4eSVivien Didelot 		if (ds->ops->get_strings)
566afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
56791da11f8SLennert Buytenhek 	}
56891da11f8SLennert Buytenhek }
56991da11f8SLennert Buytenhek 
570badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
571badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
572badf3adaSFlorian Fainelli 					   uint64_t *data)
573badf3adaSFlorian Fainelli {
574badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
5753cc9f257SFlorian Fainelli 	struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
5763cc9f257SFlorian Fainelli 	struct dsa_switch *ds = cpu_dp->ds;
5773cc9f257SFlorian Fainelli 	s8 cpu_port = cpu_dp->index;
578badf3adaSFlorian Fainelli 	int count = 0;
579badf3adaSFlorian Fainelli 
5803cc9f257SFlorian Fainelli 	if (cpu_dp->ethtool_ops.get_sset_count) {
5813cc9f257SFlorian Fainelli 		count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
5823cc9f257SFlorian Fainelli 		cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data);
583badf3adaSFlorian Fainelli 	}
584badf3adaSFlorian Fainelli 
5859d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
5869d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
587badf3adaSFlorian Fainelli }
588badf3adaSFlorian Fainelli 
589badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
590badf3adaSFlorian Fainelli {
591badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
5923cc9f257SFlorian Fainelli 	struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
5933cc9f257SFlorian Fainelli 	struct dsa_switch *ds = cpu_dp->ds;
594badf3adaSFlorian Fainelli 	int count = 0;
595badf3adaSFlorian Fainelli 
5963cc9f257SFlorian Fainelli 	if (cpu_dp->ethtool_ops.get_sset_count)
5973cc9f257SFlorian Fainelli 		count += cpu_dp->ethtool_ops.get_sset_count(dev, sset);
598badf3adaSFlorian Fainelli 
5999d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
6009d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
601badf3adaSFlorian Fainelli 
602badf3adaSFlorian Fainelli 	return count;
603badf3adaSFlorian Fainelli }
604badf3adaSFlorian Fainelli 
605badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
606badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
607badf3adaSFlorian Fainelli {
608badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
6093cc9f257SFlorian Fainelli 	struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
6103cc9f257SFlorian Fainelli 	struct dsa_switch *ds = cpu_dp->ds;
6113cc9f257SFlorian Fainelli 	s8 cpu_port = cpu_dp->index;
612badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
613badf3adaSFlorian Fainelli 	int mcount = 0, count;
614badf3adaSFlorian Fainelli 	unsigned int i;
615badf3adaSFlorian Fainelli 	uint8_t pfx[4];
616badf3adaSFlorian Fainelli 	uint8_t *ndata;
617badf3adaSFlorian Fainelli 
618badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
619badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
620badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
621badf3adaSFlorian Fainelli 
6223cc9f257SFlorian Fainelli 	if (cpu_dp->ethtool_ops.get_sset_count) {
6233cc9f257SFlorian Fainelli 		mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
6243cc9f257SFlorian Fainelli 		cpu_dp->ethtool_ops.get_strings(dev, stringset, data);
625badf3adaSFlorian Fainelli 	}
626badf3adaSFlorian Fainelli 
6279d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
628badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
629badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
630badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
631badf3adaSFlorian Fainelli 		 * constructed earlier
632badf3adaSFlorian Fainelli 		 */
6339d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
6349d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
635badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
636badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
637badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
638badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
639badf3adaSFlorian Fainelli 		}
640badf3adaSFlorian Fainelli 	}
641badf3adaSFlorian Fainelli }
642badf3adaSFlorian Fainelli 
64391da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
64491da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
64591da11f8SLennert Buytenhek 					uint64_t *data)
64691da11f8SLennert Buytenhek {
64791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
648afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
6495f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
650f613ed66SFlorian Fainelli 	unsigned int start;
6515f6b4e14SFlorian Fainelli 	int i;
65291da11f8SLennert Buytenhek 
6535f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
6545f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
6555f6b4e14SFlorian Fainelli 
6565f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
657f613ed66SFlorian Fainelli 		do {
6585f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
6595f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
6605f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
6615f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
6625f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
6635f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
6645f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
6655f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
6665f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
6675f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
6685f6b4e14SFlorian Fainelli 	}
6699d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
670afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
67191da11f8SLennert Buytenhek }
67291da11f8SLennert Buytenhek 
67391da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
67491da11f8SLennert Buytenhek {
67591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
676afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
67791da11f8SLennert Buytenhek 
67891da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
67991da11f8SLennert Buytenhek 		int count;
68091da11f8SLennert Buytenhek 
68191da11f8SLennert Buytenhek 		count = 4;
6829d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
6839d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
68491da11f8SLennert Buytenhek 
68591da11f8SLennert Buytenhek 		return count;
68691da11f8SLennert Buytenhek 	}
68791da11f8SLennert Buytenhek 
68891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
68991da11f8SLennert Buytenhek }
69091da11f8SLennert Buytenhek 
69119e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
69219e57c4eSFlorian Fainelli {
69319e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
694afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
69519e57c4eSFlorian Fainelli 
6969d490b4eSVivien Didelot 	if (ds->ops->get_wol)
697afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
69819e57c4eSFlorian Fainelli }
69919e57c4eSFlorian Fainelli 
70019e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
70119e57c4eSFlorian Fainelli {
70219e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
703afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
70419e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
70519e57c4eSFlorian Fainelli 
7069d490b4eSVivien Didelot 	if (ds->ops->set_wol)
707afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
70819e57c4eSFlorian Fainelli 
70919e57c4eSFlorian Fainelli 	return ret;
71019e57c4eSFlorian Fainelli }
71119e57c4eSFlorian Fainelli 
7127905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7137905288fSFlorian Fainelli {
7147905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
715afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7167905288fSFlorian Fainelli 	int ret;
7177905288fSFlorian Fainelli 
7187b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
7197b9cc738SVivien Didelot 	if (!p->phy)
7207b9cc738SVivien Didelot 		return -ENODEV;
7217b9cc738SVivien Didelot 
72208f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
7237905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7247905288fSFlorian Fainelli 
72508f50061SVivien Didelot 	ret = ds->ops->set_mac_eee(ds, p->dp->index, e);
7267905288fSFlorian Fainelli 	if (ret)
7277905288fSFlorian Fainelli 		return ret;
7287905288fSFlorian Fainelli 
729c48f7eb3SVivien Didelot 	if (e->eee_enabled) {
730c48f7eb3SVivien Didelot 		ret = phy_init_eee(p->phy, 0);
731c48f7eb3SVivien Didelot 		if (ret)
732c48f7eb3SVivien Didelot 			return ret;
733c48f7eb3SVivien Didelot 	}
734c48f7eb3SVivien Didelot 
7357b9cc738SVivien Didelot 	return phy_ethtool_set_eee(p->phy, e);
7367905288fSFlorian Fainelli }
7377905288fSFlorian Fainelli 
7387905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7397905288fSFlorian Fainelli {
7407905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
741afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7427905288fSFlorian Fainelli 	int ret;
7437905288fSFlorian Fainelli 
7447b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
7457b9cc738SVivien Didelot 	if (!p->phy)
7467b9cc738SVivien Didelot 		return -ENODEV;
7477b9cc738SVivien Didelot 
74808f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
7497905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7507905288fSFlorian Fainelli 
75108f50061SVivien Didelot 	ret = ds->ops->get_mac_eee(ds, p->dp->index, e);
7527905288fSFlorian Fainelli 	if (ret)
7537905288fSFlorian Fainelli 		return ret;
7547905288fSFlorian Fainelli 
7557b9cc738SVivien Didelot 	return phy_ethtool_get_eee(p->phy, e);
7567905288fSFlorian Fainelli }
7577905288fSFlorian Fainelli 
75804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
75904ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
76004ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
76104ff53f9SFlorian Fainelli {
76204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7636d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
76404ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
76504ff53f9SFlorian Fainelli 	int err = 0;
76604ff53f9SFlorian Fainelli 
76704ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
76804ff53f9SFlorian Fainelli 	if (!netpoll)
76904ff53f9SFlorian Fainelli 		return -ENOMEM;
77004ff53f9SFlorian Fainelli 
77104ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
77204ff53f9SFlorian Fainelli 	if (err) {
77304ff53f9SFlorian Fainelli 		kfree(netpoll);
77404ff53f9SFlorian Fainelli 		goto out;
77504ff53f9SFlorian Fainelli 	}
77604ff53f9SFlorian Fainelli 
77704ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
77804ff53f9SFlorian Fainelli out:
77904ff53f9SFlorian Fainelli 	return err;
78004ff53f9SFlorian Fainelli }
78104ff53f9SFlorian Fainelli 
78204ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
78304ff53f9SFlorian Fainelli {
78404ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
78504ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
78604ff53f9SFlorian Fainelli 
78704ff53f9SFlorian Fainelli 	if (!netpoll)
78804ff53f9SFlorian Fainelli 		return;
78904ff53f9SFlorian Fainelli 
79004ff53f9SFlorian Fainelli 	p->netpoll = NULL;
79104ff53f9SFlorian Fainelli 
79204ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
79304ff53f9SFlorian Fainelli }
79404ff53f9SFlorian Fainelli 
79504ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
79604ff53f9SFlorian Fainelli {
79704ff53f9SFlorian Fainelli }
79804ff53f9SFlorian Fainelli #endif
79904ff53f9SFlorian Fainelli 
80044bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
80144bb765cSFlorian Fainelli 					char *name, size_t len)
80244bb765cSFlorian Fainelli {
80344bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
80444bb765cSFlorian Fainelli 
805afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
80644bb765cSFlorian Fainelli 		return -EINVAL;
8073a543ef4SFlorian Fainelli 
8083a543ef4SFlorian Fainelli 	return 0;
8093a543ef4SFlorian Fainelli }
8103a543ef4SFlorian Fainelli 
811f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
812f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
813f50f2127SFlorian Fainelli 			     unsigned long cookie)
814f50f2127SFlorian Fainelli {
815f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
816f50f2127SFlorian Fainelli 
817f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
818f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
819f50f2127SFlorian Fainelli 			return mall_tc_entry;
820f50f2127SFlorian Fainelli 
821f50f2127SFlorian Fainelli 	return NULL;
822f50f2127SFlorian Fainelli }
823f50f2127SFlorian Fainelli 
824f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
825f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
826f50f2127SFlorian Fainelli 				      bool ingress)
827f50f2127SFlorian Fainelli {
828f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
829f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
8305fd9fc4eSJiri Pirko 	__be16 protocol = cls->common.protocol;
831f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
832f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
833f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
834f50f2127SFlorian Fainelli 	struct net_device *to_dev;
835f50f2127SFlorian Fainelli 	const struct tc_action *a;
836f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
837f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
838f50f2127SFlorian Fainelli 	int ifindex;
839f50f2127SFlorian Fainelli 
840f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
841f50f2127SFlorian Fainelli 		return err;
842f50f2127SFlorian Fainelli 
8433bcc0cecSJiri Pirko 	if (!tcf_exts_has_one_action(cls->exts))
844f50f2127SFlorian Fainelli 		return err;
845f50f2127SFlorian Fainelli 
846f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
847f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
848f50f2127SFlorian Fainelli 
849f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
850f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
851f50f2127SFlorian Fainelli 
852f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
853f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
854f50f2127SFlorian Fainelli 		if (!to_dev)
855f50f2127SFlorian Fainelli 			return -EINVAL;
856f50f2127SFlorian Fainelli 
857f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
858f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
859f50f2127SFlorian Fainelli 
860f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
861f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
862f50f2127SFlorian Fainelli 			return -ENOMEM;
863f50f2127SFlorian Fainelli 
864f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
865f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
866f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
867f50f2127SFlorian Fainelli 
868f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
869f50f2127SFlorian Fainelli 
870f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
871f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
872f50f2127SFlorian Fainelli 
873f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
874f50f2127SFlorian Fainelli 					       ingress);
875f50f2127SFlorian Fainelli 		if (err) {
876f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
877f50f2127SFlorian Fainelli 			return err;
878f50f2127SFlorian Fainelli 		}
879f50f2127SFlorian Fainelli 
880f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
881f50f2127SFlorian Fainelli 	}
882f50f2127SFlorian Fainelli 
883f50f2127SFlorian Fainelli 	return 0;
884f50f2127SFlorian Fainelli }
885f50f2127SFlorian Fainelli 
886f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
887f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
888f50f2127SFlorian Fainelli {
889f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
890f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
891f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
892f50f2127SFlorian Fainelli 
893f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
894f50f2127SFlorian Fainelli 		return;
895f50f2127SFlorian Fainelli 
896f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
897f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
898f50f2127SFlorian Fainelli 		return;
899f50f2127SFlorian Fainelli 
900f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
901f50f2127SFlorian Fainelli 
902f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
903f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
904f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
905f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
906f50f2127SFlorian Fainelli 		break;
907f50f2127SFlorian Fainelli 	default:
908f50f2127SFlorian Fainelli 		WARN_ON(1);
909f50f2127SFlorian Fainelli 	}
910f50f2127SFlorian Fainelli 
911f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
912f50f2127SFlorian Fainelli }
913f50f2127SFlorian Fainelli 
9143fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
9153fbae382SJiri Pirko 					   struct tc_cls_matchall_offload *cls)
916f50f2127SFlorian Fainelli {
9175fd9fc4eSJiri Pirko 	bool ingress = TC_H_MAJ(cls->common.handle) == TC_H_MAJ(TC_H_INGRESS);
918a5fcf8a6SJiri Pirko 
9195fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
920a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
921f50f2127SFlorian Fainelli 
9223fbae382SJiri Pirko 	switch (cls->command) {
9233fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
9245fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
9253fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
9263fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
9273fbae382SJiri Pirko 		return 0;
9283fbae382SJiri Pirko 	default:
9293fbae382SJiri Pirko 		return -EOPNOTSUPP;
9303fbae382SJiri Pirko 	}
9313fbae382SJiri Pirko }
9323fbae382SJiri Pirko 
9333fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
934de4784caSJiri Pirko 			      void *type_data)
9353fbae382SJiri Pirko {
9362572ac53SJiri Pirko 	switch (type) {
937ade9b658SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
938de4784caSJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data);
939f50f2127SFlorian Fainelli 	default:
940a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
941f50f2127SFlorian Fainelli 	}
942f50f2127SFlorian Fainelli }
943f50f2127SFlorian Fainelli 
944f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
945f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
946f613ed66SFlorian Fainelli {
947f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
9485f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
949f613ed66SFlorian Fainelli 	unsigned int start;
9505f6b4e14SFlorian Fainelli 	int i;
951f613ed66SFlorian Fainelli 
952f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
9535f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
9545f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
9555f6b4e14SFlorian Fainelli 
9565f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
957f613ed66SFlorian Fainelli 		do {
9585f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
9595f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
9605f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
9615f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
9625f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
9635f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9645f6b4e14SFlorian Fainelli 
9655f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
9665f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
9675f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
9685f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
9695f6b4e14SFlorian Fainelli 	}
970f613ed66SFlorian Fainelli }
971f613ed66SFlorian Fainelli 
972af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
973af42192cSFlorian Fainelli {
974af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
975af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
976af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
977af42192cSFlorian Fainelli }
978af42192cSFlorian Fainelli 
979bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
980bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
981bf9f2648SFlorian Fainelli {
982bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
983bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
984bf9f2648SFlorian Fainelli 
985bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
986bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
987bf9f2648SFlorian Fainelli 
988bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
989bf9f2648SFlorian Fainelli }
990bf9f2648SFlorian Fainelli 
991bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
992bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
993bf9f2648SFlorian Fainelli {
994bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
995bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
996bf9f2648SFlorian Fainelli 
997bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
998bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
999bf9f2648SFlorian Fainelli 
1000bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1001bf9f2648SFlorian Fainelli }
1002bf9f2648SFlorian Fainelli 
100391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
100491da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
10053d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
10063d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
100791da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
100891da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
10096793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
10106793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
10116793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
101291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
101391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
101491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
101519e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
101619e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
10177905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
10187905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1019bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1020bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1021bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1022bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
102391da11f8SLennert Buytenhek };
102491da11f8SLennert Buytenhek 
10253e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1026d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1027d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
10283e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1029d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1030d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1031d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
103237b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
103337b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
1034*2bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1035d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1036abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
103704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
103804ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
103904ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
104004ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
104104ff53f9SFlorian Fainelli #endif
104244bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1043f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1044f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
104598237d43SScott Feldman };
104698237d43SScott Feldman 
10479d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1048f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
104935636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1050ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1051ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1052d442ad4aSStephen Hemminger };
105391da11f8SLennert Buytenhek 
1054f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1055f37db85dSFlorian Fainelli 	.name	= "dsa",
1056f37db85dSFlorian Fainelli };
1057f37db85dSFlorian Fainelli 
10580d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
10590d8bcdd3SFlorian Fainelli {
10600d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1061afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
10620d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
10630d8bcdd3SFlorian Fainelli 
10640d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
10650d8bcdd3SFlorian Fainelli 		status_changed = 1;
10660d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
10670d8bcdd3SFlorian Fainelli 	}
10680d8bcdd3SFlorian Fainelli 
10690d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
10700d8bcdd3SFlorian Fainelli 		status_changed = 1;
10710d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
10720d8bcdd3SFlorian Fainelli 	}
10730d8bcdd3SFlorian Fainelli 
10740d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
10750d8bcdd3SFlorian Fainelli 		status_changed = 1;
10760d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
10770d8bcdd3SFlorian Fainelli 	}
10780d8bcdd3SFlorian Fainelli 
10799d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1080afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1081ec9436baSFlorian Fainelli 
10820d8bcdd3SFlorian Fainelli 	if (status_changed)
10830d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
10840d8bcdd3SFlorian Fainelli }
10850d8bcdd3SFlorian Fainelli 
1086ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1087ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1088ce31b31cSFlorian Fainelli {
1089b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1090b71be352SAndrew Lunn 	struct dsa_switch *ds;
1091ce31b31cSFlorian Fainelli 
1092b71be352SAndrew Lunn 	if (dev) {
1093b71be352SAndrew Lunn 		p = netdev_priv(dev);
1094afdcf151SVivien Didelot 		ds = p->dp->ds;
10959d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1096afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1097b71be352SAndrew Lunn 	}
1098ce31b31cSFlorian Fainelli 
1099ce31b31cSFlorian Fainelli 	return 0;
1100ce31b31cSFlorian Fainelli }
1101ce31b31cSFlorian Fainelli 
110291da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1103c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1104cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1105cd28a1a9SFlorian Fainelli 				 int addr)
1106c305c165SFlorian Fainelli {
1107afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1108c305c165SFlorian Fainelli 
11097f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1110d25b8e74SRussell King 	if (!p->phy) {
1111d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1112c305c165SFlorian Fainelli 		return -ENODEV;
1113d25b8e74SRussell King 	}
1114c305c165SFlorian Fainelli 
1115c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1116211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1117c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
11184078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1119c305c165SFlorian Fainelli 				  p->phy_interface);
1120c305c165SFlorian Fainelli }
1121c305c165SFlorian Fainelli 
11229697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
11230d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
11240d8bcdd3SFlorian Fainelli {
1125afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
11260d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1127ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
11286819563eSFlorian Fainelli 	u32 phy_flags = 0;
112919334920SGuenter Roeck 	int mode, ret;
11300d8bcdd3SFlorian Fainelli 
1131afdcf151SVivien Didelot 	port_dn = p->dp->dn;
113219334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
113319334920SGuenter Roeck 	if (mode < 0)
113419334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
113519334920SGuenter Roeck 	p->phy_interface = mode;
11360d8bcdd3SFlorian Fainelli 
11370d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
11380d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
11390d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
11400d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
11410d8bcdd3SFlorian Fainelli 		 */
11420d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
11430d8bcdd3SFlorian Fainelli 		if (ret) {
1144d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
11459697f1cdSFlorian Fainelli 			return ret;
11460d8bcdd3SFlorian Fainelli 		}
1147ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
11480d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
11490d8bcdd3SFlorian Fainelli 	}
11500d8bcdd3SFlorian Fainelli 
11519d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1152afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
11536819563eSFlorian Fainelli 
1154cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1155d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1156d25b8e74SRussell King 
1157cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1158cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1159cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1160cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1161cd28a1a9SFlorian Fainelli 		 */
1162d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1163d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1164d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1165d25b8e74SRussell King 			if (ret) {
1166d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
11670d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1168cd28a1a9SFlorian Fainelli 				return ret;
1169d25b8e74SRussell King 			}
1170cd28a1a9SFlorian Fainelli 		} else {
11710d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1172cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1173cd28a1a9SFlorian Fainelli 						phy_flags,
11740d8bcdd3SFlorian Fainelli 						p->phy_interface);
1175cd28a1a9SFlorian Fainelli 		}
11760d8f3c67SJohan Hovold 
11770d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1178cd28a1a9SFlorian Fainelli 	}
11790d8bcdd3SFlorian Fainelli 
1180ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1181ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1182ce31b31cSFlorian Fainelli 
11830d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
11840d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
11850d8bcdd3SFlorian Fainelli 	 */
1186b31f65fbSAndrew Lunn 	if (!p->phy) {
1187afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1188d25b8e74SRussell King 		if (ret) {
1189afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1190afdcf151SVivien Didelot 				   p->dp->index, ret);
1191881eadabSJohan Hovold 			if (phy_is_fixed)
1192881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1193c305c165SFlorian Fainelli 			return ret;
1194d25b8e74SRussell King 		}
11950d8bcdd3SFlorian Fainelli 	}
11969697f1cdSFlorian Fainelli 
11972220943aSAndrew Lunn 	phy_attached_info(p->phy);
11982220943aSAndrew Lunn 
11999697f1cdSFlorian Fainelli 	return 0;
1200b31f65fbSAndrew Lunn }
12010d8bcdd3SFlorian Fainelli 
1202448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1203448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1204448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1205448b4482SAndrew Lunn 					    void *_unused)
1206448b4482SAndrew Lunn {
1207448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1208448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1209448b4482SAndrew Lunn }
1210448b4482SAndrew Lunn 
121124462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
121224462549SFlorian Fainelli {
121324462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
121424462549SFlorian Fainelli 
1215f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1216f154be24SFlorian Fainelli 
121724462549SFlorian Fainelli 	if (p->phy) {
121824462549SFlorian Fainelli 		phy_stop(p->phy);
121924462549SFlorian Fainelli 		p->old_pause = -1;
122024462549SFlorian Fainelli 		p->old_link = -1;
122124462549SFlorian Fainelli 		p->old_duplex = -1;
122224462549SFlorian Fainelli 		phy_suspend(p->phy);
122324462549SFlorian Fainelli 	}
122424462549SFlorian Fainelli 
122524462549SFlorian Fainelli 	return 0;
122624462549SFlorian Fainelli }
122724462549SFlorian Fainelli 
122824462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
122924462549SFlorian Fainelli {
123024462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
123124462549SFlorian Fainelli 
123224462549SFlorian Fainelli 	netif_device_attach(slave_dev);
123324462549SFlorian Fainelli 
123424462549SFlorian Fainelli 	if (p->phy) {
123524462549SFlorian Fainelli 		phy_resume(p->phy);
123624462549SFlorian Fainelli 		phy_start(p->phy);
123724462549SFlorian Fainelli 	}
123824462549SFlorian Fainelli 
123924462549SFlorian Fainelli 	return 0;
124024462549SFlorian Fainelli }
124124462549SFlorian Fainelli 
12424cfbf09cSVivien Didelot int dsa_slave_create(struct dsa_port *port, const char *name)
124391da11f8SLennert Buytenhek {
12444cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1245badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
124683c0afaeSAndrew Lunn 	struct net_device *master;
124791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
124891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
1249a29342e7SFlorian Fainelli 	struct dsa_port *cpu_dp;
125091da11f8SLennert Buytenhek 	int ret;
125191da11f8SLennert Buytenhek 
1252a29342e7SFlorian Fainelli 	cpu_dp = ds->dst->cpu_dp;
1253a29342e7SFlorian Fainelli 	master = cpu_dp->netdev;
125483c0afaeSAndrew Lunn 
1255c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1256c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
125791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1258d87d6f44SGuenter Roeck 		return -ENOMEM;
125991da11f8SLennert Buytenhek 
1260f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1261f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
12627ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
12632fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
12640a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
12653e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
12669d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
12678b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
12688b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1269f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1270d442ad4aSStephen Hemminger 
1271448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1272448b4482SAndrew Lunn 				 NULL);
1273448b4482SAndrew Lunn 
12744cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
12754cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
127691da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
127791da11f8SLennert Buytenhek 
127891da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
12795f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
12805f6b4e14SFlorian Fainelli 	if (!p->stats64) {
12815f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
12825f6b4e14SFlorian Fainelli 		return -ENOMEM;
12835f6b4e14SFlorian Fainelli 	}
12844cfbf09cSVivien Didelot 	p->dp = port;
1285f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
128639a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
12875075314eSAlexander Duyck 
12880d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
12890d8bcdd3SFlorian Fainelli 	p->old_link = -1;
12900d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
12910d8bcdd3SFlorian Fainelli 
12924cfbf09cSVivien Didelot 	port->netdev = slave_dev;
129391da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
129491da11f8SLennert Buytenhek 	if (ret) {
1295a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1296a2ae6007SJoe Perches 			   ret, slave_dev->name);
12974cfbf09cSVivien Didelot 		port->netdev = NULL;
12985f6b4e14SFlorian Fainelli 		free_percpu(p->stats64);
129991da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1300d87d6f44SGuenter Roeck 		return ret;
130191da11f8SLennert Buytenhek 	}
130291da11f8SLennert Buytenhek 
130391da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
130491da11f8SLennert Buytenhek 
13050071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
13060071f56eSAndrew Lunn 	if (ret) {
13070071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
130873dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
13095f6b4e14SFlorian Fainelli 		free_percpu(p->stats64);
13100071f56eSAndrew Lunn 		free_netdev(slave_dev);
13110071f56eSAndrew Lunn 		return ret;
13120071f56eSAndrew Lunn 	}
13130071f56eSAndrew Lunn 
1314d87d6f44SGuenter Roeck 	return 0;
131591da11f8SLennert Buytenhek }
1316b73adef6SFlorian Fainelli 
1317cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1318cda5c15bSNeil Armstrong {
1319cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1320881eadabSJohan Hovold 	struct device_node *port_dn;
1321881eadabSJohan Hovold 
1322afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1323cda5c15bSNeil Armstrong 
1324cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1325881eadabSJohan Hovold 	if (p->phy) {
1326cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1327881eadabSJohan Hovold 
1328881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1329881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1330881eadabSJohan Hovold 	}
1331cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
13325f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1333cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1334cda5c15bSNeil Armstrong }
1335cda5c15bSNeil Armstrong 
1336b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1337b73adef6SFlorian Fainelli {
1338b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1339b73adef6SFlorian Fainelli }
1340b73adef6SFlorian Fainelli 
13418e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
13428e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1343b73adef6SFlorian Fainelli {
134417d7802bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
134517d7802bSVivien Didelot 	struct dsa_port *dp = p->dp;
13468e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1347b73adef6SFlorian Fainelli 
13488e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
13498e92ab3aSVivien Didelot 		if (info->linking) {
135017d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
13518e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
13528e92ab3aSVivien Didelot 		} else {
135317d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
13548e92ab3aSVivien Didelot 			err = NOTIFY_OK;
13558e92ab3aSVivien Didelot 		}
13566debb68aSVivien Didelot 	}
1357b73adef6SFlorian Fainelli 
13588e92ab3aSVivien Didelot 	return err;
1359b73adef6SFlorian Fainelli }
1360b73adef6SFlorian Fainelli 
136188e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1362b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1363b73adef6SFlorian Fainelli {
13646debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1365b73adef6SFlorian Fainelli 
13668e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
13678e92ab3aSVivien Didelot 		return NOTIFY_DONE;
13688e92ab3aSVivien Didelot 
13698e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
13708e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1371b73adef6SFlorian Fainelli 
1372b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1373b73adef6SFlorian Fainelli }
137488e4f0caSVivien Didelot 
1375c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1376c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1377c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1378c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1379c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1380c9eb3e0fSArkadi Sharshevsky };
1381c9eb3e0fSArkadi Sharshevsky 
1382c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1383c9eb3e0fSArkadi Sharshevsky {
1384c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1385c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1386c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1387c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1388c9eb3e0fSArkadi Sharshevsky 	struct dsa_slave_priv *p = netdev_priv(dev);
1389c9eb3e0fSArkadi Sharshevsky 	int err;
1390c9eb3e0fSArkadi Sharshevsky 
1391c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1392c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1393c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1394c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1395c9eb3e0fSArkadi Sharshevsky 		err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid);
1396c9eb3e0fSArkadi Sharshevsky 		if (err) {
1397c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1398c9eb3e0fSArkadi Sharshevsky 			break;
1399c9eb3e0fSArkadi Sharshevsky 		}
1400c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1401c9eb3e0fSArkadi Sharshevsky 					 &fdb_info->info);
1402c9eb3e0fSArkadi Sharshevsky 		break;
1403c9eb3e0fSArkadi Sharshevsky 
1404c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1405c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1406c9eb3e0fSArkadi Sharshevsky 		err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid);
1407c9eb3e0fSArkadi Sharshevsky 		if (err) {
1408c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1409c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1410c9eb3e0fSArkadi Sharshevsky 		}
1411c9eb3e0fSArkadi Sharshevsky 		break;
1412c9eb3e0fSArkadi Sharshevsky 	}
1413c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1414c9eb3e0fSArkadi Sharshevsky 
1415c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1416c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1417c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1418c9eb3e0fSArkadi Sharshevsky }
1419c9eb3e0fSArkadi Sharshevsky 
1420c9eb3e0fSArkadi Sharshevsky static int
1421c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1422c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1423c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1424c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1425c9eb3e0fSArkadi Sharshevsky {
1426c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1427c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1428c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1429c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1430c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1431c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1432c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1433c9eb3e0fSArkadi Sharshevsky 	return 0;
1434c9eb3e0fSArkadi Sharshevsky }
1435c9eb3e0fSArkadi Sharshevsky 
1436c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1437c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
1438c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
1439c9eb3e0fSArkadi Sharshevsky {
1440c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1441c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
1442c9eb3e0fSArkadi Sharshevsky 
1443c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
1444c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1445c9eb3e0fSArkadi Sharshevsky 
1446c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1447c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
1448c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
1449c9eb3e0fSArkadi Sharshevsky 
1450c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
1451c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
1452c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
1453c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
1454c9eb3e0fSArkadi Sharshevsky 
1455c9eb3e0fSArkadi Sharshevsky 	switch (event) {
1456c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1457c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1458c9eb3e0fSArkadi Sharshevsky 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1459c9eb3e0fSArkadi Sharshevsky 						      ptr))
1460c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
1461c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
1462c9eb3e0fSArkadi Sharshevsky 		break;
1463c9eb3e0fSArkadi Sharshevsky 	default:
1464c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
1465c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1466c9eb3e0fSArkadi Sharshevsky 	}
1467c9eb3e0fSArkadi Sharshevsky 
1468c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
1469c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
1470c9eb3e0fSArkadi Sharshevsky 
1471c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
1472c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1473c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
1474c9eb3e0fSArkadi Sharshevsky }
1475c9eb3e0fSArkadi Sharshevsky 
147688e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
147788e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
147888e4f0caSVivien Didelot };
147988e4f0caSVivien Didelot 
1480c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
1481c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
1482c9eb3e0fSArkadi Sharshevsky };
1483c9eb3e0fSArkadi Sharshevsky 
148488e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
148588e4f0caSVivien Didelot {
1486c9eb3e0fSArkadi Sharshevsky 	int err;
1487c9eb3e0fSArkadi Sharshevsky 
1488c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
1489c9eb3e0fSArkadi Sharshevsky 	if (err)
1490c9eb3e0fSArkadi Sharshevsky 		return err;
1491c9eb3e0fSArkadi Sharshevsky 
1492c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1493c9eb3e0fSArkadi Sharshevsky 	if (err)
1494c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
1495c9eb3e0fSArkadi Sharshevsky 
1496c9eb3e0fSArkadi Sharshevsky 	return 0;
1497c9eb3e0fSArkadi Sharshevsky 
1498c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
1499c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
1500c9eb3e0fSArkadi Sharshevsky 	return err;
150188e4f0caSVivien Didelot }
150288e4f0caSVivien Didelot 
150388e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
150488e4f0caSVivien Didelot {
150588e4f0caSVivien Didelot 	int err;
150688e4f0caSVivien Didelot 
1507c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1508c9eb3e0fSArkadi Sharshevsky 	if (err)
1509c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1510c9eb3e0fSArkadi Sharshevsky 
151188e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
151288e4f0caSVivien Didelot 	if (err)
151388e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
151488e4f0caSVivien Didelot }
1515