xref: /openbmc/linux/net/dsa/slave.c (revision 69bfac968a06aab5927160f8736485f85c3e8ee8)
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 
274*69bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
2754c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
2764c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
27735636062SScott Feldman {
278d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
279b8d866acSVivien Didelot 	int ret;
28035636062SScott Feldman 
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 
397*69bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
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 
472*69bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
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 
617c4b364ceSYangbo 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) {
779b94cbc90SVladimir Oltean 		int count = 0;
78091da11f8SLennert Buytenhek 
781b94cbc90SVladimir Oltean 		if (ds->ops->get_sset_count) {
782b94cbc90SVladimir Oltean 			count = ds->ops->get_sset_count(ds, dp->index, sset);
783b94cbc90SVladimir Oltean 			if (count < 0)
78491da11f8SLennert Buytenhek 				return count;
785b94cbc90SVladimir Oltean 		}
786b94cbc90SVladimir Oltean 
787b94cbc90SVladimir Oltean 		return count + 4;
788a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
789a71acad9SOleksij Rempel 		return net_selftest_get_count();
79091da11f8SLennert Buytenhek 	}
79191da11f8SLennert Buytenhek 
79291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
79391da11f8SLennert Buytenhek }
79491da11f8SLennert Buytenhek 
795a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
796a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
797a71acad9SOleksij Rempel {
798a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
799a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
800a71acad9SOleksij Rempel 
801a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
802a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
803a71acad9SOleksij Rempel 		return;
804a71acad9SOleksij Rempel 	}
805a71acad9SOleksij Rempel 
806a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
807a71acad9SOleksij Rempel }
808a71acad9SOleksij Rempel 
80919e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
81019e57c4eSFlorian Fainelli {
811d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
812d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
81319e57c4eSFlorian Fainelli 
814aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
815aab9c406SFlorian Fainelli 
8169d490b4eSVivien Didelot 	if (ds->ops->get_wol)
817d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
81819e57c4eSFlorian Fainelli }
81919e57c4eSFlorian Fainelli 
82019e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
82119e57c4eSFlorian Fainelli {
822d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
823d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
82419e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
82519e57c4eSFlorian Fainelli 
826aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
827aab9c406SFlorian Fainelli 
8289d490b4eSVivien Didelot 	if (ds->ops->set_wol)
829d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
83019e57c4eSFlorian Fainelli 
83119e57c4eSFlorian Fainelli 	return ret;
83219e57c4eSFlorian Fainelli }
83319e57c4eSFlorian Fainelli 
8347905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
8357905288fSFlorian Fainelli {
836d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
837d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8387905288fSFlorian Fainelli 	int ret;
8397905288fSFlorian Fainelli 
8407b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
84100670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8427b9cc738SVivien Didelot 		return -ENODEV;
8437b9cc738SVivien Didelot 
84408f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
8457905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8467905288fSFlorian Fainelli 
847d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
8487905288fSFlorian Fainelli 	if (ret)
8497905288fSFlorian Fainelli 		return ret;
8507905288fSFlorian Fainelli 
851aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
8527905288fSFlorian Fainelli }
8537905288fSFlorian Fainelli 
8547905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8557905288fSFlorian Fainelli {
856d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
857d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8587905288fSFlorian Fainelli 	int ret;
8597905288fSFlorian Fainelli 
8607b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
86100670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8627b9cc738SVivien Didelot 		return -ENODEV;
8637b9cc738SVivien Didelot 
86408f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8657905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8667905288fSFlorian Fainelli 
867d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8687905288fSFlorian Fainelli 	if (ret)
8697905288fSFlorian Fainelli 		return ret;
8707905288fSFlorian Fainelli 
871aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
872aab9c406SFlorian Fainelli }
873aab9c406SFlorian Fainelli 
874aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
875aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
876aab9c406SFlorian Fainelli {
877aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
878aab9c406SFlorian Fainelli 
879aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
880aab9c406SFlorian Fainelli }
881aab9c406SFlorian Fainelli 
882aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
883aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
884aab9c406SFlorian Fainelli {
885aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
886aab9c406SFlorian Fainelli 
887aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8887905288fSFlorian Fainelli }
8897905288fSFlorian Fainelli 
890a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
891a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
892a2a1a13bSHeiner Kallweit {
893a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
894a2a1a13bSHeiner Kallweit 
895a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
896a2a1a13bSHeiner Kallweit }
897a2a1a13bSHeiner Kallweit 
898a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
899a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
900a2a1a13bSHeiner Kallweit {
901a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
902a2a1a13bSHeiner Kallweit 
903a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
904a2a1a13bSHeiner Kallweit }
905a2a1a13bSHeiner Kallweit 
90604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
90704ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
90804ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
90904ff53f9SFlorian Fainelli {
910d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
91104ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
91204ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
91304ff53f9SFlorian Fainelli 	int err = 0;
91404ff53f9SFlorian Fainelli 
91504ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
91604ff53f9SFlorian Fainelli 	if (!netpoll)
91704ff53f9SFlorian Fainelli 		return -ENOMEM;
91804ff53f9SFlorian Fainelli 
91904ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
92004ff53f9SFlorian Fainelli 	if (err) {
92104ff53f9SFlorian Fainelli 		kfree(netpoll);
92204ff53f9SFlorian Fainelli 		goto out;
92304ff53f9SFlorian Fainelli 	}
92404ff53f9SFlorian Fainelli 
92504ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
92604ff53f9SFlorian Fainelli out:
92704ff53f9SFlorian Fainelli 	return err;
92804ff53f9SFlorian Fainelli }
92904ff53f9SFlorian Fainelli 
93004ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
93104ff53f9SFlorian Fainelli {
93204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
93304ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
93404ff53f9SFlorian Fainelli 
93504ff53f9SFlorian Fainelli 	if (!netpoll)
93604ff53f9SFlorian Fainelli 		return;
93704ff53f9SFlorian Fainelli 
93804ff53f9SFlorian Fainelli 	p->netpoll = NULL;
93904ff53f9SFlorian Fainelli 
940c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
94104ff53f9SFlorian Fainelli }
94204ff53f9SFlorian Fainelli 
94304ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
94404ff53f9SFlorian Fainelli {
94504ff53f9SFlorian Fainelli }
94604ff53f9SFlorian Fainelli #endif
94704ff53f9SFlorian Fainelli 
94844bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
94944bb765cSFlorian Fainelli 					char *name, size_t len)
95044bb765cSFlorian Fainelli {
951d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
95244bb765cSFlorian Fainelli 
953d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
954d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
955d484210bSJiri Pirko 	 * should be removed with legacy support.
956d484210bSJiri Pirko 	 */
957d484210bSJiri Pirko 	if (dp->ds->devlink)
958d484210bSJiri Pirko 		return -EOPNOTSUPP;
959d484210bSJiri Pirko 
960d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
96144bb765cSFlorian Fainelli 		return -EINVAL;
9623a543ef4SFlorian Fainelli 
9633a543ef4SFlorian Fainelli 	return 0;
9643a543ef4SFlorian Fainelli }
9653a543ef4SFlorian Fainelli 
966f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9674fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
968f50f2127SFlorian Fainelli {
9694fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
970f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
971f50f2127SFlorian Fainelli 
972f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
973f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
974f50f2127SFlorian Fainelli 			return mall_tc_entry;
975f50f2127SFlorian Fainelli 
976f50f2127SFlorian Fainelli 	return NULL;
977f50f2127SFlorian Fainelli }
978f50f2127SFlorian Fainelli 
979e13c2075SVladimir Oltean static int
980e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
981f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
982f50f2127SFlorian Fainelli 				  bool ingress)
983f50f2127SFlorian Fainelli {
984d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
985f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
986e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
987f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
988d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9899681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
990d945097bSVivien Didelot 	struct dsa_port *to_dp;
991e13c2075SVladimir Oltean 	int err;
992e13c2075SVladimir Oltean 
993f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
99434297176SVladimir Oltean 		return -EOPNOTSUPP;
995f50f2127SFlorian Fainelli 
99653eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
997319a1d19SJiri Pirko 					      cls->common.extack))
99834297176SVladimir Oltean 		return -EOPNOTSUPP;
999319a1d19SJiri Pirko 
10009681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
1001f50f2127SFlorian Fainelli 
100265722159SVladimir Oltean 	if (!act->dev)
100365722159SVladimir Oltean 		return -EINVAL;
100465722159SVladimir Oltean 
10059681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1006f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1007f50f2127SFlorian Fainelli 
1008f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1009f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1010f50f2127SFlorian Fainelli 		return -ENOMEM;
1011f50f2127SFlorian Fainelli 
1012f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1013f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1014f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1015f50f2127SFlorian Fainelli 
10169681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1017f50f2127SFlorian Fainelli 
1018d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1019f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1020f50f2127SFlorian Fainelli 
1021d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1022f50f2127SFlorian Fainelli 	if (err) {
1023f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1024f50f2127SFlorian Fainelli 		return err;
1025f50f2127SFlorian Fainelli 	}
1026f50f2127SFlorian Fainelli 
1027f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1028e13c2075SVladimir Oltean 
1029e13c2075SVladimir Oltean 	return err;
1030f50f2127SFlorian Fainelli }
1031f50f2127SFlorian Fainelli 
103234297176SVladimir Oltean static int
103334297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
103434297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
103534297176SVladimir Oltean 				  bool ingress)
103634297176SVladimir Oltean {
103734297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
103834297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
103934297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
104034297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
104134297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
104234297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
104334297176SVladimir Oltean 	struct flow_action_entry *act;
104434297176SVladimir Oltean 	int err;
104534297176SVladimir Oltean 
104634297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
104734297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1048c75a33c8SJacob Keller 				   "Policing offload not implemented");
104934297176SVladimir Oltean 		return -EOPNOTSUPP;
105034297176SVladimir Oltean 	}
105134297176SVladimir Oltean 
105234297176SVladimir Oltean 	if (!ingress) {
105334297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1054c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
105534297176SVladimir Oltean 		return -EOPNOTSUPP;
105634297176SVladimir Oltean 	}
105734297176SVladimir Oltean 
105834297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
105934297176SVladimir Oltean 					      cls->common.extack))
106034297176SVladimir Oltean 		return -EOPNOTSUPP;
106134297176SVladimir Oltean 
106234297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
106334297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
106434297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1065c75a33c8SJacob Keller 					   "Only one port policer allowed");
106634297176SVladimir Oltean 			return -EEXIST;
106734297176SVladimir Oltean 		}
106834297176SVladimir Oltean 	}
106934297176SVladimir Oltean 
107034297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
107134297176SVladimir Oltean 
107234297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
107334297176SVladimir Oltean 	if (!mall_tc_entry)
107434297176SVladimir Oltean 		return -ENOMEM;
107534297176SVladimir Oltean 
107634297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
107734297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
107834297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
107934297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
108034297176SVladimir Oltean 	policer->burst = act->police.burst;
108134297176SVladimir Oltean 
108234297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
108334297176SVladimir Oltean 	if (err) {
108434297176SVladimir Oltean 		kfree(mall_tc_entry);
108534297176SVladimir Oltean 		return err;
108634297176SVladimir Oltean 	}
108734297176SVladimir Oltean 
108834297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
108934297176SVladimir Oltean 
109034297176SVladimir Oltean 	return err;
109134297176SVladimir Oltean }
109234297176SVladimir Oltean 
1093e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1094e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1095e13c2075SVladimir Oltean 				      bool ingress)
1096e13c2075SVladimir Oltean {
1097e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1098e13c2075SVladimir Oltean 
1099e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1100e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1101e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1102e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
110334297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
110434297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
110534297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1106e13c2075SVladimir Oltean 
1107e13c2075SVladimir Oltean 	return err;
1108f50f2127SFlorian Fainelli }
1109f50f2127SFlorian Fainelli 
1110f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1111f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1112f50f2127SFlorian Fainelli {
1113d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1114f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1115d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1116f50f2127SFlorian Fainelli 
11174fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1118f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1119f50f2127SFlorian Fainelli 		return;
1120f50f2127SFlorian Fainelli 
1121f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1122f50f2127SFlorian Fainelli 
1123f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1124f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
112534297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
112634297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
112734297176SVladimir Oltean 						 &mall_tc_entry->mirror);
112834297176SVladimir Oltean 		break;
112934297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
113034297176SVladimir Oltean 		if (ds->ops->port_policer_del)
113134297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1132f50f2127SFlorian Fainelli 		break;
1133f50f2127SFlorian Fainelli 	default:
1134f50f2127SFlorian Fainelli 		WARN_ON(1);
1135f50f2127SFlorian Fainelli 	}
1136f50f2127SFlorian Fainelli 
1137f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1138f50f2127SFlorian Fainelli }
1139f50f2127SFlorian Fainelli 
11403fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
11416b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
11426b3eb752SJiri Pirko 					   bool ingress)
1143f50f2127SFlorian Fainelli {
11445fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1145a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1146f50f2127SFlorian Fainelli 
11473fbae382SJiri Pirko 	switch (cls->command) {
11483fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
11495fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
11503fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
11513fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
11523fbae382SJiri Pirko 		return 0;
11533fbae382SJiri Pirko 	default:
11543fbae382SJiri Pirko 		return -EOPNOTSUPP;
11553fbae382SJiri Pirko 	}
11563fbae382SJiri Pirko }
11573fbae382SJiri Pirko 
1158ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1159ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1160ed11bb1fSVladimir Oltean 				    bool ingress)
1161ed11bb1fSVladimir Oltean {
1162ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1163ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1164ed11bb1fSVladimir Oltean 	int port = dp->index;
1165ed11bb1fSVladimir Oltean 
1166ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1167ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1168ed11bb1fSVladimir Oltean 
1169ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1170ed11bb1fSVladimir Oltean }
1171ed11bb1fSVladimir Oltean 
1172ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1173ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1174ed11bb1fSVladimir Oltean 				    bool ingress)
1175ed11bb1fSVladimir Oltean {
1176ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1177ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1178ed11bb1fSVladimir Oltean 	int port = dp->index;
1179ed11bb1fSVladimir Oltean 
1180ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1181ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1182ed11bb1fSVladimir Oltean 
1183ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1184ed11bb1fSVladimir Oltean }
1185ed11bb1fSVladimir Oltean 
1186ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1187ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1188ed11bb1fSVladimir Oltean 				      bool ingress)
1189ed11bb1fSVladimir Oltean {
1190ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1191ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1192ed11bb1fSVladimir Oltean 	int port = dp->index;
1193ed11bb1fSVladimir Oltean 
1194ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1195ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1196ed11bb1fSVladimir Oltean 
1197ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1198ed11bb1fSVladimir Oltean }
1199ed11bb1fSVladimir Oltean 
1200ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1201ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1202ed11bb1fSVladimir Oltean 					 bool ingress)
1203ed11bb1fSVladimir Oltean {
1204ed11bb1fSVladimir Oltean 	switch (cls->command) {
1205ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1206ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1207ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1208ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1209ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1210ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1211ed11bb1fSVladimir Oltean 	default:
1212ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1213ed11bb1fSVladimir Oltean 	}
1214ed11bb1fSVladimir Oltean }
1215ed11bb1fSVladimir Oltean 
12166b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12176b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
12186b3eb752SJiri Pirko {
12196b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
12206b3eb752SJiri Pirko 
122144ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
122244ae12a7SJiri Pirko 		return -EOPNOTSUPP;
122344ae12a7SJiri Pirko 
12246b3eb752SJiri Pirko 	switch (type) {
12256b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
12266b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1227ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1228ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
12296b3eb752SJiri Pirko 	default:
12306b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12316b3eb752SJiri Pirko 	}
12326b3eb752SJiri Pirko }
12336b3eb752SJiri Pirko 
12346b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
12356b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12366b3eb752SJiri Pirko {
12376b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
12386b3eb752SJiri Pirko }
12396b3eb752SJiri Pirko 
12406b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
12416b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12426b3eb752SJiri Pirko {
12436b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
12446b3eb752SJiri Pirko }
12456b3eb752SJiri Pirko 
1246955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1247955bcb6eSPablo Neira Ayuso 
12486b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1249955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
12506b3eb752SJiri Pirko {
1251955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1252a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
12536b3eb752SJiri Pirko 
125432f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12556b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
125632f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12576b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12586b3eb752SJiri Pirko 	else
12596b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12606b3eb752SJiri Pirko 
1261955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1262955bcb6eSPablo Neira Ayuso 
12636b3eb752SJiri Pirko 	switch (f->command) {
12649c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12650d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12660d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12670d4fd02eSPablo Neira Ayuso 
12680c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1269955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1270955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1271955bcb6eSPablo Neira Ayuso 
1272955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1273955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1274955bcb6eSPablo Neira Ayuso 		return 0;
12759c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
127614bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1277955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1278955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1279955bcb6eSPablo Neira Ayuso 
1280955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1281955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12826b3eb752SJiri Pirko 		return 0;
12836b3eb752SJiri Pirko 	default:
12846b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12856b3eb752SJiri Pirko 	}
12866b3eb752SJiri Pirko }
12876b3eb752SJiri Pirko 
12883fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
12893fb24a43SPablo Neira Ayuso 				    void *type_data)
12903fb24a43SPablo Neira Ayuso {
12913fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
12923fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
12933fb24a43SPablo Neira Ayuso 
12943fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
12953fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
12963fb24a43SPablo Neira Ayuso 
12973fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
12983fb24a43SPablo Neira Ayuso }
12993fb24a43SPablo Neira Ayuso 
13003fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1301de4784caSJiri Pirko 			      void *type_data)
13023fbae382SJiri Pirko {
130347d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
130447d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
130547d23af2SVladimir Oltean 
13063fb24a43SPablo Neira Ayuso 	switch (type) {
13073fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
13086b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
13093fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
13103fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
13113fb24a43SPablo Neira Ayuso 	default:
13123fb24a43SPablo Neira Ayuso 		break;
13133fb24a43SPablo Neira Ayuso 	}
131447d23af2SVladimir Oltean 
131547d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1316a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
131747d23af2SVladimir Oltean 
131847d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1319f50f2127SFlorian Fainelli }
1320f50f2127SFlorian Fainelli 
1321bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1322bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1323bf9f2648SFlorian Fainelli {
1324d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1325d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1326bf9f2648SFlorian Fainelli 
1327bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1328bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1329bf9f2648SFlorian Fainelli 
1330d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1331bf9f2648SFlorian Fainelli }
1332bf9f2648SFlorian Fainelli 
1333bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1334bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1335bf9f2648SFlorian Fainelli {
1336d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1337d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1338bf9f2648SFlorian Fainelli 
1339bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1340bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1341bf9f2648SFlorian Fainelli 
1342d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1343bf9f2648SFlorian Fainelli }
1344bf9f2648SFlorian Fainelli 
13450336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
13460336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
13470336369dSBrandon Streiff {
13480336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
13490336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
13500336369dSBrandon Streiff 
13510336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
13520336369dSBrandon Streiff 		return -EOPNOTSUPP;
13530336369dSBrandon Streiff 
13540336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
13550336369dSBrandon Streiff }
13560336369dSBrandon Streiff 
1357061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1358061f6a50SFlorian Fainelli 				     u16 vid)
1359061f6a50SFlorian Fainelli {
13602209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1361061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
136288236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
136388236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1364b7a9e0daSVladimir Oltean 		.vid = vid,
136588236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
136688236591SVladimir Oltean 		.flags = 0,
136788236591SVladimir Oltean 	};
136831046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1369061f6a50SFlorian Fainelli 	int ret;
1370061f6a50SFlorian Fainelli 
137188236591SVladimir Oltean 	/* User port... */
137231046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
137331046a5fSVladimir Oltean 	if (ret) {
137431046a5fSVladimir Oltean 		if (extack._msg)
137531046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
137688236591SVladimir Oltean 		return ret;
137731046a5fSVladimir Oltean 	}
137888236591SVladimir Oltean 
137988236591SVladimir Oltean 	/* And CPU port... */
138031046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
138131046a5fSVladimir Oltean 	if (ret) {
138231046a5fSVladimir Oltean 		if (extack._msg)
138331046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
138431046a5fSVladimir Oltean 				   extack._msg);
13857e1741b4SVivien Didelot 		return ret;
138631046a5fSVladimir Oltean 	}
13877e1741b4SVivien Didelot 
13882209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1389061f6a50SFlorian Fainelli }
1390061f6a50SFlorian Fainelli 
1391061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1392061f6a50SFlorian Fainelli 				      u16 vid)
1393061f6a50SFlorian Fainelli {
13942209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1395061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
139688236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1397b7a9e0daSVladimir Oltean 		.vid = vid,
139888236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
139988236591SVladimir Oltean 		.flags = 0,
140088236591SVladimir Oltean 	};
14012209158cSVladimir Oltean 	int err;
1402061f6a50SFlorian Fainelli 
14037e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
14047e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
14057e1741b4SVivien Didelot 	 */
14062209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
14072209158cSVladimir Oltean 	if (err)
14082209158cSVladimir Oltean 		return err;
14092209158cSVladimir Oltean 
14102209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
14112209158cSVladimir Oltean 
14122209158cSVladimir Oltean 	return 0;
1413061f6a50SFlorian Fainelli }
1414061f6a50SFlorian Fainelli 
1415bff33f7eSVladimir Oltean struct dsa_hw_port {
1416bff33f7eSVladimir Oltean 	struct list_head list;
1417bff33f7eSVladimir Oltean 	struct net_device *dev;
1418bff33f7eSVladimir Oltean 	int old_mtu;
1419bff33f7eSVladimir Oltean };
1420bff33f7eSVladimir Oltean 
1421bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1422bff33f7eSVladimir Oltean {
1423bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1424bff33f7eSVladimir Oltean 	int err;
1425bff33f7eSVladimir Oltean 
1426bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1427bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1428bff33f7eSVladimir Oltean 			continue;
1429bff33f7eSVladimir Oltean 
1430bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1431bff33f7eSVladimir Oltean 		if (err)
1432bff33f7eSVladimir Oltean 			goto rollback;
1433bff33f7eSVladimir Oltean 	}
1434bff33f7eSVladimir Oltean 
1435bff33f7eSVladimir Oltean 	return 0;
1436bff33f7eSVladimir Oltean 
1437bff33f7eSVladimir Oltean rollback:
1438bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1439bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1440bff33f7eSVladimir Oltean 			continue;
1441bff33f7eSVladimir Oltean 
1442bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1443bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1444bff33f7eSVladimir Oltean 	}
1445bff33f7eSVladimir Oltean 
1446bff33f7eSVladimir Oltean 	return err;
1447bff33f7eSVladimir Oltean }
1448bff33f7eSVladimir Oltean 
1449bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1450bff33f7eSVladimir Oltean {
1451bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1452bff33f7eSVladimir Oltean 
1453bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1454bff33f7eSVladimir Oltean 		kfree(p);
1455bff33f7eSVladimir Oltean }
1456bff33f7eSVladimir Oltean 
1457bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1458bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1459bff33f7eSVladimir Oltean {
1460bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1461bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1462bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1463bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1464bff33f7eSVladimir Oltean 	int err;
1465bff33f7eSVladimir Oltean 
1466bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1467bff33f7eSVladimir Oltean 		return;
1468bff33f7eSVladimir Oltean 
1469bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1470bff33f7eSVladimir Oltean 		return;
1471bff33f7eSVladimir Oltean 
1472bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1473bff33f7eSVladimir Oltean 
1474bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1475bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1476bff33f7eSVladimir Oltean 	 */
1477bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1478bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1479bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1480bff33f7eSVladimir Oltean 			struct net_device *slave;
1481bff33f7eSVladimir Oltean 
1482bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1483bff33f7eSVladimir Oltean 				continue;
1484bff33f7eSVladimir Oltean 
1485bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1486bff33f7eSVladimir Oltean 				continue;
1487bff33f7eSVladimir Oltean 
1488bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1489bff33f7eSVladimir Oltean 				continue;
1490bff33f7eSVladimir Oltean 
1491bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1492bff33f7eSVladimir Oltean 
1493bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1494bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1495bff33f7eSVladimir Oltean 
1496bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1497bff33f7eSVladimir Oltean 			if (!hw_port)
1498bff33f7eSVladimir Oltean 				goto out;
1499bff33f7eSVladimir Oltean 
1500bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1501bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1502bff33f7eSVladimir Oltean 
1503bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1504bff33f7eSVladimir Oltean 		}
1505bff33f7eSVladimir Oltean 	}
1506bff33f7eSVladimir Oltean 
1507bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1508bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1509bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1510bff33f7eSVladimir Oltean 	 */
1511bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1512bff33f7eSVladimir Oltean 	if (!err)
1513bff33f7eSVladimir Oltean 		goto out;
1514bff33f7eSVladimir Oltean 
1515bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1516bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1517bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1518bff33f7eSVladimir Oltean 	 */
1519bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1520bff33f7eSVladimir Oltean 
1521bff33f7eSVladimir Oltean out:
1522bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1523bff33f7eSVladimir Oltean }
1524bff33f7eSVladimir Oltean 
152553da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1526bfcb8132SVladimir Oltean {
1527bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1528bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1529bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1530bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
15314e4ab795SVladimir Oltean 	struct dsa_port *dp_iter;
1532bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1533bfcb8132SVladimir Oltean 	int port = p->dp->index;
1534bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1535bfcb8132SVladimir Oltean 	int new_master_mtu;
1536bfcb8132SVladimir Oltean 	int old_master_mtu;
1537bfcb8132SVladimir Oltean 	int mtu_limit;
1538bfcb8132SVladimir Oltean 	int cpu_mtu;
15394e4ab795SVladimir Oltean 	int err;
1540bfcb8132SVladimir Oltean 
1541bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1542bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1543bfcb8132SVladimir Oltean 
15444e4ab795SVladimir Oltean 	list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1545bfcb8132SVladimir Oltean 		int slave_mtu;
1546bfcb8132SVladimir Oltean 
15474e4ab795SVladimir Oltean 		if (!dsa_port_is_user(dp_iter))
1548bfcb8132SVladimir Oltean 			continue;
1549bfcb8132SVladimir Oltean 
1550bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1551bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1552bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1553bfcb8132SVladimir Oltean 		 */
15544e4ab795SVladimir Oltean 		if (!dp_iter->slave)
1555bfcb8132SVladimir Oltean 			continue;
1556bfcb8132SVladimir Oltean 
1557bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1558bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1559bfcb8132SVladimir Oltean 		 */
15604e4ab795SVladimir Oltean 		if (dp_iter == dp)
1561bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1562bfcb8132SVladimir Oltean 		else
15634e4ab795SVladimir Oltean 			slave_mtu = dp_iter->slave->mtu;
1564bfcb8132SVladimir Oltean 
1565bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1566bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1567bfcb8132SVladimir Oltean 	}
1568bfcb8132SVladimir Oltean 
1569bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1570bfcb8132SVladimir Oltean 
1571bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1572bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
15734e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1574bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1575bfcb8132SVladimir Oltean 		return -ERANGE;
1576bfcb8132SVladimir Oltean 
1577bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1578bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1579bfcb8132SVladimir Oltean 	 */
1580bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1581bfcb8132SVladimir Oltean 
1582bfcb8132SVladimir Oltean 	/* Start applying stuff */
1583bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1584bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1585bfcb8132SVladimir Oltean 		if (err < 0)
1586bfcb8132SVladimir Oltean 			goto out_master_failed;
1587bfcb8132SVladimir Oltean 
1588bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
158988faba20SVladimir Oltean 		 * upstream switches, so create a non-targeted notifier which
159088faba20SVladimir Oltean 		 * updates all switches.
1591bfcb8132SVladimir Oltean 		 */
159288faba20SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1593bfcb8132SVladimir Oltean 		if (err)
1594bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1595bfcb8132SVladimir Oltean 	}
1596bfcb8132SVladimir Oltean 
159788faba20SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, true);
1598bfcb8132SVladimir Oltean 	if (err)
1599bfcb8132SVladimir Oltean 		goto out_port_failed;
1600bfcb8132SVladimir Oltean 
1601bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1602bfcb8132SVladimir Oltean 
1603bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1604bff33f7eSVladimir Oltean 
1605bfcb8132SVladimir Oltean 	return 0;
1606bfcb8132SVladimir Oltean 
1607bfcb8132SVladimir Oltean out_port_failed:
1608bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1609bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
16104e500251SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops),
161188faba20SVladimir Oltean 				    false);
1612bfcb8132SVladimir Oltean out_cpu_failed:
1613bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1614bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1615bfcb8132SVladimir Oltean out_master_failed:
1616bfcb8132SVladimir Oltean 	return err;
1617bfcb8132SVladimir Oltean }
1618bfcb8132SVladimir Oltean 
161991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
162091da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
16213d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
16223d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1623aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1624c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
16256793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
16266793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
16276793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
162891da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
162991da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
163091da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
163119e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
163219e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
16337905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
16347905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1635aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1636aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1637a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1638a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1639bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1640bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
16410336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
1642a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
164391da11f8SLennert Buytenhek };
164491da11f8SLennert Buytenhek 
16452a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
16464b9c9358SVladimir Oltean static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
16472a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16482a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid,
164987b0984eSPetr Machata 			      u16 flags,
165087b0984eSPetr Machata 			      struct netlink_ext_ack *extack)
16512a93c1a3SFlorian Fainelli {
16522a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16532a93c1a3SFlorian Fainelli 
16542a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
16552a93c1a3SFlorian Fainelli }
16562a93c1a3SFlorian Fainelli 
16574b9c9358SVladimir Oltean static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
16582a93c1a3SFlorian Fainelli 			      struct net_device *dev,
16592a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid)
16602a93c1a3SFlorian Fainelli {
16612a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
16622a93c1a3SFlorian Fainelli 
16632a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
16642a93c1a3SFlorian Fainelli }
16652a93c1a3SFlorian Fainelli 
1666716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1667716efee2SJiri Pirko {
1668716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1669716efee2SJiri Pirko 
1670716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1671716efee2SJiri Pirko }
1672716efee2SJiri Pirko 
1673c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1674c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1675c2ec5f2eSOleksij Rempel {
1676c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1677c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1678c2ec5f2eSOleksij Rempel 
1679c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1680c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1681c2ec5f2eSOleksij Rempel 	else
1682c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1683c2ec5f2eSOleksij Rempel }
1684c2ec5f2eSOleksij Rempel 
16850994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
16860994d492SFelix Fietkau 				       struct net_device_path *path)
16870994d492SFelix Fietkau {
16880994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
16890994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
16900994d492SFelix Fietkau 
16910994d492SFelix Fietkau 	path->dev = ctx->dev;
16920994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
16930994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
16940994d492SFelix Fietkau 	path->dsa.port = dp->index;
16950994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
16960994d492SFelix Fietkau 
16970994d492SFelix Fietkau 	return 0;
16980994d492SFelix Fietkau }
16990994d492SFelix Fietkau 
17003e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1701d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1702d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
17033e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1704d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1705d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1706d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
170737b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
170837b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
17092bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1710d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1711abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
171204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
171304ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
171404ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
171504ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
171604ff53f9SFlorian Fainelli #endif
171744bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1718f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1719c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1720929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1721061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1722061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1723716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1724bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
17250994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
172698237d43SScott Feldman };
172798237d43SScott Feldman 
1728f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1729f37db85dSFlorian Fainelli 	.name	= "dsa",
1730f37db85dSFlorian Fainelli };
1731f37db85dSFlorian Fainelli 
1732aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1733aab9c406SFlorian Fainelli {
1734aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1735aab9c406SFlorian Fainelli 
1736765bda93SRussell King 	if (dp->pl)
1737aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1738aab9c406SFlorian Fainelli }
1739aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1740aab9c406SFlorian Fainelli 
17415c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1742aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1743aab9c406SFlorian Fainelli {
17445c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1745aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1746aab9c406SFlorian Fainelli 
1747aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1748aab9c406SFlorian Fainelli 	 * not be called if it was not.
1749aab9c406SFlorian Fainelli 	 */
1750aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1751ce31b31cSFlorian Fainelli }
1752ce31b31cSFlorian Fainelli 
175391da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1754c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1755c916e8e1SOleksij Rempel 				 u32 flags)
1756c305c165SFlorian Fainelli {
1757d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1758d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1759c305c165SFlorian Fainelli 
17600115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
17610115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1762d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1763c305c165SFlorian Fainelli 		return -ENODEV;
1764d25b8e74SRussell King 	}
1765c305c165SFlorian Fainelli 
1766c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
1767c916e8e1SOleksij Rempel 
1768aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1769c305c165SFlorian Fainelli }
1770c305c165SFlorian Fainelli 
17714fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
17720d8bcdd3SFlorian Fainelli {
1773d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1774d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1775d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
17760c65b2b9SAndrew Lunn 	phy_interface_t mode;
17776819563eSFlorian Fainelli 	u32 phy_flags = 0;
17780c65b2b9SAndrew Lunn 	int ret;
17790d8bcdd3SFlorian Fainelli 
17800c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
17810c65b2b9SAndrew Lunn 	if (ret)
178219334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
17830d8bcdd3SFlorian Fainelli 
178444cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
178544cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
178644cc27e4SIoana Ciornei 
17875c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
17885c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
17895c05c1dbSRussell King 	 * this if the switch provides such a callback.
17905c05c1dbSRussell King 	 */
17915c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
17925c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
17935c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
17945c05c1dbSRussell King 	}
17955c05c1dbSRussell King 
179644cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
179777373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1798aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1799aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1800aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1801aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1802aab9c406SFlorian Fainelli 	}
1803aab9c406SFlorian Fainelli 
18049d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1805d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
18066819563eSFlorian Fainelli 
1807aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
18086146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
18096146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
18106146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
18110d8bcdd3SFlorian Fainelli 		 */
1812c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1813d25b8e74SRussell King 		if (ret) {
1814aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1815aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1816d945097bSVivien Didelot 				   dp->index, ret);
1817aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1818c305c165SFlorian Fainelli 			return ret;
1819d25b8e74SRussell King 		}
18200d8bcdd3SFlorian Fainelli 	}
18219697f1cdSFlorian Fainelli 
18226146dd45SVladimir Oltean 	return ret;
1823b31f65fbSAndrew Lunn }
18240d8bcdd3SFlorian Fainelli 
182553da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
182653da0ebaSVladimir Oltean {
182753da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
182853da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
182953da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
183053da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
183153da0ebaSVladimir Oltean 
18324e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
18334e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
183453da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
183553da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
183653da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
183753da0ebaSVladimir Oltean 	 */
183853da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
183953da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
184053da0ebaSVladimir Oltean 
184153da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
184253da0ebaSVladimir Oltean }
184353da0ebaSVladimir Oltean 
18441a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
18451a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
18461a33e10eSCong Wang 					    struct netdev_queue *txq,
18471a33e10eSCong Wang 					    void *_unused)
18481a33e10eSCong Wang {
18491a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
18501a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
18511a33e10eSCong Wang }
18521a33e10eSCong Wang 
185324462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
185424462549SFlorian Fainelli {
1855aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
185624462549SFlorian Fainelli 
1857a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1858a94c689eSFlorian Fainelli 		return 0;
1859a94c689eSFlorian Fainelli 
1860f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1861f154be24SFlorian Fainelli 
1862aab9c406SFlorian Fainelli 	rtnl_lock();
1863aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1864aab9c406SFlorian Fainelli 	rtnl_unlock();
186524462549SFlorian Fainelli 
186624462549SFlorian Fainelli 	return 0;
186724462549SFlorian Fainelli }
186824462549SFlorian Fainelli 
186924462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
187024462549SFlorian Fainelli {
1871aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1872aab9c406SFlorian Fainelli 
1873a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1874a94c689eSFlorian Fainelli 		return 0;
1875a94c689eSFlorian Fainelli 
187624462549SFlorian Fainelli 	netif_device_attach(slave_dev);
187724462549SFlorian Fainelli 
1878aab9c406SFlorian Fainelli 	rtnl_lock();
1879aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1880aab9c406SFlorian Fainelli 	rtnl_unlock();
188124462549SFlorian Fainelli 
188224462549SFlorian Fainelli 	return 0;
188324462549SFlorian Fainelli }
188424462549SFlorian Fainelli 
1885951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
188691da11f8SLennert Buytenhek {
188724a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1888f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
18894cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1890951259aaSVivien Didelot 	const char *name = port->name;
189191da11f8SLennert Buytenhek 	struct net_device *slave_dev;
189291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
189391da11f8SLennert Buytenhek 	int ret;
189491da11f8SLennert Buytenhek 
189555199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
189655199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
189755199df6SFlorian Fainelli 
189855199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
189955199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
190055199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
190191da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1902d87d6f44SGuenter Roeck 		return -ENOMEM;
190391da11f8SLennert Buytenhek 
19049b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
19059b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
19069b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1907f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
19082b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
19097ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
191083216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
1911a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1912a2c7023fSXiaofei Shen 	else
19132fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
19140a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
19153e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1916bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1917bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1918f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1919d442ad4aSStephen Hemminger 
19201a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
19211a33e10eSCong Wang 				 NULL);
19221a33e10eSCong Wang 
19234cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
19244cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
192591da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
192691da11f8SLennert Buytenhek 
192791da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
19286a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
19296a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
19305f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
19315f6b4e14SFlorian Fainelli 		return -ENOMEM;
19325f6b4e14SFlorian Fainelli 	}
1933e131a563SAlexander Lobakin 
1934e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1935e131a563SAlexander Lobakin 	if (ret)
1936e131a563SAlexander Lobakin 		goto out_free;
1937e131a563SAlexander Lobakin 
19384cfbf09cSVivien Didelot 	p->dp = port;
1939f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
1940f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
194153da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
194291da11f8SLennert Buytenhek 
1943bfcb8132SVladimir Oltean 	rtnl_lock();
1944bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1945bfcb8132SVladimir Oltean 	rtnl_unlock();
19464349abdbSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
1947bdc40a3fSRasmus Villemoes 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1948bdc40a3fSRasmus Villemoes 			 ret, ETH_DATA_LEN, port->index);
1949bfcb8132SVladimir Oltean 
195091da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
195191da11f8SLennert Buytenhek 
19524fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
19530071f56eSAndrew Lunn 	if (ret) {
1954c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
1955c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1956c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
1957e131a563SAlexander Lobakin 		goto out_gcells;
1958e804441cSFlorian Fainelli 	}
1959e804441cSFlorian Fainelli 
19602f1e8ea7SVladimir Oltean 	rtnl_lock();
19612f1e8ea7SVladimir Oltean 
19622f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1963e804441cSFlorian Fainelli 	if (ret) {
1964e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1965e804441cSFlorian Fainelli 			   ret, slave_dev->name);
19662f1e8ea7SVladimir Oltean 		rtnl_unlock();
1967e804441cSFlorian Fainelli 		goto out_phy;
19680071f56eSAndrew Lunn 	}
19690071f56eSAndrew Lunn 
19702f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
19712f1e8ea7SVladimir Oltean 
19722f1e8ea7SVladimir Oltean 	rtnl_unlock();
19732f1e8ea7SVladimir Oltean 
19742f1e8ea7SVladimir Oltean 	if (ret)
19752f1e8ea7SVladimir Oltean 		goto out_unregister;
19762f1e8ea7SVladimir Oltean 
1977d87d6f44SGuenter Roeck 	return 0;
1978e804441cSFlorian Fainelli 
19792f1e8ea7SVladimir Oltean out_unregister:
19802f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1981e804441cSFlorian Fainelli out_phy:
1982aab9c406SFlorian Fainelli 	rtnl_lock();
1983aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1984aab9c406SFlorian Fainelli 	rtnl_unlock();
1985aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1986e131a563SAlexander Lobakin out_gcells:
1987e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1988e804441cSFlorian Fainelli out_free:
19896a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1990e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1991f8b8b1cdSVivien Didelot 	port->slave = NULL;
1992e804441cSFlorian Fainelli 	return ret;
199391da11f8SLennert Buytenhek }
1994b73adef6SFlorian Fainelli 
1995cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1996cda5c15bSNeil Armstrong {
19972f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
1998d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1999cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2000cda5c15bSNeil Armstrong 
2001cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2002aab9c406SFlorian Fainelli 	rtnl_lock();
20032f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
20042f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2005aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2006aab9c406SFlorian Fainelli 	rtnl_unlock();
2007881eadabSJohan Hovold 
2008aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2009e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
20106a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2011cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2012cda5c15bSNeil Armstrong }
2013cda5c15bSNeil Armstrong 
20144d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2015b73adef6SFlorian Fainelli {
2016b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2017b73adef6SFlorian Fainelli }
2018a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2019b73adef6SFlorian Fainelli 
20208e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
20218e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2022b73adef6SFlorian Fainelli {
2023d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
20242afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
20258e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2026b73adef6SFlorian Fainelli 
20272afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20282afc526aSVladimir Oltean 
20298e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
20308e92ab3aSVivien Didelot 		if (info->linking) {
20312afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2032bff33f7eSVladimir Oltean 			if (!err)
2033bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
20348e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
20358e92ab3aSVivien Didelot 		} else {
203617d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
20378e92ab3aSVivien Didelot 			err = NOTIFY_OK;
20388e92ab3aSVivien Didelot 		}
2039058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2040058102a6STobias Waldekranz 		if (info->linking) {
2041058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
20422afc526aSVladimir Oltean 						info->upper_info, extack);
2043058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2044058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2045058102a6STobias Waldekranz 						   "Offloading not supported");
2046058102a6STobias Waldekranz 				err = 0;
2047058102a6STobias Waldekranz 			}
2048058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2049058102a6STobias Waldekranz 		} else {
2050058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2051058102a6STobias Waldekranz 			err = NOTIFY_OK;
2052058102a6STobias Waldekranz 		}
205318596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
205418596f50SGeorge McCollister 		if (info->linking) {
205518596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
205618596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
205718596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
205818596f50SGeorge McCollister 						   "Offloading not supported");
205918596f50SGeorge McCollister 				err = 0;
206018596f50SGeorge McCollister 			}
206118596f50SGeorge McCollister 			err = notifier_from_errno(err);
206218596f50SGeorge McCollister 		} else {
206318596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
206418596f50SGeorge McCollister 			err = NOTIFY_OK;
206518596f50SGeorge McCollister 		}
2066058102a6STobias Waldekranz 	}
2067058102a6STobias Waldekranz 
2068058102a6STobias Waldekranz 	return err;
2069058102a6STobias Waldekranz }
2070058102a6STobias Waldekranz 
2071058102a6STobias Waldekranz static int
2072058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2073058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2074058102a6STobias Waldekranz {
2075058102a6STobias Waldekranz 	struct net_device *lower;
2076058102a6STobias Waldekranz 	struct list_head *iter;
2077058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2078058102a6STobias Waldekranz 	struct dsa_port *dp;
2079058102a6STobias Waldekranz 
2080058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2081058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2082058102a6STobias Waldekranz 			continue;
2083058102a6STobias Waldekranz 
2084058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2085058102a6STobias Waldekranz 		if (!dp->lag_dev)
2086058102a6STobias Waldekranz 			/* Software LAG */
2087058102a6STobias Waldekranz 			continue;
2088058102a6STobias Waldekranz 
2089058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2090058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2091058102a6STobias Waldekranz 			break;
20926debb68aSVivien Didelot 	}
2093b73adef6SFlorian Fainelli 
20948e92ab3aSVivien Didelot 	return err;
2095b73adef6SFlorian Fainelli }
2096b73adef6SFlorian Fainelli 
2097eb46e8daSVladimir Oltean static int
2098eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2099eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2100cc1d5bdaSFlorian Fainelli {
2101cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
2102cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
2103cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2104cc1d5bdaSFlorian Fainelli 
2105cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2106cc1d5bdaSFlorian Fainelli 
2107cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2108cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2109cc1d5bdaSFlorian Fainelli 
2110cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2111cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2112cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2113cc1d5bdaSFlorian Fainelli 
2114cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
2115cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
2116cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2117cc1d5bdaSFlorian Fainelli 
2118cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2119cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
2120cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2121cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2122cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2123cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2124cc1d5bdaSFlorian Fainelli 	}
2125cc1d5bdaSFlorian Fainelli 
2126cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2127cc1d5bdaSFlorian Fainelli }
2128cc1d5bdaSFlorian Fainelli 
21292b138406SVladimir Oltean static int
21302b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
21312b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
21322b138406SVladimir Oltean {
21332b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
21342b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
21352b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
21362b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
21372b138406SVladimir Oltean 	int err = NOTIFY_DONE;
21382b138406SVladimir Oltean 	u16 vid;
21392b138406SVladimir Oltean 
2140adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
21412b138406SVladimir Oltean 		return NOTIFY_DONE;
21422b138406SVladimir Oltean 
21432b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
21442b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
21452b138406SVladimir Oltean 
21462b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
21472b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
21482b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
21492b138406SVladimir Oltean 	 */
21502b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
21512b138406SVladimir Oltean 	if (err == 0) {
21522b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
21532b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
21542b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
21552b138406SVladimir Oltean 	}
21562b138406SVladimir Oltean 
21572b138406SVladimir Oltean 	return NOTIFY_DONE;
21582b138406SVladimir Oltean }
21592b138406SVladimir Oltean 
216088e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
2161b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
2162b73adef6SFlorian Fainelli {
21636debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2164b73adef6SFlorian Fainelli 
216583501299SVladimir Oltean 	switch (event) {
21662b138406SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
21672b138406SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
2168e358bef7SVladimir Oltean 		struct dsa_switch *ds;
2169e358bef7SVladimir Oltean 		struct dsa_port *dp;
2170e358bef7SVladimir Oltean 		int err;
21712b138406SVladimir Oltean 
217253bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
2173eb46e8daSVladimir Oltean 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
21742b138406SVladimir Oltean 
2175e358bef7SVladimir Oltean 		dp = dsa_slave_to_port(dev);
2176e358bef7SVladimir Oltean 		ds = dp->ds;
2177e358bef7SVladimir Oltean 
2178e358bef7SVladimir Oltean 		if (ds->ops->port_prechangeupper) {
2179e358bef7SVladimir Oltean 			err = ds->ops->port_prechangeupper(ds, dp->index, info);
2180e358bef7SVladimir Oltean 			if (err)
2181e358bef7SVladimir Oltean 				return notifier_from_errno(err);
2182e358bef7SVladimir Oltean 		}
2183e358bef7SVladimir Oltean 
21842b138406SVladimir Oltean 		if (is_vlan_dev(info->upper_dev))
21852b138406SVladimir Oltean 			return dsa_slave_check_8021q_upper(dev, ptr);
218683501299SVladimir Oltean 		break;
21872b138406SVladimir Oltean 	}
218883501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2189058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
21908e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2191058102a6STobias Waldekranz 
2192058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2193058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2194058102a6STobias Waldekranz 
2195058102a6STobias Waldekranz 		break;
2196058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2197058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2198058102a6STobias Waldekranz 		struct dsa_port *dp;
2199058102a6STobias Waldekranz 		int err;
2200058102a6STobias Waldekranz 
2201058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2202058102a6STobias Waldekranz 			break;
2203058102a6STobias Waldekranz 
2204058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2205058102a6STobias Waldekranz 
2206058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2207058102a6STobias Waldekranz 		return notifier_from_errno(err);
2208058102a6STobias Waldekranz 	}
2209c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2210c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2211c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2212c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2213c0a8a9c2SVladimir Oltean 
2214c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2215c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2216c0a8a9c2SVladimir Oltean 
2217c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2218c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2219c0a8a9c2SVladimir Oltean 
2220c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2221c0a8a9c2SVladimir Oltean 			if (!dsa_is_user_port(dp->ds, dp->index))
2222c0a8a9c2SVladimir Oltean 				continue;
2223c0a8a9c2SVladimir Oltean 
2224c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2225c0a8a9c2SVladimir Oltean 		}
2226c0a8a9c2SVladimir Oltean 
2227c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2228c0a8a9c2SVladimir Oltean 
2229c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2230c0a8a9c2SVladimir Oltean 	}
2231c0a8a9c2SVladimir Oltean 	default:
2232c0a8a9c2SVladimir Oltean 		break;
2233cc1d5bdaSFlorian Fainelli 	}
2234b73adef6SFlorian Fainelli 
2235b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2236b73adef6SFlorian Fainelli }
223788e4f0caSVivien Didelot 
2238c4bb76a9SVladimir Oltean static void
2239c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2240c4bb76a9SVladimir Oltean {
2241c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2242c4bb76a9SVladimir Oltean 	struct switchdev_notifier_fdb_info info;
2243c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2244c4bb76a9SVladimir Oltean 
2245c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2246c4bb76a9SVladimir Oltean 		return;
2247c4bb76a9SVladimir Oltean 
2248c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2249c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2250c4bb76a9SVladimir Oltean 	info.offloaded = true;
2251c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2252c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2253c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2254c4bb76a9SVladimir Oltean }
2255c9eb3e0fSArkadi Sharshevsky 
2256c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2257c9eb3e0fSArkadi Sharshevsky {
2258c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2259c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2260c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2261c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2262c9eb3e0fSArkadi Sharshevsky 	int err;
2263c9eb3e0fSArkadi Sharshevsky 
2264c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2265c4bb76a9SVladimir Oltean 
2266c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2267c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2268c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2269c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2270c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2271c9eb3e0fSArkadi Sharshevsky 		if (err) {
2272c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2273c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2274c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2275c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2276c9eb3e0fSArkadi Sharshevsky 			break;
2277c9eb3e0fSArkadi Sharshevsky 		}
2278c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2279c9eb3e0fSArkadi Sharshevsky 		break;
2280c9eb3e0fSArkadi Sharshevsky 
2281c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2282c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2283c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2284c9eb3e0fSArkadi Sharshevsky 		if (err) {
2285c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2286c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2287c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2288c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2289c9eb3e0fSArkadi Sharshevsky 		}
22902fd18650SVladimir Oltean 
2291c9eb3e0fSArkadi Sharshevsky 		break;
2292c9eb3e0fSArkadi Sharshevsky 	}
2293c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2294c9eb3e0fSArkadi Sharshevsky 
2295c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2296c4bb76a9SVladimir Oltean 	if (dsa_is_user_port(ds, dp->index))
2297c4bb76a9SVladimir Oltean 		dev_put(dp->slave);
2298c9eb3e0fSArkadi Sharshevsky }
2299c9eb3e0fSArkadi Sharshevsky 
2300d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev,
2301d5f19486SVladimir Oltean 			      struct netdev_nested_priv *priv)
2302d5f19486SVladimir Oltean {
2303d5f19486SVladimir Oltean 	if (dsa_slave_dev_check(lower_dev)) {
2304d5f19486SVladimir Oltean 		priv->data = (void *)netdev_priv(lower_dev);
2305d5f19486SVladimir Oltean 		return 1;
2306d5f19486SVladimir Oltean 	}
2307d5f19486SVladimir Oltean 
2308d5f19486SVladimir Oltean 	return 0;
2309d5f19486SVladimir Oltean }
2310d5f19486SVladimir Oltean 
2311d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2312d5f19486SVladimir Oltean {
2313d5f19486SVladimir Oltean 	struct netdev_nested_priv priv = {
2314d5f19486SVladimir Oltean 		.data = NULL,
2315d5f19486SVladimir Oltean 	};
2316d5f19486SVladimir Oltean 
2317d5f19486SVladimir Oltean 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2318d5f19486SVladimir Oltean 
2319d5f19486SVladimir Oltean 	return (struct dsa_slave_priv *)priv.data;
2320d5f19486SVladimir Oltean }
2321d5f19486SVladimir Oltean 
2322c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2323c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2324c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2325c9eb3e0fSArkadi Sharshevsky {
2326c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2327c4bb76a9SVladimir Oltean 	const struct switchdev_notifier_fdb_info *fdb_info;
2328c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
2329c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
233079b139f4SVivien Didelot 	int err;
233179b139f4SVivien Didelot 
2332447d290aSVladimir Oltean 	switch (event) {
2333447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
233479b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
233579b139f4SVivien Didelot 						     dsa_slave_dev_check,
233679b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
233779b139f4SVivien Didelot 		return notifier_from_errno(err);
2338447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2339447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2340d5f19486SVladimir Oltean 		fdb_info = ptr;
2341d5f19486SVladimir Oltean 
2342d5f19486SVladimir Oltean 		if (dsa_slave_dev_check(dev)) {
23432c4eca3eSVladimir Oltean 			if (!fdb_info->added_by_user || fdb_info->is_local)
2344d5f19486SVladimir Oltean 				return NOTIFY_OK;
2345c9eb3e0fSArkadi Sharshevsky 
2346c4bb76a9SVladimir Oltean 			dp = dsa_slave_to_port(dev);
2347d5f19486SVladimir Oltean 		} else {
2348d5f19486SVladimir Oltean 			/* Snoop addresses learnt on foreign interfaces
2349d5f19486SVladimir Oltean 			 * bridged with us, for switches that don't
2350d5f19486SVladimir Oltean 			 * automatically learn SA from CPU-injected traffic
2351d5f19486SVladimir Oltean 			 */
2352d5f19486SVladimir Oltean 			struct net_device *br_dev;
2353d5f19486SVladimir Oltean 			struct dsa_slave_priv *p;
2354d5f19486SVladimir Oltean 
2355d5f19486SVladimir Oltean 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2356d5f19486SVladimir Oltean 			if (!br_dev)
2357d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2358d5f19486SVladimir Oltean 
2359d5f19486SVladimir Oltean 			if (!netif_is_bridge_master(br_dev))
2360d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2361d5f19486SVladimir Oltean 
2362d5f19486SVladimir Oltean 			p = dsa_slave_dev_lower_find(br_dev);
2363d5f19486SVladimir Oltean 			if (!p)
2364d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2365d5f19486SVladimir Oltean 
2366d5f19486SVladimir Oltean 			dp = p->dp->cpu_dp;
2367d5f19486SVladimir Oltean 
2368d5f19486SVladimir Oltean 			if (!dp->ds->assisted_learning_on_cpu_port)
2369d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2370a324d3d4SVladimir Oltean 
2371a324d3d4SVladimir Oltean 			/* When the bridge learns an address on an offloaded
2372a324d3d4SVladimir Oltean 			 * LAG we don't want to send traffic to the CPU, the
2373a324d3d4SVladimir Oltean 			 * other ports bridged with the LAG should be able to
2374a324d3d4SVladimir Oltean 			 * autonomously forward towards it.
2375a324d3d4SVladimir Oltean 			 */
237603cbb870SVladimir Oltean 			if (dsa_tree_offloads_bridge_port(dp->ds->dst, dev))
2377a324d3d4SVladimir Oltean 				return NOTIFY_DONE;
2378d5f19486SVladimir Oltean 		}
2379c4bb76a9SVladimir Oltean 
23805fb4a451SVladimir Oltean 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
23815fb4a451SVladimir Oltean 			return NOTIFY_DONE;
23825fb4a451SVladimir Oltean 
2383c9eb3e0fSArkadi Sharshevsky 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2384c9eb3e0fSArkadi Sharshevsky 		if (!switchdev_work)
2385c9eb3e0fSArkadi Sharshevsky 			return NOTIFY_BAD;
2386c9eb3e0fSArkadi Sharshevsky 
2387c9eb3e0fSArkadi Sharshevsky 		INIT_WORK(&switchdev_work->work,
2388c9eb3e0fSArkadi Sharshevsky 			  dsa_slave_switchdev_event_work);
2389c4bb76a9SVladimir Oltean 		switchdev_work->ds = dp->ds;
2390c4bb76a9SVladimir Oltean 		switchdev_work->port = dp->index;
2391c9eb3e0fSArkadi Sharshevsky 		switchdev_work->event = event;
2392c9eb3e0fSArkadi Sharshevsky 
2393c4bb76a9SVladimir Oltean 		ether_addr_copy(switchdev_work->addr,
2394c4bb76a9SVladimir Oltean 				fdb_info->addr);
2395c4bb76a9SVladimir Oltean 		switchdev_work->vid = fdb_info->vid;
2396c4bb76a9SVladimir Oltean 
2397d5f19486SVladimir Oltean 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2398d5f19486SVladimir Oltean 		if (dsa_is_user_port(dp->ds, dp->index))
2399c9eb3e0fSArkadi Sharshevsky 			dev_hold(dev);
2400447d290aSVladimir Oltean 		dsa_schedule_work(&switchdev_work->work);
2401c9eb3e0fSArkadi Sharshevsky 		break;
2402c9eb3e0fSArkadi Sharshevsky 	default:
2403c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2404c9eb3e0fSArkadi Sharshevsky 	}
2405c9eb3e0fSArkadi Sharshevsky 
2406c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2407c9eb3e0fSArkadi Sharshevsky }
2408c9eb3e0fSArkadi Sharshevsky 
24092b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
24102b239f67SPetr Machata 					      unsigned long event, void *ptr)
24112b239f67SPetr Machata {
24122b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
241379b139f4SVivien Didelot 	int err;
24142b239f67SPetr Machata 
24152b239f67SPetr Machata 	switch (event) {
241679b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
241779b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
241879b139f4SVivien Didelot 						    dsa_slave_dev_check,
241979b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
242079b139f4SVivien Didelot 		return notifier_from_errno(err);
24212b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
242279b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
242379b139f4SVivien Didelot 						    dsa_slave_dev_check,
242479b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
242579b139f4SVivien Didelot 		return notifier_from_errno(err);
24269ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
242779b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
242879b139f4SVivien Didelot 						     dsa_slave_dev_check,
242979b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
243079b139f4SVivien Didelot 		return notifier_from_errno(err);
24312b239f67SPetr Machata 	}
24322b239f67SPetr Machata 
24332b239f67SPetr Machata 	return NOTIFY_DONE;
24342b239f67SPetr Machata }
24352b239f67SPetr Machata 
243688e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
243788e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
243888e4f0caSVivien Didelot };
243988e4f0caSVivien Didelot 
2440010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2441c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2442c9eb3e0fSArkadi Sharshevsky };
2443c9eb3e0fSArkadi Sharshevsky 
2444010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
24452b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
24462b239f67SPetr Machata };
24472b239f67SPetr Machata 
244888e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
244988e4f0caSVivien Didelot {
24502b239f67SPetr Machata 	struct notifier_block *nb;
2451c9eb3e0fSArkadi Sharshevsky 	int err;
2452c9eb3e0fSArkadi Sharshevsky 
2453c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2454c9eb3e0fSArkadi Sharshevsky 	if (err)
2455c9eb3e0fSArkadi Sharshevsky 		return err;
2456c9eb3e0fSArkadi Sharshevsky 
2457c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2458c9eb3e0fSArkadi Sharshevsky 	if (err)
2459c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2460c9eb3e0fSArkadi Sharshevsky 
24612b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
24622b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
24632b239f67SPetr Machata 	if (err)
24642b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
24652b239f67SPetr Machata 
2466c9eb3e0fSArkadi Sharshevsky 	return 0;
2467c9eb3e0fSArkadi Sharshevsky 
24682b239f67SPetr Machata err_switchdev_blocking_nb:
24692b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2470c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2471c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2472c9eb3e0fSArkadi Sharshevsky 	return err;
247388e4f0caSVivien Didelot }
247488e4f0caSVivien Didelot 
247588e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
247688e4f0caSVivien Didelot {
24772b239f67SPetr Machata 	struct notifier_block *nb;
247888e4f0caSVivien Didelot 	int err;
247988e4f0caSVivien Didelot 
24802b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
24812b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
24822b239f67SPetr Machata 	if (err)
24832b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
24842b239f67SPetr Machata 
2485c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2486c9eb3e0fSArkadi Sharshevsky 	if (err)
2487c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2488c9eb3e0fSArkadi Sharshevsky 
248988e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
249088e4f0caSVivien Didelot 	if (err)
249188e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
249288e4f0caSVivien Didelot }
2493