xref: /openbmc/linux/net/dsa/slave.c (revision e13c2075280e5b25118d3330752b47f919d6545e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
291da11f8SLennert Buytenhek /*
391da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
4e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
591da11f8SLennert Buytenhek  */
691da11f8SLennert Buytenhek 
791da11f8SLennert Buytenhek #include <linux/list.h>
8df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
9b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1091da11f8SLennert Buytenhek #include <linux/phy.h>
11a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
12aab9c406SFlorian Fainelli #include <linux/phylink.h>
130d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
140d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
157f854420SAndrew Lunn #include <linux/mdio.h>
16b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
17f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
18f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
19b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2004ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2190af1059SBrandon Streiff #include <linux/ptp_classify.h>
22ea5dd34bSVivien Didelot 
2391da11f8SLennert Buytenhek #include "dsa_priv.h"
2491da11f8SLennert Buytenhek 
2591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2791da11f8SLennert Buytenhek {
2891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2991da11f8SLennert Buytenhek 
300d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
319d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3291da11f8SLennert Buytenhek 
3391da11f8SLennert Buytenhek 	return 0xffff;
3491da11f8SLennert Buytenhek }
3591da11f8SLennert Buytenhek 
3691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3791da11f8SLennert Buytenhek {
3891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3991da11f8SLennert Buytenhek 
400d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
419d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4291da11f8SLennert Buytenhek 
4391da11f8SLennert Buytenhek 	return 0;
4491da11f8SLennert Buytenhek }
4591da11f8SLennert Buytenhek 
4691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4791da11f8SLennert Buytenhek {
4891da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
520b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5349463b7fSVivien Didelot 		 ds->dst->index, ds->index);
54c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
5524df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5691da11f8SLennert Buytenhek }
5791da11f8SLennert Buytenhek 
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek /* slave device handling ****************************************************/
60abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
61c0840801SLennert Buytenhek {
62d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
63c0840801SLennert Buytenhek }
64c0840801SLennert Buytenhek 
6591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6691da11f8SLennert Buytenhek {
67d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
68d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
69df02c6ffSLennert Buytenhek 	int err;
70df02c6ffSLennert Buytenhek 
71df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
72df02c6ffSLennert Buytenhek 		return -ENETDOWN;
73df02c6ffSLennert Buytenhek 
748feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
75a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
76df02c6ffSLennert Buytenhek 		if (err < 0)
77df02c6ffSLennert Buytenhek 			goto out;
78df02c6ffSLennert Buytenhek 	}
79df02c6ffSLennert Buytenhek 
80df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
81df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
82df02c6ffSLennert Buytenhek 		if (err < 0)
83df02c6ffSLennert Buytenhek 			goto del_unicast;
84df02c6ffSLennert Buytenhek 	}
85df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
86df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
87df02c6ffSLennert Buytenhek 		if (err < 0)
88df02c6ffSLennert Buytenhek 			goto clear_allmulti;
89df02c6ffSLennert Buytenhek 	}
90df02c6ffSLennert Buytenhek 
918640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
92b2f2af21SFlorian Fainelli 	if (err)
93b2f2af21SFlorian Fainelli 		goto clear_promisc;
94b73adef6SFlorian Fainelli 
9591da11f8SLennert Buytenhek 	return 0;
96df02c6ffSLennert Buytenhek 
97b2f2af21SFlorian Fainelli clear_promisc:
98b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
994fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
100df02c6ffSLennert Buytenhek clear_allmulti:
101df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
102df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
103df02c6ffSLennert Buytenhek del_unicast:
1048feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
105a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
106df02c6ffSLennert Buytenhek out:
107df02c6ffSLennert Buytenhek 	return err;
10891da11f8SLennert Buytenhek }
10991da11f8SLennert Buytenhek 
11091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11191da11f8SLennert Buytenhek {
112d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
113d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
114df02c6ffSLennert Buytenhek 
1158640f8dcSRussell King 	dsa_port_disable_rt(dp);
1166457edfeSVivien Didelot 
117df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
118a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
119df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
120df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
121df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
122df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
123df02c6ffSLennert Buytenhek 
1248feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
125a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
126df02c6ffSLennert Buytenhek 
12791da11f8SLennert Buytenhek 	return 0;
12891da11f8SLennert Buytenhek }
12991da11f8SLennert Buytenhek 
13091da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
13191da11f8SLennert Buytenhek {
132d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
13317ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
13491da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
13517ab4f61SRundong Ge 			dev_set_allmulti(master,
13617ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
13791da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
13817ab4f61SRundong Ge 			dev_set_promiscuity(master,
13917ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
14017ab4f61SRundong Ge 	}
14191da11f8SLennert Buytenhek }
14291da11f8SLennert Buytenhek 
14391da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
14491da11f8SLennert Buytenhek {
145d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
14691da11f8SLennert Buytenhek 
14791da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
148a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
14991da11f8SLennert Buytenhek }
15091da11f8SLennert Buytenhek 
151df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
15291da11f8SLennert Buytenhek {
153d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
154df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
155df02c6ffSLennert Buytenhek 	int err;
156df02c6ffSLennert Buytenhek 
157df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
158df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
159df02c6ffSLennert Buytenhek 
160df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
161df02c6ffSLennert Buytenhek 		goto out;
162df02c6ffSLennert Buytenhek 
1638feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
164a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
165df02c6ffSLennert Buytenhek 		if (err < 0)
166df02c6ffSLennert Buytenhek 			return err;
167df02c6ffSLennert Buytenhek 	}
168df02c6ffSLennert Buytenhek 
1698feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
170a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
171df02c6ffSLennert Buytenhek 
172df02c6ffSLennert Buytenhek out:
173d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
17491da11f8SLennert Buytenhek 
17591da11f8SLennert Buytenhek 	return 0;
17691da11f8SLennert Buytenhek }
17791da11f8SLennert Buytenhek 
1782bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1792bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1802bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1812bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1822bedde1aSArkadi Sharshevsky 	int idx;
1832bedde1aSArkadi Sharshevsky };
1842bedde1aSArkadi Sharshevsky 
1852bedde1aSArkadi Sharshevsky static int
1862bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1872bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1882bedde1aSArkadi Sharshevsky {
1892bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1902bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1912bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1922bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1932bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1942bedde1aSArkadi Sharshevsky 
1952bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
1962bedde1aSArkadi Sharshevsky 		goto skip;
1972bedde1aSArkadi Sharshevsky 
1982bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1992bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2002bedde1aSArkadi Sharshevsky 	if (!nlh)
2012bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2022bedde1aSArkadi Sharshevsky 
2032bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2042bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2052bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2062bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2072bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2112bedde1aSArkadi Sharshevsky 
2122bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2132bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2142bedde1aSArkadi Sharshevsky 
2152bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2162bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2172bedde1aSArkadi Sharshevsky 
2182bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2192bedde1aSArkadi Sharshevsky 
2202bedde1aSArkadi Sharshevsky skip:
2212bedde1aSArkadi Sharshevsky 	dump->idx++;
2222bedde1aSArkadi Sharshevsky 	return 0;
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky nla_put_failure:
2252bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2262bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2272bedde1aSArkadi Sharshevsky }
2282bedde1aSArkadi Sharshevsky 
2292bedde1aSArkadi Sharshevsky static int
2302bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2312bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2322bedde1aSArkadi Sharshevsky 		   int *idx)
2332bedde1aSArkadi Sharshevsky {
234d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2352bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2362bedde1aSArkadi Sharshevsky 		.dev = dev,
2372bedde1aSArkadi Sharshevsky 		.skb = skb,
2382bedde1aSArkadi Sharshevsky 		.cb = cb,
2392bedde1aSArkadi Sharshevsky 		.idx = *idx,
2402bedde1aSArkadi Sharshevsky 	};
2412bedde1aSArkadi Sharshevsky 	int err;
2422bedde1aSArkadi Sharshevsky 
243de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2442bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
245de40fc5dSVivien Didelot 
2462bedde1aSArkadi Sharshevsky 	return err;
2472bedde1aSArkadi Sharshevsky }
2482bedde1aSArkadi Sharshevsky 
24991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25091da11f8SLennert Buytenhek {
2510336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2520336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2530336369dSBrandon Streiff 	int port = p->dp->index;
2540336369dSBrandon Streiff 
2550336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2560336369dSBrandon Streiff 	switch (cmd) {
2570336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2580336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2590336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2600336369dSBrandon Streiff 		break;
2610336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	}
2660336369dSBrandon Streiff 
267aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
26891da11f8SLennert Buytenhek }
26991da11f8SLennert Buytenhek 
27035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
271f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
2727ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
27335636062SScott Feldman {
274d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
275b8d866acSVivien Didelot 	int ret;
27635636062SScott Feldman 
27735636062SScott Feldman 	switch (attr->id) {
2781f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
279fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
28035636062SScott Feldman 		break;
281fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
282c02c4175SVivien Didelot 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
283c02c4175SVivien Didelot 					      trans);
284fb2dabadSVivien Didelot 		break;
28534a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
286072bb190SVivien Didelot 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
28734a79f63SVivien Didelot 		break;
288ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
289ea87005aSFlorian Fainelli 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
290ea87005aSFlorian Fainelli 						trans);
291ea87005aSFlorian Fainelli 		break;
29257652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
29357652796SRussell King 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
29457652796SRussell King 		break;
29508cc83ccSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
29608cc83ccSVivien Didelot 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
29708cc83ccSVivien Didelot 		break;
29835636062SScott Feldman 	default:
29935636062SScott Feldman 		ret = -EOPNOTSUPP;
30035636062SScott Feldman 		break;
30135636062SScott Feldman 	}
30235636062SScott Feldman 
30335636062SScott Feldman 	return ret;
30435636062SScott Feldman }
30535636062SScott Feldman 
306bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
307bdcff080SVivien Didelot 			      const struct switchdev_obj *obj,
308bdcff080SVivien Didelot 			      struct switchdev_trans *trans)
309bdcff080SVivien Didelot {
310bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
311bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
312bdcff080SVivien Didelot 	int err;
313bdcff080SVivien Didelot 
314bdcff080SVivien Didelot 	if (obj->orig_dev != dev)
315bdcff080SVivien Didelot 		return -EOPNOTSUPP;
316bdcff080SVivien Didelot 
317c5335d73SVivien Didelot 	if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
318c5335d73SVivien Didelot 		return 0;
319c5335d73SVivien Didelot 
320bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
321bdcff080SVivien Didelot 
322bdcff080SVivien Didelot 	err = dsa_port_vlan_add(dp, &vlan, trans);
323bdcff080SVivien Didelot 	if (err)
324bdcff080SVivien Didelot 		return err;
325bdcff080SVivien Didelot 
326b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
327b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
328b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
329b9499904SVivien Didelot 	 */
330b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
331b9499904SVivien Didelot 
3327e1741b4SVivien Didelot 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
3337e1741b4SVivien Didelot 	if (err)
3347e1741b4SVivien Didelot 		return err;
3357e1741b4SVivien Didelot 
336bdcff080SVivien Didelot 	return 0;
337bdcff080SVivien Didelot }
338bdcff080SVivien Didelot 
339ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
340648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
34179b139f4SVivien Didelot 				  struct switchdev_trans *trans,
34279b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
343ba14d9ebSVivien Didelot {
344d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
345ba14d9ebSVivien Didelot 	int err;
346ba14d9ebSVivien Didelot 
347ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
348ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
349ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
350ba14d9ebSVivien Didelot 	 */
351ba14d9ebSVivien Didelot 
3529e8f4a54SJiri Pirko 	switch (obj->id) {
3538df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
35479b139f4SVivien Didelot 		if (obj->orig_dev != dev)
35579b139f4SVivien Didelot 			return -EOPNOTSUPP;
356bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
3578df30255SVivien Didelot 		break;
3585f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3595f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
3605f4dbc50SAndrew Lunn 		 * but on the CPU port.
3615f4dbc50SAndrew Lunn 		 */
3625f4dbc50SAndrew Lunn 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
3635f4dbc50SAndrew Lunn 				       trans);
3645f4dbc50SAndrew Lunn 		break;
36557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
366bdcff080SVivien Didelot 		err = dsa_slave_vlan_add(dev, obj, trans);
36711149536SVivien Didelot 		break;
368ba14d9ebSVivien Didelot 	default:
369ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
370ba14d9ebSVivien Didelot 		break;
371ba14d9ebSVivien Didelot 	}
372ba14d9ebSVivien Didelot 
373ba14d9ebSVivien Didelot 	return err;
374ba14d9ebSVivien Didelot }
375ba14d9ebSVivien Didelot 
376bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
377bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
378bdcff080SVivien Didelot {
379bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
380bdcff080SVivien Didelot 
381bdcff080SVivien Didelot 	if (obj->orig_dev != dev)
382bdcff080SVivien Didelot 		return -EOPNOTSUPP;
383bdcff080SVivien Didelot 
384c5335d73SVivien Didelot 	if (dp->bridge_dev && !br_vlan_enabled(dp->bridge_dev))
385c5335d73SVivien Didelot 		return 0;
386c5335d73SVivien Didelot 
3877e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
3887e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
3897e1741b4SVivien Didelot 	 */
390bdcff080SVivien Didelot 	return dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
391bdcff080SVivien Didelot }
392bdcff080SVivien Didelot 
393ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
394648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
395ba14d9ebSVivien Didelot {
396d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
397ba14d9ebSVivien Didelot 	int err;
398ba14d9ebSVivien Didelot 
3999e8f4a54SJiri Pirko 	switch (obj->id) {
4008df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
40179b139f4SVivien Didelot 		if (obj->orig_dev != dev)
40279b139f4SVivien Didelot 			return -EOPNOTSUPP;
403bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4048df30255SVivien Didelot 		break;
4055f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
4065f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4075f4dbc50SAndrew Lunn 		 * but on the CPU port.
4085f4dbc50SAndrew Lunn 		 */
4095f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4105f4dbc50SAndrew Lunn 		break;
41157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
412bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
41311149536SVivien Didelot 		break;
414ba14d9ebSVivien Didelot 	default:
415ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
416ba14d9ebSVivien Didelot 		break;
417ba14d9ebSVivien Didelot 	}
418ba14d9ebSVivien Didelot 
419ba14d9ebSVivien Didelot 	return err;
420ba14d9ebSVivien Didelot }
421ba14d9ebSVivien Didelot 
422929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
423929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
424b73adef6SFlorian Fainelli {
425d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
426d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
427a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
428b73adef6SFlorian Fainelli 
42915b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
43015b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
43115b04aceSJiri Pirko 	 * should be removed with legacy support.
43215b04aceSJiri Pirko 	 */
43315b04aceSJiri Pirko 	if (dp->ds->devlink)
43415b04aceSJiri Pirko 		return -EOPNOTSUPP;
43515b04aceSJiri Pirko 
436929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
437929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
438929d6c14SFlorian Fainelli 
439929d6c14SFlorian Fainelli 	return 0;
440929d6c14SFlorian Fainelli }
441929d6c14SFlorian Fainelli 
4424fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
44304ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
44404ff53f9SFlorian Fainelli {
44504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
4464fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4474fa7b718SVivien Didelot 
44804ff53f9SFlorian Fainelli 	if (p->netpoll)
44904ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
45004ff53f9SFlorian Fainelli #else
45104ff53f9SFlorian Fainelli 	BUG();
45204ff53f9SFlorian Fainelli #endif
45304ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
45404ff53f9SFlorian Fainelli }
45504ff53f9SFlorian Fainelli 
45690af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
45790af1059SBrandon Streiff 				 struct sk_buff *skb)
45890af1059SBrandon Streiff {
45990af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
46090af1059SBrandon Streiff 	struct sk_buff *clone;
46190af1059SBrandon Streiff 	unsigned int type;
46290af1059SBrandon Streiff 
46390af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
46490af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
46590af1059SBrandon Streiff 		return;
46690af1059SBrandon Streiff 
46790af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
46890af1059SBrandon Streiff 		return;
46990af1059SBrandon Streiff 
47090af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
47190af1059SBrandon Streiff 	if (!clone)
47290af1059SBrandon Streiff 		return;
47390af1059SBrandon Streiff 
474146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = clone;
475146d442cSVladimir Oltean 
47690af1059SBrandon Streiff 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
47790af1059SBrandon Streiff 		return;
47890af1059SBrandon Streiff 
47990af1059SBrandon Streiff 	kfree_skb(clone);
48090af1059SBrandon Streiff }
48190af1059SBrandon Streiff 
48297a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
48397a69a0dSVladimir Oltean {
48497a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
48597a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
48697a69a0dSVladimir Oltean 	 */
48797a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
48897a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
48997a69a0dSVladimir Oltean 
49097a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
49197a69a0dSVladimir Oltean 	 * do not modify its EtherType
49297a69a0dSVladimir Oltean 	 */
49397a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
49497a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
49597a69a0dSVladimir Oltean 
49697a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
49797a69a0dSVladimir Oltean }
49897a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
49997a69a0dSVladimir Oltean 
5003e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5013e8a72d1SFlorian Fainelli {
5023e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5035f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
5044ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5053e8a72d1SFlorian Fainelli 
5065f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
5075f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
5085f6b4e14SFlorian Fainelli 	s->tx_packets++;
5095f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
5105f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
5113e8a72d1SFlorian Fainelli 
512146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = NULL;
51387671375SVladimir Oltean 
51490af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
51590af1059SBrandon Streiff 	 * switch driver
51690af1059SBrandon Streiff 	 */
51790af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
51890af1059SBrandon Streiff 
519fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
520fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
521fe47d563SVivien Didelot 	 */
5224ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
523fe47d563SVivien Didelot 	if (!nskb) {
524fe47d563SVivien Didelot 		kfree_skb(skb);
5254ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
526fe47d563SVivien Didelot 	}
5275aed85ceSFlorian Fainelli 
52897a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
52997a69a0dSVladimir Oltean }
53004ff53f9SFlorian Fainelli 
53191da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
53291da11f8SLennert Buytenhek 
53391da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
53491da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
53591da11f8SLennert Buytenhek {
5367826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
5377826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
5387826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
53991da11f8SLennert Buytenhek }
54091da11f8SLennert Buytenhek 
5413d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
5423d762a0fSGuenter Roeck {
543d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
544d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5453d762a0fSGuenter Roeck 
5469d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
547d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
5483d762a0fSGuenter Roeck 
5493d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
5503d762a0fSGuenter Roeck }
5513d762a0fSGuenter Roeck 
5523d762a0fSGuenter Roeck static void
5533d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
5543d762a0fSGuenter Roeck {
555d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
556d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5573d762a0fSGuenter Roeck 
5589d490b4eSVivien Didelot 	if (ds->ops->get_regs)
559d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
5603d762a0fSGuenter Roeck }
5613d762a0fSGuenter Roeck 
562aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
563aab9c406SFlorian Fainelli {
564aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
565aab9c406SFlorian Fainelli 
566aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
567aab9c406SFlorian Fainelli }
568aab9c406SFlorian Fainelli 
5696793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5706793abb4SGuenter Roeck {
571d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
572d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5736793abb4SGuenter Roeck 
5740e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
575ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5766793abb4SGuenter Roeck 
5779d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5789d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5796793abb4SGuenter Roeck 
5806793abb4SGuenter Roeck 	return 0;
5816793abb4SGuenter Roeck }
5826793abb4SGuenter Roeck 
5836793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5846793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5856793abb4SGuenter Roeck {
586d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
587d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5886793abb4SGuenter Roeck 
5899d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5909d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5916793abb4SGuenter Roeck 
5926793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5936793abb4SGuenter Roeck }
5946793abb4SGuenter Roeck 
5956793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5966793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5976793abb4SGuenter Roeck {
598d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
599d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6006793abb4SGuenter Roeck 
6019d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
6029d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
6036793abb4SGuenter Roeck 
6046793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6056793abb4SGuenter Roeck }
6066793abb4SGuenter Roeck 
60791da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
60891da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
60991da11f8SLennert Buytenhek {
610d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
611d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
61291da11f8SLennert Buytenhek 
61391da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
61491da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
61591da11f8SLennert Buytenhek 
61691da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
61791da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
61891da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
61991da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
6209d490b4eSVivien Didelot 		if (ds->ops->get_strings)
62189f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
62289f09048SFlorian Fainelli 					     data + 4 * len);
62391da11f8SLennert Buytenhek 	}
62491da11f8SLennert Buytenhek }
62591da11f8SLennert Buytenhek 
62691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
62791da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
62891da11f8SLennert Buytenhek 					uint64_t *data)
62991da11f8SLennert Buytenhek {
630d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
63191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
632d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6335f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
634f613ed66SFlorian Fainelli 	unsigned int start;
6355f6b4e14SFlorian Fainelli 	int i;
63691da11f8SLennert Buytenhek 
6375f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
6385f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
6395f6b4e14SFlorian Fainelli 
6405f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
641f613ed66SFlorian Fainelli 		do {
6425f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
6435f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
6445f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
6455f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
6465f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
6475f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
6485f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
6495f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
6505f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
6515f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
6525f6b4e14SFlorian Fainelli 	}
6539d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
654d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
65591da11f8SLennert Buytenhek }
65691da11f8SLennert Buytenhek 
65791da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
65891da11f8SLennert Buytenhek {
659d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
660d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
66191da11f8SLennert Buytenhek 
66291da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
66391da11f8SLennert Buytenhek 		int count;
66491da11f8SLennert Buytenhek 
66591da11f8SLennert Buytenhek 		count = 4;
6669d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
66789f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
66891da11f8SLennert Buytenhek 
66991da11f8SLennert Buytenhek 		return count;
67091da11f8SLennert Buytenhek 	}
67191da11f8SLennert Buytenhek 
67291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
67391da11f8SLennert Buytenhek }
67491da11f8SLennert Buytenhek 
67519e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
67619e57c4eSFlorian Fainelli {
677d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
678d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
67919e57c4eSFlorian Fainelli 
680aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
681aab9c406SFlorian Fainelli 
6829d490b4eSVivien Didelot 	if (ds->ops->get_wol)
683d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
68419e57c4eSFlorian Fainelli }
68519e57c4eSFlorian Fainelli 
68619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
68719e57c4eSFlorian Fainelli {
688d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
689d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
69019e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
69119e57c4eSFlorian Fainelli 
692aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
693aab9c406SFlorian Fainelli 
6949d490b4eSVivien Didelot 	if (ds->ops->set_wol)
695d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
69619e57c4eSFlorian Fainelli 
69719e57c4eSFlorian Fainelli 	return ret;
69819e57c4eSFlorian Fainelli }
69919e57c4eSFlorian Fainelli 
7007905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7017905288fSFlorian Fainelli {
702d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
703d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7047905288fSFlorian Fainelli 	int ret;
7057905288fSFlorian Fainelli 
7067b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
70700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7087b9cc738SVivien Didelot 		return -ENODEV;
7097b9cc738SVivien Didelot 
71008f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
7117905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7127905288fSFlorian Fainelli 
713d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
7147905288fSFlorian Fainelli 	if (ret)
7157905288fSFlorian Fainelli 		return ret;
7167905288fSFlorian Fainelli 
717aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
7187905288fSFlorian Fainelli }
7197905288fSFlorian Fainelli 
7207905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7217905288fSFlorian Fainelli {
722d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
723d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7247905288fSFlorian Fainelli 	int ret;
7257905288fSFlorian Fainelli 
7267b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
72700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7287b9cc738SVivien Didelot 		return -ENODEV;
7297b9cc738SVivien Didelot 
73008f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
7317905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7327905288fSFlorian Fainelli 
733d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
7347905288fSFlorian Fainelli 	if (ret)
7357905288fSFlorian Fainelli 		return ret;
7367905288fSFlorian Fainelli 
737aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
738aab9c406SFlorian Fainelli }
739aab9c406SFlorian Fainelli 
740aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
741aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
742aab9c406SFlorian Fainelli {
743aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
744aab9c406SFlorian Fainelli 
745aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
746aab9c406SFlorian Fainelli }
747aab9c406SFlorian Fainelli 
748aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
749aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
750aab9c406SFlorian Fainelli {
751aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
752aab9c406SFlorian Fainelli 
753aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
7547905288fSFlorian Fainelli }
7557905288fSFlorian Fainelli 
756a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
757a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
758a2a1a13bSHeiner Kallweit {
759a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
760a2a1a13bSHeiner Kallweit 
761a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
762a2a1a13bSHeiner Kallweit }
763a2a1a13bSHeiner Kallweit 
764a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
765a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
766a2a1a13bSHeiner Kallweit {
767a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
768a2a1a13bSHeiner Kallweit 
769a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
770a2a1a13bSHeiner Kallweit }
771a2a1a13bSHeiner Kallweit 
77204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
77304ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
77404ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
77504ff53f9SFlorian Fainelli {
776d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
77704ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
77804ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
77904ff53f9SFlorian Fainelli 	int err = 0;
78004ff53f9SFlorian Fainelli 
78104ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
78204ff53f9SFlorian Fainelli 	if (!netpoll)
78304ff53f9SFlorian Fainelli 		return -ENOMEM;
78404ff53f9SFlorian Fainelli 
78504ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
78604ff53f9SFlorian Fainelli 	if (err) {
78704ff53f9SFlorian Fainelli 		kfree(netpoll);
78804ff53f9SFlorian Fainelli 		goto out;
78904ff53f9SFlorian Fainelli 	}
79004ff53f9SFlorian Fainelli 
79104ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
79204ff53f9SFlorian Fainelli out:
79304ff53f9SFlorian Fainelli 	return err;
79404ff53f9SFlorian Fainelli }
79504ff53f9SFlorian Fainelli 
79604ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
79704ff53f9SFlorian Fainelli {
79804ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
79904ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
80004ff53f9SFlorian Fainelli 
80104ff53f9SFlorian Fainelli 	if (!netpoll)
80204ff53f9SFlorian Fainelli 		return;
80304ff53f9SFlorian Fainelli 
80404ff53f9SFlorian Fainelli 	p->netpoll = NULL;
80504ff53f9SFlorian Fainelli 
806c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
80704ff53f9SFlorian Fainelli }
80804ff53f9SFlorian Fainelli 
80904ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
81004ff53f9SFlorian Fainelli {
81104ff53f9SFlorian Fainelli }
81204ff53f9SFlorian Fainelli #endif
81304ff53f9SFlorian Fainelli 
81444bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
81544bb765cSFlorian Fainelli 					char *name, size_t len)
81644bb765cSFlorian Fainelli {
817d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
81844bb765cSFlorian Fainelli 
819d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
820d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
821d484210bSJiri Pirko 	 * should be removed with legacy support.
822d484210bSJiri Pirko 	 */
823d484210bSJiri Pirko 	if (dp->ds->devlink)
824d484210bSJiri Pirko 		return -EOPNOTSUPP;
825d484210bSJiri Pirko 
826d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
82744bb765cSFlorian Fainelli 		return -EINVAL;
8283a543ef4SFlorian Fainelli 
8293a543ef4SFlorian Fainelli 	return 0;
8303a543ef4SFlorian Fainelli }
8313a543ef4SFlorian Fainelli 
832f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
8334fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
834f50f2127SFlorian Fainelli {
8354fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
836f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
837f50f2127SFlorian Fainelli 
838f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
839f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
840f50f2127SFlorian Fainelli 			return mall_tc_entry;
841f50f2127SFlorian Fainelli 
842f50f2127SFlorian Fainelli 	return NULL;
843f50f2127SFlorian Fainelli }
844f50f2127SFlorian Fainelli 
845*e13c2075SVladimir Oltean static int
846*e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
847f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
848f50f2127SFlorian Fainelli 				  bool ingress)
849f50f2127SFlorian Fainelli {
850d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
851f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
852*e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
853f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
854d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8559681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
856d945097bSVivien Didelot 	struct dsa_port *to_dp;
857*e13c2075SVladimir Oltean 	int err;
858*e13c2075SVladimir Oltean 
859*e13c2075SVladimir Oltean 	act = &cls->rule->action.entries[0];
860f50f2127SFlorian Fainelli 
861f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
862f50f2127SFlorian Fainelli 		return err;
863f50f2127SFlorian Fainelli 
864*e13c2075SVladimir Oltean 	if (!act->dev)
865*e13c2075SVladimir Oltean 		return -EINVAL;
866f50f2127SFlorian Fainelli 
86753eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
868319a1d19SJiri Pirko 					      cls->common.extack))
869319a1d19SJiri Pirko 		return err;
870319a1d19SJiri Pirko 
8719681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
872f50f2127SFlorian Fainelli 
8739681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
874f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
875f50f2127SFlorian Fainelli 
876f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
877f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
878f50f2127SFlorian Fainelli 		return -ENOMEM;
879f50f2127SFlorian Fainelli 
880f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
881f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
882f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
883f50f2127SFlorian Fainelli 
8849681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
885f50f2127SFlorian Fainelli 
886d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
887f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
888f50f2127SFlorian Fainelli 
889d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
890f50f2127SFlorian Fainelli 	if (err) {
891f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
892f50f2127SFlorian Fainelli 		return err;
893f50f2127SFlorian Fainelli 	}
894f50f2127SFlorian Fainelli 
895f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
896*e13c2075SVladimir Oltean 
897*e13c2075SVladimir Oltean 	return err;
898f50f2127SFlorian Fainelli }
899f50f2127SFlorian Fainelli 
900*e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
901*e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
902*e13c2075SVladimir Oltean 				      bool ingress)
903*e13c2075SVladimir Oltean {
904*e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
905*e13c2075SVladimir Oltean 
906*e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
907*e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
908*e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
909*e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
910*e13c2075SVladimir Oltean 
911*e13c2075SVladimir Oltean 	return err;
912f50f2127SFlorian Fainelli }
913f50f2127SFlorian Fainelli 
914f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
915f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
916f50f2127SFlorian Fainelli {
917d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
918f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
919d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
920f50f2127SFlorian Fainelli 
921f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
922f50f2127SFlorian Fainelli 		return;
923f50f2127SFlorian Fainelli 
9244fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
925f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
926f50f2127SFlorian Fainelli 		return;
927f50f2127SFlorian Fainelli 
928f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
929f50f2127SFlorian Fainelli 
930f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
931f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
932d945097bSVivien Didelot 		ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
933f50f2127SFlorian Fainelli 		break;
934f50f2127SFlorian Fainelli 	default:
935f50f2127SFlorian Fainelli 		WARN_ON(1);
936f50f2127SFlorian Fainelli 	}
937f50f2127SFlorian Fainelli 
938f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
939f50f2127SFlorian Fainelli }
940f50f2127SFlorian Fainelli 
9413fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
9426b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
9436b3eb752SJiri Pirko 					   bool ingress)
944f50f2127SFlorian Fainelli {
9455fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
946a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
947f50f2127SFlorian Fainelli 
9483fbae382SJiri Pirko 	switch (cls->command) {
9493fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
9505fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
9513fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
9523fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
9533fbae382SJiri Pirko 		return 0;
9543fbae382SJiri Pirko 	default:
9553fbae382SJiri Pirko 		return -EOPNOTSUPP;
9563fbae382SJiri Pirko 	}
9573fbae382SJiri Pirko }
9583fbae382SJiri Pirko 
959ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
960ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
961ed11bb1fSVladimir Oltean 				    bool ingress)
962ed11bb1fSVladimir Oltean {
963ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
964ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
965ed11bb1fSVladimir Oltean 	int port = dp->index;
966ed11bb1fSVladimir Oltean 
967ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
968ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
969ed11bb1fSVladimir Oltean 
970ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
971ed11bb1fSVladimir Oltean }
972ed11bb1fSVladimir Oltean 
973ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
974ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
975ed11bb1fSVladimir Oltean 				    bool ingress)
976ed11bb1fSVladimir Oltean {
977ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
978ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
979ed11bb1fSVladimir Oltean 	int port = dp->index;
980ed11bb1fSVladimir Oltean 
981ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
982ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
983ed11bb1fSVladimir Oltean 
984ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
985ed11bb1fSVladimir Oltean }
986ed11bb1fSVladimir Oltean 
987ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
988ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
989ed11bb1fSVladimir Oltean 				      bool ingress)
990ed11bb1fSVladimir Oltean {
991ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
992ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
993ed11bb1fSVladimir Oltean 	int port = dp->index;
994ed11bb1fSVladimir Oltean 
995ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
996ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
997ed11bb1fSVladimir Oltean 
998ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
999ed11bb1fSVladimir Oltean }
1000ed11bb1fSVladimir Oltean 
1001ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1002ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1003ed11bb1fSVladimir Oltean 					 bool ingress)
1004ed11bb1fSVladimir Oltean {
1005ed11bb1fSVladimir Oltean 	switch (cls->command) {
1006ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1007ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1008ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1009ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1010ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1011ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1012ed11bb1fSVladimir Oltean 	default:
1013ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1014ed11bb1fSVladimir Oltean 	}
1015ed11bb1fSVladimir Oltean }
1016ed11bb1fSVladimir Oltean 
10176b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
10186b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
10196b3eb752SJiri Pirko {
10206b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
10216b3eb752SJiri Pirko 
102244ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
102344ae12a7SJiri Pirko 		return -EOPNOTSUPP;
102444ae12a7SJiri Pirko 
10256b3eb752SJiri Pirko 	switch (type) {
10266b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
10276b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1028ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1029ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
10306b3eb752SJiri Pirko 	default:
10316b3eb752SJiri Pirko 		return -EOPNOTSUPP;
10326b3eb752SJiri Pirko 	}
10336b3eb752SJiri Pirko }
10346b3eb752SJiri Pirko 
10356b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
10366b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
10376b3eb752SJiri Pirko {
10386b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
10396b3eb752SJiri Pirko }
10406b3eb752SJiri Pirko 
10416b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
10426b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
10436b3eb752SJiri Pirko {
10446b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
10456b3eb752SJiri Pirko }
10466b3eb752SJiri Pirko 
1047955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1048955bcb6eSPablo Neira Ayuso 
10496b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1050955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
10516b3eb752SJiri Pirko {
1052955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1053a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
10546b3eb752SJiri Pirko 
105532f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
10566b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
105732f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
10586b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
10596b3eb752SJiri Pirko 	else
10606b3eb752SJiri Pirko 		return -EOPNOTSUPP;
10616b3eb752SJiri Pirko 
1062955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1063955bcb6eSPablo Neira Ayuso 
10646b3eb752SJiri Pirko 	switch (f->command) {
10659c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
10660d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
10670d4fd02eSPablo Neira Ayuso 			return -EBUSY;
10680d4fd02eSPablo Neira Ayuso 
10690c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1070955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1071955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1072955bcb6eSPablo Neira Ayuso 
1073955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1074955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1075955bcb6eSPablo Neira Ayuso 		return 0;
10769c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
107714bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1078955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1079955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1080955bcb6eSPablo Neira Ayuso 
1081955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1082955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
10836b3eb752SJiri Pirko 		return 0;
10846b3eb752SJiri Pirko 	default:
10856b3eb752SJiri Pirko 		return -EOPNOTSUPP;
10866b3eb752SJiri Pirko 	}
10876b3eb752SJiri Pirko }
10886b3eb752SJiri Pirko 
10893fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1090de4784caSJiri Pirko 			      void *type_data)
10913fbae382SJiri Pirko {
109247d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
109347d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
109447d23af2SVladimir Oltean 
109547d23af2SVladimir Oltean 	if (type == TC_SETUP_BLOCK)
10966b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
109747d23af2SVladimir Oltean 
109847d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1099a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
110047d23af2SVladimir Oltean 
110147d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1102f50f2127SFlorian Fainelli }
1103f50f2127SFlorian Fainelli 
1104f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
1105f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
1106f613ed66SFlorian Fainelli {
1107f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
11085f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
1109f613ed66SFlorian Fainelli 	unsigned int start;
11105f6b4e14SFlorian Fainelli 	int i;
1111f613ed66SFlorian Fainelli 
1112f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
11135f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
11145f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
11155f6b4e14SFlorian Fainelli 
11165f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
1117f613ed66SFlorian Fainelli 		do {
11185f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
11195f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
11205f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
11215f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
11225f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
11235f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
11245f6b4e14SFlorian Fainelli 
11255f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
11265f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
11275f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
11285f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
11295f6b4e14SFlorian Fainelli 	}
1130f613ed66SFlorian Fainelli }
1131f613ed66SFlorian Fainelli 
1132bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1133bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1134bf9f2648SFlorian Fainelli {
1135d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1136d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1137bf9f2648SFlorian Fainelli 
1138bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1139bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1140bf9f2648SFlorian Fainelli 
1141d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1142bf9f2648SFlorian Fainelli }
1143bf9f2648SFlorian Fainelli 
1144bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1145bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1146bf9f2648SFlorian Fainelli {
1147d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1148d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1149bf9f2648SFlorian Fainelli 
1150bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1151bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1152bf9f2648SFlorian Fainelli 
1153d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1154bf9f2648SFlorian Fainelli }
1155bf9f2648SFlorian Fainelli 
11560336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
11570336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
11580336369dSBrandon Streiff {
11590336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
11600336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
11610336369dSBrandon Streiff 
11620336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
11630336369dSBrandon Streiff 		return -EOPNOTSUPP;
11640336369dSBrandon Streiff 
11650336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
11660336369dSBrandon Streiff }
11670336369dSBrandon Streiff 
1168061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1169061f6a50SFlorian Fainelli 				     u16 vid)
1170061f6a50SFlorian Fainelli {
1171061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1172061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1173061f6a50SFlorian Fainelli 	int ret;
1174061f6a50SFlorian Fainelli 
1175061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1176061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1177061f6a50SFlorian Fainelli 	 */
1178061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1179c5335d73SVivien Didelot 		if (!br_vlan_enabled(dp->bridge_dev))
1180c5335d73SVivien Didelot 			return 0;
1181c5335d73SVivien Didelot 
1182061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1183061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1184061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1185061f6a50SFlorian Fainelli 		 */
1186061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1187061f6a50SFlorian Fainelli 		if (ret == 0)
1188061f6a50SFlorian Fainelli 			return -EBUSY;
1189061f6a50SFlorian Fainelli 	}
1190061f6a50SFlorian Fainelli 
1191cf360866SVivien Didelot 	ret = dsa_port_vid_add(dp, vid, 0);
11929b236d2aSVladimir Oltean 	if (ret)
1193cf360866SVivien Didelot 		return ret;
1194cf360866SVivien Didelot 
11957e1741b4SVivien Didelot 	ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
11969b236d2aSVladimir Oltean 	if (ret)
11977e1741b4SVivien Didelot 		return ret;
11987e1741b4SVivien Didelot 
1199cf360866SVivien Didelot 	return 0;
1200061f6a50SFlorian Fainelli }
1201061f6a50SFlorian Fainelli 
1202061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1203061f6a50SFlorian Fainelli 				      u16 vid)
1204061f6a50SFlorian Fainelli {
1205061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1206061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1207061f6a50SFlorian Fainelli 	int ret;
1208061f6a50SFlorian Fainelli 
1209061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1210061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1211061f6a50SFlorian Fainelli 	 */
1212061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1213c5335d73SVivien Didelot 		if (!br_vlan_enabled(dp->bridge_dev))
1214c5335d73SVivien Didelot 			return 0;
1215c5335d73SVivien Didelot 
1216061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1217061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1218061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1219061f6a50SFlorian Fainelli 		 */
1220061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1221061f6a50SFlorian Fainelli 		if (ret == 0)
1222061f6a50SFlorian Fainelli 			return -EBUSY;
1223061f6a50SFlorian Fainelli 	}
1224061f6a50SFlorian Fainelli 
12257e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
12267e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
12277e1741b4SVivien Didelot 	 */
12289b236d2aSVladimir Oltean 	return dsa_port_vid_del(dp, vid);
1229061f6a50SFlorian Fainelli }
1230061f6a50SFlorian Fainelli 
1231bff33f7eSVladimir Oltean struct dsa_hw_port {
1232bff33f7eSVladimir Oltean 	struct list_head list;
1233bff33f7eSVladimir Oltean 	struct net_device *dev;
1234bff33f7eSVladimir Oltean 	int old_mtu;
1235bff33f7eSVladimir Oltean };
1236bff33f7eSVladimir Oltean 
1237bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1238bff33f7eSVladimir Oltean {
1239bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1240bff33f7eSVladimir Oltean 	int err;
1241bff33f7eSVladimir Oltean 
1242bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1243bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1244bff33f7eSVladimir Oltean 			continue;
1245bff33f7eSVladimir Oltean 
1246bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1247bff33f7eSVladimir Oltean 		if (err)
1248bff33f7eSVladimir Oltean 			goto rollback;
1249bff33f7eSVladimir Oltean 	}
1250bff33f7eSVladimir Oltean 
1251bff33f7eSVladimir Oltean 	return 0;
1252bff33f7eSVladimir Oltean 
1253bff33f7eSVladimir Oltean rollback:
1254bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1255bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1256bff33f7eSVladimir Oltean 			continue;
1257bff33f7eSVladimir Oltean 
1258bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1259bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1260bff33f7eSVladimir Oltean 	}
1261bff33f7eSVladimir Oltean 
1262bff33f7eSVladimir Oltean 	return err;
1263bff33f7eSVladimir Oltean }
1264bff33f7eSVladimir Oltean 
1265bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1266bff33f7eSVladimir Oltean {
1267bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1268bff33f7eSVladimir Oltean 
1269bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1270bff33f7eSVladimir Oltean 		kfree(p);
1271bff33f7eSVladimir Oltean }
1272bff33f7eSVladimir Oltean 
1273bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1274bff33f7eSVladimir Oltean void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1275bff33f7eSVladimir Oltean {
1276bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1277bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1278bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1279bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1280bff33f7eSVladimir Oltean 	int err;
1281bff33f7eSVladimir Oltean 
1282bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1283bff33f7eSVladimir Oltean 		return;
1284bff33f7eSVladimir Oltean 
1285bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1286bff33f7eSVladimir Oltean 		return;
1287bff33f7eSVladimir Oltean 
1288bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1289bff33f7eSVladimir Oltean 
1290bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1291bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1292bff33f7eSVladimir Oltean 	 */
1293bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1294bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1295bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1296bff33f7eSVladimir Oltean 			struct net_device *slave;
1297bff33f7eSVladimir Oltean 
1298bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1299bff33f7eSVladimir Oltean 				continue;
1300bff33f7eSVladimir Oltean 
1301bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1302bff33f7eSVladimir Oltean 				continue;
1303bff33f7eSVladimir Oltean 
1304bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1305bff33f7eSVladimir Oltean 				continue;
1306bff33f7eSVladimir Oltean 
1307bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1308bff33f7eSVladimir Oltean 
1309bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1310bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1311bff33f7eSVladimir Oltean 
1312bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1313bff33f7eSVladimir Oltean 			if (!hw_port)
1314bff33f7eSVladimir Oltean 				goto out;
1315bff33f7eSVladimir Oltean 
1316bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1317bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1318bff33f7eSVladimir Oltean 
1319bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1320bff33f7eSVladimir Oltean 		}
1321bff33f7eSVladimir Oltean 	}
1322bff33f7eSVladimir Oltean 
1323bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1324bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1325bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1326bff33f7eSVladimir Oltean 	 */
1327bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1328bff33f7eSVladimir Oltean 	if (!err)
1329bff33f7eSVladimir Oltean 		goto out;
1330bff33f7eSVladimir Oltean 
1331bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1332bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1333bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1334bff33f7eSVladimir Oltean 	 */
1335bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1336bff33f7eSVladimir Oltean 
1337bff33f7eSVladimir Oltean out:
1338bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1339bff33f7eSVladimir Oltean }
1340bff33f7eSVladimir Oltean 
1341bfcb8132SVladimir Oltean static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1342bfcb8132SVladimir Oltean {
1343bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1344bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1345bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1346bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1347bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1348bfcb8132SVladimir Oltean 	int port = p->dp->index;
1349bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1350bfcb8132SVladimir Oltean 	int new_master_mtu;
1351bfcb8132SVladimir Oltean 	int old_master_mtu;
1352bfcb8132SVladimir Oltean 	int mtu_limit;
1353bfcb8132SVladimir Oltean 	int cpu_mtu;
1354bfcb8132SVladimir Oltean 	int err, i;
1355bfcb8132SVladimir Oltean 
1356bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1357bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1358bfcb8132SVladimir Oltean 
1359bfcb8132SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1360bfcb8132SVladimir Oltean 		int slave_mtu;
1361bfcb8132SVladimir Oltean 
1362bfcb8132SVladimir Oltean 		if (!dsa_is_user_port(ds, i))
1363bfcb8132SVladimir Oltean 			continue;
1364bfcb8132SVladimir Oltean 
1365bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1366bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1367bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1368bfcb8132SVladimir Oltean 		 */
1369bfcb8132SVladimir Oltean 		if (!dsa_to_port(ds, i)->slave)
1370bfcb8132SVladimir Oltean 			continue;
1371bfcb8132SVladimir Oltean 
1372bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1373bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1374bfcb8132SVladimir Oltean 		 */
1375bfcb8132SVladimir Oltean 		if (i == port)
1376bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1377bfcb8132SVladimir Oltean 		else
1378bfcb8132SVladimir Oltean 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1379bfcb8132SVladimir Oltean 
1380bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1381bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1382bfcb8132SVladimir Oltean 	}
1383bfcb8132SVladimir Oltean 
1384bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1385bfcb8132SVladimir Oltean 
1386bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1387bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
1388bfcb8132SVladimir Oltean 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1389bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1390bfcb8132SVladimir Oltean 		return -ERANGE;
1391bfcb8132SVladimir Oltean 
1392bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1393bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1394bfcb8132SVladimir Oltean 	 */
1395bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1396bfcb8132SVladimir Oltean 
1397bfcb8132SVladimir Oltean 	/* Start applying stuff */
1398bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1399bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1400bfcb8132SVladimir Oltean 		if (err < 0)
1401bfcb8132SVladimir Oltean 			goto out_master_failed;
1402bfcb8132SVladimir Oltean 
1403bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1404bfcb8132SVladimir Oltean 		 * upstream switches.
1405bfcb8132SVladimir Oltean 		 */
1406bfcb8132SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1407bfcb8132SVladimir Oltean 		if (err)
1408bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1409bfcb8132SVladimir Oltean 	}
1410bfcb8132SVladimir Oltean 
1411bfcb8132SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, false);
1412bfcb8132SVladimir Oltean 	if (err)
1413bfcb8132SVladimir Oltean 		goto out_port_failed;
1414bfcb8132SVladimir Oltean 
1415bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1416bfcb8132SVladimir Oltean 
1417bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1418bff33f7eSVladimir Oltean 
1419bfcb8132SVladimir Oltean 	return 0;
1420bfcb8132SVladimir Oltean 
1421bfcb8132SVladimir Oltean out_port_failed:
1422bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1423bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1424bfcb8132SVladimir Oltean 				    cpu_dp->tag_ops->overhead,
1425bfcb8132SVladimir Oltean 				    true);
1426bfcb8132SVladimir Oltean out_cpu_failed:
1427bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1428bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1429bfcb8132SVladimir Oltean out_master_failed:
1430bfcb8132SVladimir Oltean 	return err;
1431bfcb8132SVladimir Oltean }
1432bfcb8132SVladimir Oltean 
143391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
143491da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
14353d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
14363d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1437aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1438c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
14396793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
14406793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
14416793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
144291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
144391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
144491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
144519e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
144619e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
14477905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
14487905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1449aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1450aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1451a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1452a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1453bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1454bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
14550336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
145691da11f8SLennert Buytenhek };
145791da11f8SLennert Buytenhek 
14582a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
14592a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
14602a93c1a3SFlorian Fainelli 		       struct net_device *dev,
14612a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid,
146287b0984eSPetr Machata 		       u16 flags,
146387b0984eSPetr Machata 		       struct netlink_ext_ack *extack)
14642a93c1a3SFlorian Fainelli {
14652a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
14662a93c1a3SFlorian Fainelli 
14672a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
14682a93c1a3SFlorian Fainelli }
14692a93c1a3SFlorian Fainelli 
14702a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
14712a93c1a3SFlorian Fainelli 		       struct net_device *dev,
14722a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid)
14732a93c1a3SFlorian Fainelli {
14742a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
14752a93c1a3SFlorian Fainelli 
14762a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
14772a93c1a3SFlorian Fainelli }
14782a93c1a3SFlorian Fainelli 
1479716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1480716efee2SJiri Pirko {
1481716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1482716efee2SJiri Pirko 
1483716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1484716efee2SJiri Pirko }
1485716efee2SJiri Pirko 
14863e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1487d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1488d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
14893e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1490d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1491d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1492d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
149337b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
149437b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
14952bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1496d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1497abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
149804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
149904ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
150004ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
150104ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
150204ff53f9SFlorian Fainelli #endif
150344bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1504f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1505f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
1506929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1507061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1508061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1509716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1510bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
151198237d43SScott Feldman };
151298237d43SScott Feldman 
1513f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1514f37db85dSFlorian Fainelli 	.name	= "dsa",
1515f37db85dSFlorian Fainelli };
1516f37db85dSFlorian Fainelli 
1517aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1518aab9c406SFlorian Fainelli {
1519aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1520aab9c406SFlorian Fainelli 
1521aab9c406SFlorian Fainelli 	phylink_mac_change(dp->pl, up);
1522aab9c406SFlorian Fainelli }
1523aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1524aab9c406SFlorian Fainelli 
1525aab9c406SFlorian Fainelli static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1526aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1527aab9c406SFlorian Fainelli {
1528aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1529aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1530aab9c406SFlorian Fainelli 
1531aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1532aab9c406SFlorian Fainelli 	 * not be called if it was not.
1533aab9c406SFlorian Fainelli 	 */
1534aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1535ce31b31cSFlorian Fainelli }
1536ce31b31cSFlorian Fainelli 
153791da11f8SLennert Buytenhek /* slave device setup *******************************************************/
15384fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1539c305c165SFlorian Fainelli {
1540d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1541d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1542c305c165SFlorian Fainelli 
15430115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
15440115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1545d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1546c305c165SFlorian Fainelli 		return -ENODEV;
1547d25b8e74SRussell King 	}
1548c305c165SFlorian Fainelli 
1549aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1550c305c165SFlorian Fainelli }
1551c305c165SFlorian Fainelli 
15524fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
15530d8bcdd3SFlorian Fainelli {
1554d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1555d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1556d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
15570c65b2b9SAndrew Lunn 	phy_interface_t mode;
15586819563eSFlorian Fainelli 	u32 phy_flags = 0;
15590c65b2b9SAndrew Lunn 	int ret;
15600d8bcdd3SFlorian Fainelli 
15610c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
15620c65b2b9SAndrew Lunn 	if (ret)
156319334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
15640d8bcdd3SFlorian Fainelli 
156544cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
156644cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
156744cc27e4SIoana Ciornei 
156844cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
156977373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1570aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1571aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1572aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1573aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1574aab9c406SFlorian Fainelli 	}
1575aab9c406SFlorian Fainelli 
1576aab9c406SFlorian Fainelli 	/* Register only if the switch provides such a callback, since this
1577aab9c406SFlorian Fainelli 	 * callback takes precedence over polling the link GPIO in PHYLINK
1578aab9c406SFlorian Fainelli 	 * (see phylink_get_fixed_state).
15790d8bcdd3SFlorian Fainelli 	 */
1580aab9c406SFlorian Fainelli 	if (ds->ops->phylink_fixed_state)
1581aab9c406SFlorian Fainelli 		phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
15820d8bcdd3SFlorian Fainelli 
15839d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1584d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
15856819563eSFlorian Fainelli 
1586aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
15876146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
15886146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
15896146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
15900d8bcdd3SFlorian Fainelli 		 */
1591d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1592d25b8e74SRussell King 		if (ret) {
1593aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1594aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1595d945097bSVivien Didelot 				   dp->index, ret);
1596aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1597c305c165SFlorian Fainelli 			return ret;
1598d25b8e74SRussell King 		}
15990d8bcdd3SFlorian Fainelli 	}
16009697f1cdSFlorian Fainelli 
16016146dd45SVladimir Oltean 	return ret;
1602b31f65fbSAndrew Lunn }
16030d8bcdd3SFlorian Fainelli 
160424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
160524462549SFlorian Fainelli {
1606aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
160724462549SFlorian Fainelli 
1608a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1609a94c689eSFlorian Fainelli 		return 0;
1610a94c689eSFlorian Fainelli 
1611f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1612f154be24SFlorian Fainelli 
1613aab9c406SFlorian Fainelli 	rtnl_lock();
1614aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1615aab9c406SFlorian Fainelli 	rtnl_unlock();
161624462549SFlorian Fainelli 
161724462549SFlorian Fainelli 	return 0;
161824462549SFlorian Fainelli }
161924462549SFlorian Fainelli 
162024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
162124462549SFlorian Fainelli {
1622aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1623aab9c406SFlorian Fainelli 
1624a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1625a94c689eSFlorian Fainelli 		return 0;
1626a94c689eSFlorian Fainelli 
162724462549SFlorian Fainelli 	netif_device_attach(slave_dev);
162824462549SFlorian Fainelli 
1629aab9c406SFlorian Fainelli 	rtnl_lock();
1630aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1631aab9c406SFlorian Fainelli 	rtnl_unlock();
163224462549SFlorian Fainelli 
163324462549SFlorian Fainelli 	return 0;
163424462549SFlorian Fainelli }
163524462549SFlorian Fainelli 
16366158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val)
16376158eaa7SVivien Didelot {
1638d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
1639d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
16406158eaa7SVivien Didelot 	struct dsa_notifier_register_info rinfo = {
16416158eaa7SVivien Didelot 		.switch_number = dp->ds->index,
16426158eaa7SVivien Didelot 		.port_number = dp->index,
16436158eaa7SVivien Didelot 		.master = master,
16446158eaa7SVivien Didelot 		.info.dev = dev,
16456158eaa7SVivien Didelot 	};
16466158eaa7SVivien Didelot 
16476158eaa7SVivien Didelot 	call_dsa_notifiers(val, dev, &rinfo.info);
16486158eaa7SVivien Didelot }
16496158eaa7SVivien Didelot 
1650951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
165191da11f8SLennert Buytenhek {
165224a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1653f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
16544cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1655951259aaSVivien Didelot 	const char *name = port->name;
165691da11f8SLennert Buytenhek 	struct net_device *slave_dev;
165791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
165891da11f8SLennert Buytenhek 	int ret;
165991da11f8SLennert Buytenhek 
166055199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
166155199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
166255199df6SFlorian Fainelli 
166355199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
166455199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
166555199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
166691da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1667d87d6f44SGuenter Roeck 		return -ENOMEM;
166891da11f8SLennert Buytenhek 
16699b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
16709b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
16719b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1672f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
16737ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
16744974f9b7SPetr Štetiar 	if (!IS_ERR_OR_NULL(port->mac))
1675a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1676a2c7023fSXiaofei Shen 	else
16772fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
16780a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
16793e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
16808b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
1681bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1682bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1683bfcb8132SVladimir Oltean 	else
16848b1efc0fSJarod Wilson 		slave_dev->max_mtu = ETH_MAX_MTU;
1685f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1686d442ad4aSStephen Hemminger 
16874cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
16884cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
168991da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
169091da11f8SLennert Buytenhek 
169191da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
16925f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
16935f6b4e14SFlorian Fainelli 	if (!p->stats64) {
16945f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
16955f6b4e14SFlorian Fainelli 		return -ENOMEM;
16965f6b4e14SFlorian Fainelli 	}
16974cfbf09cSVivien Didelot 	p->dp = port;
1698f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
169915240248SVivien Didelot 	p->xmit = cpu_dp->tag_ops->xmit;
1700f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
170191da11f8SLennert Buytenhek 
1702bfcb8132SVladimir Oltean 	rtnl_lock();
1703bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1704bfcb8132SVladimir Oltean 	rtnl_unlock();
1705bfcb8132SVladimir Oltean 	if (ret && ret != -EOPNOTSUPP) {
1706bfcb8132SVladimir Oltean 		dev_err(ds->dev, "error %d setting MTU on port %d\n",
1707bfcb8132SVladimir Oltean 			ret, port->index);
1708bfcb8132SVladimir Oltean 		goto out_free;
1709bfcb8132SVladimir Oltean 	}
1710bfcb8132SVladimir Oltean 
171191da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
171291da11f8SLennert Buytenhek 
17134fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
17140071f56eSAndrew Lunn 	if (ret) {
17150071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
1716e804441cSFlorian Fainelli 		goto out_free;
1717e804441cSFlorian Fainelli 	}
1718e804441cSFlorian Fainelli 
17196158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
172060724d4bSFlorian Fainelli 
1721e804441cSFlorian Fainelli 	ret = register_netdev(slave_dev);
1722e804441cSFlorian Fainelli 	if (ret) {
1723e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1724e804441cSFlorian Fainelli 			   ret, slave_dev->name);
1725e804441cSFlorian Fainelli 		goto out_phy;
17260071f56eSAndrew Lunn 	}
17270071f56eSAndrew Lunn 
1728d87d6f44SGuenter Roeck 	return 0;
1729e804441cSFlorian Fainelli 
1730e804441cSFlorian Fainelli out_phy:
1731aab9c406SFlorian Fainelli 	rtnl_lock();
1732aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1733aab9c406SFlorian Fainelli 	rtnl_unlock();
1734aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1735e804441cSFlorian Fainelli out_free:
1736e804441cSFlorian Fainelli 	free_percpu(p->stats64);
1737e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1738f8b8b1cdSVivien Didelot 	port->slave = NULL;
1739e804441cSFlorian Fainelli 	return ret;
174091da11f8SLennert Buytenhek }
1741b73adef6SFlorian Fainelli 
1742cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1743cda5c15bSNeil Armstrong {
1744d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1745cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1746cda5c15bSNeil Armstrong 
1747cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1748aab9c406SFlorian Fainelli 	rtnl_lock();
1749aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1750aab9c406SFlorian Fainelli 	rtnl_unlock();
1751881eadabSJohan Hovold 
17526158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1753cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1754aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
17555f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1756cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1757cda5c15bSNeil Armstrong }
1758cda5c15bSNeil Armstrong 
17594d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
1760b73adef6SFlorian Fainelli {
1761b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1762b73adef6SFlorian Fainelli }
1763b73adef6SFlorian Fainelli 
17648e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
17658e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1766b73adef6SFlorian Fainelli {
1767d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
17688e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1769b73adef6SFlorian Fainelli 
17708e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
17718e92ab3aSVivien Didelot 		if (info->linking) {
177217d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
1773bff33f7eSVladimir Oltean 			if (!err)
1774bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
17758e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
17768e92ab3aSVivien Didelot 		} else {
177717d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
17788e92ab3aSVivien Didelot 			err = NOTIFY_OK;
17798e92ab3aSVivien Didelot 		}
17806debb68aSVivien Didelot 	}
1781b73adef6SFlorian Fainelli 
17828e92ab3aSVivien Didelot 	return err;
1783b73adef6SFlorian Fainelli }
1784b73adef6SFlorian Fainelli 
1785cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev,
1786cc1d5bdaSFlorian Fainelli 				      struct netdev_notifier_changeupper_info *
1787cc1d5bdaSFlorian Fainelli 				      info)
1788cc1d5bdaSFlorian Fainelli {
1789cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
1790cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
1791cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
1792cc1d5bdaSFlorian Fainelli 
1793cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1794cc1d5bdaSFlorian Fainelli 
1795cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
1796cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1797cc1d5bdaSFlorian Fainelli 
1798cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
1799cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
1800cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1801cc1d5bdaSFlorian Fainelli 
1802cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
1803cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
1804cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1805cc1d5bdaSFlorian Fainelli 
1806cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1807cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
1808cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1809cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
1810cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
1811cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
1812cc1d5bdaSFlorian Fainelli 	}
1813cc1d5bdaSFlorian Fainelli 
1814cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
1815cc1d5bdaSFlorian Fainelli }
1816cc1d5bdaSFlorian Fainelli 
181788e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1818b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1819b73adef6SFlorian Fainelli {
18206debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1821b73adef6SFlorian Fainelli 
1822cc1d5bdaSFlorian Fainelli 	if (event == NETDEV_CHANGEUPPER) {
182353bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
1824cc1d5bdaSFlorian Fainelli 			return dsa_slave_upper_vlan_check(dev, ptr);
18258e92ab3aSVivien Didelot 
18268e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1827cc1d5bdaSFlorian Fainelli 	}
1828b73adef6SFlorian Fainelli 
1829b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1830b73adef6SFlorian Fainelli }
183188e4f0caSVivien Didelot 
1832c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1833c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1834c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1835c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1836c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1837c9eb3e0fSArkadi Sharshevsky };
1838c9eb3e0fSArkadi Sharshevsky 
1839c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1840c9eb3e0fSArkadi Sharshevsky {
1841c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1842c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1843c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1844c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1845d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1846c9eb3e0fSArkadi Sharshevsky 	int err;
1847c9eb3e0fSArkadi Sharshevsky 
1848c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1849c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1850c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1851c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1852a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1853a37fb855SVivien Didelot 			break;
1854a37fb855SVivien Didelot 
1855d945097bSVivien Didelot 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1856c9eb3e0fSArkadi Sharshevsky 		if (err) {
1857c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1858c9eb3e0fSArkadi Sharshevsky 			break;
1859c9eb3e0fSArkadi Sharshevsky 		}
1860e9ba0fbcSIdo Schimmel 		fdb_info->offloaded = true;
1861c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
18626685987cSPetr Machata 					 &fdb_info->info, NULL);
1863c9eb3e0fSArkadi Sharshevsky 		break;
1864c9eb3e0fSArkadi Sharshevsky 
1865c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1866c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1867a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1868a37fb855SVivien Didelot 			break;
1869a37fb855SVivien Didelot 
1870d945097bSVivien Didelot 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1871c9eb3e0fSArkadi Sharshevsky 		if (err) {
1872c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1873c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1874c9eb3e0fSArkadi Sharshevsky 		}
1875c9eb3e0fSArkadi Sharshevsky 		break;
1876c9eb3e0fSArkadi Sharshevsky 	}
1877c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1878c9eb3e0fSArkadi Sharshevsky 
1879c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1880c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1881c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1882c9eb3e0fSArkadi Sharshevsky }
1883c9eb3e0fSArkadi Sharshevsky 
1884c9eb3e0fSArkadi Sharshevsky static int
1885c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1886c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1887c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1888c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1889c9eb3e0fSArkadi Sharshevsky {
1890c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1891c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1892c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1893c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1894c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1895c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1896c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1897c9eb3e0fSArkadi Sharshevsky 	return 0;
1898c9eb3e0fSArkadi Sharshevsky }
1899c9eb3e0fSArkadi Sharshevsky 
1900c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1901c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
1902c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
1903c9eb3e0fSArkadi Sharshevsky {
1904c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1905c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
190679b139f4SVivien Didelot 	int err;
190779b139f4SVivien Didelot 
190879b139f4SVivien Didelot 	if (event == SWITCHDEV_PORT_ATTR_SET) {
190979b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
191079b139f4SVivien Didelot 						     dsa_slave_dev_check,
191179b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
191279b139f4SVivien Didelot 		return notifier_from_errno(err);
191379b139f4SVivien Didelot 	}
1914c9eb3e0fSArkadi Sharshevsky 
1915c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
1916c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1917c9eb3e0fSArkadi Sharshevsky 
1918c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1919c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
1920c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
1921c9eb3e0fSArkadi Sharshevsky 
1922c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
1923c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
1924c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
1925c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
1926c9eb3e0fSArkadi Sharshevsky 
1927c9eb3e0fSArkadi Sharshevsky 	switch (event) {
1928c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1929c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1930a37fb855SVivien Didelot 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1931c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
1932c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
1933c9eb3e0fSArkadi Sharshevsky 		break;
1934c9eb3e0fSArkadi Sharshevsky 	default:
1935c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
1936c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1937c9eb3e0fSArkadi Sharshevsky 	}
1938c9eb3e0fSArkadi Sharshevsky 
1939c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
1940c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
1941c9eb3e0fSArkadi Sharshevsky 
1942c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
1943c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1944c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
1945c9eb3e0fSArkadi Sharshevsky }
1946c9eb3e0fSArkadi Sharshevsky 
19472b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
19482b239f67SPetr Machata 					      unsigned long event, void *ptr)
19492b239f67SPetr Machata {
19502b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
195179b139f4SVivien Didelot 	int err;
19522b239f67SPetr Machata 
19532b239f67SPetr Machata 	switch (event) {
195479b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
195579b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
195679b139f4SVivien Didelot 						    dsa_slave_dev_check,
195779b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
195879b139f4SVivien Didelot 		return notifier_from_errno(err);
19592b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
196079b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
196179b139f4SVivien Didelot 						    dsa_slave_dev_check,
196279b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
196379b139f4SVivien Didelot 		return notifier_from_errno(err);
19649ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
196579b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
196679b139f4SVivien Didelot 						     dsa_slave_dev_check,
196779b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
196879b139f4SVivien Didelot 		return notifier_from_errno(err);
19692b239f67SPetr Machata 	}
19702b239f67SPetr Machata 
19712b239f67SPetr Machata 	return NOTIFY_DONE;
19722b239f67SPetr Machata }
19732b239f67SPetr Machata 
197488e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
197588e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
197688e4f0caSVivien Didelot };
197788e4f0caSVivien Didelot 
1978c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
1979c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
1980c9eb3e0fSArkadi Sharshevsky };
1981c9eb3e0fSArkadi Sharshevsky 
19822b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
19832b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
19842b239f67SPetr Machata };
19852b239f67SPetr Machata 
198688e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
198788e4f0caSVivien Didelot {
19882b239f67SPetr Machata 	struct notifier_block *nb;
1989c9eb3e0fSArkadi Sharshevsky 	int err;
1990c9eb3e0fSArkadi Sharshevsky 
1991c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
1992c9eb3e0fSArkadi Sharshevsky 	if (err)
1993c9eb3e0fSArkadi Sharshevsky 		return err;
1994c9eb3e0fSArkadi Sharshevsky 
1995c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1996c9eb3e0fSArkadi Sharshevsky 	if (err)
1997c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
1998c9eb3e0fSArkadi Sharshevsky 
19992b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
20002b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
20012b239f67SPetr Machata 	if (err)
20022b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
20032b239f67SPetr Machata 
2004c9eb3e0fSArkadi Sharshevsky 	return 0;
2005c9eb3e0fSArkadi Sharshevsky 
20062b239f67SPetr Machata err_switchdev_blocking_nb:
20072b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2008c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2009c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2010c9eb3e0fSArkadi Sharshevsky 	return err;
201188e4f0caSVivien Didelot }
201288e4f0caSVivien Didelot 
201388e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
201488e4f0caSVivien Didelot {
20152b239f67SPetr Machata 	struct notifier_block *nb;
201688e4f0caSVivien Didelot 	int err;
201788e4f0caSVivien Didelot 
20182b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
20192b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
20202b239f67SPetr Machata 	if (err)
20212b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
20222b239f67SPetr Machata 
2023c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2024c9eb3e0fSArkadi Sharshevsky 	if (err)
2025c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2026c9eb3e0fSArkadi Sharshevsky 
202788e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
202888e4f0caSVivien Didelot 	if (err)
202988e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
203088e4f0caSVivien Didelot }
2031