xref: /openbmc/linux/net/dsa/slave.c (revision fb2dabad69f099fb9c03a44276778911da50ba29)
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 
320*fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
321*fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
322*fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
323*fb2dabadSVivien Didelot {
324*fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
325*fb2dabadSVivien Didelot 	struct dsa_switch *ds = p->parent;
326*fb2dabadSVivien Didelot 
327*fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
328*fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
329*fb2dabadSVivien Didelot 		return 0;
330*fb2dabadSVivien Didelot 
331*fb2dabadSVivien Didelot 	if (ds->drv->port_vlan_filtering)
332*fb2dabadSVivien Didelot 		return ds->drv->port_vlan_filtering(ds, p->port,
333*fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
334*fb2dabadSVivien Didelot 
335*fb2dabadSVivien Didelot 	return 0;
336*fb2dabadSVivien Didelot }
337*fb2dabadSVivien 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;
354*fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
355*fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
356*fb2dabadSVivien 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 
451b73adef6SFlorian Fainelli 	if (ds->drv->port_join_bridge)
452a6692754SVivien Didelot 		ret = ds->drv->port_join_bridge(ds, p->port, br);
453b73adef6SFlorian Fainelli 
454b73adef6SFlorian Fainelli 	return ret;
455b73adef6SFlorian Fainelli }
456b73adef6SFlorian Fainelli 
457b73adef6SFlorian Fainelli static int 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 	int ret = -EOPNOTSUPP;
462b73adef6SFlorian Fainelli 
463b73adef6SFlorian Fainelli 
464b73adef6SFlorian Fainelli 	if (ds->drv->port_leave_bridge)
465a6692754SVivien Didelot 		ret = ds->drv->port_leave_bridge(ds, p->port);
466b73adef6SFlorian Fainelli 
467b73adef6SFlorian Fainelli 	p->bridge_dev = NULL;
468b73adef6SFlorian Fainelli 
469b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
470b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
471b73adef6SFlorian Fainelli 	 */
472b73adef6SFlorian Fainelli 	dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
473b73adef6SFlorian Fainelli 
474b73adef6SFlorian Fainelli 	return ret;
475b73adef6SFlorian Fainelli }
476b73adef6SFlorian Fainelli 
477f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
478f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
479b73adef6SFlorian Fainelli {
480b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
481b73adef6SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
482b73adef6SFlorian Fainelli 
483f8e20a9fSScott Feldman 	switch (attr->id) {
4841f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
48542275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
48642275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
487f8e20a9fSScott Feldman 		break;
488f8e20a9fSScott Feldman 	default:
489f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
490f8e20a9fSScott Feldman 	}
491b73adef6SFlorian Fainelli 
492b73adef6SFlorian Fainelli 	return 0;
493b73adef6SFlorian Fainelli }
494b73adef6SFlorian Fainelli 
49504ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
49604ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
49704ff53f9SFlorian Fainelli {
49804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
49904ff53f9SFlorian Fainelli 	if (p->netpoll)
50004ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
50104ff53f9SFlorian Fainelli #else
50204ff53f9SFlorian Fainelli 	BUG();
50304ff53f9SFlorian Fainelli #endif
50404ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
50504ff53f9SFlorian Fainelli }
50604ff53f9SFlorian Fainelli 
5073e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5083e8a72d1SFlorian Fainelli {
5093e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5104ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5113e8a72d1SFlorian Fainelli 
5124ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
5134ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
5143e8a72d1SFlorian Fainelli 
5154ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
5164ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
5174ed70ce9SFlorian Fainelli 	if (!nskb)
5184ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
5195aed85ceSFlorian Fainelli 
52004ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
52104ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
52204ff53f9SFlorian Fainelli 	 */
52304ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
52404ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
52504ff53f9SFlorian Fainelli 
5264ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
5274ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
5284ed70ce9SFlorian Fainelli 	 */
5294ed70ce9SFlorian Fainelli 	nskb->dev = p->parent->dst->master_netdev;
5304ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
5315aed85ceSFlorian Fainelli 
5325aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
5335aed85ceSFlorian Fainelli }
5345aed85ceSFlorian Fainelli 
5354ed70ce9SFlorian Fainelli static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
5364ed70ce9SFlorian Fainelli 					    struct net_device *dev)
5374ed70ce9SFlorian Fainelli {
5384ed70ce9SFlorian Fainelli 	/* Just return the original SKB */
5394ed70ce9SFlorian Fainelli 	return skb;
5404ed70ce9SFlorian Fainelli }
5414ed70ce9SFlorian Fainelli 
54291da11f8SLennert Buytenhek 
54391da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
54491da11f8SLennert Buytenhek static int
54591da11f8SLennert Buytenhek dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
54691da11f8SLennert Buytenhek {
54791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
54891da11f8SLennert Buytenhek 	int err;
54991da11f8SLennert Buytenhek 
55091da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
55191da11f8SLennert Buytenhek 	if (p->phy != NULL) {
55291da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
55391da11f8SLennert Buytenhek 		if (err == 0)
55491da11f8SLennert Buytenhek 			err = phy_ethtool_gset(p->phy, cmd);
55591da11f8SLennert Buytenhek 	}
55691da11f8SLennert Buytenhek 
55791da11f8SLennert Buytenhek 	return err;
55891da11f8SLennert Buytenhek }
55991da11f8SLennert Buytenhek 
56091da11f8SLennert Buytenhek static int
56191da11f8SLennert Buytenhek dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
56291da11f8SLennert Buytenhek {
56391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
56491da11f8SLennert Buytenhek 
56591da11f8SLennert Buytenhek 	if (p->phy != NULL)
56691da11f8SLennert Buytenhek 		return phy_ethtool_sset(p->phy, cmd);
56791da11f8SLennert Buytenhek 
56891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
56991da11f8SLennert Buytenhek }
57091da11f8SLennert Buytenhek 
57191da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
57291da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
57391da11f8SLennert Buytenhek {
5747826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
5757826d43fSJiri Pirko 	strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
5767826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
5777826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
57891da11f8SLennert Buytenhek }
57991da11f8SLennert Buytenhek 
5803d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
5813d762a0fSGuenter Roeck {
5823d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5833d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
5843d762a0fSGuenter Roeck 
5853d762a0fSGuenter Roeck 	if (ds->drv->get_regs_len)
5863d762a0fSGuenter Roeck 		return ds->drv->get_regs_len(ds, p->port);
5873d762a0fSGuenter Roeck 
5883d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
5893d762a0fSGuenter Roeck }
5903d762a0fSGuenter Roeck 
5913d762a0fSGuenter Roeck static void
5923d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
5933d762a0fSGuenter Roeck {
5943d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
5953d762a0fSGuenter Roeck 	struct dsa_switch *ds = p->parent;
5963d762a0fSGuenter Roeck 
5973d762a0fSGuenter Roeck 	if (ds->drv->get_regs)
5983d762a0fSGuenter Roeck 		ds->drv->get_regs(ds, p->port, regs, _p);
5993d762a0fSGuenter Roeck }
6003d762a0fSGuenter Roeck 
60191da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
60291da11f8SLennert Buytenhek {
60391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
60491da11f8SLennert Buytenhek 
60591da11f8SLennert Buytenhek 	if (p->phy != NULL)
60691da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
60791da11f8SLennert Buytenhek 
60891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
60991da11f8SLennert Buytenhek }
61091da11f8SLennert Buytenhek 
61191da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
61291da11f8SLennert Buytenhek {
61391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
61491da11f8SLennert Buytenhek 
61591da11f8SLennert Buytenhek 	if (p->phy != NULL) {
61691da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
61791da11f8SLennert Buytenhek 		return p->phy->link;
61891da11f8SLennert Buytenhek 	}
61991da11f8SLennert Buytenhek 
62091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
62191da11f8SLennert Buytenhek }
62291da11f8SLennert Buytenhek 
6236793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6246793abb4SGuenter Roeck {
6256793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6266793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6276793abb4SGuenter Roeck 
6286793abb4SGuenter Roeck 	if (ds->pd->eeprom_len)
6296793abb4SGuenter Roeck 		return ds->pd->eeprom_len;
6306793abb4SGuenter Roeck 
6316793abb4SGuenter Roeck 	if (ds->drv->get_eeprom_len)
6326793abb4SGuenter Roeck 		return ds->drv->get_eeprom_len(ds);
6336793abb4SGuenter Roeck 
6346793abb4SGuenter Roeck 	return 0;
6356793abb4SGuenter Roeck }
6366793abb4SGuenter Roeck 
6376793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6386793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6396793abb4SGuenter Roeck {
6406793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6416793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6426793abb4SGuenter Roeck 
6436793abb4SGuenter Roeck 	if (ds->drv->get_eeprom)
6446793abb4SGuenter Roeck 		return ds->drv->get_eeprom(ds, eeprom, data);
6456793abb4SGuenter Roeck 
6466793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6476793abb4SGuenter Roeck }
6486793abb4SGuenter Roeck 
6496793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
6506793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6516793abb4SGuenter Roeck {
6526793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
6536793abb4SGuenter Roeck 	struct dsa_switch *ds = p->parent;
6546793abb4SGuenter Roeck 
6556793abb4SGuenter Roeck 	if (ds->drv->set_eeprom)
6566793abb4SGuenter Roeck 		return ds->drv->set_eeprom(ds, eeprom, data);
6576793abb4SGuenter Roeck 
6586793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6596793abb4SGuenter Roeck }
6606793abb4SGuenter Roeck 
66191da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
66291da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
66391da11f8SLennert Buytenhek {
66491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
66591da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
66691da11f8SLennert Buytenhek 
66791da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
66891da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
66991da11f8SLennert Buytenhek 
67091da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
67191da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
67291da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
67391da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
67491da11f8SLennert Buytenhek 		if (ds->drv->get_strings != NULL)
67591da11f8SLennert Buytenhek 			ds->drv->get_strings(ds, p->port, data + 4 * len);
67691da11f8SLennert Buytenhek 	}
67791da11f8SLennert Buytenhek }
67891da11f8SLennert Buytenhek 
67991da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
68091da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
68191da11f8SLennert Buytenhek 					uint64_t *data)
68291da11f8SLennert Buytenhek {
68391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
68491da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
68591da11f8SLennert Buytenhek 
68691da11f8SLennert Buytenhek 	data[0] = p->dev->stats.tx_packets;
68791da11f8SLennert Buytenhek 	data[1] = p->dev->stats.tx_bytes;
68891da11f8SLennert Buytenhek 	data[2] = p->dev->stats.rx_packets;
68991da11f8SLennert Buytenhek 	data[3] = p->dev->stats.rx_bytes;
69091da11f8SLennert Buytenhek 	if (ds->drv->get_ethtool_stats != NULL)
69191da11f8SLennert Buytenhek 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
69291da11f8SLennert Buytenhek }
69391da11f8SLennert Buytenhek 
69491da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
69591da11f8SLennert Buytenhek {
69691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
69791da11f8SLennert Buytenhek 	struct dsa_switch *ds = p->parent;
69891da11f8SLennert Buytenhek 
69991da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
70091da11f8SLennert Buytenhek 		int count;
70191da11f8SLennert Buytenhek 
70291da11f8SLennert Buytenhek 		count = 4;
70391da11f8SLennert Buytenhek 		if (ds->drv->get_sset_count != NULL)
70491da11f8SLennert Buytenhek 			count += ds->drv->get_sset_count(ds);
70591da11f8SLennert Buytenhek 
70691da11f8SLennert Buytenhek 		return count;
70791da11f8SLennert Buytenhek 	}
70891da11f8SLennert Buytenhek 
70991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
71091da11f8SLennert Buytenhek }
71191da11f8SLennert Buytenhek 
71219e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
71319e57c4eSFlorian Fainelli {
71419e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
71519e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
71619e57c4eSFlorian Fainelli 
71719e57c4eSFlorian Fainelli 	if (ds->drv->get_wol)
71819e57c4eSFlorian Fainelli 		ds->drv->get_wol(ds, p->port, w);
71919e57c4eSFlorian Fainelli }
72019e57c4eSFlorian Fainelli 
72119e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
72219e57c4eSFlorian Fainelli {
72319e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
72419e57c4eSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
72519e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
72619e57c4eSFlorian Fainelli 
72719e57c4eSFlorian Fainelli 	if (ds->drv->set_wol)
72819e57c4eSFlorian Fainelli 		ret = ds->drv->set_wol(ds, p->port, w);
72919e57c4eSFlorian Fainelli 
73019e57c4eSFlorian Fainelli 	return ret;
73119e57c4eSFlorian Fainelli }
73219e57c4eSFlorian Fainelli 
7337905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7347905288fSFlorian Fainelli {
7357905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7367905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
7377905288fSFlorian Fainelli 	int ret;
7387905288fSFlorian Fainelli 
7397905288fSFlorian Fainelli 	if (!ds->drv->set_eee)
7407905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7417905288fSFlorian Fainelli 
7427905288fSFlorian Fainelli 	ret = ds->drv->set_eee(ds, p->port, p->phy, e);
7437905288fSFlorian Fainelli 	if (ret)
7447905288fSFlorian Fainelli 		return ret;
7457905288fSFlorian Fainelli 
7467905288fSFlorian Fainelli 	if (p->phy)
7477905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
7487905288fSFlorian Fainelli 
7497905288fSFlorian Fainelli 	return ret;
7507905288fSFlorian Fainelli }
7517905288fSFlorian Fainelli 
7527905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7537905288fSFlorian Fainelli {
7547905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7557905288fSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
7567905288fSFlorian Fainelli 	int ret;
7577905288fSFlorian Fainelli 
7587905288fSFlorian Fainelli 	if (!ds->drv->get_eee)
7597905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7607905288fSFlorian Fainelli 
7617905288fSFlorian Fainelli 	ret = ds->drv->get_eee(ds, p->port, e);
7627905288fSFlorian Fainelli 	if (ret)
7637905288fSFlorian Fainelli 		return ret;
7647905288fSFlorian Fainelli 
7657905288fSFlorian Fainelli 	if (p->phy)
7667905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
7677905288fSFlorian Fainelli 
7687905288fSFlorian Fainelli 	return ret;
7697905288fSFlorian Fainelli }
7707905288fSFlorian Fainelli 
77104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
77204ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
77304ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
77404ff53f9SFlorian Fainelli {
77504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
77604ff53f9SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
77704ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
77804ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
77904ff53f9SFlorian Fainelli 	int err = 0;
78004ff53f9SFlorian Fainelli 
78104ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
78204ff53f9SFlorian Fainelli 	if (!netpoll)
78304ff53f9SFlorian Fainelli 		return -ENOMEM;
78404ff53f9SFlorian Fainelli 
78504ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
78604ff53f9SFlorian Fainelli 	if (err) {
78704ff53f9SFlorian Fainelli 		kfree(netpoll);
78804ff53f9SFlorian Fainelli 		goto out;
78904ff53f9SFlorian Fainelli 	}
79004ff53f9SFlorian Fainelli 
79104ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
79204ff53f9SFlorian Fainelli out:
79304ff53f9SFlorian Fainelli 	return err;
79404ff53f9SFlorian Fainelli }
79504ff53f9SFlorian Fainelli 
79604ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
79704ff53f9SFlorian Fainelli {
79804ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
79904ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
80004ff53f9SFlorian Fainelli 
80104ff53f9SFlorian Fainelli 	if (!netpoll)
80204ff53f9SFlorian Fainelli 		return;
80304ff53f9SFlorian Fainelli 
80404ff53f9SFlorian Fainelli 	p->netpoll = NULL;
80504ff53f9SFlorian Fainelli 
80604ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
80704ff53f9SFlorian Fainelli }
80804ff53f9SFlorian Fainelli 
80904ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
81004ff53f9SFlorian Fainelli {
81104ff53f9SFlorian Fainelli }
81204ff53f9SFlorian Fainelli #endif
81304ff53f9SFlorian Fainelli 
81491da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
81591da11f8SLennert Buytenhek 	.get_settings		= dsa_slave_get_settings,
81691da11f8SLennert Buytenhek 	.set_settings		= dsa_slave_set_settings,
81791da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
8183d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
8193d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
82091da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
82191da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
8226793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
8236793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
8246793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
82591da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
82691da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
82791da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
82819e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
82919e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
8307905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
8317905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
83291da11f8SLennert Buytenhek };
83391da11f8SLennert Buytenhek 
8343e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
835d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
836d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
8373e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
838d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
839d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
840d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
841ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
842ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
843ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
844d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
845abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
84604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
84704ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
84804ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
84904ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
85004ff53f9SFlorian Fainelli #endif
85111149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
85211149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
85311149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
85498237d43SScott Feldman };
85598237d43SScott Feldman 
8569d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
857f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
85835636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
859ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
860ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
861ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
862d442ad4aSStephen Hemminger };
86391da11f8SLennert Buytenhek 
864f37db85dSFlorian Fainelli static struct device_type dsa_type = {
865f37db85dSFlorian Fainelli 	.name	= "dsa",
866f37db85dSFlorian Fainelli };
867f37db85dSFlorian Fainelli 
8680d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
8690d8bcdd3SFlorian Fainelli {
8700d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
871ec9436baSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
8720d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
8730d8bcdd3SFlorian Fainelli 
8740d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
8750d8bcdd3SFlorian Fainelli 		status_changed = 1;
8760d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
8770d8bcdd3SFlorian Fainelli 	}
8780d8bcdd3SFlorian Fainelli 
8790d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
8800d8bcdd3SFlorian Fainelli 		status_changed = 1;
8810d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
8820d8bcdd3SFlorian Fainelli 	}
8830d8bcdd3SFlorian Fainelli 
8840d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
8850d8bcdd3SFlorian Fainelli 		status_changed = 1;
8860d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
8870d8bcdd3SFlorian Fainelli 	}
8880d8bcdd3SFlorian Fainelli 
889ec9436baSFlorian Fainelli 	if (ds->drv->adjust_link && status_changed)
890ec9436baSFlorian Fainelli 		ds->drv->adjust_link(ds, p->port, p->phy);
891ec9436baSFlorian Fainelli 
8920d8bcdd3SFlorian Fainelli 	if (status_changed)
8930d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
8940d8bcdd3SFlorian Fainelli }
8950d8bcdd3SFlorian Fainelli 
896ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
897ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
898ce31b31cSFlorian Fainelli {
899ce31b31cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
900ce31b31cSFlorian Fainelli 	struct dsa_switch *ds = p->parent;
901ce31b31cSFlorian Fainelli 
902ce31b31cSFlorian Fainelli 	if (ds->drv->fixed_link_update)
903ce31b31cSFlorian Fainelli 		ds->drv->fixed_link_update(ds, p->port, status);
904ce31b31cSFlorian Fainelli 
905ce31b31cSFlorian Fainelli 	return 0;
906ce31b31cSFlorian Fainelli }
907ce31b31cSFlorian Fainelli 
90891da11f8SLennert Buytenhek /* slave device setup *******************************************************/
909c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
910cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
911cd28a1a9SFlorian Fainelli 				 int addr)
912c305c165SFlorian Fainelli {
913c305c165SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
914c305c165SFlorian Fainelli 
9157f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
916d25b8e74SRussell King 	if (!p->phy) {
917d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
918c305c165SFlorian Fainelli 		return -ENODEV;
919d25b8e74SRussell King 	}
920c305c165SFlorian Fainelli 
921c305c165SFlorian Fainelli 	/* Use already configured phy mode */
922211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
923c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
924c305c165SFlorian Fainelli 	phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
925c305c165SFlorian Fainelli 			   p->phy_interface);
926c305c165SFlorian Fainelli 
927c305c165SFlorian Fainelli 	return 0;
928c305c165SFlorian Fainelli }
929c305c165SFlorian Fainelli 
9309697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
9310d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
9320d8bcdd3SFlorian Fainelli {
9330d8bcdd3SFlorian Fainelli 	struct dsa_switch *ds = p->parent;
9340d8bcdd3SFlorian Fainelli 	struct dsa_chip_data *cd = ds->pd;
9350d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
936ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
9376819563eSFlorian Fainelli 	u32 phy_flags = 0;
93819334920SGuenter Roeck 	int mode, ret;
9390d8bcdd3SFlorian Fainelli 
9400d8bcdd3SFlorian Fainelli 	port_dn = cd->port_dn[p->port];
94119334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
94219334920SGuenter Roeck 	if (mode < 0)
94319334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
94419334920SGuenter Roeck 	p->phy_interface = mode;
9450d8bcdd3SFlorian Fainelli 
9460d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
9470d8bcdd3SFlorian Fainelli 	if (of_phy_is_fixed_link(port_dn)) {
9480d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
9490d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
9500d8bcdd3SFlorian Fainelli 		 */
9510d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
9520d8bcdd3SFlorian Fainelli 		if (ret) {
953d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
9549697f1cdSFlorian Fainelli 			return ret;
9550d8bcdd3SFlorian Fainelli 		}
956ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
9570d8bcdd3SFlorian Fainelli 		phy_dn = port_dn;
9580d8bcdd3SFlorian Fainelli 	}
9590d8bcdd3SFlorian Fainelli 
9606819563eSFlorian Fainelli 	if (ds->drv->get_phy_flags)
9616819563eSFlorian Fainelli 		phy_flags = ds->drv->get_phy_flags(ds, p->port);
9626819563eSFlorian Fainelli 
963cd28a1a9SFlorian Fainelli 	if (phy_dn) {
964d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
965d25b8e74SRussell King 
966cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
967cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
968cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
969cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
970cd28a1a9SFlorian Fainelli 		 */
971d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
972d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
973d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
974d25b8e74SRussell King 			if (ret) {
975d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
976cd28a1a9SFlorian Fainelli 				return ret;
977d25b8e74SRussell King 			}
978cd28a1a9SFlorian Fainelli 		} else {
9790d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
980cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
981cd28a1a9SFlorian Fainelli 						phy_flags,
9820d8bcdd3SFlorian Fainelli 						p->phy_interface);
983cd28a1a9SFlorian Fainelli 		}
984cd28a1a9SFlorian Fainelli 	}
9850d8bcdd3SFlorian Fainelli 
986ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
987ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
988ce31b31cSFlorian Fainelli 
9890d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
9900d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
9910d8bcdd3SFlorian Fainelli 	 */
992b31f65fbSAndrew Lunn 	if (!p->phy) {
993cd28a1a9SFlorian Fainelli 		ret = dsa_slave_phy_connect(p, slave_dev, p->port);
994d25b8e74SRussell King 		if (ret) {
995d25b8e74SRussell King 			netdev_err(slave_dev, "failed to connect to port %d: %d\n", p->port, ret);
996c305c165SFlorian Fainelli 			return ret;
997d25b8e74SRussell King 		}
9980d8bcdd3SFlorian Fainelli 	}
9999697f1cdSFlorian Fainelli 
10002220943aSAndrew Lunn 	phy_attached_info(p->phy);
10012220943aSAndrew Lunn 
10029697f1cdSFlorian Fainelli 	return 0;
1003b31f65fbSAndrew Lunn }
10040d8bcdd3SFlorian Fainelli 
1005448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1006448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1007448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1008448b4482SAndrew Lunn 					    void *_unused)
1009448b4482SAndrew Lunn {
1010448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1011448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1012448b4482SAndrew Lunn }
1013448b4482SAndrew Lunn 
101424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
101524462549SFlorian Fainelli {
101624462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
101724462549SFlorian Fainelli 
101824462549SFlorian Fainelli 	if (p->phy) {
101924462549SFlorian Fainelli 		phy_stop(p->phy);
102024462549SFlorian Fainelli 		p->old_pause = -1;
102124462549SFlorian Fainelli 		p->old_link = -1;
102224462549SFlorian Fainelli 		p->old_duplex = -1;
102324462549SFlorian Fainelli 		phy_suspend(p->phy);
102424462549SFlorian Fainelli 	}
102524462549SFlorian Fainelli 
102624462549SFlorian Fainelli 	return 0;
102724462549SFlorian Fainelli }
102824462549SFlorian Fainelli 
102924462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
103024462549SFlorian Fainelli {
103124462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
103224462549SFlorian Fainelli 
103324462549SFlorian Fainelli 	netif_device_attach(slave_dev);
103424462549SFlorian Fainelli 
103524462549SFlorian Fainelli 	if (p->phy) {
103624462549SFlorian Fainelli 		phy_resume(p->phy);
103724462549SFlorian Fainelli 		phy_start(p->phy);
103824462549SFlorian Fainelli 	}
103924462549SFlorian Fainelli 
104024462549SFlorian Fainelli 	return 0;
104124462549SFlorian Fainelli }
104224462549SFlorian Fainelli 
1043d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
104491da11f8SLennert Buytenhek 		     int port, char *name)
104591da11f8SLennert Buytenhek {
1046e84665c9SLennert Buytenhek 	struct net_device *master = ds->dst->master_netdev;
104791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
104891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
104991da11f8SLennert Buytenhek 	int ret;
105091da11f8SLennert Buytenhek 
1051c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1052c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
105391da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1054d87d6f44SGuenter Roeck 		return -ENOMEM;
105591da11f8SLennert Buytenhek 
105691da11f8SLennert Buytenhek 	slave_dev->features = master->vlan_features;
10577ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
10582fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
10590a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
10603e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
10619d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
1062f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1063d442ad4aSStephen Hemminger 
1064448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1065448b4482SAndrew Lunn 				 NULL);
1066448b4482SAndrew Lunn 
106791da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1068bd47497aSFlorian Fainelli 	slave_dev->dev.of_node = ds->pd->port_dn[port];
106991da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
107091da11f8SLennert Buytenhek 
107191da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
107291da11f8SLennert Buytenhek 	p->dev = slave_dev;
107391da11f8SLennert Buytenhek 	p->parent = ds;
107491da11f8SLennert Buytenhek 	p->port = port;
10750d8bcdd3SFlorian Fainelli 
10765075314eSAlexander Duyck 	switch (ds->dst->tag_protocol) {
10775075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_DSA
10785075314eSAlexander Duyck 	case DSA_TAG_PROTO_DSA:
10795075314eSAlexander Duyck 		p->xmit = dsa_netdev_ops.xmit;
10805075314eSAlexander Duyck 		break;
10815075314eSAlexander Duyck #endif
10825075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_EDSA
10835075314eSAlexander Duyck 	case DSA_TAG_PROTO_EDSA:
10845075314eSAlexander Duyck 		p->xmit = edsa_netdev_ops.xmit;
10855075314eSAlexander Duyck 		break;
10865075314eSAlexander Duyck #endif
10875075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_TRAILER
10885075314eSAlexander Duyck 	case DSA_TAG_PROTO_TRAILER:
10895075314eSAlexander Duyck 		p->xmit = trailer_netdev_ops.xmit;
10905075314eSAlexander Duyck 		break;
10915075314eSAlexander Duyck #endif
10925075314eSAlexander Duyck #ifdef CONFIG_NET_DSA_TAG_BRCM
10935075314eSAlexander Duyck 	case DSA_TAG_PROTO_BRCM:
10945075314eSAlexander Duyck 		p->xmit = brcm_netdev_ops.xmit;
10955075314eSAlexander Duyck 		break;
10965075314eSAlexander Duyck #endif
10975075314eSAlexander Duyck 	default:
10985075314eSAlexander Duyck 		p->xmit	= dsa_slave_notag_xmit;
10995075314eSAlexander Duyck 		break;
11005075314eSAlexander Duyck 	}
11015075314eSAlexander Duyck 
11020d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
11030d8bcdd3SFlorian Fainelli 	p->old_link = -1;
11040d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
11050d8bcdd3SFlorian Fainelli 
1106d87d6f44SGuenter Roeck 	ds->ports[port] = slave_dev;
110791da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
110891da11f8SLennert Buytenhek 	if (ret) {
1109a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1110a2ae6007SJoe Perches 			   ret, slave_dev->name);
1111d87d6f44SGuenter Roeck 		ds->ports[port] = NULL;
111291da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1113d87d6f44SGuenter Roeck 		return ret;
111491da11f8SLennert Buytenhek 	}
111591da11f8SLennert Buytenhek 
111691da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
111791da11f8SLennert Buytenhek 
11180071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
11190071f56eSAndrew Lunn 	if (ret) {
11200071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
112173dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
11220071f56eSAndrew Lunn 		free_netdev(slave_dev);
11230071f56eSAndrew Lunn 		return ret;
11240071f56eSAndrew Lunn 	}
11250071f56eSAndrew Lunn 
1126d87d6f44SGuenter Roeck 	return 0;
112791da11f8SLennert Buytenhek }
1128b73adef6SFlorian Fainelli 
1129cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1130cda5c15bSNeil Armstrong {
1131cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1132cda5c15bSNeil Armstrong 
1133cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1134cda5c15bSNeil Armstrong 	if (p->phy)
1135cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1136cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1137cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1138cda5c15bSNeil Armstrong }
1139cda5c15bSNeil Armstrong 
1140b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1141b73adef6SFlorian Fainelli {
1142b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1143b73adef6SFlorian Fainelli }
1144b73adef6SFlorian Fainelli 
1145b73adef6SFlorian Fainelli static int dsa_slave_master_changed(struct net_device *dev)
1146b73adef6SFlorian Fainelli {
1147b73adef6SFlorian Fainelli 	struct net_device *master = netdev_master_upper_dev_get(dev);
1148b06b107aSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
1149b73adef6SFlorian Fainelli 	int err = 0;
1150b73adef6SFlorian Fainelli 
1151b73adef6SFlorian Fainelli 	if (master && master->rtnl_link_ops &&
1152b73adef6SFlorian Fainelli 	    !strcmp(master->rtnl_link_ops->kind, "bridge"))
1153b73adef6SFlorian Fainelli 		err = dsa_slave_bridge_port_join(dev, master);
1154b06b107aSGuenter Roeck 	else if (dsa_port_is_bridged(p))
1155b73adef6SFlorian Fainelli 		err = dsa_slave_bridge_port_leave(dev);
1156b73adef6SFlorian Fainelli 
1157b73adef6SFlorian Fainelli 	return err;
1158b73adef6SFlorian Fainelli }
1159b73adef6SFlorian Fainelli 
1160b73adef6SFlorian Fainelli int dsa_slave_netdevice_event(struct notifier_block *unused,
1161b73adef6SFlorian Fainelli 			      unsigned long event, void *ptr)
1162b73adef6SFlorian Fainelli {
1163b73adef6SFlorian Fainelli 	struct net_device *dev;
1164b73adef6SFlorian Fainelli 	int err = 0;
1165b73adef6SFlorian Fainelli 
1166b73adef6SFlorian Fainelli 	switch (event) {
1167b73adef6SFlorian Fainelli 	case NETDEV_CHANGEUPPER:
1168b73adef6SFlorian Fainelli 		dev = netdev_notifier_info_to_dev(ptr);
1169b73adef6SFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
1170b73adef6SFlorian Fainelli 			goto out;
1171b73adef6SFlorian Fainelli 
1172b73adef6SFlorian Fainelli 		err = dsa_slave_master_changed(dev);
1173efd29b3dSVivien Didelot 		if (err && err != -EOPNOTSUPP)
1174b73adef6SFlorian Fainelli 			netdev_warn(dev, "failed to reflect master change\n");
1175b73adef6SFlorian Fainelli 
1176b73adef6SFlorian Fainelli 		break;
1177b73adef6SFlorian Fainelli 	}
1178b73adef6SFlorian Fainelli 
1179b73adef6SFlorian Fainelli out:
1180b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1181b73adef6SFlorian Fainelli }
1182