xref: /openbmc/linux/net/dsa/slave.c (revision 8b0d3ea555876533b6aa61479335be2c9bdb47e7)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #include <linux/list.h>
12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
13b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1491da11f8SLennert Buytenhek #include <linux/phy.h>
15a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
160d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
170d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
187f854420SAndrew Lunn #include <linux/mdio.h>
19f50f2127SFlorian Fainelli #include <linux/list.h>
20c6e970a0SAndrew Lunn #include <net/dsa.h>
21b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
2298237d43SScott Feldman #include <net/switchdev.h>
23f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
24f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
25b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2604ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2791da11f8SLennert Buytenhek #include "dsa_priv.h"
2891da11f8SLennert Buytenhek 
29f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev);
30f50f2127SFlorian Fainelli 
3104d3a4c6SVivien Didelot static int dsa_slave_notify(struct net_device *dev, unsigned long e, void *v)
3204d3a4c6SVivien Didelot {
3304d3a4c6SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
3404d3a4c6SVivien Didelot 	struct raw_notifier_head *nh = &p->dp->ds->dst->nh;
3504d3a4c6SVivien Didelot 	int err;
3604d3a4c6SVivien Didelot 
3704d3a4c6SVivien Didelot 	err = raw_notifier_call_chain(nh, e, v);
3804d3a4c6SVivien Didelot 
3904d3a4c6SVivien Didelot 	return notifier_to_errno(err);
4004d3a4c6SVivien Didelot }
4104d3a4c6SVivien Didelot 
4291da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
4391da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
4491da11f8SLennert Buytenhek {
4591da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4691da11f8SLennert Buytenhek 
470d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
489d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
4991da11f8SLennert Buytenhek 
5091da11f8SLennert Buytenhek 	return 0xffff;
5191da11f8SLennert Buytenhek }
5291da11f8SLennert Buytenhek 
5391da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
5491da11f8SLennert Buytenhek {
5591da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
5691da11f8SLennert Buytenhek 
570d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
589d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
5991da11f8SLennert Buytenhek 
6091da11f8SLennert Buytenhek 	return 0;
6191da11f8SLennert Buytenhek }
6291da11f8SLennert Buytenhek 
6391da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
6491da11f8SLennert Buytenhek {
6591da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
6691da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
6791da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
6891da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
690b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
700b7b498dSFlorian Fainelli 		 ds->dst->tree, ds->index);
71c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
7224df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
7391da11f8SLennert Buytenhek }
7491da11f8SLennert Buytenhek 
7591da11f8SLennert Buytenhek 
7691da11f8SLennert Buytenhek /* slave device handling ****************************************************/
77abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
78c0840801SLennert Buytenhek {
79c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
80c0840801SLennert Buytenhek 
81afdcf151SVivien Didelot 	return p->dp->ds->dst->master_netdev->ifindex;
82c0840801SLennert Buytenhek }
83c0840801SLennert Buytenhek 
84a5e9a02eSVivien Didelot static inline bool dsa_port_is_bridged(struct dsa_port *dp)
85b73adef6SFlorian Fainelli {
86a5e9a02eSVivien Didelot 	return !!dp->bridge_dev;
87b73adef6SFlorian Fainelli }
88b73adef6SFlorian Fainelli 
89c5d35cb3SVivien Didelot static void dsa_slave_set_state(struct net_device *dev, u8 state)
904acfee81SVivien Didelot {
91c5d35cb3SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
92c5d35cb3SVivien Didelot 	struct dsa_port *dp = p->dp;
93c5d35cb3SVivien Didelot 	struct dsa_switch *ds = dp->ds;
94c5d35cb3SVivien Didelot 	int port = dp->index;
95732f794cSVivien Didelot 
964acfee81SVivien Didelot 	if (ds->ops->port_stp_state_set)
974acfee81SVivien Didelot 		ds->ops->port_stp_state_set(ds, port, state);
98732f794cSVivien Didelot 
99732f794cSVivien Didelot 	if (ds->ops->port_fast_age) {
100732f794cSVivien Didelot 		/* Fast age FDB entries or flush appropriate forwarding database
101732f794cSVivien Didelot 		 * for the given port, if we are moving it from Learning or
102732f794cSVivien Didelot 		 * Forwarding state, to Disabled or Blocking or Listening state.
103732f794cSVivien Didelot 		 */
104732f794cSVivien Didelot 
105732f794cSVivien Didelot 		if ((dp->stp_state == BR_STATE_LEARNING ||
106732f794cSVivien Didelot 		     dp->stp_state == BR_STATE_FORWARDING) &&
107732f794cSVivien Didelot 		    (state == BR_STATE_DISABLED ||
108732f794cSVivien Didelot 		     state == BR_STATE_BLOCKING ||
109732f794cSVivien Didelot 		     state == BR_STATE_LISTENING))
110732f794cSVivien Didelot 			ds->ops->port_fast_age(ds, port);
111732f794cSVivien Didelot 	}
112732f794cSVivien Didelot 
113732f794cSVivien Didelot 	dp->stp_state = state;
1144acfee81SVivien Didelot }
1154acfee81SVivien Didelot 
11691da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
11791da11f8SLennert Buytenhek {
118df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
119afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
120afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
121a5e9a02eSVivien Didelot 	u8 stp_state = dsa_port_is_bridged(p->dp) ?
122b73adef6SFlorian Fainelli 			BR_STATE_BLOCKING : BR_STATE_FORWARDING;
123df02c6ffSLennert Buytenhek 	int err;
124df02c6ffSLennert Buytenhek 
125df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
126df02c6ffSLennert Buytenhek 		return -ENETDOWN;
127df02c6ffSLennert Buytenhek 
1288feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
129a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
130df02c6ffSLennert Buytenhek 		if (err < 0)
131df02c6ffSLennert Buytenhek 			goto out;
132df02c6ffSLennert Buytenhek 	}
133df02c6ffSLennert Buytenhek 
134df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
135df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
136df02c6ffSLennert Buytenhek 		if (err < 0)
137df02c6ffSLennert Buytenhek 			goto del_unicast;
138df02c6ffSLennert Buytenhek 	}
139df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
140df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
141df02c6ffSLennert Buytenhek 		if (err < 0)
142df02c6ffSLennert Buytenhek 			goto clear_allmulti;
143df02c6ffSLennert Buytenhek 	}
144df02c6ffSLennert Buytenhek 
1459d490b4eSVivien Didelot 	if (ds->ops->port_enable) {
146afdcf151SVivien Didelot 		err = ds->ops->port_enable(ds, p->dp->index, p->phy);
147b2f2af21SFlorian Fainelli 		if (err)
148b2f2af21SFlorian Fainelli 			goto clear_promisc;
149b2f2af21SFlorian Fainelli 	}
150b2f2af21SFlorian Fainelli 
151c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, stp_state);
152b73adef6SFlorian Fainelli 
153f7f1de51SFlorian Fainelli 	if (p->phy)
154f7f1de51SFlorian Fainelli 		phy_start(p->phy);
155f7f1de51SFlorian Fainelli 
15691da11f8SLennert Buytenhek 	return 0;
157df02c6ffSLennert Buytenhek 
158b2f2af21SFlorian Fainelli clear_promisc:
159b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1604fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
161df02c6ffSLennert Buytenhek clear_allmulti:
162df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
163df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
164df02c6ffSLennert Buytenhek del_unicast:
1658feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
166a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
167df02c6ffSLennert Buytenhek out:
168df02c6ffSLennert Buytenhek 	return err;
16991da11f8SLennert Buytenhek }
17091da11f8SLennert Buytenhek 
17191da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
17291da11f8SLennert Buytenhek {
173df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
174afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
175afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
176df02c6ffSLennert Buytenhek 
177f7f1de51SFlorian Fainelli 	if (p->phy)
178f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
179f7f1de51SFlorian Fainelli 
180df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
181a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
182df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
183df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
184df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
185df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
186df02c6ffSLennert Buytenhek 
1878feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
188a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
189df02c6ffSLennert Buytenhek 
1909d490b4eSVivien Didelot 	if (ds->ops->port_disable)
191afdcf151SVivien Didelot 		ds->ops->port_disable(ds, p->dp->index, p->phy);
192b2f2af21SFlorian Fainelli 
193c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, BR_STATE_DISABLED);
194b73adef6SFlorian Fainelli 
19591da11f8SLennert Buytenhek 	return 0;
19691da11f8SLennert Buytenhek }
19791da11f8SLennert Buytenhek 
19891da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
19991da11f8SLennert Buytenhek {
20091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
201afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
20291da11f8SLennert Buytenhek 
20391da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
20491da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
20591da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
20691da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
20791da11f8SLennert Buytenhek }
20891da11f8SLennert Buytenhek 
20991da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
21091da11f8SLennert Buytenhek {
21191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
212afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
21391da11f8SLennert Buytenhek 
21491da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
215a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
21691da11f8SLennert Buytenhek }
21791da11f8SLennert Buytenhek 
218df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
21991da11f8SLennert Buytenhek {
220df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
221afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
222df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
223df02c6ffSLennert Buytenhek 	int err;
224df02c6ffSLennert Buytenhek 
225df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
226df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
227df02c6ffSLennert Buytenhek 
228df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
229df02c6ffSLennert Buytenhek 		goto out;
230df02c6ffSLennert Buytenhek 
2318feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
232a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
233df02c6ffSLennert Buytenhek 		if (err < 0)
234df02c6ffSLennert Buytenhek 			return err;
235df02c6ffSLennert Buytenhek 	}
236df02c6ffSLennert Buytenhek 
2378feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
238a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
239df02c6ffSLennert Buytenhek 
240df02c6ffSLennert Buytenhek out:
241d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
24291da11f8SLennert Buytenhek 
24391da11f8SLennert Buytenhek 	return 0;
24491da11f8SLennert Buytenhek }
24591da11f8SLennert Buytenhek 
24611149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev,
2478f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan,
248f8db8348SJiri Pirko 				   struct switchdev_trans *trans)
24911149536SVivien Didelot {
25011149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
251afdcf151SVivien Didelot 	struct dsa_port *dp = p->dp;
252afdcf151SVivien Didelot 	struct dsa_switch *ds = dp->ds;
25311149536SVivien Didelot 
25479a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans)) {
2559d490b4eSVivien Didelot 		if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
25611149536SVivien Didelot 			return -EOPNOTSUPP;
25711149536SVivien Didelot 
258afdcf151SVivien Didelot 		return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
25911149536SVivien Didelot 	}
26011149536SVivien Didelot 
261afdcf151SVivien Didelot 	ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
2624d5770b3SVivien Didelot 
26311149536SVivien Didelot 	return 0;
26411149536SVivien Didelot }
26511149536SVivien Didelot 
26611149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev,
2678f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan)
26811149536SVivien Didelot {
26911149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
270afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
27111149536SVivien Didelot 
2729d490b4eSVivien Didelot 	if (!ds->ops->port_vlan_del)
27311149536SVivien Didelot 		return -EOPNOTSUPP;
27411149536SVivien Didelot 
275afdcf151SVivien Didelot 	return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
27611149536SVivien Didelot }
27711149536SVivien Didelot 
27811149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev,
2798f24f309SJiri Pirko 				    struct switchdev_obj_port_vlan *vlan,
280648b4a99SJiri Pirko 				    switchdev_obj_dump_cb_t *cb)
28111149536SVivien Didelot {
28211149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
283afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
28411149536SVivien Didelot 
2859d490b4eSVivien Didelot 	if (ds->ops->port_vlan_dump)
286afdcf151SVivien Didelot 		return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
28765aebfc0SVivien Didelot 
28811149536SVivien Didelot 	return -EOPNOTSUPP;
28911149536SVivien Didelot }
29011149536SVivien Didelot 
291ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev,
29252ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb,
293f8db8348SJiri Pirko 				  struct switchdev_trans *trans)
294cdf09697SDavid S. Miller {
295cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
296afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
297146a3206SVivien Didelot 
2988497aa61SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
2999d490b4eSVivien Didelot 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
300146a3206SVivien Didelot 			return -EOPNOTSUPP;
301cdf09697SDavid S. Miller 
302afdcf151SVivien Didelot 		return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
3038497aa61SVivien Didelot 	}
304cdf09697SDavid S. Miller 
305afdcf151SVivien Didelot 	ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
3068497aa61SVivien Didelot 
3078497aa61SVivien Didelot 	return 0;
308cdf09697SDavid S. Miller }
309cdf09697SDavid S. Miller 
310ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev,
31152ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb)
312cdf09697SDavid S. Miller {
313cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
314afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
315cdf09697SDavid S. Miller 	int ret = -EOPNOTSUPP;
316cdf09697SDavid S. Miller 
3179d490b4eSVivien Didelot 	if (ds->ops->port_fdb_del)
318afdcf151SVivien Didelot 		ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
319cdf09697SDavid S. Miller 
320cdf09697SDavid S. Miller 	return ret;
321cdf09697SDavid S. Miller }
322cdf09697SDavid S. Miller 
323ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev,
32452ba57cfSJiri Pirko 				   struct switchdev_obj_port_fdb *fdb,
325648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
326cdf09697SDavid S. Miller {
327cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
328afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
329cdf09697SDavid S. Miller 
3309d490b4eSVivien Didelot 	if (ds->ops->port_fdb_dump)
331afdcf151SVivien Didelot 		return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
332ea70ba98SVivien Didelot 
333cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
334cdf09697SDavid S. Miller }
335cdf09697SDavid S. Miller 
3368df30255SVivien Didelot static int dsa_slave_port_mdb_add(struct net_device *dev,
3378df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb,
3388df30255SVivien Didelot 				  struct switchdev_trans *trans)
3398df30255SVivien Didelot {
3408df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
341afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3428df30255SVivien Didelot 
3438df30255SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3448df30255SVivien Didelot 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
3458df30255SVivien Didelot 			return -EOPNOTSUPP;
3468df30255SVivien Didelot 
347afdcf151SVivien Didelot 		return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
3488df30255SVivien Didelot 	}
3498df30255SVivien Didelot 
350afdcf151SVivien Didelot 	ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
3518df30255SVivien Didelot 
3528df30255SVivien Didelot 	return 0;
3538df30255SVivien Didelot }
3548df30255SVivien Didelot 
3558df30255SVivien Didelot static int dsa_slave_port_mdb_del(struct net_device *dev,
3568df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb)
3578df30255SVivien Didelot {
3588df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
359afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3608df30255SVivien Didelot 
3618df30255SVivien Didelot 	if (ds->ops->port_mdb_del)
362afdcf151SVivien Didelot 		return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
3638df30255SVivien Didelot 
3648df30255SVivien Didelot 	return -EOPNOTSUPP;
3658df30255SVivien Didelot }
3668df30255SVivien Didelot 
3678df30255SVivien Didelot static int dsa_slave_port_mdb_dump(struct net_device *dev,
3688df30255SVivien Didelot 				   struct switchdev_obj_port_mdb *mdb,
3698df30255SVivien Didelot 				   switchdev_obj_dump_cb_t *cb)
3708df30255SVivien Didelot {
3718df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
372afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3738df30255SVivien Didelot 
3748df30255SVivien Didelot 	if (ds->ops->port_mdb_dump)
375afdcf151SVivien Didelot 		return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
3768df30255SVivien Didelot 
3778df30255SVivien Didelot 	return -EOPNOTSUPP;
3788df30255SVivien Didelot }
3798df30255SVivien Didelot 
38091da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
38191da11f8SLennert Buytenhek {
38291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
38391da11f8SLennert Buytenhek 
38491da11f8SLennert Buytenhek 	if (p->phy != NULL)
38528b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
38691da11f8SLennert Buytenhek 
38791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
38891da11f8SLennert Buytenhek }
38991da11f8SLennert Buytenhek 
39043c44a9fSVivien Didelot static int dsa_slave_stp_state_set(struct net_device *dev,
39143c44a9fSVivien Didelot 				   const struct switchdev_attr *attr,
39243c44a9fSVivien Didelot 				   struct switchdev_trans *trans)
393b73adef6SFlorian Fainelli {
394b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
395afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
396b73adef6SFlorian Fainelli 
39743c44a9fSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
3989d490b4eSVivien Didelot 		return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
399b73adef6SFlorian Fainelli 
400c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, attr->u.stp_state);
40143c44a9fSVivien Didelot 
40243c44a9fSVivien Didelot 	return 0;
403b73adef6SFlorian Fainelli }
404b73adef6SFlorian Fainelli 
405fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
406fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
407fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
408fb2dabadSVivien Didelot {
409fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
410afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
411fb2dabadSVivien Didelot 
412fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
413fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
414fb2dabadSVivien Didelot 		return 0;
415fb2dabadSVivien Didelot 
4169d490b4eSVivien Didelot 	if (ds->ops->port_vlan_filtering)
417afdcf151SVivien Didelot 		return ds->ops->port_vlan_filtering(ds, p->dp->index,
418fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
419fb2dabadSVivien Didelot 
420fb2dabadSVivien Didelot 	return 0;
421fb2dabadSVivien Didelot }
422fb2dabadSVivien Didelot 
423e893de1bSVivien Didelot static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
42434a79f63SVivien Didelot 					    unsigned int ageing_time)
42534a79f63SVivien Didelot {
42634a79f63SVivien Didelot 	int i;
42734a79f63SVivien Didelot 
42826895e29SVivien Didelot 	for (i = 0; i < ds->num_ports; ++i) {
42934a79f63SVivien Didelot 		struct dsa_port *dp = &ds->ports[i];
43034a79f63SVivien Didelot 
43134a79f63SVivien Didelot 		if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
43234a79f63SVivien Didelot 			ageing_time = dp->ageing_time;
43334a79f63SVivien Didelot 	}
43434a79f63SVivien Didelot 
43534a79f63SVivien Didelot 	return ageing_time;
43634a79f63SVivien Didelot }
43734a79f63SVivien Didelot 
43834a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev,
43934a79f63SVivien Didelot 				 const struct switchdev_attr *attr,
44034a79f63SVivien Didelot 				 struct switchdev_trans *trans)
44134a79f63SVivien Didelot {
44234a79f63SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
443afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
44434a79f63SVivien Didelot 	unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
44534a79f63SVivien Didelot 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
44634a79f63SVivien Didelot 
4470f3da6afSVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
4480f3da6afSVivien Didelot 		if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
4490f3da6afSVivien Didelot 			return -ERANGE;
4500f3da6afSVivien Didelot 		if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
4510f3da6afSVivien Didelot 			return -ERANGE;
45234a79f63SVivien Didelot 		return 0;
4530f3da6afSVivien Didelot 	}
45434a79f63SVivien Didelot 
45534a79f63SVivien Didelot 	/* Keep the fastest ageing time in case of multiple bridges */
456afdcf151SVivien Didelot 	p->dp->ageing_time = ageing_time;
45734a79f63SVivien Didelot 	ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
45834a79f63SVivien Didelot 
4599d490b4eSVivien Didelot 	if (ds->ops->set_ageing_time)
4609d490b4eSVivien Didelot 		return ds->ops->set_ageing_time(ds, ageing_time);
46134a79f63SVivien Didelot 
46234a79f63SVivien Didelot 	return 0;
46334a79f63SVivien Didelot }
46434a79f63SVivien Didelot 
46535636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
466f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
4677ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
46835636062SScott Feldman {
469b8d866acSVivien Didelot 	int ret;
47035636062SScott Feldman 
47135636062SScott Feldman 	switch (attr->id) {
4721f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
47343c44a9fSVivien Didelot 		ret = dsa_slave_stp_state_set(dev, attr, trans);
47435636062SScott Feldman 		break;
475fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
476fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
477fb2dabadSVivien Didelot 		break;
47834a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
47934a79f63SVivien Didelot 		ret = dsa_slave_ageing_time(dev, attr, trans);
48034a79f63SVivien Didelot 		break;
48135636062SScott Feldman 	default:
48235636062SScott Feldman 		ret = -EOPNOTSUPP;
48335636062SScott Feldman 		break;
48435636062SScott Feldman 	}
48535636062SScott Feldman 
48635636062SScott Feldman 	return ret;
48735636062SScott Feldman }
48835636062SScott Feldman 
489ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
490648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
4917ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
492ba14d9ebSVivien Didelot {
493ba14d9ebSVivien Didelot 	int err;
494ba14d9ebSVivien Didelot 
495ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
496ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
497ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
498ba14d9ebSVivien Didelot 	 */
499ba14d9ebSVivien Didelot 
5009e8f4a54SJiri Pirko 	switch (obj->id) {
50157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
502648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_add(dev,
503648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj),
504648b4a99SJiri Pirko 					     trans);
505ba14d9ebSVivien Didelot 		break;
5068df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5078df30255SVivien Didelot 		err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5088df30255SVivien Didelot 					     trans);
5098df30255SVivien Didelot 		break;
51057d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
511648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
512648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
513648b4a99SJiri Pirko 					      trans);
51411149536SVivien Didelot 		break;
515ba14d9ebSVivien Didelot 	default:
516ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
517ba14d9ebSVivien Didelot 		break;
518ba14d9ebSVivien Didelot 	}
519ba14d9ebSVivien Didelot 
520ba14d9ebSVivien Didelot 	return err;
521ba14d9ebSVivien Didelot }
522ba14d9ebSVivien Didelot 
523ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
524648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
525ba14d9ebSVivien Didelot {
526ba14d9ebSVivien Didelot 	int err;
527ba14d9ebSVivien Didelot 
5289e8f4a54SJiri Pirko 	switch (obj->id) {
52957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
530648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_del(dev,
531648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj));
532ba14d9ebSVivien Didelot 		break;
5338df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5348df30255SVivien Didelot 		err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
5358df30255SVivien Didelot 		break;
53657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
537648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
538648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
53911149536SVivien Didelot 		break;
540ba14d9ebSVivien Didelot 	default:
541ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
542ba14d9ebSVivien Didelot 		break;
543ba14d9ebSVivien Didelot 	}
544ba14d9ebSVivien Didelot 
545ba14d9ebSVivien Didelot 	return err;
546ba14d9ebSVivien Didelot }
547ba14d9ebSVivien Didelot 
548ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
549648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
550648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
551ba14d9ebSVivien Didelot {
552ba14d9ebSVivien Didelot 	int err;
553ba14d9ebSVivien Didelot 
5549e8f4a54SJiri Pirko 	switch (obj->id) {
55557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
556648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_dump(dev,
557648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_FDB(obj),
558648b4a99SJiri Pirko 					      cb);
559ba14d9ebSVivien Didelot 		break;
5608df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5618df30255SVivien Didelot 		err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5628df30255SVivien Didelot 					      cb);
5638df30255SVivien Didelot 		break;
56457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
565648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
566648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
567648b4a99SJiri Pirko 					       cb);
56811149536SVivien Didelot 		break;
569ba14d9ebSVivien Didelot 	default:
570ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
571ba14d9ebSVivien Didelot 		break;
572ba14d9ebSVivien Didelot 	}
573ba14d9ebSVivien Didelot 
574ba14d9ebSVivien Didelot 	return err;
575ba14d9ebSVivien Didelot }
576ba14d9ebSVivien Didelot 
577b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
578b73adef6SFlorian Fainelli 				      struct net_device *br)
579b73adef6SFlorian Fainelli {
580b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
58104d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
58204d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
58304d3a4c6SVivien Didelot 		.port = p->dp->index,
58404d3a4c6SVivien Didelot 		.br = br,
58504d3a4c6SVivien Didelot 	};
58604d3a4c6SVivien Didelot 	int err;
587b73adef6SFlorian Fainelli 
5889c265426SVivien Didelot 	/* Here the port is already bridged. Reflect the current configuration
5899c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
5909c265426SVivien Didelot 	 */
591a5e9a02eSVivien Didelot 	p->dp->bridge_dev = br;
592b73adef6SFlorian Fainelli 
59304d3a4c6SVivien Didelot 	err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_JOIN, &info);
594b73adef6SFlorian Fainelli 
5959c265426SVivien Didelot 	/* The bridging is rolled back on error */
59604d3a4c6SVivien Didelot 	if (err)
5979c265426SVivien Didelot 		p->dp->bridge_dev = NULL;
5989c265426SVivien Didelot 
59904d3a4c6SVivien Didelot 	return err;
600b73adef6SFlorian Fainelli }
601b73adef6SFlorian Fainelli 
602f123f2fbSVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev,
603f123f2fbSVivien Didelot 					struct net_device *br)
604b73adef6SFlorian Fainelli {
605b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
60604d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
60704d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
60804d3a4c6SVivien Didelot 		.port = p->dp->index,
60904d3a4c6SVivien Didelot 		.br = br,
61004d3a4c6SVivien Didelot 	};
61104d3a4c6SVivien Didelot 	int err;
612b73adef6SFlorian Fainelli 
6139c265426SVivien Didelot 	/* Here the port is already unbridged. Reflect the current configuration
6149c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
6159c265426SVivien Didelot 	 */
616f123f2fbSVivien Didelot 	p->dp->bridge_dev = NULL;
617b73adef6SFlorian Fainelli 
61804d3a4c6SVivien Didelot 	err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
61904d3a4c6SVivien Didelot 	if (err)
62004d3a4c6SVivien Didelot 		netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
621b73adef6SFlorian Fainelli 
622b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
623b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
624b73adef6SFlorian Fainelli 	 */
625c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, BR_STATE_FORWARDING);
626b73adef6SFlorian Fainelli }
627b73adef6SFlorian Fainelli 
628f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
629f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
630b73adef6SFlorian Fainelli {
631b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
632afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
633b73adef6SFlorian Fainelli 
634f8e20a9fSScott Feldman 	switch (attr->id) {
6351f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
63642275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
63742275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
638f8e20a9fSScott Feldman 		break;
639f8e20a9fSScott Feldman 	default:
640f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
641f8e20a9fSScott Feldman 	}
642b73adef6SFlorian Fainelli 
643b73adef6SFlorian Fainelli 	return 0;
644b73adef6SFlorian Fainelli }
645b73adef6SFlorian Fainelli 
64604ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
64704ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
64804ff53f9SFlorian Fainelli {
64904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
65004ff53f9SFlorian Fainelli 	if (p->netpoll)
65104ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
65204ff53f9SFlorian Fainelli #else
65304ff53f9SFlorian Fainelli 	BUG();
65404ff53f9SFlorian Fainelli #endif
65504ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
65604ff53f9SFlorian Fainelli }
65704ff53f9SFlorian Fainelli 
6583e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6593e8a72d1SFlorian Fainelli {
6603e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6614ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6623e8a72d1SFlorian Fainelli 
6634ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
6644ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
6653e8a72d1SFlorian Fainelli 
6664ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
6674ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
6684ed70ce9SFlorian Fainelli 	if (!nskb)
6694ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
6705aed85ceSFlorian Fainelli 
67104ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
67204ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
67304ff53f9SFlorian Fainelli 	 */
67404ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
67504ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
67604ff53f9SFlorian Fainelli 
6774ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
6784ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
6794ed70ce9SFlorian Fainelli 	 */
680afdcf151SVivien Didelot 	nskb->dev = p->dp->ds->dst->master_netdev;
6814ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
6825aed85ceSFlorian Fainelli 
6835aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
6845aed85ceSFlorian Fainelli }
6855aed85ceSFlorian Fainelli 
68691da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
68791da11f8SLennert Buytenhek static int
688bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
689bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
69091da11f8SLennert Buytenhek {
69191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
692e69e4626SFlorian Fainelli 	int err = -EOPNOTSUPP;
69391da11f8SLennert Buytenhek 
694e69e4626SFlorian Fainelli 	if (p->phy != NULL)
695bb10bb3eSPhilippe Reynes 		err = phy_ethtool_ksettings_get(p->phy, cmd);
69691da11f8SLennert Buytenhek 
69791da11f8SLennert Buytenhek 	return err;
69891da11f8SLennert Buytenhek }
69991da11f8SLennert Buytenhek 
70091da11f8SLennert Buytenhek static int
701bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
702bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
70391da11f8SLennert Buytenhek {
70491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
70591da11f8SLennert Buytenhek 
70691da11f8SLennert Buytenhek 	if (p->phy != NULL)
707bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
70891da11f8SLennert Buytenhek 
70991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
71091da11f8SLennert Buytenhek }
71191da11f8SLennert Buytenhek 
71291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
71391da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
71491da11f8SLennert Buytenhek {
7157826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
7167826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
7177826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
71891da11f8SLennert Buytenhek }
71991da11f8SLennert Buytenhek 
7203d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
7213d762a0fSGuenter Roeck {
7223d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
723afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7243d762a0fSGuenter Roeck 
7259d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
726afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
7273d762a0fSGuenter Roeck 
7283d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
7293d762a0fSGuenter Roeck }
7303d762a0fSGuenter Roeck 
7313d762a0fSGuenter Roeck static void
7323d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
7333d762a0fSGuenter Roeck {
7343d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
735afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7363d762a0fSGuenter Roeck 
7379d490b4eSVivien Didelot 	if (ds->ops->get_regs)
738afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
7393d762a0fSGuenter Roeck }
7403d762a0fSGuenter Roeck 
74191da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
74291da11f8SLennert Buytenhek {
74391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
74491da11f8SLennert Buytenhek 
74591da11f8SLennert Buytenhek 	if (p->phy != NULL)
74691da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
74791da11f8SLennert Buytenhek 
74891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
74991da11f8SLennert Buytenhek }
75091da11f8SLennert Buytenhek 
75191da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
75291da11f8SLennert Buytenhek {
75391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
75491da11f8SLennert Buytenhek 
75591da11f8SLennert Buytenhek 	if (p->phy != NULL) {
75691da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
75791da11f8SLennert Buytenhek 		return p->phy->link;
75891da11f8SLennert Buytenhek 	}
75991da11f8SLennert Buytenhek 
76091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
76191da11f8SLennert Buytenhek }
76291da11f8SLennert Buytenhek 
7636793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
7646793abb4SGuenter Roeck {
7656793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
766afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7676793abb4SGuenter Roeck 
7680e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
769ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
7706793abb4SGuenter Roeck 
7719d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7729d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7736793abb4SGuenter Roeck 
7746793abb4SGuenter Roeck 	return 0;
7756793abb4SGuenter Roeck }
7766793abb4SGuenter Roeck 
7776793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7786793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7796793abb4SGuenter Roeck {
7806793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
781afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7826793abb4SGuenter Roeck 
7839d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7849d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7856793abb4SGuenter Roeck 
7866793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7876793abb4SGuenter Roeck }
7886793abb4SGuenter Roeck 
7896793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7906793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7916793abb4SGuenter Roeck {
7926793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
793afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7946793abb4SGuenter Roeck 
7959d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7969d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7976793abb4SGuenter Roeck 
7986793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7996793abb4SGuenter Roeck }
8006793abb4SGuenter Roeck 
80191da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
80291da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
80391da11f8SLennert Buytenhek {
80491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
805afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
80691da11f8SLennert Buytenhek 
80791da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
80891da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
80991da11f8SLennert Buytenhek 
81091da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
81191da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
81291da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
81391da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
8149d490b4eSVivien Didelot 		if (ds->ops->get_strings)
815afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
81691da11f8SLennert Buytenhek 	}
81791da11f8SLennert Buytenhek }
81891da11f8SLennert Buytenhek 
819badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
820badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
821badf3adaSFlorian Fainelli 					   uint64_t *data)
822badf3adaSFlorian Fainelli {
823badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
824*8b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
825*8b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
826badf3adaSFlorian Fainelli 	int count = 0;
827badf3adaSFlorian Fainelli 
828badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
829badf3adaSFlorian Fainelli 		count = dst->master_ethtool_ops.get_sset_count(dev,
830badf3adaSFlorian Fainelli 							       ETH_SS_STATS);
831badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
832badf3adaSFlorian Fainelli 	}
833badf3adaSFlorian Fainelli 
8349d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
8359d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
836badf3adaSFlorian Fainelli }
837badf3adaSFlorian Fainelli 
838badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
839badf3adaSFlorian Fainelli {
840badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
841*8b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
842badf3adaSFlorian Fainelli 	int count = 0;
843badf3adaSFlorian Fainelli 
844badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count)
845badf3adaSFlorian Fainelli 		count += dst->master_ethtool_ops.get_sset_count(dev, sset);
846badf3adaSFlorian Fainelli 
8479d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
8489d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
849badf3adaSFlorian Fainelli 
850badf3adaSFlorian Fainelli 	return count;
851badf3adaSFlorian Fainelli }
852badf3adaSFlorian Fainelli 
853badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
854badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
855badf3adaSFlorian Fainelli {
856badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
857*8b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
858*8b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
859badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
860badf3adaSFlorian Fainelli 	int mcount = 0, count;
861badf3adaSFlorian Fainelli 	unsigned int i;
862badf3adaSFlorian Fainelli 	uint8_t pfx[4];
863badf3adaSFlorian Fainelli 	uint8_t *ndata;
864badf3adaSFlorian Fainelli 
865badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
866badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
867badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
868badf3adaSFlorian Fainelli 
869badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
870badf3adaSFlorian Fainelli 		mcount = dst->master_ethtool_ops.get_sset_count(dev,
871badf3adaSFlorian Fainelli 								ETH_SS_STATS);
872badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_strings(dev, stringset, data);
873badf3adaSFlorian Fainelli 	}
874badf3adaSFlorian Fainelli 
8759d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
876badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
877badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
878badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
879badf3adaSFlorian Fainelli 		 * constructed earlier
880badf3adaSFlorian Fainelli 		 */
8819d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
8829d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
883badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
884badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
885badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
886badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
887badf3adaSFlorian Fainelli 		}
888badf3adaSFlorian Fainelli 	}
889badf3adaSFlorian Fainelli }
890badf3adaSFlorian Fainelli 
89191da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
89291da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
89391da11f8SLennert Buytenhek 					uint64_t *data)
89491da11f8SLennert Buytenhek {
89591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
896afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
89791da11f8SLennert Buytenhek 
89846e7b8d8SVivien Didelot 	data[0] = dev->stats.tx_packets;
89946e7b8d8SVivien Didelot 	data[1] = dev->stats.tx_bytes;
90046e7b8d8SVivien Didelot 	data[2] = dev->stats.rx_packets;
90146e7b8d8SVivien Didelot 	data[3] = dev->stats.rx_bytes;
9029d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
903afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
90491da11f8SLennert Buytenhek }
90591da11f8SLennert Buytenhek 
90691da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
90791da11f8SLennert Buytenhek {
90891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
909afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
91091da11f8SLennert Buytenhek 
91191da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
91291da11f8SLennert Buytenhek 		int count;
91391da11f8SLennert Buytenhek 
91491da11f8SLennert Buytenhek 		count = 4;
9159d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
9169d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
91791da11f8SLennert Buytenhek 
91891da11f8SLennert Buytenhek 		return count;
91991da11f8SLennert Buytenhek 	}
92091da11f8SLennert Buytenhek 
92191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
92291da11f8SLennert Buytenhek }
92391da11f8SLennert Buytenhek 
92419e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
92519e57c4eSFlorian Fainelli {
92619e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
927afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
92819e57c4eSFlorian Fainelli 
9299d490b4eSVivien Didelot 	if (ds->ops->get_wol)
930afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
93119e57c4eSFlorian Fainelli }
93219e57c4eSFlorian Fainelli 
93319e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
93419e57c4eSFlorian Fainelli {
93519e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
936afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
93719e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
93819e57c4eSFlorian Fainelli 
9399d490b4eSVivien Didelot 	if (ds->ops->set_wol)
940afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
94119e57c4eSFlorian Fainelli 
94219e57c4eSFlorian Fainelli 	return ret;
94319e57c4eSFlorian Fainelli }
94419e57c4eSFlorian Fainelli 
9457905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
9467905288fSFlorian Fainelli {
9477905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
948afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9497905288fSFlorian Fainelli 	int ret;
9507905288fSFlorian Fainelli 
9519d490b4eSVivien Didelot 	if (!ds->ops->set_eee)
9527905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9537905288fSFlorian Fainelli 
954afdcf151SVivien Didelot 	ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
9557905288fSFlorian Fainelli 	if (ret)
9567905288fSFlorian Fainelli 		return ret;
9577905288fSFlorian Fainelli 
9587905288fSFlorian Fainelli 	if (p->phy)
9597905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
9607905288fSFlorian Fainelli 
9617905288fSFlorian Fainelli 	return ret;
9627905288fSFlorian Fainelli }
9637905288fSFlorian Fainelli 
9647905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
9657905288fSFlorian Fainelli {
9667905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
967afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9687905288fSFlorian Fainelli 	int ret;
9697905288fSFlorian Fainelli 
9709d490b4eSVivien Didelot 	if (!ds->ops->get_eee)
9717905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9727905288fSFlorian Fainelli 
973afdcf151SVivien Didelot 	ret = ds->ops->get_eee(ds, p->dp->index, e);
9747905288fSFlorian Fainelli 	if (ret)
9757905288fSFlorian Fainelli 		return ret;
9767905288fSFlorian Fainelli 
9777905288fSFlorian Fainelli 	if (p->phy)
9787905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
9797905288fSFlorian Fainelli 
9807905288fSFlorian Fainelli 	return ret;
9817905288fSFlorian Fainelli }
9827905288fSFlorian Fainelli 
98304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
98404ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
98504ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
98604ff53f9SFlorian Fainelli {
98704ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
988afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
98904ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
99004ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
99104ff53f9SFlorian Fainelli 	int err = 0;
99204ff53f9SFlorian Fainelli 
99304ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
99404ff53f9SFlorian Fainelli 	if (!netpoll)
99504ff53f9SFlorian Fainelli 		return -ENOMEM;
99604ff53f9SFlorian Fainelli 
99704ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
99804ff53f9SFlorian Fainelli 	if (err) {
99904ff53f9SFlorian Fainelli 		kfree(netpoll);
100004ff53f9SFlorian Fainelli 		goto out;
100104ff53f9SFlorian Fainelli 	}
100204ff53f9SFlorian Fainelli 
100304ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
100404ff53f9SFlorian Fainelli out:
100504ff53f9SFlorian Fainelli 	return err;
100604ff53f9SFlorian Fainelli }
100704ff53f9SFlorian Fainelli 
100804ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
100904ff53f9SFlorian Fainelli {
101004ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
101104ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
101204ff53f9SFlorian Fainelli 
101304ff53f9SFlorian Fainelli 	if (!netpoll)
101404ff53f9SFlorian Fainelli 		return;
101504ff53f9SFlorian Fainelli 
101604ff53f9SFlorian Fainelli 	p->netpoll = NULL;
101704ff53f9SFlorian Fainelli 
101804ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
101904ff53f9SFlorian Fainelli }
102004ff53f9SFlorian Fainelli 
102104ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
102204ff53f9SFlorian Fainelli {
102304ff53f9SFlorian Fainelli }
102404ff53f9SFlorian Fainelli #endif
102504ff53f9SFlorian Fainelli 
102644bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
102744bb765cSFlorian Fainelli 					char *name, size_t len)
102844bb765cSFlorian Fainelli {
102944bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
103044bb765cSFlorian Fainelli 
1031afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
103244bb765cSFlorian Fainelli 		return -EINVAL;
10333a543ef4SFlorian Fainelli 
10343a543ef4SFlorian Fainelli 	return 0;
10353a543ef4SFlorian Fainelli }
10363a543ef4SFlorian Fainelli 
1037f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
1038f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1039f50f2127SFlorian Fainelli 			     unsigned long cookie)
1040f50f2127SFlorian Fainelli {
1041f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1042f50f2127SFlorian Fainelli 
1043f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1044f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1045f50f2127SFlorian Fainelli 			return mall_tc_entry;
1046f50f2127SFlorian Fainelli 
1047f50f2127SFlorian Fainelli 	return NULL;
1048f50f2127SFlorian Fainelli }
1049f50f2127SFlorian Fainelli 
1050f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
1051f50f2127SFlorian Fainelli 				      __be16 protocol,
1052f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
1053f50f2127SFlorian Fainelli 				      bool ingress)
1054f50f2127SFlorian Fainelli {
1055f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1056f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1057f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1058f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
1059f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
1060f50f2127SFlorian Fainelli 	struct net_device *to_dev;
1061f50f2127SFlorian Fainelli 	const struct tc_action *a;
1062f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
1063f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
1064f50f2127SFlorian Fainelli 	int ifindex;
1065f50f2127SFlorian Fainelli 
1066f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
1067f50f2127SFlorian Fainelli 		return err;
1068f50f2127SFlorian Fainelli 
1069f50f2127SFlorian Fainelli 	if (!tc_single_action(cls->exts))
1070f50f2127SFlorian Fainelli 		return err;
1071f50f2127SFlorian Fainelli 
1072f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
1073f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
1074f50f2127SFlorian Fainelli 
1075f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1076f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
1077f50f2127SFlorian Fainelli 
1078f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
1079f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
1080f50f2127SFlorian Fainelli 		if (!to_dev)
1081f50f2127SFlorian Fainelli 			return -EINVAL;
1082f50f2127SFlorian Fainelli 
1083f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
1084f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
1085f50f2127SFlorian Fainelli 
1086f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1087f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
1088f50f2127SFlorian Fainelli 			return -ENOMEM;
1089f50f2127SFlorian Fainelli 
1090f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
1091f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1092f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
1093f50f2127SFlorian Fainelli 
1094f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
1095f50f2127SFlorian Fainelli 
1096f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
1097f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
1098f50f2127SFlorian Fainelli 
1099f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1100f50f2127SFlorian Fainelli 					       ingress);
1101f50f2127SFlorian Fainelli 		if (err) {
1102f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
1103f50f2127SFlorian Fainelli 			return err;
1104f50f2127SFlorian Fainelli 		}
1105f50f2127SFlorian Fainelli 
1106f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1107f50f2127SFlorian Fainelli 	}
1108f50f2127SFlorian Fainelli 
1109f50f2127SFlorian Fainelli 	return 0;
1110f50f2127SFlorian Fainelli }
1111f50f2127SFlorian Fainelli 
1112f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1113f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1114f50f2127SFlorian Fainelli {
1115f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1116f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1117f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1118f50f2127SFlorian Fainelli 
1119f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
1120f50f2127SFlorian Fainelli 		return;
1121f50f2127SFlorian Fainelli 
1122f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1123f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1124f50f2127SFlorian Fainelli 		return;
1125f50f2127SFlorian Fainelli 
1126f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1127f50f2127SFlorian Fainelli 
1128f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1129f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
1130f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
1131f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
1132f50f2127SFlorian Fainelli 		break;
1133f50f2127SFlorian Fainelli 	default:
1134f50f2127SFlorian Fainelli 		WARN_ON(1);
1135f50f2127SFlorian Fainelli 	}
1136f50f2127SFlorian Fainelli 
1137f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1138f50f2127SFlorian Fainelli }
1139f50f2127SFlorian Fainelli 
1140f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1141f50f2127SFlorian Fainelli 			      __be16 protocol, struct tc_to_netdev *tc)
1142f50f2127SFlorian Fainelli {
1143f50f2127SFlorian Fainelli 	bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1144f50f2127SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1145f50f2127SFlorian Fainelli 
1146f50f2127SFlorian Fainelli 	switch (tc->type) {
1147f50f2127SFlorian Fainelli 	case TC_SETUP_MATCHALL:
1148f50f2127SFlorian Fainelli 		switch (tc->cls_mall->command) {
1149f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_REPLACE:
1150f50f2127SFlorian Fainelli 			return dsa_slave_add_cls_matchall(dev, protocol,
1151f50f2127SFlorian Fainelli 							  tc->cls_mall,
1152f50f2127SFlorian Fainelli 							  ingress);
1153f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_DESTROY:
1154f50f2127SFlorian Fainelli 			dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1155f50f2127SFlorian Fainelli 			return 0;
1156f50f2127SFlorian Fainelli 		}
1157f50f2127SFlorian Fainelli 	default:
1158f50f2127SFlorian Fainelli 		break;
1159f50f2127SFlorian Fainelli 	}
1160f50f2127SFlorian Fainelli 
1161f50f2127SFlorian Fainelli 	return ret;
1162f50f2127SFlorian Fainelli }
1163f50f2127SFlorian Fainelli 
1164af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1165af42192cSFlorian Fainelli {
1166af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
1167af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1168af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
1169af42192cSFlorian Fainelli }
1170af42192cSFlorian Fainelli 
1171bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1172bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1173bf9f2648SFlorian Fainelli {
1174bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1175bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1176bf9f2648SFlorian Fainelli 
1177bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1178bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1179bf9f2648SFlorian Fainelli 
1180bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1181bf9f2648SFlorian Fainelli }
1182bf9f2648SFlorian Fainelli 
1183bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1184bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1185bf9f2648SFlorian Fainelli {
1186bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1187bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1188bf9f2648SFlorian Fainelli 
1189bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1190bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1191bf9f2648SFlorian Fainelli 
1192bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1193bf9f2648SFlorian Fainelli }
1194bf9f2648SFlorian Fainelli 
119591da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
119691da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
11973d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
11983d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
119991da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
120091da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
12016793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
12026793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
12036793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
120491da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
120591da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
120691da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
120719e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
120819e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
12097905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
12107905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1211bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1212bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1213bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1214bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
121591da11f8SLennert Buytenhek };
121691da11f8SLennert Buytenhek 
12173e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1218d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1219d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
12203e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1221d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1222d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1223d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1224ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
1225ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
1226ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
1227d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1228abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
122904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
123004ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
123104ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
123204ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
123304ff53f9SFlorian Fainelli #endif
123411149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
123511149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
123611149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
123744bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1238f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
123998237d43SScott Feldman };
124098237d43SScott Feldman 
12419d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1242f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
124335636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1244ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1245ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1246ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
1247d442ad4aSStephen Hemminger };
124891da11f8SLennert Buytenhek 
1249f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1250f37db85dSFlorian Fainelli 	.name	= "dsa",
1251f37db85dSFlorian Fainelli };
1252f37db85dSFlorian Fainelli 
12530d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
12540d8bcdd3SFlorian Fainelli {
12550d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1256afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12570d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
12580d8bcdd3SFlorian Fainelli 
12590d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
12600d8bcdd3SFlorian Fainelli 		status_changed = 1;
12610d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
12620d8bcdd3SFlorian Fainelli 	}
12630d8bcdd3SFlorian Fainelli 
12640d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
12650d8bcdd3SFlorian Fainelli 		status_changed = 1;
12660d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
12670d8bcdd3SFlorian Fainelli 	}
12680d8bcdd3SFlorian Fainelli 
12690d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
12700d8bcdd3SFlorian Fainelli 		status_changed = 1;
12710d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
12720d8bcdd3SFlorian Fainelli 	}
12730d8bcdd3SFlorian Fainelli 
12749d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1275afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1276ec9436baSFlorian Fainelli 
12770d8bcdd3SFlorian Fainelli 	if (status_changed)
12780d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
12790d8bcdd3SFlorian Fainelli }
12800d8bcdd3SFlorian Fainelli 
1281ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1282ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1283ce31b31cSFlorian Fainelli {
1284b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1285b71be352SAndrew Lunn 	struct dsa_switch *ds;
1286ce31b31cSFlorian Fainelli 
1287b71be352SAndrew Lunn 	if (dev) {
1288b71be352SAndrew Lunn 		p = netdev_priv(dev);
1289afdcf151SVivien Didelot 		ds = p->dp->ds;
12909d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1291afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1292b71be352SAndrew Lunn 	}
1293ce31b31cSFlorian Fainelli 
1294ce31b31cSFlorian Fainelli 	return 0;
1295ce31b31cSFlorian Fainelli }
1296ce31b31cSFlorian Fainelli 
129791da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1298c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1299cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1300cd28a1a9SFlorian Fainelli 				 int addr)
1301c305c165SFlorian Fainelli {
1302afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1303c305c165SFlorian Fainelli 
13047f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1305d25b8e74SRussell King 	if (!p->phy) {
1306d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1307c305c165SFlorian Fainelli 		return -ENODEV;
1308d25b8e74SRussell King 	}
1309c305c165SFlorian Fainelli 
1310c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1311211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1312c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
13134078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1314c305c165SFlorian Fainelli 				  p->phy_interface);
1315c305c165SFlorian Fainelli }
1316c305c165SFlorian Fainelli 
13179697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
13180d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
13190d8bcdd3SFlorian Fainelli {
1320afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
13210d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1322ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
13236819563eSFlorian Fainelli 	u32 phy_flags = 0;
132419334920SGuenter Roeck 	int mode, ret;
13250d8bcdd3SFlorian Fainelli 
1326afdcf151SVivien Didelot 	port_dn = p->dp->dn;
132719334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
132819334920SGuenter Roeck 	if (mode < 0)
132919334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
133019334920SGuenter Roeck 	p->phy_interface = mode;
13310d8bcdd3SFlorian Fainelli 
13320d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
13330d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
13340d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
13350d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
13360d8bcdd3SFlorian Fainelli 		 */
13370d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
13380d8bcdd3SFlorian Fainelli 		if (ret) {
1339d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
13409697f1cdSFlorian Fainelli 			return ret;
13410d8bcdd3SFlorian Fainelli 		}
1342ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
13430d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
13440d8bcdd3SFlorian Fainelli 	}
13450d8bcdd3SFlorian Fainelli 
13469d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1347afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
13486819563eSFlorian Fainelli 
1349cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1350d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1351d25b8e74SRussell King 
1352cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1353cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1354cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1355cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1356cd28a1a9SFlorian Fainelli 		 */
1357d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1358d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1359d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1360d25b8e74SRussell King 			if (ret) {
1361d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
13620d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1363cd28a1a9SFlorian Fainelli 				return ret;
1364d25b8e74SRussell King 			}
1365cd28a1a9SFlorian Fainelli 		} else {
13660d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1367cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1368cd28a1a9SFlorian Fainelli 						phy_flags,
13690d8bcdd3SFlorian Fainelli 						p->phy_interface);
1370cd28a1a9SFlorian Fainelli 		}
13710d8f3c67SJohan Hovold 
13720d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1373cd28a1a9SFlorian Fainelli 	}
13740d8bcdd3SFlorian Fainelli 
1375ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1376ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1377ce31b31cSFlorian Fainelli 
13780d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
13790d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
13800d8bcdd3SFlorian Fainelli 	 */
1381b31f65fbSAndrew Lunn 	if (!p->phy) {
1382afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1383d25b8e74SRussell King 		if (ret) {
1384afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1385afdcf151SVivien Didelot 				   p->dp->index, ret);
1386881eadabSJohan Hovold 			if (phy_is_fixed)
1387881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1388c305c165SFlorian Fainelli 			return ret;
1389d25b8e74SRussell King 		}
13900d8bcdd3SFlorian Fainelli 	}
13919697f1cdSFlorian Fainelli 
13922220943aSAndrew Lunn 	phy_attached_info(p->phy);
13932220943aSAndrew Lunn 
13949697f1cdSFlorian Fainelli 	return 0;
1395b31f65fbSAndrew Lunn }
13960d8bcdd3SFlorian Fainelli 
1397448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1398448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1399448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1400448b4482SAndrew Lunn 					    void *_unused)
1401448b4482SAndrew Lunn {
1402448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1403448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1404448b4482SAndrew Lunn }
1405448b4482SAndrew Lunn 
140624462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
140724462549SFlorian Fainelli {
140824462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
140924462549SFlorian Fainelli 
1410f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1411f154be24SFlorian Fainelli 
141224462549SFlorian Fainelli 	if (p->phy) {
141324462549SFlorian Fainelli 		phy_stop(p->phy);
141424462549SFlorian Fainelli 		p->old_pause = -1;
141524462549SFlorian Fainelli 		p->old_link = -1;
141624462549SFlorian Fainelli 		p->old_duplex = -1;
141724462549SFlorian Fainelli 		phy_suspend(p->phy);
141824462549SFlorian Fainelli 	}
141924462549SFlorian Fainelli 
142024462549SFlorian Fainelli 	return 0;
142124462549SFlorian Fainelli }
142224462549SFlorian Fainelli 
142324462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
142424462549SFlorian Fainelli {
142524462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
142624462549SFlorian Fainelli 
142724462549SFlorian Fainelli 	netif_device_attach(slave_dev);
142824462549SFlorian Fainelli 
142924462549SFlorian Fainelli 	if (p->phy) {
143024462549SFlorian Fainelli 		phy_resume(p->phy);
143124462549SFlorian Fainelli 		phy_start(p->phy);
143224462549SFlorian Fainelli 	}
143324462549SFlorian Fainelli 
143424462549SFlorian Fainelli 	return 0;
143524462549SFlorian Fainelli }
143624462549SFlorian Fainelli 
1437d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
143883c0afaeSAndrew Lunn 		     int port, const char *name)
143991da11f8SLennert Buytenhek {
1440badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
144183c0afaeSAndrew Lunn 	struct net_device *master;
144291da11f8SLennert Buytenhek 	struct net_device *slave_dev;
144391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
144491da11f8SLennert Buytenhek 	int ret;
144591da11f8SLennert Buytenhek 
144683c0afaeSAndrew Lunn 	master = ds->dst->master_netdev;
144783c0afaeSAndrew Lunn 	if (ds->master_netdev)
144883c0afaeSAndrew Lunn 		master = ds->master_netdev;
144983c0afaeSAndrew Lunn 
1450c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1451c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
145291da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1453d87d6f44SGuenter Roeck 		return -ENOMEM;
145491da11f8SLennert Buytenhek 
1455f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1456f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14577ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14582fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
14590a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14603e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14619d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
14628b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14638b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1464f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1465d442ad4aSStephen Hemminger 
1466448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1467448b4482SAndrew Lunn 				 NULL);
1468448b4482SAndrew Lunn 
146991da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1470189b0d93SAndrew Lunn 	slave_dev->dev.of_node = ds->ports[port].dn;
147191da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
147291da11f8SLennert Buytenhek 
147391da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
1474afdcf151SVivien Didelot 	p->dp = &ds->ports[port];
1475f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
147639a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
14775075314eSAlexander Duyck 
14780d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
14790d8bcdd3SFlorian Fainelli 	p->old_link = -1;
14800d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
14810d8bcdd3SFlorian Fainelli 
1482c8b09808SAndrew Lunn 	ds->ports[port].netdev = slave_dev;
148391da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
148491da11f8SLennert Buytenhek 	if (ret) {
1485a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1486a2ae6007SJoe Perches 			   ret, slave_dev->name);
1487c8b09808SAndrew Lunn 		ds->ports[port].netdev = NULL;
148891da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1489d87d6f44SGuenter Roeck 		return ret;
149091da11f8SLennert Buytenhek 	}
149191da11f8SLennert Buytenhek 
149291da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
149391da11f8SLennert Buytenhek 
14940071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
14950071f56eSAndrew Lunn 	if (ret) {
14960071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
149773dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
14980071f56eSAndrew Lunn 		free_netdev(slave_dev);
14990071f56eSAndrew Lunn 		return ret;
15000071f56eSAndrew Lunn 	}
15010071f56eSAndrew Lunn 
1502d87d6f44SGuenter Roeck 	return 0;
150391da11f8SLennert Buytenhek }
1504b73adef6SFlorian Fainelli 
1505cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1506cda5c15bSNeil Armstrong {
1507cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1508881eadabSJohan Hovold 	struct device_node *port_dn;
1509881eadabSJohan Hovold 
1510afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1511cda5c15bSNeil Armstrong 
1512cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1513881eadabSJohan Hovold 	if (p->phy) {
1514cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1515881eadabSJohan Hovold 
1516881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1517881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1518881eadabSJohan Hovold 	}
1519cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1520cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1521cda5c15bSNeil Armstrong }
1522cda5c15bSNeil Armstrong 
1523b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1524b73adef6SFlorian Fainelli {
1525b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1526b73adef6SFlorian Fainelli }
1527b73adef6SFlorian Fainelli 
15288e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
15298e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1530b73adef6SFlorian Fainelli {
15318e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1532b73adef6SFlorian Fainelli 
15338e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
15348e92ab3aSVivien Didelot 		if (info->linking) {
15358e92ab3aSVivien Didelot 			err = dsa_slave_bridge_port_join(dev, info->upper_dev);
15368e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
15378e92ab3aSVivien Didelot 		} else {
15388e92ab3aSVivien Didelot 			dsa_slave_bridge_port_leave(dev, info->upper_dev);
15398e92ab3aSVivien Didelot 			err = NOTIFY_OK;
15408e92ab3aSVivien Didelot 		}
15416debb68aSVivien Didelot 	}
1542b73adef6SFlorian Fainelli 
15438e92ab3aSVivien Didelot 	return err;
1544b73adef6SFlorian Fainelli }
1545b73adef6SFlorian Fainelli 
154688e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1547b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1548b73adef6SFlorian Fainelli {
15496debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1550b73adef6SFlorian Fainelli 
15518e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
15528e92ab3aSVivien Didelot 		return NOTIFY_DONE;
15538e92ab3aSVivien Didelot 
15548e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
15558e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1556b73adef6SFlorian Fainelli 
1557b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1558b73adef6SFlorian Fainelli }
155988e4f0caSVivien Didelot 
156088e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
156188e4f0caSVivien Didelot 	.notifier_call	= dsa_slave_netdevice_event,
156288e4f0caSVivien Didelot };
156388e4f0caSVivien Didelot 
156488e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
156588e4f0caSVivien Didelot {
156688e4f0caSVivien Didelot 	return register_netdevice_notifier(&dsa_slave_nb);
156788e4f0caSVivien Didelot }
156888e4f0caSVivien Didelot 
156988e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
157088e4f0caSVivien Didelot {
157188e4f0caSVivien Didelot 	int err;
157288e4f0caSVivien Didelot 
157388e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
157488e4f0caSVivien Didelot 	if (err)
157588e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
157688e4f0caSVivien Didelot }
1577