xref: /openbmc/linux/net/dsa/slave.c (revision 2f1e8ea726e9020e01e9e2ae29c2d5eb11133032)
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 
31754a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
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 
38454a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
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 
448f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
44904ff53f9SFlorian Fainelli #else
45004ff53f9SFlorian Fainelli 	BUG();
45104ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
452f78ed220SEric Dumazet #endif
45304ff53f9SFlorian Fainelli }
45404ff53f9SFlorian Fainelli 
45590af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
45690af1059SBrandon Streiff 				 struct sk_buff *skb)
45790af1059SBrandon Streiff {
45890af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
45990af1059SBrandon Streiff 	struct sk_buff *clone;
46090af1059SBrandon Streiff 	unsigned int type;
46190af1059SBrandon Streiff 
46290af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
46390af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
46490af1059SBrandon Streiff 		return;
46590af1059SBrandon Streiff 
46690af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
46790af1059SBrandon Streiff 		return;
46890af1059SBrandon Streiff 
46990af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
47090af1059SBrandon Streiff 	if (!clone)
47190af1059SBrandon Streiff 		return;
47290af1059SBrandon Streiff 
473146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = clone;
474146d442cSVladimir Oltean 
47590af1059SBrandon Streiff 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
47690af1059SBrandon Streiff 		return;
47790af1059SBrandon Streiff 
47890af1059SBrandon Streiff 	kfree_skb(clone);
47990af1059SBrandon Streiff }
48090af1059SBrandon Streiff 
48197a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
48297a69a0dSVladimir Oltean {
48397a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
48497a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
48597a69a0dSVladimir Oltean 	 */
48697a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
48797a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
48897a69a0dSVladimir Oltean 
48997a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
49097a69a0dSVladimir Oltean 	 * do not modify its EtherType
49197a69a0dSVladimir Oltean 	 */
49297a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
49397a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
49497a69a0dSVladimir Oltean 
49597a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
49697a69a0dSVladimir Oltean }
49797a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
49897a69a0dSVladimir Oltean 
4993e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5003e8a72d1SFlorian Fainelli {
5013e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5025f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
5034ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5043e8a72d1SFlorian Fainelli 
5055f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
5065f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
5075f6b4e14SFlorian Fainelli 	s->tx_packets++;
5085f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
5095f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
5103e8a72d1SFlorian Fainelli 
511146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = NULL;
51287671375SVladimir Oltean 
51390af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
51490af1059SBrandon Streiff 	 * switch driver
51590af1059SBrandon Streiff 	 */
51690af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
51790af1059SBrandon Streiff 
518fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
519fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
520fe47d563SVivien Didelot 	 */
5214ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
522fe47d563SVivien Didelot 	if (!nskb) {
523fe47d563SVivien Didelot 		kfree_skb(skb);
5244ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
525fe47d563SVivien Didelot 	}
5265aed85ceSFlorian Fainelli 
52797a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
52897a69a0dSVladimir Oltean }
52904ff53f9SFlorian Fainelli 
53091da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
53191da11f8SLennert Buytenhek 
53291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
53391da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
53491da11f8SLennert Buytenhek {
5357826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
5367826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
5377826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
53891da11f8SLennert Buytenhek }
53991da11f8SLennert Buytenhek 
5403d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
5413d762a0fSGuenter Roeck {
542d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
543d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5443d762a0fSGuenter Roeck 
5459d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
546d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
5473d762a0fSGuenter Roeck 
5483d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
5493d762a0fSGuenter Roeck }
5503d762a0fSGuenter Roeck 
5513d762a0fSGuenter Roeck static void
5523d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
5533d762a0fSGuenter Roeck {
554d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
555d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5563d762a0fSGuenter Roeck 
5579d490b4eSVivien Didelot 	if (ds->ops->get_regs)
558d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
5593d762a0fSGuenter Roeck }
5603d762a0fSGuenter Roeck 
561aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
562aab9c406SFlorian Fainelli {
563aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
564aab9c406SFlorian Fainelli 
565aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
566aab9c406SFlorian Fainelli }
567aab9c406SFlorian Fainelli 
5686793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5696793abb4SGuenter Roeck {
570d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
571d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5726793abb4SGuenter Roeck 
5730e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
574ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5756793abb4SGuenter Roeck 
5769d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5779d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5786793abb4SGuenter Roeck 
5796793abb4SGuenter Roeck 	return 0;
5806793abb4SGuenter Roeck }
5816793abb4SGuenter Roeck 
5826793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5836793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5846793abb4SGuenter Roeck {
585d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
586d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5876793abb4SGuenter Roeck 
5889d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5899d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5906793abb4SGuenter Roeck 
5916793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5926793abb4SGuenter Roeck }
5936793abb4SGuenter Roeck 
5946793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5956793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5966793abb4SGuenter Roeck {
597d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
598d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5996793abb4SGuenter Roeck 
6009d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
6019d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
6026793abb4SGuenter Roeck 
6036793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6046793abb4SGuenter Roeck }
6056793abb4SGuenter Roeck 
60691da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
60791da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
60891da11f8SLennert Buytenhek {
609d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
610d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
61191da11f8SLennert Buytenhek 
61291da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
61391da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
61491da11f8SLennert Buytenhek 
61591da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
61691da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
61791da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
61891da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
6199d490b4eSVivien Didelot 		if (ds->ops->get_strings)
62089f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
62189f09048SFlorian Fainelli 					     data + 4 * len);
62291da11f8SLennert Buytenhek 	}
62391da11f8SLennert Buytenhek }
62491da11f8SLennert Buytenhek 
62591da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
62691da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
62791da11f8SLennert Buytenhek 					uint64_t *data)
62891da11f8SLennert Buytenhek {
629d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
63091da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
631d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6325f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
633f613ed66SFlorian Fainelli 	unsigned int start;
6345f6b4e14SFlorian Fainelli 	int i;
63591da11f8SLennert Buytenhek 
6365f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
6375f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
6385f6b4e14SFlorian Fainelli 
6395f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
640f613ed66SFlorian Fainelli 		do {
6415f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
6425f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
6435f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
6445f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
6455f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
6465f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
6475f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
6485f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
6495f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
6505f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
6515f6b4e14SFlorian Fainelli 	}
6529d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
653d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
65491da11f8SLennert Buytenhek }
65591da11f8SLennert Buytenhek 
65691da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
65791da11f8SLennert Buytenhek {
658d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
659d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
66091da11f8SLennert Buytenhek 
66191da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
66291da11f8SLennert Buytenhek 		int count;
66391da11f8SLennert Buytenhek 
66491da11f8SLennert Buytenhek 		count = 4;
6659d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
66689f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
66791da11f8SLennert Buytenhek 
66891da11f8SLennert Buytenhek 		return count;
66991da11f8SLennert Buytenhek 	}
67091da11f8SLennert Buytenhek 
67191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
67291da11f8SLennert Buytenhek }
67391da11f8SLennert Buytenhek 
67419e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
67519e57c4eSFlorian Fainelli {
676d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
677d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
67819e57c4eSFlorian Fainelli 
679aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
680aab9c406SFlorian Fainelli 
6819d490b4eSVivien Didelot 	if (ds->ops->get_wol)
682d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
68319e57c4eSFlorian Fainelli }
68419e57c4eSFlorian Fainelli 
68519e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
68619e57c4eSFlorian Fainelli {
687d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
688d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
68919e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
69019e57c4eSFlorian Fainelli 
691aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
692aab9c406SFlorian Fainelli 
6939d490b4eSVivien Didelot 	if (ds->ops->set_wol)
694d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
69519e57c4eSFlorian Fainelli 
69619e57c4eSFlorian Fainelli 	return ret;
69719e57c4eSFlorian Fainelli }
69819e57c4eSFlorian Fainelli 
6997905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7007905288fSFlorian Fainelli {
701d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
702d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7037905288fSFlorian Fainelli 	int ret;
7047905288fSFlorian Fainelli 
7057b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
70600670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7077b9cc738SVivien Didelot 		return -ENODEV;
7087b9cc738SVivien Didelot 
70908f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
7107905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7117905288fSFlorian Fainelli 
712d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
7137905288fSFlorian Fainelli 	if (ret)
7147905288fSFlorian Fainelli 		return ret;
7157905288fSFlorian Fainelli 
716aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
7177905288fSFlorian Fainelli }
7187905288fSFlorian Fainelli 
7197905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7207905288fSFlorian Fainelli {
721d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
722d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7237905288fSFlorian Fainelli 	int ret;
7247905288fSFlorian Fainelli 
7257b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
72600670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7277b9cc738SVivien Didelot 		return -ENODEV;
7287b9cc738SVivien Didelot 
72908f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
7307905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7317905288fSFlorian Fainelli 
732d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
7337905288fSFlorian Fainelli 	if (ret)
7347905288fSFlorian Fainelli 		return ret;
7357905288fSFlorian Fainelli 
736aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
737aab9c406SFlorian Fainelli }
738aab9c406SFlorian Fainelli 
739aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
740aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
741aab9c406SFlorian Fainelli {
742aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
743aab9c406SFlorian Fainelli 
744aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
745aab9c406SFlorian Fainelli }
746aab9c406SFlorian Fainelli 
747aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
748aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
749aab9c406SFlorian Fainelli {
750aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
751aab9c406SFlorian Fainelli 
752aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
7537905288fSFlorian Fainelli }
7547905288fSFlorian Fainelli 
755a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
756a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
757a2a1a13bSHeiner Kallweit {
758a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
759a2a1a13bSHeiner Kallweit 
760a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
761a2a1a13bSHeiner Kallweit }
762a2a1a13bSHeiner Kallweit 
763a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
764a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
765a2a1a13bSHeiner Kallweit {
766a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
767a2a1a13bSHeiner Kallweit 
768a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
769a2a1a13bSHeiner Kallweit }
770a2a1a13bSHeiner Kallweit 
77104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
77204ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
77304ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
77404ff53f9SFlorian Fainelli {
775d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
77604ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
77704ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
77804ff53f9SFlorian Fainelli 	int err = 0;
77904ff53f9SFlorian Fainelli 
78004ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
78104ff53f9SFlorian Fainelli 	if (!netpoll)
78204ff53f9SFlorian Fainelli 		return -ENOMEM;
78304ff53f9SFlorian Fainelli 
78404ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
78504ff53f9SFlorian Fainelli 	if (err) {
78604ff53f9SFlorian Fainelli 		kfree(netpoll);
78704ff53f9SFlorian Fainelli 		goto out;
78804ff53f9SFlorian Fainelli 	}
78904ff53f9SFlorian Fainelli 
79004ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
79104ff53f9SFlorian Fainelli out:
79204ff53f9SFlorian Fainelli 	return err;
79304ff53f9SFlorian Fainelli }
79404ff53f9SFlorian Fainelli 
79504ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
79604ff53f9SFlorian Fainelli {
79704ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
79804ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
79904ff53f9SFlorian Fainelli 
80004ff53f9SFlorian Fainelli 	if (!netpoll)
80104ff53f9SFlorian Fainelli 		return;
80204ff53f9SFlorian Fainelli 
80304ff53f9SFlorian Fainelli 	p->netpoll = NULL;
80404ff53f9SFlorian Fainelli 
805c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
80604ff53f9SFlorian Fainelli }
80704ff53f9SFlorian Fainelli 
80804ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
80904ff53f9SFlorian Fainelli {
81004ff53f9SFlorian Fainelli }
81104ff53f9SFlorian Fainelli #endif
81204ff53f9SFlorian Fainelli 
81344bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
81444bb765cSFlorian Fainelli 					char *name, size_t len)
81544bb765cSFlorian Fainelli {
816d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
81744bb765cSFlorian Fainelli 
818d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
819d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
820d484210bSJiri Pirko 	 * should be removed with legacy support.
821d484210bSJiri Pirko 	 */
822d484210bSJiri Pirko 	if (dp->ds->devlink)
823d484210bSJiri Pirko 		return -EOPNOTSUPP;
824d484210bSJiri Pirko 
825d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
82644bb765cSFlorian Fainelli 		return -EINVAL;
8273a543ef4SFlorian Fainelli 
8283a543ef4SFlorian Fainelli 	return 0;
8293a543ef4SFlorian Fainelli }
8303a543ef4SFlorian Fainelli 
831f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
8324fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
833f50f2127SFlorian Fainelli {
8344fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
835f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
836f50f2127SFlorian Fainelli 
837f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
838f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
839f50f2127SFlorian Fainelli 			return mall_tc_entry;
840f50f2127SFlorian Fainelli 
841f50f2127SFlorian Fainelli 	return NULL;
842f50f2127SFlorian Fainelli }
843f50f2127SFlorian Fainelli 
844e13c2075SVladimir Oltean static int
845e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
846f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
847f50f2127SFlorian Fainelli 				  bool ingress)
848f50f2127SFlorian Fainelli {
849d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
850f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
851e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
852f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
853d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8549681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
855d945097bSVivien Didelot 	struct dsa_port *to_dp;
856e13c2075SVladimir Oltean 	int err;
857e13c2075SVladimir Oltean 
858f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
85934297176SVladimir Oltean 		return -EOPNOTSUPP;
860f50f2127SFlorian Fainelli 
86153eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
862319a1d19SJiri Pirko 					      cls->common.extack))
86334297176SVladimir Oltean 		return -EOPNOTSUPP;
864319a1d19SJiri Pirko 
8659681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
866f50f2127SFlorian Fainelli 
86765722159SVladimir Oltean 	if (!act->dev)
86865722159SVladimir Oltean 		return -EINVAL;
86965722159SVladimir Oltean 
8709681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
871f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
872f50f2127SFlorian Fainelli 
873f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
874f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
875f50f2127SFlorian Fainelli 		return -ENOMEM;
876f50f2127SFlorian Fainelli 
877f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
878f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
879f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
880f50f2127SFlorian Fainelli 
8819681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
882f50f2127SFlorian Fainelli 
883d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
884f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
885f50f2127SFlorian Fainelli 
886d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
887f50f2127SFlorian Fainelli 	if (err) {
888f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
889f50f2127SFlorian Fainelli 		return err;
890f50f2127SFlorian Fainelli 	}
891f50f2127SFlorian Fainelli 
892f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
893e13c2075SVladimir Oltean 
894e13c2075SVladimir Oltean 	return err;
895f50f2127SFlorian Fainelli }
896f50f2127SFlorian Fainelli 
89734297176SVladimir Oltean static int
89834297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
89934297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
90034297176SVladimir Oltean 				  bool ingress)
90134297176SVladimir Oltean {
90234297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
90334297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
90434297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
90534297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
90634297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
90734297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
90834297176SVladimir Oltean 	struct flow_action_entry *act;
90934297176SVladimir Oltean 	int err;
91034297176SVladimir Oltean 
91134297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
91234297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
913c75a33c8SJacob Keller 				   "Policing offload not implemented");
91434297176SVladimir Oltean 		return -EOPNOTSUPP;
91534297176SVladimir Oltean 	}
91634297176SVladimir Oltean 
91734297176SVladimir Oltean 	if (!ingress) {
91834297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
919c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
92034297176SVladimir Oltean 		return -EOPNOTSUPP;
92134297176SVladimir Oltean 	}
92234297176SVladimir Oltean 
92334297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
92434297176SVladimir Oltean 					      cls->common.extack))
92534297176SVladimir Oltean 		return -EOPNOTSUPP;
92634297176SVladimir Oltean 
92734297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
92834297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
92934297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
930c75a33c8SJacob Keller 					   "Only one port policer allowed");
93134297176SVladimir Oltean 			return -EEXIST;
93234297176SVladimir Oltean 		}
93334297176SVladimir Oltean 	}
93434297176SVladimir Oltean 
93534297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
93634297176SVladimir Oltean 
93734297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
93834297176SVladimir Oltean 	if (!mall_tc_entry)
93934297176SVladimir Oltean 		return -ENOMEM;
94034297176SVladimir Oltean 
94134297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
94234297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
94334297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
94434297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
94534297176SVladimir Oltean 	policer->burst = act->police.burst;
94634297176SVladimir Oltean 
94734297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
94834297176SVladimir Oltean 	if (err) {
94934297176SVladimir Oltean 		kfree(mall_tc_entry);
95034297176SVladimir Oltean 		return err;
95134297176SVladimir Oltean 	}
95234297176SVladimir Oltean 
95334297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
95434297176SVladimir Oltean 
95534297176SVladimir Oltean 	return err;
95634297176SVladimir Oltean }
95734297176SVladimir Oltean 
958e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
959e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
960e13c2075SVladimir Oltean 				      bool ingress)
961e13c2075SVladimir Oltean {
962e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
963e13c2075SVladimir Oltean 
964e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
965e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
966e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
967e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
96834297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
96934297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
97034297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
971e13c2075SVladimir Oltean 
972e13c2075SVladimir Oltean 	return err;
973f50f2127SFlorian Fainelli }
974f50f2127SFlorian Fainelli 
975f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
976f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
977f50f2127SFlorian Fainelli {
978d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
979f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
980d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
981f50f2127SFlorian Fainelli 
9824fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
983f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
984f50f2127SFlorian Fainelli 		return;
985f50f2127SFlorian Fainelli 
986f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
987f50f2127SFlorian Fainelli 
988f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
989f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
99034297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
99134297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
99234297176SVladimir Oltean 						 &mall_tc_entry->mirror);
99334297176SVladimir Oltean 		break;
99434297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
99534297176SVladimir Oltean 		if (ds->ops->port_policer_del)
99634297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
997f50f2127SFlorian Fainelli 		break;
998f50f2127SFlorian Fainelli 	default:
999f50f2127SFlorian Fainelli 		WARN_ON(1);
1000f50f2127SFlorian Fainelli 	}
1001f50f2127SFlorian Fainelli 
1002f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1003f50f2127SFlorian Fainelli }
1004f50f2127SFlorian Fainelli 
10053fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
10066b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
10076b3eb752SJiri Pirko 					   bool ingress)
1008f50f2127SFlorian Fainelli {
10095fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1010a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1011f50f2127SFlorian Fainelli 
10123fbae382SJiri Pirko 	switch (cls->command) {
10133fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
10145fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
10153fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
10163fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
10173fbae382SJiri Pirko 		return 0;
10183fbae382SJiri Pirko 	default:
10193fbae382SJiri Pirko 		return -EOPNOTSUPP;
10203fbae382SJiri Pirko 	}
10213fbae382SJiri Pirko }
10223fbae382SJiri Pirko 
1023ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1024ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1025ed11bb1fSVladimir Oltean 				    bool ingress)
1026ed11bb1fSVladimir Oltean {
1027ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1028ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1029ed11bb1fSVladimir Oltean 	int port = dp->index;
1030ed11bb1fSVladimir Oltean 
1031ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1032ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1033ed11bb1fSVladimir Oltean 
1034ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1035ed11bb1fSVladimir Oltean }
1036ed11bb1fSVladimir Oltean 
1037ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1038ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1039ed11bb1fSVladimir Oltean 				    bool ingress)
1040ed11bb1fSVladimir Oltean {
1041ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1042ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1043ed11bb1fSVladimir Oltean 	int port = dp->index;
1044ed11bb1fSVladimir Oltean 
1045ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1046ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1047ed11bb1fSVladimir Oltean 
1048ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1049ed11bb1fSVladimir Oltean }
1050ed11bb1fSVladimir Oltean 
1051ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1052ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1053ed11bb1fSVladimir Oltean 				      bool ingress)
1054ed11bb1fSVladimir Oltean {
1055ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1056ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1057ed11bb1fSVladimir Oltean 	int port = dp->index;
1058ed11bb1fSVladimir Oltean 
1059ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1060ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1061ed11bb1fSVladimir Oltean 
1062ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1063ed11bb1fSVladimir Oltean }
1064ed11bb1fSVladimir Oltean 
1065ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1066ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1067ed11bb1fSVladimir Oltean 					 bool ingress)
1068ed11bb1fSVladimir Oltean {
1069ed11bb1fSVladimir Oltean 	switch (cls->command) {
1070ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1071ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1072ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1073ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1074ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1075ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1076ed11bb1fSVladimir Oltean 	default:
1077ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1078ed11bb1fSVladimir Oltean 	}
1079ed11bb1fSVladimir Oltean }
1080ed11bb1fSVladimir Oltean 
10816b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
10826b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
10836b3eb752SJiri Pirko {
10846b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
10856b3eb752SJiri Pirko 
108644ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
108744ae12a7SJiri Pirko 		return -EOPNOTSUPP;
108844ae12a7SJiri Pirko 
10896b3eb752SJiri Pirko 	switch (type) {
10906b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
10916b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1092ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1093ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
10946b3eb752SJiri Pirko 	default:
10956b3eb752SJiri Pirko 		return -EOPNOTSUPP;
10966b3eb752SJiri Pirko 	}
10976b3eb752SJiri Pirko }
10986b3eb752SJiri Pirko 
10996b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
11006b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11016b3eb752SJiri Pirko {
11026b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
11036b3eb752SJiri Pirko }
11046b3eb752SJiri Pirko 
11056b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
11066b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11076b3eb752SJiri Pirko {
11086b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
11096b3eb752SJiri Pirko }
11106b3eb752SJiri Pirko 
1111955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1112955bcb6eSPablo Neira Ayuso 
11136b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1114955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
11156b3eb752SJiri Pirko {
1116955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1117a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
11186b3eb752SJiri Pirko 
111932f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
11206b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
112132f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
11226b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
11236b3eb752SJiri Pirko 	else
11246b3eb752SJiri Pirko 		return -EOPNOTSUPP;
11256b3eb752SJiri Pirko 
1126955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1127955bcb6eSPablo Neira Ayuso 
11286b3eb752SJiri Pirko 	switch (f->command) {
11299c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
11300d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
11310d4fd02eSPablo Neira Ayuso 			return -EBUSY;
11320d4fd02eSPablo Neira Ayuso 
11330c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1134955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1135955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1136955bcb6eSPablo Neira Ayuso 
1137955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1138955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1139955bcb6eSPablo Neira Ayuso 		return 0;
11409c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
114114bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1142955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1143955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1144955bcb6eSPablo Neira Ayuso 
1145955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1146955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
11476b3eb752SJiri Pirko 		return 0;
11486b3eb752SJiri Pirko 	default:
11496b3eb752SJiri Pirko 		return -EOPNOTSUPP;
11506b3eb752SJiri Pirko 	}
11516b3eb752SJiri Pirko }
11526b3eb752SJiri Pirko 
11533fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1154de4784caSJiri Pirko 			      void *type_data)
11553fbae382SJiri Pirko {
115647d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
115747d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
115847d23af2SVladimir Oltean 
115947d23af2SVladimir Oltean 	if (type == TC_SETUP_BLOCK)
11606b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
116147d23af2SVladimir Oltean 
116247d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1163a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
116447d23af2SVladimir Oltean 
116547d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1166f50f2127SFlorian Fainelli }
1167f50f2127SFlorian Fainelli 
1168f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
1169f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
1170f613ed66SFlorian Fainelli {
1171f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
11725f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
1173f613ed66SFlorian Fainelli 	unsigned int start;
11745f6b4e14SFlorian Fainelli 	int i;
1175f613ed66SFlorian Fainelli 
1176f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
11775f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
11785f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
11795f6b4e14SFlorian Fainelli 
11805f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
1181f613ed66SFlorian Fainelli 		do {
11825f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
11835f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
11845f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
11855f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
11865f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
11875f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
11885f6b4e14SFlorian Fainelli 
11895f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
11905f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
11915f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
11925f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
11935f6b4e14SFlorian Fainelli 	}
1194f613ed66SFlorian Fainelli }
1195f613ed66SFlorian Fainelli 
1196bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1197bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1198bf9f2648SFlorian Fainelli {
1199d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1200d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1201bf9f2648SFlorian Fainelli 
1202bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1203bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1204bf9f2648SFlorian Fainelli 
1205d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1206bf9f2648SFlorian Fainelli }
1207bf9f2648SFlorian Fainelli 
1208bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1209bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1210bf9f2648SFlorian Fainelli {
1211d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1212d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1213bf9f2648SFlorian Fainelli 
1214bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1215bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1216bf9f2648SFlorian Fainelli 
1217d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1218bf9f2648SFlorian Fainelli }
1219bf9f2648SFlorian Fainelli 
12200336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
12210336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
12220336369dSBrandon Streiff {
12230336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
12240336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
12250336369dSBrandon Streiff 
12260336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
12270336369dSBrandon Streiff 		return -EOPNOTSUPP;
12280336369dSBrandon Streiff 
12290336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
12300336369dSBrandon Streiff }
12310336369dSBrandon Streiff 
1232061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1233061f6a50SFlorian Fainelli 				     u16 vid)
1234061f6a50SFlorian Fainelli {
1235061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1236061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1237061f6a50SFlorian Fainelli 	int ret;
1238061f6a50SFlorian Fainelli 
1239061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1240061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1241061f6a50SFlorian Fainelli 	 */
1242061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
124354a0ed0dSRussell King 		if (dsa_port_skip_vlan_configuration(dp))
1244c5335d73SVivien Didelot 			return 0;
1245c5335d73SVivien Didelot 
1246061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1247061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1248061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1249061f6a50SFlorian Fainelli 		 */
1250061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1251061f6a50SFlorian Fainelli 		if (ret == 0)
1252061f6a50SFlorian Fainelli 			return -EBUSY;
1253061f6a50SFlorian Fainelli 	}
1254061f6a50SFlorian Fainelli 
1255cf360866SVivien Didelot 	ret = dsa_port_vid_add(dp, vid, 0);
12569b236d2aSVladimir Oltean 	if (ret)
1257cf360866SVivien Didelot 		return ret;
1258cf360866SVivien Didelot 
12597e1741b4SVivien Didelot 	ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
12609b236d2aSVladimir Oltean 	if (ret)
12617e1741b4SVivien Didelot 		return ret;
12627e1741b4SVivien Didelot 
1263cf360866SVivien Didelot 	return 0;
1264061f6a50SFlorian Fainelli }
1265061f6a50SFlorian Fainelli 
1266061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1267061f6a50SFlorian Fainelli 				      u16 vid)
1268061f6a50SFlorian Fainelli {
1269061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1270061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1271061f6a50SFlorian Fainelli 	int ret;
1272061f6a50SFlorian Fainelli 
1273061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1274061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1275061f6a50SFlorian Fainelli 	 */
1276061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
127754a0ed0dSRussell King 		if (dsa_port_skip_vlan_configuration(dp))
1278c5335d73SVivien Didelot 			return 0;
1279c5335d73SVivien Didelot 
1280061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1281061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1282061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1283061f6a50SFlorian Fainelli 		 */
1284061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1285061f6a50SFlorian Fainelli 		if (ret == 0)
1286061f6a50SFlorian Fainelli 			return -EBUSY;
1287061f6a50SFlorian Fainelli 	}
1288061f6a50SFlorian Fainelli 
12897e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
12907e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
12917e1741b4SVivien Didelot 	 */
12929b236d2aSVladimir Oltean 	return dsa_port_vid_del(dp, vid);
1293061f6a50SFlorian Fainelli }
1294061f6a50SFlorian Fainelli 
1295bff33f7eSVladimir Oltean struct dsa_hw_port {
1296bff33f7eSVladimir Oltean 	struct list_head list;
1297bff33f7eSVladimir Oltean 	struct net_device *dev;
1298bff33f7eSVladimir Oltean 	int old_mtu;
1299bff33f7eSVladimir Oltean };
1300bff33f7eSVladimir Oltean 
1301bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1302bff33f7eSVladimir Oltean {
1303bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1304bff33f7eSVladimir Oltean 	int err;
1305bff33f7eSVladimir Oltean 
1306bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1307bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1308bff33f7eSVladimir Oltean 			continue;
1309bff33f7eSVladimir Oltean 
1310bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1311bff33f7eSVladimir Oltean 		if (err)
1312bff33f7eSVladimir Oltean 			goto rollback;
1313bff33f7eSVladimir Oltean 	}
1314bff33f7eSVladimir Oltean 
1315bff33f7eSVladimir Oltean 	return 0;
1316bff33f7eSVladimir Oltean 
1317bff33f7eSVladimir Oltean rollback:
1318bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1319bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1320bff33f7eSVladimir Oltean 			continue;
1321bff33f7eSVladimir Oltean 
1322bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1323bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1324bff33f7eSVladimir Oltean 	}
1325bff33f7eSVladimir Oltean 
1326bff33f7eSVladimir Oltean 	return err;
1327bff33f7eSVladimir Oltean }
1328bff33f7eSVladimir Oltean 
1329bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1330bff33f7eSVladimir Oltean {
1331bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1332bff33f7eSVladimir Oltean 
1333bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1334bff33f7eSVladimir Oltean 		kfree(p);
1335bff33f7eSVladimir Oltean }
1336bff33f7eSVladimir Oltean 
1337bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1338bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1339bff33f7eSVladimir Oltean {
1340bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1341bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1342bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1343bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1344bff33f7eSVladimir Oltean 	int err;
1345bff33f7eSVladimir Oltean 
1346bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1347bff33f7eSVladimir Oltean 		return;
1348bff33f7eSVladimir Oltean 
1349bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1350bff33f7eSVladimir Oltean 		return;
1351bff33f7eSVladimir Oltean 
1352bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1353bff33f7eSVladimir Oltean 
1354bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1355bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1356bff33f7eSVladimir Oltean 	 */
1357bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1358bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1359bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1360bff33f7eSVladimir Oltean 			struct net_device *slave;
1361bff33f7eSVladimir Oltean 
1362bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1363bff33f7eSVladimir Oltean 				continue;
1364bff33f7eSVladimir Oltean 
1365bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1366bff33f7eSVladimir Oltean 				continue;
1367bff33f7eSVladimir Oltean 
1368bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1369bff33f7eSVladimir Oltean 				continue;
1370bff33f7eSVladimir Oltean 
1371bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1372bff33f7eSVladimir Oltean 
1373bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1374bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1375bff33f7eSVladimir Oltean 
1376bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1377bff33f7eSVladimir Oltean 			if (!hw_port)
1378bff33f7eSVladimir Oltean 				goto out;
1379bff33f7eSVladimir Oltean 
1380bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1381bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1382bff33f7eSVladimir Oltean 
1383bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1384bff33f7eSVladimir Oltean 		}
1385bff33f7eSVladimir Oltean 	}
1386bff33f7eSVladimir Oltean 
1387bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1388bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1389bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1390bff33f7eSVladimir Oltean 	 */
1391bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1392bff33f7eSVladimir Oltean 	if (!err)
1393bff33f7eSVladimir Oltean 		goto out;
1394bff33f7eSVladimir Oltean 
1395bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1396bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1397bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1398bff33f7eSVladimir Oltean 	 */
1399bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1400bff33f7eSVladimir Oltean 
1401bff33f7eSVladimir Oltean out:
1402bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1403bff33f7eSVladimir Oltean }
1404bff33f7eSVladimir Oltean 
1405bfcb8132SVladimir Oltean static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1406bfcb8132SVladimir Oltean {
1407bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1408bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1409bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1410bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1411bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1412bfcb8132SVladimir Oltean 	int port = p->dp->index;
1413bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1414bfcb8132SVladimir Oltean 	int new_master_mtu;
1415bfcb8132SVladimir Oltean 	int old_master_mtu;
1416bfcb8132SVladimir Oltean 	int mtu_limit;
1417bfcb8132SVladimir Oltean 	int cpu_mtu;
1418bfcb8132SVladimir Oltean 	int err, i;
1419bfcb8132SVladimir Oltean 
1420bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1421bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1422bfcb8132SVladimir Oltean 
1423bfcb8132SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1424bfcb8132SVladimir Oltean 		int slave_mtu;
1425bfcb8132SVladimir Oltean 
1426bfcb8132SVladimir Oltean 		if (!dsa_is_user_port(ds, i))
1427bfcb8132SVladimir Oltean 			continue;
1428bfcb8132SVladimir Oltean 
1429bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1430bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1431bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1432bfcb8132SVladimir Oltean 		 */
1433bfcb8132SVladimir Oltean 		if (!dsa_to_port(ds, i)->slave)
1434bfcb8132SVladimir Oltean 			continue;
1435bfcb8132SVladimir Oltean 
1436bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1437bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1438bfcb8132SVladimir Oltean 		 */
1439bfcb8132SVladimir Oltean 		if (i == port)
1440bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1441bfcb8132SVladimir Oltean 		else
1442bfcb8132SVladimir Oltean 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1443bfcb8132SVladimir Oltean 
1444bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1445bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1446bfcb8132SVladimir Oltean 	}
1447bfcb8132SVladimir Oltean 
1448bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1449bfcb8132SVladimir Oltean 
1450bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1451bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
1452bfcb8132SVladimir Oltean 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1453bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1454bfcb8132SVladimir Oltean 		return -ERANGE;
1455bfcb8132SVladimir Oltean 
1456bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1457bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1458bfcb8132SVladimir Oltean 	 */
1459bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1460bfcb8132SVladimir Oltean 
1461bfcb8132SVladimir Oltean 	/* Start applying stuff */
1462bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1463bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1464bfcb8132SVladimir Oltean 		if (err < 0)
1465bfcb8132SVladimir Oltean 			goto out_master_failed;
1466bfcb8132SVladimir Oltean 
1467bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1468bfcb8132SVladimir Oltean 		 * upstream switches.
1469bfcb8132SVladimir Oltean 		 */
1470bfcb8132SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1471bfcb8132SVladimir Oltean 		if (err)
1472bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1473bfcb8132SVladimir Oltean 	}
1474bfcb8132SVladimir Oltean 
1475bfcb8132SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, false);
1476bfcb8132SVladimir Oltean 	if (err)
1477bfcb8132SVladimir Oltean 		goto out_port_failed;
1478bfcb8132SVladimir Oltean 
1479bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1480bfcb8132SVladimir Oltean 
1481bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1482bff33f7eSVladimir Oltean 
1483bfcb8132SVladimir Oltean 	return 0;
1484bfcb8132SVladimir Oltean 
1485bfcb8132SVladimir Oltean out_port_failed:
1486bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1487bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1488bfcb8132SVladimir Oltean 				    cpu_dp->tag_ops->overhead,
1489bfcb8132SVladimir Oltean 				    true);
1490bfcb8132SVladimir Oltean out_cpu_failed:
1491bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1492bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1493bfcb8132SVladimir Oltean out_master_failed:
1494bfcb8132SVladimir Oltean 	return err;
1495bfcb8132SVladimir Oltean }
1496bfcb8132SVladimir Oltean 
149791da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
149891da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
14993d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
15003d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1501aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1502c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
15036793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
15046793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
15056793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
150691da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
150791da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
150891da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
150919e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
151019e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
15117905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
15127905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1513aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1514aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1515a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1516a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1517bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1518bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
15190336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
152091da11f8SLennert Buytenhek };
152191da11f8SLennert Buytenhek 
15222a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
15232a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
15242a93c1a3SFlorian Fainelli 		       struct net_device *dev,
15252a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid,
152687b0984eSPetr Machata 		       u16 flags,
152787b0984eSPetr Machata 		       struct netlink_ext_ack *extack)
15282a93c1a3SFlorian Fainelli {
15292a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15302a93c1a3SFlorian Fainelli 
15312a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
15322a93c1a3SFlorian Fainelli }
15332a93c1a3SFlorian Fainelli 
15342a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
15352a93c1a3SFlorian Fainelli 		       struct net_device *dev,
15362a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid)
15372a93c1a3SFlorian Fainelli {
15382a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15392a93c1a3SFlorian Fainelli 
15402a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
15412a93c1a3SFlorian Fainelli }
15422a93c1a3SFlorian Fainelli 
1543716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1544716efee2SJiri Pirko {
1545716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1546716efee2SJiri Pirko 
1547716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1548716efee2SJiri Pirko }
1549716efee2SJiri Pirko 
15503e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1551d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1552d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
15533e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1554d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1555d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1556d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
155737b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
155837b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
15592bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1560d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1561abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
156204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
156304ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
156404ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
156504ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
156604ff53f9SFlorian Fainelli #endif
156744bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1568f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1569f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
1570929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1571061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1572061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1573716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1574bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
157598237d43SScott Feldman };
157698237d43SScott Feldman 
1577f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1578f37db85dSFlorian Fainelli 	.name	= "dsa",
1579f37db85dSFlorian Fainelli };
1580f37db85dSFlorian Fainelli 
1581aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1582aab9c406SFlorian Fainelli {
1583aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1584aab9c406SFlorian Fainelli 
1585765bda93SRussell King 	if (dp->pl)
1586aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1587aab9c406SFlorian Fainelli }
1588aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1589aab9c406SFlorian Fainelli 
15905c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1591aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1592aab9c406SFlorian Fainelli {
15935c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1594aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1595aab9c406SFlorian Fainelli 
1596aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1597aab9c406SFlorian Fainelli 	 * not be called if it was not.
1598aab9c406SFlorian Fainelli 	 */
1599aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1600ce31b31cSFlorian Fainelli }
1601ce31b31cSFlorian Fainelli 
160291da11f8SLennert Buytenhek /* slave device setup *******************************************************/
16034fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1604c305c165SFlorian Fainelli {
1605d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1606d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1607c305c165SFlorian Fainelli 
16080115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
16090115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1610d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1611c305c165SFlorian Fainelli 		return -ENODEV;
1612d25b8e74SRussell King 	}
1613c305c165SFlorian Fainelli 
1614aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1615c305c165SFlorian Fainelli }
1616c305c165SFlorian Fainelli 
16174fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
16180d8bcdd3SFlorian Fainelli {
1619d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1620d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1621d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
16220c65b2b9SAndrew Lunn 	phy_interface_t mode;
16236819563eSFlorian Fainelli 	u32 phy_flags = 0;
16240c65b2b9SAndrew Lunn 	int ret;
16250d8bcdd3SFlorian Fainelli 
16260c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
16270c65b2b9SAndrew Lunn 	if (ret)
162819334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
16290d8bcdd3SFlorian Fainelli 
163044cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
163144cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
163244cc27e4SIoana Ciornei 
16335c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
16345c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
16355c05c1dbSRussell King 	 * this if the switch provides such a callback.
16365c05c1dbSRussell King 	 */
16375c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
16385c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
16395c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
16405c05c1dbSRussell King 	}
16415c05c1dbSRussell King 
164244cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
164377373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1644aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1645aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1646aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1647aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1648aab9c406SFlorian Fainelli 	}
1649aab9c406SFlorian Fainelli 
16509d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1651d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
16526819563eSFlorian Fainelli 
1653aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
16546146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
16556146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
16566146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
16570d8bcdd3SFlorian Fainelli 		 */
1658d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1659d25b8e74SRussell King 		if (ret) {
1660aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1661aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1662d945097bSVivien Didelot 				   dp->index, ret);
1663aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1664c305c165SFlorian Fainelli 			return ret;
1665d25b8e74SRussell King 		}
16660d8bcdd3SFlorian Fainelli 	}
16679697f1cdSFlorian Fainelli 
16686146dd45SVladimir Oltean 	return ret;
1669b31f65fbSAndrew Lunn }
16700d8bcdd3SFlorian Fainelli 
16711a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
16721a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
16731a33e10eSCong Wang 					    struct netdev_queue *txq,
16741a33e10eSCong Wang 					    void *_unused)
16751a33e10eSCong Wang {
16761a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
16771a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
16781a33e10eSCong Wang }
16791a33e10eSCong Wang 
168024462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
168124462549SFlorian Fainelli {
1682aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
168324462549SFlorian Fainelli 
1684a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1685a94c689eSFlorian Fainelli 		return 0;
1686a94c689eSFlorian Fainelli 
1687f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1688f154be24SFlorian Fainelli 
1689aab9c406SFlorian Fainelli 	rtnl_lock();
1690aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1691aab9c406SFlorian Fainelli 	rtnl_unlock();
169224462549SFlorian Fainelli 
169324462549SFlorian Fainelli 	return 0;
169424462549SFlorian Fainelli }
169524462549SFlorian Fainelli 
169624462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
169724462549SFlorian Fainelli {
1698aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1699aab9c406SFlorian Fainelli 
1700a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1701a94c689eSFlorian Fainelli 		return 0;
1702a94c689eSFlorian Fainelli 
170324462549SFlorian Fainelli 	netif_device_attach(slave_dev);
170424462549SFlorian Fainelli 
1705aab9c406SFlorian Fainelli 	rtnl_lock();
1706aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1707aab9c406SFlorian Fainelli 	rtnl_unlock();
170824462549SFlorian Fainelli 
170924462549SFlorian Fainelli 	return 0;
171024462549SFlorian Fainelli }
171124462549SFlorian Fainelli 
17126158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val)
17136158eaa7SVivien Didelot {
1714d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
1715d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
17166158eaa7SVivien Didelot 	struct dsa_notifier_register_info rinfo = {
17176158eaa7SVivien Didelot 		.switch_number = dp->ds->index,
17186158eaa7SVivien Didelot 		.port_number = dp->index,
17196158eaa7SVivien Didelot 		.master = master,
17206158eaa7SVivien Didelot 		.info.dev = dev,
17216158eaa7SVivien Didelot 	};
17226158eaa7SVivien Didelot 
17236158eaa7SVivien Didelot 	call_dsa_notifiers(val, dev, &rinfo.info);
17246158eaa7SVivien Didelot }
17256158eaa7SVivien Didelot 
1726951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
172791da11f8SLennert Buytenhek {
172824a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1729f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
17304cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1731951259aaSVivien Didelot 	const char *name = port->name;
173291da11f8SLennert Buytenhek 	struct net_device *slave_dev;
173391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
173491da11f8SLennert Buytenhek 	int ret;
173591da11f8SLennert Buytenhek 
173655199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
173755199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
173855199df6SFlorian Fainelli 
173955199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
174055199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
174155199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
174291da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1743d87d6f44SGuenter Roeck 		return -ENOMEM;
174491da11f8SLennert Buytenhek 
17459b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
17469b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
17479b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1748f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
17492b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
17507ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
17514974f9b7SPetr Štetiar 	if (!IS_ERR_OR_NULL(port->mac))
1752a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1753a2c7023fSXiaofei Shen 	else
17542fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
17550a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
17563e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1757bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1758bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1759f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1760d442ad4aSStephen Hemminger 
17611a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
17621a33e10eSCong Wang 				 NULL);
17631a33e10eSCong Wang 
17644cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
17654cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
176691da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
176791da11f8SLennert Buytenhek 
176891da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
17695f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
17705f6b4e14SFlorian Fainelli 	if (!p->stats64) {
17715f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
17725f6b4e14SFlorian Fainelli 		return -ENOMEM;
17735f6b4e14SFlorian Fainelli 	}
1774e131a563SAlexander Lobakin 
1775e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1776e131a563SAlexander Lobakin 	if (ret)
1777e131a563SAlexander Lobakin 		goto out_free;
1778e131a563SAlexander Lobakin 
17794cfbf09cSVivien Didelot 	p->dp = port;
1780f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
178115240248SVivien Didelot 	p->xmit = cpu_dp->tag_ops->xmit;
1782f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
178391da11f8SLennert Buytenhek 
1784bfcb8132SVladimir Oltean 	rtnl_lock();
1785bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1786bfcb8132SVladimir Oltean 	rtnl_unlock();
178772579e14SVladimir Oltean 	if (ret)
178872579e14SVladimir Oltean 		dev_warn(ds->dev, "nonfatal error %d setting MTU on port %d\n",
1789bfcb8132SVladimir Oltean 			 ret, port->index);
1790bfcb8132SVladimir Oltean 
179191da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
179291da11f8SLennert Buytenhek 
17934fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
17940071f56eSAndrew Lunn 	if (ret) {
179565951a9eSFlorian Fainelli 		netdev_err(master, "error %d setting up slave PHY for %s\n",
179665951a9eSFlorian Fainelli 			   ret, slave_dev->name);
1797e131a563SAlexander Lobakin 		goto out_gcells;
1798e804441cSFlorian Fainelli 	}
1799e804441cSFlorian Fainelli 
18006158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
180160724d4bSFlorian Fainelli 
1802*2f1e8ea7SVladimir Oltean 	rtnl_lock();
1803*2f1e8ea7SVladimir Oltean 
1804*2f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1805e804441cSFlorian Fainelli 	if (ret) {
1806e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1807e804441cSFlorian Fainelli 			   ret, slave_dev->name);
1808*2f1e8ea7SVladimir Oltean 		rtnl_unlock();
1809e804441cSFlorian Fainelli 		goto out_phy;
18100071f56eSAndrew Lunn 	}
18110071f56eSAndrew Lunn 
1812*2f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
1813*2f1e8ea7SVladimir Oltean 
1814*2f1e8ea7SVladimir Oltean 	rtnl_unlock();
1815*2f1e8ea7SVladimir Oltean 
1816*2f1e8ea7SVladimir Oltean 	if (ret)
1817*2f1e8ea7SVladimir Oltean 		goto out_unregister;
1818*2f1e8ea7SVladimir Oltean 
1819d87d6f44SGuenter Roeck 	return 0;
1820e804441cSFlorian Fainelli 
1821*2f1e8ea7SVladimir Oltean out_unregister:
1822*2f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1823e804441cSFlorian Fainelli out_phy:
1824aab9c406SFlorian Fainelli 	rtnl_lock();
1825aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1826aab9c406SFlorian Fainelli 	rtnl_unlock();
1827aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1828e131a563SAlexander Lobakin out_gcells:
1829e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1830e804441cSFlorian Fainelli out_free:
1831e804441cSFlorian Fainelli 	free_percpu(p->stats64);
1832e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1833f8b8b1cdSVivien Didelot 	port->slave = NULL;
1834e804441cSFlorian Fainelli 	return ret;
183591da11f8SLennert Buytenhek }
1836b73adef6SFlorian Fainelli 
1837cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1838cda5c15bSNeil Armstrong {
1839*2f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
1840d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1841cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1842cda5c15bSNeil Armstrong 
1843cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1844aab9c406SFlorian Fainelli 	rtnl_lock();
1845*2f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
1846*2f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
1847aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1848aab9c406SFlorian Fainelli 	rtnl_unlock();
1849881eadabSJohan Hovold 
18506158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1851aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
1852e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
18535f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1854cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1855cda5c15bSNeil Armstrong }
1856cda5c15bSNeil Armstrong 
18574d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
1858b73adef6SFlorian Fainelli {
1859b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1860b73adef6SFlorian Fainelli }
1861b73adef6SFlorian Fainelli 
18628e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
18638e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1864b73adef6SFlorian Fainelli {
1865d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
18668e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1867b73adef6SFlorian Fainelli 
18688e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
18698e92ab3aSVivien Didelot 		if (info->linking) {
187017d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
1871bff33f7eSVladimir Oltean 			if (!err)
1872bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
18738e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
18748e92ab3aSVivien Didelot 		} else {
187517d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
18768e92ab3aSVivien Didelot 			err = NOTIFY_OK;
18778e92ab3aSVivien Didelot 		}
18786debb68aSVivien Didelot 	}
1879b73adef6SFlorian Fainelli 
18808e92ab3aSVivien Didelot 	return err;
1881b73adef6SFlorian Fainelli }
1882b73adef6SFlorian Fainelli 
1883cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev,
1884cc1d5bdaSFlorian Fainelli 				      struct netdev_notifier_changeupper_info *
1885cc1d5bdaSFlorian Fainelli 				      info)
1886cc1d5bdaSFlorian Fainelli {
1887cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
1888cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
1889cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
1890cc1d5bdaSFlorian Fainelli 
1891cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1892cc1d5bdaSFlorian Fainelli 
1893cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
1894cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1895cc1d5bdaSFlorian Fainelli 
1896cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
1897cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
1898cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1899cc1d5bdaSFlorian Fainelli 
1900cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
1901cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
1902cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1903cc1d5bdaSFlorian Fainelli 
1904cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1905cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
1906cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1907cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
1908cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
1909cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
1910cc1d5bdaSFlorian Fainelli 	}
1911cc1d5bdaSFlorian Fainelli 
1912cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
1913cc1d5bdaSFlorian Fainelli }
1914cc1d5bdaSFlorian Fainelli 
191588e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1916b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1917b73adef6SFlorian Fainelli {
19186debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1919b73adef6SFlorian Fainelli 
1920cc1d5bdaSFlorian Fainelli 	if (event == NETDEV_CHANGEUPPER) {
192153bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
1922cc1d5bdaSFlorian Fainelli 			return dsa_slave_upper_vlan_check(dev, ptr);
19238e92ab3aSVivien Didelot 
19248e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1925cc1d5bdaSFlorian Fainelli 	}
1926b73adef6SFlorian Fainelli 
1927b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1928b73adef6SFlorian Fainelli }
192988e4f0caSVivien Didelot 
1930c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1931c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1932c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1933c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1934c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1935c9eb3e0fSArkadi Sharshevsky };
1936c9eb3e0fSArkadi Sharshevsky 
1937c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1938c9eb3e0fSArkadi Sharshevsky {
1939c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1940c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1941c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1942c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1943d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1944c9eb3e0fSArkadi Sharshevsky 	int err;
1945c9eb3e0fSArkadi Sharshevsky 
1946c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1947c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1948c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1949c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1950a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1951a37fb855SVivien Didelot 			break;
1952a37fb855SVivien Didelot 
1953d945097bSVivien Didelot 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1954c9eb3e0fSArkadi Sharshevsky 		if (err) {
1955c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1956c9eb3e0fSArkadi Sharshevsky 			break;
1957c9eb3e0fSArkadi Sharshevsky 		}
1958e9ba0fbcSIdo Schimmel 		fdb_info->offloaded = true;
1959c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
19606685987cSPetr Machata 					 &fdb_info->info, NULL);
1961c9eb3e0fSArkadi Sharshevsky 		break;
1962c9eb3e0fSArkadi Sharshevsky 
1963c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1964c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1965a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1966a37fb855SVivien Didelot 			break;
1967a37fb855SVivien Didelot 
1968d945097bSVivien Didelot 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1969c9eb3e0fSArkadi Sharshevsky 		if (err) {
1970c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1971c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1972c9eb3e0fSArkadi Sharshevsky 		}
1973c9eb3e0fSArkadi Sharshevsky 		break;
1974c9eb3e0fSArkadi Sharshevsky 	}
1975c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1976c9eb3e0fSArkadi Sharshevsky 
1977c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1978c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1979c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1980c9eb3e0fSArkadi Sharshevsky }
1981c9eb3e0fSArkadi Sharshevsky 
1982c9eb3e0fSArkadi Sharshevsky static int
1983c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1984c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1985c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1986c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1987c9eb3e0fSArkadi Sharshevsky {
1988c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1989c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1990c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1991c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1992c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1993c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1994c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1995c9eb3e0fSArkadi Sharshevsky 	return 0;
1996c9eb3e0fSArkadi Sharshevsky }
1997c9eb3e0fSArkadi Sharshevsky 
1998c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1999c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2000c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2001c9eb3e0fSArkadi Sharshevsky {
2002c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2003c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
200479b139f4SVivien Didelot 	int err;
200579b139f4SVivien Didelot 
200679b139f4SVivien Didelot 	if (event == SWITCHDEV_PORT_ATTR_SET) {
200779b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
200879b139f4SVivien Didelot 						     dsa_slave_dev_check,
200979b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
201079b139f4SVivien Didelot 		return notifier_from_errno(err);
201179b139f4SVivien Didelot 	}
2012c9eb3e0fSArkadi Sharshevsky 
2013c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
2014c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2015c9eb3e0fSArkadi Sharshevsky 
2016c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2017c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
2018c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
2019c9eb3e0fSArkadi Sharshevsky 
2020c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
2021c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
2022c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
2023c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
2024c9eb3e0fSArkadi Sharshevsky 
2025c9eb3e0fSArkadi Sharshevsky 	switch (event) {
2026df561f66SGustavo A. R. Silva 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2027c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2028a37fb855SVivien Didelot 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
2029c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
2030c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
2031c9eb3e0fSArkadi Sharshevsky 		break;
2032c9eb3e0fSArkadi Sharshevsky 	default:
2033c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
2034c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2035c9eb3e0fSArkadi Sharshevsky 	}
2036c9eb3e0fSArkadi Sharshevsky 
2037c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
2038c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2039c9eb3e0fSArkadi Sharshevsky 
2040c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
2041c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2042c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
2043c9eb3e0fSArkadi Sharshevsky }
2044c9eb3e0fSArkadi Sharshevsky 
20452b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
20462b239f67SPetr Machata 					      unsigned long event, void *ptr)
20472b239f67SPetr Machata {
20482b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
204979b139f4SVivien Didelot 	int err;
20502b239f67SPetr Machata 
20512b239f67SPetr Machata 	switch (event) {
205279b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
205379b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
205479b139f4SVivien Didelot 						    dsa_slave_dev_check,
205579b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
205679b139f4SVivien Didelot 		return notifier_from_errno(err);
20572b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
205879b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
205979b139f4SVivien Didelot 						    dsa_slave_dev_check,
206079b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
206179b139f4SVivien Didelot 		return notifier_from_errno(err);
20629ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
206379b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
206479b139f4SVivien Didelot 						     dsa_slave_dev_check,
206579b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
206679b139f4SVivien Didelot 		return notifier_from_errno(err);
20672b239f67SPetr Machata 	}
20682b239f67SPetr Machata 
20692b239f67SPetr Machata 	return NOTIFY_DONE;
20702b239f67SPetr Machata }
20712b239f67SPetr Machata 
207288e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
207388e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
207488e4f0caSVivien Didelot };
207588e4f0caSVivien Didelot 
2076c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
2077c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2078c9eb3e0fSArkadi Sharshevsky };
2079c9eb3e0fSArkadi Sharshevsky 
20802b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
20812b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
20822b239f67SPetr Machata };
20832b239f67SPetr Machata 
208488e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
208588e4f0caSVivien Didelot {
20862b239f67SPetr Machata 	struct notifier_block *nb;
2087c9eb3e0fSArkadi Sharshevsky 	int err;
2088c9eb3e0fSArkadi Sharshevsky 
2089c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2090c9eb3e0fSArkadi Sharshevsky 	if (err)
2091c9eb3e0fSArkadi Sharshevsky 		return err;
2092c9eb3e0fSArkadi Sharshevsky 
2093c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2094c9eb3e0fSArkadi Sharshevsky 	if (err)
2095c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2096c9eb3e0fSArkadi Sharshevsky 
20972b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
20982b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
20992b239f67SPetr Machata 	if (err)
21002b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
21012b239f67SPetr Machata 
2102c9eb3e0fSArkadi Sharshevsky 	return 0;
2103c9eb3e0fSArkadi Sharshevsky 
21042b239f67SPetr Machata err_switchdev_blocking_nb:
21052b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2106c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2107c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2108c9eb3e0fSArkadi Sharshevsky 	return err;
210988e4f0caSVivien Didelot }
211088e4f0caSVivien Didelot 
211188e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
211288e4f0caSVivien Didelot {
21132b239f67SPetr Machata 	struct notifier_block *nb;
211488e4f0caSVivien Didelot 	int err;
211588e4f0caSVivien Didelot 
21162b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
21172b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
21182b239f67SPetr Machata 	if (err)
21192b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
21202b239f67SPetr Machata 
2121c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2122c9eb3e0fSArkadi Sharshevsky 	if (err)
2123c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2124c9eb3e0fSArkadi Sharshevsky 
212588e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
212688e4f0caSVivien Didelot 	if (err)
212788e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
212888e4f0caSVivien Didelot }
2129