xref: /openbmc/linux/net/dsa/slave.c (revision c4b364ce1270d689ee5010001344b8eae3685f32)
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 
27435636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
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 
28135636062SScott Feldman 	switch (attr->id) {
2821f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
28303cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
28403cbb870SVladimir Oltean 			return -EOPNOTSUPP;
28503cbb870SVladimir Oltean 
286bae33f2bSVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state);
28735636062SScott Feldman 		break;
288fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
28903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
29003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
29103cbb870SVladimir Oltean 
29289153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
29389153ed6SVladimir Oltean 					      extack);
294fb2dabadSVivien Didelot 		break;
29534a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
29603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
29703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
29803cbb870SVladimir Oltean 
299bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
30034a79f63SVivien Didelot 		break;
301ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
30203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
30303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30403cbb870SVladimir Oltean 
305a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
306a8b659e7SVladimir Oltean 						extack);
307ea87005aSFlorian Fainelli 		break;
30857652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
30903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
31003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
31103cbb870SVladimir Oltean 
312a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
31357652796SRussell King 		break;
31408cc83ccSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
31503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
31603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
31703cbb870SVladimir Oltean 
318a8b659e7SVladimir Oltean 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, extack);
31908cc83ccSVivien Didelot 		break;
32035636062SScott Feldman 	default:
32135636062SScott Feldman 		ret = -EOPNOTSUPP;
32235636062SScott Feldman 		break;
32335636062SScott Feldman 	}
32435636062SScott Feldman 
32535636062SScott Feldman 	return ret;
32635636062SScott Feldman }
32735636062SScott Feldman 
3281ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
3291ce39f0eSVladimir Oltean static int
3301ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
3311ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
3321ce39f0eSVladimir Oltean {
3331ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
3341ce39f0eSVladimir Oltean 	struct list_head *iter;
3351ce39f0eSVladimir Oltean 
3361ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
3371ce39f0eSVladimir Oltean 		u16 vid;
3381ce39f0eSVladimir Oltean 
3391ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
3401ce39f0eSVladimir Oltean 			continue;
3411ce39f0eSVladimir Oltean 
3421ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
343b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
3441ce39f0eSVladimir Oltean 			return -EBUSY;
3451ce39f0eSVladimir Oltean 	}
3461ce39f0eSVladimir Oltean 
3471ce39f0eSVladimir Oltean 	return 0;
3481ce39f0eSVladimir Oltean }
3491ce39f0eSVladimir Oltean 
350bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
3510ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
3520ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
353bdcff080SVivien Didelot {
3542209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
355bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
356bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
357b7a9e0daSVladimir Oltean 	int err;
358bdcff080SVivien Didelot 
3590ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
3600ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
361c5335d73SVivien Didelot 		return 0;
3620ee2af4eSVladimir Oltean 	}
363c5335d73SVivien Didelot 
364bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
365bdcff080SVivien Didelot 
3661ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
3671ce39f0eSVladimir Oltean 	 * the same VID.
3681ce39f0eSVladimir Oltean 	 */
369ffb68fc5SVladimir Oltean 	if (br_vlan_enabled(dp->bridge_dev)) {
3701ce39f0eSVladimir Oltean 		rcu_read_lock();
3711ce39f0eSVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
3721ce39f0eSVladimir Oltean 		rcu_read_unlock();
37331046a5fSVladimir Oltean 		if (err) {
37431046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
37531046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
3761ce39f0eSVladimir Oltean 			return err;
3771ce39f0eSVladimir Oltean 		}
37831046a5fSVladimir Oltean 	}
3791ce39f0eSVladimir Oltean 
38031046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp, &vlan, extack);
381bdcff080SVivien Didelot 	if (err)
382bdcff080SVivien Didelot 		return err;
383bdcff080SVivien Didelot 
384b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
385b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
386b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
387b9499904SVivien Didelot 	 */
388b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
389b9499904SVivien Didelot 
39031046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
3917e1741b4SVivien Didelot 	if (err)
3927e1741b4SVivien Didelot 		return err;
3937e1741b4SVivien Didelot 
394b7a9e0daSVladimir Oltean 	return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
395bdcff080SVivien Didelot }
396bdcff080SVivien Didelot 
397ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
398648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
39979b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
400ba14d9ebSVivien Didelot {
401d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
402ba14d9ebSVivien Didelot 	int err;
403ba14d9ebSVivien Didelot 
4049e8f4a54SJiri Pirko 	switch (obj->id) {
4058df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
40603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
40779b139f4SVivien Didelot 			return -EOPNOTSUPP;
40803cbb870SVladimir Oltean 
409ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4108df30255SVivien Didelot 		break;
4115f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
41203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
41303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
41403cbb870SVladimir Oltean 
4155f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4165f4dbc50SAndrew Lunn 		 * but on the CPU port.
4175f4dbc50SAndrew Lunn 		 */
418ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4195f4dbc50SAndrew Lunn 		break;
42057d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
42103cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
42203cbb870SVladimir Oltean 			return -EOPNOTSUPP;
42303cbb870SVladimir Oltean 
4240ee2af4eSVladimir Oltean 		err = dsa_slave_vlan_add(dev, obj, extack);
42511149536SVivien Didelot 		break;
426c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
42703cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
428c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
42903cbb870SVladimir Oltean 
430c595c433SHoratiu Vultur 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
431c595c433SHoratiu Vultur 		break;
432c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_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_ring_role(dp,
437c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
438c595c433SHoratiu Vultur 		break;
439ba14d9ebSVivien Didelot 	default:
440ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
441ba14d9ebSVivien Didelot 		break;
442ba14d9ebSVivien Didelot 	}
443ba14d9ebSVivien Didelot 
444ba14d9ebSVivien Didelot 	return err;
445ba14d9ebSVivien Didelot }
446ba14d9ebSVivien Didelot 
447bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
448bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
449bdcff080SVivien Didelot {
4502209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
451bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4522209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
453b7a9e0daSVladimir Oltean 	int err;
454bdcff080SVivien Didelot 
45554a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
456c5335d73SVivien Didelot 		return 0;
457c5335d73SVivien Didelot 
4582209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
4592209158cSVladimir Oltean 
4607e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
4617e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
4627e1741b4SVivien Didelot 	 */
4632209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, vlan);
4642209158cSVladimir Oltean 	if (err)
4652209158cSVladimir Oltean 		return err;
4662209158cSVladimir Oltean 
467b7a9e0daSVladimir Oltean 	vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
4682209158cSVladimir Oltean 
4692209158cSVladimir Oltean 	return 0;
470bdcff080SVivien Didelot }
471bdcff080SVivien Didelot 
472ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
473648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
474ba14d9ebSVivien Didelot {
475d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
476ba14d9ebSVivien Didelot 	int err;
477ba14d9ebSVivien Didelot 
4789e8f4a54SJiri Pirko 	switch (obj->id) {
4798df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
48003cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
48179b139f4SVivien Didelot 			return -EOPNOTSUPP;
48203cbb870SVladimir Oltean 
483bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4848df30255SVivien Didelot 		break;
4855f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
48603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
48703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
48803cbb870SVladimir Oltean 
4895f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4905f4dbc50SAndrew Lunn 		 * but on the CPU port.
4915f4dbc50SAndrew Lunn 		 */
4925f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4935f4dbc50SAndrew Lunn 		break;
49457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
49503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
49603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
49703cbb870SVladimir Oltean 
498bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
49911149536SVivien Didelot 		break;
500c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
50103cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
502c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
50303cbb870SVladimir Oltean 
504c595c433SHoratiu Vultur 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
505c595c433SHoratiu Vultur 		break;
506c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
50703cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, obj->orig_dev))
508c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
50903cbb870SVladimir Oltean 
510c595c433SHoratiu Vultur 		err = dsa_port_mrp_del_ring_role(dp,
511c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
512c595c433SHoratiu Vultur 		break;
513ba14d9ebSVivien Didelot 	default:
514ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
515ba14d9ebSVivien Didelot 		break;
516ba14d9ebSVivien Didelot 	}
517ba14d9ebSVivien Didelot 
518ba14d9ebSVivien Didelot 	return err;
519ba14d9ebSVivien Didelot }
520ba14d9ebSVivien Didelot 
521929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
522929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
523b73adef6SFlorian Fainelli {
524d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
525d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
526a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
527b73adef6SFlorian Fainelli 
52815b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
52915b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
53015b04aceSJiri Pirko 	 * should be removed with legacy support.
53115b04aceSJiri Pirko 	 */
53215b04aceSJiri Pirko 	if (dp->ds->devlink)
53315b04aceSJiri Pirko 		return -EOPNOTSUPP;
53415b04aceSJiri Pirko 
535929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
536929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
537929d6c14SFlorian Fainelli 
538929d6c14SFlorian Fainelli 	return 0;
539929d6c14SFlorian Fainelli }
540929d6c14SFlorian Fainelli 
5414fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
54204ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
54304ff53f9SFlorian Fainelli {
54404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
5454fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
5464fa7b718SVivien Didelot 
547f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
54804ff53f9SFlorian Fainelli #else
54904ff53f9SFlorian Fainelli 	BUG();
55004ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
551f78ed220SEric Dumazet #endif
55204ff53f9SFlorian Fainelli }
55304ff53f9SFlorian Fainelli 
55490af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
55590af1059SBrandon Streiff 				 struct sk_buff *skb)
55690af1059SBrandon Streiff {
55790af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
55890af1059SBrandon Streiff 
559cfd12c06SYangbo Lu 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
560cfd12c06SYangbo Lu 		return;
561cfd12c06SYangbo Lu 
56290af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
56390af1059SBrandon Streiff 		return;
56490af1059SBrandon Streiff 
5655c5416f5SYangbo Lu 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
56690af1059SBrandon Streiff }
56790af1059SBrandon Streiff 
56897a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
56997a69a0dSVladimir Oltean {
57097a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
57197a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
57297a69a0dSVladimir Oltean 	 */
57397a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
57497a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
57597a69a0dSVladimir Oltean 
57697a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
57797a69a0dSVladimir Oltean 	 * do not modify its EtherType
57897a69a0dSVladimir Oltean 	 */
57997a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
58097a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
58197a69a0dSVladimir Oltean 
58297a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
58397a69a0dSVladimir Oltean }
58497a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
58597a69a0dSVladimir Oltean 
586a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
587a3b0b647SVladimir Oltean {
588a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
589a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
590a3b0b647SVladimir Oltean 
591a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
592a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
593a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
594a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
595a3b0b647SVladimir Oltean 	 */
596a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
597a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
598a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
599a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
600a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
601a3b0b647SVladimir Oltean 
602a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
603a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
604a3b0b647SVladimir Oltean 		return 0;
605a3b0b647SVladimir Oltean 
606a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
607a3b0b647SVladimir Oltean 				GFP_ATOMIC);
608a3b0b647SVladimir Oltean }
609a3b0b647SVladimir Oltean 
6103e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6113e8a72d1SFlorian Fainelli {
6123e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6134ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6143e8a72d1SFlorian Fainelli 
6156a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
6163e8a72d1SFlorian Fainelli 
617*c4b364ceSYangbo Lu 	memset(skb->cb, 0, sizeof(skb->cb));
61887671375SVladimir Oltean 
619cf536ea3SYangbo Lu 	/* Handle tx timestamp if any */
62090af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
62190af1059SBrandon Streiff 
622a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
623a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
624a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
625a3b0b647SVladimir Oltean 	}
626a3b0b647SVladimir Oltean 
627a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
628a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
629a3b0b647SVladimir Oltean 	 */
630a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
631a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
632a3b0b647SVladimir Oltean 
633fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
634fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
635fe47d563SVivien Didelot 	 */
6364ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
637fe47d563SVivien Didelot 	if (!nskb) {
638fe47d563SVivien Didelot 		kfree_skb(skb);
6394ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
640fe47d563SVivien Didelot 	}
6415aed85ceSFlorian Fainelli 
64297a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
64397a69a0dSVladimir Oltean }
64404ff53f9SFlorian Fainelli 
64591da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
64691da11f8SLennert Buytenhek 
64791da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
64891da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
64991da11f8SLennert Buytenhek {
6507826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6517826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6527826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
65391da11f8SLennert Buytenhek }
65491da11f8SLennert Buytenhek 
6553d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6563d762a0fSGuenter Roeck {
657d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
658d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6593d762a0fSGuenter Roeck 
6609d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
661d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
6623d762a0fSGuenter Roeck 
6633d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6643d762a0fSGuenter Roeck }
6653d762a0fSGuenter Roeck 
6663d762a0fSGuenter Roeck static void
6673d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6683d762a0fSGuenter Roeck {
669d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
670d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6713d762a0fSGuenter Roeck 
6729d490b4eSVivien Didelot 	if (ds->ops->get_regs)
673d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
6743d762a0fSGuenter Roeck }
6753d762a0fSGuenter Roeck 
676aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
677aab9c406SFlorian Fainelli {
678aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
679aab9c406SFlorian Fainelli 
680aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
681aab9c406SFlorian Fainelli }
682aab9c406SFlorian Fainelli 
6836793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6846793abb4SGuenter Roeck {
685d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
686d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6876793abb4SGuenter Roeck 
6880e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
689ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
6906793abb4SGuenter Roeck 
6919d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
6929d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
6936793abb4SGuenter Roeck 
6946793abb4SGuenter Roeck 	return 0;
6956793abb4SGuenter Roeck }
6966793abb4SGuenter Roeck 
6976793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6986793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6996793abb4SGuenter Roeck {
700d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
701d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7026793abb4SGuenter Roeck 
7039d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7049d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7056793abb4SGuenter Roeck 
7066793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7076793abb4SGuenter Roeck }
7086793abb4SGuenter Roeck 
7096793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7106793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7116793abb4SGuenter Roeck {
712d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
713d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7146793abb4SGuenter Roeck 
7159d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7169d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7176793abb4SGuenter Roeck 
7186793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7196793abb4SGuenter Roeck }
7206793abb4SGuenter Roeck 
72191da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
72291da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
72391da11f8SLennert Buytenhek {
724d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
725d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
72691da11f8SLennert Buytenhek 
72791da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
72891da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
72991da11f8SLennert Buytenhek 
73091da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
73191da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
73291da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
73391da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
7349d490b4eSVivien Didelot 		if (ds->ops->get_strings)
73589f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
73689f09048SFlorian Fainelli 					     data + 4 * len);
737a71acad9SOleksij Rempel 	} else if (stringset ==  ETH_SS_TEST) {
738a71acad9SOleksij Rempel 		net_selftest_get_strings(data);
73991da11f8SLennert Buytenhek 	}
740a71acad9SOleksij Rempel 
74191da11f8SLennert Buytenhek }
74291da11f8SLennert Buytenhek 
74391da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
74491da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
74591da11f8SLennert Buytenhek 					uint64_t *data)
74691da11f8SLennert Buytenhek {
747d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
748d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7495f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
750f613ed66SFlorian Fainelli 	unsigned int start;
7515f6b4e14SFlorian Fainelli 	int i;
75291da11f8SLennert Buytenhek 
7535f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
7545f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
7555f6b4e14SFlorian Fainelli 
7566a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
757f613ed66SFlorian Fainelli 		do {
7585f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
7595f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
7605f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
7615f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
7625f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
7635f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
7645f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
7655f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
7665f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
7675f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
7685f6b4e14SFlorian Fainelli 	}
7699d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
770d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
77191da11f8SLennert Buytenhek }
77291da11f8SLennert Buytenhek 
77391da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
77491da11f8SLennert Buytenhek {
775d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
776d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
77791da11f8SLennert Buytenhek 
77891da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
77991da11f8SLennert Buytenhek 		int count;
78091da11f8SLennert Buytenhek 
78191da11f8SLennert Buytenhek 		count = 4;
7829d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
78389f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
78491da11f8SLennert Buytenhek 
78591da11f8SLennert Buytenhek 		return count;
786a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
787a71acad9SOleksij Rempel 		return net_selftest_get_count();
78891da11f8SLennert Buytenhek 	}
78991da11f8SLennert Buytenhek 
79091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
79191da11f8SLennert Buytenhek }
79291da11f8SLennert Buytenhek 
793a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
794a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
795a71acad9SOleksij Rempel {
796a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
797a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
798a71acad9SOleksij Rempel 
799a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
800a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
801a71acad9SOleksij Rempel 		return;
802a71acad9SOleksij Rempel 	}
803a71acad9SOleksij Rempel 
804a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
805a71acad9SOleksij Rempel }
806a71acad9SOleksij Rempel 
80719e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
80819e57c4eSFlorian Fainelli {
809d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
810d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
81119e57c4eSFlorian Fainelli 
812aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
813aab9c406SFlorian Fainelli 
8149d490b4eSVivien Didelot 	if (ds->ops->get_wol)
815d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
81619e57c4eSFlorian Fainelli }
81719e57c4eSFlorian Fainelli 
81819e57c4eSFlorian Fainelli static int dsa_slave_set_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 	int ret = -EOPNOTSUPP;
82319e57c4eSFlorian Fainelli 
824aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
825aab9c406SFlorian Fainelli 
8269d490b4eSVivien Didelot 	if (ds->ops->set_wol)
827d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
82819e57c4eSFlorian Fainelli 
82919e57c4eSFlorian Fainelli 	return ret;
83019e57c4eSFlorian Fainelli }
83119e57c4eSFlorian Fainelli 
8327905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
8337905288fSFlorian Fainelli {
834d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
835d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8367905288fSFlorian Fainelli 	int ret;
8377905288fSFlorian Fainelli 
8387b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
83900670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8407b9cc738SVivien Didelot 		return -ENODEV;
8417b9cc738SVivien Didelot 
84208f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
8437905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8447905288fSFlorian Fainelli 
845d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
8467905288fSFlorian Fainelli 	if (ret)
8477905288fSFlorian Fainelli 		return ret;
8487905288fSFlorian Fainelli 
849aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
8507905288fSFlorian Fainelli }
8517905288fSFlorian Fainelli 
8527905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8537905288fSFlorian Fainelli {
854d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
855d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8567905288fSFlorian Fainelli 	int ret;
8577905288fSFlorian Fainelli 
8587b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
85900670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8607b9cc738SVivien Didelot 		return -ENODEV;
8617b9cc738SVivien Didelot 
86208f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8637905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8647905288fSFlorian Fainelli 
865d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8667905288fSFlorian Fainelli 	if (ret)
8677905288fSFlorian Fainelli 		return ret;
8687905288fSFlorian Fainelli 
869aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
870aab9c406SFlorian Fainelli }
871aab9c406SFlorian Fainelli 
872aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
873aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
874aab9c406SFlorian Fainelli {
875aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
876aab9c406SFlorian Fainelli 
877aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
878aab9c406SFlorian Fainelli }
879aab9c406SFlorian Fainelli 
880aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
881aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
882aab9c406SFlorian Fainelli {
883aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
884aab9c406SFlorian Fainelli 
885aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8867905288fSFlorian Fainelli }
8877905288fSFlorian Fainelli 
888a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
889a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
890a2a1a13bSHeiner Kallweit {
891a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
892a2a1a13bSHeiner Kallweit 
893a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
894a2a1a13bSHeiner Kallweit }
895a2a1a13bSHeiner Kallweit 
896a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
897a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
898a2a1a13bSHeiner Kallweit {
899a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
900a2a1a13bSHeiner Kallweit 
901a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
902a2a1a13bSHeiner Kallweit }
903a2a1a13bSHeiner Kallweit 
90404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
90504ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
90604ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
90704ff53f9SFlorian Fainelli {
908d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
90904ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
91004ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
91104ff53f9SFlorian Fainelli 	int err = 0;
91204ff53f9SFlorian Fainelli 
91304ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
91404ff53f9SFlorian Fainelli 	if (!netpoll)
91504ff53f9SFlorian Fainelli 		return -ENOMEM;
91604ff53f9SFlorian Fainelli 
91704ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
91804ff53f9SFlorian Fainelli 	if (err) {
91904ff53f9SFlorian Fainelli 		kfree(netpoll);
92004ff53f9SFlorian Fainelli 		goto out;
92104ff53f9SFlorian Fainelli 	}
92204ff53f9SFlorian Fainelli 
92304ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
92404ff53f9SFlorian Fainelli out:
92504ff53f9SFlorian Fainelli 	return err;
92604ff53f9SFlorian Fainelli }
92704ff53f9SFlorian Fainelli 
92804ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
92904ff53f9SFlorian Fainelli {
93004ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
93104ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
93204ff53f9SFlorian Fainelli 
93304ff53f9SFlorian Fainelli 	if (!netpoll)
93404ff53f9SFlorian Fainelli 		return;
93504ff53f9SFlorian Fainelli 
93604ff53f9SFlorian Fainelli 	p->netpoll = NULL;
93704ff53f9SFlorian Fainelli 
938c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
93904ff53f9SFlorian Fainelli }
94004ff53f9SFlorian Fainelli 
94104ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
94204ff53f9SFlorian Fainelli {
94304ff53f9SFlorian Fainelli }
94404ff53f9SFlorian Fainelli #endif
94504ff53f9SFlorian Fainelli 
94644bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
94744bb765cSFlorian Fainelli 					char *name, size_t len)
94844bb765cSFlorian Fainelli {
949d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
95044bb765cSFlorian Fainelli 
951d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
952d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
953d484210bSJiri Pirko 	 * should be removed with legacy support.
954d484210bSJiri Pirko 	 */
955d484210bSJiri Pirko 	if (dp->ds->devlink)
956d484210bSJiri Pirko 		return -EOPNOTSUPP;
957d484210bSJiri Pirko 
958d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
95944bb765cSFlorian Fainelli 		return -EINVAL;
9603a543ef4SFlorian Fainelli 
9613a543ef4SFlorian Fainelli 	return 0;
9623a543ef4SFlorian Fainelli }
9633a543ef4SFlorian Fainelli 
964f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9654fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
966f50f2127SFlorian Fainelli {
9674fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
968f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
969f50f2127SFlorian Fainelli 
970f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
971f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
972f50f2127SFlorian Fainelli 			return mall_tc_entry;
973f50f2127SFlorian Fainelli 
974f50f2127SFlorian Fainelli 	return NULL;
975f50f2127SFlorian Fainelli }
976f50f2127SFlorian Fainelli 
977e13c2075SVladimir Oltean static int
978e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
979f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
980f50f2127SFlorian Fainelli 				  bool ingress)
981f50f2127SFlorian Fainelli {
982d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
983f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
984e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
985f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
986d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9879681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
988d945097bSVivien Didelot 	struct dsa_port *to_dp;
989e13c2075SVladimir Oltean 	int err;
990e13c2075SVladimir Oltean 
991f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
99234297176SVladimir Oltean 		return -EOPNOTSUPP;
993f50f2127SFlorian Fainelli 
99453eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
995319a1d19SJiri Pirko 					      cls->common.extack))
99634297176SVladimir Oltean 		return -EOPNOTSUPP;
997319a1d19SJiri Pirko 
9989681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
999f50f2127SFlorian Fainelli 
100065722159SVladimir Oltean 	if (!act->dev)
100165722159SVladimir Oltean 		return -EINVAL;
100265722159SVladimir Oltean 
10039681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1004f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1005f50f2127SFlorian Fainelli 
1006f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1007f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1008f50f2127SFlorian Fainelli 		return -ENOMEM;
1009f50f2127SFlorian Fainelli 
1010f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1011f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1012f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1013f50f2127SFlorian Fainelli 
10149681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1015f50f2127SFlorian Fainelli 
1016d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1017f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1018f50f2127SFlorian Fainelli 
1019d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1020f50f2127SFlorian Fainelli 	if (err) {
1021f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1022f50f2127SFlorian Fainelli 		return err;
1023f50f2127SFlorian Fainelli 	}
1024f50f2127SFlorian Fainelli 
1025f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1026e13c2075SVladimir Oltean 
1027e13c2075SVladimir Oltean 	return err;
1028f50f2127SFlorian Fainelli }
1029f50f2127SFlorian Fainelli 
103034297176SVladimir Oltean static int
103134297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
103234297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
103334297176SVladimir Oltean 				  bool ingress)
103434297176SVladimir Oltean {
103534297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
103634297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
103734297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
103834297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
103934297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
104034297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
104134297176SVladimir Oltean 	struct flow_action_entry *act;
104234297176SVladimir Oltean 	int err;
104334297176SVladimir Oltean 
104434297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
104534297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1046c75a33c8SJacob Keller 				   "Policing offload not implemented");
104734297176SVladimir Oltean 		return -EOPNOTSUPP;
104834297176SVladimir Oltean 	}
104934297176SVladimir Oltean 
105034297176SVladimir Oltean 	if (!ingress) {
105134297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1052c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
105334297176SVladimir Oltean 		return -EOPNOTSUPP;
105434297176SVladimir Oltean 	}
105534297176SVladimir Oltean 
105634297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
105734297176SVladimir Oltean 					      cls->common.extack))
105834297176SVladimir Oltean 		return -EOPNOTSUPP;
105934297176SVladimir Oltean 
106034297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
106134297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
106234297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1063c75a33c8SJacob Keller 					   "Only one port policer allowed");
106434297176SVladimir Oltean 			return -EEXIST;
106534297176SVladimir Oltean 		}
106634297176SVladimir Oltean 	}
106734297176SVladimir Oltean 
106834297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
106934297176SVladimir Oltean 
107034297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
107134297176SVladimir Oltean 	if (!mall_tc_entry)
107234297176SVladimir Oltean 		return -ENOMEM;
107334297176SVladimir Oltean 
107434297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
107534297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
107634297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
107734297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
107834297176SVladimir Oltean 	policer->burst = act->police.burst;
107934297176SVladimir Oltean 
108034297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
108134297176SVladimir Oltean 	if (err) {
108234297176SVladimir Oltean 		kfree(mall_tc_entry);
108334297176SVladimir Oltean 		return err;
108434297176SVladimir Oltean 	}
108534297176SVladimir Oltean 
108634297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
108734297176SVladimir Oltean 
108834297176SVladimir Oltean 	return err;
108934297176SVladimir Oltean }
109034297176SVladimir Oltean 
1091e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1092e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1093e13c2075SVladimir Oltean 				      bool ingress)
1094e13c2075SVladimir Oltean {
1095e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1096e13c2075SVladimir Oltean 
1097e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1098e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1099e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1100e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
110134297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
110234297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
110334297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1104e13c2075SVladimir Oltean 
1105e13c2075SVladimir Oltean 	return err;
1106f50f2127SFlorian Fainelli }
1107f50f2127SFlorian Fainelli 
1108f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1109f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1110f50f2127SFlorian Fainelli {
1111d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1112f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1113d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1114f50f2127SFlorian Fainelli 
11154fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1116f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1117f50f2127SFlorian Fainelli 		return;
1118f50f2127SFlorian Fainelli 
1119f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1120f50f2127SFlorian Fainelli 
1121f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1122f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
112334297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
112434297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
112534297176SVladimir Oltean 						 &mall_tc_entry->mirror);
112634297176SVladimir Oltean 		break;
112734297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
112834297176SVladimir Oltean 		if (ds->ops->port_policer_del)
112934297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1130f50f2127SFlorian Fainelli 		break;
1131f50f2127SFlorian Fainelli 	default:
1132f50f2127SFlorian Fainelli 		WARN_ON(1);
1133f50f2127SFlorian Fainelli 	}
1134f50f2127SFlorian Fainelli 
1135f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1136f50f2127SFlorian Fainelli }
1137f50f2127SFlorian Fainelli 
11383fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
11396b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
11406b3eb752SJiri Pirko 					   bool ingress)
1141f50f2127SFlorian Fainelli {
11425fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1143a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1144f50f2127SFlorian Fainelli 
11453fbae382SJiri Pirko 	switch (cls->command) {
11463fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
11475fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
11483fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
11493fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
11503fbae382SJiri Pirko 		return 0;
11513fbae382SJiri Pirko 	default:
11523fbae382SJiri Pirko 		return -EOPNOTSUPP;
11533fbae382SJiri Pirko 	}
11543fbae382SJiri Pirko }
11553fbae382SJiri Pirko 
1156ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1157ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1158ed11bb1fSVladimir Oltean 				    bool ingress)
1159ed11bb1fSVladimir Oltean {
1160ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1161ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1162ed11bb1fSVladimir Oltean 	int port = dp->index;
1163ed11bb1fSVladimir Oltean 
1164ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1165ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1166ed11bb1fSVladimir Oltean 
1167ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1168ed11bb1fSVladimir Oltean }
1169ed11bb1fSVladimir Oltean 
1170ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1171ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1172ed11bb1fSVladimir Oltean 				    bool ingress)
1173ed11bb1fSVladimir Oltean {
1174ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1175ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1176ed11bb1fSVladimir Oltean 	int port = dp->index;
1177ed11bb1fSVladimir Oltean 
1178ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1179ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1180ed11bb1fSVladimir Oltean 
1181ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1182ed11bb1fSVladimir Oltean }
1183ed11bb1fSVladimir Oltean 
1184ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1185ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1186ed11bb1fSVladimir Oltean 				      bool ingress)
1187ed11bb1fSVladimir Oltean {
1188ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1189ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1190ed11bb1fSVladimir Oltean 	int port = dp->index;
1191ed11bb1fSVladimir Oltean 
1192ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1193ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1194ed11bb1fSVladimir Oltean 
1195ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1196ed11bb1fSVladimir Oltean }
1197ed11bb1fSVladimir Oltean 
1198ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1199ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1200ed11bb1fSVladimir Oltean 					 bool ingress)
1201ed11bb1fSVladimir Oltean {
1202ed11bb1fSVladimir Oltean 	switch (cls->command) {
1203ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1204ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1205ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1206ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1207ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1208ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1209ed11bb1fSVladimir Oltean 	default:
1210ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1211ed11bb1fSVladimir Oltean 	}
1212ed11bb1fSVladimir Oltean }
1213ed11bb1fSVladimir Oltean 
12146b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12156b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
12166b3eb752SJiri Pirko {
12176b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
12186b3eb752SJiri Pirko 
121944ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
122044ae12a7SJiri Pirko 		return -EOPNOTSUPP;
122144ae12a7SJiri Pirko 
12226b3eb752SJiri Pirko 	switch (type) {
12236b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
12246b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1225ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1226ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
12276b3eb752SJiri Pirko 	default:
12286b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12296b3eb752SJiri Pirko 	}
12306b3eb752SJiri Pirko }
12316b3eb752SJiri Pirko 
12326b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
12336b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12346b3eb752SJiri Pirko {
12356b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
12366b3eb752SJiri Pirko }
12376b3eb752SJiri Pirko 
12386b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
12396b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12406b3eb752SJiri Pirko {
12416b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
12426b3eb752SJiri Pirko }
12436b3eb752SJiri Pirko 
1244955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1245955bcb6eSPablo Neira Ayuso 
12466b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1247955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
12486b3eb752SJiri Pirko {
1249955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1250a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
12516b3eb752SJiri Pirko 
125232f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12536b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
125432f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12556b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12566b3eb752SJiri Pirko 	else
12576b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12586b3eb752SJiri Pirko 
1259955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1260955bcb6eSPablo Neira Ayuso 
12616b3eb752SJiri Pirko 	switch (f->command) {
12629c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12630d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12640d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12650d4fd02eSPablo Neira Ayuso 
12660c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1267955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1268955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1269955bcb6eSPablo Neira Ayuso 
1270955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1271955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1272955bcb6eSPablo Neira Ayuso 		return 0;
12739c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
127414bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1275955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1276955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1277955bcb6eSPablo Neira Ayuso 
1278955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1279955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12806b3eb752SJiri Pirko 		return 0;
12816b3eb752SJiri Pirko 	default:
12826b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12836b3eb752SJiri Pirko 	}
12846b3eb752SJiri Pirko }
12856b3eb752SJiri Pirko 
12863fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
12873fb24a43SPablo Neira Ayuso 				    void *type_data)
12883fb24a43SPablo Neira Ayuso {
12893fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
12903fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
12913fb24a43SPablo Neira Ayuso 
12923fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
12933fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
12943fb24a43SPablo Neira Ayuso 
12953fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
12963fb24a43SPablo Neira Ayuso }
12973fb24a43SPablo Neira Ayuso 
12983fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1299de4784caSJiri Pirko 			      void *type_data)
13003fbae382SJiri Pirko {
130147d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
130247d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
130347d23af2SVladimir Oltean 
13043fb24a43SPablo Neira Ayuso 	switch (type) {
13053fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
13066b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
13073fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
13083fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
13093fb24a43SPablo Neira Ayuso 	default:
13103fb24a43SPablo Neira Ayuso 		break;
13113fb24a43SPablo Neira Ayuso 	}
131247d23af2SVladimir Oltean 
131347d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1314a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
131547d23af2SVladimir Oltean 
131647d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1317f50f2127SFlorian Fainelli }
1318f50f2127SFlorian Fainelli 
1319bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1320bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1321bf9f2648SFlorian Fainelli {
1322d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1323d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1324bf9f2648SFlorian Fainelli 
1325bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1326bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1327bf9f2648SFlorian Fainelli 
1328d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1329bf9f2648SFlorian Fainelli }
1330bf9f2648SFlorian Fainelli 
1331bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1332bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1333bf9f2648SFlorian Fainelli {
1334d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1335d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1336bf9f2648SFlorian Fainelli 
1337bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1338bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1339bf9f2648SFlorian Fainelli 
1340d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1341bf9f2648SFlorian Fainelli }
1342bf9f2648SFlorian Fainelli 
13430336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
13440336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
13450336369dSBrandon Streiff {
13460336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
13470336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
13480336369dSBrandon Streiff 
13490336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
13500336369dSBrandon Streiff 		return -EOPNOTSUPP;
13510336369dSBrandon Streiff 
13520336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
13530336369dSBrandon Streiff }
13540336369dSBrandon Streiff 
1355061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1356061f6a50SFlorian Fainelli 				     u16 vid)
1357061f6a50SFlorian Fainelli {
13582209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1359061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
136088236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
136188236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1362b7a9e0daSVladimir Oltean 		.vid = vid,
136388236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
136488236591SVladimir Oltean 		.flags = 0,
136588236591SVladimir Oltean 	};
136631046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1367061f6a50SFlorian Fainelli 	int ret;
1368061f6a50SFlorian Fainelli 
136988236591SVladimir Oltean 	/* User port... */
137031046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
137131046a5fSVladimir Oltean 	if (ret) {
137231046a5fSVladimir Oltean 		if (extack._msg)
137331046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
137488236591SVladimir Oltean 		return ret;
137531046a5fSVladimir Oltean 	}
137688236591SVladimir Oltean 
137788236591SVladimir Oltean 	/* And CPU port... */
137831046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
137931046a5fSVladimir Oltean 	if (ret) {
138031046a5fSVladimir Oltean 		if (extack._msg)
138131046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
138231046a5fSVladimir Oltean 				   extack._msg);
13837e1741b4SVivien Didelot 		return ret;
138431046a5fSVladimir Oltean 	}
13857e1741b4SVivien Didelot 
13862209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1387061f6a50SFlorian Fainelli }
1388061f6a50SFlorian Fainelli 
1389061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1390061f6a50SFlorian Fainelli 				      u16 vid)
1391061f6a50SFlorian Fainelli {
13922209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1393061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
139488236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1395b7a9e0daSVladimir Oltean 		.vid = vid,
139688236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
139788236591SVladimir Oltean 		.flags = 0,
139888236591SVladimir Oltean 	};
13992209158cSVladimir Oltean 	int err;
1400061f6a50SFlorian Fainelli 
14017e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
14027e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
14037e1741b4SVivien Didelot 	 */
14042209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
14052209158cSVladimir Oltean 	if (err)
14062209158cSVladimir Oltean 		return err;
14072209158cSVladimir Oltean 
14082209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
14092209158cSVladimir Oltean 
14102209158cSVladimir Oltean 	return 0;
1411061f6a50SFlorian Fainelli }
1412061f6a50SFlorian Fainelli 
1413bff33f7eSVladimir Oltean struct dsa_hw_port {
1414bff33f7eSVladimir Oltean 	struct list_head list;
1415bff33f7eSVladimir Oltean 	struct net_device *dev;
1416bff33f7eSVladimir Oltean 	int old_mtu;
1417bff33f7eSVladimir Oltean };
1418bff33f7eSVladimir Oltean 
1419bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1420bff33f7eSVladimir Oltean {
1421bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1422bff33f7eSVladimir Oltean 	int err;
1423bff33f7eSVladimir Oltean 
1424bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1425bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1426bff33f7eSVladimir Oltean 			continue;
1427bff33f7eSVladimir Oltean 
1428bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1429bff33f7eSVladimir Oltean 		if (err)
1430bff33f7eSVladimir Oltean 			goto rollback;
1431bff33f7eSVladimir Oltean 	}
1432bff33f7eSVladimir Oltean 
1433bff33f7eSVladimir Oltean 	return 0;
1434bff33f7eSVladimir Oltean 
1435bff33f7eSVladimir Oltean rollback:
1436bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1437bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1438bff33f7eSVladimir Oltean 			continue;
1439bff33f7eSVladimir Oltean 
1440bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1441bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1442bff33f7eSVladimir Oltean 	}
1443bff33f7eSVladimir Oltean 
1444bff33f7eSVladimir Oltean 	return err;
1445bff33f7eSVladimir Oltean }
1446bff33f7eSVladimir Oltean 
1447bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1448bff33f7eSVladimir Oltean {
1449bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1450bff33f7eSVladimir Oltean 
1451bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1452bff33f7eSVladimir Oltean 		kfree(p);
1453bff33f7eSVladimir Oltean }
1454bff33f7eSVladimir Oltean 
1455bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1456bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1457bff33f7eSVladimir Oltean {
1458bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1459bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1460bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1461bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1462bff33f7eSVladimir Oltean 	int err;
1463bff33f7eSVladimir Oltean 
1464bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1465bff33f7eSVladimir Oltean 		return;
1466bff33f7eSVladimir Oltean 
1467bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1468bff33f7eSVladimir Oltean 		return;
1469bff33f7eSVladimir Oltean 
1470bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1471bff33f7eSVladimir Oltean 
1472bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1473bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1474bff33f7eSVladimir Oltean 	 */
1475bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1476bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1477bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1478bff33f7eSVladimir Oltean 			struct net_device *slave;
1479bff33f7eSVladimir Oltean 
1480bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1481bff33f7eSVladimir Oltean 				continue;
1482bff33f7eSVladimir Oltean 
1483bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1484bff33f7eSVladimir Oltean 				continue;
1485bff33f7eSVladimir Oltean 
1486bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1487bff33f7eSVladimir Oltean 				continue;
1488bff33f7eSVladimir Oltean 
1489bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1490bff33f7eSVladimir Oltean 
1491bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1492bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1493bff33f7eSVladimir Oltean 
1494bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1495bff33f7eSVladimir Oltean 			if (!hw_port)
1496bff33f7eSVladimir Oltean 				goto out;
1497bff33f7eSVladimir Oltean 
1498bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1499bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1500bff33f7eSVladimir Oltean 
1501bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1502bff33f7eSVladimir Oltean 		}
1503bff33f7eSVladimir Oltean 	}
1504bff33f7eSVladimir Oltean 
1505bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1506bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1507bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1508bff33f7eSVladimir Oltean 	 */
1509bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1510bff33f7eSVladimir Oltean 	if (!err)
1511bff33f7eSVladimir Oltean 		goto out;
1512bff33f7eSVladimir Oltean 
1513bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1514bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1515bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1516bff33f7eSVladimir Oltean 	 */
1517bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1518bff33f7eSVladimir Oltean 
1519bff33f7eSVladimir Oltean out:
1520bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1521bff33f7eSVladimir Oltean }
1522bff33f7eSVladimir Oltean 
152353da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1524bfcb8132SVladimir Oltean {
1525bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1526bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1527bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1528bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1529bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1530bfcb8132SVladimir Oltean 	int port = p->dp->index;
1531bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1532bfcb8132SVladimir Oltean 	int new_master_mtu;
1533bfcb8132SVladimir Oltean 	int old_master_mtu;
1534bfcb8132SVladimir Oltean 	int mtu_limit;
1535bfcb8132SVladimir Oltean 	int cpu_mtu;
1536bfcb8132SVladimir Oltean 	int err, i;
1537bfcb8132SVladimir Oltean 
1538bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1539bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1540bfcb8132SVladimir Oltean 
1541bfcb8132SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1542bfcb8132SVladimir Oltean 		int slave_mtu;
1543bfcb8132SVladimir Oltean 
1544bfcb8132SVladimir Oltean 		if (!dsa_is_user_port(ds, i))
1545bfcb8132SVladimir Oltean 			continue;
1546bfcb8132SVladimir Oltean 
1547bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1548bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1549bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1550bfcb8132SVladimir Oltean 		 */
1551bfcb8132SVladimir Oltean 		if (!dsa_to_port(ds, i)->slave)
1552bfcb8132SVladimir Oltean 			continue;
1553bfcb8132SVladimir Oltean 
1554bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1555bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1556bfcb8132SVladimir Oltean 		 */
1557bfcb8132SVladimir Oltean 		if (i == port)
1558bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1559bfcb8132SVladimir Oltean 		else
1560bfcb8132SVladimir Oltean 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1561bfcb8132SVladimir Oltean 
1562bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1563bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1564bfcb8132SVladimir Oltean 	}
1565bfcb8132SVladimir Oltean 
1566bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1567bfcb8132SVladimir Oltean 
1568bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1569bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
1570bfcb8132SVladimir Oltean 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1571bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1572bfcb8132SVladimir Oltean 		return -ERANGE;
1573bfcb8132SVladimir Oltean 
1574bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1575bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1576bfcb8132SVladimir Oltean 	 */
1577bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1578bfcb8132SVladimir Oltean 
1579bfcb8132SVladimir Oltean 	/* Start applying stuff */
1580bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1581bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1582bfcb8132SVladimir Oltean 		if (err < 0)
1583bfcb8132SVladimir Oltean 			goto out_master_failed;
1584bfcb8132SVladimir Oltean 
1585bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1586bfcb8132SVladimir Oltean 		 * upstream switches.
1587bfcb8132SVladimir Oltean 		 */
1588bfcb8132SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1589bfcb8132SVladimir Oltean 		if (err)
1590bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1591bfcb8132SVladimir Oltean 	}
1592bfcb8132SVladimir Oltean 
1593bfcb8132SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, false);
1594bfcb8132SVladimir Oltean 	if (err)
1595bfcb8132SVladimir Oltean 		goto out_port_failed;
1596bfcb8132SVladimir Oltean 
1597bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1598bfcb8132SVladimir Oltean 
1599bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1600bff33f7eSVladimir Oltean 
1601bfcb8132SVladimir Oltean 	return 0;
1602bfcb8132SVladimir Oltean 
1603bfcb8132SVladimir Oltean out_port_failed:
1604bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1605bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1606bfcb8132SVladimir Oltean 				    cpu_dp->tag_ops->overhead,
1607bfcb8132SVladimir Oltean 				    true);
1608bfcb8132SVladimir Oltean out_cpu_failed:
1609bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1610bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1611bfcb8132SVladimir Oltean out_master_failed:
1612bfcb8132SVladimir Oltean 	return err;
1613bfcb8132SVladimir Oltean }
1614bfcb8132SVladimir Oltean 
161591da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
161691da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
16173d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
16183d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1619aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1620c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
16216793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
16226793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
16236793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
162491da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
162591da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
162691da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
162719e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
162819e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
16297905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
16307905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1631aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1632aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1633a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1634a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1635bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1636bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
16370336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
1638a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
163991da11f8SLennert Buytenhek };
164091da11f8SLennert Buytenhek 
16412a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
16424b9c9358SVladimir Oltean static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
16432a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16442a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid,
164587b0984eSPetr Machata 			      u16 flags,
164687b0984eSPetr Machata 			      struct netlink_ext_ack *extack)
16472a93c1a3SFlorian Fainelli {
16482a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16492a93c1a3SFlorian Fainelli 
16502a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
16512a93c1a3SFlorian Fainelli }
16522a93c1a3SFlorian Fainelli 
16534b9c9358SVladimir Oltean static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
16542a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16552a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid)
16562a93c1a3SFlorian Fainelli {
16572a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16582a93c1a3SFlorian Fainelli 
16592a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
16602a93c1a3SFlorian Fainelli }
16612a93c1a3SFlorian Fainelli 
1662716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1663716efee2SJiri Pirko {
1664716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1665716efee2SJiri Pirko 
1666716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1667716efee2SJiri Pirko }
1668716efee2SJiri Pirko 
1669c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1670c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1671c2ec5f2eSOleksij Rempel {
1672c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1673c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1674c2ec5f2eSOleksij Rempel 
1675c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1676c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1677c2ec5f2eSOleksij Rempel 	else
1678c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1679c2ec5f2eSOleksij Rempel }
1680c2ec5f2eSOleksij Rempel 
16810994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
16820994d492SFelix Fietkau 				       struct net_device_path *path)
16830994d492SFelix Fietkau {
16840994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
16850994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
16860994d492SFelix Fietkau 
16870994d492SFelix Fietkau 	path->dev = ctx->dev;
16880994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
16890994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
16900994d492SFelix Fietkau 	path->dsa.port = dp->index;
16910994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
16920994d492SFelix Fietkau 
16930994d492SFelix Fietkau 	return 0;
16940994d492SFelix Fietkau }
16950994d492SFelix Fietkau 
16963e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1697d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1698d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
16993e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1700d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1701d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1702d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
170337b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
170437b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
17052bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1706d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1707abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
170804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
170904ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
171004ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
171104ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
171204ff53f9SFlorian Fainelli #endif
171344bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1714f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1715c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1716929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1717061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1718061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1719716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1720bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
17210994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
172298237d43SScott Feldman };
172398237d43SScott Feldman 
1724f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1725f37db85dSFlorian Fainelli 	.name	= "dsa",
1726f37db85dSFlorian Fainelli };
1727f37db85dSFlorian Fainelli 
1728aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1729aab9c406SFlorian Fainelli {
1730aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1731aab9c406SFlorian Fainelli 
1732765bda93SRussell King 	if (dp->pl)
1733aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1734aab9c406SFlorian Fainelli }
1735aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1736aab9c406SFlorian Fainelli 
17375c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1738aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1739aab9c406SFlorian Fainelli {
17405c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1741aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1742aab9c406SFlorian Fainelli 
1743aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1744aab9c406SFlorian Fainelli 	 * not be called if it was not.
1745aab9c406SFlorian Fainelli 	 */
1746aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1747ce31b31cSFlorian Fainelli }
1748ce31b31cSFlorian Fainelli 
174991da11f8SLennert Buytenhek /* slave device setup *******************************************************/
17504fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1751c305c165SFlorian Fainelli {
1752d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1753d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1754c305c165SFlorian Fainelli 
17550115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
17560115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1757d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1758c305c165SFlorian Fainelli 		return -ENODEV;
1759d25b8e74SRussell King 	}
1760c305c165SFlorian Fainelli 
1761aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1762c305c165SFlorian Fainelli }
1763c305c165SFlorian Fainelli 
17644fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
17650d8bcdd3SFlorian Fainelli {
1766d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1767d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1768d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
17690c65b2b9SAndrew Lunn 	phy_interface_t mode;
17706819563eSFlorian Fainelli 	u32 phy_flags = 0;
17710c65b2b9SAndrew Lunn 	int ret;
17720d8bcdd3SFlorian Fainelli 
17730c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
17740c65b2b9SAndrew Lunn 	if (ret)
177519334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
17760d8bcdd3SFlorian Fainelli 
177744cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
177844cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
177944cc27e4SIoana Ciornei 
17805c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
17815c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
17825c05c1dbSRussell King 	 * this if the switch provides such a callback.
17835c05c1dbSRussell King 	 */
17845c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
17855c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
17865c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
17875c05c1dbSRussell King 	}
17885c05c1dbSRussell King 
178944cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
179077373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1791aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1792aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1793aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1794aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1795aab9c406SFlorian Fainelli 	}
1796aab9c406SFlorian Fainelli 
17979d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1798d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
17996819563eSFlorian Fainelli 
1800aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
18016146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
18026146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
18036146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
18040d8bcdd3SFlorian Fainelli 		 */
1805d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1806d25b8e74SRussell King 		if (ret) {
1807aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1808aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1809d945097bSVivien Didelot 				   dp->index, ret);
1810aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1811c305c165SFlorian Fainelli 			return ret;
1812d25b8e74SRussell King 		}
18130d8bcdd3SFlorian Fainelli 	}
18149697f1cdSFlorian Fainelli 
18156146dd45SVladimir Oltean 	return ret;
1816b31f65fbSAndrew Lunn }
18170d8bcdd3SFlorian Fainelli 
181853da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
181953da0ebaSVladimir Oltean {
182053da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
182153da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
182253da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
182353da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
182453da0ebaSVladimir Oltean 
182553da0ebaSVladimir Oltean 	if (cpu_dp->tag_ops->tail_tag)
182653da0ebaSVladimir Oltean 		slave->needed_tailroom = cpu_dp->tag_ops->overhead;
182753da0ebaSVladimir Oltean 	else
182853da0ebaSVladimir Oltean 		slave->needed_headroom = cpu_dp->tag_ops->overhead;
182953da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
183053da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
183153da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
183253da0ebaSVladimir Oltean 	 */
183353da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
183453da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
183553da0ebaSVladimir Oltean 
183653da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
183753da0ebaSVladimir Oltean }
183853da0ebaSVladimir Oltean 
18391a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
18401a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
18411a33e10eSCong Wang 					    struct netdev_queue *txq,
18421a33e10eSCong Wang 					    void *_unused)
18431a33e10eSCong Wang {
18441a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
18451a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
18461a33e10eSCong Wang }
18471a33e10eSCong Wang 
184824462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
184924462549SFlorian Fainelli {
1850aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
185124462549SFlorian Fainelli 
1852a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1853a94c689eSFlorian Fainelli 		return 0;
1854a94c689eSFlorian Fainelli 
1855f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1856f154be24SFlorian Fainelli 
1857aab9c406SFlorian Fainelli 	rtnl_lock();
1858aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1859aab9c406SFlorian Fainelli 	rtnl_unlock();
186024462549SFlorian Fainelli 
186124462549SFlorian Fainelli 	return 0;
186224462549SFlorian Fainelli }
186324462549SFlorian Fainelli 
186424462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
186524462549SFlorian Fainelli {
1866aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1867aab9c406SFlorian Fainelli 
1868a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1869a94c689eSFlorian Fainelli 		return 0;
1870a94c689eSFlorian Fainelli 
187124462549SFlorian Fainelli 	netif_device_attach(slave_dev);
187224462549SFlorian Fainelli 
1873aab9c406SFlorian Fainelli 	rtnl_lock();
1874aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1875aab9c406SFlorian Fainelli 	rtnl_unlock();
187624462549SFlorian Fainelli 
187724462549SFlorian Fainelli 	return 0;
187824462549SFlorian Fainelli }
187924462549SFlorian Fainelli 
1880951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
188191da11f8SLennert Buytenhek {
188224a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1883f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
18844cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1885951259aaSVivien Didelot 	const char *name = port->name;
188691da11f8SLennert Buytenhek 	struct net_device *slave_dev;
188791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
188891da11f8SLennert Buytenhek 	int ret;
188991da11f8SLennert Buytenhek 
189055199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
189155199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
189255199df6SFlorian Fainelli 
189355199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
189455199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
189555199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
189691da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1897d87d6f44SGuenter Roeck 		return -ENOMEM;
189891da11f8SLennert Buytenhek 
18999b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
19009b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
19019b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1902f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
19032b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
19047ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
190583216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
1906a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1907a2c7023fSXiaofei Shen 	else
19082fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
19090a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
19103e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1911bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1912bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1913f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1914d442ad4aSStephen Hemminger 
19151a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
19161a33e10eSCong Wang 				 NULL);
19171a33e10eSCong Wang 
19184cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
19194cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
192091da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
192191da11f8SLennert Buytenhek 
192291da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
19236a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
19246a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
19255f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
19265f6b4e14SFlorian Fainelli 		return -ENOMEM;
19275f6b4e14SFlorian Fainelli 	}
1928e131a563SAlexander Lobakin 
1929e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1930e131a563SAlexander Lobakin 	if (ret)
1931e131a563SAlexander Lobakin 		goto out_free;
1932e131a563SAlexander Lobakin 
19334cfbf09cSVivien Didelot 	p->dp = port;
1934f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
1935f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
193653da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
193791da11f8SLennert Buytenhek 
1938bfcb8132SVladimir Oltean 	rtnl_lock();
1939bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1940bfcb8132SVladimir Oltean 	rtnl_unlock();
19414349abdbSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
1942bdc40a3fSRasmus Villemoes 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1943bdc40a3fSRasmus Villemoes 			 ret, ETH_DATA_LEN, port->index);
1944bfcb8132SVladimir Oltean 
194591da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
194691da11f8SLennert Buytenhek 
19474fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
19480071f56eSAndrew Lunn 	if (ret) {
1949c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
1950c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1951c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
1952e131a563SAlexander Lobakin 		goto out_gcells;
1953e804441cSFlorian Fainelli 	}
1954e804441cSFlorian Fainelli 
19552f1e8ea7SVladimir Oltean 	rtnl_lock();
19562f1e8ea7SVladimir Oltean 
19572f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1958e804441cSFlorian Fainelli 	if (ret) {
1959e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1960e804441cSFlorian Fainelli 			   ret, slave_dev->name);
19612f1e8ea7SVladimir Oltean 		rtnl_unlock();
1962e804441cSFlorian Fainelli 		goto out_phy;
19630071f56eSAndrew Lunn 	}
19640071f56eSAndrew Lunn 
19652f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
19662f1e8ea7SVladimir Oltean 
19672f1e8ea7SVladimir Oltean 	rtnl_unlock();
19682f1e8ea7SVladimir Oltean 
19692f1e8ea7SVladimir Oltean 	if (ret)
19702f1e8ea7SVladimir Oltean 		goto out_unregister;
19712f1e8ea7SVladimir Oltean 
1972d87d6f44SGuenter Roeck 	return 0;
1973e804441cSFlorian Fainelli 
19742f1e8ea7SVladimir Oltean out_unregister:
19752f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1976e804441cSFlorian Fainelli out_phy:
1977aab9c406SFlorian Fainelli 	rtnl_lock();
1978aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1979aab9c406SFlorian Fainelli 	rtnl_unlock();
1980aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1981e131a563SAlexander Lobakin out_gcells:
1982e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1983e804441cSFlorian Fainelli out_free:
19846a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1985e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1986f8b8b1cdSVivien Didelot 	port->slave = NULL;
1987e804441cSFlorian Fainelli 	return ret;
198891da11f8SLennert Buytenhek }
1989b73adef6SFlorian Fainelli 
1990cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1991cda5c15bSNeil Armstrong {
19922f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
1993d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1994cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1995cda5c15bSNeil Armstrong 
1996cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1997aab9c406SFlorian Fainelli 	rtnl_lock();
19982f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
19992f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2000aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2001aab9c406SFlorian Fainelli 	rtnl_unlock();
2002881eadabSJohan Hovold 
2003aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2004e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
20056a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2006cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2007cda5c15bSNeil Armstrong }
2008cda5c15bSNeil Armstrong 
20094d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2010b73adef6SFlorian Fainelli {
2011b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2012b73adef6SFlorian Fainelli }
2013a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2014b73adef6SFlorian Fainelli 
20158e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
20168e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2017b73adef6SFlorian Fainelli {
2018d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
20192afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
20208e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2021b73adef6SFlorian Fainelli 
20222afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20232afc526aSVladimir Oltean 
20248e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
20258e92ab3aSVivien Didelot 		if (info->linking) {
20262afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2027bff33f7eSVladimir Oltean 			if (!err)
2028bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
20298e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
20308e92ab3aSVivien Didelot 		} else {
203117d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
20328e92ab3aSVivien Didelot 			err = NOTIFY_OK;
20338e92ab3aSVivien Didelot 		}
2034058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2035058102a6STobias Waldekranz 		if (info->linking) {
2036058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
20372afc526aSVladimir Oltean 						info->upper_info, extack);
2038058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2039058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2040058102a6STobias Waldekranz 						   "Offloading not supported");
2041058102a6STobias Waldekranz 				err = 0;
2042058102a6STobias Waldekranz 			}
2043058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2044058102a6STobias Waldekranz 		} else {
2045058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2046058102a6STobias Waldekranz 			err = NOTIFY_OK;
2047058102a6STobias Waldekranz 		}
204818596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
204918596f50SGeorge McCollister 		if (info->linking) {
205018596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
205118596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
205218596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
205318596f50SGeorge McCollister 						   "Offloading not supported");
205418596f50SGeorge McCollister 				err = 0;
205518596f50SGeorge McCollister 			}
205618596f50SGeorge McCollister 			err = notifier_from_errno(err);
205718596f50SGeorge McCollister 		} else {
205818596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
205918596f50SGeorge McCollister 			err = NOTIFY_OK;
206018596f50SGeorge McCollister 		}
2061058102a6STobias Waldekranz 	}
2062058102a6STobias Waldekranz 
2063058102a6STobias Waldekranz 	return err;
2064058102a6STobias Waldekranz }
2065058102a6STobias Waldekranz 
2066058102a6STobias Waldekranz static int
2067058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2068058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2069058102a6STobias Waldekranz {
2070058102a6STobias Waldekranz 	struct net_device *lower;
2071058102a6STobias Waldekranz 	struct list_head *iter;
2072058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2073058102a6STobias Waldekranz 	struct dsa_port *dp;
2074058102a6STobias Waldekranz 
2075058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2076058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2077058102a6STobias Waldekranz 			continue;
2078058102a6STobias Waldekranz 
2079058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2080058102a6STobias Waldekranz 		if (!dp->lag_dev)
2081058102a6STobias Waldekranz 			/* Software LAG */
2082058102a6STobias Waldekranz 			continue;
2083058102a6STobias Waldekranz 
2084058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2085058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2086058102a6STobias Waldekranz 			break;
20876debb68aSVivien Didelot 	}
2088b73adef6SFlorian Fainelli 
20898e92ab3aSVivien Didelot 	return err;
2090b73adef6SFlorian Fainelli }
2091b73adef6SFlorian Fainelli 
2092eb46e8daSVladimir Oltean static int
2093eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2094eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2095cc1d5bdaSFlorian Fainelli {
2096cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
2097cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
2098cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2099cc1d5bdaSFlorian Fainelli 
2100cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2101cc1d5bdaSFlorian Fainelli 
2102cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2103cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2104cc1d5bdaSFlorian Fainelli 
2105cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2106cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2107cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2108cc1d5bdaSFlorian Fainelli 
2109cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
2110cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
2111cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2112cc1d5bdaSFlorian Fainelli 
2113cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2114cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
2115cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2116cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2117cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2118cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2119cc1d5bdaSFlorian Fainelli 	}
2120cc1d5bdaSFlorian Fainelli 
2121cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2122cc1d5bdaSFlorian Fainelli }
2123cc1d5bdaSFlorian Fainelli 
21242b138406SVladimir Oltean static int
21252b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
21262b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
21272b138406SVladimir Oltean {
21282b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
21292b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
21302b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
21312b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
21322b138406SVladimir Oltean 	int err = NOTIFY_DONE;
21332b138406SVladimir Oltean 	u16 vid;
21342b138406SVladimir Oltean 
2135adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
21362b138406SVladimir Oltean 		return NOTIFY_DONE;
21372b138406SVladimir Oltean 
21382b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
21392b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
21402b138406SVladimir Oltean 
21412b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
21422b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
21432b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
21442b138406SVladimir Oltean 	 */
21452b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
21462b138406SVladimir Oltean 	if (err == 0) {
21472b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
21482b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
21492b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
21502b138406SVladimir Oltean 	}
21512b138406SVladimir Oltean 
21522b138406SVladimir Oltean 	return NOTIFY_DONE;
21532b138406SVladimir Oltean }
21542b138406SVladimir Oltean 
215588e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
2156b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
2157b73adef6SFlorian Fainelli {
21586debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2159b73adef6SFlorian Fainelli 
216083501299SVladimir Oltean 	switch (event) {
21612b138406SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
21622b138406SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
2163e358bef7SVladimir Oltean 		struct dsa_switch *ds;
2164e358bef7SVladimir Oltean 		struct dsa_port *dp;
2165e358bef7SVladimir Oltean 		int err;
21662b138406SVladimir Oltean 
216753bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
2168eb46e8daSVladimir Oltean 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
21692b138406SVladimir Oltean 
2170e358bef7SVladimir Oltean 		dp = dsa_slave_to_port(dev);
2171e358bef7SVladimir Oltean 		ds = dp->ds;
2172e358bef7SVladimir Oltean 
2173e358bef7SVladimir Oltean 		if (ds->ops->port_prechangeupper) {
2174e358bef7SVladimir Oltean 			err = ds->ops->port_prechangeupper(ds, dp->index, info);
2175e358bef7SVladimir Oltean 			if (err)
2176e358bef7SVladimir Oltean 				return notifier_from_errno(err);
2177e358bef7SVladimir Oltean 		}
2178e358bef7SVladimir Oltean 
21792b138406SVladimir Oltean 		if (is_vlan_dev(info->upper_dev))
21802b138406SVladimir Oltean 			return dsa_slave_check_8021q_upper(dev, ptr);
218183501299SVladimir Oltean 		break;
21822b138406SVladimir Oltean 	}
218383501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2184058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
21858e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2186058102a6STobias Waldekranz 
2187058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2188058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2189058102a6STobias Waldekranz 
2190058102a6STobias Waldekranz 		break;
2191058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2192058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2193058102a6STobias Waldekranz 		struct dsa_port *dp;
2194058102a6STobias Waldekranz 		int err;
2195058102a6STobias Waldekranz 
2196058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2197058102a6STobias Waldekranz 			break;
2198058102a6STobias Waldekranz 
2199058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2200058102a6STobias Waldekranz 
2201058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2202058102a6STobias Waldekranz 		return notifier_from_errno(err);
2203058102a6STobias Waldekranz 	}
2204c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2205c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2206c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2207c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2208c0a8a9c2SVladimir Oltean 
2209c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2210c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2211c0a8a9c2SVladimir Oltean 
2212c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2213c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2214c0a8a9c2SVladimir Oltean 
2215c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2216c0a8a9c2SVladimir Oltean 			if (!dsa_is_user_port(dp->ds, dp->index))
2217c0a8a9c2SVladimir Oltean 				continue;
2218c0a8a9c2SVladimir Oltean 
2219c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2220c0a8a9c2SVladimir Oltean 		}
2221c0a8a9c2SVladimir Oltean 
2222c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2223c0a8a9c2SVladimir Oltean 
2224c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2225c0a8a9c2SVladimir Oltean 	}
2226c0a8a9c2SVladimir Oltean 	default:
2227c0a8a9c2SVladimir Oltean 		break;
2228cc1d5bdaSFlorian Fainelli 	}
2229b73adef6SFlorian Fainelli 
2230b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2231b73adef6SFlorian Fainelli }
223288e4f0caSVivien Didelot 
2233c4bb76a9SVladimir Oltean static void
2234c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2235c4bb76a9SVladimir Oltean {
2236c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2237c4bb76a9SVladimir Oltean 	struct switchdev_notifier_fdb_info info;
2238c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2239c4bb76a9SVladimir Oltean 
2240c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2241c4bb76a9SVladimir Oltean 		return;
2242c4bb76a9SVladimir Oltean 
2243c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2244c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2245c4bb76a9SVladimir Oltean 	info.offloaded = true;
2246c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2247c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2248c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2249c4bb76a9SVladimir Oltean }
2250c9eb3e0fSArkadi Sharshevsky 
2251c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2252c9eb3e0fSArkadi Sharshevsky {
2253c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2254c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2255c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2256c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2257c9eb3e0fSArkadi Sharshevsky 	int err;
2258c9eb3e0fSArkadi Sharshevsky 
2259c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2260c4bb76a9SVladimir Oltean 
2261c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2262c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2263c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2264c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2265c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2266c9eb3e0fSArkadi Sharshevsky 		if (err) {
2267c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2268c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2269c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2270c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2271c9eb3e0fSArkadi Sharshevsky 			break;
2272c9eb3e0fSArkadi Sharshevsky 		}
2273c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2274c9eb3e0fSArkadi Sharshevsky 		break;
2275c9eb3e0fSArkadi Sharshevsky 
2276c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2277c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2278c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2279c9eb3e0fSArkadi Sharshevsky 		if (err) {
2280c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2281c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2282c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2283c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2284c9eb3e0fSArkadi Sharshevsky 		}
22852fd18650SVladimir Oltean 
2286c9eb3e0fSArkadi Sharshevsky 		break;
2287c9eb3e0fSArkadi Sharshevsky 	}
2288c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2289c9eb3e0fSArkadi Sharshevsky 
2290c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2291c4bb76a9SVladimir Oltean 	if (dsa_is_user_port(ds, dp->index))
2292c4bb76a9SVladimir Oltean 		dev_put(dp->slave);
2293c9eb3e0fSArkadi Sharshevsky }
2294c9eb3e0fSArkadi Sharshevsky 
2295d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev,
2296d5f19486SVladimir Oltean 			      struct netdev_nested_priv *priv)
2297d5f19486SVladimir Oltean {
2298d5f19486SVladimir Oltean 	if (dsa_slave_dev_check(lower_dev)) {
2299d5f19486SVladimir Oltean 		priv->data = (void *)netdev_priv(lower_dev);
2300d5f19486SVladimir Oltean 		return 1;
2301d5f19486SVladimir Oltean 	}
2302d5f19486SVladimir Oltean 
2303d5f19486SVladimir Oltean 	return 0;
2304d5f19486SVladimir Oltean }
2305d5f19486SVladimir Oltean 
2306d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2307d5f19486SVladimir Oltean {
2308d5f19486SVladimir Oltean 	struct netdev_nested_priv priv = {
2309d5f19486SVladimir Oltean 		.data = NULL,
2310d5f19486SVladimir Oltean 	};
2311d5f19486SVladimir Oltean 
2312d5f19486SVladimir Oltean 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2313d5f19486SVladimir Oltean 
2314d5f19486SVladimir Oltean 	return (struct dsa_slave_priv *)priv.data;
2315d5f19486SVladimir Oltean }
2316d5f19486SVladimir Oltean 
2317c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2318c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2319c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2320c9eb3e0fSArkadi Sharshevsky {
2321c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2322c4bb76a9SVladimir Oltean 	const struct switchdev_notifier_fdb_info *fdb_info;
2323c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
2324c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
232579b139f4SVivien Didelot 	int err;
232679b139f4SVivien Didelot 
2327447d290aSVladimir Oltean 	switch (event) {
2328447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
232979b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
233079b139f4SVivien Didelot 						     dsa_slave_dev_check,
233179b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
233279b139f4SVivien Didelot 		return notifier_from_errno(err);
2333447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2334447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2335d5f19486SVladimir Oltean 		fdb_info = ptr;
2336d5f19486SVladimir Oltean 
2337d5f19486SVladimir Oltean 		if (dsa_slave_dev_check(dev)) {
23382c4eca3eSVladimir Oltean 			if (!fdb_info->added_by_user || fdb_info->is_local)
2339d5f19486SVladimir Oltean 				return NOTIFY_OK;
2340c9eb3e0fSArkadi Sharshevsky 
2341c4bb76a9SVladimir Oltean 			dp = dsa_slave_to_port(dev);
2342d5f19486SVladimir Oltean 		} else {
2343d5f19486SVladimir Oltean 			/* Snoop addresses learnt on foreign interfaces
2344d5f19486SVladimir Oltean 			 * bridged with us, for switches that don't
2345d5f19486SVladimir Oltean 			 * automatically learn SA from CPU-injected traffic
2346d5f19486SVladimir Oltean 			 */
2347d5f19486SVladimir Oltean 			struct net_device *br_dev;
2348d5f19486SVladimir Oltean 			struct dsa_slave_priv *p;
2349d5f19486SVladimir Oltean 
2350d5f19486SVladimir Oltean 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2351d5f19486SVladimir Oltean 			if (!br_dev)
2352d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2353d5f19486SVladimir Oltean 
2354d5f19486SVladimir Oltean 			if (!netif_is_bridge_master(br_dev))
2355d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2356d5f19486SVladimir Oltean 
2357d5f19486SVladimir Oltean 			p = dsa_slave_dev_lower_find(br_dev);
2358d5f19486SVladimir Oltean 			if (!p)
2359d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2360d5f19486SVladimir Oltean 
2361d5f19486SVladimir Oltean 			dp = p->dp->cpu_dp;
2362d5f19486SVladimir Oltean 
2363d5f19486SVladimir Oltean 			if (!dp->ds->assisted_learning_on_cpu_port)
2364d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2365a324d3d4SVladimir Oltean 
2366a324d3d4SVladimir Oltean 			/* When the bridge learns an address on an offloaded
2367a324d3d4SVladimir Oltean 			 * LAG we don't want to send traffic to the CPU, the
2368a324d3d4SVladimir Oltean 			 * other ports bridged with the LAG should be able to
2369a324d3d4SVladimir Oltean 			 * autonomously forward towards it.
2370a324d3d4SVladimir Oltean 			 */
237103cbb870SVladimir Oltean 			if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev))
2372a324d3d4SVladimir Oltean 				return NOTIFY_DONE;
2373d5f19486SVladimir Oltean 		}
2374c4bb76a9SVladimir Oltean 
23755fb4a451SVladimir Oltean 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
23765fb4a451SVladimir Oltean 			return NOTIFY_DONE;
23775fb4a451SVladimir Oltean 
2378c9eb3e0fSArkadi Sharshevsky 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2379c9eb3e0fSArkadi Sharshevsky 		if (!switchdev_work)
2380c9eb3e0fSArkadi Sharshevsky 			return NOTIFY_BAD;
2381c9eb3e0fSArkadi Sharshevsky 
2382c9eb3e0fSArkadi Sharshevsky 		INIT_WORK(&switchdev_work->work,
2383c9eb3e0fSArkadi Sharshevsky 			  dsa_slave_switchdev_event_work);
2384c4bb76a9SVladimir Oltean 		switchdev_work->ds = dp->ds;
2385c4bb76a9SVladimir Oltean 		switchdev_work->port = dp->index;
2386c9eb3e0fSArkadi Sharshevsky 		switchdev_work->event = event;
2387c9eb3e0fSArkadi Sharshevsky 
2388c4bb76a9SVladimir Oltean 		ether_addr_copy(switchdev_work->addr,
2389c4bb76a9SVladimir Oltean 				fdb_info->addr);
2390c4bb76a9SVladimir Oltean 		switchdev_work->vid = fdb_info->vid;
2391c4bb76a9SVladimir Oltean 
2392d5f19486SVladimir Oltean 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2393d5f19486SVladimir Oltean 		if (dsa_is_user_port(dp->ds, dp->index))
2394c9eb3e0fSArkadi Sharshevsky 			dev_hold(dev);
2395447d290aSVladimir Oltean 		dsa_schedule_work(&switchdev_work->work);
2396c9eb3e0fSArkadi Sharshevsky 		break;
2397c9eb3e0fSArkadi Sharshevsky 	default:
2398c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2399c9eb3e0fSArkadi Sharshevsky 	}
2400c9eb3e0fSArkadi Sharshevsky 
2401c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2402c9eb3e0fSArkadi Sharshevsky }
2403c9eb3e0fSArkadi Sharshevsky 
24042b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
24052b239f67SPetr Machata 					      unsigned long event, void *ptr)
24062b239f67SPetr Machata {
24072b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
240879b139f4SVivien Didelot 	int err;
24092b239f67SPetr Machata 
24102b239f67SPetr Machata 	switch (event) {
241179b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
241279b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
241379b139f4SVivien Didelot 						    dsa_slave_dev_check,
241479b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
241579b139f4SVivien Didelot 		return notifier_from_errno(err);
24162b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
241779b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
241879b139f4SVivien Didelot 						    dsa_slave_dev_check,
241979b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
242079b139f4SVivien Didelot 		return notifier_from_errno(err);
24219ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
242279b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
242379b139f4SVivien Didelot 						     dsa_slave_dev_check,
242479b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
242579b139f4SVivien Didelot 		return notifier_from_errno(err);
24262b239f67SPetr Machata 	}
24272b239f67SPetr Machata 
24282b239f67SPetr Machata 	return NOTIFY_DONE;
24292b239f67SPetr Machata }
24302b239f67SPetr Machata 
243188e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
243288e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
243388e4f0caSVivien Didelot };
243488e4f0caSVivien Didelot 
2435010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2436c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2437c9eb3e0fSArkadi Sharshevsky };
2438c9eb3e0fSArkadi Sharshevsky 
2439010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
24402b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
24412b239f67SPetr Machata };
24422b239f67SPetr Machata 
244388e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
244488e4f0caSVivien Didelot {
24452b239f67SPetr Machata 	struct notifier_block *nb;
2446c9eb3e0fSArkadi Sharshevsky 	int err;
2447c9eb3e0fSArkadi Sharshevsky 
2448c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2449c9eb3e0fSArkadi Sharshevsky 	if (err)
2450c9eb3e0fSArkadi Sharshevsky 		return err;
2451c9eb3e0fSArkadi Sharshevsky 
2452c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2453c9eb3e0fSArkadi Sharshevsky 	if (err)
2454c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2455c9eb3e0fSArkadi Sharshevsky 
24562b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
24572b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
24582b239f67SPetr Machata 	if (err)
24592b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
24602b239f67SPetr Machata 
2461c9eb3e0fSArkadi Sharshevsky 	return 0;
2462c9eb3e0fSArkadi Sharshevsky 
24632b239f67SPetr Machata err_switchdev_blocking_nb:
24642b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2465c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2466c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2467c9eb3e0fSArkadi Sharshevsky 	return err;
246888e4f0caSVivien Didelot }
246988e4f0caSVivien Didelot 
247088e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
247188e4f0caSVivien Didelot {
24722b239f67SPetr Machata 	struct notifier_block *nb;
247388e4f0caSVivien Didelot 	int err;
247488e4f0caSVivien Didelot 
24752b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
24762b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
24772b239f67SPetr Machata 	if (err)
24782b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
24792b239f67SPetr Machata 
2480c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2481c9eb3e0fSArkadi Sharshevsky 	if (err)
2482c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2483c9eb3e0fSArkadi Sharshevsky 
248488e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
248588e4f0caSVivien Didelot 	if (err)
248688e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
248788e4f0caSVivien Didelot }
2488