xref: /openbmc/linux/net/dsa/slave.c (revision 8e92ab3a426e04dc355b196e3b4474f633025a3b)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #include <linux/list.h>
12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
13b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1491da11f8SLennert Buytenhek #include <linux/phy.h>
15a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
160d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
170d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
187f854420SAndrew Lunn #include <linux/mdio.h>
19f50f2127SFlorian Fainelli #include <linux/list.h>
20b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
2198237d43SScott Feldman #include <net/switchdev.h>
22f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
23f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
24b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2504ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2691da11f8SLennert Buytenhek #include "dsa_priv.h"
2791da11f8SLennert Buytenhek 
28f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev);
29f50f2127SFlorian Fainelli 
3091da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
3191da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
3291da11f8SLennert Buytenhek {
3391da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3491da11f8SLennert Buytenhek 
350d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
369d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3791da11f8SLennert Buytenhek 
3891da11f8SLennert Buytenhek 	return 0xffff;
3991da11f8SLennert Buytenhek }
4091da11f8SLennert Buytenhek 
4191da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
4291da11f8SLennert Buytenhek {
4391da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4491da11f8SLennert Buytenhek 
450d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
469d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4791da11f8SLennert Buytenhek 
4891da11f8SLennert Buytenhek 	return 0;
4991da11f8SLennert Buytenhek }
5091da11f8SLennert Buytenhek 
5191da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
5291da11f8SLennert Buytenhek {
5391da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5491da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5591da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5691da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
570b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
580b7b498dSFlorian Fainelli 		 ds->dst->tree, ds->index);
59c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
6024df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
6191da11f8SLennert Buytenhek }
6291da11f8SLennert Buytenhek 
6391da11f8SLennert Buytenhek 
6491da11f8SLennert Buytenhek /* slave device handling ****************************************************/
65abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
66c0840801SLennert Buytenhek {
67c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
68c0840801SLennert Buytenhek 
69afdcf151SVivien Didelot 	return p->dp->ds->dst->master_netdev->ifindex;
70c0840801SLennert Buytenhek }
71c0840801SLennert Buytenhek 
72a5e9a02eSVivien Didelot static inline bool dsa_port_is_bridged(struct dsa_port *dp)
73b73adef6SFlorian Fainelli {
74a5e9a02eSVivien Didelot 	return !!dp->bridge_dev;
75b73adef6SFlorian Fainelli }
76b73adef6SFlorian Fainelli 
774acfee81SVivien Didelot static void dsa_port_set_stp_state(struct dsa_switch *ds, int port, u8 state)
784acfee81SVivien Didelot {
79732f794cSVivien Didelot 	struct dsa_port *dp = &ds->ports[port];
80732f794cSVivien Didelot 
814acfee81SVivien Didelot 	if (ds->ops->port_stp_state_set)
824acfee81SVivien Didelot 		ds->ops->port_stp_state_set(ds, port, state);
83732f794cSVivien Didelot 
84732f794cSVivien Didelot 	if (ds->ops->port_fast_age) {
85732f794cSVivien Didelot 		/* Fast age FDB entries or flush appropriate forwarding database
86732f794cSVivien Didelot 		 * for the given port, if we are moving it from Learning or
87732f794cSVivien Didelot 		 * Forwarding state, to Disabled or Blocking or Listening state.
88732f794cSVivien Didelot 		 */
89732f794cSVivien Didelot 
90732f794cSVivien Didelot 		if ((dp->stp_state == BR_STATE_LEARNING ||
91732f794cSVivien Didelot 		     dp->stp_state == BR_STATE_FORWARDING) &&
92732f794cSVivien Didelot 		    (state == BR_STATE_DISABLED ||
93732f794cSVivien Didelot 		     state == BR_STATE_BLOCKING ||
94732f794cSVivien Didelot 		     state == BR_STATE_LISTENING))
95732f794cSVivien Didelot 			ds->ops->port_fast_age(ds, port);
96732f794cSVivien Didelot 	}
97732f794cSVivien Didelot 
98732f794cSVivien Didelot 	dp->stp_state = state;
994acfee81SVivien Didelot }
1004acfee81SVivien Didelot 
10191da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
10291da11f8SLennert Buytenhek {
103df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
104afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
105afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
106a5e9a02eSVivien Didelot 	u8 stp_state = dsa_port_is_bridged(p->dp) ?
107b73adef6SFlorian Fainelli 			BR_STATE_BLOCKING : BR_STATE_FORWARDING;
108df02c6ffSLennert Buytenhek 	int err;
109df02c6ffSLennert Buytenhek 
110df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
111df02c6ffSLennert Buytenhek 		return -ENETDOWN;
112df02c6ffSLennert Buytenhek 
1138feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
114a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
115df02c6ffSLennert Buytenhek 		if (err < 0)
116df02c6ffSLennert Buytenhek 			goto out;
117df02c6ffSLennert Buytenhek 	}
118df02c6ffSLennert Buytenhek 
119df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
120df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
121df02c6ffSLennert Buytenhek 		if (err < 0)
122df02c6ffSLennert Buytenhek 			goto del_unicast;
123df02c6ffSLennert Buytenhek 	}
124df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
125df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
126df02c6ffSLennert Buytenhek 		if (err < 0)
127df02c6ffSLennert Buytenhek 			goto clear_allmulti;
128df02c6ffSLennert Buytenhek 	}
129df02c6ffSLennert Buytenhek 
1309d490b4eSVivien Didelot 	if (ds->ops->port_enable) {
131afdcf151SVivien Didelot 		err = ds->ops->port_enable(ds, p->dp->index, p->phy);
132b2f2af21SFlorian Fainelli 		if (err)
133b2f2af21SFlorian Fainelli 			goto clear_promisc;
134b2f2af21SFlorian Fainelli 	}
135b2f2af21SFlorian Fainelli 
136afdcf151SVivien Didelot 	dsa_port_set_stp_state(ds, p->dp->index, stp_state);
137b73adef6SFlorian Fainelli 
138f7f1de51SFlorian Fainelli 	if (p->phy)
139f7f1de51SFlorian Fainelli 		phy_start(p->phy);
140f7f1de51SFlorian Fainelli 
14191da11f8SLennert Buytenhek 	return 0;
142df02c6ffSLennert Buytenhek 
143b2f2af21SFlorian Fainelli clear_promisc:
144b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1454fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
146df02c6ffSLennert Buytenhek clear_allmulti:
147df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
148df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
149df02c6ffSLennert Buytenhek del_unicast:
1508feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
151a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
152df02c6ffSLennert Buytenhek out:
153df02c6ffSLennert Buytenhek 	return err;
15491da11f8SLennert Buytenhek }
15591da11f8SLennert Buytenhek 
15691da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
15791da11f8SLennert Buytenhek {
158df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
159afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
160afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
161df02c6ffSLennert Buytenhek 
162f7f1de51SFlorian Fainelli 	if (p->phy)
163f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
164f7f1de51SFlorian Fainelli 
165df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
166a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
167df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
168df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
169df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
170df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
171df02c6ffSLennert Buytenhek 
1728feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
173a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
174df02c6ffSLennert Buytenhek 
1759d490b4eSVivien Didelot 	if (ds->ops->port_disable)
176afdcf151SVivien Didelot 		ds->ops->port_disable(ds, p->dp->index, p->phy);
177b2f2af21SFlorian Fainelli 
178afdcf151SVivien Didelot 	dsa_port_set_stp_state(ds, p->dp->index, BR_STATE_DISABLED);
179b73adef6SFlorian Fainelli 
18091da11f8SLennert Buytenhek 	return 0;
18191da11f8SLennert Buytenhek }
18291da11f8SLennert Buytenhek 
18391da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
18491da11f8SLennert Buytenhek {
18591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
186afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
18791da11f8SLennert Buytenhek 
18891da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
18991da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
19091da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
19191da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
19291da11f8SLennert Buytenhek }
19391da11f8SLennert Buytenhek 
19491da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
19591da11f8SLennert Buytenhek {
19691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
197afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
19891da11f8SLennert Buytenhek 
19991da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
200a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
20191da11f8SLennert Buytenhek }
20291da11f8SLennert Buytenhek 
203df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
20491da11f8SLennert Buytenhek {
205df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
206afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
207df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
208df02c6ffSLennert Buytenhek 	int err;
209df02c6ffSLennert Buytenhek 
210df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
211df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
212df02c6ffSLennert Buytenhek 
213df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
214df02c6ffSLennert Buytenhek 		goto out;
215df02c6ffSLennert Buytenhek 
2168feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
217a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
218df02c6ffSLennert Buytenhek 		if (err < 0)
219df02c6ffSLennert Buytenhek 			return err;
220df02c6ffSLennert Buytenhek 	}
221df02c6ffSLennert Buytenhek 
2228feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
223a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
224df02c6ffSLennert Buytenhek 
225df02c6ffSLennert Buytenhek out:
226d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
22791da11f8SLennert Buytenhek 
22891da11f8SLennert Buytenhek 	return 0;
22991da11f8SLennert Buytenhek }
23091da11f8SLennert Buytenhek 
23111149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev,
2328f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan,
233f8db8348SJiri Pirko 				   struct switchdev_trans *trans)
23411149536SVivien Didelot {
23511149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
236afdcf151SVivien Didelot 	struct dsa_port *dp = p->dp;
237afdcf151SVivien Didelot 	struct dsa_switch *ds = dp->ds;
23811149536SVivien Didelot 
23979a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans)) {
2409d490b4eSVivien Didelot 		if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
24111149536SVivien Didelot 			return -EOPNOTSUPP;
24211149536SVivien Didelot 
243afdcf151SVivien Didelot 		return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
24411149536SVivien Didelot 	}
24511149536SVivien Didelot 
246afdcf151SVivien Didelot 	ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
2474d5770b3SVivien Didelot 
24811149536SVivien Didelot 	return 0;
24911149536SVivien Didelot }
25011149536SVivien Didelot 
25111149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev,
2528f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan)
25311149536SVivien Didelot {
25411149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
255afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
25611149536SVivien Didelot 
2579d490b4eSVivien Didelot 	if (!ds->ops->port_vlan_del)
25811149536SVivien Didelot 		return -EOPNOTSUPP;
25911149536SVivien Didelot 
260afdcf151SVivien Didelot 	return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
26111149536SVivien Didelot }
26211149536SVivien Didelot 
26311149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev,
2648f24f309SJiri Pirko 				    struct switchdev_obj_port_vlan *vlan,
265648b4a99SJiri Pirko 				    switchdev_obj_dump_cb_t *cb)
26611149536SVivien Didelot {
26711149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
268afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
26911149536SVivien Didelot 
2709d490b4eSVivien Didelot 	if (ds->ops->port_vlan_dump)
271afdcf151SVivien Didelot 		return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
27265aebfc0SVivien Didelot 
27311149536SVivien Didelot 	return -EOPNOTSUPP;
27411149536SVivien Didelot }
27511149536SVivien Didelot 
276ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev,
27752ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb,
278f8db8348SJiri Pirko 				  struct switchdev_trans *trans)
279cdf09697SDavid S. Miller {
280cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
281afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
282146a3206SVivien Didelot 
2838497aa61SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
2849d490b4eSVivien Didelot 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
285146a3206SVivien Didelot 			return -EOPNOTSUPP;
286cdf09697SDavid S. Miller 
287afdcf151SVivien Didelot 		return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
2888497aa61SVivien Didelot 	}
289cdf09697SDavid S. Miller 
290afdcf151SVivien Didelot 	ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
2918497aa61SVivien Didelot 
2928497aa61SVivien Didelot 	return 0;
293cdf09697SDavid S. Miller }
294cdf09697SDavid S. Miller 
295ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev,
29652ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb)
297cdf09697SDavid S. Miller {
298cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
299afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
300cdf09697SDavid S. Miller 	int ret = -EOPNOTSUPP;
301cdf09697SDavid S. Miller 
3029d490b4eSVivien Didelot 	if (ds->ops->port_fdb_del)
303afdcf151SVivien Didelot 		ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
304cdf09697SDavid S. Miller 
305cdf09697SDavid S. Miller 	return ret;
306cdf09697SDavid S. Miller }
307cdf09697SDavid S. Miller 
308ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev,
30952ba57cfSJiri Pirko 				   struct switchdev_obj_port_fdb *fdb,
310648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
311cdf09697SDavid S. Miller {
312cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
313afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
314cdf09697SDavid S. Miller 
3159d490b4eSVivien Didelot 	if (ds->ops->port_fdb_dump)
316afdcf151SVivien Didelot 		return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
317ea70ba98SVivien Didelot 
318cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
319cdf09697SDavid S. Miller }
320cdf09697SDavid S. Miller 
3218df30255SVivien Didelot static int dsa_slave_port_mdb_add(struct net_device *dev,
3228df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb,
3238df30255SVivien Didelot 				  struct switchdev_trans *trans)
3248df30255SVivien Didelot {
3258df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
326afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3278df30255SVivien Didelot 
3288df30255SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3298df30255SVivien Didelot 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
3308df30255SVivien Didelot 			return -EOPNOTSUPP;
3318df30255SVivien Didelot 
332afdcf151SVivien Didelot 		return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
3338df30255SVivien Didelot 	}
3348df30255SVivien Didelot 
335afdcf151SVivien Didelot 	ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
3368df30255SVivien Didelot 
3378df30255SVivien Didelot 	return 0;
3388df30255SVivien Didelot }
3398df30255SVivien Didelot 
3408df30255SVivien Didelot static int dsa_slave_port_mdb_del(struct net_device *dev,
3418df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb)
3428df30255SVivien Didelot {
3438df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
344afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3458df30255SVivien Didelot 
3468df30255SVivien Didelot 	if (ds->ops->port_mdb_del)
347afdcf151SVivien Didelot 		return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
3488df30255SVivien Didelot 
3498df30255SVivien Didelot 	return -EOPNOTSUPP;
3508df30255SVivien Didelot }
3518df30255SVivien Didelot 
3528df30255SVivien Didelot static int dsa_slave_port_mdb_dump(struct net_device *dev,
3538df30255SVivien Didelot 				   struct switchdev_obj_port_mdb *mdb,
3548df30255SVivien Didelot 				   switchdev_obj_dump_cb_t *cb)
3558df30255SVivien Didelot {
3568df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
357afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3588df30255SVivien Didelot 
3598df30255SVivien Didelot 	if (ds->ops->port_mdb_dump)
360afdcf151SVivien Didelot 		return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
3618df30255SVivien Didelot 
3628df30255SVivien Didelot 	return -EOPNOTSUPP;
3638df30255SVivien Didelot }
3648df30255SVivien Didelot 
36591da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
36691da11f8SLennert Buytenhek {
36791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
36891da11f8SLennert Buytenhek 
36991da11f8SLennert Buytenhek 	if (p->phy != NULL)
37028b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
37191da11f8SLennert Buytenhek 
37291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
37391da11f8SLennert Buytenhek }
37491da11f8SLennert Buytenhek 
37543c44a9fSVivien Didelot static int dsa_slave_stp_state_set(struct net_device *dev,
37643c44a9fSVivien Didelot 				   const struct switchdev_attr *attr,
37743c44a9fSVivien Didelot 				   struct switchdev_trans *trans)
378b73adef6SFlorian Fainelli {
379b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
380afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
381b73adef6SFlorian Fainelli 
38243c44a9fSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
3839d490b4eSVivien Didelot 		return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
384b73adef6SFlorian Fainelli 
385afdcf151SVivien Didelot 	dsa_port_set_stp_state(ds, p->dp->index, attr->u.stp_state);
38643c44a9fSVivien Didelot 
38743c44a9fSVivien Didelot 	return 0;
388b73adef6SFlorian Fainelli }
389b73adef6SFlorian Fainelli 
390fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
391fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
392fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
393fb2dabadSVivien Didelot {
394fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
395afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
396fb2dabadSVivien Didelot 
397fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
398fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
399fb2dabadSVivien Didelot 		return 0;
400fb2dabadSVivien Didelot 
4019d490b4eSVivien Didelot 	if (ds->ops->port_vlan_filtering)
402afdcf151SVivien Didelot 		return ds->ops->port_vlan_filtering(ds, p->dp->index,
403fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
404fb2dabadSVivien Didelot 
405fb2dabadSVivien Didelot 	return 0;
406fb2dabadSVivien Didelot }
407fb2dabadSVivien Didelot 
40834a79f63SVivien Didelot static int dsa_fastest_ageing_time(struct dsa_switch *ds,
40934a79f63SVivien Didelot 				   unsigned int ageing_time)
41034a79f63SVivien Didelot {
41134a79f63SVivien Didelot 	int i;
41234a79f63SVivien Didelot 
41326895e29SVivien Didelot 	for (i = 0; i < ds->num_ports; ++i) {
41434a79f63SVivien Didelot 		struct dsa_port *dp = &ds->ports[i];
41534a79f63SVivien Didelot 
41634a79f63SVivien Didelot 		if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
41734a79f63SVivien Didelot 			ageing_time = dp->ageing_time;
41834a79f63SVivien Didelot 	}
41934a79f63SVivien Didelot 
42034a79f63SVivien Didelot 	return ageing_time;
42134a79f63SVivien Didelot }
42234a79f63SVivien Didelot 
42334a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev,
42434a79f63SVivien Didelot 				 const struct switchdev_attr *attr,
42534a79f63SVivien Didelot 				 struct switchdev_trans *trans)
42634a79f63SVivien Didelot {
42734a79f63SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
428afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
42934a79f63SVivien Didelot 	unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
43034a79f63SVivien Didelot 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
43134a79f63SVivien Didelot 
43234a79f63SVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
43334a79f63SVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
43434a79f63SVivien Didelot 		return 0;
43534a79f63SVivien Didelot 
43634a79f63SVivien Didelot 	/* Keep the fastest ageing time in case of multiple bridges */
437afdcf151SVivien Didelot 	p->dp->ageing_time = ageing_time;
43834a79f63SVivien Didelot 	ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
43934a79f63SVivien Didelot 
4409d490b4eSVivien Didelot 	if (ds->ops->set_ageing_time)
4419d490b4eSVivien Didelot 		return ds->ops->set_ageing_time(ds, ageing_time);
44234a79f63SVivien Didelot 
44334a79f63SVivien Didelot 	return 0;
44434a79f63SVivien Didelot }
44534a79f63SVivien Didelot 
44635636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
447f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
4487ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
44935636062SScott Feldman {
450b8d866acSVivien Didelot 	int ret;
45135636062SScott Feldman 
45235636062SScott Feldman 	switch (attr->id) {
4531f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
45443c44a9fSVivien Didelot 		ret = dsa_slave_stp_state_set(dev, attr, trans);
45535636062SScott Feldman 		break;
456fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
457fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
458fb2dabadSVivien Didelot 		break;
45934a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
46034a79f63SVivien Didelot 		ret = dsa_slave_ageing_time(dev, attr, trans);
46134a79f63SVivien Didelot 		break;
46235636062SScott Feldman 	default:
46335636062SScott Feldman 		ret = -EOPNOTSUPP;
46435636062SScott Feldman 		break;
46535636062SScott Feldman 	}
46635636062SScott Feldman 
46735636062SScott Feldman 	return ret;
46835636062SScott Feldman }
46935636062SScott Feldman 
470ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
471648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
4727ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
473ba14d9ebSVivien Didelot {
474ba14d9ebSVivien Didelot 	int err;
475ba14d9ebSVivien Didelot 
476ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
477ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
478ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
479ba14d9ebSVivien Didelot 	 */
480ba14d9ebSVivien Didelot 
4819e8f4a54SJiri Pirko 	switch (obj->id) {
48257d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
483648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_add(dev,
484648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj),
485648b4a99SJiri Pirko 					     trans);
486ba14d9ebSVivien Didelot 		break;
4878df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
4888df30255SVivien Didelot 		err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
4898df30255SVivien Didelot 					     trans);
4908df30255SVivien Didelot 		break;
49157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
492648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
493648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
494648b4a99SJiri Pirko 					      trans);
49511149536SVivien Didelot 		break;
496ba14d9ebSVivien Didelot 	default:
497ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
498ba14d9ebSVivien Didelot 		break;
499ba14d9ebSVivien Didelot 	}
500ba14d9ebSVivien Didelot 
501ba14d9ebSVivien Didelot 	return err;
502ba14d9ebSVivien Didelot }
503ba14d9ebSVivien Didelot 
504ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
505648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
506ba14d9ebSVivien Didelot {
507ba14d9ebSVivien Didelot 	int err;
508ba14d9ebSVivien Didelot 
5099e8f4a54SJiri Pirko 	switch (obj->id) {
51057d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
511648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_del(dev,
512648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj));
513ba14d9ebSVivien Didelot 		break;
5148df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5158df30255SVivien Didelot 		err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
5168df30255SVivien Didelot 		break;
51757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
518648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
519648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
52011149536SVivien Didelot 		break;
521ba14d9ebSVivien Didelot 	default:
522ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
523ba14d9ebSVivien Didelot 		break;
524ba14d9ebSVivien Didelot 	}
525ba14d9ebSVivien Didelot 
526ba14d9ebSVivien Didelot 	return err;
527ba14d9ebSVivien Didelot }
528ba14d9ebSVivien Didelot 
529ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
530648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
531648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
532ba14d9ebSVivien Didelot {
533ba14d9ebSVivien Didelot 	int err;
534ba14d9ebSVivien Didelot 
5359e8f4a54SJiri Pirko 	switch (obj->id) {
53657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
537648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_dump(dev,
538648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_FDB(obj),
539648b4a99SJiri Pirko 					      cb);
540ba14d9ebSVivien Didelot 		break;
5418df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5428df30255SVivien Didelot 		err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5438df30255SVivien Didelot 					      cb);
5448df30255SVivien Didelot 		break;
54557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
546648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
547648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
548648b4a99SJiri Pirko 					       cb);
54911149536SVivien Didelot 		break;
550ba14d9ebSVivien Didelot 	default:
551ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
552ba14d9ebSVivien Didelot 		break;
553ba14d9ebSVivien Didelot 	}
554ba14d9ebSVivien Didelot 
555ba14d9ebSVivien Didelot 	return err;
556ba14d9ebSVivien Didelot }
557ba14d9ebSVivien Didelot 
558b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
559b73adef6SFlorian Fainelli 				      struct net_device *br)
560b73adef6SFlorian Fainelli {
561b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
562afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
563b73adef6SFlorian Fainelli 	int ret = -EOPNOTSUPP;
564b73adef6SFlorian Fainelli 
565a5e9a02eSVivien Didelot 	p->dp->bridge_dev = br;
566b73adef6SFlorian Fainelli 
5679d490b4eSVivien Didelot 	if (ds->ops->port_bridge_join)
568afdcf151SVivien Didelot 		ret = ds->ops->port_bridge_join(ds, p->dp->index, br);
569b73adef6SFlorian Fainelli 
5706debb68aSVivien Didelot 	return ret == -EOPNOTSUPP ? 0 : ret;
571b73adef6SFlorian Fainelli }
572b73adef6SFlorian Fainelli 
573f123f2fbSVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev,
574f123f2fbSVivien Didelot 					struct net_device *br)
575b73adef6SFlorian Fainelli {
576b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
577afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
578b73adef6SFlorian Fainelli 
579f123f2fbSVivien Didelot 	p->dp->bridge_dev = NULL;
580b73adef6SFlorian Fainelli 
5819d490b4eSVivien Didelot 	if (ds->ops->port_bridge_leave)
582f123f2fbSVivien Didelot 		ds->ops->port_bridge_leave(ds, p->dp->index, br);
583b73adef6SFlorian Fainelli 
584b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
585b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
586b73adef6SFlorian Fainelli 	 */
587afdcf151SVivien Didelot 	dsa_port_set_stp_state(ds, p->dp->index, BR_STATE_FORWARDING);
588b73adef6SFlorian Fainelli }
589b73adef6SFlorian Fainelli 
590f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
591f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
592b73adef6SFlorian Fainelli {
593b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
594afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
595b73adef6SFlorian Fainelli 
596f8e20a9fSScott Feldman 	switch (attr->id) {
5971f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
59842275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
59942275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
600f8e20a9fSScott Feldman 		break;
601f8e20a9fSScott Feldman 	default:
602f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
603f8e20a9fSScott Feldman 	}
604b73adef6SFlorian Fainelli 
605b73adef6SFlorian Fainelli 	return 0;
606b73adef6SFlorian Fainelli }
607b73adef6SFlorian Fainelli 
60804ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
60904ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
61004ff53f9SFlorian Fainelli {
61104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
61204ff53f9SFlorian Fainelli 	if (p->netpoll)
61304ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
61404ff53f9SFlorian Fainelli #else
61504ff53f9SFlorian Fainelli 	BUG();
61604ff53f9SFlorian Fainelli #endif
61704ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
61804ff53f9SFlorian Fainelli }
61904ff53f9SFlorian Fainelli 
6203e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6213e8a72d1SFlorian Fainelli {
6223e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6234ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6243e8a72d1SFlorian Fainelli 
6254ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
6264ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
6273e8a72d1SFlorian Fainelli 
6284ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
6294ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
6304ed70ce9SFlorian Fainelli 	if (!nskb)
6314ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
6325aed85ceSFlorian Fainelli 
63304ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
63404ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
63504ff53f9SFlorian Fainelli 	 */
63604ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
63704ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
63804ff53f9SFlorian Fainelli 
6394ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
6404ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
6414ed70ce9SFlorian Fainelli 	 */
642afdcf151SVivien Didelot 	nskb->dev = p->dp->ds->dst->master_netdev;
6434ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
6445aed85ceSFlorian Fainelli 
6455aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
6465aed85ceSFlorian Fainelli }
6475aed85ceSFlorian Fainelli 
64891da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
64991da11f8SLennert Buytenhek static int
650bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
651bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
65291da11f8SLennert Buytenhek {
65391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
65491da11f8SLennert Buytenhek 	int err;
65591da11f8SLennert Buytenhek 
65691da11f8SLennert Buytenhek 	err = -EOPNOTSUPP;
65791da11f8SLennert Buytenhek 	if (p->phy != NULL) {
65891da11f8SLennert Buytenhek 		err = phy_read_status(p->phy);
65991da11f8SLennert Buytenhek 		if (err == 0)
660bb10bb3eSPhilippe Reynes 			err = phy_ethtool_ksettings_get(p->phy, cmd);
66191da11f8SLennert Buytenhek 	}
66291da11f8SLennert Buytenhek 
66391da11f8SLennert Buytenhek 	return err;
66491da11f8SLennert Buytenhek }
66591da11f8SLennert Buytenhek 
66691da11f8SLennert Buytenhek static int
667bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
668bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
66991da11f8SLennert Buytenhek {
67091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
67191da11f8SLennert Buytenhek 
67291da11f8SLennert Buytenhek 	if (p->phy != NULL)
673bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
67491da11f8SLennert Buytenhek 
67591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
67691da11f8SLennert Buytenhek }
67791da11f8SLennert Buytenhek 
67891da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
67991da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
68091da11f8SLennert Buytenhek {
6817826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6827826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6837826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
68491da11f8SLennert Buytenhek }
68591da11f8SLennert Buytenhek 
6863d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6873d762a0fSGuenter Roeck {
6883d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
689afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
6903d762a0fSGuenter Roeck 
6919d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
692afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
6933d762a0fSGuenter Roeck 
6943d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6953d762a0fSGuenter Roeck }
6963d762a0fSGuenter Roeck 
6973d762a0fSGuenter Roeck static void
6983d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6993d762a0fSGuenter Roeck {
7003d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
701afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7023d762a0fSGuenter Roeck 
7039d490b4eSVivien Didelot 	if (ds->ops->get_regs)
704afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
7053d762a0fSGuenter Roeck }
7063d762a0fSGuenter Roeck 
70791da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
70891da11f8SLennert Buytenhek {
70991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
71091da11f8SLennert Buytenhek 
71191da11f8SLennert Buytenhek 	if (p->phy != NULL)
71291da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
71391da11f8SLennert Buytenhek 
71491da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
71591da11f8SLennert Buytenhek }
71691da11f8SLennert Buytenhek 
71791da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
71891da11f8SLennert Buytenhek {
71991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
72091da11f8SLennert Buytenhek 
72191da11f8SLennert Buytenhek 	if (p->phy != NULL) {
72291da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
72391da11f8SLennert Buytenhek 		return p->phy->link;
72491da11f8SLennert Buytenhek 	}
72591da11f8SLennert Buytenhek 
72691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
72791da11f8SLennert Buytenhek }
72891da11f8SLennert Buytenhek 
7296793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
7306793abb4SGuenter Roeck {
7316793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
732afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7336793abb4SGuenter Roeck 
7340e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
735ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
7366793abb4SGuenter Roeck 
7379d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7389d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7396793abb4SGuenter Roeck 
7406793abb4SGuenter Roeck 	return 0;
7416793abb4SGuenter Roeck }
7426793abb4SGuenter Roeck 
7436793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7446793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7456793abb4SGuenter Roeck {
7466793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
747afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7486793abb4SGuenter Roeck 
7499d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7509d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7516793abb4SGuenter Roeck 
7526793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7536793abb4SGuenter Roeck }
7546793abb4SGuenter Roeck 
7556793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7566793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7576793abb4SGuenter Roeck {
7586793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
759afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7606793abb4SGuenter Roeck 
7619d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7629d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7636793abb4SGuenter Roeck 
7646793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7656793abb4SGuenter Roeck }
7666793abb4SGuenter Roeck 
76791da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
76891da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
76991da11f8SLennert Buytenhek {
77091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
771afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
77291da11f8SLennert Buytenhek 
77391da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
77491da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
77591da11f8SLennert Buytenhek 
77691da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
77791da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
77891da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
77991da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
7809d490b4eSVivien Didelot 		if (ds->ops->get_strings)
781afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
78291da11f8SLennert Buytenhek 	}
78391da11f8SLennert Buytenhek }
78491da11f8SLennert Buytenhek 
785badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
786badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
787badf3adaSFlorian Fainelli 					   uint64_t *data)
788badf3adaSFlorian Fainelli {
789badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
7909520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
791badf3adaSFlorian Fainelli 	s8 cpu_port = dst->cpu_port;
792badf3adaSFlorian Fainelli 	int count = 0;
793badf3adaSFlorian Fainelli 
794badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
795badf3adaSFlorian Fainelli 		count = dst->master_ethtool_ops.get_sset_count(dev,
796badf3adaSFlorian Fainelli 							       ETH_SS_STATS);
797badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
798badf3adaSFlorian Fainelli 	}
799badf3adaSFlorian Fainelli 
8009d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
8019d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
802badf3adaSFlorian Fainelli }
803badf3adaSFlorian Fainelli 
804badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
805badf3adaSFlorian Fainelli {
806badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8079520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
808badf3adaSFlorian Fainelli 	int count = 0;
809badf3adaSFlorian Fainelli 
810badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count)
811badf3adaSFlorian Fainelli 		count += dst->master_ethtool_ops.get_sset_count(dev, sset);
812badf3adaSFlorian Fainelli 
8139d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
8149d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
815badf3adaSFlorian Fainelli 
816badf3adaSFlorian Fainelli 	return count;
817badf3adaSFlorian Fainelli }
818badf3adaSFlorian Fainelli 
819badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
820badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
821badf3adaSFlorian Fainelli {
822badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8239520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
824badf3adaSFlorian Fainelli 	s8 cpu_port = dst->cpu_port;
825badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
826badf3adaSFlorian Fainelli 	int mcount = 0, count;
827badf3adaSFlorian Fainelli 	unsigned int i;
828badf3adaSFlorian Fainelli 	uint8_t pfx[4];
829badf3adaSFlorian Fainelli 	uint8_t *ndata;
830badf3adaSFlorian Fainelli 
831badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
832badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
833badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
834badf3adaSFlorian Fainelli 
835badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
836badf3adaSFlorian Fainelli 		mcount = dst->master_ethtool_ops.get_sset_count(dev,
837badf3adaSFlorian Fainelli 								ETH_SS_STATS);
838badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_strings(dev, stringset, data);
839badf3adaSFlorian Fainelli 	}
840badf3adaSFlorian Fainelli 
8419d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
842badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
843badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
844badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
845badf3adaSFlorian Fainelli 		 * constructed earlier
846badf3adaSFlorian Fainelli 		 */
8479d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
8489d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
849badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
850badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
851badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
852badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
853badf3adaSFlorian Fainelli 		}
854badf3adaSFlorian Fainelli 	}
855badf3adaSFlorian Fainelli }
856badf3adaSFlorian Fainelli 
85791da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
85891da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
85991da11f8SLennert Buytenhek 					uint64_t *data)
86091da11f8SLennert Buytenhek {
86191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
862afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
86391da11f8SLennert Buytenhek 
86446e7b8d8SVivien Didelot 	data[0] = dev->stats.tx_packets;
86546e7b8d8SVivien Didelot 	data[1] = dev->stats.tx_bytes;
86646e7b8d8SVivien Didelot 	data[2] = dev->stats.rx_packets;
86746e7b8d8SVivien Didelot 	data[3] = dev->stats.rx_bytes;
8689d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
869afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
87091da11f8SLennert Buytenhek }
87191da11f8SLennert Buytenhek 
87291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
87391da11f8SLennert Buytenhek {
87491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
875afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
87691da11f8SLennert Buytenhek 
87791da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
87891da11f8SLennert Buytenhek 		int count;
87991da11f8SLennert Buytenhek 
88091da11f8SLennert Buytenhek 		count = 4;
8819d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
8829d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
88391da11f8SLennert Buytenhek 
88491da11f8SLennert Buytenhek 		return count;
88591da11f8SLennert Buytenhek 	}
88691da11f8SLennert Buytenhek 
88791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
88891da11f8SLennert Buytenhek }
88991da11f8SLennert Buytenhek 
89019e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
89119e57c4eSFlorian Fainelli {
89219e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
893afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
89419e57c4eSFlorian Fainelli 
8959d490b4eSVivien Didelot 	if (ds->ops->get_wol)
896afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
89719e57c4eSFlorian Fainelli }
89819e57c4eSFlorian Fainelli 
89919e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
90019e57c4eSFlorian Fainelli {
90119e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
902afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
90319e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
90419e57c4eSFlorian Fainelli 
9059d490b4eSVivien Didelot 	if (ds->ops->set_wol)
906afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
90719e57c4eSFlorian Fainelli 
90819e57c4eSFlorian Fainelli 	return ret;
90919e57c4eSFlorian Fainelli }
91019e57c4eSFlorian Fainelli 
9117905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
9127905288fSFlorian Fainelli {
9137905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
914afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9157905288fSFlorian Fainelli 	int ret;
9167905288fSFlorian Fainelli 
9179d490b4eSVivien Didelot 	if (!ds->ops->set_eee)
9187905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9197905288fSFlorian Fainelli 
920afdcf151SVivien Didelot 	ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
9217905288fSFlorian Fainelli 	if (ret)
9227905288fSFlorian Fainelli 		return ret;
9237905288fSFlorian Fainelli 
9247905288fSFlorian Fainelli 	if (p->phy)
9257905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
9267905288fSFlorian Fainelli 
9277905288fSFlorian Fainelli 	return ret;
9287905288fSFlorian Fainelli }
9297905288fSFlorian Fainelli 
9307905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
9317905288fSFlorian Fainelli {
9327905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
933afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9347905288fSFlorian Fainelli 	int ret;
9357905288fSFlorian Fainelli 
9369d490b4eSVivien Didelot 	if (!ds->ops->get_eee)
9377905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9387905288fSFlorian Fainelli 
939afdcf151SVivien Didelot 	ret = ds->ops->get_eee(ds, p->dp->index, e);
9407905288fSFlorian Fainelli 	if (ret)
9417905288fSFlorian Fainelli 		return ret;
9427905288fSFlorian Fainelli 
9437905288fSFlorian Fainelli 	if (p->phy)
9447905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
9457905288fSFlorian Fainelli 
9467905288fSFlorian Fainelli 	return ret;
9477905288fSFlorian Fainelli }
9487905288fSFlorian Fainelli 
94904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
95004ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
95104ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
95204ff53f9SFlorian Fainelli {
95304ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
954afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
95504ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
95604ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
95704ff53f9SFlorian Fainelli 	int err = 0;
95804ff53f9SFlorian Fainelli 
95904ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
96004ff53f9SFlorian Fainelli 	if (!netpoll)
96104ff53f9SFlorian Fainelli 		return -ENOMEM;
96204ff53f9SFlorian Fainelli 
96304ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
96404ff53f9SFlorian Fainelli 	if (err) {
96504ff53f9SFlorian Fainelli 		kfree(netpoll);
96604ff53f9SFlorian Fainelli 		goto out;
96704ff53f9SFlorian Fainelli 	}
96804ff53f9SFlorian Fainelli 
96904ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
97004ff53f9SFlorian Fainelli out:
97104ff53f9SFlorian Fainelli 	return err;
97204ff53f9SFlorian Fainelli }
97304ff53f9SFlorian Fainelli 
97404ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
97504ff53f9SFlorian Fainelli {
97604ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
97704ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
97804ff53f9SFlorian Fainelli 
97904ff53f9SFlorian Fainelli 	if (!netpoll)
98004ff53f9SFlorian Fainelli 		return;
98104ff53f9SFlorian Fainelli 
98204ff53f9SFlorian Fainelli 	p->netpoll = NULL;
98304ff53f9SFlorian Fainelli 
98404ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
98504ff53f9SFlorian Fainelli }
98604ff53f9SFlorian Fainelli 
98704ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
98804ff53f9SFlorian Fainelli {
98904ff53f9SFlorian Fainelli }
99004ff53f9SFlorian Fainelli #endif
99104ff53f9SFlorian Fainelli 
99244bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
99344bb765cSFlorian Fainelli 					char *name, size_t len)
99444bb765cSFlorian Fainelli {
99544bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
99644bb765cSFlorian Fainelli 
997afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
99844bb765cSFlorian Fainelli 		return -EINVAL;
9993a543ef4SFlorian Fainelli 
10003a543ef4SFlorian Fainelli 	return 0;
10013a543ef4SFlorian Fainelli }
10023a543ef4SFlorian Fainelli 
1003f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
1004f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1005f50f2127SFlorian Fainelli 			     unsigned long cookie)
1006f50f2127SFlorian Fainelli {
1007f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1008f50f2127SFlorian Fainelli 
1009f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1010f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1011f50f2127SFlorian Fainelli 			return mall_tc_entry;
1012f50f2127SFlorian Fainelli 
1013f50f2127SFlorian Fainelli 	return NULL;
1014f50f2127SFlorian Fainelli }
1015f50f2127SFlorian Fainelli 
1016f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
1017f50f2127SFlorian Fainelli 				      __be16 protocol,
1018f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
1019f50f2127SFlorian Fainelli 				      bool ingress)
1020f50f2127SFlorian Fainelli {
1021f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1022f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1023f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1024f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
1025f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
1026f50f2127SFlorian Fainelli 	struct net_device *to_dev;
1027f50f2127SFlorian Fainelli 	const struct tc_action *a;
1028f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
1029f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
1030f50f2127SFlorian Fainelli 	int ifindex;
1031f50f2127SFlorian Fainelli 
1032f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
1033f50f2127SFlorian Fainelli 		return err;
1034f50f2127SFlorian Fainelli 
1035f50f2127SFlorian Fainelli 	if (!tc_single_action(cls->exts))
1036f50f2127SFlorian Fainelli 		return err;
1037f50f2127SFlorian Fainelli 
1038f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
1039f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
1040f50f2127SFlorian Fainelli 
1041f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1042f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
1043f50f2127SFlorian Fainelli 
1044f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
1045f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
1046f50f2127SFlorian Fainelli 		if (!to_dev)
1047f50f2127SFlorian Fainelli 			return -EINVAL;
1048f50f2127SFlorian Fainelli 
1049f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
1050f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
1051f50f2127SFlorian Fainelli 
1052f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1053f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
1054f50f2127SFlorian Fainelli 			return -ENOMEM;
1055f50f2127SFlorian Fainelli 
1056f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
1057f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1058f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
1059f50f2127SFlorian Fainelli 
1060f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
1061f50f2127SFlorian Fainelli 
1062f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
1063f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
1064f50f2127SFlorian Fainelli 
1065f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1066f50f2127SFlorian Fainelli 					       ingress);
1067f50f2127SFlorian Fainelli 		if (err) {
1068f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
1069f50f2127SFlorian Fainelli 			return err;
1070f50f2127SFlorian Fainelli 		}
1071f50f2127SFlorian Fainelli 
1072f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1073f50f2127SFlorian Fainelli 	}
1074f50f2127SFlorian Fainelli 
1075f50f2127SFlorian Fainelli 	return 0;
1076f50f2127SFlorian Fainelli }
1077f50f2127SFlorian Fainelli 
1078f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1079f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1080f50f2127SFlorian Fainelli {
1081f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1082f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1083f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1084f50f2127SFlorian Fainelli 
1085f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
1086f50f2127SFlorian Fainelli 		return;
1087f50f2127SFlorian Fainelli 
1088f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1089f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1090f50f2127SFlorian Fainelli 		return;
1091f50f2127SFlorian Fainelli 
1092f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1093f50f2127SFlorian Fainelli 
1094f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1095f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
1096f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
1097f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
1098f50f2127SFlorian Fainelli 		break;
1099f50f2127SFlorian Fainelli 	default:
1100f50f2127SFlorian Fainelli 		WARN_ON(1);
1101f50f2127SFlorian Fainelli 	}
1102f50f2127SFlorian Fainelli 
1103f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1104f50f2127SFlorian Fainelli }
1105f50f2127SFlorian Fainelli 
1106f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1107f50f2127SFlorian Fainelli 			      __be16 protocol, struct tc_to_netdev *tc)
1108f50f2127SFlorian Fainelli {
1109f50f2127SFlorian Fainelli 	bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1110f50f2127SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1111f50f2127SFlorian Fainelli 
1112f50f2127SFlorian Fainelli 	switch (tc->type) {
1113f50f2127SFlorian Fainelli 	case TC_SETUP_MATCHALL:
1114f50f2127SFlorian Fainelli 		switch (tc->cls_mall->command) {
1115f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_REPLACE:
1116f50f2127SFlorian Fainelli 			return dsa_slave_add_cls_matchall(dev, protocol,
1117f50f2127SFlorian Fainelli 							  tc->cls_mall,
1118f50f2127SFlorian Fainelli 							  ingress);
1119f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_DESTROY:
1120f50f2127SFlorian Fainelli 			dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1121f50f2127SFlorian Fainelli 			return 0;
1122f50f2127SFlorian Fainelli 		}
1123f50f2127SFlorian Fainelli 	default:
1124f50f2127SFlorian Fainelli 		break;
1125f50f2127SFlorian Fainelli 	}
1126f50f2127SFlorian Fainelli 
1127f50f2127SFlorian Fainelli 	return ret;
1128f50f2127SFlorian Fainelli }
1129f50f2127SFlorian Fainelli 
1130af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1131af42192cSFlorian Fainelli {
1132af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
1133af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1134af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
1135af42192cSFlorian Fainelli }
1136af42192cSFlorian Fainelli 
1137bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1138bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1139bf9f2648SFlorian Fainelli {
1140bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1141bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1142bf9f2648SFlorian Fainelli 
1143bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1144bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1145bf9f2648SFlorian Fainelli 
1146bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1147bf9f2648SFlorian Fainelli }
1148bf9f2648SFlorian Fainelli 
1149bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1150bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1151bf9f2648SFlorian Fainelli {
1152bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1153bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1154bf9f2648SFlorian Fainelli 
1155bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1156bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1157bf9f2648SFlorian Fainelli 
1158bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1159bf9f2648SFlorian Fainelli }
1160bf9f2648SFlorian Fainelli 
116191da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
116291da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
11633d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
11643d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
116591da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
116691da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
11676793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
11686793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
11696793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
117091da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
117191da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
117291da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
117319e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
117419e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
11757905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
11767905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1177bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1178bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1179bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1180bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
118191da11f8SLennert Buytenhek };
118291da11f8SLennert Buytenhek 
11833e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1184d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1185d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
11863e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1187d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1188d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1189d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1190ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
1191ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
1192ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
1193d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1194abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
119504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
119604ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
119704ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
119804ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
119904ff53f9SFlorian Fainelli #endif
120011149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
120111149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
120211149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
120344bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1204f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
120598237d43SScott Feldman };
120698237d43SScott Feldman 
12079d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1208f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
120935636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1210ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1211ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1212ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
1213d442ad4aSStephen Hemminger };
121491da11f8SLennert Buytenhek 
1215f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1216f37db85dSFlorian Fainelli 	.name	= "dsa",
1217f37db85dSFlorian Fainelli };
1218f37db85dSFlorian Fainelli 
12190d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
12200d8bcdd3SFlorian Fainelli {
12210d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1222afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12230d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
12240d8bcdd3SFlorian Fainelli 
12250d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
12260d8bcdd3SFlorian Fainelli 		status_changed = 1;
12270d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
12280d8bcdd3SFlorian Fainelli 	}
12290d8bcdd3SFlorian Fainelli 
12300d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
12310d8bcdd3SFlorian Fainelli 		status_changed = 1;
12320d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
12330d8bcdd3SFlorian Fainelli 	}
12340d8bcdd3SFlorian Fainelli 
12350d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
12360d8bcdd3SFlorian Fainelli 		status_changed = 1;
12370d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
12380d8bcdd3SFlorian Fainelli 	}
12390d8bcdd3SFlorian Fainelli 
12409d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1241afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1242ec9436baSFlorian Fainelli 
12430d8bcdd3SFlorian Fainelli 	if (status_changed)
12440d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
12450d8bcdd3SFlorian Fainelli }
12460d8bcdd3SFlorian Fainelli 
1247ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1248ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1249ce31b31cSFlorian Fainelli {
1250b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1251b71be352SAndrew Lunn 	struct dsa_switch *ds;
1252ce31b31cSFlorian Fainelli 
1253b71be352SAndrew Lunn 	if (dev) {
1254b71be352SAndrew Lunn 		p = netdev_priv(dev);
1255afdcf151SVivien Didelot 		ds = p->dp->ds;
12569d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1257afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1258b71be352SAndrew Lunn 	}
1259ce31b31cSFlorian Fainelli 
1260ce31b31cSFlorian Fainelli 	return 0;
1261ce31b31cSFlorian Fainelli }
1262ce31b31cSFlorian Fainelli 
126391da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1264c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1265cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1266cd28a1a9SFlorian Fainelli 				 int addr)
1267c305c165SFlorian Fainelli {
1268afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1269c305c165SFlorian Fainelli 
12707f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1271d25b8e74SRussell King 	if (!p->phy) {
1272d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1273c305c165SFlorian Fainelli 		return -ENODEV;
1274d25b8e74SRussell King 	}
1275c305c165SFlorian Fainelli 
1276c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1277211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1278c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
12794078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1280c305c165SFlorian Fainelli 				  p->phy_interface);
1281c305c165SFlorian Fainelli }
1282c305c165SFlorian Fainelli 
12839697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
12840d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
12850d8bcdd3SFlorian Fainelli {
1286afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12870d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1288ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
12896819563eSFlorian Fainelli 	u32 phy_flags = 0;
129019334920SGuenter Roeck 	int mode, ret;
12910d8bcdd3SFlorian Fainelli 
1292afdcf151SVivien Didelot 	port_dn = p->dp->dn;
129319334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
129419334920SGuenter Roeck 	if (mode < 0)
129519334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
129619334920SGuenter Roeck 	p->phy_interface = mode;
12970d8bcdd3SFlorian Fainelli 
12980d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
12990d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
13000d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
13010d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
13020d8bcdd3SFlorian Fainelli 		 */
13030d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
13040d8bcdd3SFlorian Fainelli 		if (ret) {
1305d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
13069697f1cdSFlorian Fainelli 			return ret;
13070d8bcdd3SFlorian Fainelli 		}
1308ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
13090d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
13100d8bcdd3SFlorian Fainelli 	}
13110d8bcdd3SFlorian Fainelli 
13129d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1313afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
13146819563eSFlorian Fainelli 
1315cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1316d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1317d25b8e74SRussell King 
1318cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1319cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1320cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1321cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1322cd28a1a9SFlorian Fainelli 		 */
1323d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1324d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1325d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1326d25b8e74SRussell King 			if (ret) {
1327d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
13280d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1329cd28a1a9SFlorian Fainelli 				return ret;
1330d25b8e74SRussell King 			}
1331cd28a1a9SFlorian Fainelli 		} else {
13320d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1333cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1334cd28a1a9SFlorian Fainelli 						phy_flags,
13350d8bcdd3SFlorian Fainelli 						p->phy_interface);
1336cd28a1a9SFlorian Fainelli 		}
13370d8f3c67SJohan Hovold 
13380d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1339cd28a1a9SFlorian Fainelli 	}
13400d8bcdd3SFlorian Fainelli 
1341ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1342ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1343ce31b31cSFlorian Fainelli 
13440d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
13450d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
13460d8bcdd3SFlorian Fainelli 	 */
1347b31f65fbSAndrew Lunn 	if (!p->phy) {
1348afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1349d25b8e74SRussell King 		if (ret) {
1350afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1351afdcf151SVivien Didelot 				   p->dp->index, ret);
1352881eadabSJohan Hovold 			if (phy_is_fixed)
1353881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1354c305c165SFlorian Fainelli 			return ret;
1355d25b8e74SRussell King 		}
13560d8bcdd3SFlorian Fainelli 	}
13579697f1cdSFlorian Fainelli 
13582220943aSAndrew Lunn 	phy_attached_info(p->phy);
13592220943aSAndrew Lunn 
13609697f1cdSFlorian Fainelli 	return 0;
1361b31f65fbSAndrew Lunn }
13620d8bcdd3SFlorian Fainelli 
1363448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1364448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1365448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1366448b4482SAndrew Lunn 					    void *_unused)
1367448b4482SAndrew Lunn {
1368448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1369448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1370448b4482SAndrew Lunn }
1371448b4482SAndrew Lunn 
137224462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
137324462549SFlorian Fainelli {
137424462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
137524462549SFlorian Fainelli 
1376f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1377f154be24SFlorian Fainelli 
137824462549SFlorian Fainelli 	if (p->phy) {
137924462549SFlorian Fainelli 		phy_stop(p->phy);
138024462549SFlorian Fainelli 		p->old_pause = -1;
138124462549SFlorian Fainelli 		p->old_link = -1;
138224462549SFlorian Fainelli 		p->old_duplex = -1;
138324462549SFlorian Fainelli 		phy_suspend(p->phy);
138424462549SFlorian Fainelli 	}
138524462549SFlorian Fainelli 
138624462549SFlorian Fainelli 	return 0;
138724462549SFlorian Fainelli }
138824462549SFlorian Fainelli 
138924462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
139024462549SFlorian Fainelli {
139124462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
139224462549SFlorian Fainelli 
139324462549SFlorian Fainelli 	netif_device_attach(slave_dev);
139424462549SFlorian Fainelli 
139524462549SFlorian Fainelli 	if (p->phy) {
139624462549SFlorian Fainelli 		phy_resume(p->phy);
139724462549SFlorian Fainelli 		phy_start(p->phy);
139824462549SFlorian Fainelli 	}
139924462549SFlorian Fainelli 
140024462549SFlorian Fainelli 	return 0;
140124462549SFlorian Fainelli }
140224462549SFlorian Fainelli 
1403d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
140483c0afaeSAndrew Lunn 		     int port, const char *name)
140591da11f8SLennert Buytenhek {
1406badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
140783c0afaeSAndrew Lunn 	struct net_device *master;
140891da11f8SLennert Buytenhek 	struct net_device *slave_dev;
140991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
141091da11f8SLennert Buytenhek 	int ret;
141191da11f8SLennert Buytenhek 
141283c0afaeSAndrew Lunn 	master = ds->dst->master_netdev;
141383c0afaeSAndrew Lunn 	if (ds->master_netdev)
141483c0afaeSAndrew Lunn 		master = ds->master_netdev;
141583c0afaeSAndrew Lunn 
1416c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1417c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
141891da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1419d87d6f44SGuenter Roeck 		return -ENOMEM;
142091da11f8SLennert Buytenhek 
1421f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1422f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14237ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14242fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
14250a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14263e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14279d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
14288b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14298b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1430f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1431d442ad4aSStephen Hemminger 
1432448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1433448b4482SAndrew Lunn 				 NULL);
1434448b4482SAndrew Lunn 
143591da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1436189b0d93SAndrew Lunn 	slave_dev->dev.of_node = ds->ports[port].dn;
143791da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
143891da11f8SLennert Buytenhek 
143991da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
1440afdcf151SVivien Didelot 	p->dp = &ds->ports[port];
1441f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
144239a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
14435075314eSAlexander Duyck 
14440d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
14450d8bcdd3SFlorian Fainelli 	p->old_link = -1;
14460d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
14470d8bcdd3SFlorian Fainelli 
1448c8b09808SAndrew Lunn 	ds->ports[port].netdev = slave_dev;
144991da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
145091da11f8SLennert Buytenhek 	if (ret) {
1451a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1452a2ae6007SJoe Perches 			   ret, slave_dev->name);
1453c8b09808SAndrew Lunn 		ds->ports[port].netdev = NULL;
145491da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1455d87d6f44SGuenter Roeck 		return ret;
145691da11f8SLennert Buytenhek 	}
145791da11f8SLennert Buytenhek 
145891da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
145991da11f8SLennert Buytenhek 
14600071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
14610071f56eSAndrew Lunn 	if (ret) {
14620071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
146373dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
14640071f56eSAndrew Lunn 		free_netdev(slave_dev);
14650071f56eSAndrew Lunn 		return ret;
14660071f56eSAndrew Lunn 	}
14670071f56eSAndrew Lunn 
1468d87d6f44SGuenter Roeck 	return 0;
146991da11f8SLennert Buytenhek }
1470b73adef6SFlorian Fainelli 
1471cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1472cda5c15bSNeil Armstrong {
1473cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1474881eadabSJohan Hovold 	struct device_node *port_dn;
1475881eadabSJohan Hovold 
1476afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1477cda5c15bSNeil Armstrong 
1478cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1479881eadabSJohan Hovold 	if (p->phy) {
1480cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1481881eadabSJohan Hovold 
1482881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1483881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1484881eadabSJohan Hovold 	}
1485cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1486cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1487cda5c15bSNeil Armstrong }
1488cda5c15bSNeil Armstrong 
1489b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1490b73adef6SFlorian Fainelli {
1491b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1492b73adef6SFlorian Fainelli }
1493b73adef6SFlorian Fainelli 
1494*8e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
1495*8e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1496b73adef6SFlorian Fainelli {
1497*8e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1498b73adef6SFlorian Fainelli 
1499*8e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
1500*8e92ab3aSVivien Didelot 		if (info->linking) {
1501*8e92ab3aSVivien Didelot 			err = dsa_slave_bridge_port_join(dev, info->upper_dev);
1502*8e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
1503*8e92ab3aSVivien Didelot 		} else {
1504*8e92ab3aSVivien Didelot 			dsa_slave_bridge_port_leave(dev, info->upper_dev);
1505*8e92ab3aSVivien Didelot 			err = NOTIFY_OK;
1506*8e92ab3aSVivien Didelot 		}
15076debb68aSVivien Didelot 	}
1508b73adef6SFlorian Fainelli 
1509*8e92ab3aSVivien Didelot 	return err;
1510b73adef6SFlorian Fainelli }
1511b73adef6SFlorian Fainelli 
151288e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1513b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1514b73adef6SFlorian Fainelli {
15156debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1516b73adef6SFlorian Fainelli 
1517*8e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
1518*8e92ab3aSVivien Didelot 		return NOTIFY_DONE;
1519*8e92ab3aSVivien Didelot 
1520*8e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
1521*8e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1522b73adef6SFlorian Fainelli 
1523b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1524b73adef6SFlorian Fainelli }
152588e4f0caSVivien Didelot 
152688e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
152788e4f0caSVivien Didelot 	.notifier_call	= dsa_slave_netdevice_event,
152888e4f0caSVivien Didelot };
152988e4f0caSVivien Didelot 
153088e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
153188e4f0caSVivien Didelot {
153288e4f0caSVivien Didelot 	return register_netdevice_notifier(&dsa_slave_nb);
153388e4f0caSVivien Didelot }
153488e4f0caSVivien Didelot 
153588e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
153688e4f0caSVivien Didelot {
153788e4f0caSVivien Didelot 	int err;
153888e4f0caSVivien Didelot 
153988e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
154088e4f0caSVivien Didelot 	if (err)
154188e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
154288e4f0caSVivien Didelot }
1543