xref: /openbmc/linux/net/dsa/slave.c (revision bcebb976ec433e1f8a81d1a70db26420e85386aa)
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 
30a93ecdd9SVivien Didelot static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
3104d3a4c6SVivien Didelot {
32a93ecdd9SVivien 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 
3023fdb023bSVivien Didelot static int dsa_port_fdb_add(struct dsa_port *dp,
30352ba57cfSJiri Pirko 			    const struct switchdev_obj_port_fdb *fdb,
304f8db8348SJiri Pirko 			    struct switchdev_trans *trans)
305cdf09697SDavid S. Miller {
3063fdb023bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
307146a3206SVivien Didelot 
3088497aa61SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3099d490b4eSVivien Didelot 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
310146a3206SVivien Didelot 			return -EOPNOTSUPP;
311cdf09697SDavid S. Miller 
3123fdb023bSVivien Didelot 		return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
3138497aa61SVivien Didelot 	}
314cdf09697SDavid S. Miller 
3153fdb023bSVivien Didelot 	ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
3168497aa61SVivien Didelot 
3178497aa61SVivien Didelot 	return 0;
318cdf09697SDavid S. Miller }
319cdf09697SDavid S. Miller 
3203fdb023bSVivien Didelot static int dsa_port_fdb_del(struct dsa_port *dp,
32152ba57cfSJiri Pirko 			    const struct switchdev_obj_port_fdb *fdb)
322cdf09697SDavid S. Miller {
3233fdb023bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
324cdf09697SDavid S. Miller 	int ret = -EOPNOTSUPP;
325cdf09697SDavid S. Miller 
3269d490b4eSVivien Didelot 	if (ds->ops->port_fdb_del)
3273fdb023bSVivien Didelot 		ret = ds->ops->port_fdb_del(ds, dp->index, fdb);
328cdf09697SDavid S. Miller 
329cdf09697SDavid S. Miller 	return ret;
330cdf09697SDavid S. Miller }
331cdf09697SDavid S. Miller 
3323fdb023bSVivien Didelot static int dsa_port_fdb_dump(struct dsa_port *dp,
33352ba57cfSJiri Pirko 			     struct switchdev_obj_port_fdb *fdb,
334648b4a99SJiri Pirko 			     switchdev_obj_dump_cb_t *cb)
335cdf09697SDavid S. Miller {
3363fdb023bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
337cdf09697SDavid S. Miller 
3389d490b4eSVivien Didelot 	if (ds->ops->port_fdb_dump)
3393fdb023bSVivien Didelot 		return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
340ea70ba98SVivien Didelot 
341cdf09697SDavid S. Miller 	return -EOPNOTSUPP;
342cdf09697SDavid S. Miller }
343cdf09697SDavid S. Miller 
344*bcebb976SVivien Didelot static int dsa_port_mdb_add(struct dsa_port *dp,
3458df30255SVivien Didelot 			    const struct switchdev_obj_port_mdb *mdb,
3468df30255SVivien Didelot 			    struct switchdev_trans *trans)
3478df30255SVivien Didelot {
348*bcebb976SVivien Didelot 	struct dsa_switch *ds = dp->ds;
3498df30255SVivien Didelot 
3508df30255SVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
3518df30255SVivien Didelot 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
3528df30255SVivien Didelot 			return -EOPNOTSUPP;
3538df30255SVivien Didelot 
354*bcebb976SVivien Didelot 		return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
3558df30255SVivien Didelot 	}
3568df30255SVivien Didelot 
357*bcebb976SVivien Didelot 	ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
3588df30255SVivien Didelot 
3598df30255SVivien Didelot 	return 0;
3608df30255SVivien Didelot }
3618df30255SVivien Didelot 
362*bcebb976SVivien Didelot static int dsa_port_mdb_del(struct dsa_port *dp,
3638df30255SVivien Didelot 			    const struct switchdev_obj_port_mdb *mdb)
3648df30255SVivien Didelot {
365*bcebb976SVivien Didelot 	struct dsa_switch *ds = dp->ds;
3668df30255SVivien Didelot 
3678df30255SVivien Didelot 	if (ds->ops->port_mdb_del)
368*bcebb976SVivien Didelot 		return ds->ops->port_mdb_del(ds, dp->index, mdb);
3698df30255SVivien Didelot 
3708df30255SVivien Didelot 	return -EOPNOTSUPP;
3718df30255SVivien Didelot }
3728df30255SVivien Didelot 
373*bcebb976SVivien Didelot static int dsa_port_mdb_dump(struct dsa_port *dp,
3748df30255SVivien Didelot 			     struct switchdev_obj_port_mdb *mdb,
3758df30255SVivien Didelot 			     switchdev_obj_dump_cb_t *cb)
3768df30255SVivien Didelot {
377*bcebb976SVivien Didelot 	struct dsa_switch *ds = dp->ds;
3788df30255SVivien Didelot 
3798df30255SVivien Didelot 	if (ds->ops->port_mdb_dump)
380*bcebb976SVivien Didelot 		return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);
3818df30255SVivien Didelot 
3828df30255SVivien Didelot 	return -EOPNOTSUPP;
3838df30255SVivien Didelot }
3848df30255SVivien Didelot 
38591da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
38691da11f8SLennert Buytenhek {
38791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
38891da11f8SLennert Buytenhek 
38991da11f8SLennert Buytenhek 	if (p->phy != NULL)
39028b04113SRichard Cochran 		return phy_mii_ioctl(p->phy, ifr, cmd);
39191da11f8SLennert Buytenhek 
39291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
39391da11f8SLennert Buytenhek }
39491da11f8SLennert Buytenhek 
395fb2dabadSVivien Didelot static int dsa_slave_vlan_filtering(struct net_device *dev,
396fb2dabadSVivien Didelot 				    const struct switchdev_attr *attr,
397fb2dabadSVivien Didelot 				    struct switchdev_trans *trans)
398fb2dabadSVivien Didelot {
399fb2dabadSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
400afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
401fb2dabadSVivien Didelot 
402fb2dabadSVivien Didelot 	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
403fb2dabadSVivien Didelot 	if (switchdev_trans_ph_prepare(trans))
404fb2dabadSVivien Didelot 		return 0;
405fb2dabadSVivien Didelot 
4069d490b4eSVivien Didelot 	if (ds->ops->port_vlan_filtering)
407afdcf151SVivien Didelot 		return ds->ops->port_vlan_filtering(ds, p->dp->index,
408fb2dabadSVivien Didelot 						    attr->u.vlan_filtering);
409fb2dabadSVivien Didelot 
410fb2dabadSVivien Didelot 	return 0;
411fb2dabadSVivien Didelot }
412fb2dabadSVivien Didelot 
413e893de1bSVivien Didelot static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
41434a79f63SVivien Didelot 					    unsigned int ageing_time)
41534a79f63SVivien Didelot {
41634a79f63SVivien Didelot 	int i;
41734a79f63SVivien Didelot 
41826895e29SVivien Didelot 	for (i = 0; i < ds->num_ports; ++i) {
41934a79f63SVivien Didelot 		struct dsa_port *dp = &ds->ports[i];
42034a79f63SVivien Didelot 
42134a79f63SVivien Didelot 		if (dp && dp->ageing_time && dp->ageing_time < ageing_time)
42234a79f63SVivien Didelot 			ageing_time = dp->ageing_time;
42334a79f63SVivien Didelot 	}
42434a79f63SVivien Didelot 
42534a79f63SVivien Didelot 	return ageing_time;
42634a79f63SVivien Didelot }
42734a79f63SVivien Didelot 
42834a79f63SVivien Didelot static int dsa_slave_ageing_time(struct net_device *dev,
42934a79f63SVivien Didelot 				 const struct switchdev_attr *attr,
43034a79f63SVivien Didelot 				 struct switchdev_trans *trans)
43134a79f63SVivien Didelot {
43234a79f63SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
433afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
43434a79f63SVivien Didelot 	unsigned long ageing_jiffies = clock_t_to_jiffies(attr->u.ageing_time);
43534a79f63SVivien Didelot 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
43634a79f63SVivien Didelot 
4370f3da6afSVivien Didelot 	if (switchdev_trans_ph_prepare(trans)) {
4380f3da6afSVivien Didelot 		if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
4390f3da6afSVivien Didelot 			return -ERANGE;
4400f3da6afSVivien Didelot 		if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
4410f3da6afSVivien Didelot 			return -ERANGE;
44234a79f63SVivien Didelot 		return 0;
4430f3da6afSVivien Didelot 	}
44434a79f63SVivien Didelot 
44534a79f63SVivien Didelot 	/* Keep the fastest ageing time in case of multiple bridges */
446afdcf151SVivien Didelot 	p->dp->ageing_time = ageing_time;
44734a79f63SVivien Didelot 	ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
44834a79f63SVivien Didelot 
4499d490b4eSVivien Didelot 	if (ds->ops->set_ageing_time)
4509d490b4eSVivien Didelot 		return ds->ops->set_ageing_time(ds, ageing_time);
45134a79f63SVivien Didelot 
45234a79f63SVivien Didelot 	return 0;
45334a79f63SVivien Didelot }
45434a79f63SVivien Didelot 
45535636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
456f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
4577ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
45835636062SScott Feldman {
459fd364541SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
460fd364541SVivien Didelot 	struct dsa_port *dp = p->dp;
461b8d866acSVivien Didelot 	int ret;
46235636062SScott Feldman 
46335636062SScott Feldman 	switch (attr->id) {
4641f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
465fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
46635636062SScott Feldman 		break;
467fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
468fb2dabadSVivien Didelot 		ret = dsa_slave_vlan_filtering(dev, attr, trans);
469fb2dabadSVivien Didelot 		break;
47034a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
47134a79f63SVivien Didelot 		ret = dsa_slave_ageing_time(dev, attr, trans);
47234a79f63SVivien Didelot 		break;
47335636062SScott Feldman 	default:
47435636062SScott Feldman 		ret = -EOPNOTSUPP;
47535636062SScott Feldman 		break;
47635636062SScott Feldman 	}
47735636062SScott Feldman 
47835636062SScott Feldman 	return ret;
47935636062SScott Feldman }
48035636062SScott Feldman 
481ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
482648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
4837ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
484ba14d9ebSVivien Didelot {
4853fdb023bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4863fdb023bSVivien Didelot 	struct dsa_port *dp = p->dp;
487ba14d9ebSVivien Didelot 	int err;
488ba14d9ebSVivien Didelot 
489ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
490ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
491ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
492ba14d9ebSVivien Didelot 	 */
493ba14d9ebSVivien Didelot 
4949e8f4a54SJiri Pirko 	switch (obj->id) {
49557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
4963fdb023bSVivien Didelot 		err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
497ba14d9ebSVivien Didelot 		break;
4988df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
499*bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
5008df30255SVivien Didelot 		break;
50157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
502648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_add(dev,
503648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj),
504648b4a99SJiri Pirko 					      trans);
50511149536SVivien Didelot 		break;
506ba14d9ebSVivien Didelot 	default:
507ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
508ba14d9ebSVivien Didelot 		break;
509ba14d9ebSVivien Didelot 	}
510ba14d9ebSVivien Didelot 
511ba14d9ebSVivien Didelot 	return err;
512ba14d9ebSVivien Didelot }
513ba14d9ebSVivien Didelot 
514ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
515648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
516ba14d9ebSVivien Didelot {
5173fdb023bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
5183fdb023bSVivien Didelot 	struct dsa_port *dp = p->dp;
519ba14d9ebSVivien Didelot 	int err;
520ba14d9ebSVivien Didelot 
5219e8f4a54SJiri Pirko 	switch (obj->id) {
52257d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
5233fdb023bSVivien Didelot 		err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
524ba14d9ebSVivien Didelot 		break;
5258df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
526*bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
5278df30255SVivien Didelot 		break;
52857d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
529648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_del(dev,
530648b4a99SJiri Pirko 					      SWITCHDEV_OBJ_PORT_VLAN(obj));
53111149536SVivien Didelot 		break;
532ba14d9ebSVivien Didelot 	default:
533ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
534ba14d9ebSVivien Didelot 		break;
535ba14d9ebSVivien Didelot 	}
536ba14d9ebSVivien Didelot 
537ba14d9ebSVivien Didelot 	return err;
538ba14d9ebSVivien Didelot }
539ba14d9ebSVivien Didelot 
540ba14d9ebSVivien Didelot static int dsa_slave_port_obj_dump(struct net_device *dev,
541648b4a99SJiri Pirko 				   struct switchdev_obj *obj,
542648b4a99SJiri Pirko 				   switchdev_obj_dump_cb_t *cb)
543ba14d9ebSVivien Didelot {
5443fdb023bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
5453fdb023bSVivien Didelot 	struct dsa_port *dp = p->dp;
546ba14d9ebSVivien Didelot 	int err;
547ba14d9ebSVivien Didelot 
5489e8f4a54SJiri Pirko 	switch (obj->id) {
54957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_FDB:
5503fdb023bSVivien Didelot 		err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
551ba14d9ebSVivien Didelot 		break;
5528df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
553*bcebb976SVivien Didelot 		err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
5548df30255SVivien Didelot 		break;
55557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
556648b4a99SJiri Pirko 		err = dsa_slave_port_vlan_dump(dev,
557648b4a99SJiri Pirko 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
558648b4a99SJiri Pirko 					       cb);
55911149536SVivien Didelot 		break;
560ba14d9ebSVivien Didelot 	default:
561ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
562ba14d9ebSVivien Didelot 		break;
563ba14d9ebSVivien Didelot 	}
564ba14d9ebSVivien Didelot 
565ba14d9ebSVivien Didelot 	return err;
566ba14d9ebSVivien Didelot }
567ba14d9ebSVivien Didelot 
56817d7802bSVivien Didelot static int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
569b73adef6SFlorian Fainelli {
57004d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
57117d7802bSVivien Didelot 		.sw_index = dp->ds->index,
57217d7802bSVivien Didelot 		.port = dp->index,
57304d3a4c6SVivien Didelot 		.br = br,
57404d3a4c6SVivien Didelot 	};
57504d3a4c6SVivien Didelot 	int err;
576b73adef6SFlorian Fainelli 
5779c265426SVivien Didelot 	/* Here the port is already bridged. Reflect the current configuration
5789c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
5799c265426SVivien Didelot 	 */
58017d7802bSVivien Didelot 	dp->bridge_dev = br;
581b73adef6SFlorian Fainelli 
58217d7802bSVivien Didelot 	err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
583b73adef6SFlorian Fainelli 
5849c265426SVivien Didelot 	/* The bridging is rolled back on error */
58504d3a4c6SVivien Didelot 	if (err)
58617d7802bSVivien Didelot 		dp->bridge_dev = NULL;
5879c265426SVivien Didelot 
58804d3a4c6SVivien Didelot 	return err;
589b73adef6SFlorian Fainelli }
590b73adef6SFlorian Fainelli 
59117d7802bSVivien Didelot static void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
592b73adef6SFlorian Fainelli {
59304d3a4c6SVivien Didelot 	struct dsa_notifier_bridge_info info = {
59417d7802bSVivien Didelot 		.sw_index = dp->ds->index,
59517d7802bSVivien Didelot 		.port = dp->index,
59604d3a4c6SVivien Didelot 		.br = br,
59704d3a4c6SVivien Didelot 	};
59804d3a4c6SVivien Didelot 	int err;
599b73adef6SFlorian Fainelli 
6009c265426SVivien Didelot 	/* Here the port is already unbridged. Reflect the current configuration
6019c265426SVivien Didelot 	 * so that drivers can program their chips accordingly.
6029c265426SVivien Didelot 	 */
60317d7802bSVivien Didelot 	dp->bridge_dev = NULL;
604b73adef6SFlorian Fainelli 
60517d7802bSVivien Didelot 	err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
60604d3a4c6SVivien Didelot 	if (err)
60717d7802bSVivien Didelot 		pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
608b73adef6SFlorian Fainelli 
609b73adef6SFlorian Fainelli 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
610b73adef6SFlorian Fainelli 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
611b73adef6SFlorian Fainelli 	 */
61217d7802bSVivien Didelot 	dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
613b73adef6SFlorian Fainelli }
614b73adef6SFlorian Fainelli 
615f8e20a9fSScott Feldman static int dsa_slave_port_attr_get(struct net_device *dev,
616f8e20a9fSScott Feldman 				   struct switchdev_attr *attr)
617b73adef6SFlorian Fainelli {
618b73adef6SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
619afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
620b73adef6SFlorian Fainelli 
621f8e20a9fSScott Feldman 	switch (attr->id) {
6221f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
62342275bd8SScott Feldman 		attr->u.ppid.id_len = sizeof(ds->index);
62442275bd8SScott Feldman 		memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
625f8e20a9fSScott Feldman 		break;
626f8e20a9fSScott Feldman 	default:
627f8e20a9fSScott Feldman 		return -EOPNOTSUPP;
628f8e20a9fSScott Feldman 	}
629b73adef6SFlorian Fainelli 
630b73adef6SFlorian Fainelli 	return 0;
631b73adef6SFlorian Fainelli }
632b73adef6SFlorian Fainelli 
63304ff53f9SFlorian Fainelli static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
63404ff53f9SFlorian Fainelli 					       struct sk_buff *skb)
63504ff53f9SFlorian Fainelli {
63604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
63704ff53f9SFlorian Fainelli 	if (p->netpoll)
63804ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
63904ff53f9SFlorian Fainelli #else
64004ff53f9SFlorian Fainelli 	BUG();
64104ff53f9SFlorian Fainelli #endif
64204ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
64304ff53f9SFlorian Fainelli }
64404ff53f9SFlorian Fainelli 
6453e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6463e8a72d1SFlorian Fainelli {
6473e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6484ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6493e8a72d1SFlorian Fainelli 
6504ed70ce9SFlorian Fainelli 	dev->stats.tx_packets++;
6514ed70ce9SFlorian Fainelli 	dev->stats.tx_bytes += skb->len;
6523e8a72d1SFlorian Fainelli 
6534ed70ce9SFlorian Fainelli 	/* Transmit function may have to reallocate the original SKB */
6544ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
6554ed70ce9SFlorian Fainelli 	if (!nskb)
6564ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
6575aed85ceSFlorian Fainelli 
65804ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
65904ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
66004ff53f9SFlorian Fainelli 	 */
66104ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
66204ff53f9SFlorian Fainelli 		return dsa_netpoll_send_skb(p, nskb);
66304ff53f9SFlorian Fainelli 
6644ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
6654ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
6664ed70ce9SFlorian Fainelli 	 */
667afdcf151SVivien Didelot 	nskb->dev = p->dp->ds->dst->master_netdev;
6684ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
6695aed85ceSFlorian Fainelli 
6705aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
6715aed85ceSFlorian Fainelli }
6725aed85ceSFlorian Fainelli 
67391da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
67491da11f8SLennert Buytenhek static int
675bb10bb3eSPhilippe Reynes dsa_slave_get_link_ksettings(struct net_device *dev,
676bb10bb3eSPhilippe Reynes 			     struct ethtool_link_ksettings *cmd)
67791da11f8SLennert Buytenhek {
67891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
679e69e4626SFlorian Fainelli 	int err = -EOPNOTSUPP;
68091da11f8SLennert Buytenhek 
681e69e4626SFlorian Fainelli 	if (p->phy != NULL)
682bb10bb3eSPhilippe Reynes 		err = phy_ethtool_ksettings_get(p->phy, cmd);
68391da11f8SLennert Buytenhek 
68491da11f8SLennert Buytenhek 	return err;
68591da11f8SLennert Buytenhek }
68691da11f8SLennert Buytenhek 
68791da11f8SLennert Buytenhek static int
688bb10bb3eSPhilippe Reynes dsa_slave_set_link_ksettings(struct net_device *dev,
689bb10bb3eSPhilippe Reynes 			     const struct ethtool_link_ksettings *cmd)
69091da11f8SLennert Buytenhek {
69191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
69291da11f8SLennert Buytenhek 
69391da11f8SLennert Buytenhek 	if (p->phy != NULL)
694bb10bb3eSPhilippe Reynes 		return phy_ethtool_ksettings_set(p->phy, cmd);
69591da11f8SLennert Buytenhek 
69691da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
69791da11f8SLennert Buytenhek }
69891da11f8SLennert Buytenhek 
69991da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
70091da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
70191da11f8SLennert Buytenhek {
7027826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
7037826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
7047826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
70591da11f8SLennert Buytenhek }
70691da11f8SLennert Buytenhek 
7073d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
7083d762a0fSGuenter Roeck {
7093d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
710afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7113d762a0fSGuenter Roeck 
7129d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
713afdcf151SVivien Didelot 		return ds->ops->get_regs_len(ds, p->dp->index);
7143d762a0fSGuenter Roeck 
7153d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
7163d762a0fSGuenter Roeck }
7173d762a0fSGuenter Roeck 
7183d762a0fSGuenter Roeck static void
7193d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
7203d762a0fSGuenter Roeck {
7213d762a0fSGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
722afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7233d762a0fSGuenter Roeck 
7249d490b4eSVivien Didelot 	if (ds->ops->get_regs)
725afdcf151SVivien Didelot 		ds->ops->get_regs(ds, p->dp->index, regs, _p);
7263d762a0fSGuenter Roeck }
7273d762a0fSGuenter Roeck 
72891da11f8SLennert Buytenhek static int dsa_slave_nway_reset(struct net_device *dev)
72991da11f8SLennert Buytenhek {
73091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
73191da11f8SLennert Buytenhek 
73291da11f8SLennert Buytenhek 	if (p->phy != NULL)
73391da11f8SLennert Buytenhek 		return genphy_restart_aneg(p->phy);
73491da11f8SLennert Buytenhek 
73591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
73691da11f8SLennert Buytenhek }
73791da11f8SLennert Buytenhek 
73891da11f8SLennert Buytenhek static u32 dsa_slave_get_link(struct net_device *dev)
73991da11f8SLennert Buytenhek {
74091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
74191da11f8SLennert Buytenhek 
74291da11f8SLennert Buytenhek 	if (p->phy != NULL) {
74391da11f8SLennert Buytenhek 		genphy_update_link(p->phy);
74491da11f8SLennert Buytenhek 		return p->phy->link;
74591da11f8SLennert Buytenhek 	}
74691da11f8SLennert Buytenhek 
74791da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
74891da11f8SLennert Buytenhek }
74991da11f8SLennert Buytenhek 
7506793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
7516793abb4SGuenter Roeck {
7526793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
753afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7546793abb4SGuenter Roeck 
7550e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
756ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
7576793abb4SGuenter Roeck 
7589d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7599d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7606793abb4SGuenter Roeck 
7616793abb4SGuenter Roeck 	return 0;
7626793abb4SGuenter Roeck }
7636793abb4SGuenter Roeck 
7646793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7656793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7666793abb4SGuenter Roeck {
7676793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
768afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7696793abb4SGuenter Roeck 
7709d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7719d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7726793abb4SGuenter Roeck 
7736793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7746793abb4SGuenter Roeck }
7756793abb4SGuenter Roeck 
7766793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7776793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7786793abb4SGuenter Roeck {
7796793abb4SGuenter Roeck 	struct dsa_slave_priv *p = netdev_priv(dev);
780afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
7816793abb4SGuenter Roeck 
7829d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7839d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7846793abb4SGuenter Roeck 
7856793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7866793abb4SGuenter Roeck }
7876793abb4SGuenter Roeck 
78891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
78991da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
79091da11f8SLennert Buytenhek {
79191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
792afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
79391da11f8SLennert Buytenhek 
79491da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
79591da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
79691da11f8SLennert Buytenhek 
79791da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
79891da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
79991da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
80091da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
8019d490b4eSVivien Didelot 		if (ds->ops->get_strings)
802afdcf151SVivien Didelot 			ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
80391da11f8SLennert Buytenhek 	}
80491da11f8SLennert Buytenhek }
80591da11f8SLennert Buytenhek 
806badf3adaSFlorian Fainelli static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
807badf3adaSFlorian Fainelli 					   struct ethtool_stats *stats,
808badf3adaSFlorian Fainelli 					   uint64_t *data)
809badf3adaSFlorian Fainelli {
810badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8118b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
8128b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
813badf3adaSFlorian Fainelli 	int count = 0;
814badf3adaSFlorian Fainelli 
815badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
816badf3adaSFlorian Fainelli 		count = dst->master_ethtool_ops.get_sset_count(dev,
817badf3adaSFlorian Fainelli 							       ETH_SS_STATS);
818badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_ethtool_stats(dev, stats, data);
819badf3adaSFlorian Fainelli 	}
820badf3adaSFlorian Fainelli 
8219d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
8229d490b4eSVivien Didelot 		ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
823badf3adaSFlorian Fainelli }
824badf3adaSFlorian Fainelli 
825badf3adaSFlorian Fainelli static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
826badf3adaSFlorian Fainelli {
827badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8288b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
829badf3adaSFlorian Fainelli 	int count = 0;
830badf3adaSFlorian Fainelli 
831badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count)
832badf3adaSFlorian Fainelli 		count += dst->master_ethtool_ops.get_sset_count(dev, sset);
833badf3adaSFlorian Fainelli 
8349d490b4eSVivien Didelot 	if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
8359d490b4eSVivien Didelot 		count += ds->ops->get_sset_count(ds);
836badf3adaSFlorian Fainelli 
837badf3adaSFlorian Fainelli 	return count;
838badf3adaSFlorian Fainelli }
839badf3adaSFlorian Fainelli 
840badf3adaSFlorian Fainelli static void dsa_cpu_port_get_strings(struct net_device *dev,
841badf3adaSFlorian Fainelli 				     uint32_t stringset, uint8_t *data)
842badf3adaSFlorian Fainelli {
843badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = dev->dsa_ptr;
8448b0d3ea5SVivien Didelot 	struct dsa_switch *ds = dst->cpu_dp->ds;
8458b0d3ea5SVivien Didelot 	s8 cpu_port = dst->cpu_dp->index;
846badf3adaSFlorian Fainelli 	int len = ETH_GSTRING_LEN;
847badf3adaSFlorian Fainelli 	int mcount = 0, count;
848badf3adaSFlorian Fainelli 	unsigned int i;
849badf3adaSFlorian Fainelli 	uint8_t pfx[4];
850badf3adaSFlorian Fainelli 	uint8_t *ndata;
851badf3adaSFlorian Fainelli 
852badf3adaSFlorian Fainelli 	snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
853badf3adaSFlorian Fainelli 	/* We do not want to be NULL-terminated, since this is a prefix */
854badf3adaSFlorian Fainelli 	pfx[sizeof(pfx) - 1] = '_';
855badf3adaSFlorian Fainelli 
856badf3adaSFlorian Fainelli 	if (dst->master_ethtool_ops.get_sset_count) {
857badf3adaSFlorian Fainelli 		mcount = dst->master_ethtool_ops.get_sset_count(dev,
858badf3adaSFlorian Fainelli 								ETH_SS_STATS);
859badf3adaSFlorian Fainelli 		dst->master_ethtool_ops.get_strings(dev, stringset, data);
860badf3adaSFlorian Fainelli 	}
861badf3adaSFlorian Fainelli 
8629d490b4eSVivien Didelot 	if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
863badf3adaSFlorian Fainelli 		ndata = data + mcount * len;
864badf3adaSFlorian Fainelli 		/* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
865badf3adaSFlorian Fainelli 		 * the output after to prepend our CPU port prefix we
866badf3adaSFlorian Fainelli 		 * constructed earlier
867badf3adaSFlorian Fainelli 		 */
8689d490b4eSVivien Didelot 		ds->ops->get_strings(ds, cpu_port, ndata);
8699d490b4eSVivien Didelot 		count = ds->ops->get_sset_count(ds);
870badf3adaSFlorian Fainelli 		for (i = 0; i < count; i++) {
871badf3adaSFlorian Fainelli 			memmove(ndata + (i * len + sizeof(pfx)),
872badf3adaSFlorian Fainelli 				ndata + i * len, len - sizeof(pfx));
873badf3adaSFlorian Fainelli 			memcpy(ndata + i * len, pfx, sizeof(pfx));
874badf3adaSFlorian Fainelli 		}
875badf3adaSFlorian Fainelli 	}
876badf3adaSFlorian Fainelli }
877badf3adaSFlorian Fainelli 
87891da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
87991da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
88091da11f8SLennert Buytenhek 					uint64_t *data)
88191da11f8SLennert Buytenhek {
88291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
883afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
88491da11f8SLennert Buytenhek 
88546e7b8d8SVivien Didelot 	data[0] = dev->stats.tx_packets;
88646e7b8d8SVivien Didelot 	data[1] = dev->stats.tx_bytes;
88746e7b8d8SVivien Didelot 	data[2] = dev->stats.rx_packets;
88846e7b8d8SVivien Didelot 	data[3] = dev->stats.rx_bytes;
8899d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
890afdcf151SVivien Didelot 		ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
89191da11f8SLennert Buytenhek }
89291da11f8SLennert Buytenhek 
89391da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
89491da11f8SLennert Buytenhek {
89591da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
896afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
89791da11f8SLennert Buytenhek 
89891da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
89991da11f8SLennert Buytenhek 		int count;
90091da11f8SLennert Buytenhek 
90191da11f8SLennert Buytenhek 		count = 4;
9029d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
9039d490b4eSVivien Didelot 			count += ds->ops->get_sset_count(ds);
90491da11f8SLennert Buytenhek 
90591da11f8SLennert Buytenhek 		return count;
90691da11f8SLennert Buytenhek 	}
90791da11f8SLennert Buytenhek 
90891da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
90991da11f8SLennert Buytenhek }
91091da11f8SLennert Buytenhek 
91119e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
91219e57c4eSFlorian Fainelli {
91319e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
914afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
91519e57c4eSFlorian Fainelli 
9169d490b4eSVivien Didelot 	if (ds->ops->get_wol)
917afdcf151SVivien Didelot 		ds->ops->get_wol(ds, p->dp->index, w);
91819e57c4eSFlorian Fainelli }
91919e57c4eSFlorian Fainelli 
92019e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
92119e57c4eSFlorian Fainelli {
92219e57c4eSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
923afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
92419e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
92519e57c4eSFlorian Fainelli 
9269d490b4eSVivien Didelot 	if (ds->ops->set_wol)
927afdcf151SVivien Didelot 		ret = ds->ops->set_wol(ds, p->dp->index, w);
92819e57c4eSFlorian Fainelli 
92919e57c4eSFlorian Fainelli 	return ret;
93019e57c4eSFlorian Fainelli }
93119e57c4eSFlorian Fainelli 
9327905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
9337905288fSFlorian Fainelli {
9347905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
935afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9367905288fSFlorian Fainelli 	int ret;
9377905288fSFlorian Fainelli 
9389d490b4eSVivien Didelot 	if (!ds->ops->set_eee)
9397905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9407905288fSFlorian Fainelli 
941afdcf151SVivien Didelot 	ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
9427905288fSFlorian Fainelli 	if (ret)
9437905288fSFlorian Fainelli 		return ret;
9447905288fSFlorian Fainelli 
9457905288fSFlorian Fainelli 	if (p->phy)
9467905288fSFlorian Fainelli 		ret = phy_ethtool_set_eee(p->phy, e);
9477905288fSFlorian Fainelli 
9487905288fSFlorian Fainelli 	return ret;
9497905288fSFlorian Fainelli }
9507905288fSFlorian Fainelli 
9517905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
9527905288fSFlorian Fainelli {
9537905288fSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
954afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
9557905288fSFlorian Fainelli 	int ret;
9567905288fSFlorian Fainelli 
9579d490b4eSVivien Didelot 	if (!ds->ops->get_eee)
9587905288fSFlorian Fainelli 		return -EOPNOTSUPP;
9597905288fSFlorian Fainelli 
960afdcf151SVivien Didelot 	ret = ds->ops->get_eee(ds, p->dp->index, e);
9617905288fSFlorian Fainelli 	if (ret)
9627905288fSFlorian Fainelli 		return ret;
9637905288fSFlorian Fainelli 
9647905288fSFlorian Fainelli 	if (p->phy)
9657905288fSFlorian Fainelli 		ret = phy_ethtool_get_eee(p->phy, e);
9667905288fSFlorian Fainelli 
9677905288fSFlorian Fainelli 	return ret;
9687905288fSFlorian Fainelli }
9697905288fSFlorian Fainelli 
97004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
97104ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
97204ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
97304ff53f9SFlorian Fainelli {
97404ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
975afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
97604ff53f9SFlorian Fainelli 	struct net_device *master = ds->dst->master_netdev;
97704ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
97804ff53f9SFlorian Fainelli 	int err = 0;
97904ff53f9SFlorian Fainelli 
98004ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
98104ff53f9SFlorian Fainelli 	if (!netpoll)
98204ff53f9SFlorian Fainelli 		return -ENOMEM;
98304ff53f9SFlorian Fainelli 
98404ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
98504ff53f9SFlorian Fainelli 	if (err) {
98604ff53f9SFlorian Fainelli 		kfree(netpoll);
98704ff53f9SFlorian Fainelli 		goto out;
98804ff53f9SFlorian Fainelli 	}
98904ff53f9SFlorian Fainelli 
99004ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
99104ff53f9SFlorian Fainelli out:
99204ff53f9SFlorian Fainelli 	return err;
99304ff53f9SFlorian Fainelli }
99404ff53f9SFlorian Fainelli 
99504ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
99604ff53f9SFlorian Fainelli {
99704ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
99804ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
99904ff53f9SFlorian Fainelli 
100004ff53f9SFlorian Fainelli 	if (!netpoll)
100104ff53f9SFlorian Fainelli 		return;
100204ff53f9SFlorian Fainelli 
100304ff53f9SFlorian Fainelli 	p->netpoll = NULL;
100404ff53f9SFlorian Fainelli 
100504ff53f9SFlorian Fainelli 	__netpoll_free_async(netpoll);
100604ff53f9SFlorian Fainelli }
100704ff53f9SFlorian Fainelli 
100804ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
100904ff53f9SFlorian Fainelli {
101004ff53f9SFlorian Fainelli }
101104ff53f9SFlorian Fainelli #endif
101204ff53f9SFlorian Fainelli 
101344bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
101444bb765cSFlorian Fainelli 					char *name, size_t len)
101544bb765cSFlorian Fainelli {
101644bb765cSFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
101744bb765cSFlorian Fainelli 
1018afdcf151SVivien Didelot 	if (snprintf(name, len, "p%d", p->dp->index) >= len)
101944bb765cSFlorian Fainelli 		return -EINVAL;
10203a543ef4SFlorian Fainelli 
10213a543ef4SFlorian Fainelli 	return 0;
10223a543ef4SFlorian Fainelli }
10233a543ef4SFlorian Fainelli 
1024f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
1025f50f2127SFlorian Fainelli dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
1026f50f2127SFlorian Fainelli 			     unsigned long cookie)
1027f50f2127SFlorian Fainelli {
1028f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1029f50f2127SFlorian Fainelli 
1030f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1031f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1032f50f2127SFlorian Fainelli 			return mall_tc_entry;
1033f50f2127SFlorian Fainelli 
1034f50f2127SFlorian Fainelli 	return NULL;
1035f50f2127SFlorian Fainelli }
1036f50f2127SFlorian Fainelli 
1037f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
1038f50f2127SFlorian Fainelli 				      __be16 protocol,
1039f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
1040f50f2127SFlorian Fainelli 				      bool ingress)
1041f50f2127SFlorian Fainelli {
1042f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1043f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1044f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1045f50f2127SFlorian Fainelli 	struct net *net = dev_net(dev);
1046f50f2127SFlorian Fainelli 	struct dsa_slave_priv *to_p;
1047f50f2127SFlorian Fainelli 	struct net_device *to_dev;
1048f50f2127SFlorian Fainelli 	const struct tc_action *a;
1049f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
1050f50f2127SFlorian Fainelli 	LIST_HEAD(actions);
1051f50f2127SFlorian Fainelli 	int ifindex;
1052f50f2127SFlorian Fainelli 
1053f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
1054f50f2127SFlorian Fainelli 		return err;
1055f50f2127SFlorian Fainelli 
1056f50f2127SFlorian Fainelli 	if (!tc_single_action(cls->exts))
1057f50f2127SFlorian Fainelli 		return err;
1058f50f2127SFlorian Fainelli 
1059f50f2127SFlorian Fainelli 	tcf_exts_to_list(cls->exts, &actions);
1060f50f2127SFlorian Fainelli 	a = list_first_entry(&actions, struct tc_action, list);
1061f50f2127SFlorian Fainelli 
1062f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1063f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
1064f50f2127SFlorian Fainelli 
1065f50f2127SFlorian Fainelli 		ifindex = tcf_mirred_ifindex(a);
1066f50f2127SFlorian Fainelli 		to_dev = __dev_get_by_index(net, ifindex);
1067f50f2127SFlorian Fainelli 		if (!to_dev)
1068f50f2127SFlorian Fainelli 			return -EINVAL;
1069f50f2127SFlorian Fainelli 
1070f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
1071f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
1072f50f2127SFlorian Fainelli 
1073f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1074f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
1075f50f2127SFlorian Fainelli 			return -ENOMEM;
1076f50f2127SFlorian Fainelli 
1077f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
1078f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1079f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
1080f50f2127SFlorian Fainelli 
1081f50f2127SFlorian Fainelli 		to_p = netdev_priv(to_dev);
1082f50f2127SFlorian Fainelli 
1083f50f2127SFlorian Fainelli 		mirror->to_local_port = to_p->dp->index;
1084f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
1085f50f2127SFlorian Fainelli 
1086f50f2127SFlorian Fainelli 		err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
1087f50f2127SFlorian Fainelli 					       ingress);
1088f50f2127SFlorian Fainelli 		if (err) {
1089f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
1090f50f2127SFlorian Fainelli 			return err;
1091f50f2127SFlorian Fainelli 		}
1092f50f2127SFlorian Fainelli 
1093f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1094f50f2127SFlorian Fainelli 	}
1095f50f2127SFlorian Fainelli 
1096f50f2127SFlorian Fainelli 	return 0;
1097f50f2127SFlorian Fainelli }
1098f50f2127SFlorian Fainelli 
1099f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1100f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1101f50f2127SFlorian Fainelli {
1102f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1103f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1104f50f2127SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1105f50f2127SFlorian Fainelli 
1106f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
1107f50f2127SFlorian Fainelli 		return;
1108f50f2127SFlorian Fainelli 
1109f50f2127SFlorian Fainelli 	mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
1110f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1111f50f2127SFlorian Fainelli 		return;
1112f50f2127SFlorian Fainelli 
1113f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1114f50f2127SFlorian Fainelli 
1115f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1116f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
1117f50f2127SFlorian Fainelli 		ds->ops->port_mirror_del(ds, p->dp->index,
1118f50f2127SFlorian Fainelli 					 &mall_tc_entry->mirror);
1119f50f2127SFlorian Fainelli 		break;
1120f50f2127SFlorian Fainelli 	default:
1121f50f2127SFlorian Fainelli 		WARN_ON(1);
1122f50f2127SFlorian Fainelli 	}
1123f50f2127SFlorian Fainelli 
1124f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1125f50f2127SFlorian Fainelli }
1126f50f2127SFlorian Fainelli 
1127f50f2127SFlorian Fainelli static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
1128f50f2127SFlorian Fainelli 			      __be16 protocol, struct tc_to_netdev *tc)
1129f50f2127SFlorian Fainelli {
1130f50f2127SFlorian Fainelli 	bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1131f50f2127SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1132f50f2127SFlorian Fainelli 
1133f50f2127SFlorian Fainelli 	switch (tc->type) {
1134f50f2127SFlorian Fainelli 	case TC_SETUP_MATCHALL:
1135f50f2127SFlorian Fainelli 		switch (tc->cls_mall->command) {
1136f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_REPLACE:
1137f50f2127SFlorian Fainelli 			return dsa_slave_add_cls_matchall(dev, protocol,
1138f50f2127SFlorian Fainelli 							  tc->cls_mall,
1139f50f2127SFlorian Fainelli 							  ingress);
1140f50f2127SFlorian Fainelli 		case TC_CLSMATCHALL_DESTROY:
1141f50f2127SFlorian Fainelli 			dsa_slave_del_cls_matchall(dev, tc->cls_mall);
1142f50f2127SFlorian Fainelli 			return 0;
1143f50f2127SFlorian Fainelli 		}
1144f50f2127SFlorian Fainelli 	default:
1145f50f2127SFlorian Fainelli 		break;
1146f50f2127SFlorian Fainelli 	}
1147f50f2127SFlorian Fainelli 
1148f50f2127SFlorian Fainelli 	return ret;
1149f50f2127SFlorian Fainelli }
1150f50f2127SFlorian Fainelli 
1151af42192cSFlorian Fainelli void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
1152af42192cSFlorian Fainelli {
1153af42192cSFlorian Fainelli 	ops->get_sset_count = dsa_cpu_port_get_sset_count;
1154af42192cSFlorian Fainelli 	ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
1155af42192cSFlorian Fainelli 	ops->get_strings = dsa_cpu_port_get_strings;
1156af42192cSFlorian Fainelli }
1157af42192cSFlorian Fainelli 
1158bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1159bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1160bf9f2648SFlorian Fainelli {
1161bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1162bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1163bf9f2648SFlorian Fainelli 
1164bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1165bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1166bf9f2648SFlorian Fainelli 
1167bf9f2648SFlorian Fainelli 	return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
1168bf9f2648SFlorian Fainelli }
1169bf9f2648SFlorian Fainelli 
1170bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1171bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1172bf9f2648SFlorian Fainelli {
1173bf9f2648SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1174bf9f2648SFlorian Fainelli 	struct dsa_switch *ds = p->dp->ds;
1175bf9f2648SFlorian Fainelli 
1176bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1177bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1178bf9f2648SFlorian Fainelli 
1179bf9f2648SFlorian Fainelli 	return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
1180bf9f2648SFlorian Fainelli }
1181bf9f2648SFlorian Fainelli 
118291da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
118391da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
11843d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
11853d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
118691da11f8SLennert Buytenhek 	.nway_reset		= dsa_slave_nway_reset,
118791da11f8SLennert Buytenhek 	.get_link		= dsa_slave_get_link,
11886793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
11896793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
11906793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
119191da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
119291da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
119391da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
119419e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
119519e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
11967905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
11977905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1198bb10bb3eSPhilippe Reynes 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1199bb10bb3eSPhilippe Reynes 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1200bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1201bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
120291da11f8SLennert Buytenhek };
120391da11f8SLennert Buytenhek 
12043e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1205d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1206d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
12073e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1208d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1209d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1210d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
1211ba14d9ebSVivien Didelot 	.ndo_fdb_add		= switchdev_port_fdb_add,
1212ba14d9ebSVivien Didelot 	.ndo_fdb_del		= switchdev_port_fdb_del,
1213ba14d9ebSVivien Didelot 	.ndo_fdb_dump		= switchdev_port_fdb_dump,
1214d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1215abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
121604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
121704ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
121804ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
121904ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
122004ff53f9SFlorian Fainelli #endif
122111149536SVivien Didelot 	.ndo_bridge_getlink	= switchdev_port_bridge_getlink,
122211149536SVivien Didelot 	.ndo_bridge_setlink	= switchdev_port_bridge_setlink,
122311149536SVivien Didelot 	.ndo_bridge_dellink	= switchdev_port_bridge_dellink,
122444bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1225f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
122698237d43SScott Feldman };
122798237d43SScott Feldman 
12289d47c0a2SJiri Pirko static const struct switchdev_ops dsa_slave_switchdev_ops = {
1229f8e20a9fSScott Feldman 	.switchdev_port_attr_get	= dsa_slave_port_attr_get,
123035636062SScott Feldman 	.switchdev_port_attr_set	= dsa_slave_port_attr_set,
1231ba14d9ebSVivien Didelot 	.switchdev_port_obj_add		= dsa_slave_port_obj_add,
1232ba14d9ebSVivien Didelot 	.switchdev_port_obj_del		= dsa_slave_port_obj_del,
1233ba14d9ebSVivien Didelot 	.switchdev_port_obj_dump	= dsa_slave_port_obj_dump,
1234d442ad4aSStephen Hemminger };
123591da11f8SLennert Buytenhek 
1236f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1237f37db85dSFlorian Fainelli 	.name	= "dsa",
1238f37db85dSFlorian Fainelli };
1239f37db85dSFlorian Fainelli 
12400d8bcdd3SFlorian Fainelli static void dsa_slave_adjust_link(struct net_device *dev)
12410d8bcdd3SFlorian Fainelli {
12420d8bcdd3SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1243afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
12440d8bcdd3SFlorian Fainelli 	unsigned int status_changed = 0;
12450d8bcdd3SFlorian Fainelli 
12460d8bcdd3SFlorian Fainelli 	if (p->old_link != p->phy->link) {
12470d8bcdd3SFlorian Fainelli 		status_changed = 1;
12480d8bcdd3SFlorian Fainelli 		p->old_link = p->phy->link;
12490d8bcdd3SFlorian Fainelli 	}
12500d8bcdd3SFlorian Fainelli 
12510d8bcdd3SFlorian Fainelli 	if (p->old_duplex != p->phy->duplex) {
12520d8bcdd3SFlorian Fainelli 		status_changed = 1;
12530d8bcdd3SFlorian Fainelli 		p->old_duplex = p->phy->duplex;
12540d8bcdd3SFlorian Fainelli 	}
12550d8bcdd3SFlorian Fainelli 
12560d8bcdd3SFlorian Fainelli 	if (p->old_pause != p->phy->pause) {
12570d8bcdd3SFlorian Fainelli 		status_changed = 1;
12580d8bcdd3SFlorian Fainelli 		p->old_pause = p->phy->pause;
12590d8bcdd3SFlorian Fainelli 	}
12600d8bcdd3SFlorian Fainelli 
12619d490b4eSVivien Didelot 	if (ds->ops->adjust_link && status_changed)
1262afdcf151SVivien Didelot 		ds->ops->adjust_link(ds, p->dp->index, p->phy);
1263ec9436baSFlorian Fainelli 
12640d8bcdd3SFlorian Fainelli 	if (status_changed)
12650d8bcdd3SFlorian Fainelli 		phy_print_status(p->phy);
12660d8bcdd3SFlorian Fainelli }
12670d8bcdd3SFlorian Fainelli 
1268ce31b31cSFlorian Fainelli static int dsa_slave_fixed_link_update(struct net_device *dev,
1269ce31b31cSFlorian Fainelli 				       struct fixed_phy_status *status)
1270ce31b31cSFlorian Fainelli {
1271b71be352SAndrew Lunn 	struct dsa_slave_priv *p;
1272b71be352SAndrew Lunn 	struct dsa_switch *ds;
1273ce31b31cSFlorian Fainelli 
1274b71be352SAndrew Lunn 	if (dev) {
1275b71be352SAndrew Lunn 		p = netdev_priv(dev);
1276afdcf151SVivien Didelot 		ds = p->dp->ds;
12779d490b4eSVivien Didelot 		if (ds->ops->fixed_link_update)
1278afdcf151SVivien Didelot 			ds->ops->fixed_link_update(ds, p->dp->index, status);
1279b71be352SAndrew Lunn 	}
1280ce31b31cSFlorian Fainelli 
1281ce31b31cSFlorian Fainelli 	return 0;
1282ce31b31cSFlorian Fainelli }
1283ce31b31cSFlorian Fainelli 
128491da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1285c305c165SFlorian Fainelli static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
1286cd28a1a9SFlorian Fainelli 				 struct net_device *slave_dev,
1287cd28a1a9SFlorian Fainelli 				 int addr)
1288c305c165SFlorian Fainelli {
1289afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
1290c305c165SFlorian Fainelli 
12917f854420SAndrew Lunn 	p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
1292d25b8e74SRussell King 	if (!p->phy) {
1293d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1294c305c165SFlorian Fainelli 		return -ENODEV;
1295d25b8e74SRussell King 	}
1296c305c165SFlorian Fainelli 
1297c305c165SFlorian Fainelli 	/* Use already configured phy mode */
1298211c504aSFlorian Fainelli 	if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1299c305c165SFlorian Fainelli 		p->phy_interface = p->phy->interface;
13004078b76cSFlorian Fainelli 	return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
1301c305c165SFlorian Fainelli 				  p->phy_interface);
1302c305c165SFlorian Fainelli }
1303c305c165SFlorian Fainelli 
13049697f1cdSFlorian Fainelli static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
13050d8bcdd3SFlorian Fainelli 				struct net_device *slave_dev)
13060d8bcdd3SFlorian Fainelli {
1307afdcf151SVivien Didelot 	struct dsa_switch *ds = p->dp->ds;
13080d8bcdd3SFlorian Fainelli 	struct device_node *phy_dn, *port_dn;
1309ce31b31cSFlorian Fainelli 	bool phy_is_fixed = false;
13106819563eSFlorian Fainelli 	u32 phy_flags = 0;
131119334920SGuenter Roeck 	int mode, ret;
13120d8bcdd3SFlorian Fainelli 
1313afdcf151SVivien Didelot 	port_dn = p->dp->dn;
131419334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
131519334920SGuenter Roeck 	if (mode < 0)
131619334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
131719334920SGuenter Roeck 	p->phy_interface = mode;
13180d8bcdd3SFlorian Fainelli 
13190d8bcdd3SFlorian Fainelli 	phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
13200d8f3c67SJohan Hovold 	if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
13210d8bcdd3SFlorian Fainelli 		/* In the case of a fixed PHY, the DT node associated
13220d8bcdd3SFlorian Fainelli 		 * to the fixed PHY is the Port DT node
13230d8bcdd3SFlorian Fainelli 		 */
13240d8bcdd3SFlorian Fainelli 		ret = of_phy_register_fixed_link(port_dn);
13250d8bcdd3SFlorian Fainelli 		if (ret) {
1326d25b8e74SRussell King 			netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
13279697f1cdSFlorian Fainelli 			return ret;
13280d8bcdd3SFlorian Fainelli 		}
1329ce31b31cSFlorian Fainelli 		phy_is_fixed = true;
13300d8f3c67SJohan Hovold 		phy_dn = of_node_get(port_dn);
13310d8bcdd3SFlorian Fainelli 	}
13320d8bcdd3SFlorian Fainelli 
13339d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1334afdcf151SVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
13356819563eSFlorian Fainelli 
1336cd28a1a9SFlorian Fainelli 	if (phy_dn) {
1337d25b8e74SRussell King 		int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
1338d25b8e74SRussell King 
1339cd28a1a9SFlorian Fainelli 		/* If this PHY address is part of phys_mii_mask, which means
1340cd28a1a9SFlorian Fainelli 		 * that we need to divert reads and writes to/from it, then we
1341cd28a1a9SFlorian Fainelli 		 * want to bind this device using the slave MII bus created by
1342cd28a1a9SFlorian Fainelli 		 * DSA to make that happen.
1343cd28a1a9SFlorian Fainelli 		 */
1344d25b8e74SRussell King 		if (!phy_is_fixed && phy_id >= 0 &&
1345d25b8e74SRussell King 		    (ds->phys_mii_mask & (1 << phy_id))) {
1346d25b8e74SRussell King 			ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
1347d25b8e74SRussell King 			if (ret) {
1348d25b8e74SRussell King 				netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
13490d8f3c67SJohan Hovold 				of_node_put(phy_dn);
1350cd28a1a9SFlorian Fainelli 				return ret;
1351d25b8e74SRussell King 			}
1352cd28a1a9SFlorian Fainelli 		} else {
13530d8bcdd3SFlorian Fainelli 			p->phy = of_phy_connect(slave_dev, phy_dn,
1354cd28a1a9SFlorian Fainelli 						dsa_slave_adjust_link,
1355cd28a1a9SFlorian Fainelli 						phy_flags,
13560d8bcdd3SFlorian Fainelli 						p->phy_interface);
1357cd28a1a9SFlorian Fainelli 		}
13580d8f3c67SJohan Hovold 
13590d8f3c67SJohan Hovold 		of_node_put(phy_dn);
1360cd28a1a9SFlorian Fainelli 	}
13610d8bcdd3SFlorian Fainelli 
1362ce31b31cSFlorian Fainelli 	if (p->phy && phy_is_fixed)
1363ce31b31cSFlorian Fainelli 		fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
1364ce31b31cSFlorian Fainelli 
13650d8bcdd3SFlorian Fainelli 	/* We could not connect to a designated PHY, so use the switch internal
13660d8bcdd3SFlorian Fainelli 	 * MDIO bus instead
13670d8bcdd3SFlorian Fainelli 	 */
1368b31f65fbSAndrew Lunn 	if (!p->phy) {
1369afdcf151SVivien Didelot 		ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
1370d25b8e74SRussell King 		if (ret) {
1371afdcf151SVivien Didelot 			netdev_err(slave_dev, "failed to connect to port %d: %d\n",
1372afdcf151SVivien Didelot 				   p->dp->index, ret);
1373881eadabSJohan Hovold 			if (phy_is_fixed)
1374881eadabSJohan Hovold 				of_phy_deregister_fixed_link(port_dn);
1375c305c165SFlorian Fainelli 			return ret;
1376d25b8e74SRussell King 		}
13770d8bcdd3SFlorian Fainelli 	}
13789697f1cdSFlorian Fainelli 
13792220943aSAndrew Lunn 	phy_attached_info(p->phy);
13802220943aSAndrew Lunn 
13819697f1cdSFlorian Fainelli 	return 0;
1382b31f65fbSAndrew Lunn }
13830d8bcdd3SFlorian Fainelli 
1384448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1385448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1386448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1387448b4482SAndrew Lunn 					    void *_unused)
1388448b4482SAndrew Lunn {
1389448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1390448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1391448b4482SAndrew Lunn }
1392448b4482SAndrew Lunn 
139324462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
139424462549SFlorian Fainelli {
139524462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
139624462549SFlorian Fainelli 
1397f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1398f154be24SFlorian Fainelli 
139924462549SFlorian Fainelli 	if (p->phy) {
140024462549SFlorian Fainelli 		phy_stop(p->phy);
140124462549SFlorian Fainelli 		p->old_pause = -1;
140224462549SFlorian Fainelli 		p->old_link = -1;
140324462549SFlorian Fainelli 		p->old_duplex = -1;
140424462549SFlorian Fainelli 		phy_suspend(p->phy);
140524462549SFlorian Fainelli 	}
140624462549SFlorian Fainelli 
140724462549SFlorian Fainelli 	return 0;
140824462549SFlorian Fainelli }
140924462549SFlorian Fainelli 
141024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
141124462549SFlorian Fainelli {
141224462549SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
141324462549SFlorian Fainelli 
141424462549SFlorian Fainelli 	netif_device_attach(slave_dev);
141524462549SFlorian Fainelli 
141624462549SFlorian Fainelli 	if (p->phy) {
141724462549SFlorian Fainelli 		phy_resume(p->phy);
141824462549SFlorian Fainelli 		phy_start(p->phy);
141924462549SFlorian Fainelli 	}
142024462549SFlorian Fainelli 
142124462549SFlorian Fainelli 	return 0;
142224462549SFlorian Fainelli }
142324462549SFlorian Fainelli 
1424d87d6f44SGuenter Roeck int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
142583c0afaeSAndrew Lunn 		     int port, const char *name)
142691da11f8SLennert Buytenhek {
1427badf3adaSFlorian Fainelli 	struct dsa_switch_tree *dst = ds->dst;
142883c0afaeSAndrew Lunn 	struct net_device *master;
142991da11f8SLennert Buytenhek 	struct net_device *slave_dev;
143091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
143191da11f8SLennert Buytenhek 	int ret;
143291da11f8SLennert Buytenhek 
143383c0afaeSAndrew Lunn 	master = ds->dst->master_netdev;
143483c0afaeSAndrew Lunn 	if (ds->master_netdev)
143583c0afaeSAndrew Lunn 		master = ds->master_netdev;
143683c0afaeSAndrew Lunn 
1437c835a677STom Gundersen 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
1438c835a677STom Gundersen 				 NET_NAME_UNKNOWN, ether_setup);
143991da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1440d87d6f44SGuenter Roeck 		return -ENOMEM;
144191da11f8SLennert Buytenhek 
1442f50f2127SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1443f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14447ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14452fcc8005SBjørn Mork 	eth_hw_addr_inherit(slave_dev, master);
14460a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14473e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14489d47c0a2SJiri Pirko 	slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
14498b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14508b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1451f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1452d442ad4aSStephen Hemminger 
1453448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1454448b4482SAndrew Lunn 				 NULL);
1455448b4482SAndrew Lunn 
145691da11f8SLennert Buytenhek 	SET_NETDEV_DEV(slave_dev, parent);
1457189b0d93SAndrew Lunn 	slave_dev->dev.of_node = ds->ports[port].dn;
145891da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
145991da11f8SLennert Buytenhek 
146091da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
1461afdcf151SVivien Didelot 	p->dp = &ds->ports[port];
1462f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
146339a7f2a4SAndrew Lunn 	p->xmit = dst->tag_ops->xmit;
14645075314eSAlexander Duyck 
14650d8bcdd3SFlorian Fainelli 	p->old_pause = -1;
14660d8bcdd3SFlorian Fainelli 	p->old_link = -1;
14670d8bcdd3SFlorian Fainelli 	p->old_duplex = -1;
14680d8bcdd3SFlorian Fainelli 
1469c8b09808SAndrew Lunn 	ds->ports[port].netdev = slave_dev;
147091da11f8SLennert Buytenhek 	ret = register_netdev(slave_dev);
147191da11f8SLennert Buytenhek 	if (ret) {
1472a2ae6007SJoe Perches 		netdev_err(master, "error %d registering interface %s\n",
1473a2ae6007SJoe Perches 			   ret, slave_dev->name);
1474c8b09808SAndrew Lunn 		ds->ports[port].netdev = NULL;
147591da11f8SLennert Buytenhek 		free_netdev(slave_dev);
1476d87d6f44SGuenter Roeck 		return ret;
147791da11f8SLennert Buytenhek 	}
147891da11f8SLennert Buytenhek 
147991da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
148091da11f8SLennert Buytenhek 
14810071f56eSAndrew Lunn 	ret = dsa_slave_phy_setup(p, slave_dev);
14820071f56eSAndrew Lunn 	if (ret) {
14830071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
148473dcb556SFlorian Fainelli 		unregister_netdev(slave_dev);
14850071f56eSAndrew Lunn 		free_netdev(slave_dev);
14860071f56eSAndrew Lunn 		return ret;
14870071f56eSAndrew Lunn 	}
14880071f56eSAndrew Lunn 
1489d87d6f44SGuenter Roeck 	return 0;
149091da11f8SLennert Buytenhek }
1491b73adef6SFlorian Fainelli 
1492cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1493cda5c15bSNeil Armstrong {
1494cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1495881eadabSJohan Hovold 	struct device_node *port_dn;
1496881eadabSJohan Hovold 
1497afdcf151SVivien Didelot 	port_dn = p->dp->dn;
1498cda5c15bSNeil Armstrong 
1499cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1500881eadabSJohan Hovold 	if (p->phy) {
1501cda5c15bSNeil Armstrong 		phy_disconnect(p->phy);
1502881eadabSJohan Hovold 
1503881eadabSJohan Hovold 		if (of_phy_is_fixed_link(port_dn))
1504881eadabSJohan Hovold 			of_phy_deregister_fixed_link(port_dn);
1505881eadabSJohan Hovold 	}
1506cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1507cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1508cda5c15bSNeil Armstrong }
1509cda5c15bSNeil Armstrong 
1510b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1511b73adef6SFlorian Fainelli {
1512b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1513b73adef6SFlorian Fainelli }
1514b73adef6SFlorian Fainelli 
15158e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
15168e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1517b73adef6SFlorian Fainelli {
151817d7802bSVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
151917d7802bSVivien Didelot 	struct dsa_port *dp = p->dp;
15208e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1521b73adef6SFlorian Fainelli 
15228e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
15238e92ab3aSVivien Didelot 		if (info->linking) {
152417d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
15258e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
15268e92ab3aSVivien Didelot 		} else {
152717d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
15288e92ab3aSVivien Didelot 			err = NOTIFY_OK;
15298e92ab3aSVivien Didelot 		}
15306debb68aSVivien Didelot 	}
1531b73adef6SFlorian Fainelli 
15328e92ab3aSVivien Didelot 	return err;
1533b73adef6SFlorian Fainelli }
1534b73adef6SFlorian Fainelli 
153588e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1536b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1537b73adef6SFlorian Fainelli {
15386debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1539b73adef6SFlorian Fainelli 
15408e92ab3aSVivien Didelot 	if (dev->netdev_ops != &dsa_slave_netdev_ops)
15418e92ab3aSVivien Didelot 		return NOTIFY_DONE;
15428e92ab3aSVivien Didelot 
15438e92ab3aSVivien Didelot 	if (event == NETDEV_CHANGEUPPER)
15448e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1545b73adef6SFlorian Fainelli 
1546b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1547b73adef6SFlorian Fainelli }
154888e4f0caSVivien Didelot 
154988e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
155088e4f0caSVivien Didelot 	.notifier_call	= dsa_slave_netdevice_event,
155188e4f0caSVivien Didelot };
155288e4f0caSVivien Didelot 
155388e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
155488e4f0caSVivien Didelot {
155588e4f0caSVivien Didelot 	return register_netdevice_notifier(&dsa_slave_nb);
155688e4f0caSVivien Didelot }
155788e4f0caSVivien Didelot 
155888e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
155988e4f0caSVivien Didelot {
156088e4f0caSVivien Didelot 	int err;
156188e4f0caSVivien Didelot 
156288e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
156388e4f0caSVivien Didelot 	if (err)
156488e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
156588e4f0caSVivien Didelot }
1566