xref: /openbmc/linux/net/dsa/slave.c (revision 4fa7b718881a5358286903b796968cea80993820)
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 
2022bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
2032bedde1aSArkadi Sharshevsky 	struct net_device *dev;
2042bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
2052bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
2062bedde1aSArkadi Sharshevsky 	int idx;
2072bedde1aSArkadi Sharshevsky };
2082bedde1aSArkadi Sharshevsky 
2092bedde1aSArkadi Sharshevsky static int
2102bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
2112bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
2122bedde1aSArkadi Sharshevsky {
2132bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
2142bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
2152bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
2162bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
2172bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
2182bedde1aSArkadi Sharshevsky 
2192bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2202bedde1aSArkadi Sharshevsky 		goto skip;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2232bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2242bedde1aSArkadi Sharshevsky 	if (!nlh)
2252bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2262bedde1aSArkadi Sharshevsky 
2272bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2282bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2292bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2302bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2312bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2322bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2332bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2342bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2352bedde1aSArkadi Sharshevsky 
2362bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2372bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2382bedde1aSArkadi Sharshevsky 
2392bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2402bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2412bedde1aSArkadi Sharshevsky 
2422bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2432bedde1aSArkadi Sharshevsky 
2442bedde1aSArkadi Sharshevsky skip:
2452bedde1aSArkadi Sharshevsky 	dump->idx++;
2462bedde1aSArkadi Sharshevsky 	return 0;
2472bedde1aSArkadi Sharshevsky 
2482bedde1aSArkadi Sharshevsky nla_put_failure:
2492bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2502bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2512bedde1aSArkadi Sharshevsky }
2522bedde1aSArkadi Sharshevsky 
2532bedde1aSArkadi Sharshevsky static int
2542bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2552bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2562bedde1aSArkadi Sharshevsky 		   int *idx)
2572bedde1aSArkadi Sharshevsky {
2582bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2592bedde1aSArkadi Sharshevsky 		.dev = dev,
2602bedde1aSArkadi Sharshevsky 		.skb = skb,
2612bedde1aSArkadi Sharshevsky 		.cb = cb,
2622bedde1aSArkadi Sharshevsky 		.idx = *idx,
2632bedde1aSArkadi Sharshevsky 	};
2642bedde1aSArkadi Sharshevsky 	struct dsa_slave_priv *p = netdev_priv(dev);
2652bedde1aSArkadi Sharshevsky 	struct dsa_port *dp = p->dp;
2662bedde1aSArkadi Sharshevsky 	struct dsa_switch *ds = dp->ds;
2672bedde1aSArkadi Sharshevsky 	int err;
2682bedde1aSArkadi Sharshevsky 
2692bedde1aSArkadi Sharshevsky 	if (!ds->ops->port_fdb_dump)
2702bedde1aSArkadi Sharshevsky 		return -EOPNOTSUPP;
2712bedde1aSArkadi Sharshevsky 
2722bedde1aSArkadi Sharshevsky 	err = ds->ops->port_fdb_dump(ds, dp->index,
2732bedde1aSArkadi Sharshevsky 				     dsa_slave_port_fdb_do_dump,
2742bedde1aSArkadi Sharshevsky 				     &dump);
2752bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
2762bedde1aSArkadi Sharshevsky 	return err;
2772bedde1aSArkadi Sharshevsky }
2782bedde1aSArkadi 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 
388*4fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
38904ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
39004ff53f9SFlorian Fainelli {
39104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
392*4fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
393*4fa7b718SVivien Didelot 
39404ff53f9SFlorian Fainelli 	if (p->netpoll)
39504ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
39604ff53f9SFlorian Fainelli #else
39704ff53f9SFlorian Fainelli 	BUG();
39804ff53f9SFlorian Fainelli #endif
39904ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
40004ff53f9SFlorian Fainelli }
40104ff53f9SFlorian Fainelli 
4023e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
4033e8a72d1SFlorian Fainelli {
4043e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4055f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
4064ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
4073e8a72d1SFlorian Fainelli 
4085f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
4095f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
4105f6b4e14SFlorian Fainelli 	s->tx_packets++;
4115f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
4125f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
4133e8a72d1SFlorian Fainelli 
414fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
415fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
416fe47d563SVivien Didelot 	 */
4174ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
418fe47d563SVivien Didelot 	if (!nskb) {
419fe47d563SVivien Didelot 		kfree_skb(skb);
4204ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
421fe47d563SVivien Didelot 	}
4225aed85ceSFlorian Fainelli 
42304ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
42404ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
42504ff53f9SFlorian Fainelli 	 */
42604ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
427*4fa7b718SVivien Didelot 		return dsa_slave_netpoll_send_skb(dev, nskb);
42804ff53f9SFlorian Fainelli 
4294ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
4304ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
4314ed70ce9SFlorian Fainelli 	 */
4326d3c8c0dSFlorian Fainelli 	nskb->dev = dsa_master_netdev(p);
4334ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
4345aed85ceSFlorian Fainelli 
4355aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
4365aed85ceSFlorian Fainelli }
4375aed85ceSFlorian Fainelli 
43891da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
43991da11f8SLennert Buytenhek static int
440bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
441bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
44291da11f8SLennert Buytenhek {
44391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
44491da11f8SLennert Buytenhek 
4455514174fSyuval.shaia@oracle.com 	if (!p->phy)
4465514174fSyuval.shaia@oracle.com 		return -EOPNOTSUPP;
44791da11f8SLennert Buytenhek 
4485514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(p->phy, cmd);
4495514174fSyuval.shaia@oracle.com 
4505514174fSyuval.shaia@oracle.com 	return 0;
45191da11f8SLennert Buytenhek }
45291da11f8SLennert Buytenhek 
45391da11f8SLennert Buytenhek static int
454bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
455bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
45691da11f8SLennert Buytenhek {
45791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
45891da11f8SLennert Buytenhek 
45991da11f8SLennert Buytenhek 	if (p->phy != NULL)
460bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
46191da11f8SLennert Buytenhek 
46291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
46391da11f8SLennert Buytenhek }
46491da11f8SLennert Buytenhek 
46591da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
46691da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
46791da11f8SLennert Buytenhek {
4687826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
4697826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
4707826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
47191da11f8SLennert Buytenhek }
47291da11f8SLennert Buytenhek 
4733d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
4743d762a0fSGuenter Roeck {
4753d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
476afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
4773d762a0fSGuenter Roeck 
4789d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
479afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
4803d762a0fSGuenter Roeck 
4813d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
4823d762a0fSGuenter Roeck }
4833d762a0fSGuenter Roeck 
4843d762a0fSGuenter Roeck static void
4853d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
4863d762a0fSGuenter Roeck {
4873d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
488afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
4893d762a0fSGuenter Roeck 
4909d490b4eSVivien Didelot 	if (ds->ops->get_regs)
491afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
4923d762a0fSGuenter Roeck }
4933d762a0fSGuenter Roeck 
49491da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
49591da11f8SLennert Buytenhek {
49691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
49791da11f8SLennert Buytenhek 
49891da11f8SLennert Buytenhek 	if (p->phy != NULL)
49991da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
50091da11f8SLennert Buytenhek 
50191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
50291da11f8SLennert Buytenhek }
50391da11f8SLennert Buytenhek 
50491da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
50591da11f8SLennert Buytenhek {
50691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
50791da11f8SLennert Buytenhek 
50891da11f8SLennert Buytenhek 	if (p->phy != NULL) {
50991da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
51091da11f8SLennert Buytenhek 		return p->phy->link;
51191da11f8SLennert Buytenhek 	}
51291da11f8SLennert Buytenhek 
51391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
51491da11f8SLennert Buytenhek }
51591da11f8SLennert Buytenhek 
5166793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5176793abb4SGuenter Roeck {
5186793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
519afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5206793abb4SGuenter Roeck 
5210e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
522ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5236793abb4SGuenter Roeck 
5249d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5259d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5266793abb4SGuenter Roeck 
5276793abb4SGuenter Roeck 	return 0;
5286793abb4SGuenter Roeck }
5296793abb4SGuenter Roeck 
5306793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5316793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5326793abb4SGuenter Roeck {
5336793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
534afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5356793abb4SGuenter Roeck 
5369d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5379d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5386793abb4SGuenter Roeck 
5396793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5406793abb4SGuenter Roeck }
5416793abb4SGuenter Roeck 
5426793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5436793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5446793abb4SGuenter Roeck {
5456793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
546afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5476793abb4SGuenter Roeck 
5489d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
5499d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
5506793abb4SGuenter Roeck 
5516793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5526793abb4SGuenter Roeck }
5536793abb4SGuenter Roeck 
55491da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
55591da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
55691da11f8SLennert Buytenhek {
55791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
558afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
55991da11f8SLennert Buytenhek 
56091da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
56191da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
56291da11f8SLennert Buytenhek 
56391da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
56491da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
56591da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
56691da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
5679d490b4eSVivien Didelot 		if (ds->ops->get_strings)
568afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
56991da11f8SLennert Buytenhek 	}
57091da11f8SLennert Buytenhek }
57191da11f8SLennert Buytenhek 
57291da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
57391da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
57491da11f8SLennert Buytenhek 					uint64_t *data)
57591da11f8SLennert Buytenhek {
57691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
577afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
5785f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
579f613ed66SFlorian Fainelli 	unsigned int start;
5805f6b4e14SFlorian Fainelli 	int i;
58191da11f8SLennert Buytenhek 
5825f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
5835f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
5845f6b4e14SFlorian Fainelli 
5855f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
586f613ed66SFlorian Fainelli 		do {
5875f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
5885f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
5895f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
5905f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
5915f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
5925f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
5935f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
5945f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
5955f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
5965f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
5975f6b4e14SFlorian Fainelli 	}
5989d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
599afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
60091da11f8SLennert Buytenhek }
60191da11f8SLennert Buytenhek 
60291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
60391da11f8SLennert Buytenhek {
60491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
605afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
60691da11f8SLennert Buytenhek 
60791da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
60891da11f8SLennert Buytenhek 		int count;
60991da11f8SLennert Buytenhek 
61091da11f8SLennert Buytenhek 		count = 4;
6119d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
6129d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
61391da11f8SLennert Buytenhek 
61491da11f8SLennert Buytenhek 		return count;
61591da11f8SLennert Buytenhek 	}
61691da11f8SLennert Buytenhek 
61791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
61891da11f8SLennert Buytenhek }
61991da11f8SLennert Buytenhek 
62019e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
62119e57c4eSFlorian Fainelli {
62219e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
623afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
62419e57c4eSFlorian Fainelli 
6259d490b4eSVivien Didelot 	if (ds->ops->get_wol)
626afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
62719e57c4eSFlorian Fainelli }
62819e57c4eSFlorian Fainelli 
62919e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
63019e57c4eSFlorian Fainelli {
63119e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
632afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
63319e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
63419e57c4eSFlorian Fainelli 
6359d490b4eSVivien Didelot 	if (ds->ops->set_wol)
636afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
63719e57c4eSFlorian Fainelli 
63819e57c4eSFlorian Fainelli 	return ret;
63919e57c4eSFlorian Fainelli }
64019e57c4eSFlorian Fainelli 
6417905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
6427905288fSFlorian Fainelli {
6437905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
644afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
6457905288fSFlorian Fainelli 	int ret;
6467905288fSFlorian Fainelli 
6477b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
6487b9cc738SVivien Didelot 	if (!p->phy)
6497b9cc738SVivien Didelot 		return -ENODEV;
6507b9cc738SVivien Didelot 
65108f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
6527905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6537905288fSFlorian Fainelli 
65408f50061SVivien Didelot 	ret = ds->ops->set_mac_eee(ds, p->dp->index, e);
6557905288fSFlorian Fainelli 	if (ret)
6567905288fSFlorian Fainelli 		return ret;
6577905288fSFlorian Fainelli 
658c48f7eb3SVivien Didelot 	if (e->eee_enabled) {
659c48f7eb3SVivien Didelot 		ret = phy_init_eee(p->phy, 0);
660c48f7eb3SVivien Didelot 		if (ret)
661c48f7eb3SVivien Didelot 			return ret;
662c48f7eb3SVivien Didelot 	}
663c48f7eb3SVivien Didelot 
6647b9cc738SVivien Didelot 	return phy_ethtool_set_eee(p->phy, e);
6657905288fSFlorian Fainelli }
6667905288fSFlorian Fainelli 
6677905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
6687905288fSFlorian Fainelli {
6697905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
670afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
6717905288fSFlorian Fainelli 	int ret;
6727905288fSFlorian Fainelli 
6737b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
6747b9cc738SVivien Didelot 	if (!p->phy)
6757b9cc738SVivien Didelot 		return -ENODEV;
6767b9cc738SVivien Didelot 
67708f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
6787905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6797905288fSFlorian Fainelli 
68008f50061SVivien Didelot 	ret = ds->ops->get_mac_eee(ds, p->dp->index, e);
6817905288fSFlorian Fainelli 	if (ret)
6827905288fSFlorian Fainelli 		return ret;
6837905288fSFlorian Fainelli 
6847b9cc738SVivien Didelot 	return phy_ethtool_get_eee(p->phy, e);
6857905288fSFlorian Fainelli }
6867905288fSFlorian Fainelli 
68704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
68804ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
68904ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
69004ff53f9SFlorian Fainelli {
69104ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6926d3c8c0dSFlorian Fainelli 	struct net_device *master = dsa_master_netdev(p);
69304ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
69404ff53f9SFlorian Fainelli 	int err = 0;
69504ff53f9SFlorian Fainelli 
69604ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
69704ff53f9SFlorian Fainelli 	if (!netpoll)
69804ff53f9SFlorian Fainelli 		return -ENOMEM;
69904ff53f9SFlorian Fainelli 
70004ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
70104ff53f9SFlorian Fainelli 	if (err) {
70204ff53f9SFlorian Fainelli 		kfree(netpoll);
70304ff53f9SFlorian Fainelli 		goto out;
70404ff53f9SFlorian Fainelli 	}
70504ff53f9SFlorian Fainelli 
70604ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
70704ff53f9SFlorian Fainelli out:
70804ff53f9SFlorian Fainelli 	return err;
70904ff53f9SFlorian Fainelli }
71004ff53f9SFlorian Fainelli 
71104ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
71204ff53f9SFlorian Fainelli {
71304ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
71404ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
71504ff53f9SFlorian Fainelli 
71604ff53f9SFlorian Fainelli 	if (!netpoll)
71704ff53f9SFlorian Fainelli 		return;
71804ff53f9SFlorian Fainelli 
71904ff53f9SFlorian Fainelli 	p->netpoll = NULL;
72004ff53f9SFlorian Fainelli 
72104ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
72204ff53f9SFlorian Fainelli }
72304ff53f9SFlorian Fainelli 
72404ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
72504ff53f9SFlorian Fainelli {
72604ff53f9SFlorian Fainelli }
72704ff53f9SFlorian Fainelli #endif
72804ff53f9SFlorian Fainelli 
72944bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
73044bb765cSFlorian Fainelli 					char *name, size_t len)
73144bb765cSFlorian Fainelli {
73244bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
73344bb765cSFlorian Fainelli 
734afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
73544bb765cSFlorian Fainelli 		return -EINVAL;
7363a543ef4SFlorian Fainelli 
7373a543ef4SFlorian Fainelli 	return 0;
7383a543ef4SFlorian Fainelli }
7393a543ef4SFlorian Fainelli 
740f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
741*4fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
742f50f2127SFlorian Fainelli {
743*4fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
744f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
745f50f2127SFlorian Fainelli 
746f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
747f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
748f50f2127SFlorian Fainelli 			return mall_tc_entry;
749f50f2127SFlorian Fainelli 
750f50f2127SFlorian Fainelli 	return NULL;
751f50f2127SFlorian Fainelli }
752f50f2127SFlorian Fainelli 
753f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
754f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
755f50f2127SFlorian Fainelli 				      bool ingress)
756f50f2127SFlorian Fainelli {
757f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
758f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
7595fd9fc4eSJiri Pirko 	__be16 protocol = cls->common.protocol;
760f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
761f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
762f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
763f50f2127SFlorian Fainelli 	struct net_device *to_dev;
764f50f2127SFlorian Fainelli 	const struct tc_action *a;
765f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
766f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
767f50f2127SFlorian Fainelli 	int ifindex;
768f50f2127SFlorian Fainelli 
769f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
770f50f2127SFlorian Fainelli 		return err;
771f50f2127SFlorian Fainelli 
7723bcc0cecSJiri Pirko 	if (!tcf_exts_has_one_action(cls->exts))
773f50f2127SFlorian Fainelli 		return err;
774f50f2127SFlorian Fainelli 
775f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
776f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
777f50f2127SFlorian Fainelli 
778f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
779f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
780f50f2127SFlorian Fainelli 
781f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
782f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
783f50f2127SFlorian Fainelli 		if (!to_dev)
784f50f2127SFlorian Fainelli 			return -EINVAL;
785f50f2127SFlorian Fainelli 
786f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
787f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
788f50f2127SFlorian Fainelli 
789f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
790f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
791f50f2127SFlorian Fainelli 			return -ENOMEM;
792f50f2127SFlorian Fainelli 
793f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
794f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
795f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
796f50f2127SFlorian Fainelli 
797f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
798f50f2127SFlorian Fainelli 
799f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
800f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
801f50f2127SFlorian Fainelli 
802f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
803f50f2127SFlorian Fainelli 					       ingress);
804f50f2127SFlorian Fainelli 		if (err) {
805f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
806f50f2127SFlorian Fainelli 			return err;
807f50f2127SFlorian Fainelli 		}
808f50f2127SFlorian Fainelli 
809f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
810f50f2127SFlorian Fainelli 	}
811f50f2127SFlorian Fainelli 
812f50f2127SFlorian Fainelli 	return 0;
813f50f2127SFlorian Fainelli }
814f50f2127SFlorian Fainelli 
815f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
816f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
817f50f2127SFlorian Fainelli {
818f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
819f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
820f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
821f50f2127SFlorian Fainelli 
822f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
823f50f2127SFlorian Fainelli 		return;
824f50f2127SFlorian Fainelli 
825*4fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
826f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
827f50f2127SFlorian Fainelli 		return;
828f50f2127SFlorian Fainelli 
829f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
830f50f2127SFlorian Fainelli 
831f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
832f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
833f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
834f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
835f50f2127SFlorian Fainelli 		break;
836f50f2127SFlorian Fainelli 	default:
837f50f2127SFlorian Fainelli 		WARN_ON(1);
838f50f2127SFlorian Fainelli 	}
839f50f2127SFlorian Fainelli 
840f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
841f50f2127SFlorian Fainelli }
842f50f2127SFlorian Fainelli 
8433fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
8443fbae382SJiri Pirko 					   struct tc_cls_matchall_offload *cls)
845f50f2127SFlorian Fainelli {
846a2e8da93SJiri Pirko 	bool ingress;
847a2e8da93SJiri Pirko 
848a2e8da93SJiri Pirko 	if (is_classid_clsact_ingress(cls->common.classid))
849a2e8da93SJiri Pirko 		ingress = true;
850a2e8da93SJiri Pirko 	else if (is_classid_clsact_egress(cls->common.classid))
851a2e8da93SJiri Pirko 		ingress = false;
852a2e8da93SJiri Pirko 	else
853a2e8da93SJiri Pirko 		return -EOPNOTSUPP;
854a5fcf8a6SJiri Pirko 
8555fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
856a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
857f50f2127SFlorian Fainelli 
8583fbae382SJiri Pirko 	switch (cls->command) {
8593fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
8605fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
8613fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
8623fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
8633fbae382SJiri Pirko 		return 0;
8643fbae382SJiri Pirko 	default:
8653fbae382SJiri Pirko 		return -EOPNOTSUPP;
8663fbae382SJiri Pirko 	}
8673fbae382SJiri Pirko }
8683fbae382SJiri Pirko 
8693fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
870de4784caSJiri Pirko 			      void *type_data)
8713fbae382SJiri Pirko {
8722572ac53SJiri Pirko 	switch (type) {
873ade9b658SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
874de4784caSJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data);
875f50f2127SFlorian Fainelli 	default:
876a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
877f50f2127SFlorian Fainelli 	}
878f50f2127SFlorian Fainelli }
879f50f2127SFlorian Fainelli 
880f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
881f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
882f613ed66SFlorian Fainelli {
883f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
8845f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
885f613ed66SFlorian Fainelli 	unsigned int start;
8865f6b4e14SFlorian Fainelli 	int i;
887f613ed66SFlorian Fainelli 
888f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
8895f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
8905f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
8915f6b4e14SFlorian Fainelli 
8925f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
893f613ed66SFlorian Fainelli 		do {
8945f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
8955f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
8965f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
8975f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
8985f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
8995f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9005f6b4e14SFlorian Fainelli 
9015f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
9025f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
9035f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
9045f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
9055f6b4e14SFlorian Fainelli 	}
906f613ed66SFlorian Fainelli }
907f613ed66SFlorian Fainelli 
908bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
909bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
910bf9f2648SFlorian Fainelli {
911bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
912bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
913bf9f2648SFlorian Fainelli 
914bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
915bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
916bf9f2648SFlorian Fainelli 
917bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
918bf9f2648SFlorian Fainelli }
919bf9f2648SFlorian Fainelli 
920bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
921bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
922bf9f2648SFlorian Fainelli {
923bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
924bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
925bf9f2648SFlorian Fainelli 
926bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
927bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
928bf9f2648SFlorian Fainelli 
929bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
930bf9f2648SFlorian Fainelli }
931bf9f2648SFlorian Fainelli 
93291da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
93391da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
9343d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
9353d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
93691da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
93791da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
9386793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
9396793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
9406793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
94191da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
94291da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
94391da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
94419e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
94519e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
9467905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
9477905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
948bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
949bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
950bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
951bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
95291da11f8SLennert Buytenhek };
95391da11f8SLennert Buytenhek 
9543e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
955d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
956d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
9573e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
958d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
959d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
960d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
96137b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
96237b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
9632bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
964d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
965abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
96604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
96704ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
96804ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
96904ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
97004ff53f9SFlorian Fainelli #endif
97144bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
972f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
973f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
97498237d43SScott Feldman };
97598237d43SScott Feldman 
9769d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
977f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
97835636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
979ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
980ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
981d442ad4aSStephen Hemminger };
98291da11f8SLennert Buytenhek 
983f37db85dSFlorian Fainelli static struct device_type dsa_type = {
984f37db85dSFlorian Fainelli 	.name	= "dsa",
985f37db85dSFlorian Fainelli };
986f37db85dSFlorian Fainelli 
9870d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
9880d8bcdd3SFlorian Fainelli {
9890d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
990afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9910d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
9920d8bcdd3SFlorian Fainelli 
9930d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
9940d8bcdd3SFlorian Fainelli 		status_changed = 1;
9950d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
9960d8bcdd3SFlorian Fainelli 	}
9970d8bcdd3SFlorian Fainelli 
9980d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
9990d8bcdd3SFlorian Fainelli 		status_changed = 1;
10000d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
10010d8bcdd3SFlorian Fainelli 	}
10020d8bcdd3SFlorian Fainelli 
10030d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
10040d8bcdd3SFlorian Fainelli 		status_changed = 1;
10050d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
10060d8bcdd3SFlorian Fainelli 	}
10070d8bcdd3SFlorian Fainelli 
10089d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1009afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1010ec9436baSFlorian Fainelli 
10110d8bcdd3SFlorian Fainelli 	if (status_changed)
10120d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
10130d8bcdd3SFlorian Fainelli }
10140d8bcdd3SFlorian Fainelli 
1015ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1016ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1017ce31b31cSFlorian Fainelli {
1018b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1019b71be352SAndrew Lunn 	struct dsa_switch *ds;
1020ce31b31cSFlorian Fainelli 
1021b71be352SAndrew Lunn 	if (dev) {
1022b71be352SAndrew Lunn 		p = netdev_priv(dev);
1023afdcf151SVivien Didelot 		ds = p->dp->ds;
10249d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1025afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1026b71be352SAndrew Lunn 	}
1027ce31b31cSFlorian Fainelli 
1028ce31b31cSFlorian Fainelli 	return 0;
1029ce31b31cSFlorian Fainelli }
1030ce31b31cSFlorian Fainelli 
103191da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1032*4fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1033c305c165SFlorian Fainelli {
1034*4fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1035afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1036c305c165SFlorian Fainelli 
10377f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1038d25b8e74SRussell King 	if (!p->phy) {
1039d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1040c305c165SFlorian Fainelli 		return -ENODEV;
1041d25b8e74SRussell King 	}
1042c305c165SFlorian Fainelli 
1043c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1044211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1045c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
10464078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1047c305c165SFlorian Fainelli 				  p->phy_interface);
1048c305c165SFlorian Fainelli }
1049c305c165SFlorian Fainelli 
1050*4fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
10510d8bcdd3SFlorian Fainelli {
1052*4fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1053afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
10540d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1055ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
10566819563eSFlorian Fainelli 	u32 phy_flags = 0;
105719334920SGuenter Roeck 	int mode, ret;
10580d8bcdd3SFlorian Fainelli 
1059afdcf151SVivien Didelot 	port_dn = p->dp->dn;
106019334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
106119334920SGuenter Roeck 	if (mode < 0)
106219334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
106319334920SGuenter Roeck 	p->phy_interface = mode;
10640d8bcdd3SFlorian Fainelli 
10650d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
10660d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
10670d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
10680d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
10690d8bcdd3SFlorian Fainelli 		 */
10700d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
10710d8bcdd3SFlorian Fainelli 		if (ret) {
1072d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
10739697f1cdSFlorian Fainelli 			return ret;
10740d8bcdd3SFlorian Fainelli 		}
1075ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
10760d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
10770d8bcdd3SFlorian Fainelli 	}
10780d8bcdd3SFlorian Fainelli 
10799d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1080afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
10816819563eSFlorian Fainelli 
1082cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1083d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1084d25b8e74SRussell King 
1085cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1086cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1087cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1088cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1089cd28a1a9SFlorian Fainelli 		 */
1090d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1091d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1092*4fa7b718SVivien Didelot 			ret = dsa_slave_phy_connect(slave_dev, phy_id);
1093d25b8e74SRussell King 			if (ret) {
1094d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
10950d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1096cd28a1a9SFlorian Fainelli 				return ret;
1097d25b8e74SRussell King 			}
1098cd28a1a9SFlorian Fainelli 		} else {
10990d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1100cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1101cd28a1a9SFlorian Fainelli 						phy_flags,
11020d8bcdd3SFlorian Fainelli 						p->phy_interface);
1103cd28a1a9SFlorian Fainelli 		}
11040d8f3c67SJohan Hovold 
11050d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1106cd28a1a9SFlorian Fainelli 	}
11070d8bcdd3SFlorian Fainelli 
1108ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1109ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1110ce31b31cSFlorian Fainelli 
11110d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
11120d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
11130d8bcdd3SFlorian Fainelli 	 */
1114b31f65fbSAndrew Lunn 	if (!p->phy) {
1115*4fa7b718SVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, p->dp->index);
1116d25b8e74SRussell King 		if (ret) {
1117afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1118afdcf151SVivien Didelot 				   p->dp->index, ret);
1119881eadabSJohan Hovold 			if (phy_is_fixed)
1120881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1121c305c165SFlorian Fainelli 			return ret;
1122d25b8e74SRussell King 		}
11230d8bcdd3SFlorian Fainelli 	}
11249697f1cdSFlorian Fainelli 
11252220943aSAndrew Lunn 	phy_attached_info(p->phy);
11262220943aSAndrew Lunn 
11279697f1cdSFlorian Fainelli 	return 0;
1128b31f65fbSAndrew Lunn }
11290d8bcdd3SFlorian Fainelli 
1130448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1131448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1132448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1133448b4482SAndrew Lunn 					    void *_unused)
1134448b4482SAndrew Lunn {
1135448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1136448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1137448b4482SAndrew Lunn }
1138448b4482SAndrew Lunn 
113924462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
114024462549SFlorian Fainelli {
114124462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
114224462549SFlorian Fainelli 
1143f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1144f154be24SFlorian Fainelli 
114524462549SFlorian Fainelli 	if (p->phy) {
114624462549SFlorian Fainelli 		phy_stop(p->phy);
114724462549SFlorian Fainelli 		p->old_pause = -1;
114824462549SFlorian Fainelli 		p->old_link = -1;
114924462549SFlorian Fainelli 		p->old_duplex = -1;
115024462549SFlorian Fainelli 		phy_suspend(p->phy);
115124462549SFlorian Fainelli 	}
115224462549SFlorian Fainelli 
115324462549SFlorian Fainelli 	return 0;
115424462549SFlorian Fainelli }
115524462549SFlorian Fainelli 
115624462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
115724462549SFlorian Fainelli {
115824462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
115924462549SFlorian Fainelli 
116024462549SFlorian Fainelli 	netif_device_attach(slave_dev);
116124462549SFlorian Fainelli 
116224462549SFlorian Fainelli 	if (p->phy) {
116324462549SFlorian Fainelli 		phy_resume(p->phy);
116424462549SFlorian Fainelli 		phy_start(p->phy);
116524462549SFlorian Fainelli 	}
116624462549SFlorian Fainelli 
116724462549SFlorian Fainelli 	return 0;
116824462549SFlorian Fainelli }
116924462549SFlorian Fainelli 
11704cfbf09cSVivien Didelot int dsa_slave_create(struct dsa_port *port, const char *name)
117191da11f8SLennert Buytenhek {
11724cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1173badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
117483c0afaeSAndrew Lunn 	struct net_device *master;
117591da11f8SLennert Buytenhek 	struct net_device *slave_dev;
117691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
1177a29342e7SFlorian Fainelli 	struct dsa_port *cpu_dp;
117891da11f8SLennert Buytenhek 	int ret;
117991da11f8SLennert Buytenhek 
1180a29342e7SFlorian Fainelli 	cpu_dp = ds->dst->cpu_dp;
1181a29342e7SFlorian Fainelli 	master = cpu_dp->netdev;
118283c0afaeSAndrew Lunn 
118355199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
118455199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
118555199df6SFlorian Fainelli 
118655199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
118755199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
118855199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
118991da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1190d87d6f44SGuenter Roeck 		return -ENOMEM;
119191da11f8SLennert Buytenhek 
1192f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1193f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
11947ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
11952fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
11960a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
11973e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
11989d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
11998b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
12008b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1201f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1202d442ad4aSStephen Hemminger 
1203448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1204448b4482SAndrew Lunn 				 NULL);
1205448b4482SAndrew Lunn 
12064cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
12074cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
120891da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
120991da11f8SLennert Buytenhek 
121091da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
12115f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
12125f6b4e14SFlorian Fainelli 	if (!p->stats64) {
12135f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
12145f6b4e14SFlorian Fainelli 		return -ENOMEM;
12155f6b4e14SFlorian Fainelli 	}
12164cfbf09cSVivien Didelot 	p->dp = port;
1217f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
121839a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
12195075314eSAlexander Duyck 
12200d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
12210d8bcdd3SFlorian Fainelli 	p->old_link = -1;
12220d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
12230d8bcdd3SFlorian Fainelli 
12244cfbf09cSVivien Didelot 	port->netdev = slave_dev;
122591da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
122691da11f8SLennert Buytenhek 	if (ret) {
1227a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1228a2ae6007SJoe Perches 			   ret, slave_dev->name);
12294cfbf09cSVivien Didelot 		port->netdev = NULL;
12305f6b4e14SFlorian Fainelli 		free_percpu(p->stats64);
123191da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1232d87d6f44SGuenter Roeck 		return ret;
123391da11f8SLennert Buytenhek 	}
123491da11f8SLennert Buytenhek 
123591da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
123691da11f8SLennert Buytenhek 
1237*4fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
12380071f56eSAndrew Lunn 	if (ret) {
12390071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
124073dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
12415f6b4e14SFlorian Fainelli 		free_percpu(p->stats64);
12420071f56eSAndrew Lunn 		free_netdev(slave_dev);
12430071f56eSAndrew Lunn 		return ret;
12440071f56eSAndrew Lunn 	}
12450071f56eSAndrew Lunn 
1246d87d6f44SGuenter Roeck 	return 0;
124791da11f8SLennert Buytenhek }
1248b73adef6SFlorian Fainelli 
1249cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1250cda5c15bSNeil Armstrong {
1251cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1252881eadabSJohan Hovold 	struct device_node *port_dn;
1253881eadabSJohan Hovold 
1254afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1255cda5c15bSNeil Armstrong 
1256cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1257881eadabSJohan Hovold 	if (p->phy) {
1258cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1259881eadabSJohan Hovold 
1260881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1261881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1262881eadabSJohan Hovold 	}
1263cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
12645f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1265cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1266cda5c15bSNeil Armstrong }
1267cda5c15bSNeil Armstrong 
1268b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1269b73adef6SFlorian Fainelli {
1270b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1271b73adef6SFlorian Fainelli }
1272b73adef6SFlorian Fainelli 
12738e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
12748e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1275b73adef6SFlorian Fainelli {
127617d7802bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
127717d7802bSVivien Didelot 	struct dsa_port *dp = p->dp;
12788e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1279b73adef6SFlorian Fainelli 
12808e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
12818e92ab3aSVivien Didelot 		if (info->linking) {
128217d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
12838e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
12848e92ab3aSVivien Didelot 		} else {
128517d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
12868e92ab3aSVivien Didelot 			err = NOTIFY_OK;
12878e92ab3aSVivien Didelot 		}
12886debb68aSVivien Didelot 	}
1289b73adef6SFlorian Fainelli 
12908e92ab3aSVivien Didelot 	return err;
1291b73adef6SFlorian Fainelli }
1292b73adef6SFlorian Fainelli 
129388e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1294b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1295b73adef6SFlorian Fainelli {
12966debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1297b73adef6SFlorian Fainelli 
129853bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
12998e92ab3aSVivien Didelot 		return NOTIFY_DONE;
13008e92ab3aSVivien Didelot 
13018e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
13028e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1303b73adef6SFlorian Fainelli 
1304b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1305b73adef6SFlorian Fainelli }
130688e4f0caSVivien Didelot 
1307c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1308c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1309c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1310c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1311c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1312c9eb3e0fSArkadi Sharshevsky };
1313c9eb3e0fSArkadi Sharshevsky 
1314c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1315c9eb3e0fSArkadi Sharshevsky {
1316c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1317c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1318c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1319c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1320c9eb3e0fSArkadi Sharshevsky 	struct dsa_slave_priv *p = netdev_priv(dev);
1321c9eb3e0fSArkadi Sharshevsky 	int err;
1322c9eb3e0fSArkadi Sharshevsky 
1323c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1324c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1325c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1326c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1327c9eb3e0fSArkadi Sharshevsky 		err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid);
1328c9eb3e0fSArkadi Sharshevsky 		if (err) {
1329c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1330c9eb3e0fSArkadi Sharshevsky 			break;
1331c9eb3e0fSArkadi Sharshevsky 		}
1332c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1333c9eb3e0fSArkadi Sharshevsky 					 &fdb_info->info);
1334c9eb3e0fSArkadi Sharshevsky 		break;
1335c9eb3e0fSArkadi Sharshevsky 
1336c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1337c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1338c9eb3e0fSArkadi Sharshevsky 		err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid);
1339c9eb3e0fSArkadi Sharshevsky 		if (err) {
1340c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1341c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1342c9eb3e0fSArkadi Sharshevsky 		}
1343c9eb3e0fSArkadi Sharshevsky 		break;
1344c9eb3e0fSArkadi Sharshevsky 	}
1345c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1346c9eb3e0fSArkadi Sharshevsky 
1347c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1348c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1349c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1350c9eb3e0fSArkadi Sharshevsky }
1351c9eb3e0fSArkadi Sharshevsky 
1352c9eb3e0fSArkadi Sharshevsky static int
1353c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1354c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1355c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1356c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1357c9eb3e0fSArkadi Sharshevsky {
1358c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1359c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1360c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1361c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1362c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1363c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1364c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1365c9eb3e0fSArkadi Sharshevsky 	return 0;
1366c9eb3e0fSArkadi Sharshevsky }
1367c9eb3e0fSArkadi Sharshevsky 
1368c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1369c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
1370c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
1371c9eb3e0fSArkadi Sharshevsky {
1372c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1373c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
1374c9eb3e0fSArkadi Sharshevsky 
1375c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
1376c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1377c9eb3e0fSArkadi Sharshevsky 
1378c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1379c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
1380c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
1381c9eb3e0fSArkadi Sharshevsky 
1382c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
1383c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
1384c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
1385c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
1386c9eb3e0fSArkadi Sharshevsky 
1387c9eb3e0fSArkadi Sharshevsky 	switch (event) {
1388c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1389c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1390c9eb3e0fSArkadi Sharshevsky 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1391c9eb3e0fSArkadi Sharshevsky 						      ptr))
1392c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
1393c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
1394c9eb3e0fSArkadi Sharshevsky 		break;
1395c9eb3e0fSArkadi Sharshevsky 	default:
1396c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
1397c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1398c9eb3e0fSArkadi Sharshevsky 	}
1399c9eb3e0fSArkadi Sharshevsky 
1400c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
1401c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
1402c9eb3e0fSArkadi Sharshevsky 
1403c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
1404c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1405c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
1406c9eb3e0fSArkadi Sharshevsky }
1407c9eb3e0fSArkadi Sharshevsky 
140888e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
140988e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
141088e4f0caSVivien Didelot };
141188e4f0caSVivien Didelot 
1412c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
1413c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
1414c9eb3e0fSArkadi Sharshevsky };
1415c9eb3e0fSArkadi Sharshevsky 
141688e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
141788e4f0caSVivien Didelot {
1418c9eb3e0fSArkadi Sharshevsky 	int err;
1419c9eb3e0fSArkadi Sharshevsky 
1420c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
1421c9eb3e0fSArkadi Sharshevsky 	if (err)
1422c9eb3e0fSArkadi Sharshevsky 		return err;
1423c9eb3e0fSArkadi Sharshevsky 
1424c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1425c9eb3e0fSArkadi Sharshevsky 	if (err)
1426c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
1427c9eb3e0fSArkadi Sharshevsky 
1428c9eb3e0fSArkadi Sharshevsky 	return 0;
1429c9eb3e0fSArkadi Sharshevsky 
1430c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
1431c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
1432c9eb3e0fSArkadi Sharshevsky 	return err;
143388e4f0caSVivien Didelot }
143488e4f0caSVivien Didelot 
143588e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
143688e4f0caSVivien Didelot {
143788e4f0caSVivien Didelot 	int err;
143888e4f0caSVivien Didelot 
1439c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1440c9eb3e0fSArkadi Sharshevsky 	if (err)
1441c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1442c9eb3e0fSArkadi Sharshevsky 
144388e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
144488e4f0caSVivien Didelot 	if (err)
144588e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
144688e4f0caSVivien Didelot }
1447