xref: /openbmc/linux/net/dsa/slave.c (revision 7491894532341cff11babd1fe3bd68537166bcc4)
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>
18a71acad9SOleksij Rempel #include <net/selftests.h>
19f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
20b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2118596f50SGeorge McCollister #include <linux/if_hsr.h>
2204ff53f9SFlorian Fainelli #include <linux/netpoll.h>
23ea5dd34bSVivien Didelot 
2491da11f8SLennert Buytenhek #include "dsa_priv.h"
2591da11f8SLennert Buytenhek 
2691da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2791da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2891da11f8SLennert Buytenhek {
2991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3091da11f8SLennert Buytenhek 
310d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
329d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3391da11f8SLennert Buytenhek 
3491da11f8SLennert Buytenhek 	return 0xffff;
3591da11f8SLennert Buytenhek }
3691da11f8SLennert Buytenhek 
3791da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3891da11f8SLennert Buytenhek {
3991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4091da11f8SLennert Buytenhek 
410d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
429d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4391da11f8SLennert Buytenhek 
4491da11f8SLennert Buytenhek 	return 0;
4591da11f8SLennert Buytenhek }
4691da11f8SLennert Buytenhek 
4791da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4891da11f8SLennert Buytenhek {
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5291da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
530b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5449463b7fSVivien Didelot 		 ds->dst->index, ds->index);
55c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
5624df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5791da11f8SLennert Buytenhek }
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek 
6091da11f8SLennert Buytenhek /* slave device handling ****************************************************/
61abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
62c0840801SLennert Buytenhek {
63d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
64c0840801SLennert Buytenhek }
65c0840801SLennert Buytenhek 
6691da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6791da11f8SLennert Buytenhek {
68d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
69d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
70df02c6ffSLennert Buytenhek 	int err;
71df02c6ffSLennert Buytenhek 
729d5ef190SVladimir Oltean 	err = dev_open(master, NULL);
739d5ef190SVladimir Oltean 	if (err < 0) {
749d5ef190SVladimir Oltean 		netdev_err(dev, "failed to open master %s\n", master->name);
759d5ef190SVladimir Oltean 		goto out;
769d5ef190SVladimir Oltean 	}
77df02c6ffSLennert Buytenhek 
788feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
79a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
80df02c6ffSLennert Buytenhek 		if (err < 0)
81df02c6ffSLennert Buytenhek 			goto out;
82df02c6ffSLennert Buytenhek 	}
83df02c6ffSLennert Buytenhek 
84df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
85df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
86df02c6ffSLennert Buytenhek 		if (err < 0)
87df02c6ffSLennert Buytenhek 			goto del_unicast;
88df02c6ffSLennert Buytenhek 	}
89df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
90df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
91df02c6ffSLennert Buytenhek 		if (err < 0)
92df02c6ffSLennert Buytenhek 			goto clear_allmulti;
93df02c6ffSLennert Buytenhek 	}
94df02c6ffSLennert Buytenhek 
958640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
96b2f2af21SFlorian Fainelli 	if (err)
97b2f2af21SFlorian Fainelli 		goto clear_promisc;
98b73adef6SFlorian Fainelli 
9991da11f8SLennert Buytenhek 	return 0;
100df02c6ffSLennert Buytenhek 
101b2f2af21SFlorian Fainelli clear_promisc:
102b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1034fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
104df02c6ffSLennert Buytenhek clear_allmulti:
105df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
106df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
107df02c6ffSLennert Buytenhek del_unicast:
1088feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
109a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
110df02c6ffSLennert Buytenhek out:
111df02c6ffSLennert Buytenhek 	return err;
11291da11f8SLennert Buytenhek }
11391da11f8SLennert Buytenhek 
11491da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11591da11f8SLennert Buytenhek {
116d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
117d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
118df02c6ffSLennert Buytenhek 
1198640f8dcSRussell King 	dsa_port_disable_rt(dp);
1206457edfeSVivien Didelot 
121df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
122a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
123df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
124df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
125df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
126df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
127df02c6ffSLennert Buytenhek 
1288feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
129a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
130df02c6ffSLennert Buytenhek 
13191da11f8SLennert Buytenhek 	return 0;
13291da11f8SLennert Buytenhek }
13391da11f8SLennert Buytenhek 
13491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
13591da11f8SLennert Buytenhek {
136d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
13717ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
13891da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
13917ab4f61SRundong Ge 			dev_set_allmulti(master,
14017ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
14191da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
14217ab4f61SRundong Ge 			dev_set_promiscuity(master,
14317ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
14417ab4f61SRundong Ge 	}
14591da11f8SLennert Buytenhek }
14691da11f8SLennert Buytenhek 
14791da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
14891da11f8SLennert Buytenhek {
149d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
15091da11f8SLennert Buytenhek 
15191da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
152a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
15391da11f8SLennert Buytenhek }
15491da11f8SLennert Buytenhek 
155df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
15691da11f8SLennert Buytenhek {
157d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
158df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
159df02c6ffSLennert Buytenhek 	int err;
160df02c6ffSLennert Buytenhek 
161df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
162df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
163df02c6ffSLennert Buytenhek 
164df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
165df02c6ffSLennert Buytenhek 		goto out;
166df02c6ffSLennert Buytenhek 
1678feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
168a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
169df02c6ffSLennert Buytenhek 		if (err < 0)
170df02c6ffSLennert Buytenhek 			return err;
171df02c6ffSLennert Buytenhek 	}
172df02c6ffSLennert Buytenhek 
1738feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
174a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
175df02c6ffSLennert Buytenhek 
176df02c6ffSLennert Buytenhek out:
177d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
17891da11f8SLennert Buytenhek 
17991da11f8SLennert Buytenhek 	return 0;
18091da11f8SLennert Buytenhek }
18191da11f8SLennert Buytenhek 
1822bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1832bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1842bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1852bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1862bedde1aSArkadi Sharshevsky 	int idx;
1872bedde1aSArkadi Sharshevsky };
1882bedde1aSArkadi Sharshevsky 
1892bedde1aSArkadi Sharshevsky static int
1902bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1912bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1922bedde1aSArkadi Sharshevsky {
1932bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1942bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1952bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1962bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1972bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1982bedde1aSArkadi Sharshevsky 
1992bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2002bedde1aSArkadi Sharshevsky 		goto skip;
2012bedde1aSArkadi Sharshevsky 
2022bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2032bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2042bedde1aSArkadi Sharshevsky 	if (!nlh)
2052bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2062bedde1aSArkadi Sharshevsky 
2072bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2112bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2122bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2132bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2142bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2152bedde1aSArkadi Sharshevsky 
2162bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2172bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2182bedde1aSArkadi Sharshevsky 
2192bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2202bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky skip:
2252bedde1aSArkadi Sharshevsky 	dump->idx++;
2262bedde1aSArkadi Sharshevsky 	return 0;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky nla_put_failure:
2292bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2302bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2312bedde1aSArkadi Sharshevsky }
2322bedde1aSArkadi Sharshevsky 
2332bedde1aSArkadi Sharshevsky static int
2342bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2352bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2362bedde1aSArkadi Sharshevsky 		   int *idx)
2372bedde1aSArkadi Sharshevsky {
238d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2392bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2402bedde1aSArkadi Sharshevsky 		.dev = dev,
2412bedde1aSArkadi Sharshevsky 		.skb = skb,
2422bedde1aSArkadi Sharshevsky 		.cb = cb,
2432bedde1aSArkadi Sharshevsky 		.idx = *idx,
2442bedde1aSArkadi Sharshevsky 	};
2452bedde1aSArkadi Sharshevsky 	int err;
2462bedde1aSArkadi Sharshevsky 
247de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2482bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
249de40fc5dSVivien Didelot 
2502bedde1aSArkadi Sharshevsky 	return err;
2512bedde1aSArkadi Sharshevsky }
2522bedde1aSArkadi Sharshevsky 
25391da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25491da11f8SLennert Buytenhek {
2550336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2560336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2570336369dSBrandon Streiff 	int port = p->dp->index;
2580336369dSBrandon Streiff 
2590336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2600336369dSBrandon Streiff 	switch (cmd) {
2610336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2660336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2670336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2680336369dSBrandon Streiff 		break;
2690336369dSBrandon Streiff 	}
2700336369dSBrandon Streiff 
271aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
27291da11f8SLennert Buytenhek }
27391da11f8SLennert Buytenhek 
27469bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
2754c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
2764c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
27735636062SScott Feldman {
278d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
279b8d866acSVivien Didelot 	int ret;
28035636062SScott Feldman 
2810d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
2820d2cfbd4SVladimir Oltean 		return 0;
2830d2cfbd4SVladimir Oltean 
28435636062SScott Feldman 	switch (attr->id) {
2851f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
28603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
28703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
28803cbb870SVladimir Oltean 
289bae33f2bSVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state);
29035636062SScott Feldman 		break;
291fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
29203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
29303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
29403cbb870SVladimir Oltean 
29589153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
29689153ed6SVladimir Oltean 					      extack);
297fb2dabadSVivien Didelot 		break;
29834a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
29903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
30003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30103cbb870SVladimir Oltean 
302bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
30334a79f63SVivien Didelot 		break;
304ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
30503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
30603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30703cbb870SVladimir Oltean 
308a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
309a8b659e7SVladimir Oltean 						extack);
310ea87005aSFlorian Fainelli 		break;
31157652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
31203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
31303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
31403cbb870SVladimir Oltean 
315a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
31657652796SRussell King 		break;
31708cc83ccSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
31803cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
31903cbb870SVladimir Oltean 			return -EOPNOTSUPP;
32003cbb870SVladimir Oltean 
321a8b659e7SVladimir Oltean 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, extack);
32208cc83ccSVivien Didelot 		break;
32335636062SScott Feldman 	default:
32435636062SScott Feldman 		ret = -EOPNOTSUPP;
32535636062SScott Feldman 		break;
32635636062SScott Feldman 	}
32735636062SScott Feldman 
32835636062SScott Feldman 	return ret;
32935636062SScott Feldman }
33035636062SScott Feldman 
3311ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
3321ce39f0eSVladimir Oltean static int
3331ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
3341ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
3351ce39f0eSVladimir Oltean {
3361ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
3371ce39f0eSVladimir Oltean 	struct list_head *iter;
3381ce39f0eSVladimir Oltean 
3391ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
3401ce39f0eSVladimir Oltean 		u16 vid;
3411ce39f0eSVladimir Oltean 
3421ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
3431ce39f0eSVladimir Oltean 			continue;
3441ce39f0eSVladimir Oltean 
3451ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
346b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
3471ce39f0eSVladimir Oltean 			return -EBUSY;
3481ce39f0eSVladimir Oltean 	}
3491ce39f0eSVladimir Oltean 
3501ce39f0eSVladimir Oltean 	return 0;
3511ce39f0eSVladimir Oltean }
3521ce39f0eSVladimir Oltean 
353bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
3540ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
3550ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
356bdcff080SVivien Didelot {
3572209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
358bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
359bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
360b7a9e0daSVladimir Oltean 	int err;
361bdcff080SVivien Didelot 
3620ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
3630ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
364c5335d73SVivien Didelot 		return 0;
3650ee2af4eSVladimir Oltean 	}
366c5335d73SVivien Didelot 
367bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
368bdcff080SVivien Didelot 
3691ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
3701ce39f0eSVladimir Oltean 	 * the same VID.
3711ce39f0eSVladimir Oltean 	 */
372ffb68fc5SVladimir Oltean 	if (br_vlan_enabled(dp->bridge_dev)) {
3731ce39f0eSVladimir Oltean 		rcu_read_lock();
3741ce39f0eSVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
3751ce39f0eSVladimir Oltean 		rcu_read_unlock();
37631046a5fSVladimir Oltean 		if (err) {
37731046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
37831046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
3791ce39f0eSVladimir Oltean 			return err;
3801ce39f0eSVladimir Oltean 		}
38131046a5fSVladimir Oltean 	}
3821ce39f0eSVladimir Oltean 
38331046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp, &vlan, extack);
384bdcff080SVivien Didelot 	if (err)
385bdcff080SVivien Didelot 		return err;
386bdcff080SVivien Didelot 
387b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
388b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
389b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
390b9499904SVivien Didelot 	 */
391b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
392b9499904SVivien Didelot 
39331046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
3947e1741b4SVivien Didelot 	if (err)
3957e1741b4SVivien Didelot 		return err;
3967e1741b4SVivien Didelot 
397b7a9e0daSVladimir Oltean 	return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
398bdcff080SVivien Didelot }
399bdcff080SVivien Didelot 
40069bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
401648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
40279b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
403ba14d9ebSVivien Didelot {
404d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
405ba14d9ebSVivien Didelot 	int err;
406ba14d9ebSVivien Didelot 
4070d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4080d2cfbd4SVladimir Oltean 		return 0;
4090d2cfbd4SVladimir Oltean 
4109e8f4a54SJiri Pirko 	switch (obj->id) {
4118df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
41203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
41379b139f4SVivien Didelot 			return -EOPNOTSUPP;
41403cbb870SVladimir Oltean 
415ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4168df30255SVivien Didelot 		break;
4175f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
41803cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
41903cbb870SVladimir Oltean 			return -EOPNOTSUPP;
42003cbb870SVladimir Oltean 
4215f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4225f4dbc50SAndrew Lunn 		 * but on the CPU port.
4235f4dbc50SAndrew Lunn 		 */
424ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4255f4dbc50SAndrew Lunn 		break;
42657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
42703cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
42803cbb870SVladimir Oltean 			return -EOPNOTSUPP;
42903cbb870SVladimir Oltean 
4300ee2af4eSVladimir Oltean 		err = dsa_slave_vlan_add(dev, obj, extack);
43111149536SVivien Didelot 		break;
432c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
43303cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
434c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
43503cbb870SVladimir Oltean 
436c595c433SHoratiu Vultur 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
437c595c433SHoratiu Vultur 		break;
438c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
43903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
440c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
44103cbb870SVladimir Oltean 
442c595c433SHoratiu Vultur 		err = dsa_port_mrp_add_ring_role(dp,
443c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
444c595c433SHoratiu Vultur 		break;
445ba14d9ebSVivien Didelot 	default:
446ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
447ba14d9ebSVivien Didelot 		break;
448ba14d9ebSVivien Didelot 	}
449ba14d9ebSVivien Didelot 
450ba14d9ebSVivien Didelot 	return err;
451ba14d9ebSVivien Didelot }
452ba14d9ebSVivien Didelot 
453bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
454bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
455bdcff080SVivien Didelot {
4562209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
457bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4582209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
459b7a9e0daSVladimir Oltean 	int err;
460bdcff080SVivien Didelot 
46154a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
462c5335d73SVivien Didelot 		return 0;
463c5335d73SVivien Didelot 
4642209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
4652209158cSVladimir Oltean 
4667e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
4677e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
4687e1741b4SVivien Didelot 	 */
4692209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, vlan);
4702209158cSVladimir Oltean 	if (err)
4712209158cSVladimir Oltean 		return err;
4722209158cSVladimir Oltean 
473b7a9e0daSVladimir Oltean 	vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
4742209158cSVladimir Oltean 
4752209158cSVladimir Oltean 	return 0;
476bdcff080SVivien Didelot }
477bdcff080SVivien Didelot 
47869bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
479648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
480ba14d9ebSVivien Didelot {
481d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
482ba14d9ebSVivien Didelot 	int err;
483ba14d9ebSVivien Didelot 
4840d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4850d2cfbd4SVladimir Oltean 		return 0;
4860d2cfbd4SVladimir Oltean 
4879e8f4a54SJiri Pirko 	switch (obj->id) {
4888df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
48903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
49079b139f4SVivien Didelot 			return -EOPNOTSUPP;
49103cbb870SVladimir Oltean 
492bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4938df30255SVivien Didelot 		break;
4945f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
49503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
49603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
49703cbb870SVladimir Oltean 
4985f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4995f4dbc50SAndrew Lunn 		 * but on the CPU port.
5005f4dbc50SAndrew Lunn 		 */
5015f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
5025f4dbc50SAndrew Lunn 		break;
50357d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
50403cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
50503cbb870SVladimir Oltean 			return -EOPNOTSUPP;
50603cbb870SVladimir Oltean 
507bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
50811149536SVivien Didelot 		break;
509c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
51003cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
511c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
51203cbb870SVladimir Oltean 
513c595c433SHoratiu Vultur 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
514c595c433SHoratiu Vultur 		break;
515c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
51603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
517c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
51803cbb870SVladimir Oltean 
519c595c433SHoratiu Vultur 		err = dsa_port_mrp_del_ring_role(dp,
520c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
521c595c433SHoratiu Vultur 		break;
522ba14d9ebSVivien Didelot 	default:
523ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
524ba14d9ebSVivien Didelot 		break;
525ba14d9ebSVivien Didelot 	}
526ba14d9ebSVivien Didelot 
527ba14d9ebSVivien Didelot 	return err;
528ba14d9ebSVivien Didelot }
529ba14d9ebSVivien Didelot 
530929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
531929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
532b73adef6SFlorian Fainelli {
533d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
534d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
535a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
536b73adef6SFlorian Fainelli 
53715b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
53815b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
53915b04aceSJiri Pirko 	 * should be removed with legacy support.
54015b04aceSJiri Pirko 	 */
54115b04aceSJiri Pirko 	if (dp->ds->devlink)
54215b04aceSJiri Pirko 		return -EOPNOTSUPP;
54315b04aceSJiri Pirko 
544929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
545929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
546929d6c14SFlorian Fainelli 
547929d6c14SFlorian Fainelli 	return 0;
548929d6c14SFlorian Fainelli }
549929d6c14SFlorian Fainelli 
5504fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
55104ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
55204ff53f9SFlorian Fainelli {
55304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
5544fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
5554fa7b718SVivien Didelot 
556f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
55704ff53f9SFlorian Fainelli #else
55804ff53f9SFlorian Fainelli 	BUG();
55904ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
560f78ed220SEric Dumazet #endif
56104ff53f9SFlorian Fainelli }
56204ff53f9SFlorian Fainelli 
56390af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
56490af1059SBrandon Streiff 				 struct sk_buff *skb)
56590af1059SBrandon Streiff {
56690af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
56790af1059SBrandon Streiff 
568cfd12c06SYangbo Lu 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
569cfd12c06SYangbo Lu 		return;
570cfd12c06SYangbo Lu 
57190af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
57290af1059SBrandon Streiff 		return;
57390af1059SBrandon Streiff 
5745c5416f5SYangbo Lu 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
57590af1059SBrandon Streiff }
57690af1059SBrandon Streiff 
57797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
57897a69a0dSVladimir Oltean {
57997a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
58097a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
58197a69a0dSVladimir Oltean 	 */
58297a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
58397a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
58497a69a0dSVladimir Oltean 
58597a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
58697a69a0dSVladimir Oltean 	 * do not modify its EtherType
58797a69a0dSVladimir Oltean 	 */
58897a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
58997a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
59097a69a0dSVladimir Oltean 
59197a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
59297a69a0dSVladimir Oltean }
59397a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
59497a69a0dSVladimir Oltean 
595a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
596a3b0b647SVladimir Oltean {
597a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
598a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
599a3b0b647SVladimir Oltean 
600a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
601a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
602a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
603a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
604a3b0b647SVladimir Oltean 	 */
605a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
606a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
607a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
608a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
609a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
610a3b0b647SVladimir Oltean 
611a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
612a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
613a3b0b647SVladimir Oltean 		return 0;
614a3b0b647SVladimir Oltean 
615a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
616a3b0b647SVladimir Oltean 				GFP_ATOMIC);
617a3b0b647SVladimir Oltean }
618a3b0b647SVladimir Oltean 
6193e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6203e8a72d1SFlorian Fainelli {
6213e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6224ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6233e8a72d1SFlorian Fainelli 
6246a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
6253e8a72d1SFlorian Fainelli 
626c4b364ceSYangbo Lu 	memset(skb->cb, 0, sizeof(skb->cb));
62787671375SVladimir Oltean 
628cf536ea3SYangbo Lu 	/* Handle tx timestamp if any */
62990af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
63090af1059SBrandon Streiff 
631a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
632a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
633a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
634a3b0b647SVladimir Oltean 	}
635a3b0b647SVladimir Oltean 
636a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
637a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
638a3b0b647SVladimir Oltean 	 */
639a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
640a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
641a3b0b647SVladimir Oltean 
642fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
643fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
644fe47d563SVivien Didelot 	 */
6454ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
646fe47d563SVivien Didelot 	if (!nskb) {
647fe47d563SVivien Didelot 		kfree_skb(skb);
6484ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
649fe47d563SVivien Didelot 	}
6505aed85ceSFlorian Fainelli 
65197a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
65297a69a0dSVladimir Oltean }
65304ff53f9SFlorian Fainelli 
65491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
65591da11f8SLennert Buytenhek 
65691da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
65791da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
65891da11f8SLennert Buytenhek {
6597826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6607826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6617826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
66291da11f8SLennert Buytenhek }
66391da11f8SLennert Buytenhek 
6643d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6653d762a0fSGuenter Roeck {
666d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
667d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6683d762a0fSGuenter Roeck 
6699d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
670d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
6713d762a0fSGuenter Roeck 
6723d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6733d762a0fSGuenter Roeck }
6743d762a0fSGuenter Roeck 
6753d762a0fSGuenter Roeck static void
6763d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6773d762a0fSGuenter Roeck {
678d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
679d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6803d762a0fSGuenter Roeck 
6819d490b4eSVivien Didelot 	if (ds->ops->get_regs)
682d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
6833d762a0fSGuenter Roeck }
6843d762a0fSGuenter Roeck 
685aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
686aab9c406SFlorian Fainelli {
687aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
688aab9c406SFlorian Fainelli 
689aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
690aab9c406SFlorian Fainelli }
691aab9c406SFlorian Fainelli 
6926793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6936793abb4SGuenter Roeck {
694d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
695d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6966793abb4SGuenter Roeck 
6970e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
698ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
6996793abb4SGuenter Roeck 
7009d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
7019d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
7026793abb4SGuenter Roeck 
7036793abb4SGuenter Roeck 	return 0;
7046793abb4SGuenter Roeck }
7056793abb4SGuenter Roeck 
7066793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
7076793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7086793abb4SGuenter Roeck {
709d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
710d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7116793abb4SGuenter Roeck 
7129d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7139d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7146793abb4SGuenter Roeck 
7156793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7166793abb4SGuenter Roeck }
7176793abb4SGuenter Roeck 
7186793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7196793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7206793abb4SGuenter Roeck {
721d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
722d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7236793abb4SGuenter Roeck 
7249d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7259d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7266793abb4SGuenter Roeck 
7276793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7286793abb4SGuenter Roeck }
7296793abb4SGuenter Roeck 
73091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
73191da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
73291da11f8SLennert Buytenhek {
733d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
734d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
73591da11f8SLennert Buytenhek 
73691da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
73791da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
73891da11f8SLennert Buytenhek 
73991da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
74091da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
74191da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
74291da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
7439d490b4eSVivien Didelot 		if (ds->ops->get_strings)
74489f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
74589f09048SFlorian Fainelli 					     data + 4 * len);
746a71acad9SOleksij Rempel 	} else if (stringset ==  ETH_SS_TEST) {
747a71acad9SOleksij Rempel 		net_selftest_get_strings(data);
74891da11f8SLennert Buytenhek 	}
749a71acad9SOleksij Rempel 
75091da11f8SLennert Buytenhek }
75191da11f8SLennert Buytenhek 
75291da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
75391da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
75491da11f8SLennert Buytenhek 					uint64_t *data)
75591da11f8SLennert Buytenhek {
756d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
757d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7585f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
759f613ed66SFlorian Fainelli 	unsigned int start;
7605f6b4e14SFlorian Fainelli 	int i;
76191da11f8SLennert Buytenhek 
7625f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
7635f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
7645f6b4e14SFlorian Fainelli 
7656a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
766f613ed66SFlorian Fainelli 		do {
7675f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
7685f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
7695f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
7705f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
7715f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
7725f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
7735f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
7745f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
7755f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
7765f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
7775f6b4e14SFlorian Fainelli 	}
7789d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
779d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
78091da11f8SLennert Buytenhek }
78191da11f8SLennert Buytenhek 
78291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
78391da11f8SLennert Buytenhek {
784d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
785d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
78691da11f8SLennert Buytenhek 
78791da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
788b94cbc90SVladimir Oltean 		int count = 0;
78991da11f8SLennert Buytenhek 
790b94cbc90SVladimir Oltean 		if (ds->ops->get_sset_count) {
791b94cbc90SVladimir Oltean 			count = ds->ops->get_sset_count(ds, dp->index, sset);
792b94cbc90SVladimir Oltean 			if (count < 0)
79391da11f8SLennert Buytenhek 				return count;
794b94cbc90SVladimir Oltean 		}
795b94cbc90SVladimir Oltean 
796b94cbc90SVladimir Oltean 		return count + 4;
797a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
798a71acad9SOleksij Rempel 		return net_selftest_get_count();
79991da11f8SLennert Buytenhek 	}
80091da11f8SLennert Buytenhek 
80191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
80291da11f8SLennert Buytenhek }
80391da11f8SLennert Buytenhek 
804a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
805a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
806a71acad9SOleksij Rempel {
807a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
808a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
809a71acad9SOleksij Rempel 
810a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
811a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
812a71acad9SOleksij Rempel 		return;
813a71acad9SOleksij Rempel 	}
814a71acad9SOleksij Rempel 
815a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
816a71acad9SOleksij Rempel }
817a71acad9SOleksij Rempel 
81819e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
81919e57c4eSFlorian Fainelli {
820d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
821d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
82219e57c4eSFlorian Fainelli 
823aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
824aab9c406SFlorian Fainelli 
8259d490b4eSVivien Didelot 	if (ds->ops->get_wol)
826d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
82719e57c4eSFlorian Fainelli }
82819e57c4eSFlorian Fainelli 
82919e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
83019e57c4eSFlorian Fainelli {
831d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
832d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
83319e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
83419e57c4eSFlorian Fainelli 
835aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
836aab9c406SFlorian Fainelli 
8379d490b4eSVivien Didelot 	if (ds->ops->set_wol)
838d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
83919e57c4eSFlorian Fainelli 
84019e57c4eSFlorian Fainelli 	return ret;
84119e57c4eSFlorian Fainelli }
84219e57c4eSFlorian Fainelli 
8437905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
8447905288fSFlorian Fainelli {
845d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
846d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8477905288fSFlorian Fainelli 	int ret;
8487905288fSFlorian Fainelli 
8497b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
85000670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8517b9cc738SVivien Didelot 		return -ENODEV;
8527b9cc738SVivien Didelot 
85308f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
8547905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8557905288fSFlorian Fainelli 
856d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
8577905288fSFlorian Fainelli 	if (ret)
8587905288fSFlorian Fainelli 		return ret;
8597905288fSFlorian Fainelli 
860aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
8617905288fSFlorian Fainelli }
8627905288fSFlorian Fainelli 
8637905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8647905288fSFlorian Fainelli {
865d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
866d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8677905288fSFlorian Fainelli 	int ret;
8687905288fSFlorian Fainelli 
8697b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
87000670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8717b9cc738SVivien Didelot 		return -ENODEV;
8727b9cc738SVivien Didelot 
87308f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8747905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8757905288fSFlorian Fainelli 
876d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8777905288fSFlorian Fainelli 	if (ret)
8787905288fSFlorian Fainelli 		return ret;
8797905288fSFlorian Fainelli 
880aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
881aab9c406SFlorian Fainelli }
882aab9c406SFlorian Fainelli 
883aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
884aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
885aab9c406SFlorian Fainelli {
886aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
887aab9c406SFlorian Fainelli 
888aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
889aab9c406SFlorian Fainelli }
890aab9c406SFlorian Fainelli 
891aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
892aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
893aab9c406SFlorian Fainelli {
894aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
895aab9c406SFlorian Fainelli 
896aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8977905288fSFlorian Fainelli }
8987905288fSFlorian Fainelli 
899a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
900a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
901a2a1a13bSHeiner Kallweit {
902a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
903a2a1a13bSHeiner Kallweit 
904a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
905a2a1a13bSHeiner Kallweit }
906a2a1a13bSHeiner Kallweit 
907a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
908a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
909a2a1a13bSHeiner Kallweit {
910a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
911a2a1a13bSHeiner Kallweit 
912a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
913a2a1a13bSHeiner Kallweit }
914a2a1a13bSHeiner Kallweit 
91504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
91604ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
91704ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
91804ff53f9SFlorian Fainelli {
919d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
92004ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
92104ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
92204ff53f9SFlorian Fainelli 	int err = 0;
92304ff53f9SFlorian Fainelli 
92404ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
92504ff53f9SFlorian Fainelli 	if (!netpoll)
92604ff53f9SFlorian Fainelli 		return -ENOMEM;
92704ff53f9SFlorian Fainelli 
92804ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
92904ff53f9SFlorian Fainelli 	if (err) {
93004ff53f9SFlorian Fainelli 		kfree(netpoll);
93104ff53f9SFlorian Fainelli 		goto out;
93204ff53f9SFlorian Fainelli 	}
93304ff53f9SFlorian Fainelli 
93404ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
93504ff53f9SFlorian Fainelli out:
93604ff53f9SFlorian Fainelli 	return err;
93704ff53f9SFlorian Fainelli }
93804ff53f9SFlorian Fainelli 
93904ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
94004ff53f9SFlorian Fainelli {
94104ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
94204ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
94304ff53f9SFlorian Fainelli 
94404ff53f9SFlorian Fainelli 	if (!netpoll)
94504ff53f9SFlorian Fainelli 		return;
94604ff53f9SFlorian Fainelli 
94704ff53f9SFlorian Fainelli 	p->netpoll = NULL;
94804ff53f9SFlorian Fainelli 
949c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
95004ff53f9SFlorian Fainelli }
95104ff53f9SFlorian Fainelli 
95204ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
95304ff53f9SFlorian Fainelli {
95404ff53f9SFlorian Fainelli }
95504ff53f9SFlorian Fainelli #endif
95604ff53f9SFlorian Fainelli 
95744bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
95844bb765cSFlorian Fainelli 					char *name, size_t len)
95944bb765cSFlorian Fainelli {
960d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
96144bb765cSFlorian Fainelli 
962d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
963d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
964d484210bSJiri Pirko 	 * should be removed with legacy support.
965d484210bSJiri Pirko 	 */
966d484210bSJiri Pirko 	if (dp->ds->devlink)
967d484210bSJiri Pirko 		return -EOPNOTSUPP;
968d484210bSJiri Pirko 
969d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
97044bb765cSFlorian Fainelli 		return -EINVAL;
9713a543ef4SFlorian Fainelli 
9723a543ef4SFlorian Fainelli 	return 0;
9733a543ef4SFlorian Fainelli }
9743a543ef4SFlorian Fainelli 
975f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9764fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
977f50f2127SFlorian Fainelli {
9784fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
979f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
980f50f2127SFlorian Fainelli 
981f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
982f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
983f50f2127SFlorian Fainelli 			return mall_tc_entry;
984f50f2127SFlorian Fainelli 
985f50f2127SFlorian Fainelli 	return NULL;
986f50f2127SFlorian Fainelli }
987f50f2127SFlorian Fainelli 
988e13c2075SVladimir Oltean static int
989e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
990f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
991f50f2127SFlorian Fainelli 				  bool ingress)
992f50f2127SFlorian Fainelli {
993d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
994f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
995e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
996f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
997d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9989681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
999d945097bSVivien Didelot 	struct dsa_port *to_dp;
1000e13c2075SVladimir Oltean 	int err;
1001e13c2075SVladimir Oltean 
1002f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
100334297176SVladimir Oltean 		return -EOPNOTSUPP;
1004f50f2127SFlorian Fainelli 
100553eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1006319a1d19SJiri Pirko 					      cls->common.extack))
100734297176SVladimir Oltean 		return -EOPNOTSUPP;
1008319a1d19SJiri Pirko 
10099681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
1010f50f2127SFlorian Fainelli 
101165722159SVladimir Oltean 	if (!act->dev)
101265722159SVladimir Oltean 		return -EINVAL;
101365722159SVladimir Oltean 
10149681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1015f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1016f50f2127SFlorian Fainelli 
1017f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1018f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1019f50f2127SFlorian Fainelli 		return -ENOMEM;
1020f50f2127SFlorian Fainelli 
1021f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1022f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1023f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1024f50f2127SFlorian Fainelli 
10259681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1026f50f2127SFlorian Fainelli 
1027d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1028f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1029f50f2127SFlorian Fainelli 
1030d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1031f50f2127SFlorian Fainelli 	if (err) {
1032f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1033f50f2127SFlorian Fainelli 		return err;
1034f50f2127SFlorian Fainelli 	}
1035f50f2127SFlorian Fainelli 
1036f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1037e13c2075SVladimir Oltean 
1038e13c2075SVladimir Oltean 	return err;
1039f50f2127SFlorian Fainelli }
1040f50f2127SFlorian Fainelli 
104134297176SVladimir Oltean static int
104234297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
104334297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
104434297176SVladimir Oltean 				  bool ingress)
104534297176SVladimir Oltean {
104634297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
104734297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
104834297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
104934297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
105034297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
105134297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
105234297176SVladimir Oltean 	struct flow_action_entry *act;
105334297176SVladimir Oltean 	int err;
105434297176SVladimir Oltean 
105534297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
105634297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1057c75a33c8SJacob Keller 				   "Policing offload not implemented");
105834297176SVladimir Oltean 		return -EOPNOTSUPP;
105934297176SVladimir Oltean 	}
106034297176SVladimir Oltean 
106134297176SVladimir Oltean 	if (!ingress) {
106234297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1063c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
106434297176SVladimir Oltean 		return -EOPNOTSUPP;
106534297176SVladimir Oltean 	}
106634297176SVladimir Oltean 
106734297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
106834297176SVladimir Oltean 					      cls->common.extack))
106934297176SVladimir Oltean 		return -EOPNOTSUPP;
107034297176SVladimir Oltean 
107134297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
107234297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
107334297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1074c75a33c8SJacob Keller 					   "Only one port policer allowed");
107534297176SVladimir Oltean 			return -EEXIST;
107634297176SVladimir Oltean 		}
107734297176SVladimir Oltean 	}
107834297176SVladimir Oltean 
107934297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
108034297176SVladimir Oltean 
108134297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
108234297176SVladimir Oltean 	if (!mall_tc_entry)
108334297176SVladimir Oltean 		return -ENOMEM;
108434297176SVladimir Oltean 
108534297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
108634297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
108734297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
108834297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
108934297176SVladimir Oltean 	policer->burst = act->police.burst;
109034297176SVladimir Oltean 
109134297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
109234297176SVladimir Oltean 	if (err) {
109334297176SVladimir Oltean 		kfree(mall_tc_entry);
109434297176SVladimir Oltean 		return err;
109534297176SVladimir Oltean 	}
109634297176SVladimir Oltean 
109734297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
109834297176SVladimir Oltean 
109934297176SVladimir Oltean 	return err;
110034297176SVladimir Oltean }
110134297176SVladimir Oltean 
1102e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1103e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1104e13c2075SVladimir Oltean 				      bool ingress)
1105e13c2075SVladimir Oltean {
1106e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1107e13c2075SVladimir Oltean 
1108e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1109e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1110e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1111e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
111234297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
111334297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
111434297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1115e13c2075SVladimir Oltean 
1116e13c2075SVladimir Oltean 	return err;
1117f50f2127SFlorian Fainelli }
1118f50f2127SFlorian Fainelli 
1119f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1120f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1121f50f2127SFlorian Fainelli {
1122d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1123f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1124d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1125f50f2127SFlorian Fainelli 
11264fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1127f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1128f50f2127SFlorian Fainelli 		return;
1129f50f2127SFlorian Fainelli 
1130f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1131f50f2127SFlorian Fainelli 
1132f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1133f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
113434297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
113534297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
113634297176SVladimir Oltean 						 &mall_tc_entry->mirror);
113734297176SVladimir Oltean 		break;
113834297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
113934297176SVladimir Oltean 		if (ds->ops->port_policer_del)
114034297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1141f50f2127SFlorian Fainelli 		break;
1142f50f2127SFlorian Fainelli 	default:
1143f50f2127SFlorian Fainelli 		WARN_ON(1);
1144f50f2127SFlorian Fainelli 	}
1145f50f2127SFlorian Fainelli 
1146f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1147f50f2127SFlorian Fainelli }
1148f50f2127SFlorian Fainelli 
11493fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
11506b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
11516b3eb752SJiri Pirko 					   bool ingress)
1152f50f2127SFlorian Fainelli {
11535fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1154a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1155f50f2127SFlorian Fainelli 
11563fbae382SJiri Pirko 	switch (cls->command) {
11573fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
11585fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
11593fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
11603fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
11613fbae382SJiri Pirko 		return 0;
11623fbae382SJiri Pirko 	default:
11633fbae382SJiri Pirko 		return -EOPNOTSUPP;
11643fbae382SJiri Pirko 	}
11653fbae382SJiri Pirko }
11663fbae382SJiri Pirko 
1167ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1168ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1169ed11bb1fSVladimir Oltean 				    bool ingress)
1170ed11bb1fSVladimir Oltean {
1171ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1172ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1173ed11bb1fSVladimir Oltean 	int port = dp->index;
1174ed11bb1fSVladimir Oltean 
1175ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1176ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1177ed11bb1fSVladimir Oltean 
1178ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1179ed11bb1fSVladimir Oltean }
1180ed11bb1fSVladimir Oltean 
1181ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1182ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1183ed11bb1fSVladimir Oltean 				    bool ingress)
1184ed11bb1fSVladimir Oltean {
1185ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1186ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1187ed11bb1fSVladimir Oltean 	int port = dp->index;
1188ed11bb1fSVladimir Oltean 
1189ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1190ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1191ed11bb1fSVladimir Oltean 
1192ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1193ed11bb1fSVladimir Oltean }
1194ed11bb1fSVladimir Oltean 
1195ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1196ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1197ed11bb1fSVladimir Oltean 				      bool ingress)
1198ed11bb1fSVladimir Oltean {
1199ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1200ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1201ed11bb1fSVladimir Oltean 	int port = dp->index;
1202ed11bb1fSVladimir Oltean 
1203ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1204ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1205ed11bb1fSVladimir Oltean 
1206ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1207ed11bb1fSVladimir Oltean }
1208ed11bb1fSVladimir Oltean 
1209ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1210ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1211ed11bb1fSVladimir Oltean 					 bool ingress)
1212ed11bb1fSVladimir Oltean {
1213ed11bb1fSVladimir Oltean 	switch (cls->command) {
1214ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1215ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1216ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1217ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1218ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1219ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1220ed11bb1fSVladimir Oltean 	default:
1221ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1222ed11bb1fSVladimir Oltean 	}
1223ed11bb1fSVladimir Oltean }
1224ed11bb1fSVladimir Oltean 
12256b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12266b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
12276b3eb752SJiri Pirko {
12286b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
12296b3eb752SJiri Pirko 
123044ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
123144ae12a7SJiri Pirko 		return -EOPNOTSUPP;
123244ae12a7SJiri Pirko 
12336b3eb752SJiri Pirko 	switch (type) {
12346b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
12356b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1236ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1237ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
12386b3eb752SJiri Pirko 	default:
12396b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12406b3eb752SJiri Pirko 	}
12416b3eb752SJiri Pirko }
12426b3eb752SJiri Pirko 
12436b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
12446b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12456b3eb752SJiri Pirko {
12466b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
12476b3eb752SJiri Pirko }
12486b3eb752SJiri Pirko 
12496b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
12506b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12516b3eb752SJiri Pirko {
12526b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
12536b3eb752SJiri Pirko }
12546b3eb752SJiri Pirko 
1255955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1256955bcb6eSPablo Neira Ayuso 
12576b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1258955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
12596b3eb752SJiri Pirko {
1260955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1261a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
12626b3eb752SJiri Pirko 
126332f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12646b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
126532f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12666b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12676b3eb752SJiri Pirko 	else
12686b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12696b3eb752SJiri Pirko 
1270955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1271955bcb6eSPablo Neira Ayuso 
12726b3eb752SJiri Pirko 	switch (f->command) {
12739c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12740d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12750d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12760d4fd02eSPablo Neira Ayuso 
12770c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1278955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1279955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1280955bcb6eSPablo Neira Ayuso 
1281955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1282955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1283955bcb6eSPablo Neira Ayuso 		return 0;
12849c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
128514bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1286955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1287955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1288955bcb6eSPablo Neira Ayuso 
1289955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1290955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12916b3eb752SJiri Pirko 		return 0;
12926b3eb752SJiri Pirko 	default:
12936b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12946b3eb752SJiri Pirko 	}
12956b3eb752SJiri Pirko }
12966b3eb752SJiri Pirko 
12973fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
12983fb24a43SPablo Neira Ayuso 				    void *type_data)
12993fb24a43SPablo Neira Ayuso {
13003fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
13013fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
13023fb24a43SPablo Neira Ayuso 
13033fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
13043fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
13053fb24a43SPablo Neira Ayuso 
13063fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
13073fb24a43SPablo Neira Ayuso }
13083fb24a43SPablo Neira Ayuso 
13093fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1310de4784caSJiri Pirko 			      void *type_data)
13113fbae382SJiri Pirko {
131247d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
131347d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
131447d23af2SVladimir Oltean 
13153fb24a43SPablo Neira Ayuso 	switch (type) {
13163fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
13176b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
13183fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
13193fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
13203fb24a43SPablo Neira Ayuso 	default:
13213fb24a43SPablo Neira Ayuso 		break;
13223fb24a43SPablo Neira Ayuso 	}
132347d23af2SVladimir Oltean 
132447d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1325a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
132647d23af2SVladimir Oltean 
132747d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1328f50f2127SFlorian Fainelli }
1329f50f2127SFlorian Fainelli 
1330bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1331bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1332bf9f2648SFlorian Fainelli {
1333d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1334d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1335bf9f2648SFlorian Fainelli 
1336bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1337bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1338bf9f2648SFlorian Fainelli 
1339d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1340bf9f2648SFlorian Fainelli }
1341bf9f2648SFlorian Fainelli 
1342bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1343bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1344bf9f2648SFlorian Fainelli {
1345d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1346d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1347bf9f2648SFlorian Fainelli 
1348bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1349bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1350bf9f2648SFlorian Fainelli 
1351d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1352bf9f2648SFlorian Fainelli }
1353bf9f2648SFlorian Fainelli 
13540336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
13550336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
13560336369dSBrandon Streiff {
13570336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
13580336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
13590336369dSBrandon Streiff 
13600336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
13610336369dSBrandon Streiff 		return -EOPNOTSUPP;
13620336369dSBrandon Streiff 
13630336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
13640336369dSBrandon Streiff }
13650336369dSBrandon Streiff 
1366061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1367061f6a50SFlorian Fainelli 				     u16 vid)
1368061f6a50SFlorian Fainelli {
13692209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1370061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
137188236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
137288236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1373b7a9e0daSVladimir Oltean 		.vid = vid,
137488236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
137588236591SVladimir Oltean 		.flags = 0,
137688236591SVladimir Oltean 	};
137731046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1378061f6a50SFlorian Fainelli 	int ret;
1379061f6a50SFlorian Fainelli 
138088236591SVladimir Oltean 	/* User port... */
138131046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
138231046a5fSVladimir Oltean 	if (ret) {
138331046a5fSVladimir Oltean 		if (extack._msg)
138431046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
138588236591SVladimir Oltean 		return ret;
138631046a5fSVladimir Oltean 	}
138788236591SVladimir Oltean 
138888236591SVladimir Oltean 	/* And CPU port... */
138931046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
139031046a5fSVladimir Oltean 	if (ret) {
139131046a5fSVladimir Oltean 		if (extack._msg)
139231046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
139331046a5fSVladimir Oltean 				   extack._msg);
13947e1741b4SVivien Didelot 		return ret;
139531046a5fSVladimir Oltean 	}
13967e1741b4SVivien Didelot 
13972209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1398061f6a50SFlorian Fainelli }
1399061f6a50SFlorian Fainelli 
1400061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1401061f6a50SFlorian Fainelli 				      u16 vid)
1402061f6a50SFlorian Fainelli {
14032209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1404061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
140588236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1406b7a9e0daSVladimir Oltean 		.vid = vid,
140788236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
140888236591SVladimir Oltean 		.flags = 0,
140988236591SVladimir Oltean 	};
14102209158cSVladimir Oltean 	int err;
1411061f6a50SFlorian Fainelli 
14127e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
14137e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
14147e1741b4SVivien Didelot 	 */
14152209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
14162209158cSVladimir Oltean 	if (err)
14172209158cSVladimir Oltean 		return err;
14182209158cSVladimir Oltean 
14192209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
14202209158cSVladimir Oltean 
14212209158cSVladimir Oltean 	return 0;
1422061f6a50SFlorian Fainelli }
1423061f6a50SFlorian Fainelli 
1424bff33f7eSVladimir Oltean struct dsa_hw_port {
1425bff33f7eSVladimir Oltean 	struct list_head list;
1426bff33f7eSVladimir Oltean 	struct net_device *dev;
1427bff33f7eSVladimir Oltean 	int old_mtu;
1428bff33f7eSVladimir Oltean };
1429bff33f7eSVladimir Oltean 
1430bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1431bff33f7eSVladimir Oltean {
1432bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1433bff33f7eSVladimir Oltean 	int err;
1434bff33f7eSVladimir Oltean 
1435bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1436bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1437bff33f7eSVladimir Oltean 			continue;
1438bff33f7eSVladimir Oltean 
1439bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1440bff33f7eSVladimir Oltean 		if (err)
1441bff33f7eSVladimir Oltean 			goto rollback;
1442bff33f7eSVladimir Oltean 	}
1443bff33f7eSVladimir Oltean 
1444bff33f7eSVladimir Oltean 	return 0;
1445bff33f7eSVladimir Oltean 
1446bff33f7eSVladimir Oltean rollback:
1447bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1448bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1449bff33f7eSVladimir Oltean 			continue;
1450bff33f7eSVladimir Oltean 
1451bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1452bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1453bff33f7eSVladimir Oltean 	}
1454bff33f7eSVladimir Oltean 
1455bff33f7eSVladimir Oltean 	return err;
1456bff33f7eSVladimir Oltean }
1457bff33f7eSVladimir Oltean 
1458bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1459bff33f7eSVladimir Oltean {
1460bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1461bff33f7eSVladimir Oltean 
1462bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1463bff33f7eSVladimir Oltean 		kfree(p);
1464bff33f7eSVladimir Oltean }
1465bff33f7eSVladimir Oltean 
1466bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1467bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1468bff33f7eSVladimir Oltean {
1469bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1470bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1471bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1472bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1473bff33f7eSVladimir Oltean 	int err;
1474bff33f7eSVladimir Oltean 
1475bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1476bff33f7eSVladimir Oltean 		return;
1477bff33f7eSVladimir Oltean 
1478bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1479bff33f7eSVladimir Oltean 		return;
1480bff33f7eSVladimir Oltean 
1481bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1482bff33f7eSVladimir Oltean 
1483bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1484bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1485bff33f7eSVladimir Oltean 	 */
1486bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1487bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1488bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1489bff33f7eSVladimir Oltean 			struct net_device *slave;
1490bff33f7eSVladimir Oltean 
1491bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1492bff33f7eSVladimir Oltean 				continue;
1493bff33f7eSVladimir Oltean 
1494bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1495bff33f7eSVladimir Oltean 				continue;
1496bff33f7eSVladimir Oltean 
1497bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1498bff33f7eSVladimir Oltean 				continue;
1499bff33f7eSVladimir Oltean 
1500bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1501bff33f7eSVladimir Oltean 
1502bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1503bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1504bff33f7eSVladimir Oltean 
1505bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1506bff33f7eSVladimir Oltean 			if (!hw_port)
1507bff33f7eSVladimir Oltean 				goto out;
1508bff33f7eSVladimir Oltean 
1509bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1510bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1511bff33f7eSVladimir Oltean 
1512bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1513bff33f7eSVladimir Oltean 		}
1514bff33f7eSVladimir Oltean 	}
1515bff33f7eSVladimir Oltean 
1516bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1517bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1518bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1519bff33f7eSVladimir Oltean 	 */
1520bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1521bff33f7eSVladimir Oltean 	if (!err)
1522bff33f7eSVladimir Oltean 		goto out;
1523bff33f7eSVladimir Oltean 
1524bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1525bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1526bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1527bff33f7eSVladimir Oltean 	 */
1528bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1529bff33f7eSVladimir Oltean 
1530bff33f7eSVladimir Oltean out:
1531bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1532bff33f7eSVladimir Oltean }
1533bff33f7eSVladimir Oltean 
153453da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1535bfcb8132SVladimir Oltean {
1536bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1537bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1538bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1539bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
15404e4ab795SVladimir Oltean 	struct dsa_port *dp_iter;
1541bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1542bfcb8132SVladimir Oltean 	int port = p->dp->index;
1543bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1544bfcb8132SVladimir Oltean 	int new_master_mtu;
1545bfcb8132SVladimir Oltean 	int old_master_mtu;
1546bfcb8132SVladimir Oltean 	int mtu_limit;
1547bfcb8132SVladimir Oltean 	int cpu_mtu;
15484e4ab795SVladimir Oltean 	int err;
1549bfcb8132SVladimir Oltean 
1550bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1551bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1552bfcb8132SVladimir Oltean 
15534e4ab795SVladimir Oltean 	list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1554bfcb8132SVladimir Oltean 		int slave_mtu;
1555bfcb8132SVladimir Oltean 
15564e4ab795SVladimir Oltean 		if (!dsa_port_is_user(dp_iter))
1557bfcb8132SVladimir Oltean 			continue;
1558bfcb8132SVladimir Oltean 
1559bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1560bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1561bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1562bfcb8132SVladimir Oltean 		 */
15634e4ab795SVladimir Oltean 		if (!dp_iter->slave)
1564bfcb8132SVladimir Oltean 			continue;
1565bfcb8132SVladimir Oltean 
1566bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1567bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1568bfcb8132SVladimir Oltean 		 */
15694e4ab795SVladimir Oltean 		if (dp_iter == dp)
1570bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1571bfcb8132SVladimir Oltean 		else
15724e4ab795SVladimir Oltean 			slave_mtu = dp_iter->slave->mtu;
1573bfcb8132SVladimir Oltean 
1574bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1575bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1576bfcb8132SVladimir Oltean 	}
1577bfcb8132SVladimir Oltean 
1578bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1579bfcb8132SVladimir Oltean 
1580bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1581bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
15824e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1583bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1584bfcb8132SVladimir Oltean 		return -ERANGE;
1585bfcb8132SVladimir Oltean 
1586bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1587bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1588bfcb8132SVladimir Oltean 	 */
1589bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1590bfcb8132SVladimir Oltean 
1591bfcb8132SVladimir Oltean 	/* Start applying stuff */
1592bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1593bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1594bfcb8132SVladimir Oltean 		if (err < 0)
1595bfcb8132SVladimir Oltean 			goto out_master_failed;
1596bfcb8132SVladimir Oltean 
1597bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
159888faba20SVladimir Oltean 		 * upstream switches, so create a non-targeted notifier which
159988faba20SVladimir Oltean 		 * updates all switches.
1600bfcb8132SVladimir Oltean 		 */
160188faba20SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1602bfcb8132SVladimir Oltean 		if (err)
1603bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1604bfcb8132SVladimir Oltean 	}
1605bfcb8132SVladimir Oltean 
160688faba20SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, true);
1607bfcb8132SVladimir Oltean 	if (err)
1608bfcb8132SVladimir Oltean 		goto out_port_failed;
1609bfcb8132SVladimir Oltean 
1610bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1611bfcb8132SVladimir Oltean 
1612bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1613bff33f7eSVladimir Oltean 
1614bfcb8132SVladimir Oltean 	return 0;
1615bfcb8132SVladimir Oltean 
1616bfcb8132SVladimir Oltean out_port_failed:
1617bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1618bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
16194e500251SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops),
162088faba20SVladimir Oltean 				    false);
1621bfcb8132SVladimir Oltean out_cpu_failed:
1622bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1623bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1624bfcb8132SVladimir Oltean out_master_failed:
1625bfcb8132SVladimir Oltean 	return err;
1626bfcb8132SVladimir Oltean }
1627bfcb8132SVladimir Oltean 
162891da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
162991da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
16303d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
16313d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1632aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1633c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
16346793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
16356793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
16366793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
163791da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
163891da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
163991da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
164019e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
164119e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
16427905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
16437905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1644aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1645aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1646a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1647a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1648bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1649bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
16500336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
1651a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
165291da11f8SLennert Buytenhek };
165391da11f8SLennert Buytenhek 
16542a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
16554b9c9358SVladimir Oltean static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
16562a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16572a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid,
165887b0984eSPetr Machata 			      u16 flags,
165987b0984eSPetr Machata 			      struct netlink_ext_ack *extack)
16602a93c1a3SFlorian Fainelli {
16612a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16622a93c1a3SFlorian Fainelli 
16632a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
16642a93c1a3SFlorian Fainelli }
16652a93c1a3SFlorian Fainelli 
16664b9c9358SVladimir Oltean static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
16672a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16682a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid)
16692a93c1a3SFlorian Fainelli {
16702a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16712a93c1a3SFlorian Fainelli 
16722a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
16732a93c1a3SFlorian Fainelli }
16742a93c1a3SFlorian Fainelli 
1675716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1676716efee2SJiri Pirko {
1677716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1678716efee2SJiri Pirko 
1679716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1680716efee2SJiri Pirko }
1681716efee2SJiri Pirko 
1682c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1683c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1684c2ec5f2eSOleksij Rempel {
1685c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1686c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1687c2ec5f2eSOleksij Rempel 
1688c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1689c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1690c2ec5f2eSOleksij Rempel 	else
1691c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1692c2ec5f2eSOleksij Rempel }
1693c2ec5f2eSOleksij Rempel 
16940994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
16950994d492SFelix Fietkau 				       struct net_device_path *path)
16960994d492SFelix Fietkau {
16970994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
16980994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
16990994d492SFelix Fietkau 
17000994d492SFelix Fietkau 	path->dev = ctx->dev;
17010994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
17020994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
17030994d492SFelix Fietkau 	path->dsa.port = dp->index;
17040994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
17050994d492SFelix Fietkau 
17060994d492SFelix Fietkau 	return 0;
17070994d492SFelix Fietkau }
17080994d492SFelix Fietkau 
17093e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1710d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1711d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
17123e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1713d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1714d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1715d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
171637b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
171737b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
17182bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1719d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1720abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
172104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
172204ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
172304ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
172404ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
172504ff53f9SFlorian Fainelli #endif
172644bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1727f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1728c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1729929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1730061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1731061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1732716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1733bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
17340994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
173598237d43SScott Feldman };
173698237d43SScott Feldman 
1737f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1738f37db85dSFlorian Fainelli 	.name	= "dsa",
1739f37db85dSFlorian Fainelli };
1740f37db85dSFlorian Fainelli 
1741aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1742aab9c406SFlorian Fainelli {
1743aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1744aab9c406SFlorian Fainelli 
1745765bda93SRussell King 	if (dp->pl)
1746aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1747aab9c406SFlorian Fainelli }
1748aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1749aab9c406SFlorian Fainelli 
17505c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1751aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1752aab9c406SFlorian Fainelli {
17535c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1754aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1755aab9c406SFlorian Fainelli 
1756aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1757aab9c406SFlorian Fainelli 	 * not be called if it was not.
1758aab9c406SFlorian Fainelli 	 */
1759aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1760ce31b31cSFlorian Fainelli }
1761ce31b31cSFlorian Fainelli 
176291da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1763c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1764c916e8e1SOleksij Rempel 				 u32 flags)
1765c305c165SFlorian Fainelli {
1766d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1767d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1768c305c165SFlorian Fainelli 
17690115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
17700115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1771d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1772c305c165SFlorian Fainelli 		return -ENODEV;
1773d25b8e74SRussell King 	}
1774c305c165SFlorian Fainelli 
1775c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
1776c916e8e1SOleksij Rempel 
1777aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1778c305c165SFlorian Fainelli }
1779c305c165SFlorian Fainelli 
17804fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
17810d8bcdd3SFlorian Fainelli {
1782d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1783d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1784d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
17850c65b2b9SAndrew Lunn 	phy_interface_t mode;
17866819563eSFlorian Fainelli 	u32 phy_flags = 0;
17870c65b2b9SAndrew Lunn 	int ret;
17880d8bcdd3SFlorian Fainelli 
17890c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
17900c65b2b9SAndrew Lunn 	if (ret)
179119334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
17920d8bcdd3SFlorian Fainelli 
179344cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
179444cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
179544cc27e4SIoana Ciornei 
17965c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
17975c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
17985c05c1dbSRussell King 	 * this if the switch provides such a callback.
17995c05c1dbSRussell King 	 */
18005c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
18015c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
18025c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
18035c05c1dbSRussell King 	}
18045c05c1dbSRussell King 
180544cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
180677373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1807aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1808aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1809aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1810aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1811aab9c406SFlorian Fainelli 	}
1812aab9c406SFlorian Fainelli 
18139d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1814d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
18156819563eSFlorian Fainelli 
1816aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
18176146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
18186146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
18196146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
18200d8bcdd3SFlorian Fainelli 		 */
1821c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1822d25b8e74SRussell King 		if (ret) {
1823aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1824aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1825d945097bSVivien Didelot 				   dp->index, ret);
1826aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1827c305c165SFlorian Fainelli 			return ret;
1828d25b8e74SRussell King 		}
18290d8bcdd3SFlorian Fainelli 	}
18309697f1cdSFlorian Fainelli 
18316146dd45SVladimir Oltean 	return ret;
1832b31f65fbSAndrew Lunn }
18330d8bcdd3SFlorian Fainelli 
183453da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
183553da0ebaSVladimir Oltean {
183653da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
183753da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
183853da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
183953da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
184053da0ebaSVladimir Oltean 
18414e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
18424e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
184353da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
184453da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
184553da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
184653da0ebaSVladimir Oltean 	 */
184753da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
184853da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
184953da0ebaSVladimir Oltean 
185053da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
185153da0ebaSVladimir Oltean }
185253da0ebaSVladimir Oltean 
18531a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
18541a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
18551a33e10eSCong Wang 					    struct netdev_queue *txq,
18561a33e10eSCong Wang 					    void *_unused)
18571a33e10eSCong Wang {
18581a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
18591a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
18601a33e10eSCong Wang }
18611a33e10eSCong Wang 
186224462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
186324462549SFlorian Fainelli {
1864aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
186524462549SFlorian Fainelli 
1866a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1867a94c689eSFlorian Fainelli 		return 0;
1868a94c689eSFlorian Fainelli 
1869f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1870f154be24SFlorian Fainelli 
1871aab9c406SFlorian Fainelli 	rtnl_lock();
1872aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1873aab9c406SFlorian Fainelli 	rtnl_unlock();
187424462549SFlorian Fainelli 
187524462549SFlorian Fainelli 	return 0;
187624462549SFlorian Fainelli }
187724462549SFlorian Fainelli 
187824462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
187924462549SFlorian Fainelli {
1880aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1881aab9c406SFlorian Fainelli 
1882a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1883a94c689eSFlorian Fainelli 		return 0;
1884a94c689eSFlorian Fainelli 
188524462549SFlorian Fainelli 	netif_device_attach(slave_dev);
188624462549SFlorian Fainelli 
1887aab9c406SFlorian Fainelli 	rtnl_lock();
1888aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1889aab9c406SFlorian Fainelli 	rtnl_unlock();
189024462549SFlorian Fainelli 
189124462549SFlorian Fainelli 	return 0;
189224462549SFlorian Fainelli }
189324462549SFlorian Fainelli 
1894951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
189591da11f8SLennert Buytenhek {
189624a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1897f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
18984cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1899951259aaSVivien Didelot 	const char *name = port->name;
190091da11f8SLennert Buytenhek 	struct net_device *slave_dev;
190191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
190291da11f8SLennert Buytenhek 	int ret;
190391da11f8SLennert Buytenhek 
190455199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
190555199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
190655199df6SFlorian Fainelli 
190755199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
190855199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
190955199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
191091da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1911d87d6f44SGuenter Roeck 		return -ENOMEM;
191291da11f8SLennert Buytenhek 
19139b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
19149b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
19159b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1916f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
19172b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
19187ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
191983216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
1920a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1921a2c7023fSXiaofei Shen 	else
19222fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
19230a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
19243e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1925bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1926bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1927f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1928d442ad4aSStephen Hemminger 
19291a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
19301a33e10eSCong Wang 				 NULL);
19311a33e10eSCong Wang 
19324cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
19334cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
193491da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
193591da11f8SLennert Buytenhek 
193691da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
19376a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
19386a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
19395f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
19405f6b4e14SFlorian Fainelli 		return -ENOMEM;
19415f6b4e14SFlorian Fainelli 	}
1942e131a563SAlexander Lobakin 
1943e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1944e131a563SAlexander Lobakin 	if (ret)
1945e131a563SAlexander Lobakin 		goto out_free;
1946e131a563SAlexander Lobakin 
19474cfbf09cSVivien Didelot 	p->dp = port;
1948f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
1949f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
195053da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
195191da11f8SLennert Buytenhek 
1952bfcb8132SVladimir Oltean 	rtnl_lock();
1953bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1954bfcb8132SVladimir Oltean 	rtnl_unlock();
19554349abdbSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
1956bdc40a3fSRasmus Villemoes 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1957bdc40a3fSRasmus Villemoes 			 ret, ETH_DATA_LEN, port->index);
1958bfcb8132SVladimir Oltean 
195991da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
196091da11f8SLennert Buytenhek 
19614fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
19620071f56eSAndrew Lunn 	if (ret) {
1963c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
1964c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1965c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
1966e131a563SAlexander Lobakin 		goto out_gcells;
1967e804441cSFlorian Fainelli 	}
1968e804441cSFlorian Fainelli 
19692f1e8ea7SVladimir Oltean 	rtnl_lock();
19702f1e8ea7SVladimir Oltean 
19712f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1972e804441cSFlorian Fainelli 	if (ret) {
1973e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1974e804441cSFlorian Fainelli 			   ret, slave_dev->name);
19752f1e8ea7SVladimir Oltean 		rtnl_unlock();
1976e804441cSFlorian Fainelli 		goto out_phy;
19770071f56eSAndrew Lunn 	}
19780071f56eSAndrew Lunn 
19792f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
19802f1e8ea7SVladimir Oltean 
19812f1e8ea7SVladimir Oltean 	rtnl_unlock();
19822f1e8ea7SVladimir Oltean 
19832f1e8ea7SVladimir Oltean 	if (ret)
19842f1e8ea7SVladimir Oltean 		goto out_unregister;
19852f1e8ea7SVladimir Oltean 
1986d87d6f44SGuenter Roeck 	return 0;
1987e804441cSFlorian Fainelli 
19882f1e8ea7SVladimir Oltean out_unregister:
19892f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1990e804441cSFlorian Fainelli out_phy:
1991aab9c406SFlorian Fainelli 	rtnl_lock();
1992aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1993aab9c406SFlorian Fainelli 	rtnl_unlock();
1994aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1995e131a563SAlexander Lobakin out_gcells:
1996e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1997e804441cSFlorian Fainelli out_free:
19986a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1999e804441cSFlorian Fainelli 	free_netdev(slave_dev);
2000f8b8b1cdSVivien Didelot 	port->slave = NULL;
2001e804441cSFlorian Fainelli 	return ret;
200291da11f8SLennert Buytenhek }
2003b73adef6SFlorian Fainelli 
2004cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
2005cda5c15bSNeil Armstrong {
20062f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
2007d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2008cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2009cda5c15bSNeil Armstrong 
2010cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2011aab9c406SFlorian Fainelli 	rtnl_lock();
20122f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
20132f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2014aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2015aab9c406SFlorian Fainelli 	rtnl_unlock();
2016881eadabSJohan Hovold 
2017aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2018e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
20196a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2020cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2021cda5c15bSNeil Armstrong }
2022cda5c15bSNeil Armstrong 
20234d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2024b73adef6SFlorian Fainelli {
2025b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2026b73adef6SFlorian Fainelli }
2027a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2028b73adef6SFlorian Fainelli 
20298e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
20308e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2031b73adef6SFlorian Fainelli {
2032d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
20332afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
20348e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2035b73adef6SFlorian Fainelli 
20362afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20372afc526aSVladimir Oltean 
20388e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
20398e92ab3aSVivien Didelot 		if (info->linking) {
20402afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2041bff33f7eSVladimir Oltean 			if (!err)
2042bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
20438e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
20448e92ab3aSVivien Didelot 		} else {
204517d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
20468e92ab3aSVivien Didelot 			err = NOTIFY_OK;
20478e92ab3aSVivien Didelot 		}
2048058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2049058102a6STobias Waldekranz 		if (info->linking) {
2050058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
20512afc526aSVladimir Oltean 						info->upper_info, extack);
2052058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2053058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2054058102a6STobias Waldekranz 						   "Offloading not supported");
2055058102a6STobias Waldekranz 				err = 0;
2056058102a6STobias Waldekranz 			}
2057058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2058058102a6STobias Waldekranz 		} else {
2059058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2060058102a6STobias Waldekranz 			err = NOTIFY_OK;
2061058102a6STobias Waldekranz 		}
206218596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
206318596f50SGeorge McCollister 		if (info->linking) {
206418596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
206518596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
206618596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
206718596f50SGeorge McCollister 						   "Offloading not supported");
206818596f50SGeorge McCollister 				err = 0;
206918596f50SGeorge McCollister 			}
207018596f50SGeorge McCollister 			err = notifier_from_errno(err);
207118596f50SGeorge McCollister 		} else {
207218596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
207318596f50SGeorge McCollister 			err = NOTIFY_OK;
207418596f50SGeorge McCollister 		}
2075058102a6STobias Waldekranz 	}
2076058102a6STobias Waldekranz 
2077058102a6STobias Waldekranz 	return err;
2078058102a6STobias Waldekranz }
2079058102a6STobias Waldekranz 
2080*74918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev,
2081*74918945SVladimir Oltean 				    struct netdev_notifier_changeupper_info *info)
2082*74918945SVladimir Oltean {
2083*74918945SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2084*74918945SVladimir Oltean 	struct netlink_ext_ack *extack;
2085*74918945SVladimir Oltean 	int err = 0;
2086*74918945SVladimir Oltean 
2087*74918945SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
2088*74918945SVladimir Oltean 
2089*74918945SVladimir Oltean 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2090*74918945SVladimir Oltean 		err = dsa_port_pre_bridge_leave(dp, info->upper_dev, extack);
2091*74918945SVladimir Oltean 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2092*74918945SVladimir Oltean 		err = dsa_port_pre_lag_leave(dp, info->upper_dev, extack);
2093*74918945SVladimir Oltean 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
2094*74918945SVladimir Oltean 	 * meaningfully enslaved to a bridge yet
2095*74918945SVladimir Oltean 	 */
2096*74918945SVladimir Oltean 
2097*74918945SVladimir Oltean 	return notifier_from_errno(err);
2098*74918945SVladimir Oltean }
2099*74918945SVladimir Oltean 
2100058102a6STobias Waldekranz static int
2101058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2102058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2103058102a6STobias Waldekranz {
2104058102a6STobias Waldekranz 	struct net_device *lower;
2105058102a6STobias Waldekranz 	struct list_head *iter;
2106058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2107058102a6STobias Waldekranz 	struct dsa_port *dp;
2108058102a6STobias Waldekranz 
2109058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2110058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2111058102a6STobias Waldekranz 			continue;
2112058102a6STobias Waldekranz 
2113058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2114058102a6STobias Waldekranz 		if (!dp->lag_dev)
2115058102a6STobias Waldekranz 			/* Software LAG */
2116058102a6STobias Waldekranz 			continue;
2117058102a6STobias Waldekranz 
2118058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2119058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2120058102a6STobias Waldekranz 			break;
21216debb68aSVivien Didelot 	}
2122b73adef6SFlorian Fainelli 
21238e92ab3aSVivien Didelot 	return err;
2124b73adef6SFlorian Fainelli }
2125b73adef6SFlorian Fainelli 
2126*74918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls
2127*74918945SVladimir Oltean  * dsa_slave_prechangeupper()
2128*74918945SVladimir Oltean  */
2129*74918945SVladimir Oltean static int
2130*74918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev,
2131*74918945SVladimir Oltean 			     struct netdev_notifier_changeupper_info *info)
2132*74918945SVladimir Oltean {
2133*74918945SVladimir Oltean 	struct net_device *lower;
2134*74918945SVladimir Oltean 	struct list_head *iter;
2135*74918945SVladimir Oltean 	int err = NOTIFY_DONE;
2136*74918945SVladimir Oltean 	struct dsa_port *dp;
2137*74918945SVladimir Oltean 
2138*74918945SVladimir Oltean 	netdev_for_each_lower_dev(dev, lower, iter) {
2139*74918945SVladimir Oltean 		if (!dsa_slave_dev_check(lower))
2140*74918945SVladimir Oltean 			continue;
2141*74918945SVladimir Oltean 
2142*74918945SVladimir Oltean 		dp = dsa_slave_to_port(lower);
2143*74918945SVladimir Oltean 		if (!dp->lag_dev)
2144*74918945SVladimir Oltean 			/* Software LAG */
2145*74918945SVladimir Oltean 			continue;
2146*74918945SVladimir Oltean 
2147*74918945SVladimir Oltean 		err = dsa_slave_prechangeupper(lower, info);
2148*74918945SVladimir Oltean 		if (notifier_to_errno(err))
2149*74918945SVladimir Oltean 			break;
2150*74918945SVladimir Oltean 	}
2151*74918945SVladimir Oltean 
2152*74918945SVladimir Oltean 	return err;
2153*74918945SVladimir Oltean }
2154*74918945SVladimir Oltean 
2155eb46e8daSVladimir Oltean static int
2156eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2157eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2158cc1d5bdaSFlorian Fainelli {
2159cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
2160cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
2161cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2162cc1d5bdaSFlorian Fainelli 
2163cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2164cc1d5bdaSFlorian Fainelli 
2165cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2166cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2167cc1d5bdaSFlorian Fainelli 
2168cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2169cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2170cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2171cc1d5bdaSFlorian Fainelli 
2172cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
2173cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
2174cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2175cc1d5bdaSFlorian Fainelli 
2176cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2177cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
2178cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2179cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2180cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2181cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2182cc1d5bdaSFlorian Fainelli 	}
2183cc1d5bdaSFlorian Fainelli 
2184cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2185cc1d5bdaSFlorian Fainelli }
2186cc1d5bdaSFlorian Fainelli 
21872b138406SVladimir Oltean static int
21882b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
21892b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
21902b138406SVladimir Oltean {
21912b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
21922b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
21932b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
21942b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
21952b138406SVladimir Oltean 	int err = NOTIFY_DONE;
21962b138406SVladimir Oltean 	u16 vid;
21972b138406SVladimir Oltean 
2198adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
21992b138406SVladimir Oltean 		return NOTIFY_DONE;
22002b138406SVladimir Oltean 
22012b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
22022b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
22032b138406SVladimir Oltean 
22042b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
22052b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
22062b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
22072b138406SVladimir Oltean 	 */
22082b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
22092b138406SVladimir Oltean 	if (err == 0) {
22102b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
22112b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
22122b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
22132b138406SVladimir Oltean 	}
22142b138406SVladimir Oltean 
22152b138406SVladimir Oltean 	return NOTIFY_DONE;
22162b138406SVladimir Oltean }
22172b138406SVladimir Oltean 
22184ede74e7SVladimir Oltean static int
22194ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
22204ede74e7SVladimir Oltean 				      struct netdev_notifier_changeupper_info *info)
2221b73adef6SFlorian Fainelli {
2222e358bef7SVladimir Oltean 	struct dsa_switch *ds;
2223e358bef7SVladimir Oltean 	struct dsa_port *dp;
2224e358bef7SVladimir Oltean 	int err;
22252b138406SVladimir Oltean 
222653bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
22274ede74e7SVladimir Oltean 		return dsa_prevent_bridging_8021q_upper(dev, info);
22282b138406SVladimir Oltean 
2229e358bef7SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2230e358bef7SVladimir Oltean 	ds = dp->ds;
2231e358bef7SVladimir Oltean 
2232e358bef7SVladimir Oltean 	if (ds->ops->port_prechangeupper) {
2233e358bef7SVladimir Oltean 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2234e358bef7SVladimir Oltean 		if (err)
2235e358bef7SVladimir Oltean 			return notifier_from_errno(err);
2236e358bef7SVladimir Oltean 	}
2237e358bef7SVladimir Oltean 
22382b138406SVladimir Oltean 	if (is_vlan_dev(info->upper_dev))
22394ede74e7SVladimir Oltean 		return dsa_slave_check_8021q_upper(dev, info);
22404ede74e7SVladimir Oltean 
22414ede74e7SVladimir Oltean 	return NOTIFY_DONE;
22424ede74e7SVladimir Oltean }
22434ede74e7SVladimir Oltean 
22444ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb,
22454ede74e7SVladimir Oltean 				     unsigned long event, void *ptr)
22464ede74e7SVladimir Oltean {
22474ede74e7SVladimir Oltean 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
22484ede74e7SVladimir Oltean 
22494ede74e7SVladimir Oltean 	switch (event) {
22504ede74e7SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
22514ede74e7SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
22524ede74e7SVladimir Oltean 		int err;
22534ede74e7SVladimir Oltean 
22544ede74e7SVladimir Oltean 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
22554ede74e7SVladimir Oltean 		if (err != NOTIFY_DONE)
22564ede74e7SVladimir Oltean 			return err;
22574ede74e7SVladimir Oltean 
2258*74918945SVladimir Oltean 		if (dsa_slave_dev_check(dev))
2259*74918945SVladimir Oltean 			return dsa_slave_prechangeupper(dev, ptr);
2260*74918945SVladimir Oltean 
2261*74918945SVladimir Oltean 		if (netif_is_lag_master(dev))
2262*74918945SVladimir Oltean 			return dsa_slave_lag_prechangeupper(dev, ptr);
2263*74918945SVladimir Oltean 
226483501299SVladimir Oltean 		break;
22652b138406SVladimir Oltean 	}
226683501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2267058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
22688e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2269058102a6STobias Waldekranz 
2270058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2271058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2272058102a6STobias Waldekranz 
2273058102a6STobias Waldekranz 		break;
2274058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2275058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2276058102a6STobias Waldekranz 		struct dsa_port *dp;
2277058102a6STobias Waldekranz 		int err;
2278058102a6STobias Waldekranz 
2279058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2280058102a6STobias Waldekranz 			break;
2281058102a6STobias Waldekranz 
2282058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2283058102a6STobias Waldekranz 
2284058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2285058102a6STobias Waldekranz 		return notifier_from_errno(err);
2286058102a6STobias Waldekranz 	}
2287c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2288c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2289c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2290c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2291c0a8a9c2SVladimir Oltean 
2292c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2293c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2294c0a8a9c2SVladimir Oltean 
2295c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2296c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2297c0a8a9c2SVladimir Oltean 
2298c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2299c0a8a9c2SVladimir Oltean 			if (!dsa_is_user_port(dp->ds, dp->index))
2300c0a8a9c2SVladimir Oltean 				continue;
2301c0a8a9c2SVladimir Oltean 
2302c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2303c0a8a9c2SVladimir Oltean 		}
2304c0a8a9c2SVladimir Oltean 
2305c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2306c0a8a9c2SVladimir Oltean 
2307c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2308c0a8a9c2SVladimir Oltean 	}
2309c0a8a9c2SVladimir Oltean 	default:
2310c0a8a9c2SVladimir Oltean 		break;
2311cc1d5bdaSFlorian Fainelli 	}
2312b73adef6SFlorian Fainelli 
2313b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2314b73adef6SFlorian Fainelli }
231588e4f0caSVivien Didelot 
2316c4bb76a9SVladimir Oltean static void
2317c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2318c4bb76a9SVladimir Oltean {
2319c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2320c4bb76a9SVladimir Oltean 	struct switchdev_notifier_fdb_info info;
2321c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2322c4bb76a9SVladimir Oltean 
2323c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2324c4bb76a9SVladimir Oltean 		return;
2325c4bb76a9SVladimir Oltean 
2326c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2327c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2328c4bb76a9SVladimir Oltean 	info.offloaded = true;
2329c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2330c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2331c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2332c4bb76a9SVladimir Oltean }
2333c9eb3e0fSArkadi Sharshevsky 
2334c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2335c9eb3e0fSArkadi Sharshevsky {
2336c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2337c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2338c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2339c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2340c9eb3e0fSArkadi Sharshevsky 	int err;
2341c9eb3e0fSArkadi Sharshevsky 
2342c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2343c4bb76a9SVladimir Oltean 
2344c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2345c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2346c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2347c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2348c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2349c9eb3e0fSArkadi Sharshevsky 		if (err) {
2350c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2351c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2352c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2353c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2354c9eb3e0fSArkadi Sharshevsky 			break;
2355c9eb3e0fSArkadi Sharshevsky 		}
2356c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2357c9eb3e0fSArkadi Sharshevsky 		break;
2358c9eb3e0fSArkadi Sharshevsky 
2359c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2360c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2361c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2362c9eb3e0fSArkadi Sharshevsky 		if (err) {
2363c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2364c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2365c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2366c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2367c9eb3e0fSArkadi Sharshevsky 		}
23682fd18650SVladimir Oltean 
2369c9eb3e0fSArkadi Sharshevsky 		break;
2370c9eb3e0fSArkadi Sharshevsky 	}
2371c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2372c9eb3e0fSArkadi Sharshevsky 
2373c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2374c4bb76a9SVladimir Oltean 	if (dsa_is_user_port(ds, dp->index))
2375c4bb76a9SVladimir Oltean 		dev_put(dp->slave);
2376c9eb3e0fSArkadi Sharshevsky }
2377c9eb3e0fSArkadi Sharshevsky 
2378d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev,
2379d5f19486SVladimir Oltean 			      struct netdev_nested_priv *priv)
2380d5f19486SVladimir Oltean {
2381d5f19486SVladimir Oltean 	if (dsa_slave_dev_check(lower_dev)) {
2382d5f19486SVladimir Oltean 		priv->data = (void *)netdev_priv(lower_dev);
2383d5f19486SVladimir Oltean 		return 1;
2384d5f19486SVladimir Oltean 	}
2385d5f19486SVladimir Oltean 
2386d5f19486SVladimir Oltean 	return 0;
2387d5f19486SVladimir Oltean }
2388d5f19486SVladimir Oltean 
2389d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2390d5f19486SVladimir Oltean {
2391d5f19486SVladimir Oltean 	struct netdev_nested_priv priv = {
2392d5f19486SVladimir Oltean 		.data = NULL,
2393d5f19486SVladimir Oltean 	};
2394d5f19486SVladimir Oltean 
2395d5f19486SVladimir Oltean 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2396d5f19486SVladimir Oltean 
2397d5f19486SVladimir Oltean 	return (struct dsa_slave_priv *)priv.data;
2398d5f19486SVladimir Oltean }
2399d5f19486SVladimir Oltean 
2400c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2401c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2402c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2403c9eb3e0fSArkadi Sharshevsky {
2404c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2405c4bb76a9SVladimir Oltean 	const struct switchdev_notifier_fdb_info *fdb_info;
2406c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
2407c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
240879b139f4SVivien Didelot 	int err;
240979b139f4SVivien Didelot 
2410447d290aSVladimir Oltean 	switch (event) {
2411447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
241279b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
241379b139f4SVivien Didelot 						     dsa_slave_dev_check,
241479b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
241579b139f4SVivien Didelot 		return notifier_from_errno(err);
2416447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2417447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2418d5f19486SVladimir Oltean 		fdb_info = ptr;
2419d5f19486SVladimir Oltean 
2420d5f19486SVladimir Oltean 		if (dsa_slave_dev_check(dev)) {
24212c4eca3eSVladimir Oltean 			if (!fdb_info->added_by_user || fdb_info->is_local)
2422d5f19486SVladimir Oltean 				return NOTIFY_OK;
2423c9eb3e0fSArkadi Sharshevsky 
2424c4bb76a9SVladimir Oltean 			dp = dsa_slave_to_port(dev);
2425d5f19486SVladimir Oltean 		} else {
2426d5f19486SVladimir Oltean 			/* Snoop addresses learnt on foreign interfaces
2427d5f19486SVladimir Oltean 			 * bridged with us, for switches that don't
2428d5f19486SVladimir Oltean 			 * automatically learn SA from CPU-injected traffic
2429d5f19486SVladimir Oltean 			 */
2430d5f19486SVladimir Oltean 			struct net_device *br_dev;
2431d5f19486SVladimir Oltean 			struct dsa_slave_priv *p;
2432d5f19486SVladimir Oltean 
2433d5f19486SVladimir Oltean 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2434d5f19486SVladimir Oltean 			if (!br_dev)
2435d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2436d5f19486SVladimir Oltean 
2437d5f19486SVladimir Oltean 			if (!netif_is_bridge_master(br_dev))
2438d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2439d5f19486SVladimir Oltean 
2440d5f19486SVladimir Oltean 			p = dsa_slave_dev_lower_find(br_dev);
2441d5f19486SVladimir Oltean 			if (!p)
2442d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2443d5f19486SVladimir Oltean 
2444d5f19486SVladimir Oltean 			dp = p->dp->cpu_dp;
2445d5f19486SVladimir Oltean 
2446d5f19486SVladimir Oltean 			if (!dp->ds->assisted_learning_on_cpu_port)
2447d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2448a324d3d4SVladimir Oltean 
2449a324d3d4SVladimir Oltean 			/* When the bridge learns an address on an offloaded
2450a324d3d4SVladimir Oltean 			 * LAG we don't want to send traffic to the CPU, the
2451a324d3d4SVladimir Oltean 			 * other ports bridged with the LAG should be able to
2452a324d3d4SVladimir Oltean 			 * autonomously forward towards it.
2453a324d3d4SVladimir Oltean 			 */
245403cbb870SVladimir Oltean 			if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev))
2455a324d3d4SVladimir Oltean 				return NOTIFY_DONE;
2456d5f19486SVladimir Oltean 		}
2457c4bb76a9SVladimir Oltean 
24585fb4a451SVladimir Oltean 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
24595fb4a451SVladimir Oltean 			return NOTIFY_DONE;
24605fb4a451SVladimir Oltean 
2461c9eb3e0fSArkadi Sharshevsky 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2462c9eb3e0fSArkadi Sharshevsky 		if (!switchdev_work)
2463c9eb3e0fSArkadi Sharshevsky 			return NOTIFY_BAD;
2464c9eb3e0fSArkadi Sharshevsky 
2465c9eb3e0fSArkadi Sharshevsky 		INIT_WORK(&switchdev_work->work,
2466c9eb3e0fSArkadi Sharshevsky 			  dsa_slave_switchdev_event_work);
2467c4bb76a9SVladimir Oltean 		switchdev_work->ds = dp->ds;
2468c4bb76a9SVladimir Oltean 		switchdev_work->port = dp->index;
2469c9eb3e0fSArkadi Sharshevsky 		switchdev_work->event = event;
2470c9eb3e0fSArkadi Sharshevsky 
2471c4bb76a9SVladimir Oltean 		ether_addr_copy(switchdev_work->addr,
2472c4bb76a9SVladimir Oltean 				fdb_info->addr);
2473c4bb76a9SVladimir Oltean 		switchdev_work->vid = fdb_info->vid;
2474c4bb76a9SVladimir Oltean 
2475d5f19486SVladimir Oltean 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2476d5f19486SVladimir Oltean 		if (dsa_is_user_port(dp->ds, dp->index))
2477c9eb3e0fSArkadi Sharshevsky 			dev_hold(dev);
2478447d290aSVladimir Oltean 		dsa_schedule_work(&switchdev_work->work);
2479c9eb3e0fSArkadi Sharshevsky 		break;
2480c9eb3e0fSArkadi Sharshevsky 	default:
2481c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2482c9eb3e0fSArkadi Sharshevsky 	}
2483c9eb3e0fSArkadi Sharshevsky 
2484c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2485c9eb3e0fSArkadi Sharshevsky }
2486c9eb3e0fSArkadi Sharshevsky 
24872b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
24882b239f67SPetr Machata 					      unsigned long event, void *ptr)
24892b239f67SPetr Machata {
24902b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
249179b139f4SVivien Didelot 	int err;
24922b239f67SPetr Machata 
24932b239f67SPetr Machata 	switch (event) {
249479b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
249579b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
249679b139f4SVivien Didelot 						    dsa_slave_dev_check,
249779b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
249879b139f4SVivien Didelot 		return notifier_from_errno(err);
24992b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
250079b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
250179b139f4SVivien Didelot 						    dsa_slave_dev_check,
250279b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
250379b139f4SVivien Didelot 		return notifier_from_errno(err);
25049ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
250579b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
250679b139f4SVivien Didelot 						     dsa_slave_dev_check,
250779b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
250879b139f4SVivien Didelot 		return notifier_from_errno(err);
25092b239f67SPetr Machata 	}
25102b239f67SPetr Machata 
25112b239f67SPetr Machata 	return NOTIFY_DONE;
25122b239f67SPetr Machata }
25132b239f67SPetr Machata 
251488e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
251588e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
251688e4f0caSVivien Didelot };
251788e4f0caSVivien Didelot 
2518010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2519c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2520c9eb3e0fSArkadi Sharshevsky };
2521c9eb3e0fSArkadi Sharshevsky 
2522010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
25232b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
25242b239f67SPetr Machata };
25252b239f67SPetr Machata 
252688e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
252788e4f0caSVivien Didelot {
25282b239f67SPetr Machata 	struct notifier_block *nb;
2529c9eb3e0fSArkadi Sharshevsky 	int err;
2530c9eb3e0fSArkadi Sharshevsky 
2531c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2532c9eb3e0fSArkadi Sharshevsky 	if (err)
2533c9eb3e0fSArkadi Sharshevsky 		return err;
2534c9eb3e0fSArkadi Sharshevsky 
2535c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2536c9eb3e0fSArkadi Sharshevsky 	if (err)
2537c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2538c9eb3e0fSArkadi Sharshevsky 
25392b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
25402b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
25412b239f67SPetr Machata 	if (err)
25422b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
25432b239f67SPetr Machata 
2544c9eb3e0fSArkadi Sharshevsky 	return 0;
2545c9eb3e0fSArkadi Sharshevsky 
25462b239f67SPetr Machata err_switchdev_blocking_nb:
25472b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2548c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2549c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2550c9eb3e0fSArkadi Sharshevsky 	return err;
255188e4f0caSVivien Didelot }
255288e4f0caSVivien Didelot 
255388e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
255488e4f0caSVivien Didelot {
25552b239f67SPetr Machata 	struct notifier_block *nb;
255688e4f0caSVivien Didelot 	int err;
255788e4f0caSVivien Didelot 
25582b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
25592b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
25602b239f67SPetr Machata 	if (err)
25612b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
25622b239f67SPetr Machata 
2563c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2564c9eb3e0fSArkadi Sharshevsky 	if (err)
2565c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2566c9eb3e0fSArkadi Sharshevsky 
256788e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
256888e4f0caSVivien Didelot 	if (err)
256988e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
257088e4f0caSVivien Didelot }
2571