xref: /openbmc/linux/net/dsa/slave.c (revision 339d82626d225e9b876665e4e89b7eb123e96b3d)
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>
18b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
1998237d43SScott Feldman #include <net/switchdev.h>
20b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2191da11f8SLennert Buytenhek #include "dsa_priv.h"
2291da11f8SLennert Buytenhek 
2391da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2491da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2591da11f8SLennert Buytenhek {
2691da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2791da11f8SLennert Buytenhek 
280d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
2991da11f8SLennert Buytenhek 		return ds->drv->phy_read(ds, addr, reg);
3091da11f8SLennert Buytenhek 
3191da11f8SLennert Buytenhek 	return 0xffff;
3291da11f8SLennert Buytenhek }
3391da11f8SLennert Buytenhek 
3491da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3591da11f8SLennert Buytenhek {
3691da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3791da11f8SLennert Buytenhek 
380d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
3991da11f8SLennert Buytenhek 		return ds->drv->phy_write(ds, addr, reg, val);
4091da11f8SLennert Buytenhek 
4191da11f8SLennert Buytenhek 	return 0;
4291da11f8SLennert Buytenhek }
4391da11f8SLennert Buytenhek 
4491da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4591da11f8SLennert Buytenhek {
4691da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4791da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
4891da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
50f490be04SFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
51f490be04SFlorian Fainelli 			ds->index, ds->pd->sw_addr);
52b4d2394dSAlexander Duyck 	ds->slave_mii_bus->parent = ds->master_dev;
5324df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5491da11f8SLennert Buytenhek }
5591da11f8SLennert Buytenhek 
5691da11f8SLennert Buytenhek 
5791da11f8SLennert Buytenhek /* slave device handling ****************************************************/
58c0840801SLennert Buytenhek static int dsa_slave_init(struct net_device *dev)
59c0840801SLennert Buytenhek {
60c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
61c0840801SLennert Buytenhek 
62e84665c9SLennert Buytenhek 	dev->iflink = p->parent->dst->master_netdev->ifindex;
63c0840801SLennert Buytenhek 
64c0840801SLennert Buytenhek 	return 0;
65c0840801SLennert Buytenhek }
66c0840801SLennert Buytenhek 
67b73adef6SFlorian Fainelli static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
68b73adef6SFlorian Fainelli {
69b73adef6SFlorian Fainelli 	return !!p->bridge_dev;
70b73adef6SFlorian Fainelli }
71b73adef6SFlorian Fainelli 
7291da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
7391da11f8SLennert Buytenhek {
74df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
75e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
76b2f2af21SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
77b73adef6SFlorian Fainelli 	u8 stp_state = dsa_port_is_bridged(p) ?
78b73adef6SFlorian Fainelli 			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 
101b2f2af21SFlorian Fainelli 	if (ds->drv->port_enable) {
102b2f2af21SFlorian Fainelli 		err = ds->drv->port_enable(ds, p->port, p->phy);
103b2f2af21SFlorian Fainelli 		if (err)
104b2f2af21SFlorian Fainelli 			goto clear_promisc;
105b2f2af21SFlorian Fainelli 	}
106b2f2af21SFlorian Fainelli 
107b73adef6SFlorian Fainelli 	if (ds->drv->port_stp_update)
108b73adef6SFlorian Fainelli 		ds->drv->port_stp_update(ds, p->port, stp_state);
109b73adef6SFlorian Fainelli 
110f7f1de51SFlorian Fainelli 	if (p->phy)
111f7f1de51SFlorian Fainelli 		phy_start(p->phy);
112f7f1de51SFlorian Fainelli 
11391da11f8SLennert Buytenhek 	return 0;
114df02c6ffSLennert Buytenhek 
115b2f2af21SFlorian Fainelli clear_promisc:
116b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
117b2f2af21SFlorian Fainelli 		dev_set_promiscuity(master, 0);
118df02c6ffSLennert Buytenhek clear_allmulti:
119df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
120df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
121df02c6ffSLennert Buytenhek del_unicast:
1228feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
123a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
124df02c6ffSLennert Buytenhek out:
125df02c6ffSLennert Buytenhek 	return err;
12691da11f8SLennert Buytenhek }
12791da11f8SLennert Buytenhek 
12891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
12991da11f8SLennert Buytenhek {
130df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
131e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
132b2f2af21SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
133df02c6ffSLennert Buytenhek 
134f7f1de51SFlorian Fainelli 	if (p->phy)
135f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
136f7f1de51SFlorian Fainelli 
137df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
138a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
139df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
140df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
141df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
142df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
143df02c6ffSLennert Buytenhek 
1448feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
145a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
146df02c6ffSLennert Buytenhek 
147b2f2af21SFlorian Fainelli 	if (ds->drv->port_disable)
148b2f2af21SFlorian Fainelli 		ds->drv->port_disable(ds, p->port, p->phy);
149b2f2af21SFlorian Fainelli 
150b73adef6SFlorian Fainelli 	if (ds->drv->port_stp_update)
151b73adef6SFlorian Fainelli 		ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
152b73adef6SFlorian Fainelli 
15391da11f8SLennert Buytenhek 	return 0;
15491da11f8SLennert Buytenhek }
15591da11f8SLennert Buytenhek 
15691da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
15791da11f8SLennert Buytenhek {
15891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
159e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
16091da11f8SLennert Buytenhek 
16191da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
16291da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
16391da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
16491da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
16591da11f8SLennert Buytenhek }
16691da11f8SLennert Buytenhek 
16791da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
16891da11f8SLennert Buytenhek {
16991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
170e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
17191da11f8SLennert Buytenhek 
17291da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
173a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
17491da11f8SLennert Buytenhek }
17591da11f8SLennert Buytenhek 
176df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
17791da11f8SLennert Buytenhek {
178df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
179e84665c9SLennert Buytenhek 	struct net_device *master = p->parent->dst->master_netdev;
180df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
181df02c6ffSLennert Buytenhek 	int err;
182df02c6ffSLennert Buytenhek 
183df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
184df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
185df02c6ffSLennert Buytenhek 
186df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
187df02c6ffSLennert Buytenhek 		goto out;
188df02c6ffSLennert Buytenhek 
1898feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
190a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
191df02c6ffSLennert Buytenhek 		if (err < 0)
192df02c6ffSLennert Buytenhek 			return err;
193df02c6ffSLennert Buytenhek 	}
194df02c6ffSLennert Buytenhek 
1958feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
196a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
197df02c6ffSLennert Buytenhek 
198df02c6ffSLennert Buytenhek out:
199d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
20091da11f8SLennert Buytenhek 
20191da11f8SLennert Buytenhek 	return 0;
20291da11f8SLennert Buytenhek }
20391da11f8SLennert Buytenhek 
204*339d8262SGuenter Roeck static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
205*339d8262SGuenter Roeck 			     struct net_device *dev,
206*339d8262SGuenter Roeck 			     const unsigned char *addr, u16 vid, u16 nlm_flags)
207*339d8262SGuenter Roeck {
208*339d8262SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
209*339d8262SGuenter Roeck 	struct dsa_switch *ds = p->parent;
210*339d8262SGuenter Roeck 	int ret = -EOPNOTSUPP;
211*339d8262SGuenter Roeck 
212*339d8262SGuenter Roeck 	if (ds->drv->fdb_add)
213*339d8262SGuenter Roeck 		ret = ds->drv->fdb_add(ds, p->port, addr, vid);
214*339d8262SGuenter Roeck 
215*339d8262SGuenter Roeck 	return ret;
216*339d8262SGuenter Roeck }
217*339d8262SGuenter Roeck 
218*339d8262SGuenter Roeck static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
219*339d8262SGuenter Roeck 			     struct net_device *dev,
220*339d8262SGuenter Roeck 			     const unsigned char *addr, u16 vid)
221*339d8262SGuenter Roeck {
222*339d8262SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
223*339d8262SGuenter Roeck 	struct dsa_switch *ds = p->parent;
224*339d8262SGuenter Roeck 	int ret = -EOPNOTSUPP;
225*339d8262SGuenter Roeck 
226*339d8262SGuenter Roeck 	if (ds->drv->fdb_del)
227*339d8262SGuenter Roeck 		ret = ds->drv->fdb_del(ds, p->port, addr, vid);
228*339d8262SGuenter Roeck 
229*339d8262SGuenter Roeck 	return ret;
230*339d8262SGuenter Roeck }
231*339d8262SGuenter Roeck 
232*339d8262SGuenter Roeck static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
233*339d8262SGuenter Roeck 			       const unsigned char *addr, u16 vid,
234*339d8262SGuenter Roeck 			       bool is_static,
235*339d8262SGuenter Roeck 			       u32 portid, u32 seq, int type,
236*339d8262SGuenter Roeck 			       unsigned int flags)
237*339d8262SGuenter Roeck {
238*339d8262SGuenter Roeck 	struct nlmsghdr *nlh;
239*339d8262SGuenter Roeck 	struct ndmsg *ndm;
240*339d8262SGuenter Roeck 
241*339d8262SGuenter Roeck 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
242*339d8262SGuenter Roeck 	if (!nlh)
243*339d8262SGuenter Roeck 		return -EMSGSIZE;
244*339d8262SGuenter Roeck 
245*339d8262SGuenter Roeck 	ndm = nlmsg_data(nlh);
246*339d8262SGuenter Roeck 	ndm->ndm_family	 = AF_BRIDGE;
247*339d8262SGuenter Roeck 	ndm->ndm_pad1    = 0;
248*339d8262SGuenter Roeck 	ndm->ndm_pad2    = 0;
249*339d8262SGuenter Roeck 	ndm->ndm_flags	 = NTF_EXT_LEARNED;
250*339d8262SGuenter Roeck 	ndm->ndm_type	 = 0;
251*339d8262SGuenter Roeck 	ndm->ndm_ifindex = dev->ifindex;
252*339d8262SGuenter Roeck 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
253*339d8262SGuenter Roeck 
254*339d8262SGuenter Roeck 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
255*339d8262SGuenter Roeck 		goto nla_put_failure;
256*339d8262SGuenter Roeck 
257*339d8262SGuenter Roeck 	if (vid && nla_put_u16(skb, NDA_VLAN, vid))
258*339d8262SGuenter Roeck 		goto nla_put_failure;
259*339d8262SGuenter Roeck 
260*339d8262SGuenter Roeck 	nlmsg_end(skb, nlh);
261*339d8262SGuenter Roeck 	return 0;
262*339d8262SGuenter Roeck 
263*339d8262SGuenter Roeck nla_put_failure:
264*339d8262SGuenter Roeck 	nlmsg_cancel(skb, nlh);
265*339d8262SGuenter Roeck 	return -EMSGSIZE;
266*339d8262SGuenter Roeck }
267*339d8262SGuenter Roeck 
268*339d8262SGuenter Roeck /* Dump information about entries, in response to GETNEIGH */
269*339d8262SGuenter Roeck static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
270*339d8262SGuenter Roeck 			      struct net_device *dev,
271*339d8262SGuenter Roeck 			      struct net_device *filter_dev, int idx)
272*339d8262SGuenter Roeck {
273*339d8262SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
274*339d8262SGuenter Roeck 	struct dsa_switch *ds = p->parent;
275*339d8262SGuenter Roeck 	unsigned char addr[ETH_ALEN] = { 0 };
276*339d8262SGuenter Roeck 	int ret;
277*339d8262SGuenter Roeck 
278*339d8262SGuenter Roeck 	if (!ds->drv->fdb_getnext)
279*339d8262SGuenter Roeck 		return -EOPNOTSUPP;
280*339d8262SGuenter Roeck 
281*339d8262SGuenter Roeck 	for (; ; idx++) {
282*339d8262SGuenter Roeck 		bool is_static;
283*339d8262SGuenter Roeck 
284*339d8262SGuenter Roeck 		ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
285*339d8262SGuenter Roeck 		if (ret < 0)
286*339d8262SGuenter Roeck 			break;
287*339d8262SGuenter Roeck 
288*339d8262SGuenter Roeck 		if (idx < cb->args[0])
289*339d8262SGuenter Roeck 			continue;
290*339d8262SGuenter Roeck 
291*339d8262SGuenter Roeck 		ret = dsa_slave_fill_info(dev, skb, addr, 0,
292*339d8262SGuenter Roeck 					  is_static,
293*339d8262SGuenter Roeck 					  NETLINK_CB(cb->skb).portid,
294*339d8262SGuenter Roeck 					  cb->nlh->nlmsg_seq,
295*339d8262SGuenter Roeck 					  RTM_NEWNEIGH, NLM_F_MULTI);
296*339d8262SGuenter Roeck 		if (ret < 0)
297*339d8262SGuenter Roeck 			break;
298*339d8262SGuenter Roeck 	}
299*339d8262SGuenter Roeck 
300*339d8262SGuenter Roeck 	return idx;
301*339d8262SGuenter Roeck }
302*339d8262SGuenter Roeck 
30391da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
30491da11f8SLennert Buytenhek {
30591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
30691da11f8SLennert Buytenhek 
30791da11f8SLennert Buytenhek 	if (p->phy != NULL)
30828b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
30991da11f8SLennert Buytenhek 
31091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
31191da11f8SLennert Buytenhek }
31291da11f8SLennert Buytenhek 
313b73adef6SFlorian Fainelli /* Return a bitmask of all ports being currently bridged within a given bridge
314b73adef6SFlorian Fainelli  * device. Note that on leave, the mask will still return the bitmask of ports
315b73adef6SFlorian Fainelli  * currently bridged, prior to port removal, and this is exactly what we want.
316b73adef6SFlorian Fainelli  */
317b73adef6SFlorian Fainelli static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
318b73adef6SFlorian Fainelli 				  struct net_device *bridge)
319b73adef6SFlorian Fainelli {
320b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p;
321b73adef6SFlorian Fainelli 	unsigned int port;
322b73adef6SFlorian Fainelli 	u32 mask = 0;
323b73adef6SFlorian Fainelli 
324b73adef6SFlorian Fainelli 	for (port = 0; port < DSA_MAX_PORTS; port++) {
325d79d2107SGuenter Roeck 		if (!dsa_is_port_initialized(ds, port))
326b73adef6SFlorian Fainelli 			continue;
327b73adef6SFlorian Fainelli 
328b73adef6SFlorian Fainelli 		p = netdev_priv(ds->ports[port]);
329b73adef6SFlorian Fainelli 
330b73adef6SFlorian Fainelli 		if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
331b73adef6SFlorian Fainelli 		    p->bridge_dev == bridge)
332b73adef6SFlorian Fainelli 			mask |= 1 << port;
333b73adef6SFlorian Fainelli 	}
334b73adef6SFlorian Fainelli 
335b73adef6SFlorian Fainelli 	return mask;
336b73adef6SFlorian Fainelli }
337b73adef6SFlorian Fainelli 
338b73adef6SFlorian Fainelli static int dsa_slave_stp_update(struct net_device *dev, u8 state)
339b73adef6SFlorian Fainelli {
340b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
341b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
342b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
343b73adef6SFlorian Fainelli 
344b73adef6SFlorian Fainelli 	if (ds->drv->port_stp_update)
345b73adef6SFlorian Fainelli 		ret = ds->drv->port_stp_update(ds, p->port, state);
346b73adef6SFlorian Fainelli 
347b73adef6SFlorian Fainelli 	return ret;
348b73adef6SFlorian Fainelli }
349b73adef6SFlorian Fainelli 
350b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
351b73adef6SFlorian Fainelli 				      struct net_device *br)
352b73adef6SFlorian Fainelli {
353b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
354b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
355b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
356b73adef6SFlorian Fainelli 
357b73adef6SFlorian Fainelli 	p->bridge_dev = br;
358b73adef6SFlorian Fainelli 
359b73adef6SFlorian Fainelli 	if (ds->drv->port_join_bridge)
360b73adef6SFlorian Fainelli 		ret = ds->drv->port_join_bridge(ds, p->port,
361b73adef6SFlorian Fainelli 						dsa_slave_br_port_mask(ds, br));
362b73adef6SFlorian Fainelli 
363b73adef6SFlorian Fainelli 	return ret;
364b73adef6SFlorian Fainelli }
365b73adef6SFlorian Fainelli 
366b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_leave(struct net_device *dev)
367b73adef6SFlorian Fainelli {
368b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
369b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
370b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
371b73adef6SFlorian Fainelli 
372b73adef6SFlorian Fainelli 
373b73adef6SFlorian Fainelli 	if (ds->drv->port_leave_bridge)
374b73adef6SFlorian Fainelli 		ret = ds->drv->port_leave_bridge(ds, p->port,
375b73adef6SFlorian Fainelli 						 dsa_slave_br_port_mask(ds, p->bridge_dev));
376b73adef6SFlorian Fainelli 
377b73adef6SFlorian Fainelli 	p->bridge_dev = NULL;
378b73adef6SFlorian Fainelli 
379b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
380b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
381b73adef6SFlorian Fainelli 	 */
382b73adef6SFlorian Fainelli 	dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
383b73adef6SFlorian Fainelli 
384b73adef6SFlorian Fainelli 	return ret;
385b73adef6SFlorian Fainelli }
386b73adef6SFlorian Fainelli 
387b73adef6SFlorian Fainelli static int dsa_slave_parent_id_get(struct net_device *dev,
388b73adef6SFlorian Fainelli 				   struct netdev_phys_item_id *psid)
389b73adef6SFlorian Fainelli {
390b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
391b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
392b73adef6SFlorian Fainelli 
393b73adef6SFlorian Fainelli 	psid->id_len = sizeof(ds->index);
394b73adef6SFlorian Fainelli 	memcpy(&psid->id, &ds->index, psid->id_len);
395b73adef6SFlorian Fainelli 
396b73adef6SFlorian Fainelli 	return 0;
397b73adef6SFlorian Fainelli }
398b73adef6SFlorian Fainelli 
3993e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
4003e8a72d1SFlorian Fainelli {
4013e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4023e8a72d1SFlorian Fainelli 
4035075314eSAlexander Duyck 	return p->xmit(skb, dev);
4043e8a72d1SFlorian Fainelli }
4053e8a72d1SFlorian Fainelli 
4065aed85ceSFlorian Fainelli static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
4075aed85ceSFlorian Fainelli 					struct net_device *dev)
4085aed85ceSFlorian Fainelli {
4095aed85ceSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4105aed85ceSFlorian Fainelli 
4115aed85ceSFlorian Fainelli 	skb->dev = p->parent->dst->master_netdev;
4125aed85ceSFlorian Fainelli 	dev_queue_xmit(skb);
4135aed85ceSFlorian Fainelli 
4145aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
4155aed85ceSFlorian Fainelli }
4165aed85ceSFlorian Fainelli 
41791da11f8SLennert Buytenhek 
41891da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
41991da11f8SLennert Buytenhek static int
42091da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
42191da11f8SLennert Buytenhek {
42291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
42391da11f8SLennert Buytenhek 	int err;
42491da11f8SLennert Buytenhek 
42591da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
42691da11f8SLennert Buytenhek 	if (p->phy != NULL) {
42791da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
42891da11f8SLennert Buytenhek 		if (err == 0)
42991da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
43091da11f8SLennert Buytenhek 	}
43191da11f8SLennert Buytenhek 
43291da11f8SLennert Buytenhek 	return err;
43391da11f8SLennert Buytenhek }
43491da11f8SLennert Buytenhek 
43591da11f8SLennert Buytenhek static int
43691da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
43791da11f8SLennert Buytenhek {
43891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
43991da11f8SLennert Buytenhek 
44091da11f8SLennert Buytenhek 	if (p->phy != NULL)
44191da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
44291da11f8SLennert Buytenhek 
44391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
44491da11f8SLennert Buytenhek }
44591da11f8SLennert Buytenhek 
44691da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
44791da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
44891da11f8SLennert Buytenhek {
4497826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
4507826d43fSJiri Pirko 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
4517826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
4527826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
45391da11f8SLennert Buytenhek }
45491da11f8SLennert Buytenhek 
4553d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
4563d762a0fSGuenter Roeck {
4573d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
4583d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
4593d762a0fSGuenter Roeck 
4603d762a0fSGuenter Roeck 	if (ds->drv->get_regs_len)
4613d762a0fSGuenter Roeck 		return ds->drv->get_regs_len(ds, p->port);
4623d762a0fSGuenter Roeck 
4633d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
4643d762a0fSGuenter Roeck }
4653d762a0fSGuenter Roeck 
4663d762a0fSGuenter Roeck static void
4673d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
4683d762a0fSGuenter Roeck {
4693d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
4703d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
4713d762a0fSGuenter Roeck 
4723d762a0fSGuenter Roeck 	if (ds->drv->get_regs)
4733d762a0fSGuenter Roeck 		ds->drv->get_regs(ds, p->port, regs, _p);
4743d762a0fSGuenter Roeck }
4753d762a0fSGuenter Roeck 
47691da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
47791da11f8SLennert Buytenhek {
47891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
47991da11f8SLennert Buytenhek 
48091da11f8SLennert Buytenhek 	if (p->phy != NULL)
48191da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
48291da11f8SLennert Buytenhek 
48391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
48491da11f8SLennert Buytenhek }
48591da11f8SLennert Buytenhek 
48691da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
48791da11f8SLennert Buytenhek {
48891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
48991da11f8SLennert Buytenhek 
49091da11f8SLennert Buytenhek 	if (p->phy != NULL) {
49191da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
49291da11f8SLennert Buytenhek 		return p->phy->link;
49391da11f8SLennert Buytenhek 	}
49491da11f8SLennert Buytenhek 
49591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
49691da11f8SLennert Buytenhek }
49791da11f8SLennert Buytenhek 
4986793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
4996793abb4SGuenter Roeck {
5006793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5016793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
5026793abb4SGuenter Roeck 
5036793abb4SGuenter Roeck 	if (ds->pd->eeprom_len)
5046793abb4SGuenter Roeck 		return ds->pd->eeprom_len;
5056793abb4SGuenter Roeck 
5066793abb4SGuenter Roeck 	if (ds->drv->get_eeprom_len)
5076793abb4SGuenter Roeck 		return ds->drv->get_eeprom_len(ds);
5086793abb4SGuenter Roeck 
5096793abb4SGuenter Roeck 	return 0;
5106793abb4SGuenter Roeck }
5116793abb4SGuenter Roeck 
5126793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5136793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5146793abb4SGuenter Roeck {
5156793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5166793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
5176793abb4SGuenter Roeck 
5186793abb4SGuenter Roeck 	if (ds->drv->get_eeprom)
5196793abb4SGuenter Roeck 		return ds->drv->get_eeprom(ds, eeprom, data);
5206793abb4SGuenter Roeck 
5216793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5226793abb4SGuenter Roeck }
5236793abb4SGuenter Roeck 
5246793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5256793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5266793abb4SGuenter Roeck {
5276793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5286793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
5296793abb4SGuenter Roeck 
5306793abb4SGuenter Roeck 	if (ds->drv->set_eeprom)
5316793abb4SGuenter Roeck 		return ds->drv->set_eeprom(ds, eeprom, data);
5326793abb4SGuenter Roeck 
5336793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5346793abb4SGuenter Roeck }
5356793abb4SGuenter Roeck 
53691da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
53791da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
53891da11f8SLennert Buytenhek {
53991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
54091da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
54191da11f8SLennert Buytenhek 
54291da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
54391da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
54491da11f8SLennert Buytenhek 
54591da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
54691da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
54791da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
54891da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
54991da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
55091da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
55191da11f8SLennert Buytenhek 	}
55291da11f8SLennert Buytenhek }
55391da11f8SLennert Buytenhek 
55491da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
55591da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
55691da11f8SLennert Buytenhek 					uint64_t *data)
55791da11f8SLennert Buytenhek {
55891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
55991da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
56091da11f8SLennert Buytenhek 
56191da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
56291da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
56391da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
56491da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
56591da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
56691da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
56791da11f8SLennert Buytenhek }
56891da11f8SLennert Buytenhek 
56991da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
57091da11f8SLennert Buytenhek {
57191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
57291da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
57391da11f8SLennert Buytenhek 
57491da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
57591da11f8SLennert Buytenhek 		int count;
57691da11f8SLennert Buytenhek 
57791da11f8SLennert Buytenhek 		count = 4;
57891da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
57991da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
58091da11f8SLennert Buytenhek 
58191da11f8SLennert Buytenhek 		return count;
58291da11f8SLennert Buytenhek 	}
58391da11f8SLennert Buytenhek 
58491da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
58591da11f8SLennert Buytenhek }
58691da11f8SLennert Buytenhek 
58719e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
58819e57c4eSFlorian Fainelli {
58919e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
59019e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
59119e57c4eSFlorian Fainelli 
59219e57c4eSFlorian Fainelli 	if (ds->drv->get_wol)
59319e57c4eSFlorian Fainelli 		ds->drv->get_wol(ds, p->port, w);
59419e57c4eSFlorian Fainelli }
59519e57c4eSFlorian Fainelli 
59619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
59719e57c4eSFlorian Fainelli {
59819e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
59919e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
60019e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
60119e57c4eSFlorian Fainelli 
60219e57c4eSFlorian Fainelli 	if (ds->drv->set_wol)
60319e57c4eSFlorian Fainelli 		ret = ds->drv->set_wol(ds, p->port, w);
60419e57c4eSFlorian Fainelli 
60519e57c4eSFlorian Fainelli 	return ret;
60619e57c4eSFlorian Fainelli }
60719e57c4eSFlorian Fainelli 
6087905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
6097905288fSFlorian Fainelli {
6107905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6117905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
6127905288fSFlorian Fainelli 	int ret;
6137905288fSFlorian Fainelli 
6147905288fSFlorian Fainelli 	if (!ds->drv->set_eee)
6157905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6167905288fSFlorian Fainelli 
6177905288fSFlorian Fainelli 	ret = ds->drv->set_eee(ds, p->port, p->phy, e);
6187905288fSFlorian Fainelli 	if (ret)
6197905288fSFlorian Fainelli 		return ret;
6207905288fSFlorian Fainelli 
6217905288fSFlorian Fainelli 	if (p->phy)
6227905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
6237905288fSFlorian Fainelli 
6247905288fSFlorian Fainelli 	return ret;
6257905288fSFlorian Fainelli }
6267905288fSFlorian Fainelli 
6277905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
6287905288fSFlorian Fainelli {
6297905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6307905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
6317905288fSFlorian Fainelli 	int ret;
6327905288fSFlorian Fainelli 
6337905288fSFlorian Fainelli 	if (!ds->drv->get_eee)
6347905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6357905288fSFlorian Fainelli 
6367905288fSFlorian Fainelli 	ret = ds->drv->get_eee(ds, p->port, e);
6377905288fSFlorian Fainelli 	if (ret)
6387905288fSFlorian Fainelli 		return ret;
6397905288fSFlorian Fainelli 
6407905288fSFlorian Fainelli 	if (p->phy)
6417905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
6427905288fSFlorian Fainelli 
6437905288fSFlorian Fainelli 	return ret;
6447905288fSFlorian Fainelli }
6457905288fSFlorian Fainelli 
64691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
64791da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
64891da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
64991da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
6503d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
6513d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
65291da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
65391da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
6546793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
6556793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
6566793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
65791da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
65891da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
65991da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
66019e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
66119e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
6627905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
6637905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
66491da11f8SLennert Buytenhek };
66591da11f8SLennert Buytenhek 
6663e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
667c0840801SLennert Buytenhek 	.ndo_init		= dsa_slave_init,
668d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
669d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
6703e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
671d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
672d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
673d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
674*339d8262SGuenter Roeck 	.ndo_fdb_add		= dsa_slave_fdb_add,
675*339d8262SGuenter Roeck 	.ndo_fdb_del		= dsa_slave_fdb_del,
676*339d8262SGuenter Roeck 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
677d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
67898237d43SScott Feldman };
67998237d43SScott Feldman 
68098237d43SScott Feldman static const struct swdev_ops dsa_slave_swdev_ops = {
68198237d43SScott Feldman 	.swdev_parent_id_get = dsa_slave_parent_id_get,
68298237d43SScott Feldman 	.swdev_port_stp_update = dsa_slave_stp_update,
683d442ad4aSStephen Hemminger };
68491da11f8SLennert Buytenhek 
6850d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
6860d8bcdd3SFlorian Fainelli {
6870d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
688ec9436baSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
6890d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
6900d8bcdd3SFlorian Fainelli 
6910d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
6920d8bcdd3SFlorian Fainelli 		status_changed = 1;
6930d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
6940d8bcdd3SFlorian Fainelli 	}
6950d8bcdd3SFlorian Fainelli 
6960d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
6970d8bcdd3SFlorian Fainelli 		status_changed = 1;
6980d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
6990d8bcdd3SFlorian Fainelli 	}
7000d8bcdd3SFlorian Fainelli 
7010d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
7020d8bcdd3SFlorian Fainelli 		status_changed = 1;
7030d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
7040d8bcdd3SFlorian Fainelli 	}
7050d8bcdd3SFlorian Fainelli 
706ec9436baSFlorian Fainelli 	if (ds->drv->adjust_link && status_changed)
707ec9436baSFlorian Fainelli 		ds->drv->adjust_link(ds, p->port, p->phy);
708ec9436baSFlorian Fainelli 
7090d8bcdd3SFlorian Fainelli 	if (status_changed)
7100d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
7110d8bcdd3SFlorian Fainelli }
7120d8bcdd3SFlorian Fainelli 
713ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
714ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
715ce31b31cSFlorian Fainelli {
716ce31b31cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
717ce31b31cSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
718ce31b31cSFlorian Fainelli 
719ce31b31cSFlorian Fainelli 	if (ds->drv->fixed_link_update)
720ce31b31cSFlorian Fainelli 		ds->drv->fixed_link_update(ds, p->port, status);
721ce31b31cSFlorian Fainelli 
722ce31b31cSFlorian Fainelli 	return 0;
723ce31b31cSFlorian Fainelli }
724ce31b31cSFlorian Fainelli 
72591da11f8SLennert Buytenhek /* slave device setup *******************************************************/
726c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
727cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
728cd28a1a9SFlorian Fainelli 				 int addr)
729c305c165SFlorian Fainelli {
730c305c165SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
731c305c165SFlorian Fainelli 
732cd28a1a9SFlorian Fainelli 	p->phy = ds->slave_mii_bus->phy_map[addr];
733c305c165SFlorian Fainelli 	if (!p->phy)
734c305c165SFlorian Fainelli 		return -ENODEV;
735c305c165SFlorian Fainelli 
736c305c165SFlorian Fainelli 	/* Use already configured phy mode */
737c305c165SFlorian Fainelli 	p->phy_interface = p->phy->interface;
738c305c165SFlorian Fainelli 	phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
739c305c165SFlorian Fainelli 			   p->phy_interface);
740c305c165SFlorian Fainelli 
741c305c165SFlorian Fainelli 	return 0;
742c305c165SFlorian Fainelli }
743c305c165SFlorian Fainelli 
7449697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
7450d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
7460d8bcdd3SFlorian Fainelli {
7470d8bcdd3SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
7480d8bcdd3SFlorian Fainelli 	struct dsa_chip_data *cd = ds->pd;
7490d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
750ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
7516819563eSFlorian Fainelli 	u32 phy_flags = 0;
75219334920SGuenter Roeck 	int mode, ret;
7530d8bcdd3SFlorian Fainelli 
7540d8bcdd3SFlorian Fainelli 	port_dn = cd->port_dn[p->port];
75519334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
75619334920SGuenter Roeck 	if (mode < 0)
75719334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
75819334920SGuenter Roeck 	p->phy_interface = mode;
7590d8bcdd3SFlorian Fainelli 
7600d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
7610d8bcdd3SFlorian Fainelli 	if (of_phy_is_fixed_link(port_dn)) {
7620d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
7630d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
7640d8bcdd3SFlorian Fainelli 		 */
7650d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
7660d8bcdd3SFlorian Fainelli 		if (ret) {
767a2ae6007SJoe Perches 			netdev_err(slave_dev, "failed to register fixed PHY\n");
7689697f1cdSFlorian Fainelli 			return ret;
7690d8bcdd3SFlorian Fainelli 		}
770ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
7710d8bcdd3SFlorian Fainelli 		phy_dn = port_dn;
7720d8bcdd3SFlorian Fainelli 	}
7730d8bcdd3SFlorian Fainelli 
7746819563eSFlorian Fainelli 	if (ds->drv->get_phy_flags)
7756819563eSFlorian Fainelli 		phy_flags = ds->drv->get_phy_flags(ds, p->port);
7766819563eSFlorian Fainelli 
777cd28a1a9SFlorian Fainelli 	if (phy_dn) {
778cd28a1a9SFlorian Fainelli 		ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
779cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
780cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
781cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
782cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
783cd28a1a9SFlorian Fainelli 		 */
78496026d05SFlorian Fainelli 		if (!phy_is_fixed && ret >= 0 &&
78596026d05SFlorian Fainelli 		    (ds->phys_mii_mask & (1 << ret))) {
786cd28a1a9SFlorian Fainelli 			ret = dsa_slave_phy_connect(p, slave_dev, ret);
787cd28a1a9SFlorian Fainelli 			if (ret)
788cd28a1a9SFlorian Fainelli 				return ret;
789cd28a1a9SFlorian Fainelli 		} else {
7900d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
791cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
792cd28a1a9SFlorian Fainelli 						phy_flags,
7930d8bcdd3SFlorian Fainelli 						p->phy_interface);
794cd28a1a9SFlorian Fainelli 		}
795cd28a1a9SFlorian Fainelli 	}
7960d8bcdd3SFlorian Fainelli 
797ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
798ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
799ce31b31cSFlorian Fainelli 
8000d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
8010d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
8020d8bcdd3SFlorian Fainelli 	 */
803b31f65fbSAndrew Lunn 	if (!p->phy) {
804cd28a1a9SFlorian Fainelli 		ret = dsa_slave_phy_connect(p, slave_dev, p->port);
805c305c165SFlorian Fainelli 		if (ret)
806c305c165SFlorian Fainelli 			return ret;
807b31f65fbSAndrew Lunn 	} else {
808a2ae6007SJoe Perches 		netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
8090d8bcdd3SFlorian Fainelli 			    p->phy->addr, p->phy->drv->name);
8100d8bcdd3SFlorian Fainelli 	}
8119697f1cdSFlorian Fainelli 
8129697f1cdSFlorian Fainelli 	return 0;
813b31f65fbSAndrew Lunn }
8140d8bcdd3SFlorian Fainelli 
81524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
81624462549SFlorian Fainelli {
81724462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
81824462549SFlorian Fainelli 
81924462549SFlorian Fainelli 	netif_device_detach(slave_dev);
82024462549SFlorian Fainelli 
82124462549SFlorian Fainelli 	if (p->phy) {
82224462549SFlorian Fainelli 		phy_stop(p->phy);
82324462549SFlorian Fainelli 		p->old_pause = -1;
82424462549SFlorian Fainelli 		p->old_link = -1;
82524462549SFlorian Fainelli 		p->old_duplex = -1;
82624462549SFlorian Fainelli 		phy_suspend(p->phy);
82724462549SFlorian Fainelli 	}
82824462549SFlorian Fainelli 
82924462549SFlorian Fainelli 	return 0;
83024462549SFlorian Fainelli }
83124462549SFlorian Fainelli 
83224462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
83324462549SFlorian Fainelli {
83424462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
83524462549SFlorian Fainelli 
83624462549SFlorian Fainelli 	netif_device_attach(slave_dev);
83724462549SFlorian Fainelli 
83824462549SFlorian Fainelli 	if (p->phy) {
83924462549SFlorian Fainelli 		phy_resume(p->phy);
84024462549SFlorian Fainelli 		phy_start(p->phy);
84124462549SFlorian Fainelli 	}
84224462549SFlorian Fainelli 
84324462549SFlorian Fainelli 	return 0;
84424462549SFlorian Fainelli }
84524462549SFlorian Fainelli 
846d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
84791da11f8SLennert Buytenhek 		     int port, char *name)
84891da11f8SLennert Buytenhek {
849e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
85091da11f8SLennert Buytenhek 	struct net_device *slave_dev;
85191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
85291da11f8SLennert Buytenhek 	int ret;
85391da11f8SLennert Buytenhek 
854c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
855c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
85691da11f8SLennert Buytenhek 	if (slave_dev == NULL)
857d87d6f44SGuenter Roeck 		return -ENOMEM;
85891da11f8SLennert Buytenhek 
85991da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
8607ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
8612fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
86291da11f8SLennert Buytenhek 	slave_dev->tx_queue_len = 0;
8633e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
86498237d43SScott Feldman 	slave_dev->swdev_ops = &dsa_slave_swdev_ops;
865d442ad4aSStephen Hemminger 
86691da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
867bd47497aSFlorian Fainelli 	slave_dev->dev.of_node = ds->pd->port_dn[port];
86891da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
86991da11f8SLennert Buytenhek 
87091da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
87191da11f8SLennert Buytenhek 	p->dev = slave_dev;
87291da11f8SLennert Buytenhek 	p->parent = ds;
87391da11f8SLennert Buytenhek 	p->port = port;
8740d8bcdd3SFlorian Fainelli 
8755075314eSAlexander Duyck 	switch (ds->dst->tag_protocol) {
8765075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA
8775075314eSAlexander Duyck 	case DSA_TAG_PROTO_DSA:
8785075314eSAlexander Duyck 		p->xmit = dsa_netdev_ops.xmit;
8795075314eSAlexander Duyck 		break;
8805075314eSAlexander Duyck #endif
8815075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA
8825075314eSAlexander Duyck 	case DSA_TAG_PROTO_EDSA:
8835075314eSAlexander Duyck 		p->xmit = edsa_netdev_ops.xmit;
8845075314eSAlexander Duyck 		break;
8855075314eSAlexander Duyck #endif
8865075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER
8875075314eSAlexander Duyck 	case DSA_TAG_PROTO_TRAILER:
8885075314eSAlexander Duyck 		p->xmit = trailer_netdev_ops.xmit;
8895075314eSAlexander Duyck 		break;
8905075314eSAlexander Duyck #endif
8915075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM
8925075314eSAlexander Duyck 	case DSA_TAG_PROTO_BRCM:
8935075314eSAlexander Duyck 		p->xmit = brcm_netdev_ops.xmit;
8945075314eSAlexander Duyck 		break;
8955075314eSAlexander Duyck #endif
8965075314eSAlexander Duyck 	default:
8975075314eSAlexander Duyck 		p->xmit	= dsa_slave_notag_xmit;
8985075314eSAlexander Duyck 		break;
8995075314eSAlexander Duyck 	}
9005075314eSAlexander Duyck 
9010d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
9020d8bcdd3SFlorian Fainelli 	p->old_link = -1;
9030d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
9040d8bcdd3SFlorian Fainelli 
9059697f1cdSFlorian Fainelli 	ret = dsa_slave_phy_setup(p, slave_dev);
9069697f1cdSFlorian Fainelli 	if (ret) {
9079697f1cdSFlorian Fainelli 		free_netdev(slave_dev);
908d87d6f44SGuenter Roeck 		return ret;
9099697f1cdSFlorian Fainelli 	}
91091da11f8SLennert Buytenhek 
911d87d6f44SGuenter Roeck 	ds->ports[port] = slave_dev;
91291da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
91391da11f8SLennert Buytenhek 	if (ret) {
914a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
915a2ae6007SJoe Perches 			   ret, slave_dev->name);
9169697f1cdSFlorian Fainelli 		phy_disconnect(p->phy);
917d87d6f44SGuenter Roeck 		ds->ports[port] = NULL;
91891da11f8SLennert Buytenhek 		free_netdev(slave_dev);
919d87d6f44SGuenter Roeck 		return ret;
92091da11f8SLennert Buytenhek 	}
92191da11f8SLennert Buytenhek 
92291da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
92391da11f8SLennert Buytenhek 
924d87d6f44SGuenter Roeck 	return 0;
92591da11f8SLennert Buytenhek }
926b73adef6SFlorian Fainelli 
927b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
928b73adef6SFlorian Fainelli {
929b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
930b73adef6SFlorian Fainelli }
931b73adef6SFlorian Fainelli 
932b73adef6SFlorian Fainelli static int dsa_slave_master_changed(struct net_device *dev)
933b73adef6SFlorian Fainelli {
934b73adef6SFlorian Fainelli 	struct net_device *master = netdev_master_upper_dev_get(dev);
935b06b107aSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
936b73adef6SFlorian Fainelli 	int err = 0;
937b73adef6SFlorian Fainelli 
938b73adef6SFlorian Fainelli 	if (master && master->rtnl_link_ops &&
939b73adef6SFlorian Fainelli 	    !strcmp(master->rtnl_link_ops->kind, "bridge"))
940b73adef6SFlorian Fainelli 		err = dsa_slave_bridge_port_join(dev, master);
941b06b107aSGuenter Roeck 	else if (dsa_port_is_bridged(p))
942b73adef6SFlorian Fainelli 		err = dsa_slave_bridge_port_leave(dev);
943b73adef6SFlorian Fainelli 
944b73adef6SFlorian Fainelli 	return err;
945b73adef6SFlorian Fainelli }
946b73adef6SFlorian Fainelli 
947b73adef6SFlorian Fainelli int dsa_slave_netdevice_event(struct notifier_block *unused,
948b73adef6SFlorian Fainelli 			      unsigned long event, void *ptr)
949b73adef6SFlorian Fainelli {
950b73adef6SFlorian Fainelli 	struct net_device *dev;
951b73adef6SFlorian Fainelli 	int err = 0;
952b73adef6SFlorian Fainelli 
953b73adef6SFlorian Fainelli 	switch (event) {
954b73adef6SFlorian Fainelli 	case NETDEV_CHANGEUPPER:
955b73adef6SFlorian Fainelli 		dev = netdev_notifier_info_to_dev(ptr);
956b73adef6SFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
957b73adef6SFlorian Fainelli 			goto out;
958b73adef6SFlorian Fainelli 
959b73adef6SFlorian Fainelli 		err = dsa_slave_master_changed(dev);
960b73adef6SFlorian Fainelli 		if (err)
961b73adef6SFlorian Fainelli 			netdev_warn(dev, "failed to reflect master change\n");
962b73adef6SFlorian Fainelli 
963b73adef6SFlorian Fainelli 		break;
964b73adef6SFlorian Fainelli 	}
965b73adef6SFlorian Fainelli 
966b73adef6SFlorian Fainelli out:
967b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
968b73adef6SFlorian Fainelli }
969