xref: /openbmc/linux/net/dsa/slave.c (revision 6debb68a2d1acd1f5eca1d9d1c79a54dc998b6d6)
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>
19b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
2098237d43SScott Feldman #include <net/switchdev.h>
21b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2204ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2391da11f8SLennert Buytenhek #include "dsa_priv.h"
2491da11f8SLennert Buytenhek 
2591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2791da11f8SLennert Buytenhek {
2891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2991da11f8SLennert Buytenhek 
300d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
3191da11f8SLennert Buytenhek 		return ds->drv->phy_read(ds, addr, reg);
3291da11f8SLennert Buytenhek 
3391da11f8SLennert Buytenhek 	return 0xffff;
3491da11f8SLennert Buytenhek }
3591da11f8SLennert Buytenhek 
3691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3791da11f8SLennert Buytenhek {
3891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3991da11f8SLennert Buytenhek 
400d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
4191da11f8SLennert Buytenhek 		return ds->drv->phy_write(ds, addr, reg, val);
4291da11f8SLennert Buytenhek 
4391da11f8SLennert Buytenhek 	return 0;
4491da11f8SLennert Buytenhek }
4591da11f8SLennert Buytenhek 
4691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4791da11f8SLennert Buytenhek {
4891da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
52f490be04SFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
53f490be04SFlorian Fainelli 			ds->index, ds->pd->sw_addr);
54b4d2394dSAlexander Duyck 	ds->slave_mii_bus->parent = ds->master_dev;
5524df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5691da11f8SLennert Buytenhek }
5791da11f8SLennert Buytenhek 
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek /* slave device handling ****************************************************/
60abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
61c0840801SLennert Buytenhek {
62c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
63c0840801SLennert Buytenhek 
64abd2be00SNicolas Dichtel 	return p->parent->dst->master_netdev->ifindex;
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)
1174fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
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 
20411149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev,
2058f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan,
206f8db8348SJiri Pirko 				   struct switchdev_trans *trans)
20711149536SVivien Didelot {
20811149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
20911149536SVivien Didelot 	struct dsa_switch *ds = p->parent;
21011149536SVivien Didelot 	int err;
21111149536SVivien Didelot 
21279a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans)) {
21376e398a6SVivien Didelot 		if (!ds->drv->port_vlan_prepare || !ds->drv->port_vlan_add)
21411149536SVivien Didelot 			return -EOPNOTSUPP;
21511149536SVivien Didelot 
21676e398a6SVivien Didelot 		err = ds->drv->port_vlan_prepare(ds, p->port, vlan, trans);
21711149536SVivien Didelot 		if (err)
21811149536SVivien Didelot 			return err;
21976e398a6SVivien Didelot 	} else {
22076e398a6SVivien Didelot 		err = ds->drv->port_vlan_add(ds, p->port, vlan, trans);
22176e398a6SVivien Didelot 		if (err)
22276e398a6SVivien Didelot 			return err;
22311149536SVivien Didelot 	}
22411149536SVivien Didelot 
22511149536SVivien Didelot 	return 0;
22611149536SVivien Didelot }
22711149536SVivien Didelot 
22811149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev,
2298f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan)
23011149536SVivien Didelot {
23111149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
23211149536SVivien Didelot 	struct dsa_switch *ds = p->parent;
23311149536SVivien Didelot 
23411149536SVivien Didelot 	if (!ds->drv->port_vlan_del)
23511149536SVivien Didelot 		return -EOPNOTSUPP;
23611149536SVivien Didelot 
23776e398a6SVivien Didelot 	return ds->drv->port_vlan_del(ds, p->port, vlan);
23811149536SVivien Didelot }
23911149536SVivien Didelot 
24011149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev,
2418f24f309SJiri Pirko 				    struct switchdev_obj_port_vlan *vlan,
242648b4a99SJiri Pirko 				    switchdev_obj_dump_cb_t *cb)
24311149536SVivien Didelot {
24411149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
24511149536SVivien Didelot 	struct dsa_switch *ds = p->parent;
24611149536SVivien Didelot 
24765aebfc0SVivien Didelot 	if (ds->drv->port_vlan_dump)
24865aebfc0SVivien Didelot 		return ds->drv->port_vlan_dump(ds, p->port, vlan, cb);
24965aebfc0SVivien Didelot 
25011149536SVivien Didelot 	return -EOPNOTSUPP;
25111149536SVivien Didelot }
25211149536SVivien Didelot 
253ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev,
25452ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb,
255f8db8348SJiri Pirko 				  struct switchdev_trans *trans)
256cdf09697SDavid S. Miller {
257cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
258cdf09697SDavid S. Miller 	struct dsa_switch *ds = p->parent;
259146a3206SVivien Didelot 	int ret;
260146a3206SVivien Didelot 
261146a3206SVivien Didelot 	if (!ds->drv->port_fdb_prepare || !ds->drv->port_fdb_add)
262146a3206SVivien Didelot 		return -EOPNOTSUPP;
263cdf09697SDavid S. Miller 
26479a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans))
265146a3206SVivien Didelot 		ret = ds->drv->port_fdb_prepare(ds, p->port, fdb, trans);
26679a62eb2SJiri Pirko 	else
2671f36faf2SVivien Didelot 		ret = ds->drv->port_fdb_add(ds, p->port, fdb, trans);
268cdf09697SDavid S. Miller 
269cdf09697SDavid S. Miller 	return ret;
270cdf09697SDavid S. Miller }
271cdf09697SDavid S. Miller 
272ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev,
27352ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb)
274cdf09697SDavid S. Miller {
275cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
276cdf09697SDavid S. Miller 	struct dsa_switch *ds = p->parent;
277cdf09697SDavid S. Miller 	int ret = -EOPNOTSUPP;
278cdf09697SDavid S. Miller 
2792a778e1bSVivien Didelot 	if (ds->drv->port_fdb_del)
2808057b3e7SVivien Didelot 		ret = ds->drv->port_fdb_del(ds, p->port, fdb);
281cdf09697SDavid S. Miller 
282cdf09697SDavid S. Miller 	return ret;
283cdf09697SDavid S. Miller }
284cdf09697SDavid S. Miller 
285ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev,
28652ba57cfSJiri Pirko 				   struct switchdev_obj_port_fdb *fdb,
287648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
288cdf09697SDavid S. Miller {
289cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
290cdf09697SDavid S. Miller 	struct dsa_switch *ds = p->parent;
291cdf09697SDavid S. Miller 
292ea70ba98SVivien Didelot 	if (ds->drv->port_fdb_dump)
293ea70ba98SVivien Didelot 		return ds->drv->port_fdb_dump(ds, p->port, fdb, cb);
294ea70ba98SVivien Didelot 
295cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
296cdf09697SDavid S. Miller }
297cdf09697SDavid S. Miller 
29891da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
29991da11f8SLennert Buytenhek {
30091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
30191da11f8SLennert Buytenhek 
30291da11f8SLennert Buytenhek 	if (p->phy != NULL)
30328b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
30491da11f8SLennert Buytenhek 
30591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
30691da11f8SLennert Buytenhek }
30791da11f8SLennert Buytenhek 
308b73adef6SFlorian Fainelli static int dsa_slave_stp_update(struct net_device *dev, u8 state)
309b73adef6SFlorian Fainelli {
310b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
311b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
312b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
313b73adef6SFlorian Fainelli 
314b73adef6SFlorian Fainelli 	if (ds->drv->port_stp_update)
315b73adef6SFlorian Fainelli 		ret = ds->drv->port_stp_update(ds, p->port, state);
316b73adef6SFlorian Fainelli 
317b73adef6SFlorian Fainelli 	return ret;
318b73adef6SFlorian Fainelli }
319b73adef6SFlorian Fainelli 
320fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
321fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
322fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
323fb2dabadSVivien Didelot {
324fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
325fb2dabadSVivien Didelot 	struct dsa_switch *ds = p->parent;
326fb2dabadSVivien Didelot 
327fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
328fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
329fb2dabadSVivien Didelot 		return 0;
330fb2dabadSVivien Didelot 
331fb2dabadSVivien Didelot 	if (ds->drv->port_vlan_filtering)
332fb2dabadSVivien Didelot 		return ds->drv->port_vlan_filtering(ds, p->port,
333fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
334fb2dabadSVivien Didelot 
335fb2dabadSVivien Didelot 	return 0;
336fb2dabadSVivien Didelot }
337fb2dabadSVivien Didelot 
33835636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
339f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
3407ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
34135636062SScott Feldman {
342b8d866acSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
343b8d866acSVivien Didelot 	struct dsa_switch *ds = p->parent;
344b8d866acSVivien Didelot 	int ret;
34535636062SScott Feldman 
34635636062SScott Feldman 	switch (attr->id) {
3471f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
348b8d866acSVivien Didelot 		if (switchdev_trans_ph_prepare(trans))
349b8d866acSVivien Didelot 			ret = ds->drv->port_stp_update ? 0 : -EOPNOTSUPP;
350b8d866acSVivien Didelot 		else
351b8d866acSVivien Didelot 			ret = ds->drv->port_stp_update(ds, p->port,
352b8d866acSVivien Didelot 						       attr->u.stp_state);
35335636062SScott Feldman 		break;
354fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
355fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
356fb2dabadSVivien Didelot 		break;
35735636062SScott Feldman 	default:
35835636062SScott Feldman 		ret = -EOPNOTSUPP;
35935636062SScott Feldman 		break;
36035636062SScott Feldman 	}
36135636062SScott Feldman 
36235636062SScott Feldman 	return ret;
36335636062SScott Feldman }
36435636062SScott Feldman 
365ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
366648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
3677ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
368ba14d9ebSVivien Didelot {
369ba14d9ebSVivien Didelot 	int err;
370ba14d9ebSVivien Didelot 
371ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
372ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
373ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
374ba14d9ebSVivien Didelot 	 */
375ba14d9ebSVivien Didelot 
3769e8f4a54SJiri Pirko 	switch (obj->id) {
37757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
378648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_add(dev,
379648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj),
380648b4a99SJiri Pirko 					     trans);
381ba14d9ebSVivien Didelot 		break;
38257d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
383648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
384648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
385648b4a99SJiri Pirko 					      trans);
38611149536SVivien Didelot 		break;
387ba14d9ebSVivien Didelot 	default:
388ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
389ba14d9ebSVivien Didelot 		break;
390ba14d9ebSVivien Didelot 	}
391ba14d9ebSVivien Didelot 
392ba14d9ebSVivien Didelot 	return err;
393ba14d9ebSVivien Didelot }
394ba14d9ebSVivien Didelot 
395ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
396648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
397ba14d9ebSVivien Didelot {
398ba14d9ebSVivien Didelot 	int err;
399ba14d9ebSVivien Didelot 
4009e8f4a54SJiri Pirko 	switch (obj->id) {
40157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
402648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_del(dev,
403648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj));
404ba14d9ebSVivien Didelot 		break;
40557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
406648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
407648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
40811149536SVivien Didelot 		break;
409ba14d9ebSVivien Didelot 	default:
410ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
411ba14d9ebSVivien Didelot 		break;
412ba14d9ebSVivien Didelot 	}
413ba14d9ebSVivien Didelot 
414ba14d9ebSVivien Didelot 	return err;
415ba14d9ebSVivien Didelot }
416ba14d9ebSVivien Didelot 
417ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
418648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
419648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
420ba14d9ebSVivien Didelot {
421ba14d9ebSVivien Didelot 	int err;
422ba14d9ebSVivien Didelot 
4239e8f4a54SJiri Pirko 	switch (obj->id) {
42457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
425648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_dump(dev,
426648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_FDB(obj),
427648b4a99SJiri Pirko 					      cb);
428ba14d9ebSVivien Didelot 		break;
42957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
430648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
431648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
432648b4a99SJiri Pirko 					       cb);
43311149536SVivien Didelot 		break;
434ba14d9ebSVivien Didelot 	default:
435ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
436ba14d9ebSVivien Didelot 		break;
437ba14d9ebSVivien Didelot 	}
438ba14d9ebSVivien Didelot 
439ba14d9ebSVivien Didelot 	return err;
440ba14d9ebSVivien Didelot }
441ba14d9ebSVivien Didelot 
442b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
443b73adef6SFlorian Fainelli 				      struct net_device *br)
444b73adef6SFlorian Fainelli {
445b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
446b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
447b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
448b73adef6SFlorian Fainelli 
449b73adef6SFlorian Fainelli 	p->bridge_dev = br;
450b73adef6SFlorian Fainelli 
45171327a4eSVivien Didelot 	if (ds->drv->port_bridge_join)
45271327a4eSVivien Didelot 		ret = ds->drv->port_bridge_join(ds, p->port, br);
453b73adef6SFlorian Fainelli 
454*6debb68aSVivien Didelot 	return ret == -EOPNOTSUPP ? 0 : ret;
455b73adef6SFlorian Fainelli }
456b73adef6SFlorian Fainelli 
45716bfa702SVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev)
458b73adef6SFlorian Fainelli {
459b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
460b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
461b73adef6SFlorian Fainelli 
462b73adef6SFlorian Fainelli 
46371327a4eSVivien Didelot 	if (ds->drv->port_bridge_leave)
46416bfa702SVivien Didelot 		ds->drv->port_bridge_leave(ds, p->port);
465b73adef6SFlorian Fainelli 
466b73adef6SFlorian Fainelli 	p->bridge_dev = NULL;
467b73adef6SFlorian Fainelli 
468b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
469b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
470b73adef6SFlorian Fainelli 	 */
471b73adef6SFlorian Fainelli 	dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
472b73adef6SFlorian Fainelli }
473b73adef6SFlorian Fainelli 
474f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
475f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
476b73adef6SFlorian Fainelli {
477b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
478b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
479b73adef6SFlorian Fainelli 
480f8e20a9fSScott Feldman 	switch (attr->id) {
4811f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
48242275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
48342275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
484f8e20a9fSScott Feldman 		break;
485f8e20a9fSScott Feldman 	default:
486f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
487f8e20a9fSScott Feldman 	}
488b73adef6SFlorian Fainelli 
489b73adef6SFlorian Fainelli 	return 0;
490b73adef6SFlorian Fainelli }
491b73adef6SFlorian Fainelli 
49204ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
49304ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
49404ff53f9SFlorian Fainelli {
49504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
49604ff53f9SFlorian Fainelli 	if (p->netpoll)
49704ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
49804ff53f9SFlorian Fainelli #else
49904ff53f9SFlorian Fainelli 	BUG();
50004ff53f9SFlorian Fainelli #endif
50104ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
50204ff53f9SFlorian Fainelli }
50304ff53f9SFlorian Fainelli 
5043e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5053e8a72d1SFlorian Fainelli {
5063e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5074ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5083e8a72d1SFlorian Fainelli 
5094ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
5104ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
5113e8a72d1SFlorian Fainelli 
5124ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
5134ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
5144ed70ce9SFlorian Fainelli 	if (!nskb)
5154ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
5165aed85ceSFlorian Fainelli 
51704ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
51804ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
51904ff53f9SFlorian Fainelli 	 */
52004ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
52104ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
52204ff53f9SFlorian Fainelli 
5234ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
5244ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
5254ed70ce9SFlorian Fainelli 	 */
5264ed70ce9SFlorian Fainelli 	nskb->dev = p->parent->dst->master_netdev;
5274ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
5285aed85ceSFlorian Fainelli 
5295aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
5305aed85ceSFlorian Fainelli }
5315aed85ceSFlorian Fainelli 
5324ed70ce9SFlorian Fainelli static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
5334ed70ce9SFlorian Fainelli 					    struct net_device *dev)
5344ed70ce9SFlorian Fainelli {
5354ed70ce9SFlorian Fainelli 	/* Just return the original SKB */
5364ed70ce9SFlorian Fainelli 	return skb;
5374ed70ce9SFlorian Fainelli }
5384ed70ce9SFlorian Fainelli 
53991da11f8SLennert Buytenhek 
54091da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
54191da11f8SLennert Buytenhek static int
54291da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
54391da11f8SLennert Buytenhek {
54491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
54591da11f8SLennert Buytenhek 	int err;
54691da11f8SLennert Buytenhek 
54791da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
54891da11f8SLennert Buytenhek 	if (p->phy != NULL) {
54991da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
55091da11f8SLennert Buytenhek 		if (err == 0)
55191da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
55291da11f8SLennert Buytenhek 	}
55391da11f8SLennert Buytenhek 
55491da11f8SLennert Buytenhek 	return err;
55591da11f8SLennert Buytenhek }
55691da11f8SLennert Buytenhek 
55791da11f8SLennert Buytenhek static int
55891da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
55991da11f8SLennert Buytenhek {
56091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
56191da11f8SLennert Buytenhek 
56291da11f8SLennert Buytenhek 	if (p->phy != NULL)
56391da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
56491da11f8SLennert Buytenhek 
56591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
56691da11f8SLennert Buytenhek }
56791da11f8SLennert Buytenhek 
56891da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
56991da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
57091da11f8SLennert Buytenhek {
5717826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
5727826d43fSJiri Pirko 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
5737826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
5747826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
57591da11f8SLennert Buytenhek }
57691da11f8SLennert Buytenhek 
5773d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
5783d762a0fSGuenter Roeck {
5793d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5803d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
5813d762a0fSGuenter Roeck 
5823d762a0fSGuenter Roeck 	if (ds->drv->get_regs_len)
5833d762a0fSGuenter Roeck 		return ds->drv->get_regs_len(ds, p->port);
5843d762a0fSGuenter Roeck 
5853d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
5863d762a0fSGuenter Roeck }
5873d762a0fSGuenter Roeck 
5883d762a0fSGuenter Roeck static void
5893d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
5903d762a0fSGuenter Roeck {
5913d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5923d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
5933d762a0fSGuenter Roeck 
5943d762a0fSGuenter Roeck 	if (ds->drv->get_regs)
5953d762a0fSGuenter Roeck 		ds->drv->get_regs(ds, p->port, regs, _p);
5963d762a0fSGuenter Roeck }
5973d762a0fSGuenter Roeck 
59891da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
59991da11f8SLennert Buytenhek {
60091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
60191da11f8SLennert Buytenhek 
60291da11f8SLennert Buytenhek 	if (p->phy != NULL)
60391da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
60491da11f8SLennert Buytenhek 
60591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
60691da11f8SLennert Buytenhek }
60791da11f8SLennert Buytenhek 
60891da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
60991da11f8SLennert Buytenhek {
61091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
61191da11f8SLennert Buytenhek 
61291da11f8SLennert Buytenhek 	if (p->phy != NULL) {
61391da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
61491da11f8SLennert Buytenhek 		return p->phy->link;
61591da11f8SLennert Buytenhek 	}
61691da11f8SLennert Buytenhek 
61791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
61891da11f8SLennert Buytenhek }
61991da11f8SLennert Buytenhek 
6206793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6216793abb4SGuenter Roeck {
6226793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6236793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6246793abb4SGuenter Roeck 
6256793abb4SGuenter Roeck 	if (ds->pd->eeprom_len)
6266793abb4SGuenter Roeck 		return ds->pd->eeprom_len;
6276793abb4SGuenter Roeck 
6286793abb4SGuenter Roeck 	if (ds->drv->get_eeprom_len)
6296793abb4SGuenter Roeck 		return ds->drv->get_eeprom_len(ds);
6306793abb4SGuenter Roeck 
6316793abb4SGuenter Roeck 	return 0;
6326793abb4SGuenter Roeck }
6336793abb4SGuenter Roeck 
6346793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6356793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6366793abb4SGuenter Roeck {
6376793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6386793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6396793abb4SGuenter Roeck 
6406793abb4SGuenter Roeck 	if (ds->drv->get_eeprom)
6416793abb4SGuenter Roeck 		return ds->drv->get_eeprom(ds, eeprom, data);
6426793abb4SGuenter Roeck 
6436793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6446793abb4SGuenter Roeck }
6456793abb4SGuenter Roeck 
6466793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
6476793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6486793abb4SGuenter Roeck {
6496793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6506793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6516793abb4SGuenter Roeck 
6526793abb4SGuenter Roeck 	if (ds->drv->set_eeprom)
6536793abb4SGuenter Roeck 		return ds->drv->set_eeprom(ds, eeprom, data);
6546793abb4SGuenter Roeck 
6556793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6566793abb4SGuenter Roeck }
6576793abb4SGuenter Roeck 
65891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
65991da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
66091da11f8SLennert Buytenhek {
66191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
66291da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
66391da11f8SLennert Buytenhek 
66491da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
66591da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
66691da11f8SLennert Buytenhek 
66791da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
66891da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
66991da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
67091da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
67191da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
67291da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
67391da11f8SLennert Buytenhek 	}
67491da11f8SLennert Buytenhek }
67591da11f8SLennert Buytenhek 
67691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
67791da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
67891da11f8SLennert Buytenhek 					uint64_t *data)
67991da11f8SLennert Buytenhek {
68091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
68191da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
68291da11f8SLennert Buytenhek 
68391da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
68491da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
68591da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
68691da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
68791da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
68891da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
68991da11f8SLennert Buytenhek }
69091da11f8SLennert Buytenhek 
69191da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
69291da11f8SLennert Buytenhek {
69391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
69491da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
69591da11f8SLennert Buytenhek 
69691da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
69791da11f8SLennert Buytenhek 		int count;
69891da11f8SLennert Buytenhek 
69991da11f8SLennert Buytenhek 		count = 4;
70091da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
70191da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
70291da11f8SLennert Buytenhek 
70391da11f8SLennert Buytenhek 		return count;
70491da11f8SLennert Buytenhek 	}
70591da11f8SLennert Buytenhek 
70691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
70791da11f8SLennert Buytenhek }
70891da11f8SLennert Buytenhek 
70919e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
71019e57c4eSFlorian Fainelli {
71119e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
71219e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
71319e57c4eSFlorian Fainelli 
71419e57c4eSFlorian Fainelli 	if (ds->drv->get_wol)
71519e57c4eSFlorian Fainelli 		ds->drv->get_wol(ds, p->port, w);
71619e57c4eSFlorian Fainelli }
71719e57c4eSFlorian Fainelli 
71819e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
71919e57c4eSFlorian Fainelli {
72019e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
72119e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
72219e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
72319e57c4eSFlorian Fainelli 
72419e57c4eSFlorian Fainelli 	if (ds->drv->set_wol)
72519e57c4eSFlorian Fainelli 		ret = ds->drv->set_wol(ds, p->port, w);
72619e57c4eSFlorian Fainelli 
72719e57c4eSFlorian Fainelli 	return ret;
72819e57c4eSFlorian Fainelli }
72919e57c4eSFlorian Fainelli 
7307905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7317905288fSFlorian Fainelli {
7327905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7337905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
7347905288fSFlorian Fainelli 	int ret;
7357905288fSFlorian Fainelli 
7367905288fSFlorian Fainelli 	if (!ds->drv->set_eee)
7377905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7387905288fSFlorian Fainelli 
7397905288fSFlorian Fainelli 	ret = ds->drv->set_eee(ds, p->port, p->phy, e);
7407905288fSFlorian Fainelli 	if (ret)
7417905288fSFlorian Fainelli 		return ret;
7427905288fSFlorian Fainelli 
7437905288fSFlorian Fainelli 	if (p->phy)
7447905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
7457905288fSFlorian Fainelli 
7467905288fSFlorian Fainelli 	return ret;
7477905288fSFlorian Fainelli }
7487905288fSFlorian Fainelli 
7497905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7507905288fSFlorian Fainelli {
7517905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7527905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
7537905288fSFlorian Fainelli 	int ret;
7547905288fSFlorian Fainelli 
7557905288fSFlorian Fainelli 	if (!ds->drv->get_eee)
7567905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7577905288fSFlorian Fainelli 
7587905288fSFlorian Fainelli 	ret = ds->drv->get_eee(ds, p->port, e);
7597905288fSFlorian Fainelli 	if (ret)
7607905288fSFlorian Fainelli 		return ret;
7617905288fSFlorian Fainelli 
7627905288fSFlorian Fainelli 	if (p->phy)
7637905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
7647905288fSFlorian Fainelli 
7657905288fSFlorian Fainelli 	return ret;
7667905288fSFlorian Fainelli }
7677905288fSFlorian Fainelli 
76804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
76904ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
77004ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
77104ff53f9SFlorian Fainelli {
77204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
77304ff53f9SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
77404ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
77504ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
77604ff53f9SFlorian Fainelli 	int err = 0;
77704ff53f9SFlorian Fainelli 
77804ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
77904ff53f9SFlorian Fainelli 	if (!netpoll)
78004ff53f9SFlorian Fainelli 		return -ENOMEM;
78104ff53f9SFlorian Fainelli 
78204ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
78304ff53f9SFlorian Fainelli 	if (err) {
78404ff53f9SFlorian Fainelli 		kfree(netpoll);
78504ff53f9SFlorian Fainelli 		goto out;
78604ff53f9SFlorian Fainelli 	}
78704ff53f9SFlorian Fainelli 
78804ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
78904ff53f9SFlorian Fainelli out:
79004ff53f9SFlorian Fainelli 	return err;
79104ff53f9SFlorian Fainelli }
79204ff53f9SFlorian Fainelli 
79304ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
79404ff53f9SFlorian Fainelli {
79504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
79604ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
79704ff53f9SFlorian Fainelli 
79804ff53f9SFlorian Fainelli 	if (!netpoll)
79904ff53f9SFlorian Fainelli 		return;
80004ff53f9SFlorian Fainelli 
80104ff53f9SFlorian Fainelli 	p->netpoll = NULL;
80204ff53f9SFlorian Fainelli 
80304ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
80404ff53f9SFlorian Fainelli }
80504ff53f9SFlorian Fainelli 
80604ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
80704ff53f9SFlorian Fainelli {
80804ff53f9SFlorian Fainelli }
80904ff53f9SFlorian Fainelli #endif
81004ff53f9SFlorian Fainelli 
81191da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
81291da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
81391da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
81491da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
8153d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
8163d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
81791da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
81891da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
8196793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
8206793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
8216793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
82291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
82391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
82491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
82519e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
82619e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
8277905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
8287905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
82991da11f8SLennert Buytenhek };
83091da11f8SLennert Buytenhek 
8313e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
832d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
833d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
8343e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
835d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
836d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
837d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
838ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
839ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
840ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
841d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
842abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
84304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
84404ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
84504ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
84604ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
84704ff53f9SFlorian Fainelli #endif
84811149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
84911149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
85011149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
85198237d43SScott Feldman };
85298237d43SScott Feldman 
8539d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
854f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
85535636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
856ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
857ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
858ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
859d442ad4aSStephen Hemminger };
86091da11f8SLennert Buytenhek 
861f37db85dSFlorian Fainelli static struct device_type dsa_type = {
862f37db85dSFlorian Fainelli 	.name	= "dsa",
863f37db85dSFlorian Fainelli };
864f37db85dSFlorian Fainelli 
8650d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
8660d8bcdd3SFlorian Fainelli {
8670d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
868ec9436baSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
8690d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
8700d8bcdd3SFlorian Fainelli 
8710d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
8720d8bcdd3SFlorian Fainelli 		status_changed = 1;
8730d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
8740d8bcdd3SFlorian Fainelli 	}
8750d8bcdd3SFlorian Fainelli 
8760d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
8770d8bcdd3SFlorian Fainelli 		status_changed = 1;
8780d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
8790d8bcdd3SFlorian Fainelli 	}
8800d8bcdd3SFlorian Fainelli 
8810d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
8820d8bcdd3SFlorian Fainelli 		status_changed = 1;
8830d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
8840d8bcdd3SFlorian Fainelli 	}
8850d8bcdd3SFlorian Fainelli 
886ec9436baSFlorian Fainelli 	if (ds->drv->adjust_link && status_changed)
887ec9436baSFlorian Fainelli 		ds->drv->adjust_link(ds, p->port, p->phy);
888ec9436baSFlorian Fainelli 
8890d8bcdd3SFlorian Fainelli 	if (status_changed)
8900d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
8910d8bcdd3SFlorian Fainelli }
8920d8bcdd3SFlorian Fainelli 
893ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
894ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
895ce31b31cSFlorian Fainelli {
896b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
897b71be352SAndrew Lunn 	struct dsa_switch *ds;
898ce31b31cSFlorian Fainelli 
899b71be352SAndrew Lunn 	if (dev) {
900b71be352SAndrew Lunn 		p = netdev_priv(dev);
901b71be352SAndrew Lunn 		ds = p->parent;
902ce31b31cSFlorian Fainelli 		if (ds->drv->fixed_link_update)
903ce31b31cSFlorian Fainelli 			ds->drv->fixed_link_update(ds, p->port, status);
904b71be352SAndrew Lunn 	}
905ce31b31cSFlorian Fainelli 
906ce31b31cSFlorian Fainelli 	return 0;
907ce31b31cSFlorian Fainelli }
908ce31b31cSFlorian Fainelli 
90991da11f8SLennert Buytenhek /* slave device setup *******************************************************/
910c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
911cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
912cd28a1a9SFlorian Fainelli 				 int addr)
913c305c165SFlorian Fainelli {
914c305c165SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
915c305c165SFlorian Fainelli 
9167f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
917d25b8e74SRussell King 	if (!p->phy) {
918d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
919c305c165SFlorian Fainelli 		return -ENODEV;
920d25b8e74SRussell King 	}
921c305c165SFlorian Fainelli 
922c305c165SFlorian Fainelli 	/* Use already configured phy mode */
923211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
924c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
925c305c165SFlorian Fainelli 	phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
926c305c165SFlorian Fainelli 			   p->phy_interface);
927c305c165SFlorian Fainelli 
928c305c165SFlorian Fainelli 	return 0;
929c305c165SFlorian Fainelli }
930c305c165SFlorian Fainelli 
9319697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
9320d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
9330d8bcdd3SFlorian Fainelli {
9340d8bcdd3SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
9350d8bcdd3SFlorian Fainelli 	struct dsa_chip_data *cd = ds->pd;
9360d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
937ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
9386819563eSFlorian Fainelli 	u32 phy_flags = 0;
93919334920SGuenter Roeck 	int mode, ret;
9400d8bcdd3SFlorian Fainelli 
9410d8bcdd3SFlorian Fainelli 	port_dn = cd->port_dn[p->port];
94219334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
94319334920SGuenter Roeck 	if (mode < 0)
94419334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
94519334920SGuenter Roeck 	p->phy_interface = mode;
9460d8bcdd3SFlorian Fainelli 
9470d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
9480d8bcdd3SFlorian Fainelli 	if (of_phy_is_fixed_link(port_dn)) {
9490d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
9500d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
9510d8bcdd3SFlorian Fainelli 		 */
9520d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
9530d8bcdd3SFlorian Fainelli 		if (ret) {
954d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
9559697f1cdSFlorian Fainelli 			return ret;
9560d8bcdd3SFlorian Fainelli 		}
957ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
9580d8bcdd3SFlorian Fainelli 		phy_dn = port_dn;
9590d8bcdd3SFlorian Fainelli 	}
9600d8bcdd3SFlorian Fainelli 
9616819563eSFlorian Fainelli 	if (ds->drv->get_phy_flags)
9626819563eSFlorian Fainelli 		phy_flags = ds->drv->get_phy_flags(ds, p->port);
9636819563eSFlorian Fainelli 
964cd28a1a9SFlorian Fainelli 	if (phy_dn) {
965d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
966d25b8e74SRussell King 
967cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
968cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
969cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
970cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
971cd28a1a9SFlorian Fainelli 		 */
972d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
973d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
974d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
975d25b8e74SRussell King 			if (ret) {
976d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
977cd28a1a9SFlorian Fainelli 				return ret;
978d25b8e74SRussell King 			}
979cd28a1a9SFlorian Fainelli 		} else {
9800d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
981cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
982cd28a1a9SFlorian Fainelli 						phy_flags,
9830d8bcdd3SFlorian Fainelli 						p->phy_interface);
984cd28a1a9SFlorian Fainelli 		}
985cd28a1a9SFlorian Fainelli 	}
9860d8bcdd3SFlorian Fainelli 
987ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
988ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
989ce31b31cSFlorian Fainelli 
9900d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
9910d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
9920d8bcdd3SFlorian Fainelli 	 */
993b31f65fbSAndrew Lunn 	if (!p->phy) {
994cd28a1a9SFlorian Fainelli 		ret = dsa_slave_phy_connect(p, slave_dev, p->port);
995d25b8e74SRussell King 		if (ret) {
996d25b8e74SRussell King 			netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
997c305c165SFlorian Fainelli 			return ret;
998d25b8e74SRussell King 		}
9990d8bcdd3SFlorian Fainelli 	}
10009697f1cdSFlorian Fainelli 
10012220943aSAndrew Lunn 	phy_attached_info(p->phy);
10022220943aSAndrew Lunn 
10039697f1cdSFlorian Fainelli 	return 0;
1004b31f65fbSAndrew Lunn }
10050d8bcdd3SFlorian Fainelli 
1006448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1007448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1008448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1009448b4482SAndrew Lunn 					    void *_unused)
1010448b4482SAndrew Lunn {
1011448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1012448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1013448b4482SAndrew Lunn }
1014448b4482SAndrew Lunn 
101524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
101624462549SFlorian Fainelli {
101724462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
101824462549SFlorian Fainelli 
101924462549SFlorian Fainelli 	if (p->phy) {
102024462549SFlorian Fainelli 		phy_stop(p->phy);
102124462549SFlorian Fainelli 		p->old_pause = -1;
102224462549SFlorian Fainelli 		p->old_link = -1;
102324462549SFlorian Fainelli 		p->old_duplex = -1;
102424462549SFlorian Fainelli 		phy_suspend(p->phy);
102524462549SFlorian Fainelli 	}
102624462549SFlorian Fainelli 
102724462549SFlorian Fainelli 	return 0;
102824462549SFlorian Fainelli }
102924462549SFlorian Fainelli 
103024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
103124462549SFlorian Fainelli {
103224462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
103324462549SFlorian Fainelli 
103424462549SFlorian Fainelli 	netif_device_attach(slave_dev);
103524462549SFlorian Fainelli 
103624462549SFlorian Fainelli 	if (p->phy) {
103724462549SFlorian Fainelli 		phy_resume(p->phy);
103824462549SFlorian Fainelli 		phy_start(p->phy);
103924462549SFlorian Fainelli 	}
104024462549SFlorian Fainelli 
104124462549SFlorian Fainelli 	return 0;
104224462549SFlorian Fainelli }
104324462549SFlorian Fainelli 
1044d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
104591da11f8SLennert Buytenhek 		     int port, char *name)
104691da11f8SLennert Buytenhek {
1047e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
104891da11f8SLennert Buytenhek 	struct net_device *slave_dev;
104991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
105091da11f8SLennert Buytenhek 	int ret;
105191da11f8SLennert Buytenhek 
1052c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1053c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
105491da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1055d87d6f44SGuenter Roeck 		return -ENOMEM;
105691da11f8SLennert Buytenhek 
105791da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
10587ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
10592fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
10600a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
10613e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
10629d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1063f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1064d442ad4aSStephen Hemminger 
1065448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1066448b4482SAndrew Lunn 				 NULL);
1067448b4482SAndrew Lunn 
106891da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1069bd47497aSFlorian Fainelli 	slave_dev->dev.of_node = ds->pd->port_dn[port];
107091da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
107191da11f8SLennert Buytenhek 
107291da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
107391da11f8SLennert Buytenhek 	p->dev = slave_dev;
107491da11f8SLennert Buytenhek 	p->parent = ds;
107591da11f8SLennert Buytenhek 	p->port = port;
10760d8bcdd3SFlorian Fainelli 
10775075314eSAlexander Duyck 	switch (ds->dst->tag_protocol) {
10785075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA
10795075314eSAlexander Duyck 	case DSA_TAG_PROTO_DSA:
10805075314eSAlexander Duyck 		p->xmit = dsa_netdev_ops.xmit;
10815075314eSAlexander Duyck 		break;
10825075314eSAlexander Duyck #endif
10835075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA
10845075314eSAlexander Duyck 	case DSA_TAG_PROTO_EDSA:
10855075314eSAlexander Duyck 		p->xmit = edsa_netdev_ops.xmit;
10865075314eSAlexander Duyck 		break;
10875075314eSAlexander Duyck #endif
10885075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER
10895075314eSAlexander Duyck 	case DSA_TAG_PROTO_TRAILER:
10905075314eSAlexander Duyck 		p->xmit = trailer_netdev_ops.xmit;
10915075314eSAlexander Duyck 		break;
10925075314eSAlexander Duyck #endif
10935075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM
10945075314eSAlexander Duyck 	case DSA_TAG_PROTO_BRCM:
10955075314eSAlexander Duyck 		p->xmit = brcm_netdev_ops.xmit;
10965075314eSAlexander Duyck 		break;
10975075314eSAlexander Duyck #endif
10985075314eSAlexander Duyck 	default:
10995075314eSAlexander Duyck 		p->xmit	= dsa_slave_notag_xmit;
11005075314eSAlexander Duyck 		break;
11015075314eSAlexander Duyck 	}
11025075314eSAlexander Duyck 
11030d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
11040d8bcdd3SFlorian Fainelli 	p->old_link = -1;
11050d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
11060d8bcdd3SFlorian Fainelli 
1107d87d6f44SGuenter Roeck 	ds->ports[port] = slave_dev;
110891da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
110991da11f8SLennert Buytenhek 	if (ret) {
1110a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1111a2ae6007SJoe Perches 			   ret, slave_dev->name);
1112d87d6f44SGuenter Roeck 		ds->ports[port] = NULL;
111391da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1114d87d6f44SGuenter Roeck 		return ret;
111591da11f8SLennert Buytenhek 	}
111691da11f8SLennert Buytenhek 
111791da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
111891da11f8SLennert Buytenhek 
11190071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
11200071f56eSAndrew Lunn 	if (ret) {
11210071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
112273dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
11230071f56eSAndrew Lunn 		free_netdev(slave_dev);
11240071f56eSAndrew Lunn 		return ret;
11250071f56eSAndrew Lunn 	}
11260071f56eSAndrew Lunn 
1127d87d6f44SGuenter Roeck 	return 0;
112891da11f8SLennert Buytenhek }
1129b73adef6SFlorian Fainelli 
1130cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1131cda5c15bSNeil Armstrong {
1132cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1133cda5c15bSNeil Armstrong 
1134cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1135cda5c15bSNeil Armstrong 	if (p->phy)
1136cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1137cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1138cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1139cda5c15bSNeil Armstrong }
1140cda5c15bSNeil Armstrong 
1141b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1142b73adef6SFlorian Fainelli {
1143b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1144b73adef6SFlorian Fainelli }
1145b73adef6SFlorian Fainelli 
1146*6debb68aSVivien Didelot static int dsa_slave_port_upper_event(struct net_device *dev,
1147*6debb68aSVivien Didelot 				      unsigned long event, void *ptr)
1148b73adef6SFlorian Fainelli {
1149*6debb68aSVivien Didelot 	struct netdev_notifier_changeupper_info *info = ptr;
1150*6debb68aSVivien Didelot 	struct net_device *upper = info->upper_dev;
1151b73adef6SFlorian Fainelli 	int err = 0;
1152b73adef6SFlorian Fainelli 
1153*6debb68aSVivien Didelot 	switch (event) {
1154*6debb68aSVivien Didelot 	case NETDEV_CHANGEUPPER:
1155*6debb68aSVivien Didelot 		if (netif_is_bridge_master(upper)) {
1156*6debb68aSVivien Didelot 			if (info->linking)
1157*6debb68aSVivien Didelot 				err = dsa_slave_bridge_port_join(dev, upper);
1158*6debb68aSVivien Didelot 			else
115916bfa702SVivien Didelot 				dsa_slave_bridge_port_leave(dev);
1160*6debb68aSVivien Didelot 		}
1161b73adef6SFlorian Fainelli 
1162*6debb68aSVivien Didelot 		break;
1163*6debb68aSVivien Didelot 	}
1164*6debb68aSVivien Didelot 
1165*6debb68aSVivien Didelot 	return notifier_from_errno(err);
1166*6debb68aSVivien Didelot }
1167*6debb68aSVivien Didelot 
1168*6debb68aSVivien Didelot static int dsa_slave_port_event(struct net_device *dev, unsigned long event,
1169*6debb68aSVivien Didelot 				void *ptr)
1170*6debb68aSVivien Didelot {
1171*6debb68aSVivien Didelot 	switch (event) {
1172*6debb68aSVivien Didelot 	case NETDEV_CHANGEUPPER:
1173*6debb68aSVivien Didelot 		return dsa_slave_port_upper_event(dev, event, ptr);
1174*6debb68aSVivien Didelot 	}
1175*6debb68aSVivien Didelot 
1176*6debb68aSVivien Didelot 	return NOTIFY_DONE;
1177b73adef6SFlorian Fainelli }
1178b73adef6SFlorian Fainelli 
1179b73adef6SFlorian Fainelli int dsa_slave_netdevice_event(struct notifier_block *unused,
1180b73adef6SFlorian Fainelli 			      unsigned long event, void *ptr)
1181b73adef6SFlorian Fainelli {
1182*6debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1183b73adef6SFlorian Fainelli 
1184*6debb68aSVivien Didelot 	if (dsa_slave_dev_check(dev))
1185*6debb68aSVivien Didelot 		return dsa_slave_port_event(dev, event, ptr);
1186b73adef6SFlorian Fainelli 
1187b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1188b73adef6SFlorian Fainelli }
1189