xref: /openbmc/linux/net/dsa/slave.c (revision a93ecdd9484a51e830249ee9877ac1c1544e75ac)
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>
21f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
22f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
23b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2404ff53f9SFlorian Fainelli #include <linux/netpoll.h>
25ea5dd34bSVivien Didelot 
2691da11f8SLennert Buytenhek #include "dsa_priv.h"
2791da11f8SLennert Buytenhek 
28f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev);
29f50f2127SFlorian Fainelli 
30*a93ecdd9SVivien Didelot static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
3104d3a4c6SVivien Didelot {
32*a93ecdd9SVivien Didelot 	struct raw_notifier_head *nh = &dp->ds->dst->nh;
3304d3a4c6SVivien Didelot 	int err;
3404d3a4c6SVivien Didelot 
3504d3a4c6SVivien Didelot 	err = raw_notifier_call_chain(nh, e, v);
3604d3a4c6SVivien Didelot 
3704d3a4c6SVivien Didelot 	return notifier_to_errno(err);
3804d3a4c6SVivien Didelot }
3904d3a4c6SVivien Didelot 
4091da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
4191da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
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_read(ds, addr, reg);
4791da11f8SLennert Buytenhek 
4891da11f8SLennert Buytenhek 	return 0xffff;
4991da11f8SLennert Buytenhek }
5091da11f8SLennert Buytenhek 
5191da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
5291da11f8SLennert Buytenhek {
5391da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
5491da11f8SLennert Buytenhek 
550d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
569d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
5791da11f8SLennert Buytenhek 
5891da11f8SLennert Buytenhek 	return 0;
5991da11f8SLennert Buytenhek }
6091da11f8SLennert Buytenhek 
6191da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
6291da11f8SLennert Buytenhek {
6391da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
6491da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
6591da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
6691da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
670b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
680b7b498dSFlorian Fainelli 		 ds->dst->tree, ds->index);
69c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
7024df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
7191da11f8SLennert Buytenhek }
7291da11f8SLennert Buytenhek 
7391da11f8SLennert Buytenhek 
7491da11f8SLennert Buytenhek /* slave device handling ****************************************************/
75abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
76c0840801SLennert Buytenhek {
77c0840801SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
78c0840801SLennert Buytenhek 
79afdcf151SVivien Didelot 	return p->dp->ds->dst->master_netdev->ifindex;
80c0840801SLennert Buytenhek }
81c0840801SLennert Buytenhek 
82a5e9a02eSVivien Didelot static inline bool dsa_port_is_bridged(struct dsa_port *dp)
83b73adef6SFlorian Fainelli {
84a5e9a02eSVivien Didelot 	return !!dp->bridge_dev;
85b73adef6SFlorian Fainelli }
86b73adef6SFlorian Fainelli 
87fd364541SVivien Didelot static int dsa_port_set_state(struct dsa_port *dp, u8 state,
88fd364541SVivien Didelot 			      struct switchdev_trans *trans)
894acfee81SVivien Didelot {
90c5d35cb3SVivien Didelot 	struct dsa_switch *ds = dp->ds;
91c5d35cb3SVivien Didelot 	int port = dp->index;
92732f794cSVivien Didelot 
93fd364541SVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
94fd364541SVivien Didelot 		return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
95fd364541SVivien Didelot 
964acfee81SVivien Didelot 	if (ds->ops->port_stp_state_set)
974acfee81SVivien Didelot 		ds->ops->port_stp_state_set(ds, port, state);
98732f794cSVivien Didelot 
99732f794cSVivien Didelot 	if (ds->ops->port_fast_age) {
100732f794cSVivien Didelot 		/* Fast age FDB entries or flush appropriate forwarding database
101732f794cSVivien Didelot 		 * for the given port, if we are moving it from Learning or
102732f794cSVivien Didelot 		 * Forwarding state, to Disabled or Blocking or Listening state.
103732f794cSVivien Didelot 		 */
104732f794cSVivien Didelot 
105732f794cSVivien Didelot 		if ((dp->stp_state == BR_STATE_LEARNING ||
106732f794cSVivien Didelot 		     dp->stp_state == BR_STATE_FORWARDING) &&
107732f794cSVivien Didelot 		    (state == BR_STATE_DISABLED ||
108732f794cSVivien Didelot 		     state == BR_STATE_BLOCKING ||
109732f794cSVivien Didelot 		     state == BR_STATE_LISTENING))
110732f794cSVivien Didelot 			ds->ops->port_fast_age(ds, port);
111732f794cSVivien Didelot 	}
112732f794cSVivien Didelot 
113732f794cSVivien Didelot 	dp->stp_state = state;
114fd364541SVivien Didelot 
115fd364541SVivien Didelot 	return 0;
116fd364541SVivien Didelot }
117fd364541SVivien Didelot 
118fd364541SVivien Didelot static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
119fd364541SVivien Didelot {
120fd364541SVivien Didelot 	int err;
121fd364541SVivien Didelot 
122fd364541SVivien Didelot 	err = dsa_port_set_state(dp, state, NULL);
123fd364541SVivien Didelot 	if (err)
124fd364541SVivien Didelot 		pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
1254acfee81SVivien Didelot }
1264acfee81SVivien Didelot 
12791da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
12891da11f8SLennert Buytenhek {
129df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
130afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
131afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
132a5e9a02eSVivien Didelot 	u8 stp_state = dsa_port_is_bridged(p->dp) ?
133b73adef6SFlorian Fainelli 			BR_STATE_BLOCKING : BR_STATE_FORWARDING;
134df02c6ffSLennert Buytenhek 	int err;
135df02c6ffSLennert Buytenhek 
136df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
137df02c6ffSLennert Buytenhek 		return -ENETDOWN;
138df02c6ffSLennert Buytenhek 
1398feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
140a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
141df02c6ffSLennert Buytenhek 		if (err < 0)
142df02c6ffSLennert Buytenhek 			goto out;
143df02c6ffSLennert Buytenhek 	}
144df02c6ffSLennert Buytenhek 
145df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
146df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
147df02c6ffSLennert Buytenhek 		if (err < 0)
148df02c6ffSLennert Buytenhek 			goto del_unicast;
149df02c6ffSLennert Buytenhek 	}
150df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
151df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
152df02c6ffSLennert Buytenhek 		if (err < 0)
153df02c6ffSLennert Buytenhek 			goto clear_allmulti;
154df02c6ffSLennert Buytenhek 	}
155df02c6ffSLennert Buytenhek 
1569d490b4eSVivien Didelot 	if (ds->ops->port_enable) {
157afdcf151SVivien Didelot 		err = ds->ops->port_enable(ds, p->dp->index, p->phy);
158b2f2af21SFlorian Fainelli 		if (err)
159b2f2af21SFlorian Fainelli 			goto clear_promisc;
160b2f2af21SFlorian Fainelli 	}
161b2f2af21SFlorian Fainelli 
162fd364541SVivien Didelot 	dsa_port_set_state_now(p->dp, stp_state);
163b73adef6SFlorian Fainelli 
164f7f1de51SFlorian Fainelli 	if (p->phy)
165f7f1de51SFlorian Fainelli 		phy_start(p->phy);
166f7f1de51SFlorian Fainelli 
16791da11f8SLennert Buytenhek 	return 0;
168df02c6ffSLennert Buytenhek 
169b2f2af21SFlorian Fainelli clear_promisc:
170b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1714fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
172df02c6ffSLennert Buytenhek clear_allmulti:
173df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
174df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
175df02c6ffSLennert Buytenhek del_unicast:
1768feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
177a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
178df02c6ffSLennert Buytenhek out:
179df02c6ffSLennert Buytenhek 	return err;
18091da11f8SLennert Buytenhek }
18191da11f8SLennert Buytenhek 
18291da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
18391da11f8SLennert Buytenhek {
184df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
185afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
186afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
187df02c6ffSLennert Buytenhek 
188f7f1de51SFlorian Fainelli 	if (p->phy)
189f7f1de51SFlorian Fainelli 		phy_stop(p->phy);
190f7f1de51SFlorian Fainelli 
191df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
192a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
193df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
194df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
195df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
196df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
197df02c6ffSLennert Buytenhek 
1988feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
199a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
200df02c6ffSLennert Buytenhek 
2019d490b4eSVivien Didelot 	if (ds->ops->port_disable)
202afdcf151SVivien Didelot 		ds->ops->port_disable(ds, p->dp->index, p->phy);
203b2f2af21SFlorian Fainelli 
204fd364541SVivien Didelot 	dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
205b73adef6SFlorian Fainelli 
20691da11f8SLennert Buytenhek 	return 0;
20791da11f8SLennert Buytenhek }
20891da11f8SLennert Buytenhek 
20991da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
21091da11f8SLennert Buytenhek {
21191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
212afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
21391da11f8SLennert Buytenhek 
21491da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
21591da11f8SLennert Buytenhek 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
21691da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
21791da11f8SLennert Buytenhek 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
21891da11f8SLennert Buytenhek }
21991da11f8SLennert Buytenhek 
22091da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
22191da11f8SLennert Buytenhek {
22291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
223afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
22491da11f8SLennert Buytenhek 
22591da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
226a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
22791da11f8SLennert Buytenhek }
22891da11f8SLennert Buytenhek 
229df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
23091da11f8SLennert Buytenhek {
231df02c6ffSLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
232afdcf151SVivien Didelot 	struct net_device *master = p->dp->ds->dst->master_netdev;
233df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
234df02c6ffSLennert Buytenhek 	int err;
235df02c6ffSLennert Buytenhek 
236df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
237df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
238df02c6ffSLennert Buytenhek 
239df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
240df02c6ffSLennert Buytenhek 		goto out;
241df02c6ffSLennert Buytenhek 
2428feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
243a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
244df02c6ffSLennert Buytenhek 		if (err < 0)
245df02c6ffSLennert Buytenhek 			return err;
246df02c6ffSLennert Buytenhek 	}
247df02c6ffSLennert Buytenhek 
2488feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
249a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
250df02c6ffSLennert Buytenhek 
251df02c6ffSLennert Buytenhek out:
252d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
25391da11f8SLennert Buytenhek 
25491da11f8SLennert Buytenhek 	return 0;
25591da11f8SLennert Buytenhek }
25691da11f8SLennert Buytenhek 
25711149536SVivien Didelot static int dsa_slave_port_vlan_add(struct net_device *dev,
2588f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan,
259f8db8348SJiri Pirko 				   struct switchdev_trans *trans)
26011149536SVivien Didelot {
26111149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
262afdcf151SVivien Didelot 	struct dsa_port *dp = p->dp;
263afdcf151SVivien Didelot 	struct dsa_switch *ds = dp->ds;
26411149536SVivien Didelot 
26579a62eb2SJiri Pirko 	if (switchdev_trans_ph_prepare(trans)) {
2669d490b4eSVivien Didelot 		if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
26711149536SVivien Didelot 			return -EOPNOTSUPP;
26811149536SVivien Didelot 
269afdcf151SVivien Didelot 		return ds->ops->port_vlan_prepare(ds, dp->index, vlan, trans);
27011149536SVivien Didelot 	}
27111149536SVivien Didelot 
272afdcf151SVivien Didelot 	ds->ops->port_vlan_add(ds, dp->index, vlan, trans);
2734d5770b3SVivien Didelot 
27411149536SVivien Didelot 	return 0;
27511149536SVivien Didelot }
27611149536SVivien Didelot 
27711149536SVivien Didelot static int dsa_slave_port_vlan_del(struct net_device *dev,
2788f24f309SJiri Pirko 				   const struct switchdev_obj_port_vlan *vlan)
27911149536SVivien Didelot {
28011149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
281afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
28211149536SVivien Didelot 
2839d490b4eSVivien Didelot 	if (!ds->ops->port_vlan_del)
28411149536SVivien Didelot 		return -EOPNOTSUPP;
28511149536SVivien Didelot 
286afdcf151SVivien Didelot 	return ds->ops->port_vlan_del(ds, p->dp->index, vlan);
28711149536SVivien Didelot }
28811149536SVivien Didelot 
28911149536SVivien Didelot static int dsa_slave_port_vlan_dump(struct net_device *dev,
2908f24f309SJiri Pirko 				    struct switchdev_obj_port_vlan *vlan,
291648b4a99SJiri Pirko 				    switchdev_obj_dump_cb_t *cb)
29211149536SVivien Didelot {
29311149536SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
294afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
29511149536SVivien Didelot 
2969d490b4eSVivien Didelot 	if (ds->ops->port_vlan_dump)
297afdcf151SVivien Didelot 		return ds->ops->port_vlan_dump(ds, p->dp->index, vlan, cb);
29865aebfc0SVivien Didelot 
29911149536SVivien Didelot 	return -EOPNOTSUPP;
30011149536SVivien Didelot }
30111149536SVivien Didelot 
302ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_add(struct net_device *dev,
30352ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb,
304f8db8348SJiri Pirko 				  struct switchdev_trans *trans)
305cdf09697SDavid S. Miller {
306cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
307afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
308146a3206SVivien Didelot 
3098497aa61SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3109d490b4eSVivien Didelot 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
311146a3206SVivien Didelot 			return -EOPNOTSUPP;
312cdf09697SDavid S. Miller 
313afdcf151SVivien Didelot 		return ds->ops->port_fdb_prepare(ds, p->dp->index, fdb, trans);
3148497aa61SVivien Didelot 	}
315cdf09697SDavid S. Miller 
316afdcf151SVivien Didelot 	ds->ops->port_fdb_add(ds, p->dp->index, fdb, trans);
3178497aa61SVivien Didelot 
3188497aa61SVivien Didelot 	return 0;
319cdf09697SDavid S. Miller }
320cdf09697SDavid S. Miller 
321ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_del(struct net_device *dev,
32252ba57cfSJiri Pirko 				  const struct switchdev_obj_port_fdb *fdb)
323cdf09697SDavid S. Miller {
324cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
325afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
326cdf09697SDavid S. Miller 	int ret = -EOPNOTSUPP;
327cdf09697SDavid S. Miller 
3289d490b4eSVivien Didelot 	if (ds->ops->port_fdb_del)
329afdcf151SVivien Didelot 		ret = ds->ops->port_fdb_del(ds, p->dp->index, fdb);
330cdf09697SDavid S. Miller 
331cdf09697SDavid S. Miller 	return ret;
332cdf09697SDavid S. Miller }
333cdf09697SDavid S. Miller 
334ba14d9ebSVivien Didelot static int dsa_slave_port_fdb_dump(struct net_device *dev,
33552ba57cfSJiri Pirko 				   struct switchdev_obj_port_fdb *fdb,
336648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
337cdf09697SDavid S. Miller {
338cdf09697SDavid S. Miller 	struct dsa_slave_priv *p = netdev_priv(dev);
339afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
340cdf09697SDavid S. Miller 
3419d490b4eSVivien Didelot 	if (ds->ops->port_fdb_dump)
342afdcf151SVivien Didelot 		return ds->ops->port_fdb_dump(ds, p->dp->index, fdb, cb);
343ea70ba98SVivien Didelot 
344cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
345cdf09697SDavid S. Miller }
346cdf09697SDavid S. Miller 
3478df30255SVivien Didelot static int dsa_slave_port_mdb_add(struct net_device *dev,
3488df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb,
3498df30255SVivien Didelot 				  struct switchdev_trans *trans)
3508df30255SVivien Didelot {
3518df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
352afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3538df30255SVivien Didelot 
3548df30255SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3558df30255SVivien Didelot 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
3568df30255SVivien Didelot 			return -EOPNOTSUPP;
3578df30255SVivien Didelot 
358afdcf151SVivien Didelot 		return ds->ops->port_mdb_prepare(ds, p->dp->index, mdb, trans);
3598df30255SVivien Didelot 	}
3608df30255SVivien Didelot 
361afdcf151SVivien Didelot 	ds->ops->port_mdb_add(ds, p->dp->index, mdb, trans);
3628df30255SVivien Didelot 
3638df30255SVivien Didelot 	return 0;
3648df30255SVivien Didelot }
3658df30255SVivien Didelot 
3668df30255SVivien Didelot static int dsa_slave_port_mdb_del(struct net_device *dev,
3678df30255SVivien Didelot 				  const struct switchdev_obj_port_mdb *mdb)
3688df30255SVivien Didelot {
3698df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
370afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3718df30255SVivien Didelot 
3728df30255SVivien Didelot 	if (ds->ops->port_mdb_del)
373afdcf151SVivien Didelot 		return ds->ops->port_mdb_del(ds, p->dp->index, mdb);
3748df30255SVivien Didelot 
3758df30255SVivien Didelot 	return -EOPNOTSUPP;
3768df30255SVivien Didelot }
3778df30255SVivien Didelot 
3788df30255SVivien Didelot static int dsa_slave_port_mdb_dump(struct net_device *dev,
3798df30255SVivien Didelot 				   struct switchdev_obj_port_mdb *mdb,
3808df30255SVivien Didelot 				   switchdev_obj_dump_cb_t *cb)
3818df30255SVivien Didelot {
3828df30255SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
383afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
3848df30255SVivien Didelot 
3858df30255SVivien Didelot 	if (ds->ops->port_mdb_dump)
386afdcf151SVivien Didelot 		return ds->ops->port_mdb_dump(ds, p->dp->index, mdb, cb);
3878df30255SVivien Didelot 
3888df30255SVivien Didelot 	return -EOPNOTSUPP;
3898df30255SVivien Didelot }
3908df30255SVivien Didelot 
39191da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
39291da11f8SLennert Buytenhek {
39391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
39491da11f8SLennert Buytenhek 
39591da11f8SLennert Buytenhek 	if (p->phy != NULL)
39628b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
39791da11f8SLennert Buytenhek 
39891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
39991da11f8SLennert Buytenhek }
40091da11f8SLennert Buytenhek 
401fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
402fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
403fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
404fb2dabadSVivien Didelot {
405fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
406afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
407fb2dabadSVivien Didelot 
408fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
409fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
410fb2dabadSVivien Didelot 		return 0;
411fb2dabadSVivien Didelot 
4129d490b4eSVivien Didelot 	if (ds->ops->port_vlan_filtering)
413afdcf151SVivien Didelot 		return ds->ops->port_vlan_filtering(ds, p->dp->index,
414fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
415fb2dabadSVivien Didelot 
416fb2dabadSVivien Didelot 	return 0;
417fb2dabadSVivien Didelot }
418fb2dabadSVivien Didelot 
419e893de1bSVivien Didelot static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
42034a79f63SVivien Didelot 					    unsigned int ageing_time)
42134a79f63SVivien Didelot {
42234a79f63SVivien Didelot 	int i;
42334a79f63SVivien Didelot 
42426895e29SVivien Didelot 	for (i = 0; i < ds->num_ports; ++i) {
42534a79f63SVivien Didelot 		struct dsa_port *dp = &ds->ports[i];
42634a79f63SVivien Didelot 
42734a79f63SVivien Didelot 		if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
42834a79f63SVivien Didelot 			ageing_time = dp->ageing_time;
42934a79f63SVivien Didelot 	}
43034a79f63SVivien Didelot 
43134a79f63SVivien Didelot 	return ageing_time;
43234a79f63SVivien Didelot }
43334a79f63SVivien Didelot 
43434a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev,
43534a79f63SVivien Didelot 				 const struct switchdev_attr *attr,
43634a79f63SVivien Didelot 				 struct switchdev_trans *trans)
43734a79f63SVivien Didelot {
43834a79f63SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
439afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
44034a79f63SVivien Didelot 	unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
44134a79f63SVivien Didelot 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
44234a79f63SVivien Didelot 
4430f3da6afSVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
4440f3da6afSVivien Didelot 		if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
4450f3da6afSVivien Didelot 			return -ERANGE;
4460f3da6afSVivien Didelot 		if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
4470f3da6afSVivien Didelot 			return -ERANGE;
44834a79f63SVivien Didelot 		return 0;
4490f3da6afSVivien Didelot 	}
45034a79f63SVivien Didelot 
45134a79f63SVivien Didelot 	/* Keep the fastest ageing time in case of multiple bridges */
452afdcf151SVivien Didelot 	p->dp->ageing_time = ageing_time;
45334a79f63SVivien Didelot 	ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
45434a79f63SVivien Didelot 
4559d490b4eSVivien Didelot 	if (ds->ops->set_ageing_time)
4569d490b4eSVivien Didelot 		return ds->ops->set_ageing_time(ds, ageing_time);
45734a79f63SVivien Didelot 
45834a79f63SVivien Didelot 	return 0;
45934a79f63SVivien Didelot }
46034a79f63SVivien Didelot 
46135636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
462f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
4637ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
46435636062SScott Feldman {
465fd364541SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
466fd364541SVivien Didelot 	struct dsa_port *dp = p->dp;
467b8d866acSVivien Didelot 	int ret;
46835636062SScott Feldman 
46935636062SScott Feldman 	switch (attr->id) {
4701f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
471fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
47235636062SScott Feldman 		break;
473fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
474fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
475fb2dabadSVivien Didelot 		break;
47634a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
47734a79f63SVivien Didelot 		ret = dsa_slave_ageing_time(dev, attr, trans);
47834a79f63SVivien Didelot 		break;
47935636062SScott Feldman 	default:
48035636062SScott Feldman 		ret = -EOPNOTSUPP;
48135636062SScott Feldman 		break;
48235636062SScott Feldman 	}
48335636062SScott Feldman 
48435636062SScott Feldman 	return ret;
48535636062SScott Feldman }
48635636062SScott Feldman 
487ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
488648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
4897ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
490ba14d9ebSVivien Didelot {
491ba14d9ebSVivien Didelot 	int err;
492ba14d9ebSVivien Didelot 
493ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
494ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
495ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
496ba14d9ebSVivien Didelot 	 */
497ba14d9ebSVivien Didelot 
4989e8f4a54SJiri Pirko 	switch (obj->id) {
49957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
500648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_add(dev,
501648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj),
502648b4a99SJiri Pirko 					     trans);
503ba14d9ebSVivien Didelot 		break;
5048df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5058df30255SVivien Didelot 		err = dsa_slave_port_mdb_add(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5068df30255SVivien Didelot 					     trans);
5078df30255SVivien Didelot 		break;
50857d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
509648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
510648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
511648b4a99SJiri Pirko 					      trans);
51211149536SVivien Didelot 		break;
513ba14d9ebSVivien Didelot 	default:
514ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
515ba14d9ebSVivien Didelot 		break;
516ba14d9ebSVivien Didelot 	}
517ba14d9ebSVivien Didelot 
518ba14d9ebSVivien Didelot 	return err;
519ba14d9ebSVivien Didelot }
520ba14d9ebSVivien Didelot 
521ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
522648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
523ba14d9ebSVivien Didelot {
524ba14d9ebSVivien Didelot 	int err;
525ba14d9ebSVivien Didelot 
5269e8f4a54SJiri Pirko 	switch (obj->id) {
52757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
528648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_del(dev,
529648b4a99SJiri Pirko 					     SWITCHDEV_OBJ_PORT_FDB(obj));
530ba14d9ebSVivien Didelot 		break;
5318df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5328df30255SVivien Didelot 		err = dsa_slave_port_mdb_del(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
5338df30255SVivien Didelot 		break;
53457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
535648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
536648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
53711149536SVivien Didelot 		break;
538ba14d9ebSVivien Didelot 	default:
539ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
540ba14d9ebSVivien Didelot 		break;
541ba14d9ebSVivien Didelot 	}
542ba14d9ebSVivien Didelot 
543ba14d9ebSVivien Didelot 	return err;
544ba14d9ebSVivien Didelot }
545ba14d9ebSVivien Didelot 
546ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
547648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
548648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
549ba14d9ebSVivien Didelot {
550ba14d9ebSVivien Didelot 	int err;
551ba14d9ebSVivien Didelot 
5529e8f4a54SJiri Pirko 	switch (obj->id) {
55357d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
554648b4a99SJiri Pirko 		err = dsa_slave_port_fdb_dump(dev,
555648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_FDB(obj),
556648b4a99SJiri Pirko 					      cb);
557ba14d9ebSVivien Didelot 		break;
5588df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
5598df30255SVivien Didelot 		err = dsa_slave_port_mdb_dump(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
5608df30255SVivien Didelot 					      cb);
5618df30255SVivien Didelot 		break;
56257d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
563648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
564648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
565648b4a99SJiri Pirko 					       cb);
56611149536SVivien Didelot 		break;
567ba14d9ebSVivien Didelot 	default:
568ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
569ba14d9ebSVivien Didelot 		break;
570ba14d9ebSVivien Didelot 	}
571ba14d9ebSVivien Didelot 
572ba14d9ebSVivien Didelot 	return err;
573ba14d9ebSVivien Didelot }
574ba14d9ebSVivien Didelot 
575b73adef6SFlorian Fainelli static int dsa_slave_bridge_port_join(struct net_device *dev,
576b73adef6SFlorian Fainelli 				      struct net_device *br)
577b73adef6SFlorian Fainelli {
578b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
57904d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
58004d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
58104d3a4c6SVivien Didelot 		.port = p->dp->index,
58204d3a4c6SVivien Didelot 		.br = br,
58304d3a4c6SVivien Didelot 	};
58404d3a4c6SVivien Didelot 	int err;
585b73adef6SFlorian Fainelli 
5869c265426SVivien Didelot 	/* Here the port is already bridged. Reflect the current configuration
5879c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
5889c265426SVivien Didelot 	 */
589a5e9a02eSVivien Didelot 	p->dp->bridge_dev = br;
590b73adef6SFlorian Fainelli 
591*a93ecdd9SVivien Didelot 	err = dsa_port_notify(p->dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
592b73adef6SFlorian Fainelli 
5939c265426SVivien Didelot 	/* The bridging is rolled back on error */
59404d3a4c6SVivien Didelot 	if (err)
5959c265426SVivien Didelot 		p->dp->bridge_dev = NULL;
5969c265426SVivien Didelot 
59704d3a4c6SVivien Didelot 	return err;
598b73adef6SFlorian Fainelli }
599b73adef6SFlorian Fainelli 
600f123f2fbSVivien Didelot static void dsa_slave_bridge_port_leave(struct net_device *dev,
601f123f2fbSVivien Didelot 					struct net_device *br)
602b73adef6SFlorian Fainelli {
603b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
60404d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
60504d3a4c6SVivien Didelot 		.sw_index = p->dp->ds->index,
60604d3a4c6SVivien Didelot 		.port = p->dp->index,
60704d3a4c6SVivien Didelot 		.br = br,
60804d3a4c6SVivien Didelot 	};
60904d3a4c6SVivien Didelot 	int err;
610b73adef6SFlorian Fainelli 
6119c265426SVivien Didelot 	/* Here the port is already unbridged. Reflect the current configuration
6129c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
6139c265426SVivien Didelot 	 */
614f123f2fbSVivien Didelot 	p->dp->bridge_dev = NULL;
615b73adef6SFlorian Fainelli 
616*a93ecdd9SVivien Didelot 	err = dsa_port_notify(p->dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
61704d3a4c6SVivien Didelot 	if (err)
61804d3a4c6SVivien Didelot 		netdev_err(dev, "failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
619b73adef6SFlorian Fainelli 
620b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
621b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
622b73adef6SFlorian Fainelli 	 */
623fd364541SVivien Didelot 	dsa_port_set_state_now(p->dp, BR_STATE_FORWARDING);
624b73adef6SFlorian Fainelli }
625b73adef6SFlorian Fainelli 
626f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
627f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
628b73adef6SFlorian Fainelli {
629b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
630afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
631b73adef6SFlorian Fainelli 
632f8e20a9fSScott Feldman 	switch (attr->id) {
6331f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
63442275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
63542275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
636f8e20a9fSScott Feldman 		break;
637f8e20a9fSScott Feldman 	default:
638f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
639f8e20a9fSScott Feldman 	}
640b73adef6SFlorian Fainelli 
641b73adef6SFlorian Fainelli 	return 0;
642b73adef6SFlorian Fainelli }
643b73adef6SFlorian Fainelli 
64404ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
64504ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
64604ff53f9SFlorian Fainelli {
64704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
64804ff53f9SFlorian Fainelli 	if (p->netpoll)
64904ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
65004ff53f9SFlorian Fainelli #else
65104ff53f9SFlorian Fainelli 	BUG();
65204ff53f9SFlorian Fainelli #endif
65304ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
65404ff53f9SFlorian Fainelli }
65504ff53f9SFlorian Fainelli 
6563e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6573e8a72d1SFlorian Fainelli {
6583e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6594ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6603e8a72d1SFlorian Fainelli 
6614ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
6624ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
6633e8a72d1SFlorian Fainelli 
6644ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
6654ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
6664ed70ce9SFlorian Fainelli 	if (!nskb)
6674ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
6685aed85ceSFlorian Fainelli 
66904ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
67004ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
67104ff53f9SFlorian Fainelli 	 */
67204ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
67304ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
67404ff53f9SFlorian Fainelli 
6754ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
6764ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
6774ed70ce9SFlorian Fainelli 	 */
678afdcf151SVivien Didelot 	nskb->dev = p->dp->ds->dst->master_netdev;
6794ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
6805aed85ceSFlorian Fainelli 
6815aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
6825aed85ceSFlorian Fainelli }
6835aed85ceSFlorian Fainelli 
68491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
68591da11f8SLennert Buytenhek static int
686bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
687bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
68891da11f8SLennert Buytenhek {
68991da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
690e69e4626SFlorian Fainelli 	int err = -EOPNOTSUPP;
69191da11f8SLennert Buytenhek 
692e69e4626SFlorian Fainelli 	if (p->phy != NULL)
693bb10bb3eSPhilippe Reynes 		err = phy_ethtool_ksettings_get(p->phy, cmd);
69491da11f8SLennert Buytenhek 
69591da11f8SLennert Buytenhek 	return err;
69691da11f8SLennert Buytenhek }
69791da11f8SLennert Buytenhek 
69891da11f8SLennert Buytenhek static int
699bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
700bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
70191da11f8SLennert Buytenhek {
70291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
70391da11f8SLennert Buytenhek 
70491da11f8SLennert Buytenhek 	if (p->phy != NULL)
705bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
70691da11f8SLennert Buytenhek 
70791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
70891da11f8SLennert Buytenhek }
70991da11f8SLennert Buytenhek 
71091da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
71191da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
71291da11f8SLennert Buytenhek {
7137826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
7147826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
7157826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
71691da11f8SLennert Buytenhek }
71791da11f8SLennert Buytenhek 
7183d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
7193d762a0fSGuenter Roeck {
7203d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
721afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7223d762a0fSGuenter Roeck 
7239d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
724afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
7253d762a0fSGuenter Roeck 
7263d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
7273d762a0fSGuenter Roeck }
7283d762a0fSGuenter Roeck 
7293d762a0fSGuenter Roeck static void
7303d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
7313d762a0fSGuenter Roeck {
7323d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
733afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7343d762a0fSGuenter Roeck 
7359d490b4eSVivien Didelot 	if (ds->ops->get_regs)
736afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
7373d762a0fSGuenter Roeck }
7383d762a0fSGuenter Roeck 
73991da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
74091da11f8SLennert Buytenhek {
74191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
74291da11f8SLennert Buytenhek 
74391da11f8SLennert Buytenhek 	if (p->phy != NULL)
74491da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
74591da11f8SLennert Buytenhek 
74691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
74791da11f8SLennert Buytenhek }
74891da11f8SLennert Buytenhek 
74991da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
75091da11f8SLennert Buytenhek {
75191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
75291da11f8SLennert Buytenhek 
75391da11f8SLennert Buytenhek 	if (p->phy != NULL) {
75491da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
75591da11f8SLennert Buytenhek 		return p->phy->link;
75691da11f8SLennert Buytenhek 	}
75791da11f8SLennert Buytenhek 
75891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
75991da11f8SLennert Buytenhek }
76091da11f8SLennert Buytenhek 
7616793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
7626793abb4SGuenter Roeck {
7636793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
764afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7656793abb4SGuenter Roeck 
7660e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
767ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
7686793abb4SGuenter Roeck 
7699d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7709d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7716793abb4SGuenter Roeck 
7726793abb4SGuenter Roeck 	return 0;
7736793abb4SGuenter Roeck }
7746793abb4SGuenter Roeck 
7756793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7766793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7776793abb4SGuenter Roeck {
7786793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
779afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7806793abb4SGuenter Roeck 
7819d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7829d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7836793abb4SGuenter Roeck 
7846793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7856793abb4SGuenter Roeck }
7866793abb4SGuenter Roeck 
7876793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7886793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7896793abb4SGuenter Roeck {
7906793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
791afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7926793abb4SGuenter Roeck 
7939d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7949d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7956793abb4SGuenter Roeck 
7966793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7976793abb4SGuenter Roeck }
7986793abb4SGuenter Roeck 
79991da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
80091da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
80191da11f8SLennert Buytenhek {
80291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
803afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
80491da11f8SLennert Buytenhek 
80591da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
80691da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
80791da11f8SLennert Buytenhek 
80891da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
80991da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
81091da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
81191da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
8129d490b4eSVivien Didelot 		if (ds->ops->get_strings)
813afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
81491da11f8SLennert Buytenhek 	}
81591da11f8SLennert Buytenhek }
81691da11f8SLennert Buytenhek 
817badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
818badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
819badf3adaSFlorian Fainelli 					   uint64_t *data)
820badf3adaSFlorian Fainelli {
821badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8228b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
8238b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
824badf3adaSFlorian Fainelli 	int count = 0;
825badf3adaSFlorian Fainelli 
826badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
827badf3adaSFlorian Fainelli 		count = dst->master_ethtool_ops.get_sset_count(dev,
828badf3adaSFlorian Fainelli 							       ETH_SS_STATS);
829badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
830badf3adaSFlorian Fainelli 	}
831badf3adaSFlorian Fainelli 
8329d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
8339d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
834badf3adaSFlorian Fainelli }
835badf3adaSFlorian Fainelli 
836badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
837badf3adaSFlorian Fainelli {
838badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8398b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
840badf3adaSFlorian Fainelli 	int count = 0;
841badf3adaSFlorian Fainelli 
842badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count)
843badf3adaSFlorian Fainelli 		count += dst->master_ethtool_ops.get_sset_count(dev, sset);
844badf3adaSFlorian Fainelli 
8459d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
8469d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
847badf3adaSFlorian Fainelli 
848badf3adaSFlorian Fainelli 	return count;
849badf3adaSFlorian Fainelli }
850badf3adaSFlorian Fainelli 
851badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
852badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
853badf3adaSFlorian Fainelli {
854badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8558b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
8568b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
857badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
858badf3adaSFlorian Fainelli 	int mcount = 0, count;
859badf3adaSFlorian Fainelli 	unsigned int i;
860badf3adaSFlorian Fainelli 	uint8_t pfx[4];
861badf3adaSFlorian Fainelli 	uint8_t *ndata;
862badf3adaSFlorian Fainelli 
863badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
864badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
865badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
866badf3adaSFlorian Fainelli 
867badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
868badf3adaSFlorian Fainelli 		mcount = dst->master_ethtool_ops.get_sset_count(dev,
869badf3adaSFlorian Fainelli 								ETH_SS_STATS);
870badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_strings(dev, stringset, data);
871badf3adaSFlorian Fainelli 	}
872badf3adaSFlorian Fainelli 
8739d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
874badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
875badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
876badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
877badf3adaSFlorian Fainelli 		 * constructed earlier
878badf3adaSFlorian Fainelli 		 */
8799d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
8809d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
881badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
882badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
883badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
884badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
885badf3adaSFlorian Fainelli 		}
886badf3adaSFlorian Fainelli 	}
887badf3adaSFlorian Fainelli }
888badf3adaSFlorian Fainelli 
88991da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
89091da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
89191da11f8SLennert Buytenhek 					uint64_t *data)
89291da11f8SLennert Buytenhek {
89391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
894afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
89591da11f8SLennert Buytenhek 
89646e7b8d8SVivien Didelot 	data[0] = dev->stats.tx_packets;
89746e7b8d8SVivien Didelot 	data[1] = dev->stats.tx_bytes;
89846e7b8d8SVivien Didelot 	data[2] = dev->stats.rx_packets;
89946e7b8d8SVivien Didelot 	data[3] = dev->stats.rx_bytes;
9009d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
901afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
90291da11f8SLennert Buytenhek }
90391da11f8SLennert Buytenhek 
90491da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
90591da11f8SLennert Buytenhek {
90691da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
907afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
90891da11f8SLennert Buytenhek 
90991da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
91091da11f8SLennert Buytenhek 		int count;
91191da11f8SLennert Buytenhek 
91291da11f8SLennert Buytenhek 		count = 4;
9139d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
9149d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
91591da11f8SLennert Buytenhek 
91691da11f8SLennert Buytenhek 		return count;
91791da11f8SLennert Buytenhek 	}
91891da11f8SLennert Buytenhek 
91991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
92091da11f8SLennert Buytenhek }
92191da11f8SLennert Buytenhek 
92219e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
92319e57c4eSFlorian Fainelli {
92419e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
925afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
92619e57c4eSFlorian Fainelli 
9279d490b4eSVivien Didelot 	if (ds->ops->get_wol)
928afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
92919e57c4eSFlorian Fainelli }
93019e57c4eSFlorian Fainelli 
93119e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
93219e57c4eSFlorian Fainelli {
93319e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
934afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
93519e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
93619e57c4eSFlorian Fainelli 
9379d490b4eSVivien Didelot 	if (ds->ops->set_wol)
938afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
93919e57c4eSFlorian Fainelli 
94019e57c4eSFlorian Fainelli 	return ret;
94119e57c4eSFlorian Fainelli }
94219e57c4eSFlorian Fainelli 
9437905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
9447905288fSFlorian Fainelli {
9457905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
946afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9477905288fSFlorian Fainelli 	int ret;
9487905288fSFlorian Fainelli 
9499d490b4eSVivien Didelot 	if (!ds->ops->set_eee)
9507905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9517905288fSFlorian Fainelli 
952afdcf151SVivien Didelot 	ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
9537905288fSFlorian Fainelli 	if (ret)
9547905288fSFlorian Fainelli 		return ret;
9557905288fSFlorian Fainelli 
9567905288fSFlorian Fainelli 	if (p->phy)
9577905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
9587905288fSFlorian Fainelli 
9597905288fSFlorian Fainelli 	return ret;
9607905288fSFlorian Fainelli }
9617905288fSFlorian Fainelli 
9627905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
9637905288fSFlorian Fainelli {
9647905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
965afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9667905288fSFlorian Fainelli 	int ret;
9677905288fSFlorian Fainelli 
9689d490b4eSVivien Didelot 	if (!ds->ops->get_eee)
9697905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9707905288fSFlorian Fainelli 
971afdcf151SVivien Didelot 	ret = ds->ops->get_eee(ds, p->dp->index, e);
9727905288fSFlorian Fainelli 	if (ret)
9737905288fSFlorian Fainelli 		return ret;
9747905288fSFlorian Fainelli 
9757905288fSFlorian Fainelli 	if (p->phy)
9767905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
9777905288fSFlorian Fainelli 
9787905288fSFlorian Fainelli 	return ret;
9797905288fSFlorian Fainelli }
9807905288fSFlorian Fainelli 
98104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
98204ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
98304ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
98404ff53f9SFlorian Fainelli {
98504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
986afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
98704ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
98804ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
98904ff53f9SFlorian Fainelli 	int err = 0;
99004ff53f9SFlorian Fainelli 
99104ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
99204ff53f9SFlorian Fainelli 	if (!netpoll)
99304ff53f9SFlorian Fainelli 		return -ENOMEM;
99404ff53f9SFlorian Fainelli 
99504ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
99604ff53f9SFlorian Fainelli 	if (err) {
99704ff53f9SFlorian Fainelli 		kfree(netpoll);
99804ff53f9SFlorian Fainelli 		goto out;
99904ff53f9SFlorian Fainelli 	}
100004ff53f9SFlorian Fainelli 
100104ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
100204ff53f9SFlorian Fainelli out:
100304ff53f9SFlorian Fainelli 	return err;
100404ff53f9SFlorian Fainelli }
100504ff53f9SFlorian Fainelli 
100604ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
100704ff53f9SFlorian Fainelli {
100804ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
100904ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
101004ff53f9SFlorian Fainelli 
101104ff53f9SFlorian Fainelli 	if (!netpoll)
101204ff53f9SFlorian Fainelli 		return;
101304ff53f9SFlorian Fainelli 
101404ff53f9SFlorian Fainelli 	p->netpoll = NULL;
101504ff53f9SFlorian Fainelli 
101604ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
101704ff53f9SFlorian Fainelli }
101804ff53f9SFlorian Fainelli 
101904ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
102004ff53f9SFlorian Fainelli {
102104ff53f9SFlorian Fainelli }
102204ff53f9SFlorian Fainelli #endif
102304ff53f9SFlorian Fainelli 
102444bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
102544bb765cSFlorian Fainelli 					char *name, size_t len)
102644bb765cSFlorian Fainelli {
102744bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
102844bb765cSFlorian Fainelli 
1029afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
103044bb765cSFlorian Fainelli 		return -EINVAL;
10313a543ef4SFlorian Fainelli 
10323a543ef4SFlorian Fainelli 	return 0;
10333a543ef4SFlorian Fainelli }
10343a543ef4SFlorian Fainelli 
1035f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
1036f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1037f50f2127SFlorian Fainelli 			     unsigned long cookie)
1038f50f2127SFlorian Fainelli {
1039f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1040f50f2127SFlorian Fainelli 
1041f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1042f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1043f50f2127SFlorian Fainelli 			return mall_tc_entry;
1044f50f2127SFlorian Fainelli 
1045f50f2127SFlorian Fainelli 	return NULL;
1046f50f2127SFlorian Fainelli }
1047f50f2127SFlorian Fainelli 
1048f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
1049f50f2127SFlorian Fainelli 				      __be16 protocol,
1050f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
1051f50f2127SFlorian Fainelli 				      bool ingress)
1052f50f2127SFlorian Fainelli {
1053f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1054f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1055f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1056f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
1057f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
1058f50f2127SFlorian Fainelli 	struct net_device *to_dev;
1059f50f2127SFlorian Fainelli 	const struct tc_action *a;
1060f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
1061f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
1062f50f2127SFlorian Fainelli 	int ifindex;
1063f50f2127SFlorian Fainelli 
1064f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
1065f50f2127SFlorian Fainelli 		return err;
1066f50f2127SFlorian Fainelli 
1067f50f2127SFlorian Fainelli 	if (!tc_single_action(cls->exts))
1068f50f2127SFlorian Fainelli 		return err;
1069f50f2127SFlorian Fainelli 
1070f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
1071f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
1072f50f2127SFlorian Fainelli 
1073f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1074f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
1075f50f2127SFlorian Fainelli 
1076f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
1077f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
1078f50f2127SFlorian Fainelli 		if (!to_dev)
1079f50f2127SFlorian Fainelli 			return -EINVAL;
1080f50f2127SFlorian Fainelli 
1081f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
1082f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
1083f50f2127SFlorian Fainelli 
1084f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1085f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
1086f50f2127SFlorian Fainelli 			return -ENOMEM;
1087f50f2127SFlorian Fainelli 
1088f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
1089f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1090f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
1091f50f2127SFlorian Fainelli 
1092f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
1093f50f2127SFlorian Fainelli 
1094f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
1095f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
1096f50f2127SFlorian Fainelli 
1097f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1098f50f2127SFlorian Fainelli 					       ingress);
1099f50f2127SFlorian Fainelli 		if (err) {
1100f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
1101f50f2127SFlorian Fainelli 			return err;
1102f50f2127SFlorian Fainelli 		}
1103f50f2127SFlorian Fainelli 
1104f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1105f50f2127SFlorian Fainelli 	}
1106f50f2127SFlorian Fainelli 
1107f50f2127SFlorian Fainelli 	return 0;
1108f50f2127SFlorian Fainelli }
1109f50f2127SFlorian Fainelli 
1110f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1111f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1112f50f2127SFlorian Fainelli {
1113f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1114f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1115f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1116f50f2127SFlorian Fainelli 
1117f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
1118f50f2127SFlorian Fainelli 		return;
1119f50f2127SFlorian Fainelli 
1120f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1121f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1122f50f2127SFlorian Fainelli 		return;
1123f50f2127SFlorian Fainelli 
1124f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1125f50f2127SFlorian Fainelli 
1126f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1127f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
1128f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
1129f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
1130f50f2127SFlorian Fainelli 		break;
1131f50f2127SFlorian Fainelli 	default:
1132f50f2127SFlorian Fainelli 		WARN_ON(1);
1133f50f2127SFlorian Fainelli 	}
1134f50f2127SFlorian Fainelli 
1135f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1136f50f2127SFlorian Fainelli }
1137f50f2127SFlorian Fainelli 
1138f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1139f50f2127SFlorian Fainelli 			      __be16 protocol, struct tc_to_netdev *tc)
1140f50f2127SFlorian Fainelli {
1141f50f2127SFlorian Fainelli 	bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1142f50f2127SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1143f50f2127SFlorian Fainelli 
1144f50f2127SFlorian Fainelli 	switch (tc->type) {
1145f50f2127SFlorian Fainelli 	case TC_SETUP_MATCHALL:
1146f50f2127SFlorian Fainelli 		switch (tc->cls_mall->command) {
1147f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_REPLACE:
1148f50f2127SFlorian Fainelli 			return dsa_slave_add_cls_matchall(dev, protocol,
1149f50f2127SFlorian Fainelli 							  tc->cls_mall,
1150f50f2127SFlorian Fainelli 							  ingress);
1151f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_DESTROY:
1152f50f2127SFlorian Fainelli 			dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1153f50f2127SFlorian Fainelli 			return 0;
1154f50f2127SFlorian Fainelli 		}
1155f50f2127SFlorian Fainelli 	default:
1156f50f2127SFlorian Fainelli 		break;
1157f50f2127SFlorian Fainelli 	}
1158f50f2127SFlorian Fainelli 
1159f50f2127SFlorian Fainelli 	return ret;
1160f50f2127SFlorian Fainelli }
1161f50f2127SFlorian Fainelli 
1162af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1163af42192cSFlorian Fainelli {
1164af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
1165af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1166af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
1167af42192cSFlorian Fainelli }
1168af42192cSFlorian Fainelli 
1169bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1170bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1171bf9f2648SFlorian Fainelli {
1172bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1173bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1174bf9f2648SFlorian Fainelli 
1175bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1176bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1177bf9f2648SFlorian Fainelli 
1178bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1179bf9f2648SFlorian Fainelli }
1180bf9f2648SFlorian Fainelli 
1181bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1182bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1183bf9f2648SFlorian Fainelli {
1184bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1185bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1186bf9f2648SFlorian Fainelli 
1187bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1188bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1189bf9f2648SFlorian Fainelli 
1190bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1191bf9f2648SFlorian Fainelli }
1192bf9f2648SFlorian Fainelli 
119391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
119491da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
11953d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
11963d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
119791da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
119891da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
11996793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
12006793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
12016793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
120291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
120391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
120491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
120519e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
120619e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
12077905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
12087905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1209bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1210bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1211bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1212bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
121391da11f8SLennert Buytenhek };
121491da11f8SLennert Buytenhek 
12153e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1216d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1217d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
12183e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1219d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1220d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1221d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1222ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
1223ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
1224ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
1225d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1226abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
122704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
122804ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
122904ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
123004ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
123104ff53f9SFlorian Fainelli #endif
123211149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
123311149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
123411149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
123544bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1236f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
123798237d43SScott Feldman };
123898237d43SScott Feldman 
12399d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1240f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
124135636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1242ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1243ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1244ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
1245d442ad4aSStephen Hemminger };
124691da11f8SLennert Buytenhek 
1247f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1248f37db85dSFlorian Fainelli 	.name	= "dsa",
1249f37db85dSFlorian Fainelli };
1250f37db85dSFlorian Fainelli 
12510d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
12520d8bcdd3SFlorian Fainelli {
12530d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1254afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12550d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
12560d8bcdd3SFlorian Fainelli 
12570d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
12580d8bcdd3SFlorian Fainelli 		status_changed = 1;
12590d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
12600d8bcdd3SFlorian Fainelli 	}
12610d8bcdd3SFlorian Fainelli 
12620d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
12630d8bcdd3SFlorian Fainelli 		status_changed = 1;
12640d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
12650d8bcdd3SFlorian Fainelli 	}
12660d8bcdd3SFlorian Fainelli 
12670d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
12680d8bcdd3SFlorian Fainelli 		status_changed = 1;
12690d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
12700d8bcdd3SFlorian Fainelli 	}
12710d8bcdd3SFlorian Fainelli 
12729d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1273afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1274ec9436baSFlorian Fainelli 
12750d8bcdd3SFlorian Fainelli 	if (status_changed)
12760d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
12770d8bcdd3SFlorian Fainelli }
12780d8bcdd3SFlorian Fainelli 
1279ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1280ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1281ce31b31cSFlorian Fainelli {
1282b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1283b71be352SAndrew Lunn 	struct dsa_switch *ds;
1284ce31b31cSFlorian Fainelli 
1285b71be352SAndrew Lunn 	if (dev) {
1286b71be352SAndrew Lunn 		p = netdev_priv(dev);
1287afdcf151SVivien Didelot 		ds = p->dp->ds;
12889d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1289afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1290b71be352SAndrew Lunn 	}
1291ce31b31cSFlorian Fainelli 
1292ce31b31cSFlorian Fainelli 	return 0;
1293ce31b31cSFlorian Fainelli }
1294ce31b31cSFlorian Fainelli 
129591da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1296c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1297cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1298cd28a1a9SFlorian Fainelli 				 int addr)
1299c305c165SFlorian Fainelli {
1300afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1301c305c165SFlorian Fainelli 
13027f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1303d25b8e74SRussell King 	if (!p->phy) {
1304d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1305c305c165SFlorian Fainelli 		return -ENODEV;
1306d25b8e74SRussell King 	}
1307c305c165SFlorian Fainelli 
1308c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1309211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1310c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
13114078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1312c305c165SFlorian Fainelli 				  p->phy_interface);
1313c305c165SFlorian Fainelli }
1314c305c165SFlorian Fainelli 
13159697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
13160d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
13170d8bcdd3SFlorian Fainelli {
1318afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
13190d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1320ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
13216819563eSFlorian Fainelli 	u32 phy_flags = 0;
132219334920SGuenter Roeck 	int mode, ret;
13230d8bcdd3SFlorian Fainelli 
1324afdcf151SVivien Didelot 	port_dn = p->dp->dn;
132519334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
132619334920SGuenter Roeck 	if (mode < 0)
132719334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
132819334920SGuenter Roeck 	p->phy_interface = mode;
13290d8bcdd3SFlorian Fainelli 
13300d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
13310d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
13320d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
13330d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
13340d8bcdd3SFlorian Fainelli 		 */
13350d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
13360d8bcdd3SFlorian Fainelli 		if (ret) {
1337d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
13389697f1cdSFlorian Fainelli 			return ret;
13390d8bcdd3SFlorian Fainelli 		}
1340ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
13410d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
13420d8bcdd3SFlorian Fainelli 	}
13430d8bcdd3SFlorian Fainelli 
13449d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1345afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
13466819563eSFlorian Fainelli 
1347cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1348d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1349d25b8e74SRussell King 
1350cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1351cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1352cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1353cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1354cd28a1a9SFlorian Fainelli 		 */
1355d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1356d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1357d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1358d25b8e74SRussell King 			if (ret) {
1359d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
13600d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1361cd28a1a9SFlorian Fainelli 				return ret;
1362d25b8e74SRussell King 			}
1363cd28a1a9SFlorian Fainelli 		} else {
13640d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1365cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1366cd28a1a9SFlorian Fainelli 						phy_flags,
13670d8bcdd3SFlorian Fainelli 						p->phy_interface);
1368cd28a1a9SFlorian Fainelli 		}
13690d8f3c67SJohan Hovold 
13700d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1371cd28a1a9SFlorian Fainelli 	}
13720d8bcdd3SFlorian Fainelli 
1373ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1374ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1375ce31b31cSFlorian Fainelli 
13760d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
13770d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
13780d8bcdd3SFlorian Fainelli 	 */
1379b31f65fbSAndrew Lunn 	if (!p->phy) {
1380afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1381d25b8e74SRussell King 		if (ret) {
1382afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1383afdcf151SVivien Didelot 				   p->dp->index, ret);
1384881eadabSJohan Hovold 			if (phy_is_fixed)
1385881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1386c305c165SFlorian Fainelli 			return ret;
1387d25b8e74SRussell King 		}
13880d8bcdd3SFlorian Fainelli 	}
13899697f1cdSFlorian Fainelli 
13902220943aSAndrew Lunn 	phy_attached_info(p->phy);
13912220943aSAndrew Lunn 
13929697f1cdSFlorian Fainelli 	return 0;
1393b31f65fbSAndrew Lunn }
13940d8bcdd3SFlorian Fainelli 
1395448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1396448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1397448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1398448b4482SAndrew Lunn 					    void *_unused)
1399448b4482SAndrew Lunn {
1400448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1401448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1402448b4482SAndrew Lunn }
1403448b4482SAndrew Lunn 
140424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
140524462549SFlorian Fainelli {
140624462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
140724462549SFlorian Fainelli 
1408f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1409f154be24SFlorian Fainelli 
141024462549SFlorian Fainelli 	if (p->phy) {
141124462549SFlorian Fainelli 		phy_stop(p->phy);
141224462549SFlorian Fainelli 		p->old_pause = -1;
141324462549SFlorian Fainelli 		p->old_link = -1;
141424462549SFlorian Fainelli 		p->old_duplex = -1;
141524462549SFlorian Fainelli 		phy_suspend(p->phy);
141624462549SFlorian Fainelli 	}
141724462549SFlorian Fainelli 
141824462549SFlorian Fainelli 	return 0;
141924462549SFlorian Fainelli }
142024462549SFlorian Fainelli 
142124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
142224462549SFlorian Fainelli {
142324462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
142424462549SFlorian Fainelli 
142524462549SFlorian Fainelli 	netif_device_attach(slave_dev);
142624462549SFlorian Fainelli 
142724462549SFlorian Fainelli 	if (p->phy) {
142824462549SFlorian Fainelli 		phy_resume(p->phy);
142924462549SFlorian Fainelli 		phy_start(p->phy);
143024462549SFlorian Fainelli 	}
143124462549SFlorian Fainelli 
143224462549SFlorian Fainelli 	return 0;
143324462549SFlorian Fainelli }
143424462549SFlorian Fainelli 
1435d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
143683c0afaeSAndrew Lunn 		     int port, const char *name)
143791da11f8SLennert Buytenhek {
1438badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
143983c0afaeSAndrew Lunn 	struct net_device *master;
144091da11f8SLennert Buytenhek 	struct net_device *slave_dev;
144191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
144291da11f8SLennert Buytenhek 	int ret;
144391da11f8SLennert Buytenhek 
144483c0afaeSAndrew Lunn 	master = ds->dst->master_netdev;
144583c0afaeSAndrew Lunn 	if (ds->master_netdev)
144683c0afaeSAndrew Lunn 		master = ds->master_netdev;
144783c0afaeSAndrew Lunn 
1448c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1449c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
145091da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1451d87d6f44SGuenter Roeck 		return -ENOMEM;
145291da11f8SLennert Buytenhek 
1453f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1454f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14557ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14562fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
14570a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14583e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14599d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
14608b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14618b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1462f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1463d442ad4aSStephen Hemminger 
1464448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1465448b4482SAndrew Lunn 				 NULL);
1466448b4482SAndrew Lunn 
146791da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1468189b0d93SAndrew Lunn 	slave_dev->dev.of_node = ds->ports[port].dn;
146991da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
147091da11f8SLennert Buytenhek 
147191da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
1472afdcf151SVivien Didelot 	p->dp = &ds->ports[port];
1473f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
147439a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
14755075314eSAlexander Duyck 
14760d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
14770d8bcdd3SFlorian Fainelli 	p->old_link = -1;
14780d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
14790d8bcdd3SFlorian Fainelli 
1480c8b09808SAndrew Lunn 	ds->ports[port].netdev = slave_dev;
148191da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
148291da11f8SLennert Buytenhek 	if (ret) {
1483a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1484a2ae6007SJoe Perches 			   ret, slave_dev->name);
1485c8b09808SAndrew Lunn 		ds->ports[port].netdev = NULL;
148691da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1487d87d6f44SGuenter Roeck 		return ret;
148891da11f8SLennert Buytenhek 	}
148991da11f8SLennert Buytenhek 
149091da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
149191da11f8SLennert Buytenhek 
14920071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
14930071f56eSAndrew Lunn 	if (ret) {
14940071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
149573dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
14960071f56eSAndrew Lunn 		free_netdev(slave_dev);
14970071f56eSAndrew Lunn 		return ret;
14980071f56eSAndrew Lunn 	}
14990071f56eSAndrew Lunn 
1500d87d6f44SGuenter Roeck 	return 0;
150191da11f8SLennert Buytenhek }
1502b73adef6SFlorian Fainelli 
1503cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1504cda5c15bSNeil Armstrong {
1505cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1506881eadabSJohan Hovold 	struct device_node *port_dn;
1507881eadabSJohan Hovold 
1508afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1509cda5c15bSNeil Armstrong 
1510cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1511881eadabSJohan Hovold 	if (p->phy) {
1512cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1513881eadabSJohan Hovold 
1514881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1515881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1516881eadabSJohan Hovold 	}
1517cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1518cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1519cda5c15bSNeil Armstrong }
1520cda5c15bSNeil Armstrong 
1521b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1522b73adef6SFlorian Fainelli {
1523b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1524b73adef6SFlorian Fainelli }
1525b73adef6SFlorian Fainelli 
15268e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
15278e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1528b73adef6SFlorian Fainelli {
15298e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1530b73adef6SFlorian Fainelli 
15318e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
15328e92ab3aSVivien Didelot 		if (info->linking) {
15338e92ab3aSVivien Didelot 			err = dsa_slave_bridge_port_join(dev, info->upper_dev);
15348e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
15358e92ab3aSVivien Didelot 		} else {
15368e92ab3aSVivien Didelot 			dsa_slave_bridge_port_leave(dev, info->upper_dev);
15378e92ab3aSVivien Didelot 			err = NOTIFY_OK;
15388e92ab3aSVivien Didelot 		}
15396debb68aSVivien Didelot 	}
1540b73adef6SFlorian Fainelli 
15418e92ab3aSVivien Didelot 	return err;
1542b73adef6SFlorian Fainelli }
1543b73adef6SFlorian Fainelli 
154488e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1545b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1546b73adef6SFlorian Fainelli {
15476debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1548b73adef6SFlorian Fainelli 
15498e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
15508e92ab3aSVivien Didelot 		return NOTIFY_DONE;
15518e92ab3aSVivien Didelot 
15528e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
15538e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1554b73adef6SFlorian Fainelli 
1555b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1556b73adef6SFlorian Fainelli }
155788e4f0caSVivien Didelot 
155888e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
155988e4f0caSVivien Didelot 	.notifier_call	= dsa_slave_netdevice_event,
156088e4f0caSVivien Didelot };
156188e4f0caSVivien Didelot 
156288e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
156388e4f0caSVivien Didelot {
156488e4f0caSVivien Didelot 	return register_netdevice_notifier(&dsa_slave_nb);
156588e4f0caSVivien Didelot }
156688e4f0caSVivien Didelot 
156788e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
156888e4f0caSVivien Didelot {
156988e4f0caSVivien Didelot 	int err;
157088e4f0caSVivien Didelot 
157188e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
157288e4f0caSVivien Didelot 	if (err)
157388e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
157488e4f0caSVivien Didelot }
1575