xref: /openbmc/linux/net/dsa/slave.c (revision e69e46261063a25c3907bed16a2e9d18b115d1fd)
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 
3004d3a4c6SVivien Didelot static int dsa_slave_notify(struct net_device *dev, unsigned long e, void *v)
3104d3a4c6SVivien Didelot {
3204d3a4c6SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
3304d3a4c6SVivien Didelot 	struct raw_notifier_head *nh = &p->dp->ds->dst->nh;
3404d3a4c6SVivien Didelot 	int err;
3504d3a4c6SVivien Didelot 
3604d3a4c6SVivien Didelot 	err = raw_notifier_call_chain(nh, e, v);
3704d3a4c6SVivien Didelot 
3804d3a4c6SVivien Didelot 	return notifier_to_errno(err);
3904d3a4c6SVivien Didelot }
4004d3a4c6SVivien Didelot 
4191da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
4291da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
4391da11f8SLennert Buytenhek {
4491da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4591da11f8SLennert Buytenhek 
460d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
479d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
4891da11f8SLennert Buytenhek 
4991da11f8SLennert Buytenhek 	return 0xffff;
5091da11f8SLennert Buytenhek }
5191da11f8SLennert Buytenhek 
5291da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
5391da11f8SLennert Buytenhek {
5491da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
5591da11f8SLennert Buytenhek 
560d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
579d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek 	return 0;
6091da11f8SLennert Buytenhek }
6191da11f8SLennert Buytenhek 
6291da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
6391da11f8SLennert Buytenhek {
6491da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
6591da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
6691da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
6791da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
680b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
690b7b498dSFlorian Fainelli 		 ds->dst->tree, ds->index);
70c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
7124df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
7291da11f8SLennert Buytenhek }
7391da11f8SLennert Buytenhek 
7491da11f8SLennert Buytenhek 
7591da11f8SLennert Buytenhek /* slave device handling ****************************************************/
76abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
77c0840801SLennert Buytenhek {
78c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
79c0840801SLennert Buytenhek 
80afdcf151SVivien Didelot 	return p->dp->ds->dst->master_netdev->ifindex;
81c0840801SLennert Buytenhek }
82c0840801SLennert Buytenhek 
83a5e9a02eSVivien Didelot static inline bool dsa_port_is_bridged(struct dsa_port *dp)
84b73adef6SFlorian Fainelli {
85a5e9a02eSVivien Didelot 	return !!dp->bridge_dev;
86b73adef6SFlorian Fainelli }
87b73adef6SFlorian Fainelli 
88c5d35cb3SVivien Didelot static void dsa_slave_set_state(struct net_device *dev, u8 state)
894acfee81SVivien Didelot {
90c5d35cb3SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
91c5d35cb3SVivien Didelot 	struct dsa_port *dp = p->dp;
92c5d35cb3SVivien Didelot 	struct dsa_switch *ds = dp->ds;
93c5d35cb3SVivien Didelot 	int port = dp->index;
94732f794cSVivien Didelot 
954acfee81SVivien Didelot 	if (ds->ops->port_stp_state_set)
964acfee81SVivien Didelot 		ds->ops->port_stp_state_set(ds, port, state);
97732f794cSVivien Didelot 
98732f794cSVivien Didelot 	if (ds->ops->port_fast_age) {
99732f794cSVivien Didelot 		/* Fast age FDB entries or flush appropriate forwarding database
100732f794cSVivien Didelot 		 * for the given port, if we are moving it from Learning or
101732f794cSVivien Didelot 		 * Forwarding state, to Disabled or Blocking or Listening state.
102732f794cSVivien Didelot 		 */
103732f794cSVivien Didelot 
104732f794cSVivien Didelot 		if ((dp->stp_state == BR_STATE_LEARNING ||
105732f794cSVivien Didelot 		     dp->stp_state == BR_STATE_FORWARDING) &&
106732f794cSVivien Didelot 		    (state == BR_STATE_DISABLED ||
107732f794cSVivien Didelot 		     state == BR_STATE_BLOCKING ||
108732f794cSVivien Didelot 		     state == BR_STATE_LISTENING))
109732f794cSVivien Didelot 			ds->ops->port_fast_age(ds, port);
110732f794cSVivien Didelot 	}
111732f794cSVivien Didelot 
112732f794cSVivien Didelot 	dp->stp_state = state;
1134acfee81SVivien Didelot }
1144acfee81SVivien Didelot 
11591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
11691da11f8SLennert Buytenhek {
117df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
118afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
119afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
120a5e9a02eSVivien Didelot 	u8 stp_state = dsa_port_is_bridged(p->dp) ?
121b73adef6SFlorian Fainelli 			BR_STATE_BLOCKING : BR_STATE_FORWARDING;
122df02c6ffSLennert Buytenhek 	int err;
123df02c6ffSLennert Buytenhek 
124df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
125df02c6ffSLennert Buytenhek 		return -ENETDOWN;
126df02c6ffSLennert Buytenhek 
1278feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
128a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
129df02c6ffSLennert Buytenhek 		if (err < 0)
130df02c6ffSLennert Buytenhek 			goto out;
131df02c6ffSLennert Buytenhek 	}
132df02c6ffSLennert Buytenhek 
133df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
134df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
135df02c6ffSLennert Buytenhek 		if (err < 0)
136df02c6ffSLennert Buytenhek 			goto del_unicast;
137df02c6ffSLennert Buytenhek 	}
138df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
139df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
140df02c6ffSLennert Buytenhek 		if (err < 0)
141df02c6ffSLennert Buytenhek 			goto clear_allmulti;
142df02c6ffSLennert Buytenhek 	}
143df02c6ffSLennert Buytenhek 
1449d490b4eSVivien Didelot 	if (ds->ops->port_enable) {
145afdcf151SVivien Didelot 		err = ds->ops->port_enable(ds, p->dp->index, p->phy);
146b2f2af21SFlorian Fainelli 		if (err)
147b2f2af21SFlorian Fainelli 			goto clear_promisc;
148b2f2af21SFlorian Fainelli 	}
149b2f2af21SFlorian Fainelli 
150c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, stp_state);
151b73adef6SFlorian Fainelli 
152f7f1de51SFlorian Fainelli 	if (p->phy)
153f7f1de51SFlorian Fainelli 		phy_start(p->phy);
154f7f1de51SFlorian Fainelli 
15591da11f8SLennert Buytenhek 	return 0;
156df02c6ffSLennert Buytenhek 
157b2f2af21SFlorian Fainelli clear_promisc:
158b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1594fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
160df02c6ffSLennert Buytenhek clear_allmulti:
161df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
162df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
163df02c6ffSLennert Buytenhek del_unicast:
1648feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
165a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
166df02c6ffSLennert Buytenhek out:
167df02c6ffSLennert Buytenhek 	return err;
16891da11f8SLennert Buytenhek }
16991da11f8SLennert Buytenhek 
17091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
17191da11f8SLennert Buytenhek {
172df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
173afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
174afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
175df02c6ffSLennert Buytenhek 
176f7f1de51SFlorian Fainelli 	if (p->phy)
177f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
178f7f1de51SFlorian Fainelli 
179df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
180a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
181df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
182df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
183df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
184df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
185df02c6ffSLennert Buytenhek 
1868feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
187a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
188df02c6ffSLennert Buytenhek 
1899d490b4eSVivien Didelot 	if (ds->ops->port_disable)
190afdcf151SVivien Didelot 		ds->ops->port_disable(ds, p->dp->index, p->phy);
191b2f2af21SFlorian Fainelli 
192c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, BR_STATE_DISABLED);
193b73adef6SFlorian Fainelli 
19491da11f8SLennert Buytenhek 	return 0;
19591da11f8SLennert Buytenhek }
19691da11f8SLennert Buytenhek 
19791da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
19891da11f8SLennert Buytenhek {
19991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
200afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
20191da11f8SLennert Buytenhek 
20291da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
20391da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
20491da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
20591da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
20691da11f8SLennert Buytenhek }
20791da11f8SLennert Buytenhek 
20891da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
20991da11f8SLennert Buytenhek {
21091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
211afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
21291da11f8SLennert Buytenhek 
21391da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
214a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
21591da11f8SLennert Buytenhek }
21691da11f8SLennert Buytenhek 
217df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
21891da11f8SLennert Buytenhek {
219df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
220afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
221df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
222df02c6ffSLennert Buytenhek 	int err;
223df02c6ffSLennert Buytenhek 
224df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
225df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
226df02c6ffSLennert Buytenhek 
227df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
228df02c6ffSLennert Buytenhek 		goto out;
229df02c6ffSLennert Buytenhek 
2308feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
231a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
232df02c6ffSLennert Buytenhek 		if (err < 0)
233df02c6ffSLennert Buytenhek 			return err;
234df02c6ffSLennert Buytenhek 	}
235df02c6ffSLennert Buytenhek 
2368feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
237a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
238df02c6ffSLennert Buytenhek 
239df02c6ffSLennert Buytenhek out:
240d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
24191da11f8SLennert Buytenhek 
24291da11f8SLennert Buytenhek 	return 0;
24391da11f8SLennert Buytenhek }
24491da11f8SLennert Buytenhek 
24511149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev,
2468f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan,
247f8db8348SJiri Pirko 				   struct switchdev_trans *trans)
24811149536SVivien Didelot {
24911149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
250afdcf151SVivien Didelot 	struct dsa_port *dp = p->dp;
251afdcf151SVivien Didelot 	struct dsa_switch *ds = dp->ds;
25211149536SVivien Didelot 
25379a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans)) {
2549d490b4eSVivien Didelot 		if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
25511149536SVivien Didelot 			return -EOPNOTSUPP;
25611149536SVivien Didelot 
257afdcf151SVivien Didelot 		return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
25811149536SVivien Didelot 	}
25911149536SVivien Didelot 
260afdcf151SVivien Didelot 	ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
2614d5770b3SVivien Didelot 
26211149536SVivien Didelot 	return 0;
26311149536SVivien Didelot }
26411149536SVivien Didelot 
26511149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev,
2668f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan)
26711149536SVivien Didelot {
26811149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
269afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
27011149536SVivien Didelot 
2719d490b4eSVivien Didelot 	if (!ds->ops->port_vlan_del)
27211149536SVivien Didelot 		return -EOPNOTSUPP;
27311149536SVivien Didelot 
274afdcf151SVivien Didelot 	return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
27511149536SVivien Didelot }
27611149536SVivien Didelot 
27711149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev,
2788f24f309SJiri Pirko 				    struct switchdev_obj_port_vlan *vlan,
279648b4a99SJiri Pirko 				    switchdev_obj_dump_cb_t *cb)
28011149536SVivien Didelot {
28111149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
282afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
28311149536SVivien Didelot 
2849d490b4eSVivien Didelot 	if (ds->ops->port_vlan_dump)
285afdcf151SVivien Didelot 		return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
28665aebfc0SVivien Didelot 
28711149536SVivien Didelot 	return -EOPNOTSUPP;
28811149536SVivien Didelot }
28911149536SVivien Didelot 
290ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev,
29152ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb,
292f8db8348SJiri Pirko 				  struct switchdev_trans *trans)
293cdf09697SDavid S. Miller {
294cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
295afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
296146a3206SVivien Didelot 
2978497aa61SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
2989d490b4eSVivien Didelot 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
299146a3206SVivien Didelot 			return -EOPNOTSUPP;
300cdf09697SDavid S. Miller 
301afdcf151SVivien Didelot 		return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
3028497aa61SVivien Didelot 	}
303cdf09697SDavid S. Miller 
304afdcf151SVivien Didelot 	ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
3058497aa61SVivien Didelot 
3068497aa61SVivien Didelot 	return 0;
307cdf09697SDavid S. Miller }
308cdf09697SDavid S. Miller 
309ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev,
31052ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb)
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 	int ret = -EOPNOTSUPP;
315cdf09697SDavid S. Miller 
3169d490b4eSVivien Didelot 	if (ds->ops->port_fdb_del)
317afdcf151SVivien Didelot 		ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
318cdf09697SDavid S. Miller 
319cdf09697SDavid S. Miller 	return ret;
320cdf09697SDavid S. Miller }
321cdf09697SDavid S. Miller 
322ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev,
32352ba57cfSJiri Pirko 				   struct switchdev_obj_port_fdb *fdb,
324648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
325cdf09697SDavid S. Miller {
326cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
327afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
328cdf09697SDavid S. Miller 
3299d490b4eSVivien Didelot 	if (ds->ops->port_fdb_dump)
330afdcf151SVivien Didelot 		return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
331ea70ba98SVivien Didelot 
332cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
333cdf09697SDavid S. Miller }
334cdf09697SDavid S. Miller 
3358df30255SVivien Didelot static int dsa_slave_port_mdb_add(struct net_device *dev,
3368df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb,
3378df30255SVivien Didelot 				  struct switchdev_trans *trans)
3388df30255SVivien Didelot {
3398df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
340afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3418df30255SVivien Didelot 
3428df30255SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3438df30255SVivien Didelot 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
3448df30255SVivien Didelot 			return -EOPNOTSUPP;
3458df30255SVivien Didelot 
346afdcf151SVivien Didelot 		return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
3478df30255SVivien Didelot 	}
3488df30255SVivien Didelot 
349afdcf151SVivien Didelot 	ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
3508df30255SVivien Didelot 
3518df30255SVivien Didelot 	return 0;
3528df30255SVivien Didelot }
3538df30255SVivien Didelot 
3548df30255SVivien Didelot static int dsa_slave_port_mdb_del(struct net_device *dev,
3558df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb)
3568df30255SVivien Didelot {
3578df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
358afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3598df30255SVivien Didelot 
3608df30255SVivien Didelot 	if (ds->ops->port_mdb_del)
361afdcf151SVivien Didelot 		return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
3628df30255SVivien Didelot 
3638df30255SVivien Didelot 	return -EOPNOTSUPP;
3648df30255SVivien Didelot }
3658df30255SVivien Didelot 
3668df30255SVivien Didelot static int dsa_slave_port_mdb_dump(struct net_device *dev,
3678df30255SVivien Didelot 				   struct switchdev_obj_port_mdb *mdb,
3688df30255SVivien Didelot 				   switchdev_obj_dump_cb_t *cb)
3698df30255SVivien Didelot {
3708df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
371afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3728df30255SVivien Didelot 
3738df30255SVivien Didelot 	if (ds->ops->port_mdb_dump)
374afdcf151SVivien Didelot 		return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
3758df30255SVivien Didelot 
3768df30255SVivien Didelot 	return -EOPNOTSUPP;
3778df30255SVivien Didelot }
3788df30255SVivien Didelot 
37991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
38091da11f8SLennert Buytenhek {
38191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
38291da11f8SLennert Buytenhek 
38391da11f8SLennert Buytenhek 	if (p->phy != NULL)
38428b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
38591da11f8SLennert Buytenhek 
38691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
38791da11f8SLennert Buytenhek }
38891da11f8SLennert Buytenhek 
38943c44a9fSVivien Didelot static int dsa_slave_stp_state_set(struct net_device *dev,
39043c44a9fSVivien Didelot 				   const struct switchdev_attr *attr,
39143c44a9fSVivien Didelot 				   struct switchdev_trans *trans)
392b73adef6SFlorian Fainelli {
393b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
394afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
395b73adef6SFlorian Fainelli 
39643c44a9fSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
3979d490b4eSVivien Didelot 		return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
398b73adef6SFlorian Fainelli 
399c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, attr->u.stp_state);
40043c44a9fSVivien Didelot 
40143c44a9fSVivien Didelot 	return 0;
402b73adef6SFlorian Fainelli }
403b73adef6SFlorian Fainelli 
404fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
405fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
406fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
407fb2dabadSVivien Didelot {
408fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
409afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
410fb2dabadSVivien Didelot 
411fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
412fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
413fb2dabadSVivien Didelot 		return 0;
414fb2dabadSVivien Didelot 
4159d490b4eSVivien Didelot 	if (ds->ops->port_vlan_filtering)
416afdcf151SVivien Didelot 		return ds->ops->port_vlan_filtering(ds, p->dp->index,
417fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
418fb2dabadSVivien Didelot 
419fb2dabadSVivien Didelot 	return 0;
420fb2dabadSVivien Didelot }
421fb2dabadSVivien Didelot 
42234a79f63SVivien Didelot static int dsa_fastest_ageing_time(struct dsa_switch *ds,
42334a79f63SVivien Didelot 				   unsigned int ageing_time)
42434a79f63SVivien Didelot {
42534a79f63SVivien Didelot 	int i;
42634a79f63SVivien Didelot 
42726895e29SVivien Didelot 	for (i = 0; i < ds->num_ports; ++i) {
42834a79f63SVivien Didelot 		struct dsa_port *dp = &ds->ports[i];
42934a79f63SVivien Didelot 
43034a79f63SVivien Didelot 		if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
43134a79f63SVivien Didelot 			ageing_time = dp->ageing_time;
43234a79f63SVivien Didelot 	}
43334a79f63SVivien Didelot 
43434a79f63SVivien Didelot 	return ageing_time;
43534a79f63SVivien Didelot }
43634a79f63SVivien Didelot 
43734a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev,
43834a79f63SVivien Didelot 				 const struct switchdev_attr *attr,
43934a79f63SVivien Didelot 				 struct switchdev_trans *trans)
44034a79f63SVivien Didelot {
44134a79f63SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
442afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
44334a79f63SVivien Didelot 	unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
44434a79f63SVivien Didelot 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
44534a79f63SVivien Didelot 
44634a79f63SVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
44734a79f63SVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
44834a79f63SVivien Didelot 		return 0;
44934a79f63SVivien Didelot 
45034a79f63SVivien Didelot 	/* Keep the fastest ageing time in case of multiple bridges */
451afdcf151SVivien Didelot 	p->dp->ageing_time = ageing_time;
45234a79f63SVivien Didelot 	ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
45334a79f63SVivien Didelot 
4549d490b4eSVivien Didelot 	if (ds->ops->set_ageing_time)
4559d490b4eSVivien Didelot 		return ds->ops->set_ageing_time(ds, ageing_time);
45634a79f63SVivien Didelot 
45734a79f63SVivien Didelot 	return 0;
45834a79f63SVivien Didelot }
45934a79f63SVivien Didelot 
46035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
461f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
4627ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
46335636062SScott Feldman {
464b8d866acSVivien Didelot 	int ret;
46535636062SScott Feldman 
46635636062SScott Feldman 	switch (attr->id) {
4671f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
46843c44a9fSVivien Didelot 		ret = dsa_slave_stp_state_set(dev, attr, trans);
46935636062SScott Feldman 		break;
470fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
471fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
472fb2dabadSVivien Didelot 		break;
47334a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
47434a79f63SVivien Didelot 		ret = dsa_slave_ageing_time(dev, attr, trans);
47534a79f63SVivien Didelot 		break;
47635636062SScott Feldman 	default:
47735636062SScott Feldman 		ret = -EOPNOTSUPP;
47835636062SScott Feldman 		break;
47935636062SScott Feldman 	}
48035636062SScott Feldman 
48135636062SScott Feldman 	return ret;
48235636062SScott Feldman }
48335636062SScott Feldman 
484ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
485648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
4867ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
487ba14d9ebSVivien Didelot {
488ba14d9ebSVivien Didelot 	int err;
489ba14d9ebSVivien Didelot 
490ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
491ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
492ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
493ba14d9ebSVivien Didelot 	 */
494ba14d9ebSVivien Didelot 
4959e8f4a54SJiri Pirko 	switch (obj->id) {
49657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
497648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_add(dev,
498648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj),
499648b4a99SJiri Pirko 					     trans);
500ba14d9ebSVivien Didelot 		break;
5018df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5028df30255SVivien Didelot 		err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5038df30255SVivien Didelot 					     trans);
5048df30255SVivien Didelot 		break;
50557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
506648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
507648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
508648b4a99SJiri Pirko 					      trans);
50911149536SVivien Didelot 		break;
510ba14d9ebSVivien Didelot 	default:
511ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
512ba14d9ebSVivien Didelot 		break;
513ba14d9ebSVivien Didelot 	}
514ba14d9ebSVivien Didelot 
515ba14d9ebSVivien Didelot 	return err;
516ba14d9ebSVivien Didelot }
517ba14d9ebSVivien Didelot 
518ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
519648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
520ba14d9ebSVivien Didelot {
521ba14d9ebSVivien Didelot 	int err;
522ba14d9ebSVivien Didelot 
5239e8f4a54SJiri Pirko 	switch (obj->id) {
52457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
525648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_del(dev,
526648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj));
527ba14d9ebSVivien Didelot 		break;
5288df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5298df30255SVivien Didelot 		err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
5308df30255SVivien Didelot 		break;
53157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
532648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
533648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
53411149536SVivien Didelot 		break;
535ba14d9ebSVivien Didelot 	default:
536ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
537ba14d9ebSVivien Didelot 		break;
538ba14d9ebSVivien Didelot 	}
539ba14d9ebSVivien Didelot 
540ba14d9ebSVivien Didelot 	return err;
541ba14d9ebSVivien Didelot }
542ba14d9ebSVivien Didelot 
543ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
544648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
545648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
546ba14d9ebSVivien Didelot {
547ba14d9ebSVivien Didelot 	int err;
548ba14d9ebSVivien Didelot 
5499e8f4a54SJiri Pirko 	switch (obj->id) {
55057d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
551648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_dump(dev,
552648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_FDB(obj),
553648b4a99SJiri Pirko 					      cb);
554ba14d9ebSVivien Didelot 		break;
5558df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5568df30255SVivien Didelot 		err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5578df30255SVivien Didelot 					      cb);
5588df30255SVivien Didelot 		break;
55957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
560648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
561648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
562648b4a99SJiri Pirko 					       cb);
56311149536SVivien Didelot 		break;
564ba14d9ebSVivien Didelot 	default:
565ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
566ba14d9ebSVivien Didelot 		break;
567ba14d9ebSVivien Didelot 	}
568ba14d9ebSVivien Didelot 
569ba14d9ebSVivien Didelot 	return err;
570ba14d9ebSVivien Didelot }
571ba14d9ebSVivien Didelot 
572b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
573b73adef6SFlorian Fainelli 				      struct net_device *br)
574b73adef6SFlorian Fainelli {
575b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
57604d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
57704d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
57804d3a4c6SVivien Didelot 		.port = p->dp->index,
57904d3a4c6SVivien Didelot 		.br = br,
58004d3a4c6SVivien Didelot 	};
58104d3a4c6SVivien Didelot 	int err;
582b73adef6SFlorian Fainelli 
5839c265426SVivien Didelot 	/* Here the port is already bridged. Reflect the current configuration
5849c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
5859c265426SVivien Didelot 	 */
586a5e9a02eSVivien Didelot 	p->dp->bridge_dev = br;
587b73adef6SFlorian Fainelli 
58804d3a4c6SVivien Didelot 	err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_JOIN, &info);
589b73adef6SFlorian Fainelli 
5909c265426SVivien Didelot 	/* The bridging is rolled back on error */
59104d3a4c6SVivien Didelot 	if (err)
5929c265426SVivien Didelot 		p->dp->bridge_dev = NULL;
5939c265426SVivien Didelot 
59404d3a4c6SVivien Didelot 	return err;
595b73adef6SFlorian Fainelli }
596b73adef6SFlorian Fainelli 
597f123f2fbSVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev,
598f123f2fbSVivien Didelot 					struct net_device *br)
599b73adef6SFlorian Fainelli {
600b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
60104d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
60204d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
60304d3a4c6SVivien Didelot 		.port = p->dp->index,
60404d3a4c6SVivien Didelot 		.br = br,
60504d3a4c6SVivien Didelot 	};
60604d3a4c6SVivien Didelot 	int err;
607b73adef6SFlorian Fainelli 
6089c265426SVivien Didelot 	/* Here the port is already unbridged. Reflect the current configuration
6099c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
6109c265426SVivien Didelot 	 */
611f123f2fbSVivien Didelot 	p->dp->bridge_dev = NULL;
612b73adef6SFlorian Fainelli 
61304d3a4c6SVivien Didelot 	err = dsa_slave_notify(dev, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
61404d3a4c6SVivien Didelot 	if (err)
61504d3a4c6SVivien Didelot 		netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
616b73adef6SFlorian Fainelli 
617b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
618b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
619b73adef6SFlorian Fainelli 	 */
620c5d35cb3SVivien Didelot 	dsa_slave_set_state(dev, BR_STATE_FORWARDING);
621b73adef6SFlorian Fainelli }
622b73adef6SFlorian Fainelli 
623f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
624f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
625b73adef6SFlorian Fainelli {
626b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
627afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
628b73adef6SFlorian Fainelli 
629f8e20a9fSScott Feldman 	switch (attr->id) {
6301f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
63142275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
63242275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
633f8e20a9fSScott Feldman 		break;
634f8e20a9fSScott Feldman 	default:
635f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
636f8e20a9fSScott Feldman 	}
637b73adef6SFlorian Fainelli 
638b73adef6SFlorian Fainelli 	return 0;
639b73adef6SFlorian Fainelli }
640b73adef6SFlorian Fainelli 
64104ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
64204ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
64304ff53f9SFlorian Fainelli {
64404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
64504ff53f9SFlorian Fainelli 	if (p->netpoll)
64604ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
64704ff53f9SFlorian Fainelli #else
64804ff53f9SFlorian Fainelli 	BUG();
64904ff53f9SFlorian Fainelli #endif
65004ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
65104ff53f9SFlorian Fainelli }
65204ff53f9SFlorian Fainelli 
6533e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6543e8a72d1SFlorian Fainelli {
6553e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6564ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6573e8a72d1SFlorian Fainelli 
6584ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
6594ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
6603e8a72d1SFlorian Fainelli 
6614ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
6624ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
6634ed70ce9SFlorian Fainelli 	if (!nskb)
6644ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
6655aed85ceSFlorian Fainelli 
66604ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
66704ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
66804ff53f9SFlorian Fainelli 	 */
66904ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
67004ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
67104ff53f9SFlorian Fainelli 
6724ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
6734ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
6744ed70ce9SFlorian Fainelli 	 */
675afdcf151SVivien Didelot 	nskb->dev = p->dp->ds->dst->master_netdev;
6764ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
6775aed85ceSFlorian Fainelli 
6785aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
6795aed85ceSFlorian Fainelli }
6805aed85ceSFlorian Fainelli 
68191da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
68291da11f8SLennert Buytenhek static int
683bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
684bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
68591da11f8SLennert Buytenhek {
68691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
687*e69e4626SFlorian Fainelli 	int err = -EOPNOTSUPP;
68891da11f8SLennert Buytenhek 
689*e69e4626SFlorian Fainelli 	if (p->phy != NULL)
690bb10bb3eSPhilippe Reynes 		err = phy_ethtool_ksettings_get(p->phy, cmd);
69191da11f8SLennert Buytenhek 
69291da11f8SLennert Buytenhek 	return err;
69391da11f8SLennert Buytenhek }
69491da11f8SLennert Buytenhek 
69591da11f8SLennert Buytenhek static int
696bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
697bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
69891da11f8SLennert Buytenhek {
69991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
70091da11f8SLennert Buytenhek 
70191da11f8SLennert Buytenhek 	if (p->phy != NULL)
702bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
70391da11f8SLennert Buytenhek 
70491da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
70591da11f8SLennert Buytenhek }
70691da11f8SLennert Buytenhek 
70791da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
70891da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
70991da11f8SLennert Buytenhek {
7107826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
7117826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
7127826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
71391da11f8SLennert Buytenhek }
71491da11f8SLennert Buytenhek 
7153d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
7163d762a0fSGuenter Roeck {
7173d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
718afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7193d762a0fSGuenter Roeck 
7209d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
721afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
7223d762a0fSGuenter Roeck 
7233d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
7243d762a0fSGuenter Roeck }
7253d762a0fSGuenter Roeck 
7263d762a0fSGuenter Roeck static void
7273d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
7283d762a0fSGuenter Roeck {
7293d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
730afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7313d762a0fSGuenter Roeck 
7329d490b4eSVivien Didelot 	if (ds->ops->get_regs)
733afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
7343d762a0fSGuenter Roeck }
7353d762a0fSGuenter Roeck 
73691da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
73791da11f8SLennert Buytenhek {
73891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
73991da11f8SLennert Buytenhek 
74091da11f8SLennert Buytenhek 	if (p->phy != NULL)
74191da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
74291da11f8SLennert Buytenhek 
74391da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
74491da11f8SLennert Buytenhek }
74591da11f8SLennert Buytenhek 
74691da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
74791da11f8SLennert Buytenhek {
74891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
74991da11f8SLennert Buytenhek 
75091da11f8SLennert Buytenhek 	if (p->phy != NULL) {
75191da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
75291da11f8SLennert Buytenhek 		return p->phy->link;
75391da11f8SLennert Buytenhek 	}
75491da11f8SLennert Buytenhek 
75591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
75691da11f8SLennert Buytenhek }
75791da11f8SLennert Buytenhek 
7586793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
7596793abb4SGuenter Roeck {
7606793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
761afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7626793abb4SGuenter Roeck 
7630e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
764ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
7656793abb4SGuenter Roeck 
7669d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7679d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7686793abb4SGuenter Roeck 
7696793abb4SGuenter Roeck 	return 0;
7706793abb4SGuenter Roeck }
7716793abb4SGuenter Roeck 
7726793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7736793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7746793abb4SGuenter Roeck {
7756793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
776afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7776793abb4SGuenter Roeck 
7789d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7799d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7806793abb4SGuenter Roeck 
7816793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7826793abb4SGuenter Roeck }
7836793abb4SGuenter Roeck 
7846793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7856793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7866793abb4SGuenter Roeck {
7876793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
788afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7896793abb4SGuenter Roeck 
7909d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7919d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7926793abb4SGuenter Roeck 
7936793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7946793abb4SGuenter Roeck }
7956793abb4SGuenter Roeck 
79691da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
79791da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
79891da11f8SLennert Buytenhek {
79991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
800afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
80191da11f8SLennert Buytenhek 
80291da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
80391da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
80491da11f8SLennert Buytenhek 
80591da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
80691da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
80791da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
80891da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
8099d490b4eSVivien Didelot 		if (ds->ops->get_strings)
810afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
81191da11f8SLennert Buytenhek 	}
81291da11f8SLennert Buytenhek }
81391da11f8SLennert Buytenhek 
814badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
815badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
816badf3adaSFlorian Fainelli 					   uint64_t *data)
817badf3adaSFlorian Fainelli {
818badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8199520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
820badf3adaSFlorian Fainelli 	s8 cpu_port = dst->cpu_port;
821badf3adaSFlorian Fainelli 	int count = 0;
822badf3adaSFlorian Fainelli 
823badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
824badf3adaSFlorian Fainelli 		count = dst->master_ethtool_ops.get_sset_count(dev,
825badf3adaSFlorian Fainelli 							       ETH_SS_STATS);
826badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
827badf3adaSFlorian Fainelli 	}
828badf3adaSFlorian Fainelli 
8299d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
8309d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
831badf3adaSFlorian Fainelli }
832badf3adaSFlorian Fainelli 
833badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
834badf3adaSFlorian Fainelli {
835badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8369520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
837badf3adaSFlorian Fainelli 	int count = 0;
838badf3adaSFlorian Fainelli 
839badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count)
840badf3adaSFlorian Fainelli 		count += dst->master_ethtool_ops.get_sset_count(dev, sset);
841badf3adaSFlorian Fainelli 
8429d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
8439d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
844badf3adaSFlorian Fainelli 
845badf3adaSFlorian Fainelli 	return count;
846badf3adaSFlorian Fainelli }
847badf3adaSFlorian Fainelli 
848badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
849badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
850badf3adaSFlorian Fainelli {
851badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8529520ed8fSVivien Didelot 	struct dsa_switch *ds = dst->cpu_switch;
853badf3adaSFlorian Fainelli 	s8 cpu_port = dst->cpu_port;
854badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
855badf3adaSFlorian Fainelli 	int mcount = 0, count;
856badf3adaSFlorian Fainelli 	unsigned int i;
857badf3adaSFlorian Fainelli 	uint8_t pfx[4];
858badf3adaSFlorian Fainelli 	uint8_t *ndata;
859badf3adaSFlorian Fainelli 
860badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
861badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
862badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
863badf3adaSFlorian Fainelli 
864badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
865badf3adaSFlorian Fainelli 		mcount = dst->master_ethtool_ops.get_sset_count(dev,
866badf3adaSFlorian Fainelli 								ETH_SS_STATS);
867badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_strings(dev, stringset, data);
868badf3adaSFlorian Fainelli 	}
869badf3adaSFlorian Fainelli 
8709d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
871badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
872badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
873badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
874badf3adaSFlorian Fainelli 		 * constructed earlier
875badf3adaSFlorian Fainelli 		 */
8769d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
8779d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
878badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
879badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
880badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
881badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
882badf3adaSFlorian Fainelli 		}
883badf3adaSFlorian Fainelli 	}
884badf3adaSFlorian Fainelli }
885badf3adaSFlorian Fainelli 
88691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
88791da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
88891da11f8SLennert Buytenhek 					uint64_t *data)
88991da11f8SLennert Buytenhek {
89091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
891afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
89291da11f8SLennert Buytenhek 
89346e7b8d8SVivien Didelot 	data[0] = dev->stats.tx_packets;
89446e7b8d8SVivien Didelot 	data[1] = dev->stats.tx_bytes;
89546e7b8d8SVivien Didelot 	data[2] = dev->stats.rx_packets;
89646e7b8d8SVivien Didelot 	data[3] = dev->stats.rx_bytes;
8979d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
898afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
89991da11f8SLennert Buytenhek }
90091da11f8SLennert Buytenhek 
90191da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
90291da11f8SLennert Buytenhek {
90391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
904afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
90591da11f8SLennert Buytenhek 
90691da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
90791da11f8SLennert Buytenhek 		int count;
90891da11f8SLennert Buytenhek 
90991da11f8SLennert Buytenhek 		count = 4;
9109d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
9119d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
91291da11f8SLennert Buytenhek 
91391da11f8SLennert Buytenhek 		return count;
91491da11f8SLennert Buytenhek 	}
91591da11f8SLennert Buytenhek 
91691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
91791da11f8SLennert Buytenhek }
91891da11f8SLennert Buytenhek 
91919e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
92019e57c4eSFlorian Fainelli {
92119e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
922afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
92319e57c4eSFlorian Fainelli 
9249d490b4eSVivien Didelot 	if (ds->ops->get_wol)
925afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
92619e57c4eSFlorian Fainelli }
92719e57c4eSFlorian Fainelli 
92819e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
92919e57c4eSFlorian Fainelli {
93019e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
931afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
93219e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
93319e57c4eSFlorian Fainelli 
9349d490b4eSVivien Didelot 	if (ds->ops->set_wol)
935afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
93619e57c4eSFlorian Fainelli 
93719e57c4eSFlorian Fainelli 	return ret;
93819e57c4eSFlorian Fainelli }
93919e57c4eSFlorian Fainelli 
9407905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
9417905288fSFlorian Fainelli {
9427905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
943afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9447905288fSFlorian Fainelli 	int ret;
9457905288fSFlorian Fainelli 
9469d490b4eSVivien Didelot 	if (!ds->ops->set_eee)
9477905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9487905288fSFlorian Fainelli 
949afdcf151SVivien Didelot 	ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
9507905288fSFlorian Fainelli 	if (ret)
9517905288fSFlorian Fainelli 		return ret;
9527905288fSFlorian Fainelli 
9537905288fSFlorian Fainelli 	if (p->phy)
9547905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
9557905288fSFlorian Fainelli 
9567905288fSFlorian Fainelli 	return ret;
9577905288fSFlorian Fainelli }
9587905288fSFlorian Fainelli 
9597905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
9607905288fSFlorian Fainelli {
9617905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
962afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9637905288fSFlorian Fainelli 	int ret;
9647905288fSFlorian Fainelli 
9659d490b4eSVivien Didelot 	if (!ds->ops->get_eee)
9667905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9677905288fSFlorian Fainelli 
968afdcf151SVivien Didelot 	ret = ds->ops->get_eee(ds, p->dp->index, e);
9697905288fSFlorian Fainelli 	if (ret)
9707905288fSFlorian Fainelli 		return ret;
9717905288fSFlorian Fainelli 
9727905288fSFlorian Fainelli 	if (p->phy)
9737905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
9747905288fSFlorian Fainelli 
9757905288fSFlorian Fainelli 	return ret;
9767905288fSFlorian Fainelli }
9777905288fSFlorian Fainelli 
97804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
97904ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
98004ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
98104ff53f9SFlorian Fainelli {
98204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
983afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
98404ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
98504ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
98604ff53f9SFlorian Fainelli 	int err = 0;
98704ff53f9SFlorian Fainelli 
98804ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
98904ff53f9SFlorian Fainelli 	if (!netpoll)
99004ff53f9SFlorian Fainelli 		return -ENOMEM;
99104ff53f9SFlorian Fainelli 
99204ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
99304ff53f9SFlorian Fainelli 	if (err) {
99404ff53f9SFlorian Fainelli 		kfree(netpoll);
99504ff53f9SFlorian Fainelli 		goto out;
99604ff53f9SFlorian Fainelli 	}
99704ff53f9SFlorian Fainelli 
99804ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
99904ff53f9SFlorian Fainelli out:
100004ff53f9SFlorian Fainelli 	return err;
100104ff53f9SFlorian Fainelli }
100204ff53f9SFlorian Fainelli 
100304ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
100404ff53f9SFlorian Fainelli {
100504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
100604ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
100704ff53f9SFlorian Fainelli 
100804ff53f9SFlorian Fainelli 	if (!netpoll)
100904ff53f9SFlorian Fainelli 		return;
101004ff53f9SFlorian Fainelli 
101104ff53f9SFlorian Fainelli 	p->netpoll = NULL;
101204ff53f9SFlorian Fainelli 
101304ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
101404ff53f9SFlorian Fainelli }
101504ff53f9SFlorian Fainelli 
101604ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
101704ff53f9SFlorian Fainelli {
101804ff53f9SFlorian Fainelli }
101904ff53f9SFlorian Fainelli #endif
102004ff53f9SFlorian Fainelli 
102144bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
102244bb765cSFlorian Fainelli 					char *name, size_t len)
102344bb765cSFlorian Fainelli {
102444bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
102544bb765cSFlorian Fainelli 
1026afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
102744bb765cSFlorian Fainelli 		return -EINVAL;
10283a543ef4SFlorian Fainelli 
10293a543ef4SFlorian Fainelli 	return 0;
10303a543ef4SFlorian Fainelli }
10313a543ef4SFlorian Fainelli 
1032f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
1033f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1034f50f2127SFlorian Fainelli 			     unsigned long cookie)
1035f50f2127SFlorian Fainelli {
1036f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1037f50f2127SFlorian Fainelli 
1038f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1039f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1040f50f2127SFlorian Fainelli 			return mall_tc_entry;
1041f50f2127SFlorian Fainelli 
1042f50f2127SFlorian Fainelli 	return NULL;
1043f50f2127SFlorian Fainelli }
1044f50f2127SFlorian Fainelli 
1045f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
1046f50f2127SFlorian Fainelli 				      __be16 protocol,
1047f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
1048f50f2127SFlorian Fainelli 				      bool ingress)
1049f50f2127SFlorian Fainelli {
1050f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1051f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1052f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1053f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
1054f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
1055f50f2127SFlorian Fainelli 	struct net_device *to_dev;
1056f50f2127SFlorian Fainelli 	const struct tc_action *a;
1057f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
1058f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
1059f50f2127SFlorian Fainelli 	int ifindex;
1060f50f2127SFlorian Fainelli 
1061f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
1062f50f2127SFlorian Fainelli 		return err;
1063f50f2127SFlorian Fainelli 
1064f50f2127SFlorian Fainelli 	if (!tc_single_action(cls->exts))
1065f50f2127SFlorian Fainelli 		return err;
1066f50f2127SFlorian Fainelli 
1067f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
1068f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
1069f50f2127SFlorian Fainelli 
1070f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1071f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
1072f50f2127SFlorian Fainelli 
1073f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
1074f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
1075f50f2127SFlorian Fainelli 		if (!to_dev)
1076f50f2127SFlorian Fainelli 			return -EINVAL;
1077f50f2127SFlorian Fainelli 
1078f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
1079f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
1080f50f2127SFlorian Fainelli 
1081f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1082f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
1083f50f2127SFlorian Fainelli 			return -ENOMEM;
1084f50f2127SFlorian Fainelli 
1085f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
1086f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1087f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
1088f50f2127SFlorian Fainelli 
1089f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
1090f50f2127SFlorian Fainelli 
1091f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
1092f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
1093f50f2127SFlorian Fainelli 
1094f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1095f50f2127SFlorian Fainelli 					       ingress);
1096f50f2127SFlorian Fainelli 		if (err) {
1097f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
1098f50f2127SFlorian Fainelli 			return err;
1099f50f2127SFlorian Fainelli 		}
1100f50f2127SFlorian Fainelli 
1101f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1102f50f2127SFlorian Fainelli 	}
1103f50f2127SFlorian Fainelli 
1104f50f2127SFlorian Fainelli 	return 0;
1105f50f2127SFlorian Fainelli }
1106f50f2127SFlorian Fainelli 
1107f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1109f50f2127SFlorian Fainelli {
1110f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1111f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1112f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1113f50f2127SFlorian Fainelli 
1114f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
1115f50f2127SFlorian Fainelli 		return;
1116f50f2127SFlorian Fainelli 
1117f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1118f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1119f50f2127SFlorian Fainelli 		return;
1120f50f2127SFlorian Fainelli 
1121f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1122f50f2127SFlorian Fainelli 
1123f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1124f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
1125f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
1126f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
1127f50f2127SFlorian Fainelli 		break;
1128f50f2127SFlorian Fainelli 	default:
1129f50f2127SFlorian Fainelli 		WARN_ON(1);
1130f50f2127SFlorian Fainelli 	}
1131f50f2127SFlorian Fainelli 
1132f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1133f50f2127SFlorian Fainelli }
1134f50f2127SFlorian Fainelli 
1135f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1136f50f2127SFlorian Fainelli 			      __be16 protocol, struct tc_to_netdev *tc)
1137f50f2127SFlorian Fainelli {
1138f50f2127SFlorian Fainelli 	bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1139f50f2127SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1140f50f2127SFlorian Fainelli 
1141f50f2127SFlorian Fainelli 	switch (tc->type) {
1142f50f2127SFlorian Fainelli 	case TC_SETUP_MATCHALL:
1143f50f2127SFlorian Fainelli 		switch (tc->cls_mall->command) {
1144f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_REPLACE:
1145f50f2127SFlorian Fainelli 			return dsa_slave_add_cls_matchall(dev, protocol,
1146f50f2127SFlorian Fainelli 							  tc->cls_mall,
1147f50f2127SFlorian Fainelli 							  ingress);
1148f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_DESTROY:
1149f50f2127SFlorian Fainelli 			dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1150f50f2127SFlorian Fainelli 			return 0;
1151f50f2127SFlorian Fainelli 		}
1152f50f2127SFlorian Fainelli 	default:
1153f50f2127SFlorian Fainelli 		break;
1154f50f2127SFlorian Fainelli 	}
1155f50f2127SFlorian Fainelli 
1156f50f2127SFlorian Fainelli 	return ret;
1157f50f2127SFlorian Fainelli }
1158f50f2127SFlorian Fainelli 
1159af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1160af42192cSFlorian Fainelli {
1161af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
1162af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1163af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
1164af42192cSFlorian Fainelli }
1165af42192cSFlorian Fainelli 
1166bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1167bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1168bf9f2648SFlorian Fainelli {
1169bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1170bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1171bf9f2648SFlorian Fainelli 
1172bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1173bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1174bf9f2648SFlorian Fainelli 
1175bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1176bf9f2648SFlorian Fainelli }
1177bf9f2648SFlorian Fainelli 
1178bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1179bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1180bf9f2648SFlorian Fainelli {
1181bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1182bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1183bf9f2648SFlorian Fainelli 
1184bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1185bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1186bf9f2648SFlorian Fainelli 
1187bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1188bf9f2648SFlorian Fainelli }
1189bf9f2648SFlorian Fainelli 
119091da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
119191da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
11923d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
11933d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
119491da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
119591da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
11966793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
11976793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
11986793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
119991da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
120091da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
120191da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
120219e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
120319e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
12047905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
12057905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1206bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1207bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1208bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1209bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
121091da11f8SLennert Buytenhek };
121191da11f8SLennert Buytenhek 
12123e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1213d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1214d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
12153e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1216d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1217d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1218d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1219ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
1220ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
1221ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
1222d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1223abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
122404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
122504ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
122604ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
122704ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
122804ff53f9SFlorian Fainelli #endif
122911149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
123011149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
123111149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
123244bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1233f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
123498237d43SScott Feldman };
123598237d43SScott Feldman 
12369d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1237f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
123835636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1239ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1240ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1241ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
1242d442ad4aSStephen Hemminger };
124391da11f8SLennert Buytenhek 
1244f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1245f37db85dSFlorian Fainelli 	.name	= "dsa",
1246f37db85dSFlorian Fainelli };
1247f37db85dSFlorian Fainelli 
12480d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
12490d8bcdd3SFlorian Fainelli {
12500d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1251afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12520d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
12530d8bcdd3SFlorian Fainelli 
12540d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
12550d8bcdd3SFlorian Fainelli 		status_changed = 1;
12560d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
12570d8bcdd3SFlorian Fainelli 	}
12580d8bcdd3SFlorian Fainelli 
12590d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
12600d8bcdd3SFlorian Fainelli 		status_changed = 1;
12610d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
12620d8bcdd3SFlorian Fainelli 	}
12630d8bcdd3SFlorian Fainelli 
12640d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
12650d8bcdd3SFlorian Fainelli 		status_changed = 1;
12660d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
12670d8bcdd3SFlorian Fainelli 	}
12680d8bcdd3SFlorian Fainelli 
12699d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1270afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1271ec9436baSFlorian Fainelli 
12720d8bcdd3SFlorian Fainelli 	if (status_changed)
12730d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
12740d8bcdd3SFlorian Fainelli }
12750d8bcdd3SFlorian Fainelli 
1276ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1277ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1278ce31b31cSFlorian Fainelli {
1279b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1280b71be352SAndrew Lunn 	struct dsa_switch *ds;
1281ce31b31cSFlorian Fainelli 
1282b71be352SAndrew Lunn 	if (dev) {
1283b71be352SAndrew Lunn 		p = netdev_priv(dev);
1284afdcf151SVivien Didelot 		ds = p->dp->ds;
12859d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1286afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1287b71be352SAndrew Lunn 	}
1288ce31b31cSFlorian Fainelli 
1289ce31b31cSFlorian Fainelli 	return 0;
1290ce31b31cSFlorian Fainelli }
1291ce31b31cSFlorian Fainelli 
129291da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1293c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1294cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1295cd28a1a9SFlorian Fainelli 				 int addr)
1296c305c165SFlorian Fainelli {
1297afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1298c305c165SFlorian Fainelli 
12997f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1300d25b8e74SRussell King 	if (!p->phy) {
1301d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1302c305c165SFlorian Fainelli 		return -ENODEV;
1303d25b8e74SRussell King 	}
1304c305c165SFlorian Fainelli 
1305c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1306211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1307c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
13084078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1309c305c165SFlorian Fainelli 				  p->phy_interface);
1310c305c165SFlorian Fainelli }
1311c305c165SFlorian Fainelli 
13129697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
13130d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
13140d8bcdd3SFlorian Fainelli {
1315afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
13160d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1317ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
13186819563eSFlorian Fainelli 	u32 phy_flags = 0;
131919334920SGuenter Roeck 	int mode, ret;
13200d8bcdd3SFlorian Fainelli 
1321afdcf151SVivien Didelot 	port_dn = p->dp->dn;
132219334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
132319334920SGuenter Roeck 	if (mode < 0)
132419334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
132519334920SGuenter Roeck 	p->phy_interface = mode;
13260d8bcdd3SFlorian Fainelli 
13270d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
13280d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
13290d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
13300d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
13310d8bcdd3SFlorian Fainelli 		 */
13320d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
13330d8bcdd3SFlorian Fainelli 		if (ret) {
1334d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
13359697f1cdSFlorian Fainelli 			return ret;
13360d8bcdd3SFlorian Fainelli 		}
1337ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
13380d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
13390d8bcdd3SFlorian Fainelli 	}
13400d8bcdd3SFlorian Fainelli 
13419d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1342afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
13436819563eSFlorian Fainelli 
1344cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1345d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1346d25b8e74SRussell King 
1347cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1348cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1349cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1350cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1351cd28a1a9SFlorian Fainelli 		 */
1352d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1353d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1354d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1355d25b8e74SRussell King 			if (ret) {
1356d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
13570d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1358cd28a1a9SFlorian Fainelli 				return ret;
1359d25b8e74SRussell King 			}
1360cd28a1a9SFlorian Fainelli 		} else {
13610d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1362cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1363cd28a1a9SFlorian Fainelli 						phy_flags,
13640d8bcdd3SFlorian Fainelli 						p->phy_interface);
1365cd28a1a9SFlorian Fainelli 		}
13660d8f3c67SJohan Hovold 
13670d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1368cd28a1a9SFlorian Fainelli 	}
13690d8bcdd3SFlorian Fainelli 
1370ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1371ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1372ce31b31cSFlorian Fainelli 
13730d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
13740d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
13750d8bcdd3SFlorian Fainelli 	 */
1376b31f65fbSAndrew Lunn 	if (!p->phy) {
1377afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1378d25b8e74SRussell King 		if (ret) {
1379afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1380afdcf151SVivien Didelot 				   p->dp->index, ret);
1381881eadabSJohan Hovold 			if (phy_is_fixed)
1382881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1383c305c165SFlorian Fainelli 			return ret;
1384d25b8e74SRussell King 		}
13850d8bcdd3SFlorian Fainelli 	}
13869697f1cdSFlorian Fainelli 
13872220943aSAndrew Lunn 	phy_attached_info(p->phy);
13882220943aSAndrew Lunn 
13899697f1cdSFlorian Fainelli 	return 0;
1390b31f65fbSAndrew Lunn }
13910d8bcdd3SFlorian Fainelli 
1392448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1393448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1394448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1395448b4482SAndrew Lunn 					    void *_unused)
1396448b4482SAndrew Lunn {
1397448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1398448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1399448b4482SAndrew Lunn }
1400448b4482SAndrew Lunn 
140124462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
140224462549SFlorian Fainelli {
140324462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
140424462549SFlorian Fainelli 
1405f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1406f154be24SFlorian Fainelli 
140724462549SFlorian Fainelli 	if (p->phy) {
140824462549SFlorian Fainelli 		phy_stop(p->phy);
140924462549SFlorian Fainelli 		p->old_pause = -1;
141024462549SFlorian Fainelli 		p->old_link = -1;
141124462549SFlorian Fainelli 		p->old_duplex = -1;
141224462549SFlorian Fainelli 		phy_suspend(p->phy);
141324462549SFlorian Fainelli 	}
141424462549SFlorian Fainelli 
141524462549SFlorian Fainelli 	return 0;
141624462549SFlorian Fainelli }
141724462549SFlorian Fainelli 
141824462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
141924462549SFlorian Fainelli {
142024462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
142124462549SFlorian Fainelli 
142224462549SFlorian Fainelli 	netif_device_attach(slave_dev);
142324462549SFlorian Fainelli 
142424462549SFlorian Fainelli 	if (p->phy) {
142524462549SFlorian Fainelli 		phy_resume(p->phy);
142624462549SFlorian Fainelli 		phy_start(p->phy);
142724462549SFlorian Fainelli 	}
142824462549SFlorian Fainelli 
142924462549SFlorian Fainelli 	return 0;
143024462549SFlorian Fainelli }
143124462549SFlorian Fainelli 
1432d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
143383c0afaeSAndrew Lunn 		     int port, const char *name)
143491da11f8SLennert Buytenhek {
1435badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
143683c0afaeSAndrew Lunn 	struct net_device *master;
143791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
143891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
143991da11f8SLennert Buytenhek 	int ret;
144091da11f8SLennert Buytenhek 
144183c0afaeSAndrew Lunn 	master = ds->dst->master_netdev;
144283c0afaeSAndrew Lunn 	if (ds->master_netdev)
144383c0afaeSAndrew Lunn 		master = ds->master_netdev;
144483c0afaeSAndrew Lunn 
1445c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1446c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
144791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1448d87d6f44SGuenter Roeck 		return -ENOMEM;
144991da11f8SLennert Buytenhek 
1450f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1451f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14527ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14532fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
14540a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14553e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14569d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
14578b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14588b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1459f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1460d442ad4aSStephen Hemminger 
1461448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1462448b4482SAndrew Lunn 				 NULL);
1463448b4482SAndrew Lunn 
146491da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1465189b0d93SAndrew Lunn 	slave_dev->dev.of_node = ds->ports[port].dn;
146691da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
146791da11f8SLennert Buytenhek 
146891da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
1469afdcf151SVivien Didelot 	p->dp = &ds->ports[port];
1470f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
147139a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
14725075314eSAlexander Duyck 
14730d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
14740d8bcdd3SFlorian Fainelli 	p->old_link = -1;
14750d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
14760d8bcdd3SFlorian Fainelli 
1477c8b09808SAndrew Lunn 	ds->ports[port].netdev = slave_dev;
147891da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
147991da11f8SLennert Buytenhek 	if (ret) {
1480a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1481a2ae6007SJoe Perches 			   ret, slave_dev->name);
1482c8b09808SAndrew Lunn 		ds->ports[port].netdev = NULL;
148391da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1484d87d6f44SGuenter Roeck 		return ret;
148591da11f8SLennert Buytenhek 	}
148691da11f8SLennert Buytenhek 
148791da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
148891da11f8SLennert Buytenhek 
14890071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
14900071f56eSAndrew Lunn 	if (ret) {
14910071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
149273dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
14930071f56eSAndrew Lunn 		free_netdev(slave_dev);
14940071f56eSAndrew Lunn 		return ret;
14950071f56eSAndrew Lunn 	}
14960071f56eSAndrew Lunn 
1497d87d6f44SGuenter Roeck 	return 0;
149891da11f8SLennert Buytenhek }
1499b73adef6SFlorian Fainelli 
1500cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1501cda5c15bSNeil Armstrong {
1502cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1503881eadabSJohan Hovold 	struct device_node *port_dn;
1504881eadabSJohan Hovold 
1505afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1506cda5c15bSNeil Armstrong 
1507cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1508881eadabSJohan Hovold 	if (p->phy) {
1509cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1510881eadabSJohan Hovold 
1511881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1512881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1513881eadabSJohan Hovold 	}
1514cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1515cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1516cda5c15bSNeil Armstrong }
1517cda5c15bSNeil Armstrong 
1518b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1519b73adef6SFlorian Fainelli {
1520b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1521b73adef6SFlorian Fainelli }
1522b73adef6SFlorian Fainelli 
15238e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
15248e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1525b73adef6SFlorian Fainelli {
15268e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1527b73adef6SFlorian Fainelli 
15288e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
15298e92ab3aSVivien Didelot 		if (info->linking) {
15308e92ab3aSVivien Didelot 			err = dsa_slave_bridge_port_join(dev, info->upper_dev);
15318e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
15328e92ab3aSVivien Didelot 		} else {
15338e92ab3aSVivien Didelot 			dsa_slave_bridge_port_leave(dev, info->upper_dev);
15348e92ab3aSVivien Didelot 			err = NOTIFY_OK;
15358e92ab3aSVivien Didelot 		}
15366debb68aSVivien Didelot 	}
1537b73adef6SFlorian Fainelli 
15388e92ab3aSVivien Didelot 	return err;
1539b73adef6SFlorian Fainelli }
1540b73adef6SFlorian Fainelli 
154188e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1542b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1543b73adef6SFlorian Fainelli {
15446debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1545b73adef6SFlorian Fainelli 
15468e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
15478e92ab3aSVivien Didelot 		return NOTIFY_DONE;
15488e92ab3aSVivien Didelot 
15498e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
15508e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1551b73adef6SFlorian Fainelli 
1552b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1553b73adef6SFlorian Fainelli }
155488e4f0caSVivien Didelot 
155588e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
155688e4f0caSVivien Didelot 	.notifier_call	= dsa_slave_netdevice_event,
155788e4f0caSVivien Didelot };
155888e4f0caSVivien Didelot 
155988e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
156088e4f0caSVivien Didelot {
156188e4f0caSVivien Didelot 	return register_netdevice_notifier(&dsa_slave_nb);
156288e4f0caSVivien Didelot }
156388e4f0caSVivien Didelot 
156488e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
156588e4f0caSVivien Didelot {
156688e4f0caSVivien Didelot 	int err;
156788e4f0caSVivien Didelot 
156888e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
156988e4f0caSVivien Didelot 	if (err)
157088e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
157188e4f0caSVivien Didelot }
1572